evaluating a function in a matrix element???
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 prechosen 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 doable 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@horow.net
2010/3/18 Frank Horowitz <frank@horow.net>:
I'm working on a piece of optimisation code where it turns out to be mathematically convenient to have a matrix where a few prechosen elements must be computed at evaluation time for the dot product (i.e. matrix multiplication) of a matrix with a vector.
The following *might* be helpful:
class X: ... def __mul__(self, other): ... return numpy.random.random() * other ... def __rmul__(self, other): ... return other * numpy.random.random()
Instances of this class calculate the product intime:
x = X() x * 1 0.222103712078775 1 * x 0.044647569053175573
How to use it:
a = numpy.asarray([[X(), X()], [0, 1]]) a array([[<__main__.X instance at 0x00AABAA8>, <__main__.X instance at 0x00E76530>], [0, 1]], dtype=object)
The first row is purely random, the second purely deterministic:
numpy.dot(a, [1, 2]) array([1.60154958363, 2], dtype=object) numpy.dot(a, [1, 2]) array([2.06294335235, 2], dtype=object)
You can convert back to dtype = numpy.float by result.astype(numpy.float):
numpy.dot(a, [1, 2]).astype(numpy.float) array([ 1.33217562, 2. ])
Friedrich
On Thu, Mar 18, 2010 at 7:01 AM, Frank Horowitz <frank@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 prechosen 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 doable 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. Sebastian
Thanks in advance for any help you might be able to provide!
Frank Horowitz frank@horow.net
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
participants (3)

Frank Horowitz

Friedrich Romstedt

Sebastian Walter