Related
I have been carrying out QMSA analysis of PdSn4 and when trying to implement a multicarrier fitting I keep getting an error
NameError: name 'DiracDelta' is not defined
My output includes only one instance where the DiracDelta function is present when looking at the second band fitting (n2, mu2). From what I understand there shouldn't be any DiracDelta functions within the output based on my model design.
import sympy as sp
from sympy.functions.special.delta_functions import DiracDelta
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import math
import symfit
from symfit import DiracDelta
from symfit import Variable, Parameter, parameters, Fit, Model, cosh
from symfit.contrib.interactive_guess import InteractiveGuess
import numpy as np
plt.ion()
data = np.loadtxt('PdSn4_010K_rhoData.txt')
xdata = np.array(data[:, 0])
rhoxx_exp = np.array(data[:, 1])
rhoxy_exp = np.array(data[:, 2])
B = Variable('B')
y1 = Variable('y1')
y2 = Variable('y2')
n_1 = Parameter('n1', value=n1, min=1.5e27, max=1.55e27)
n_2 = Parameter('n2', value=n2, min=-1.52e27, max=-1.51e27)
n_3 = Parameter('n3', value=n3, min=0.91e27, max=0.92e27)
n_4 = Parameter('n4', value=n4, min=-1.05e27, max=-1.045e27)
mu_1 = Parameter('mu1', value=mu1, min=1.4, max=1.42)
mu_2 = Parameter('mu2', value=mu2, min=0.63, max=0.65)
mu_3 = Parameter('mu3', value=mu3, min=0.085, max=0.09)
mu_4 = Parameter('mu4', value=mu4, min=0.088, max=0.092)
a = 1.1513
f = 0.992
q = 1.6e-19
R1 = -1 / (q*n_1)
R2 = -1 / (q*n_2)
R3 = -1 / (q*n_3)
R4 = -1 / (q*n_4)
rho1 = 1 / (abs(n_1) * q * mu_1)
rho2 = 1 / (abs(n_2) * q * mu_2)
rho3 = 1 / (abs(n_3) * q * mu_3)
rho4 = 1 / (abs(n_4) * q * mu_4)
dictionary = {
y1: (rho1 / (rho1**2 + (R1*B)**2) + rho2 / (rho2**2 + (R2*B)**2)
+ rho3 / (rho3**2 + (R3*B)**2) + rho4 / (rho4**2 + (R4*B)**2))
/ ((rho1 / (rho1**2 + (R1*B)**2) + rho2 / (rho2**2 + (R2*B)**2)
+ rho3 / (rho3**2 + (R3*B)**2) + rho4 / (rho4**2 + (R4*B)**2))**2
+ ((-R1*B)/(rho1**2+(R1*B)**2) + (-R2*B)/(rho2**2+(R2*B)**2)
+ (-R3*B)/(rho3**2+(R3*B)**2) + (-R4*B)/(rho4**2+(R4*B)**2))**2)
+ (a/cosh(B/f))*((rho1 / (rho1**2 + (R1*B)**2) + rho2 / (rho2**2 + (R2*B)**2)
+ rho3 / (rho3**2 + (R3*B)**2) + rho4 / (rho4**2 + (R4*B)**2))
/ ((rho1 / (rho1**2 + (R1*B)**2) + rho2 / (rho2**2 + (R2*B)**2)
+ rho3 / (rho3**2 + (R3*B)**2) + rho4 / (rho4**2 + (R4*B)**2))**2
+ ((-R1*B)/(rho1**2+(R1*B)**2) + (-R2*B)/(rho2**2+(R2*B)**2)
+ (-R3*B)/(rho3**2+(R3*B)**2) + (-R4*B)/(rho4**2+(R4*B)**2))**2))
,
y2: - ((-R1*B)/(rho1**2+(R1*B)**2) + (-R2*B)/(rho2**2+(R2*B)**2)
+ (-R3*B)/(rho3**2+(R3*B)**2) + (-R4*B)/(rho4**2+(R4*B)**2)) /
((rho1 / (rho1**2 + (R1*B)**2) + rho2 / (rho2**2 + (R2*B)**2)
+ rho3 / (rho3**2 + (R3*B)**2) + rho4 / (rho4**2 + (R4*B)**2))**2
+ ((-R1*B)/(rho1**2+(R1*B)**2) + (-R2*B)/(rho2**2+(R2*B)**2)
+ (-R3*B)/(rho3**2+(R3*B)**2) + (-R4*B)/(rho4**2+(R4*B)**2))**2)
}
model = Model(dictionary)
#print(model)
model_sim = model(B=xdata, n1=n1, n2=n2, n3=n3, n4=n4, mu1=mu1, mu2=mu2, mu3=mu3, mu4=mu4)
#print(data)
rho_xx_sim = model_sim.y1
rho_xy_sim = model_sim.y2
f = plt.figure(figsize=(15,15))
ax = f.add_subplot(211)
plt.xlabel("Magnetic Field [T]")
plt.ylabel("Rho$_{xx}$")
plt.plot(xdata, rhoxx_exp, 'k.')
plt.plot(xdata, rho_xx_sim, 'r-')
plt.xlim([0,8])
ax2 = f.add_subplot(212)
plt.xlabel("Magnetic Field [T]")
plt.ylabel("Rho$_{xy}$")
plt.plot(xdata, rhoxy_exp, 'k.')
plt.plot(xdata, rho_xy_sim, 'r-')
plt.xlim([0,8])
plt.show()
guess = InteractiveGuess(dictionary, B=xdata, y1=rho_xx_sim, y2=rho_xy_sim, n_points=250)
guess.execute()
print(guess)
fit = Fit(model, B=xdata, y1=rho_xx_sim, y2=rho_xy_sim)
fit_result = fit.execute()
print(fit_result)
Link to the data: Multicarrier Fitting Data
It should be saved as PdSn4_010K_rhoData.txt in the same directory as the file.
If anyone could figure out why there is this DiracDelta function present in my output or how to overcome this error that would be great!
Error traceback goes as follows:
NameError Traceback (most recent call last)
<ipython-input-19-68c235066887> in <module>
197
198 fit = Fit(model, B=xdata, y1=rho_xx_sim, y2=rho_xy_sim)
--> 199 fit_result = fit.execute()
200 print(fit_result)
201 #y1result, y2result = fit.model(B=xdata, **fit_result.params)
~\anaconda5\lib\site-packages\symfit\core\fit.py in execute(self, **minimize_options)
579 """
580 minimizer_ans = self.minimizer.execute(**minimize_options)
--> 581 minimizer_ans.covariance_matrix = self.covariance_matrix(
582 dict(zip(self.model.params, minimizer_ans._popt))
583 )
~\anaconda5\lib\site-packages\symfit\core\fit.py in covariance_matrix(self, best_fit_params)
278 :return: covariance matrix.
279 """
--> 280 cov_matrix = self._covariance_matrix(best_fit_params,
281 objective=self.objective)
282 if cov_matrix is None:
~\anaconda5\lib\site-packages\symfit\core\fit.py in _covariance_matrix(self, best_fit_params, objective)
236 # Helper function for self.covariance_matrix.
237 try:
--> 238 hess = objective.eval_hessian(**key2str(best_fit_params))
239 except AttributeError:
240 # Some models do not have an eval_hessian, in which case we give up
~\anaconda5\lib\site-packages\symfit\core\objectives.py in eval_hessian(self, ordered_parameters, **parameters)
367 ordered_parameters, **parameters
368 )
--> 369 evaluated_hess = super(LeastSquares, self).eval_hessian(
370 ordered_parameters, **parameters
371 )
~\anaconda5\lib\site-packages\symfit\core\objectives.py in eval_hessian(self, ordered_parameters, **parameters)
221 parameters.update(dict(zip(self.model.free_params, ordered_parameters)))
222 parameters.update(self._invariant_kwargs)
--> 223 result = self.model.eval_hessian(**key2str(parameters))._asdict()
224 # Return only the components corresponding to the dependent data.
225 return self._shape_of_dependent_data(
~\anaconda5\lib\site-packages\symfit\core\models.py in eval_hessian(self, *args, **kwargs)
897 # Evaluate the hessian model and use the resulting Ans namedtuple as a
898 # dict. From this, take the relevant components.
--> 899 eval_hess_dict = self.hessian_model(*args, **kwargs)._asdict()
900 hess = [[[np.broadcast_to(eval_hess_dict.get(D(var, p1, p2), 0),
901 eval_hess_dict[var].shape)
~\anaconda5\lib\site-packages\symfit\core\models.py in __call__(self, *args, **kwargs)
665 even for scalar valued functions. This is done for consistency.
666 """
--> 667 return ModelOutput(self.keys(), self.eval_components(*args, **kwargs))
668
669
~\anaconda5\lib\site-packages\symfit\core\models.py in eval_components(self, *args, **kwargs)
613 dependencies_kwargs = {d.name: kwargs[d.name]
614 for d in dependencies}
--> 615 kwargs[symbol.name] = components[symbol](**dependencies_kwargs)
616
617 return [np.atleast_1d(kwargs[var.name]) for var in self]
<lambdifygenerated-1024> in _lambdifygenerated(B, mu1, mu2, mu3, mu4, n1, n2, n3, n4)
1 def _lambdifygenerated(B, mu1, mu2, mu3, mu4, n1, n2, n3, n4):
----> 2 return (6.5536e-76*(-3.90625e+37*(1.31072e-75*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2*abs(n2)) - 2*sign(n2)/(mu2*n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1))) - 3.90625e+37*(1.31072e-75*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 2*B/(n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2)))))*(-2*(1.31072e-75*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2*abs(n2)) - 2*sign(n2)/(mu2*n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1))) - 2*(1.31072e-75*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 2*B/(n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2)))))*(-6.25e+18*B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) - 6.25e+18*B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) - 6.25e+18*B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) - 6.25e+18*B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))/((1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1)))**2 + (B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))**2)**3 + 1.31072e-75*(-3.90625e+37*(1.31072e-75*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2*abs(n2)) - 2*sign(n2)/(mu2*n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1))) - 3.90625e+37*(1.31072e-75*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 2*B/(n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2)))))*(-4.096e-57*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) + 6.25e+18*B/(n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))/((1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1)))**2 + (B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))**2)**2 + (-4.096e-57*B*(-2.34375e+38*B**2/n2**4 - 2.34375e+38/(mu2**2*n2**4))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 4.096e-57*B*(4*B**2/n2**3 + 4/(mu2**2*n2**3))*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**3) + 8.192e-57*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2**2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 1.25e+19*B/(n2**3*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))/(3.90625e+37*(1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1)))**2 + 3.90625e+37*(B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))**2) + 6.5536e-76*((-5.12e-38*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2*abs(n2)) + 7.8125e+37*sign(n2)/(mu2*n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(6.5536e-76*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2*abs(n2)) - sign(n2)/(mu2*n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2)))) + (-5.12e-38*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) + 7.8125e+37*B/(n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))))*(6.5536e-76*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - B/(n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2)))) + (1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1)))*(-5.12e-38*(-2.34375e+38*B**2/n2**4 - 2.34375e+38/(mu2**2*n2**4))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2*abs(n2)) - 5.12e-38*(4*B**2/n2**3 + 4/(mu2**2*n2**3))*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(mu2*(B**2/n2**2 + 1/(mu2**2*n2**2))**3*abs(n2)) + 1.024e-37*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))*sign(n2)/(mu2*n2**2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) + 1.5625e+38*DiracDelta(n2)/(mu2*n2**2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) - 1.5625e+38*sign(n2)/(mu2*n2**3*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2)))) + (B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))*(-5.12e-38*B*(-2.34375e+38*B**2/n2**4 - 2.34375e+38/(mu2**2*n2**4))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 5.12e-38*B*(4*B**2/n2**3 + 4/(mu2**2*n2**3))*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2*(B**2/n2**2 + 1/(mu2**2*n2**2))**3) + 1.024e-37*B*(7.8125e+37*B**2/n2**3 + 7.8125e+37/(mu2**2*n2**3))/(n2**2*(B**2/n2**2 + 1/(mu2**2*n2**2))**2) - 1.5625e+38*B/(n2**3*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2)))))*(-6.25e+18*B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) - 6.25e+18*B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) - 6.25e+18*B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) - 6.25e+18*B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))/((1/(mu4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))*abs(n4)) + 1/(mu3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))*abs(n3)) + 1/(mu2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))*abs(n2)) + 1/(mu1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))*abs(n1)))**2 + (B/(n4*(3.90625e+37*B**2/n4**2 + 3.90625e+37/(mu4**2*n4**2))) + B/(n3*(3.90625e+37*B**2/n3**2 + 3.90625e+37/(mu3**2*n3**2))) + B/(n2*(3.90625e+37*B**2/n2**2 + 3.90625e+37/(mu2**2*n2**2))) + B/(n1*(3.90625e+37*B**2/n1**2 + 3.90625e+37/(mu1**2*n1**2))))**2)**2)
NameError: name 'DiracDelta' is not defined
I am running this git repo code which is supposed to run out of the box with no error (new repo). So, I am not sure why I get this error. I have not change the original code and I am using the same exact dataset as the DJ-RN repo maintainer:
Initially, the error is happening here (even after changing and to & and or to |).
if radius is not None:
value = {t1: _t1, t2: _t2, t3: _t3, x_mid: _x_mid, theta: _theta, focal_length: _focal_length, vec_20: vec_2[0],
vec_21: vec_2[1], vec_22: vec_2[2], r: radius}
for i in range(4):
ansx = root1[i][0].evalf(subs=value)
ansy = root1[i][1].evalf(subs=value)
ansz = root1[i][2].evalf(subs=value)
print('ansx is: ', ansx)
print('ansy is: ', ansy)
print('ansz is: ', ansz)
print('_t1 is: ', _t1)
print('_t2 is: ', _t2)
print('_t3 is: ', _t3)
print('_focal_length is: ', _focal_length)
y2D = (-ansy + _t2) / (ansz + _t3) * _focal_length
x2D = (-ansx + _t1) / (ansz + _t3) * _focal_length
print('y2D is: ', y2D)
print('x2D is: ', x2D)
if (((y2D >= obox[1]) & (y2D <= obox[3])) | ((y2D <= obox[1]) & (y2D >= obox[3]))):
idx = i
ansx = root1[idx][0].evalf(subs=value)
ansy = root1[idx][1].evalf(subs=value)
ansz = root1[idx][2].evalf(subs=value)
print('ansz free symbols: ', ansz.free_symbols)
print('line 259: ansz is: ', ansz)
print('maxz is: ', maxz)
print('minz is: ', minz)
if ((ansz > maxz) or (ansz < minz)):
Previously this is the line that threw an error (same as title):
if (((y2D >= obox[1]) and (y2D <= obox[3])) or ((y2D <= obox[1]) and (y2D >= obox[3]))):
Then following this Stackoverflow answer by #sylee957 , I changed it to the following but no solution:
if (((y2D >= obox[1]) & (y2D <= obox[3])) | ((y2D <= obox[1]) & (y2D >= obox[3]))):
and I get:
....
y2D is: 5000*(0.321072 - (focal_length*t2*sin(theta/2)*Abs(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2) + ((-2.0*t3*(focal_length**2 + x_mid**2)*(focal_length**2*vec_22*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) + 1.4142135623731*focal_length*r*vec_21*(0.5*focal_length**2*vec_20**2*sin(theta/2)**2 - 0.5*focal_length**2*vec_20**2 + 0.5*focal_length**2*vec_21**2*sin(theta/2)**2 + 0.5*focal_length**2*vec_22**2*sin(theta/2)**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_21**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_22**2*x_mid**2*sin(theta/2)**2 - 0.5*vec_22**2*x_mid**2)**0.5 + focal_length*vec_20*x_mid*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) - t3*(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2)))*(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*sin(theta/2)**2 - 2.0*(focal_length**2 + x_mid**2)*(0.707106781186547*focal_length**2*vec_22*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) + focal_length*r*vec_21*(0.5*focal_length**2*vec_20**2*sin(theta/2)**2 - 0.5*focal_length**2*vec_20**2 + 0.5*focal_length**2*vec_21**2*sin(theta/2)**2 + 0.5*focal_length**2*vec_22**2*sin(theta/2)**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_21**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_22**2*x_mid**2*sin(theta/2)**2 - 0.5*vec_22**2*x_mid**2)**0.5 + 0.707106781186547*focal_length*vec_20*x_mid*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) - 0.707106781186547*t3*(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2)))**2*Abs(sin(theta/2)) + 4.0*(focal_length**2*r**2 - focal_length**2*t3**2*sin(theta/2)**2 - t3**2*x_mid**2*sin(theta/2)**2)*(0.5*focal_length**2*vec_21**2 + 0.5*focal_length**2*vec_22**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2 + 0.5*vec_21**2*x_mid**2)**2*Abs(sin(theta/2)))/Abs(sin(theta/2)))**0.5)/(focal_length*sin(theta/2)*Abs(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)))/((focal_length**2*vec_22*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) + 1.4142135623731*focal_length*r*vec_21*(0.5*focal_length**2*vec_20**2*sin(theta/2)**2 - 0.5*focal_length**2*vec_20**2 + 0.5*focal_length**2*vec_21**2*sin(theta/2)**2 + 0.5*focal_length**2*vec_22**2*sin(theta/2)**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_21**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_22**2*x_mid**2*sin(theta/2)**2 - 0.5*vec_22**2*x_mid**2)**0.5 + focal_length*vec_20*x_mid*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) - t3*(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2)))/((focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2))) + 23.578)
x2D is: 5000*(0.477802 - (-focal_length**2*t1*vec_21**2 - focal_length**2*t1*vec_22**2 - 2.0*focal_length*t1*vec_20*vec_22*x_mid + focal_length*vec_22*x_mid*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r)/Abs(sin(theta/2)) + 1.4142135623731*r*vec_21*x_mid*(0.5*focal_length**2*vec_20**2*sin(theta/2)**2 - 0.5*focal_length**2*vec_20**2 + 0.5*focal_length**2*vec_21**2*sin(theta/2)**2 + 0.5*focal_length**2*vec_22**2*sin(theta/2)**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_21**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_22**2*x_mid**2*sin(theta/2)**2 - 0.5*vec_22**2*x_mid**2)**0.5/Abs(sin(theta/2)) - t1*vec_20**2*x_mid**2 - t1*vec_21**2*x_mid**2 + vec_20*x_mid**2*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r)/Abs(sin(theta/2)))/(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2))/((focal_length**2*vec_22*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) + 1.4142135623731*focal_length*r*vec_21*(0.5*focal_length**2*vec_20**2*sin(theta/2)**2 - 0.5*focal_length**2*vec_20**2 + 0.5*focal_length**2*vec_21**2*sin(theta/2)**2 + 0.5*focal_length**2*vec_22**2*sin(theta/2)**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_21**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_22**2*x_mid**2*sin(theta/2)**2 - 0.5*vec_22**2*x_mid**2)**0.5 + focal_length*vec_20*x_mid*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) - t3*(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2)))/((focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2))) + 23.578)
line 259: ansz is: (focal_length**2*vec_22*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) + 1.4142135623731*focal_length*r*vec_21*(0.5*focal_length**2*vec_20**2*sin(theta/2)**2 - 0.5*focal_length**2*vec_20**2 + 0.5*focal_length**2*vec_21**2*sin(theta/2)**2 + 0.5*focal_length**2*vec_22**2*sin(theta/2)**2 + focal_length*vec_20*vec_22*x_mid + 0.5*vec_20**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_21**2*x_mid**2*sin(theta/2)**2 + 0.5*vec_22**2*x_mid**2*sin(theta/2)**2 - 0.5*vec_22**2*x_mid**2)**0.5 + focal_length*vec_20*x_mid*(vec_20**2 + vec_21**2 + vec_22**2)**0.5*cos(theta/2)*Abs(r) - t3*(focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2)))/((focal_length**2*vec_21**2 + focal_length**2*vec_22**2 + 2.0*focal_length*vec_20*vec_22*x_mid + vec_20**2*x_mid**2 + vec_21**2*x_mid**2)*Abs(sin(theta/2)))
maxz is: 0.47865486
minz is: -0.43096042
Traceback (most recent call last):
File "test.py", line 32, in <module>
otri, _ = get_param(result, hbox, obox, htri, img, radius, gamma_min, gamma_max)
File "/home/mona/research/code/DJ-RN/script/generate_utils.py", line 261, in get_param
if (ansz > maxz or ansz < minz):
File "/home/mona/anaconda3/lib/python3.8/site-packages/sympy/core/relational.py", line 384, in __nonzero__
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
As you see x2D and y2D (and respectively ansz) have lots of free parameters. I am not sure why they are not getting values and how to go after debugging this issue.
Here is the script that throws the error: test.py https://pastebin.com/raw/tnqK44az
and here's the code that the error is rising from:https://pastebin.com/raw/fyjpEdES
Here's the git discussion I created regarding this problem I am facing: https://github.com/sympy/sympy/discussions/20690
Here's the full log of running $ python test.py: https://pastebin.com/raw/g4xSbPAe
It's say that
(((y2D >= obox[1]) and (y2D <= obox[3])) or ((y2D <= obox[1]) and (y2D >= obox[3])))
or
(((y2D >= obox[1]) & (y2D <= obox[3])) | ((y2D <= obox[1]) & (y2D >= obox[3])))
cannot be used as a condition for an if statement. An if requires a simple True or False. numpy uses often have problems like this because a conditional on an array produces a boolean array, e.g. np.array([True,False,True]), and they get an Ambiguity error. This appears to be the sympy equivalent.
The and/or in the first first also has this problem because they implicitly have an if test as part of their evaluation.
I'd suggest understanding exactly what these conditions are doing (or supposed to be doing). You may have to examine the pieces (y2D >= obox[1]) etc.
You may also need to raise the issue with the original repo github. And double check that your set up matches the original. Trying out-of-the-box code with your own data or variables can be tricky, especially if you don't have much experience with the underlying language (such as Python and sympy). Any little deviation for the source's intentions can lead to hard to understand errors.
In an isympy sessionI have a symbolx`:
In [31]: x
Out[31]: x
In [32]: x>0 # this is a relational
Out[32]: x > 0
In [33]: if x>0: print('yes')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-cea59247e99a> in <module>
----> 1 if x>0: print('yes')
/usr/local/lib/python3.8/dist-packages/sympy/core/relational.py in __bool__(self)
393
394 def __bool__(self):
--> 395 raise TypeError("cannot determine truth value of Relational")
396
397 def _eval_as_set(self):
TypeError: cannot determine truth value of Relational
If I do a subs to replace the variable with a numeric value, the if works:
In [35]: x.subs({x:1})
Out[35]: 1
In [36]: if x.subs({x:1})>0: print('yes')
yes
I have to simplify a transfer function using sympy. I am used to maxima and I am looking for advice to get similar performances in a python environment.
Using the following Maxima code:
A:-Avol0/(1+s/(2*pi*fp));
Zph:Rsh/(1+Rsh*Cj*s);
Zf:Rf/(1+Rf*Cf*s);
alpha:Zf*Zph/(Zf+Zph);
beta:Zph/(Zf+Zph);
BetaA:ratsimp(beta*A,s);
H:ratsimp(alpha*A/(1-BetaA),s);
I get the following:
(H)-> -(2*Avol0*Rf*Rsh*fp*pi)/((Cj+Cf)*Rf*Rsh*s^2+((2*Cj+(2*Avol0+2)*Cf)*Rf*Rsh*fp*pi+Rsh+Rf)*s+((2*Avol0+2)*Rsh+2*Rf)*fp*pi)
The same opertions in sympy do not get to such a nice result:
import numpy as np
import sympy as sy
"""
Formulas
"""
s, Rf, Cf, Rsh, Cj, Cd, Ccm, GBP, Avol0, fp, w = \
sy.symbols("s Rf Cf Rsh Cj Cd Ccm GBP Avol0 fp w")
A = -Avol0/(1+s/(2*np.pi*fp))
Zph = Rsh/(1+Rsh*Cj*s)
Zf = Rf/(1+Rf*Cf*s)
alpha = Zf*Zph/(Zf+Zph)
beta = Zph/(Zf+Zph)
Gloop = sy.ratsimp(beta*A)
H = alpha*A/(1-Gloop)
sy.ratsimp(H)
returns an unreadable result:
-1.0*(1.0*Avol0*Cf**2*Cj*Rf**3*Rsh**3*fp**2*s**3 + 0.159154943091895*Avol0*Cf**2*Cj*Rf**3*Rsh**3*fp*s**4 + 1.0*Avol0*Cf**2*Rf**3*Rsh**2*fp**2*s**2 + 0.159154943091895*Avol0*Cf**2*Rf**3*Rsh**2*fp*s**3 + 1.0*Avol0*Cf*Cj**2*Rf**3*Rsh**3*fp**2*s**3 + 0.159154943091895*Avol0*Cf*Cj**2*Rf**3*Rsh**3*fp*s**4 + 2.0*Avol0*Cf*Cj*Rf**3*Rsh**2*fp**2*s**2 + 0.318309886183791*Avol0*Cf*Cj*Rf**3*Rsh**2*fp*s**3 + 2.0*Avol0*Cf*Cj*Rf**2*Rsh**3*fp**2*s**2 + 0.318309886183791*Avol0*Cf*Cj*Rf**2*Rsh**3*fp*s**3 + 1.0*Avol0*Cf*Rf**3*Rsh*fp**2*s + 0.159154943091895*Avol0*Cf*Rf**3*Rsh*fp*s**2 + 2.0*Avol0*Cf*Rf**2*Rsh**2*fp**2*s + 0.318309886183791*Avol0*Cf*Rf**2*Rsh**2*fp*s**2 + 1.0*Avol0*Cj**2*Rf**2*Rsh**3*fp**2*s**2 + 0.159154943091895*Avol0*Cj**2*Rf**2*Rsh**3*fp*s**3 + 2.0*Avol0*Cj*Rf**2*Rsh**2*fp**2*s + 0.318309886183791*Avol0*Cj*Rf**2*Rsh**2*fp*s**2 + 1.0*Avol0*Cj*Rf*Rsh**3*fp**2*s + 0.159154943091895*Avol0*Cj*Rf*Rsh**3*fp*s**2 + 1.0*Avol0*Rf**2*Rsh*fp**2 + 0.159154943091895*Avol0*Rf**2*Rsh*fp*s + 1.0*Avol0*Rf*Rsh**2*fp**2 + 0.159154943091895*Avol0*Rf*Rsh**2*fp*s)/(1.0*Avol0*Cf**3*Cj*Rf**3*Rsh**3*fp**2*s**4 + 0.159154943091895*Avol0*Cf**3*Cj*Rf**3*Rsh**3*fp*s**5 + 1.0*Avol0*Cf**3*Rf**3*Rsh**2*fp**2*s**3 + 0.159154943091895*Avol0*Cf**3*Rf**3*Rsh**2*fp*s**4 + 1.0*Avol0*Cf**2*Cj**2*Rf**3*Rsh**3*fp**2*s**4 + 0.159154943091895*Avol0*Cf**2*Cj**2*Rf**3*Rsh**3*fp*s**5 + 2.0*Avol0*Cf**2*Cj*Rf**3*Rsh**2*fp**2*s**3 + 0.318309886183791*Avol0*Cf**2*Cj*Rf**3*Rsh**2*fp*s**4 + 3.0*Avol0*Cf**2*Cj*Rf**2*Rsh**3*fp**2*s**3 + 0.477464829275686*Avol0*Cf**2*Cj*Rf**2*Rsh**3*fp*s**4 + 1.0*Avol0*Cf**2*Rf**3*Rsh*fp**2*s**2 + 0.159154943091895*Avol0*Cf**2*Rf**3*Rsh*fp*s**3 + 3.0*Avol0*Cf**2*Rf**2*Rsh**2*fp**2*s**2 + 0.477464829275686*Avol0*Cf**2*Rf**2*Rsh**2*fp*s**3 + 2.0*Avol0*Cf*Cj**2*Rf**2*Rsh**3*fp**2*s**3 + 0.318309886183791*Avol0*Cf*Cj**2*Rf**2*Rsh**3*fp*s**4 + 4.0*Avol0*Cf*Cj*Rf**2*Rsh**2*fp**2*s**2 + 0.636619772367581*Avol0*Cf*Cj*Rf**2*Rsh**2*fp*s**3 + 3.0*Avol0*Cf*Cj*Rf*Rsh**3*fp**2*s**2 + 0.477464829275686*Avol0*Cf*Cj*Rf*Rsh**3*fp*s**3 + 2.0*Avol0*Cf*Rf**2*Rsh*fp**2*s + 0.318309886183791*Avol0*Cf*Rf**2*Rsh*fp*s**2 + 3.0*Avol0*Cf*Rf*Rsh**2*fp**2*s + 0.477464829275686*Avol0*Cf*Rf*Rsh**2*fp*s**2 + 1.0*Avol0*Cj**2*Rf*Rsh**3*fp**2*s**2 + 0.159154943091895*Avol0*Cj**2*Rf*Rsh**3*fp*s**3 + 2.0*Avol0*Cj*Rf*Rsh**2*fp**2*s + 0.318309886183791*Avol0*Cj*Rf*Rsh**2*fp*s**2 + 1.0*Avol0*Cj*Rsh**3*fp**2*s + 0.159154943091895*Avol0*Cj*Rsh**3*fp*s**2 + 1.0*Avol0*Rf*Rsh*fp**2 + 0.159154943091895*Avol0*Rf*Rsh*fp*s + 1.0*Avol0*Rsh**2*fp**2 + 0.159154943091895*Avol0*Rsh**2*fp*s + 1.0*Cf**3*Cj*Rf**3*Rsh**3*fp**2*s**4 + 0.318309886183791*Cf**3*Cj*Rf**3*Rsh**3*fp*s**5 + 0.0253302959105844*Cf**3*Cj*Rf**3*Rsh**3*s**6 + 1.0*Cf**3*Rf**3*Rsh**2*fp**2*s**3 + 0.318309886183791*Cf**3*Rf**3*Rsh**2*fp*s**4 + 0.0253302959105844*Cf**3*Rf**3*Rsh**2*s**5 + 2.0*Cf**2*Cj**2*Rf**3*Rsh**3*fp**2*s**4 + 0.636619772367581*Cf**2*Cj**2*Rf**3*Rsh**3*fp*s**5 + 0.0506605918211689*Cf**2*Cj**2*Rf**3*Rsh**3*s**6 + 4.0*Cf**2*Cj*Rf**3*Rsh**2*fp**2*s**3 + 1.27323954473516*Cf**2*Cj*Rf**3*Rsh**2*fp*s**4 + 0.101321183642338*Cf**2*Cj*Rf**3*Rsh**2*s**5 + 3.0*Cf**2*Cj*Rf**2*Rsh**3*fp**2*s**3 + 0.954929658551372*Cf**2*Cj*Rf**2*Rsh**3*fp*s**4 + 0.0759908877317533*Cf**2*Cj*Rf**2*Rsh**3*s**5 + 2.0*Cf**2*Rf**3*Rsh*fp**2*s**2 + 0.636619772367581*Cf**2*Rf**3*Rsh*fp*s**3 + 0.0506605918211689*Cf**2*Rf**3*Rsh*s**4 + 3.0*Cf**2*Rf**2*Rsh**2*fp**2*s**2 + 0.954929658551372*Cf**2*Rf**2*Rsh**2*fp*s**3 + 0.0759908877317533*Cf**2*Rf**2*Rsh**2*s**4 + 1.0*Cf*Cj**3*Rf**3*Rsh**3*fp**2*s**4 + 0.318309886183791*Cf*Cj**3*Rf**3*Rsh**3*fp*s**5 + 0.0253302959105844*Cf*Cj**3*Rf**3*Rsh**3*s**6 + 3.0*Cf*Cj**2*Rf**3*Rsh**2*fp**2*s**3 + 0.954929658551372*Cf*Cj**2*Rf**3*Rsh**2*fp*s**4 + 0.0759908877317533*Cf*Cj**2*Rf**3*Rsh**2*s**5 + 4.0*Cf*Cj**2*Rf**2*Rsh**3*fp**2*s**3 + 1.27323954473516*Cf*Cj**2*Rf**2*Rsh**3*fp*s**4 + 0.101321183642338*Cf*Cj**2*Rf**2*Rsh**3*s**5 + 3.0*Cf*Cj*Rf**3*Rsh*fp**2*s**2 + 0.954929658551372*Cf*Cj*Rf**3*Rsh*fp*s**3 + 0.0759908877317533*Cf*Cj*Rf**3*Rsh*s**4 + 8.0*Cf*Cj*Rf**2*Rsh**2*fp**2*s**2 + 2.54647908947033*Cf*Cj*Rf**2*Rsh**2*fp*s**3 + 0.202642367284676*Cf*Cj*Rf**2*Rsh**2*s**4 + 3.0*Cf*Cj*Rf*Rsh**3*fp**2*s**2 + 0.954929658551372*Cf*Cj*Rf*Rsh**3*fp*s**3 + 0.0759908877317533*Cf*Cj*Rf*Rsh**3*s**4 + 1.0*Cf*Rf**3*fp**2*s + 0.318309886183791*Cf*Rf**3*fp*s**2 + 0.0253302959105844*Cf*Rf**3*s**3 + 4.0*Cf*Rf**2*Rsh*fp**2*s + 1.27323954473516*Cf*Rf**2*Rsh*fp*s**2 + 0.101321183642338*Cf*Rf**2*Rsh*s**3 + 3.0*Cf*Rf*Rsh**2*fp**2*s + 0.954929658551372*Cf*Rf*Rsh**2*fp*s**2 + 0.0759908877317533*Cf*Rf*Rsh**2*s**3 + 1.0*Cj**3*Rf**2*Rsh**3*fp**2*s**3 + 0.318309886183791*Cj**3*Rf**2*Rsh**3*fp*s**4 + 0.0253302959105844*Cj**3*Rf**2*Rsh**3*s**5 + 3.0*Cj**2*Rf**2*Rsh**2*fp**2*s**2 + 0.954929658551372*Cj**2*Rf**2*Rsh**2*fp*s**3 + 0.0759908877317533*Cj**2*Rf**2*Rsh**2*s**4 + 2.0*Cj**2*Rf*Rsh**3*fp**2*s**2 + 0.636619772367581*Cj**2*Rf*Rsh**3*fp*s**3 + 0.0506605918211689*Cj**2*Rf*Rsh**3*s**4 + 3.0*Cj*Rf**2*Rsh*fp**2*s + 0.954929658551372*Cj*Rf**2*Rsh*fp*s**2 + 0.0759908877317533*Cj*Rf**2*Rsh*s**3 + 4.0*Cj*Rf*Rsh**2*fp**2*s + 1.27323954473516*Cj*Rf*Rsh**2*fp*s**2 + 0.101321183642338*Cj*Rf*Rsh**2*s**3 + 1.0*Cj*Rsh**3*fp**2*s + 0.318309886183791*Cj*Rsh**3*fp*s**2 + 0.0253302959105844*Cj*Rsh**3*s**3 + 1.0*Rf**2*fp**2 + 0.318309886183791*Rf**2*fp*s + 0.0253302959105844*Rf**2*s**2 + 2.0*Rf*Rsh*fp**2 + 0.636619772367581*Rf*Rsh*fp*s + 0.0506605918211689*Rf*Rsh*s**2 + 1.0*Rsh**2*fp**2 + 0.318309886183791*Rsh**2*fp*s + 0.0253302959105844*Rsh**2*s**2)
There is a difference between the maxima code and the python one: const 'pi' is kept symbolic in the first case, and approximated to a floating-point value in the second. Replacing np.pi with pi solves the problem, anyway it is weird how sympy tries to simplify the expression when pi is numeric.
I have the following symbols in my Sympy script
e0 = Symbol('e0',commutative=False)
e1 = Symbol('e1',commutative=False)
e2 = Symbol('e2',commutative=False)
e3 = Symbol('e3',commutative=False)
u = Symbol('u',commutative=False)
d0 = Symbol('d0',commutative=False)
d1 = Symbol('d1',commutative=False)
d2 = Symbol('d2',commutative=False)
d3 = Symbol('d3',commutative=False)
A_0 = (Symbol('a_00',commutative=False),Symbol('a_01',commutative=False),Symbol('a_02',commutative=False),Symbol('a_03',commutative=False))
A_1 = symbols('a_10:14',commutative=False)
A_2 = symbols('a_20:24',commutative=False)
A_3 = symbols('a_30:34',commutative=False)
B = symbols('b_0:4',commutative=False)
C = Symbol('C',commutative=False)
And I have a really big string:
e0*((d0*a_00)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_00*(-e1*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)-e2*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)-e3*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u))) + e1*((d0*a_01)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_01*(-e1*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)-e2*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)-e3*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u))) + e2*((d0*a_02)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_02*(-e1*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)-e2*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)-e3*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u))) + e3*((d0*a_03)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_03*(-e1*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)-e2*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)-e3*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u))) + e0*((d0*a_10)*(d1*u)+a_10*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + e1*((d0*a_11)*(d1*u)+a_11*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + e2*((d0*a_12)*(d1*u)+a_12*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + e3*((d0*a_13)*(d1*u)+a_13*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + e0*((d0*a_20)*(d2*u)+a_20*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + e1*((d0*a_21)*(d2*u)+a_21*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + e2*((d0*a_22)*(d2*u)+a_22*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + e3*((d0*a_23)*(d2*u)+a_23*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + e0*((d0*a_30)*(d3*u)+a_30*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + e1*((d0*a_31)*(d3*u)+a_31*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + e2*((d0*a_32)*(d3*u)+a_32*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + e3*((d0*a_33)*(d3*u)+a_33*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + e0*((d0*b_0)*u+b_0*(-e1*d1*u-e2*d2*u-e3*d3*u)) + e1*((d0*b_1)*u+b_1*(-e1*d1*u-e2*d2*u-e3*d3*u)) + e2*((d0*b_2)*u+b_2*(-e1*d1*u-e2*d2*u-e3*d3*u)) + e3*((d0*b_3)*u+b_3*(-e1*d1*u-e2*d2*u-e3*d3*u)) + e_1*((d1*a_00)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_00*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + -e0*((d1*a_01)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_01*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + e3*((d1*a_02)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_02*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + -e2*((d1*a_03)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_03*(-e1*d1*d1*u-e2*d1*d2*u-e3*d1*d3*u)) + e_1*((d1*a_10)*(d1*u)+a_10*(d1*d1*u)) + -e0*((d1*a_11)*(d1*u)+a_11*(d1*d1*u)) + e3*((d1*a_12)*(d1*u)+a_12*(d1*d1*u)) + -e2*((d1*a_13)*(d1*u)+a_13*(d1*d1*u)) + e_1*((d1*a_20)*(d2*u)+a_20*(d1*d2*u)) + -e0*((d1*a_21)*(d2*u)+a_21*(d1*d2*u)) + e3*((d1*a_22)*(d2*u)+a_22*(d1*d2*u)) + -e2*((d1*a_23)*(d2*u)+a_23*(d1*d2*u)) + e_1*((d1*a_30)*(d3*u)+a_30*(d1*d3*u)) + -e0*((d1*a_31)*(d3*u)+a_31*(d1*d3*u)) + e3*((d1*a_32)*(d3*u)+a_32*(d1*d3*u)) + -e2*((d1*a_33)*(d3*u)+a_33*(d1*d3*u)) + e_1*((d1*b_0)*u+b_0*(d1*u)) + -e0*((d1*b_1)*u+b_1*(d1*u)) + e3*((d1*b_2)*u+b_2*(d1*u)) + -e2*((d1*b_3)*u+b_3*(d1*u)) + e2*((d2*a_00)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_00*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + -e3*((d2*a_01)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_01*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + -e0*((d2*a_02)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_02*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + e1*((d2*a_03)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_03*(-e1*d2*d1*u-e2*d2*d2*u-e3*d2*d3*u)) + e2*((d2*a_10)*(d1*u)+a_10*(d2*d1*u)) + -e3*((d2*a_11)*(d1*u)+a_11*(d2*d1*u)) + -e0*((d2*a_12)*(d1*u)+a_12*(d2*d1*u)) + e1*((d2*a_13)*(d1*u)+a_13*(d2*d1*u)) + e2*((d2*a_20)*(d2*u)+a_20*(d2*d2*u)) + -e3*((d2*a_21)*(d2*u)+a_21*(d2*d2*u)) + -e0*((d2*a_22)*(d2*u)+a_22*(d2*d2*u)) + e1*((d2*a_23)*(d2*u)+a_23*(d2*d2*u)) + e2*((d2*a_30)*(d3*u)+a_30*(d2*d3*u)) + -e3*((d2*a_31)*(d3*u)+a_31*(d2*d3*u)) + -e0*((d2*a_32)*(d3*u)+a_32*(d2*d3*u)) + e1*((d2*a_33)*(d3*u)+a_33*(d2*d3*u)) + e2*((d2*b_0)*u+b_0*(d2*u)) + -e3*((d2*b_1)*u+b_1*(d2*u)) + -e0*((d2*b_2)*u+b_2*(d2*u)) + e1*((d2*b_3)*u+b_3*(d2*u)) + e3*((d3*a_00)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_00*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + e2*((d3*a_01)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_01*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + -e1*((d3*a_02)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_02*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + -e0*((d3*a_03)*(-e1*d1*u-e2*d2*u-e3*d3*u)+a_03*(-e1*d3*d1*u-e2*d3*d2*u-e3*d3*d3*u)) + e3*((d3*a_10)*(d1*u)+a_10*(d3*d1*u)) + e2*((d3*a_11)*(d1*u)+a_11*(d3*d1*u)) + -e1*((d3*a_12)*(d1*u)+a_12*(d3*d1*u)) + -e0*((d3*a_13)*(d1*u)+a_13*(d3*d1*u)) + e3*((d3*a_20)*(d2*u)+a_20*(d3*d2*u)) + e2*((d3*a_21)*(d2*u)+a_21*(d3*d2*u)) + -e1*((d3*a_22)*(d2*u)+a_22*(d3*d2*u)) + -e0*((d3*a_23)*(d2*u)+a_23*(d3*d2*u)) + e3*((d3*a_30)*(d3*u)+a_30*(d3*d3*u)) + e2*((d3*a_31)*(d3*u)+a_31*(d3*d3*u)) + -e1*((d3*a_32)*(d3*u)+a_32*(d3*d3*u)) + -e0*((d3*a_33)*(d3*u)+a_33*(d3*d3*u)) + e3*((d3*b_0)*u+b_0*(d3*u)) + e2*((d3*b_1)*u+b_1*(d3*u)) + -e1*((d3*b_2)*u+b_2*(d3*u)) + -e0*((d3*b_3)*u+b_3*(d3*u))
This is what I do to sympify it:
ns={"e0":e0,"e1":e1,"e2":e2,"e3":e3,"u":u,"d0":d0,"d1":d1,"d2":d2,"d3":d3,"a_00":A_0[0],"a_01":A_0[1],"a_02":A_0[2],"a_03":A_0[3],"a_10":A_1[0],"a_11":A_1[1],"a_12":A_1[2],"a_13":A_1[3],"a_20":A_2[0],"a_21":A_2[1],"a_22":A_2[2],"a_23":A_2[3],"a_30":A_3[0],"a_31":A_3[1],"a_32":A_3[2],"a_33":A_3[3]}
print(sympify(result,locals=ns,evaluate=False))
and the result is very different from what I had:
e_1*(a_00*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d1*a_00*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e_1*(d1*b_0*u + b_0*d1*u) + e_1*(a_10*d1*d1*u + d1*a_10*d1*u) + e_1*(a_20*d1*d2*u + d1*a_20*d2*u) + e_1*(a_30*d1*d3*u + d1*a_30*d3*u) + e0*(a_00*(-e2*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) - e3*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + (d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u)*(-e1)) + d0*a_00*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e0*(a_10*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d0*a_10*d1*u) + e0*(a_20*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d0*a_20*d2*u) + e0*(a_30*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d0*a_30*d3*u) + e0*(d0*b_0*u + b_0*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e1*(a_01*(-e2*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) - e3*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + (d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u)*(-e1)) + d0*a_01*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e1*(a_03*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d2*a_03*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e1*(a_11*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d0*a_11*d1*u) + e1*(a_21*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d0*a_21*d2*u) + e1*(a_31*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d0*a_31*d3*u) + e1*(d0*b_1*u + b_1*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e1*(d2*b_3*u + b_3*d2*u) + e1*(a_13*d2*d1*u + d2*a_13*d1*u) + e1*(a_23*d2*d2*u + d2*a_23*d2*u) + e1*(a_33*d2*d3*u + d2*a_33*d3*u) + e2*(a_00*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d2*a_00*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e2*(a_01*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d3*a_01*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e2*(a_02*(-e2*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) - e3*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + (d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u)*(-e1)) + d0*a_02*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e2*(a_12*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d0*a_12*d1*u) + e2*(a_22*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d0*a_22*d2*u) + e2*(a_32*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d0*a_32*d3*u) + e2*(d0*b_2*u + b_2*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e2*(d2*b_0*u + b_0*d2*u) + e2*(d3*b_1*u + b_1*d3*u) + e2*(a_10*d2*d1*u + d2*a_10*d1*u) + e2*(a_11*d3*d1*u + d3*a_11*d1*u) + e2*(a_20*d2*d2*u + d2*a_20*d2*u) + e2*(a_21*d3*d2*u + d3*a_21*d2*u) + e2*(a_30*d2*d3*u + d2*a_30*d3*u) + e2*(a_31*d3*d3*u + d3*a_31*d3*u) + e3*(a_00*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d3*a_00*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e3*(a_02*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d1*a_02*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e3*(a_03*(-e2*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) - e3*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + (d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u)*(-e1)) + d0*a_03*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e3*(a_13*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d0*a_13*d1*u) + e3*(a_23*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d0*a_23*d2*u) + e3*(a_33*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d0*a_33*d3*u) + e3*(d0*b_3*u + b_3*(d1*(-e1)*u - e2*d2*u - e3*d3*u)) + e3*(d1*b_2*u + b_2*d1*u) + e3*(d3*b_0*u + b_0*d3*u) + e3*(a_10*d3*d1*u + d3*a_10*d1*u) + e3*(a_12*d1*d1*u + d1*a_12*d1*u) + e3*(a_20*d3*d2*u + d3*a_20*d2*u) + e3*(a_22*d1*d2*u + d1*a_22*d2*u) + e3*(a_30*d3*d3*u + d3*a_30*d3*u) + e3*(a_32*d1*d3*u + d1*a_32*d3*u) + (a_01*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d1*a_01*(d1*(-e1)*u - e2*d2*u - e3*d3*u))*(-e0) + (a_01*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d2*a_01*(d1*(-e1)*u - e2*d2*u - e3*d3*u))*(-e3) + (a_02*(d2*(-e1)*d1*u - e2*d2**2*u - e3*d2*d3*u) + d2*a_02*(d1*(-e1)*u - e2*d2*u - e3*d3*u))*(-e0) + (a_02*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d3*a_02*(d1*(-e1)*u - e2*d2*u - e3*d3*u))*(-e1) + (a_03*(d1*(-e1)*d1*u - e2*d1*d2*u - e3*d1*d3*u) + d1*a_03*(d1*(-e1)*u - e2*d2*u - e3*d3*u))*(-e2) + (a_03*(d3*(-e1)*d1*u - e2*d3*d2*u - e3*d3**2*u) + d3*a_03*(d1*(-e1)*u - e2*d2*u - e3*d3*u))*(-e0) + (d1*b_1*u + b_1*d1*u)*(-e0) + (d1*b_3*u + b_3*d1*u)*(-e2) + (d2*b_1*u + b_1*d2*u)*(-e3) + (d2*b_2*u + b_2*d2*u)*(-e0) + (d3*b_2*u + b_2*d3*u)*(-e1) + (d3*b_3*u + b_3*d3*u)*(-e0) + (a_11*d1*d1*u + d1*a_11*d1*u)*(-e0) + (a_11*d2*d1*u + d2*a_11*d1*u)*(-e3) + (a_12*d2*d1*u + d2*a_12*d1*u)*(-e0) + (a_12*d3*d1*u + d3*a_12*d1*u)*(-e1) + (a_13*d1*d1*u + d1*a_13*d1*u)*(-e2) + (a_13*d3*d1*u + d3*a_13*d1*u)*(-e0) + (a_21*d1*d2*u + d1*a_21*d2*u)*(-e0) + (a_21*d2*d2*u + d2*a_21*d2*u)*(-e3) + (a_22*d2*d2*u + d2*a_22*d2*u)*(-e0) + (a_22*d3*d2*u + d3*a_22*d2*u)*(-e1) + (a_23*d1*d2*u + d1*a_23*d2*u)*(-e2) + (a_23*d3*d2*u + d3*a_23*d2*u)*(-e0) + (a_31*d1*d3*u + d1*a_31*d3*u)*(-e0) + (a_31*d2*d3*u + d2*a_31*d3*u)*(-e3) + (a_32*d2*d3*u + d2*a_32*d3*u)*(-e0) + (a_32*d3*d3*u + d3*a_32*d3*u)*(-e1) + (a_33*d1*d3*u + d1*a_33*d3*u)*(-e2) + (a_33*d3*d3*u + d3*a_33*d3*u)*(-e0)
You didn't include B in your ns, so the b symbols were created by sympify automatically as commutative Symbols (you also never defined e_1 by the way).
I recommend being cleaner with your processes to avoid issues like this:
Be consistent with _ vs no _ before numeric suffixes. Either use a_0 or a0 everywhere.
Name your variables the same as the symbol names.
If you have your string at execution time, don't use sympify. Rather, just define your symbol names and execute the string as an expression (expr = e0*((d0*a_00)*(-e1...). This way, Python will automatically warn you if you have undefined symbol names.
If you do have to parse strings, you can avoid automatic symbol definition by using parse_expr:
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, auto_symbol
transformations = list(standard_transformations)
transformations.remove(auto_symbol)
parse_expr('a*b', local_dict={'a': Symbol('a', commutative=False)}, transformations=transformations)
Note how this gives an error about b not being defined, rather than just defining it as a commutative Symbol.
I'm trying to write my mathcad model in python language, but I get some mistake.
The integration function should look like this:
In python I wrote such code
from __future__ import division
import sympy as sp
import numpy as np
import math
from pylab import *
print(sp.__version__)
s = sp.Symbol('s')
x = sp.symbols('x')
t_start = 11
t_info = 1
t_transf = 2
t_stat_analyze = 3
t_repeat = 3.2
P = 0.1
def M1(s):
return P/(t_info*t_start*t_stat_analyze*t_transf*(1 - (-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_info)*(s + 1/t_start)*(s + 1/t_stat_analyze)*(s + 1/t_transf)**2) + P/( t_info*t_start*t_stat_analyze*t_transf*(1 - (-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_info)*(s + 1/t_start)*(s + 1/t_stat_analyze)**2*(s + 1/t_transf)) + P/(t_info*t_start*t_stat_analyze*t_transf*(1 - (-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_info)*(s + 1/t_start)**2*(s + 1/t_stat_analyze)*(s + 1/t_transf)) + P/(t_info*t_start*t_stat_analyze*t_transf*(1 - (-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/ t_transf)))*(s + 1/t_info)**2*(s + 1/t_start)*(s + 1/t_stat_analyze)*(s + 1/t_transf)) - P*(-(-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)**2) - (-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)**2*(s + 1/t_transf)))/( t_info*t_start*t_stat_analyze*t_transf*(1 - (-P + 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))**2*(s + 1/t_info)*(s + 1/t_start)*(s + 1/t_stat_analyze)*(s + 1/t_transf))
def M2(s):
return 2*P*((s + 1/t_transf)**(-2) + 1/((s + 1/t_stat_analyze)*(s + 1/t_transf)) + (s + 1/t_stat_analyze)**(-2) + 1/((s + 1/t_start)*(s + 1/t_transf)) + 1/((s + 1/t_start)*(s + 1/t_stat_analyze)) + (s + 1/t_start)**(-2) + 1/((s + 1/ t_info)*(s + 1/t_transf)) + 1/((s + 1/t_info)*(s + 1/t_stat_analyze)) + 1/((s + 1/t_info)*(s + 1/t_start)) + (s + 1/t_info)**(-2) - (P - 1)*((s + 1/t_transf)**(-2) + 1/((s + 1/t_repeat)*(s + 1/t_transf)) + (s + 1/t_repeat)**(-2))/( t_repeat*t_transf*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_repeat)*(s + 1/t_transf)) - (P - 1)*(1/(s + 1/t_transf) + 1/(s + 1/t_repeat))/(t_repeat*t_transf*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/ t_repeat)*(s + 1/t_transf)))*(s + 1/t_repeat)*(s + 1/t_transf)**2) - (P - 1)*(1/(s + 1/t_transf) + 1/(s + 1/t_repeat))/(t_repeat*t_transf*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_repeat)*(s + 1/ t_stat_analyze)*(s + 1/t_transf)) - (P - 1)*(1/(s + 1/t_transf) + 1/(s + 1/t_repeat))/(t_repeat*t_transf*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_repeat)*(s + 1/t_start)*(s + 1/t_transf)) - (P - 1)*(1/( s + 1/t_transf) + 1/(s + 1/t_repeat))/(t_repeat*t_transf*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))*(s + 1/t_info)*(s + 1/t_repeat)*(s + 1/t_transf)) + (P - 1)**2*(1/(s + 1/t_transf) + 1/(s + 1/t_repeat))**2/( t_repeat**2*t_transf**2*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/t_transf)))**2*(s + 1/t_repeat)**2*(s + 1/t_transf)**2))/(t_info*t_start*t_stat_analyze*t_transf*(1 + (P - 1)/(t_repeat*t_transf*(s + 1/t_repeat)*(s + 1/ t_transf)))*(s + 1/t_info)*(s + 1/t_start)*(s + 1/t_stat_analyze)*(s + 1/t_transf))
T_realyze = M1(0)
D = M2(0)-M1(0)**2
alpha = T_realyze**2/D
myu = T_realyze/D
def F(t):
if t<0:
return 0
else:
return sp.integrate((myu**alpha)/(sp.gamma(alpha)*(x**(alpha-1))*sp.exp(myu*x)), (x, 0, t))
t=arange(0, 200, 1)
for i in t:
print(F(i))
i = i+1
So, when I'm trying to execute it, I had such error in
return sp.integrate
function:
$ python2.7 nta.py
1.0
('T_realyze = ', 63.800000000000026)
('D = ', 2696.760000000001)
('alpha = ', 1.5093816283243602)
('myu = ', 0.02365801925273291)
0
('myu*x = ', 0.0236580192527329*x)
('sp.exp(myu*x)', exp(0.0236580192527329*x))
0
1
('myu*x = ', 0.0236580192527329*x)
('sp.exp(myu*x)', exp(0.0236580192527329*x))
Traceback (most recent call last):
File "nta.py", line 48, in <module>
print(F(i))
File "nta.py", line 43, in F
return sp.integrate((myu**alpha)/(sp.gamma(alpha)*(x**(alpha-1))*sp.exp(myu*x)), (x, 0, t))
File "/root/anaconda2/lib/python2.7/site-packages/sympy/integrals/integrals.py", line 1280, in integrate
risch=risch, manual=manual)
File "/root/anaconda2/lib/python2.7/site-packages/sympy/integrals/integrals.py", line 486, in doit
conds=conds)
File "/root/anaconda2/lib/python2.7/site-packages/sympy/integrals/integrals.py", line 887, in _eval_integral
h = heurisch_wrapper(g, x, hints=[])
File "/root/anaconda2/lib/python2.7/site-packages/sympy/integrals/heurisch.py", line 130, in heurisch_wrapper
unnecessary_permutations)
File "/root/anaconda2/lib/python2.7/site-packages/sympy/integrals/heurisch.py", line 657, in heurisch
solution = _integrate('Q')
File "/root/anaconda2/lib/python2.7/site-packages/sympy/integrals/heurisch.py", line 646, in _integrate
numer = ring.from_expr(raw_numer)
File "/root/anaconda2/lib/python2.7/site-packages/sympy/polys/rings.py", line 371, in from_expr
raise ValueError("expected an expression convertible to a polynomial in %s, got %s" % (self, expr))
ValueError: expected an expression convertible to a polynomial in Polynomial ring in _x0, _x1, _x2, _x3 over RR[_A0,_A1,_A2,_A3,_A4,_A5,_A6,_A7,_A8,_A9,_A10,_A11,_A12,_A13,_A14,_A15,_A16,_A17,_A18,_A19,_A20,_A21,_A22,_A23,_A24,_A25,_A26,_A27,_A28,_A29,_A30,_A31,_A32,_A33,_A34] with lex order, got 0.50938162832436*_x3**2.96316463805253*(_A0 + _A10*_x0*_x1 + 2*_A11*_x1*_x3 + _x0**2*_A12 + _A14*_x0*_x2 + _A2*_x0 + 2*_A20*_x0*_x3 + _A24*_x1*_x2 + _x2**2*_A27 + 2*_A28*_x3 + _x1**2*_A30 + 3*_x3**2*_A31 + 2*_A6*_x2*_x3 + _A8*_x2 + _A9*_x1) + 1.50938162832436*_x3**4.92632927610506*(_A10*_x1*_x3 + 2*_A12*_x0*_x3 + _A13*_x1*_x2 + _A14*_x2*_x3 + 2*_A15*_x0 + _A16*_x2 + _x2**2*_A18 + _A2*_x3 + _x3**2*_A20 + _A21 + _x1**2*_A3 + 2*_A33*_x0*_x2 + _A34*_x1 + 3*_x0**2*_A5 + 2*_A7*_x0*_x1) - _A10*_x0*_x3 - _x3**2*_A11 - _A13*_x0*_x2 - _x2**2*_A17 - 2*_A19*_x1*_x2 - _A22 - _A24*_x2*_x3 - 2*_A25*_x1 - 3*_x1**2*_A29 - 2*_A3*_x0*_x1 - 2*_A30*_x1*_x3 - _A34*_x0 - _A4*_x2 - _x0**2*_A7 - _A9*_x3 + _x2*_x3 + 0.0236580192527329*_x2*(_A13*_x0*_x1 + _A14*_x0*_x3 + _A16*_x0 + 2*_A17*_x1*_x2 + 2*_A18*_x0*_x2 + _x1**2*_A19 + 2*_A23*_x2 + _A24*_x1*_x3 + 3*_x2**2*_A26 + 2*_A27*_x2*_x3 + _A32 + _x0**2*_A33 + _A4*_x1 + _x3**2*_A6 + _A8*_x3)
Sympy appears to have difficulties evaluating the integral with floating point coefficients (in this case). However, it can find the integral in closed form when the constants of the integrand expression are symbolic.
a, b, c, t = sp.symbols('a,b,c,t', positive = True)
f = sp.Integral(a * sp.exp(-c*x)/(x**b),(x,0,t)).doit()
print f
Output:
-a*(-b*c**b*gamma(-b + 1)*lowergamma(-b + 1, 0)/(c*gamma(-b + 2)) + c**b*gamma(-b + 1)*lowergamma(-b + 1, 0)/(c*gamma(-b + 2))) + a*(-b*c**b*gamma(-b + 1)*lowergamma(-b + 1, c*t)/(c*gamma(-b + 2)) + c**b*gamma(-b + 1)*lowergamma(-b + 1, c*t)/(c*gamma(-b + 2)))
You can substitute the constants in this expression to get numerical results as follows (here, I use an example value of t=4):
f.subs({a:(myu**alpha)/sp.gamma(alpha), b:(alpha-1), c:myu, t:4}).n()
0.0154626407404632
Another option is to use quad from scipy (again using t=4):
from scipy.integrate import quad
quad(lambda x: (myu**alpha)/(sp.gamma(alpha)*(x**(alpha-1))*sp.exp(myu*x)), 0 ,4)[0]
0.015462640740458165