title: Read array from input file and optimise variables Python
body: <p>I have a simple Python code which reads two matrices in from a file, and each matrix element contains two undefined variables, A and B. After being read in, A and B are assigned values and the string is evaluated using <code>eval()</code> (I am aware of the security issues with this, but this code will only ever be used by me). A <code>numpy</code> array is formed for each of the matrices and the general eigenvalue problem is solved using both matrices. The code is as follows:</p>
  
  <pre><code>from __future__ import division 
  from scipy.linalg import eigh
  import numpy as np
  
  Mat_size = 4 ## Define matrix size
  
  ## Input the two matrices from each side of the generalised eigenvalue problem
  HHfile = "Matrix_4x4HH.mat" ## left
  SSfile = "Matrix_4x4SS.mat" ## right
  
  ## Read matrix files
  f1 = open(HHfile, 'r') 
  HH_data = f1.read()
  
  f2 = open(SSfile, 'r') 
  SS_data = f2.read() 
  
  # Define dictionary of variables
  trans = {'A':2.1,
           'B':3,
          }
  
  HHmat = eval(HH_data, trans)
  SSmat = eval(SS_data, trans)
  
  ## Convert to numpy array
  HHmat2 = np.array(HHmat)
  SSmat2 = np.array(SSmat)
  
  ## Form correct matrix dimensions
  shape = ( Mat_size, Mat_size )
  HH = HHmat2.reshape( shape )
  SS = SSmat2.reshape( shape )  
  ev, ew = eigh(HH,SS)
  
  ## solve eigenvalue problem
  eigenValues,eigenVectors = eigh(HH,SS)
  print("Eigenvalue:",eigenValues[0])
  </code></pre>
  
  <p>This works and outputs an answer. However I wish to vary the parameters A and B to minimise the value that it gives me. (Something like e.g.): </p>
  
  <pre><code>def func_min(x):
      A, B = x
      ev, ew=eigh(HH,SS)
      return ev[0]
  
  x0 = np.array([2.1, 3])  # Preliminary guess
  
  minimize(func_min, x0)
  </code></pre>
  
  <p>I am having an issue with the fact that A and B have to be defined in order for the <code>eval</code> call to work; thus disallowing the optimisation routine as A and B cannot be varied since they have specified values. I have tried a few different methods of reading in the files (pandas, csv, np.genfromtxt etc...) but always seem to be left with the same issue. Is there a better way, or a modification to the current code to implement this?</p>
  
  <p>Here are the inputs from the files. I was unsure of how to include them, but I can use a file upload service if that is better.</p>
  
  <p>Input from Matrix_4x4HH.mat</p>
  
  <pre><code>-.7500000000000000/B**3*A**5/(A+B)**6-4.500000000000000/B**2*A**4/(A+B)**6-12./B*A**3/(A+B)**6-19.50000000000000*A**2/(A+B)**6-118.5000000000000*B*A/(A+B)**6-19.50000000000000*B**2/(A+B)**6-12.*B**3/A/(A+B)**6-4.500000000000000*B**4/A**2/(A+B)**6-.7500000000000000*B**5/A**3/(A+B)**6+2./B**3*A**4/(A+B)**6+13./B**2*A**3/(A+B)**6+36./B*A**2/(A+B)**6+165.*A/(A+B)**6+165.*B/(A+B)**6+36.*B**2/A/(A+B)**6+13.*B**3/A**2/(A+B)**6+2.*B**4/A**3/(A+B)**6,-.3750000000000000/B**3*A**5/(A+B)**6-2.125000000000000/B**2*A**4/(A+B)**6-4.750000000000000/B*A**3/(A+B)**6-23.87500000000000*A**2/(A+B)**6-1.750000000000000*B*A/(A+B)**6-23.87500000000000*B**2/(A+B)**6-4.750000000000000*B**3/A/(A+B)**6-2.125000000000000*B**4/A**2/(A+B)**6-.3750000000000000*B**5/A**3/(A+B)**6-1.500000000000000/B**2*A**3/(A+B)**6-5.500000000000000/B*A**2/(A+B)**6-25.*A/(A+B)**6-25.*B/(A+B)**6-5.500000000000000*B**2/A/(A+B)**6-1.500000000000000*B**3/A**2/(A+B)**6,1.500000000000000/B**3*A**6/(A+B)**7+10.12500000000000/B**2*A**5/(A+B)**7+29.12500000000000/B*A**4/(A+B)**7+45.25000000000000*A**3/(A+B)**7-135.1250000000000*B*A**2/(A+B)**7+298.7500000000000*B**2*A/(A+B)**7+14.87500000000000*B**3/(A+B)**7-5.750000000000000*B**4/A/(A+B)**7-2.375000000000000*B**5/A**2/(A+B)**7-.3750000000000000*B**6/A**3/(A+B)**7-4./B**3*A**5/(A+B)**7-27./B**2*A**4/(A+B)**7-76.50000000000000/B*A**3/(A+B)**7-9.500000000000000*A**2/(A+B)**7-305.*B*A/(A+B)**7-362.*B**2/(A+B)**7-14.50000000000000*B**3/A/(A+B)**7-1.500000000000000*B**4/A**2/(A+B)**7,-.3750000000000000/B**3*A**6/(A+B)**7-2.375000000000000/B**2*A**5/(A+B)**7-5.750000000000000/B*A**4/(A+B)**7+14.87500000000000*A**3/(A+B)**7+298.7500000000000*B*A**2/(A+B)**7-135.1250000000000*B**2*A/(A+B)**7+45.25000000000000*B**3/(A+B)**7+29.12500000000000*B**4/A/(A+B)**7+10.12500000000000*B**5/A**2/(A+B)**7+1.500000000000000*B**6/A**3/(A+B)**7-1.500000000000000/B**2*A**4/(A+B)**7-14.50000000000000/B*A**3/(A+B)**7-362.*A**2/(A+B)**7-305.*B*A/(A+B)**7-9.500000000000000*B**2/(A+B)**7-76.50000000000000*B**3/A/(A+B)**7-27.*B**4/A**2/(A+B)**7-4.*B**5/A**3/(A+B)**7,-.3750000000000000/B**3*A**5/(A+B)**6-2.125000000000000/B**2*A**4/(A+B)**6-4.750000000000000/B*A**3/(A+B)**6-23.87500000000000*A**2/(A+B)**6-1.750000000000000*B*A/(A+B)**6-23.87500000000000*B**2/(A+B)**6-4.750000000000000*B**3/A/(A+B)**6-2.125000000000000*B**4/A**2/(A+B)**6-.3750000000000000*B**5/A**3/(A+B)**6-1.500000000000000/B**2*A**3/(A+B)**6-5.500000000000000/B*A**2/(A+B)**6-25.*A/(A+B)**6-25.*B/(A+B)**6-5.500000000000000*B**2/A/(A+B)**6-1.500000000000000*B**3/A**2/(A+B)**6,-1.500000000000000/B**3*A**5/(A+B)**6-10.50000000000000/B**2*A**4/(A+B)**6-35./B*A**3/(A+B)**6-101.5000000000000*A**2/(A+B)**6-343.*B*A/(A+B)**6-101.5000000000000*B**2/(A+B)**6-35.*B**3/A/(A+B)**6-10.50000000000000*B**4/A**2/(A+B)**6-1.500000000000000*B**5/A**3/(A+B)**6+2./B**3*A**4/(A+B)**6+16./B**2*A**3/(A+B)**6+45./B*A**2/(A+B)**6+201.*A/(A+B)**6+201.*B/(A+B)**6+45.*B**2/A/(A+B)**6+16.*B**3/A**2/(A+B)**6+2.*B**4/A**3/(A+B)**6,.7500000000000000/B**3*A**6/(A+B)**7+5.625000000000000/B**2*A**5/(A+B)**7+18.87500000000000/B*A**4/(A+B)**7+19.62500000000000*A**3/(A+B)**7+170.3750000000000*B*A**2/(A+B)**7+73.87500000000000*B**2*A/(A+B)**7+57.62500000000000*B**3/(A+B)**7+4.875000000000000*B**4/A/(A+B)**7+.3750000000000000*B**5/A**2/(A+B)**7+1.500000000000000/B**2*A**4/(A+B)**7+7.500000000000000/B*A**3/(A+B)**7-1.*A**2/(A+B)**7+39.*B*A/(A+B)**7+47.50000000000000*B**2/(A+B)**7+1.500000000000000*B**3/A/(A+B)**7,.3750000000000000/B**2*A**5/(A+B)**7+4.875000000000000/B*A**4/(A+B)**7+57.62500000000000*A**3/(A+B)**7+73.87500000000000*B*A**2/(A+B)**7+170.3750000000000*B**2*A/(A+B)**7+19.62500000000000*B**3/(A+B)**7+18.87500000000000*B**4/A/(A+B)**7+5.625000000000000*B**5/A**2/(A+B)**7+.7500000000000000*B**6/A**3/(A+B)**7+1.500000000000000/B*A**3/(A+B)**7+47.50000000000000*A**2/(A+B)**7+39.*B*A/(A+B)**7-1.*B**2/(A+B)**7+7.500000000000000*B**3/A/(A+B)**7+1.500000000000000*B**4/A**2/(A+B)**7,1.500000000000000/B**3*A**6/(A+B)**7+10.12500000000000/B**2*A**5/(A+B)**7+29.12500000000000/B*A**4/(A+B)**7+45.25000000000000*A**3/(A+B)**7-135.1250000000000*B*A**2/(A+B)**7+298.7500000000000*B**2*A/(A+B)**7+14.87500000000000*B**3/(A+B)**7-5.750000000000000*B**4/A/(A+B)**7-2.375000000000000*B**5/A**2/(A+B)**7-.3750000000000000*B**6/A**3/(A+B)**7-4./B**3*A**5/(A+B)**7-27./B**2*A**4/(A+B)**7-76.50000000000000/B*A**3/(A+B)**7-9.500000000000000*A**2/(A+B)**7-305.*B*A/(A+B)**7-362.*B**2/(A+B)**7-14.50000000000000*B**3/A/(A+B)**7-1.500000000000000*B**4/A**2/(A+B)**7,.7500000000000000/B**3*A**6/(A+B)**7+5.625000000000000/B**2*A**5/(A+B)**7+18.87500000000000/B*A**4/(A+B)**7+19.62500000000000*A**3/(A+B)**7+170.3750000000000*B*A**2/(A+B)**7+73.87500000000000*B**2*A/(A+B)**7+57.62500000000000*B**3/(A+B)**7+4.875000000000000*B**4/A/(A+B)**7+.3750000000000000*B**5/A**2/(A+B)**7+1.500000000000000/B**2*A**4/(A+B)**7+7.500000000000000/B*A**3/(A+B)**7-1.*A**2/(A+B)**7+39.*B*A/(A+B)**7+47.50000000000000*B**2/(A+B)**7+1.500000000000000*B**3/A/(A+B)**7,-5.250000000000000/B**3/(A+B)**8*A**7-40.50000000000000/B**2/(A+B)**8*A**6-138.7500000000000/B/(A+B)**8*A**5-280.7500000000000/(A+B)**8*A**4-629.7500000000000*B/(A+B)**8*A**3+770.2500000000000*B**2/(A+B)**8*A**2-836.7500000000000*B**3/(A+B)**8*A-180.2500000000000*B**4/(A+B)**8-52.*B**5/(A+B)**8/A-12.75000000000000*B**6/(A+B)**8/A**2-1.500000000000000*B**7/(A+B)**8/A**3+14./B**3/(A+B)**8*A**6+107./B**2/(A+B)**8*A**5+355./B/(A+B)**8*A**4+778./(A+B)**8*A**3+284.*B/(A+B)**8*A**2+597.*B**2/(A+B)**8*A+911.*B**3/(A+B)**8+100.*B**4/(A+B)**8/A+20.*B**5/(A+B)**8/A**2+2.*B**6/(A+B)**8/A**3,.7500000000000000/B**3/(A+B)**8*A**7+5.625000000000000/B**2/(A+B)**8*A**6+18.25000000000000/B/(A+B)**8*A**5+52.50000000000000/(A+B)**8*A**4+397.*B/(A+B)**8*A**3-2356.250000000000*B**2/(A+B)**8*A**2+397.*B**3/(A+B)**8*A+52.50000000000000*B**4/(A+B)**8+18.25000000000000*B**5/(A+B)**8/A+5.625000000000000*B**6/(A+B)**8/A**2+.7500000000000000*B**7/(A+B)**8/A**3+1.500000000000000/B**2/(A+B)**8*A**5+10.50000000000000/B/(A+B)**8*A**4-276.5000000000000/(A+B)**8*A**3+1848.500000000000*B/(A+B)**8*A**2+1848.500000000000*B**2/(A+B)**8*A-276.5000000000000*B**3/(A+B)**8+10.50000000000000*B**4/(A+B)**8/A+1.500000000000000*B**5/(A+B)**8/A**2,-.3750000000000000/B**3*A**6/(A+B)**7-2.375000000000000/B**2*A**5/(A+B)**7-5.750000000000000/B*A**4/(A+B)**7+14.87500000000000*A**3/(A+B)**7+298.7500000000000*B*A**2/(A+B)**7-135.1250000000000*B**2*A/(A+B)**7+45.25000000000000*B**3/(A+B)**7+29.12500000000000*B**4/A/(A+B)**7+10.12500000000000*B**5/A**2/(A+B)**7+1.500000000000000*B**6/A**3/(A+B)**7-1.500000000000000/B**2*A**4/(A+B)**7-14.50000000000000/B*A**3/(A+B)**7-362.*A**2/(A+B)**7-305.*B*A/(A+B)**7-9.500000000000000*B**2/(A+B)**7-76.50000000000000*B**3/A/(A+B)**7-27.*B**4/A**2/(A+B)**7-4.*B**5/A**3/(A+B)**7,.3750000000000000/B**2*A**5/(A+B)**7+4.875000000000000/B*A**4/(A+B)**7+57.62500000000000*A**3/(A+B)**7+73.87500000000000*B*A**2/(A+B)**7+170.3750000000000*B**2*A/(A+B)**7+19.62500000000000*B**3/(A+B)**7+18.87500000000000*B**4/A/(A+B)**7+5.625000000000000*B**5/A**2/(A+B)**7+.7500000000000000*B**6/A**3/(A+B)**7+1.500000000000000/B*A**3/(A+B)**7+47.50000000000000*A**2/(A+B)**7+39.*B*A/(A+B)**7-1.*B**2/(A+B)**7+7.500000000000000*B**3/A/(A+B)**7+1.500000000000000*B**4/A**2/(A+B)**7,.7500000000000000/B**3/(A+B)**8*A**7+5.625000000000000/B**2/(A+B)**8*A**6+18.25000000000000/B/(A+B)**8*A**5+52.50000000000000/(A+B)**8*A**4+397.*B/(A+B)**8*A**3-2356.250000000000*B**2/(A+B)**8*A**2+397.*B**3/(A+B)**8*A+52.50000000000000*B**4/(A+B)**8+18.25000000000000*B**5/(A+B)**8/A+5.625000000000000*B**6/(A+B)**8/A**2+.7500000000000000*B**7/(A+B)**8/A**3+1.500000000000000/B**2/(A+B)**8*A**5+10.50000000000000/B/(A+B)**8*A**4-276.5000000000000/(A+B)**8*A**3+1848.500000000000*B/(A+B)**8*A**2+1848.500000000000*B**2/(A+B)**8*A-276.5000000000000*B**3/(A+B)**8+10.50000000000000*B**4/(A+B)**8/A+1.500000000000000*B**5/(A+B)**8/A**2,-1.500000000000000/B**3/(A+B)**8*A**7-12.75000000000000/B**2/(A+B)**8*A**6-52./B/(A+B)**8*A**5-180.2500000000000/(A+B)**8*A**4-836.7500000000000*B/(A+B)**8*A**3+770.2500000000000*B**2/(A+B)**8*A**2-629.7500000000000*B**3/(A+B)**8*A-280.7500000000000*B**4/(A+B)**8-138.7500000000000*B**5/(A+B)**8/A-40.50000000000000*B**6/(A+B)**8/A**2-5.250000000000000*B**7/(A+B)**8/A**3+2./B**3/(A+B)**8*A**6+20./B**2/(A+B)**8*A**5+100./B/(A+B)**8*A**4+911./(A+B)**8*A**3+597.*B/(A+B)**8*A**2+284.*B**2/(A+B)**8*A+778.*B**3/(A+B)**8+355.*B**4/(A+B)**8/A+107.*B**5/(A+B)**8/A**2+14.*B**6/(A+B)**8/A**3
  </code></pre>
  
  <p>Input from Matrix_4x4SS.mat</p>
  
  <pre><code>1./B**3*A**3/(A+B)**6+6./B**2*A**2/(A+B)**6+15./B*A/(A+B)**6+84./(A+B)**6+15.*B/A/(A+B)**6+6.*B**2/A**2/(A+B)**6+1.*B**3/A**3/(A+B)**6,-.5000000000000000/B**3*A**3/(A+B)**6-3.500000000000000/B**2*A**2/(A+B)**6-9.500000000000000/B*A/(A+B)**6-53./(A+B)**6-9.500000000000000*B/A/(A+B)**6-3.500000000000000*B**2/A**2/(A+B)**6-.5000000000000000*B**3/A**3/(A+B)**6,-2./B**3*A**4/(A+B)**7-12.50000000000000/B**2*A**3/(A+B)**7-32.50000000000000/B*A**2/(A+B)**7+18.50000000000000*A/(A+B)**7-253.*B/(A+B)**7-17.50000000000000*B**2/A/(A+B)**7-4.500000000000000*B**3/A**2/(A+B)**7-.5000000000000000*B**4/A**3/(A+B)**7,-.5000000000000000/B**3*A**4/(A+B)**7-4.500000000000000/B**2*A**3/(A+B)**7-17.50000000000000/B*A**2/(A+B)**7-253.*A/(A+B)**7+18.50000000000000*B/(A+B)**7-32.50000000000000*B**2/A/(A+B)**7-12.50000000000000*B**3/A**2/(A+B)**7-2.*B**4/A**3/(A+B)**7,-.5000000000000000/B**3*A**3/(A+B)**6-3.500000000000000/B**2*A**2/(A+B)**6-9.500000000000000/B*A/(A+B)**6-53./(A+B)**6-9.500000000000000*B/A/(A+B)**6-3.500000000000000*B**2/A**2/(A+B)**6-.5000000000000000*B**3/A**3/(A+B)**6,2./B**3*A**3/(A+B)**6+14./B**2*A**2/(A+B)**6+38./B*A/(A+B)**6+212./(A+B)**6+38.*B/A/(A+B)**6+14.*B**2/A**2/(A+B)**6+2.*B**3/A**3/(A+B)**6,1./B**3*A**4/(A+B)**7+6.500000000000000/B**2*A**3/(A+B)**7+16.50000000000000/B*A**2/(A+B)**7-19.*A/(A+B)**7+118.*B/(A+B)**7+4.500000000000000*B**2/A/(A+B)**7+.5000000000000000*B**3/A**2/(A+B)**7,.5000000000000000/B**2*A**3/(A+B)**7+4.500000000000000/B*A**2/(A+B)**7+118.*A/(A+B)**7-19.*B/(A+B)**7+16.50000000000000*B**2/A/(A+B)**7+6.500000000000000*B**3/A**2/(A+B)**7+1.*B**4/A**3/(A+B)**7,-2./B**3*A**4/(A+B)**7-12.50000000000000/B**2*A**3/(A+B)**7-32.50000000000000/B*A**2/(A+B)**7+18.50000000000000*A/(A+B)**7-253.*B/(A+B)**7-17.50000000000000*B**2/A/(A+B)**7-4.500000000000000*B**3/A**2/(A+B)**7-.5000000000000000*B**4/A**3/(A+B)**7,1./B**3*A**4/(A+B)**7+6.500000000000000/B**2*A**3/(A+B)**7+16.50000000000000/B*A**2/(A+B)**7-19.*A/(A+B)**7+118.*B/(A+B)**7+4.500000000000000*B**2/A/(A+B)**7+.5000000000000000*B**3/A**2/(A+B)**7,7./B**3/(A+B)**8*A**5+50./B**2/(A+B)**8*A**4+154./B/(A+B)**8*A**3+331./(A+B)**8*A**2-147.*B/(A+B)**8*A+848.*B**2/(A+B)**8+80.*B**3/(A+B)**8/A+19.*B**4/(A+B)**8/A**2+2.*B**5/(A+B)**8/A**3,1./B**3/(A+B)**8*A**5+8.500000000000000/B**2/(A+B)**8*A**4+31./B/(A+B)**8*A**3-152.5000000000000/(A+B)**8*A**2+1568.*B/(A+B)**8*A-152.5000000000000*B**2/(A+B)**8+31.*B**3/(A+B)**8/A+8.500000000000000*B**4/(A+B)**8/A**2+1.*B**5/(A+B)**8/A**3,-.5000000000000000/B**3*A**4/(A+B)**7-4.500000000000000/B**2*A**3/(A+B)**7-17.50000000000000/B*A**2/(A+B)**7-253.*A/(A+B)**7+18.50000000000000*B/(A+B)**7-32.50000000000000*B**2/A/(A+B)**7-12.50000000000000*B**3/A**2/(A+B)**7-2.*B**4/A**3/(A+B)**7,.5000000000000000/B**2*A**3/(A+B)**7+4.500000000000000/B*A**2/(A+B)**7+118.*A/(A+B)**7-19.*B/(A+B)**7+16.50000000000000*B**2/A/(A+B)**7+6.500000000000000*B**3/A**2/(A+B)**7+1.*B**4/A**3/(A+B)**7,1./B**3/(A+B)**8*A**5+8.500000000000000/B**2/(A+B)**8*A**4+31./B/(A+B)**8*A**3-152.5000000000000/(A+B)**8*A**2+1568.*B/(A+B)**8*A-152.5000000000000*B**2/(A+B)**8+31.*B**3/(A+B)**8/A+8.500000000000000*B**4/(A+B)**8/A**2+1.*B**5/(A+B)**8/A**3,2./B**3/(A+B)**8*A**5+19./B**2/(A+B)**8*A**4+80./B/(A+B)**8*A**3+848./(A+B)**8*A**2-147.*B/(A+B)**8*A+331.*B**2/(A+B)**8+154.*B**3/(A+B)**8/A+50.*B**4/(A+B)**8/A**2+7.*B**5/(A+B)**8/A**3
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>I guess the important part of this question is how to read in an array of equations from a file into a numpy array and be able to assign values to the variables without the need for <code>eval</code>. As an example of what can usually be done using numpy:</p>
  
  <pre><code>import numpy as np
  A = 1
  B = 1
  arr = np.array([[A+2*B,2],[2,B+6*(B+A)]])
  print arr
  </code></pre>
  
  <p>This gives:</p>
  
  <pre><code>[[ 3  2]
   [ 2 13]]
  </code></pre>
  
  <p>The numpy array contained algebraic terms and when printed the specified values of A and B were inserted. Is this possible with equations read from a file into a numpy array? I have looked through the numpy documentation and is the issue to do with the type of data being read in? i.e. I cannot specify the dtype to be int or float as each array element contains ints, floats and text. I feel there is a very simple aspect of Python I am missing that can do this.</p>