[Numpy-discussion] numpy.matlib.abs

Paulo J. S. Silva pjssilva at ime.usp.br
Tue Feb 6 05:30:26 EST 2007


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




More information about the NumPy-Discussion mailing list