473,835 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1605
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
multiplicatio n). In Python, I have to type

import Numeric
matrixmultipl y(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
matrixmultiplie s than you can shake a stick at, it gets ugly pretty fast.

F = dot(dot(Z, F),transpose(co njugate(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
multiplicati on). In Python, I have to type

import Numeric
matrixmultip ly(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
matrixmultiplie s than you can shake a stick at, it gets ugly pretty fast.

F = dot(dot(Z, F),transpose(co njugate(Z)))


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

And of course it is allowed to do this:

F = dot(Z, F)
Z = transpose(conju gate(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
multiplicatio n). In Python, I have to type

import Numeric
matrixmultipl y(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
3000
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 spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
699
34345
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
16
2429
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
2821
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 thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
8
7241
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 their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with numeric and text literals? Consider a number that plays a significant role in the implementation of...
3
2669
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 += *((s)->p++) << 8; } #define in_uint32_le(s,v) { in_uint16_le(s,v) \ v += *((s)->p++) << 16; v += *((s)->p++) << 24; } I'm personally not fond of function-like macros and wanted to turn these into static inline functions, but I'm having trouble doing...
47
32929
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 values only. Of course it is not THAT neccessary functionality, but it could be very useful. greetz Emil
11
22385
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 type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in advance -
33
8911
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 write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should be used instead of macros? Multi-statement macros, for example?
0
9808
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10523
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10561
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7766
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3995
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3089
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.