I'm importing an .csv file (exported from maple) in python with a matrix (10 by 10) with variables in each element. For example: a*b-c is in (0,0). How can I define a,b and c such that these values are passed to the matrix?
I already tried a = 1, b =2 etc. before I import the .csv matrix but this doesn't work.
import numpy as np
my_matrix = np.genfromtxt('MyMatrix.csv', dtype='unicode', delimiter=',')
I expect the variables to be passed to the matrix and the matrix to be of type float to be able to take the inverse of it.
Since every equation is in text format, you can evaluate them as Python expressions:
import numpy as np
my_matrix = np.genfromtxt('MyMatrix.csv', dtype='unicode', delimiter=',')
a = 1
b = 2
c = 3
my_matrix_eval = np.vectorize(lambda x: np.float64(eval(x)))(my_matrix)
Note: This will solve the problem as asked in the question. However, since the example you provided in the comments contains an operator (^
) that is not a valid Python expression, these characters will have to be converted to its corresponding Python operator (**
).