Using Python's sympy module to express a complicated function -
how can symbolically express following equation using python's sympy module, such later find second derivative , therefore compute hessian matrix?
the function
what i've tried:
import sympy nc,ns,tc,ysc,theta,theta_star,t,dt,sigma_s,measured,simulated=sympy.symbols(' nc ns tc ysc theta theta_star t dt sigma_s measured simulated ') chi=(1/2*nc*ns) * sympy.mpmath.nsum( 1 / tc * sympy.integrate( ((simulated - measured) / sigma_s ) **2) , (t,0,tc))
the error:
file "c:\anaconda1\lib\site-packages\sympy\concrete\expr_with_limits.py", line 358, in __new__ "specify dummy variables %s" % function) valueerror: specify dummy variables (-measured + simulated)**2/sigma_s**2
don't use sympy.mpmath.nsum
. mpmath functions numerical calculations only. represent sum symbolically, use sympy.sum
. works this
in [3]: sum(f(x), (x, 0, n)) out[3]: n ___ ╲ ╲ f(x) ╱ ╱ ‾‾‾ x = 0
secondly, sympy has told explicitly when 1 variable depends on another. i'm unclear s
, c
in equation. if variables, should create symbols s
, c
. importantly, t
needs variable, , things depend on t
need functions like
thetastar, t = symbols('thetastar t') y_sc = function('y')
and use
y_sc(thetastar, t)
(if s
, c
supposed variables well, should use y = function('y')
, y(s, c, thetastar, t)
).
Comments
Post a Comment