There's a numpy.abs but no numpy.matlib.abs.
import numpy as N import numpy.matlib as M
N.abs? Type: ufunc Base Class: <type 'numpy.ufunc'> String Form: <ufunc 'absolute'> Namespace: Interactive Docstring: y = absolute(x) takes |x| elementwise.
M.abs? Object `M.abs` not found.
Keith Goodman wrote:
There's a numpy.abs but no numpy.matlib.abs.
import numpy as N import numpy.matlib as M
N.abs? Type: ufunc Base Class: <type 'numpy.ufunc'> String Form: <ufunc 'absolute'> Namespace: Interactive Docstring: y = absolute(x) takes |x| elementwise.
M.abs? Object `M.abs` not found.
numpy.abs() is not exported via "from numpy import *", which is where numpy.matlib gets all of its non-overridden functions from. It is not exported because it conflicts with the builtin abs(). Of course, absolute() is preferred for the same reason, and numpy.matlib.absolute() does exist. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On 2/4/07, Robert Kern <robert.kern@gmail.com> wrote:
Keith Goodman wrote:
There's a numpy.abs but no numpy.matlib.abs.
import numpy as N import numpy.matlib as M
N.abs? Type: ufunc Base Class: <type 'numpy.ufunc'> String Form: <ufunc 'absolute'> Namespace: Interactive Docstring: y = absolute(x) takes |x| elementwise.
M.abs? Object `M.abs` not found.
numpy.abs() is not exported via "from numpy import *", which is where numpy.matlib gets all of its non-overridden functions from. It is not exported because it conflicts with the builtin abs().
Of course, absolute() is preferred for the same reason, and numpy.matlib.absolute() does exist.
Could numpy.matlib get the same functions as numpy? Would that have to be done with a manually maintained import list? I always use "import numpy.matlib as M" and then search for function names in ipython (M.a[TAB]). I didn't realize that some functions are missing.
Em Dom, 2007-02-04 às 17:28 -0800, Keith Goodman escreveu:
Could numpy.matlib get the same functions as numpy? Would that have to be done with a manually maintained import list? I always use "import numpy.matlib as M" and then search for function names in ipython (M.a[TAB]). I didn't realize that some functions are missing.
As the list knows, I am trying to build a special module that can convert any other module to behave nicely with matrices. I have special interest in using it as an interface to scipy modules that may return arrays when given a matrix. This effort let me to learn some tricks about modules imports in Python. I believe that if you add the following code to the end of matlib.py file it will behave just like you want without any manual intervention: --- Start Python code --- import inspect import matlib as M for i in dir(N): attribute = getattr(N, i) if type(attribute) is N.ufunc or inspect.isroutine(attribute): try: getattr(M, i) except AttributeError: setattr(M, i, attribute) --- End Python code --- Here is an ipython session: --- ipython session --- In [1]:import numpy.matlib as M In [2]:M.abs Out[2]:<ufunc 'absolute'> --- End of ipython sesssion --- By the way, there were only four functions that are missing without this code: abs, max, min, and round. You can see this by adding a "print i" in the except block above. If the list thinks this code is useful, I am donating it to numpy. Best, Paulo
On 2/6/07, Paulo J. S. Silva <pjssilva@ime.usp.br> wrote:
Em Dom, 2007-02-04 às 17:28 -0800, Keith Goodman escreveu:
Could numpy.matlib get the same functions as numpy? Would that have to be done with a manually maintained import list? I always use "import numpy.matlib as M" and then search for function names in ipython (M.a[TAB]). I didn't realize that some functions are missing.
As the list knows, I am trying to build a special module that can convert any other module to behave nicely with matrices. I have special interest in using it as an interface to scipy modules that may return arrays when given a matrix. This effort let me to learn some tricks about modules imports in Python.
I believe that if you add the following code to the end of matlib.py file it will behave just like you want without any manual intervention:
--- Start Python code ---
import inspect import matlib as M for i in dir(N): attribute = getattr(N, i) if type(attribute) is N.ufunc or inspect.isroutine(attribute): try: getattr(M, i) except AttributeError: setattr(M, i, attribute)
--- End Python code ---
Here is an ipython session:
--- ipython session ---
In [1]:import numpy.matlib as M
In [2]:M.abs Out[2]:<ufunc 'absolute'>
--- End of ipython sesssion ---
By the way, there were only four functions that are missing without this code: abs, max, min, and round. You can see this by adding a "print i" in the except block above.
If the list thinks this code is useful, I am donating it to numpy.
That is great. I could think of a few uses for abs, max, min, and round. So I would like to see them imported. BTW, why can I do x.max() x.min() x.round() but not x.abs()?
participants (3)
-
Keith Goodman -
Paulo J. S. Silva -
Robert Kern