[Numpy-discussion] Is there a memory-efficient alternative to choose?
Josh Hykes
jmhykes@ncsu....
Wed Mar 9 09:58:06 CST 2011
>
> Would the "out" keyword of np.choose be a possible solution? Using that,
> you should be able to create the properies_map in place, and perhaps you can
> then do the multiplication with the state variables in place also:
Thanks, Lutz, for this suggestion. I hadn't thought of using the "out"
keyword. This should do the trick for a number of instances in my code.
Unfortunately, I'm not sure that it will work for the more complicated
expressions. The problem is that the properties_map array can be larger than
the answer array. For example, each property could be a matrix that gets
multiplied by a vector of state variables in each cell. For example:
import numpy as np
p0 = np.array([[1., 0.], [1., 1.]]) # matrix properties
p1 = np.array([[1., 1.], [0., 1.]])
properties = {0: p0, 1: p1}
mesh_map = np.array([[0,0,0,0],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0]])
state_variables = np.arange(mesh_map.size*2).reshape(mesh_map.shape+(2,))
answer = np.zeros_like(state_variables)
for i in range(mesh_map.shape[0]):
for j in range(mesh_map.shape[1]):
m = mesh_map[i,j]
answer[i,j,:] = np.dot(properties[m], state_variables[i,j,:])
I was envisioning a sort of generator function defined through Numpy that
would operate at C speed, maybe something like:
properties_map = np.choose_generator(mesh_map, [properties[0],
properties[1]])
answer = np.tensordot(properties_map, state_variables, axes=([3,],[2]))
This may not be possible without using f2py or Cython. I just figured I'd
ask the experts before going that route.
Thanks again.
-Josh
On Tue, Mar 8, 2011 at 4:53 PM, Lutz Maibaum <lutz.maibaum@gmail.com> wrote:
> On Mar 8, 2011, at 11:56 AM, Josh Hykes wrote:
> > At some point in my code, I need to do a cell-wise multiplication of the
> properties with a state variable. The ideal method would (1) be fast (no
> Python loops) and (2) not waste memory constructing an entire property map.
> My best attempt using choose does (1) but not (2).
>
> Would the "out" keyword of np.choose be a possible solution? Using that,
> you should be able to create the properies_map in place, and perhaps you can
> then do the multiplication with the state variables in place also:
>
> import numpy as np
> properties = {0: 0.5, 1: 2.} # 2 materials
> mesh_map = np.array([[0,0,0,0], [0,1,1,0], [0,1,1,0], [0,0,0,0]]) # 4x4
> mesh
> state_variables = np.arange(mesh_map.size).reshape(mesh_map.shape) # fake
> state variables
> answer = np.empty (mesh_map.shape, np.float) # do this only once with
> dtype of properties
> np.choose(mesh_map, (properties[0], properties[1]), out=answer)
> answer[:] = answer * state_variables
>
> Hope this helps,
>
> Lutz
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110309/84fdbc11/attachment-0001.html
More information about the NumPy-Discussion
mailing list