[Numpy-discussion] evaluating a function in a matrix element???

Frank Horowitz frank at horow.net
Thu Mar 18 02:01:15 EDT 2010

Dear All,

I'm working on a piece of optimisation code where it turns out to be mathematically convenient to have a matrix where a few pre-chosen elements must be computed at evaluation time for the dot product (i.e. matrix multiplication) of a matrix with a vector.

As I see the problem, there are two basic approaches to accomplishing this. 

First (and perhaps conceptually simplest, not to mention apparently faster) might be to stash appropriate functions at their corresponding locations in the matrix (with the rest of the matrix being constants, as usual). I mucked around with this for a little while in iPython, and it appears that having dtype == object_ works for stashing the references to the functions, but fails to allow me to actually evaluate the function(s) when the matrix is used in the dot product.

Does anyone have any experience with making such a beast work within numpy? If so, how?

The second basic approach is to build a ufunc that implements the switching logic, and returns the constants and evaluated functions in the appropriate locations.  This seems to be the more do-able approach, but it requires the ufunc to be aware of both the position of each element (via index, or somesuch) as well as the arguments to the functions themselves being evaluated at the matrix elements. It appears that frompyfunc() nearly does what I want, but I am currently failing to see how to actually *use* it for anything more elaborate than the octal example code (i.e. one value in, and one value out). Does anyone have any other more elaborate examples they can point me towards?

Thanks in advance for any help you might be able to provide!

	Frank Horowitz
	frank at horow.net

More information about the NumPy-Discussion mailing list