[Numpy-discussion] The idiom for supporting matrices and arrays in a function

Colin J. Williams cjw at sympatico.ca
Thu Mar 2 05:39:01 CST 2006

Travis Oliphant wrote:

> 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(...)

How does asanyyarray() differ from asarray()?

Colin W.

> 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
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting 
> language
> that extends applications into web and mobile media. Attend the live 
> webcast
> and join the prime developer group breaking into this new coding 
> territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

More information about the Numpy-discussion mailing list