title: Python eval function with numpy arrays via string input with dictionaries
body: <p>I am implementing the code in python which has the variables stored in numpy vectors. I need to perform simple operation: something like (vec1+vec2^2)/vec3. Each element of each vector is summed and multiplied. (analog of MATLAB elementwise .* operation). </p>
  
  <p>The problem is in my code that I have dictionary which stores all vectors:</p>
  
  <pre><code>    var = {'a':np.array([1,2,2]),'b':np.array([2,1,3]),'c':np.array([3])}
  </code></pre>
  
  <p>The 3rd vector is just 1 number which means that I want to multiply this number by each element in other arrays like 3*[1,2,3]. And at the same time I have formula which is provided as a string:</p>
  
  <pre><code>    formula = '2*a*(b/c)**2'
  </code></pre>
  
  <p>I am replacing the formula using Regexp:</p>
  
  <pre><code>    formula_for_dict_variables = re.sub(r'([A-z][A-z0-9]*)', r'%(\1)s', formula)
  </code></pre>
  
  <p>which produces result:</p>
  
  <pre><code>    2*%(a)s*(%(b)s/%(c)s)**2
  </code></pre>
  
  <p>and substitute the dictionary variables:</p>
  
  <pre><code>    eval(formula%var)
  </code></pre>
  
  <p>In the case then I have just pure numbers (Not numpy arrays) everything is working, but when I place numpy.arrays in dict I receive an error.</p>
  
  <ol>
  <li><p>Could you give an example how can I solve this problem or maybe suggest some different approach. Given that vectors are stored in dictionary and formula is a string input. </p></li>
  <li><p>I also can store variables in any other container. The problem is that I don't know the name of variables and formula before the execution of code (they are provided by user).</p></li>
  <li><p>Also I think iteration through each element in vectors probably will be slow given the python for loops are slow. </p></li>
  </ol>