Re: Hygenic Macros
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/weave/weaveperformance.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/Cookbook/Python/Recipe/384122 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
Using numarray/pylab there's also dot: from pylab import * A = array(range(10)) B = array(range(10)) A * B [ 0, 1, 4, 9,16,25,36,49,64,81,] dot(A, B) 285 It might also make your code more readable. I would like A dot B, but even using ipython I can only get as close as dot A, B Dan Farina [EMAIL PROTECTED] 10/18/05 1:33 pm 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 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
I seem to remember a rather ugly hack at some point in the past that created a new operator like so A |dot| B where dot was an object which had the OR operator for left and right arguments redefined seperately so that it only made sense when used in that syntax. I guess you could hack something together along the same lines. I just wish I could remember what it was called, it's on the ActiveState Cookbook somewhere. On 18 Oct 2005, at 13:17, Adriaan Renting wrote: Using numarray/pylab there's also dot: from pylab import * A = array(range(10)) B = array(range(10)) A * B [ 0, 1, 4, 9,16,25,36,49,64,81,] dot(A, B) 285 It might also make your code more readable. I would like A dot B, but even using ipython I can only get as close as dot A, B Dan Farina [EMAIL PROTECTED] 10/18/05 1:33 pm 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 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
Ahar got it http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122 Would something like that be any use? On 18 Oct 2005, at 13:21, Alex Stapleton wrote: I seem to remember a rather ugly hack at some point in the past that created a new operator like so A |dot| B where dot was an object which had the OR operator for left and right arguments redefined seperately so that it only made sense when used in that syntax. I guess you could hack something together along the same lines. I just wish I could remember what it was called, it's on the ActiveState Cookbook somewhere. On 18 Oct 2005, at 13:17, Adriaan Renting wrote: Using numarray/pylab there's also dot: from pylab import * A = array(range(10)) B = array(range(10)) A * B [ 0, 1, 4, 9,16,25,36,49,64,81,] dot(A, B) 285 It might also make your code more readable. I would like A dot B, but even using ipython I can only get as close as dot A, B Dan Farina [EMAIL PROTECTED] 10/18/05 1:33 pm 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 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
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 [EMAIL PROTECTED] In the fields of hell where the grass grows high Are the graves of dreams allowed to die. -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hygenic Macros
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/Cookbook/Python/Recipe/384122 -- Robert Kern [EMAIL PROTECTED] In the fields of hell where the grass grows high Are the graves of dreams allowed to die. -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list