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

Bill Baxter wbaxter at gmail.com
Wed Mar 1 16:22:02 CST 2006


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?

My guess is it depends first off on whether your goal is
- to treat everything as a matrix, but also accept 2D arrays and do the
conversion to matrix internally (asmatrix())
- to treat everything as a general array, but also accept matrices as a
special case of 2D array, converting to arrays

I guess it's likely that you'd also like to make it so the type returned is
the same as the type input.  Is there some slick way to do that?

It doesn't seem to me that the latter case should really be specific to
matrix/array since matrix is just one particular subclass of array. Is this
what those UFuncs are for?  Is there some way to write functions that will
accept any object that supports the "array protocal"?

This comment about the idiom for matrix/array functions is the last major
wart remaining on that wiki page, so I'd like to wipe it out.

--bb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060301/3f2d55f0/attachment.html 


More information about the Numpy-discussion mailing list