473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hygenic Macros

Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.

Thanks,
David
Oct 18 '05 #1
6 1589
David Pokorny wrote:
Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.


Well, dot(A, B) is better. But if you must:

http://aspn.activestate.com/ASPN/Coo.../Recipe/384122

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Oct 18 '05 #2
David Pokorny wrote:
Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.

Thanks,
David


The problem here is that Python's parse trees are of non-trivial ugliness.

A page on the compiler.ast module:
http://docs.python.org/lib/node792.html

it is, in fact, perfectly possible to write yourself a pre-processor for
your particular application. You may have to fiddle with the token you
want for notation depending on how the AST fleshes out (% is used by at
least a couple of things, after all). My cursory familiarity with
python grammar suggests to me that this particular choice of token could
be a problem.

I would say try it and see. Keep in mind though that since Python's AST
is not a trivial matter like it is in Lisp and the like that doing
metaprogramming of this sort probably falls into the category of black
magic unless it turns out to be very trivial.

Another option is to define your own tiny class that will override the
__mult__ method so that you can simply do:

A * B

Which may not be what you want.

df
Oct 18 '05 #3
On Mon, 17 Oct 2005 22:23:43 -0700, David Pokorny wrote:
Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.


Yes, I see what you mean, it is pretty confusing. It almost looks like
a function that multiplies two matrices and returns the result.

Have you tried coming up with better names for your arguments than A and
B? Many people find that using self-documenting variable names helps make
code easier to understand.
--
Steven.

Oct 18 '05 #4
Steven D'Aprano wrote:
On Mon, 17 Oct 2005 22:23:43 -0700, David Pokorny wrote:
Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.


Yes, I see what you mean, it is pretty confusing. It almost looks like
a function that multiplies two matrices and returns the result.

Have you tried coming up with better names for your arguments than A and
B? Many people find that using self-documenting variable names helps make
code easier to understand.


Well, to be fair, his example was trivial. When you have more
complicated matrix expressions with transposes and conjugations and more
matrixmultiplies than you can shake a stick at, it gets ugly pretty fast.

F = dot(dot(Z, F),transpose(conjugate(Z)))

versus

from scipy import *
F = mat(F)
Z = mat(Z)
F = Z*F*Z.H

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Oct 18 '05 #5
On Tue, 18 Oct 2005 13:42:21 -0700, Robert Kern wrote:
Steven D'Aprano wrote:
On Mon, 17 Oct 2005 22:23:43 -0700, David Pokorny wrote:
Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.
Yes, I see what you mean, it is pretty confusing. It almost looks like
a function that multiplies two matrices and returns the result.

Have you tried coming up with better names for your arguments than A and
B? Many people find that using self-documenting variable names helps make
code easier to understand.


Well, to be fair, his example was trivial. When you have more
complicated matrix expressions with transposes and conjugations and more
matrixmultiplies than you can shake a stick at, it gets ugly pretty fast.

F = dot(dot(Z, F),transpose(conjugate(Z)))


No uglier than y = sin(poly(sqrt(x)))

And of course it is allowed to do this:

F = dot(Z, F)
Z = transpose(conjugate(Z))
F = dot(F, Z)

Not everything has to be a one-liner, not even in mathematics.

versus

from scipy import *
F = mat(F)
Z = mat(Z)
F = Z*F*Z.H


It's a matter of taste. But calling it "almost unreadable" is an
exaggeration.
--
Steven.

Oct 18 '05 #6
Hi,

Thanks - this cookbook entry is very cool!

I am somewhat worried about function call overhead from the infix hack
though... on second consideration, the infix issue is not as important
as eventually boosting the speed of the inner loop to which
matrixmultiply() belongs. For those who are interested in things like
fast math, I found

http://www.scipy.org/documentation/w...rformance.html

very eye-opening. For many of the examples cited there, it is possible
to simply treat the source as a string and then do the necessary macro
transformations before passing it off to a compiler...

David

Robert Kern wrote:
David Pokorny wrote:
Hi,

Just wondering if anyone has considered macros for Python. I have one
good use case. In "R", the statistical programming language, you can
multiply matrices with A %*% B (A*B corresponds to pointwise
multiplication). In Python, I have to type

import Numeric
matrixmultiply(A,B)

which makes my code almost unreadable.

Well, dot(A, B) is better. But if you must:

http://aspn.activestate.com/ASPN/Coo.../Recipe/384122

Oct 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
16
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
3
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v +=...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.