I have converted a .m file into a python file. The .m file consists of a matrix; which is a function of a variable called 'f'. Now I wanted to convert this matrix into numpy array, so that I can compute the condition number for the matrix for different values of 'f'. I used a github repo called matlab2python to convert .m file to .py file. After converting I am trying to perform some operations on the numpy array, but I am facing value error. Below is code that I tried. Now I am.
import numpy as np
from sympy import *
f=symbols('f')
Rarz = eval(np.array([[(- 640227.0) np.multiply(195.539,f ** 2),4211.96 np.multiply((- 6.45961e-14),f ** 2),(- 5469.12) np.multiply((- 6.30001e-14),f ** 2),(- 7178.23) np.multiply((- 1.0631e-12),f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.683542,0.0,0.151787],[4211.96 np.multiply((- 6.45961e-14),f ** 2),(- 10147500.0) np.multiply(193.704,f ** 2),(- 2958.16) np.multiply((- 3.10464e-13),f ** 2),(- 3882.6) np.multiply(1.14274e-12,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.369718,0.0,(- 0.947078)],[(- 5469.12) np.multiply((- 6.30001e-14),f ** 2),(- 2958.16) np.multiply((- 3.10464e-13),f ** 2),(- 51372700.0) np.multiply(193.708,f ** 2),5041.45 np.multiply((- 2.10639e-12),f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 0.480068),0.0,(- 1.22348)],[(- 7178.23) np.multiply((- 1.0631e-12),f ** 2),(- 3882.6) np.multiply(1.14274e-12,f ** 2),5041.45 np.multiply((- 2.10639e-12),f ** 2),(- 162352000.0) np.multiply(193.695,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 0.630091),0.0,1.0085],[0.0,0.0,0.0,0.0,(- 1629440000.0) np.multiply(154.953,f ** 2),(- 11753.4) np.multiply(0.000111686,f ** 2),25800.5 np.multiply((- 6.59562e-05),f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 9.50724e-06)],[0.0,0.0,0.0,0.0,(- 11753.4) np.multiply(0.000111686,f ** 2),(- 12381300000.0) np.multiply(154.953,f ** 2),(- 73445.5) np.multiply(5.30086e-05,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.27121e-05],[0.0,0.0,0.0,0.0,25800.5 np.multiply((- 6.59562e-05),f ** 2),(- 73445.5) np.multiply(5.30086e-05,f ** 2),(- 47583200000.0) np.multiply(154.953,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,3.63798e-12,(- 0.000219237)],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 1239040000.0) np.multiply(194.525,f ** 2),969793000.0 np.multiply((- 151.126),f ** 2),0.0,0.0,0.0,0.0,(- 0.680346),0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,969793000.0 np.multiply((- 151.126),f ** 2),(- 4199830000.0) np.multiply(227.587,f ** 2),0.0,0.0,0.0,0.0,1.33685e-12,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 12337000000.0) np.multiply(77.4764,f ** 2),0.0,0.0,(- 1.22465e-16),0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 49348000000.0) np.multiply(77.4764,f ** 2),0.0,2.44929e-16,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 111033000000.0) np.multiply(77.4764,f ** 2),(- 3.67394e-16),0.0,0.0],[0.683542,0.369718,(- 0.480068),(- 0.630091),0.0,0.0,0.0,0.0,0.0,(- 1.22465e-16),2.44929e-16,(- 3.67394e-16),0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,3.63798e-12,(- 0.680346),1.33685e-12,0.0,0.0,0.0,0.0,0.0,0.0],[0.151787,(- 0.947078),(- 1.22348),1.0085,(- 9.50724e-06),9.27121e-05,(- 0.000219237),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]]))
kk=Rarz.subs({f:2})
print(kk)
CodePudding user response:
Since you are interested in a purely numeric result, you don't need to use SymPy in this case. We can use eval
, passing in a string representing the expression, as well as a dictionary containing the value of the variables in the expression:
import numpy as np
s = """np.array([[(- 640227.0) np.multiply(195.539,f ** 2),4211.96 np.multiply((- 6.45961e-14),f ** 2),(- 5469.12) np.multiply((- 6.30001e-14),f ** 2),(- 7178.23) np.multiply((- 1.0631e-12),f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.683542,0.0,0.151787],[4211.96 np.multiply((- 6.45961e-14),f ** 2),(- 10147500.0) np.multiply(193.704,f ** 2),(- 2958.16) np.multiply((- 3.10464e-13),f ** 2),(- 3882.6) np.multiply(1.14274e-12,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.369718,0.0,(- 0.947078)],[(- 5469.12) np.multiply((- 6.30001e-14),f ** 2),(- 2958.16) np.multiply((- 3.10464e-13),f ** 2),(- 51372700.0) np.multiply(193.708,f ** 2),5041.45 np.multiply((- 2.10639e-12),f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 0.480068),0.0,(- 1.22348)],[(- 7178.23) np.multiply((- 1.0631e-12),f ** 2),(- 3882.6) np.multiply(1.14274e-12,f ** 2),5041.45 np.multiply((- 2.10639e-12),f ** 2),(- 162352000.0) np.multiply(193.695,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 0.630091),0.0,1.0085],[0.0,0.0,0.0,0.0,(- 1629440000.0) np.multiply(154.953,f ** 2),(- 11753.4) np.multiply(0.000111686,f ** 2),25800.5 np.multiply((- 6.59562e-05),f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 9.50724e-06)],[0.0,0.0,0.0,0.0,(- 11753.4) np.multiply(0.000111686,f ** 2),(- 12381300000.0) np.multiply(154.953,f ** 2),(- 73445.5) np.multiply(5.30086e-05,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.27121e-05],[0.0,0.0,0.0,0.0,25800.5 np.multiply((- 6.59562e-05),f ** 2),(- 73445.5) np.multiply(5.30086e-05,f ** 2),(- 47583200000.0) np.multiply(154.953,f ** 2),0.0,0.0,0.0,0.0,0.0,0.0,3.63798e-12,(- 0.000219237)],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 1239040000.0) np.multiply(194.525,f ** 2),969793000.0 np.multiply((- 151.126),f ** 2),0.0,0.0,0.0,0.0,(- 0.680346),0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,969793000.0 np.multiply((- 151.126),f ** 2),(- 4199830000.0) np.multiply(227.587,f ** 2),0.0,0.0,0.0,0.0,1.33685e-12,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 12337000000.0) np.multiply(77.4764,f ** 2),0.0,0.0,(- 1.22465e-16),0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 49348000000.0) np.multiply(77.4764,f ** 2),0.0,2.44929e-16,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(- 111033000000.0) np.multiply(77.4764,f ** 2),(- 3.67394e-16),0.0,0.0],[0.683542,0.369718,(- 0.480068),(- 0.630091),0.0,0.0,0.0,0.0,0.0,(- 1.22465e-16),2.44929e-16,(- 3.67394e-16),0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,3.63798e-12,(- 0.680346),1.33685e-12,0.0,0.0,0.0,0.0,0.0,0.0],[0.151787,(- 0.947078),(- 1.22348),1.0085,(- 9.50724e-06),9.27121e-05,(- 0.000219237),0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]])"""
t = eval(s, {"f": 2, "np": np})
print(t)