Hygenic Macros

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Oct 19 01:28:32 CEST 2005


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.




More information about the Python-list mailing list