[Numpy-discussion] The idiom for supporting matrices and arrays in a function
Travis Oliphant
oliphant at ee.byu.edu
Wed Mar 1 16:46:02 EST 2006
Bill Baxter wrote:
> The NumPy for Matlab Users's wiki is currently pleading to have
> someone fill in "/*the idiom for supporting both matrices and arrays
> in a function".
> */Does anyone know what this is?
I'm not sure what they want, exactly.
Since a matrix is a subclass of an array you can treat them the same for
the most part. The * and ** operators are the two that act differently
on arrays and matrices so if you want to be sure then you use the
functions dot and umath.multiply instead of the infix notation.
If you want to convert arbitrary objects to "some-kind of array" then
asanyarray(...)
allows sub-classes of the array to pass to your function. To be
perfectly careful, however, you will need to use explicity functions to
perform any operation you may need.
The other approach is to store the __array_wrap__ method of the input
object (if there is any), convert everything to arrays using asarray()
and then wrap the final result --- this is what ufuncs do internally.
Again, I'm not sure what they mean?
-Travis
More information about the NumPy-Discussion
mailing list