# [Numpy-discussion] Is there a memory-efficient alternative to choose?

Josh Hykes jmhykes@ncsu....
Tue Mar 8 13:56:46 CST 2011

```Hello,

I am writing a small PDE code in Python using a Cartesian mesh. Each mesh
cell is one of a few materials, with associated properties. I store these
properties in a dictionary and have a "mesh map" that tells me which
material is in each cell.

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).

To make things concrete, a simple 2D example is:

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

>>> properties_map = np.choose(mesh_map, (properties[0], properties[1]))

>>> state_variables = np.arange(mesh_map.size).reshape(mesh_map.shape) #
fake state variables

>>> answer = properties_map * state_variables

Can I get answer without storing properties_map? This seems like a common
problem, but I haven't found a pure Numpy solution. I can't seem to find the
correct keywords in Google to find what I'm looking for.

Two further notes: First, I'll be computing answer many times, but
properties_map shouldn't change. Second, in the real problem, the properties
are themselves vectors or matrices, thus my desire to avoid storing them
repeatedly.

Thanks for any insights or pointers.

-Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110308/69152cf2/attachment.html
```