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

Sebastian Walter sebastian.walter at gmail.com
Fri Mar 19 04:35:39 EDT 2010

On Thu, Mar 18, 2010 at 7:01 AM, Frank Horowitz <frank at horow.net> wrote:
> 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.

Why don't you just override dot to compute those array elements and
then internally call numpy.dot?

def dot(x,y):
    update some elements of x
    return numpy.dot(x,y)

> 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?

Could you elaborate on why you think that an array of objects is
faster than an array of floats?

> 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?
I didn't know that dot is a ufunc. According to
http://docs.scipy.org/doc/numpy/reference/ufuncs.html a ufunc is a
function that operates element wise.


> Thanks in advance for any help you might be able to provide!
>        Frank Horowitz
>        frank at horow.net
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list