ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 24 May 2019 19:10:20 +0200From numerical variables to symbolic variableshttps://ask.sagemath.org/question/46653/from-numerical-variables-to-symbolic-variables/I would like to define an Hamiltonian dynamics that I will integrate later using scipy.integrate. The Hamiltonian is computed with a symbolic expression and then I need to make substitution from the symbolic variables to the numerical variables.
N=2
var('q1 q2')
var('p1 p2')
zs = [q1,q2,p1,p2]
def dynq(t,z):
H = p1*(q1^2+1/3*q2^2) + p2*(cos(q1)-2*sin(q2*q1)^2)
jacHp = jacobian(H,tuple(ps))
dqdt = list(jacHp[0])
dqdt[0].subs({zs[i]:z[i] for i in range(0,2*N)})
dqdt[1].subs({zs[i]:z[i] for i in range(0,2*N)})
print(dqdt)
return dqdt
I call `dynq(0.,[1.,0.5,4.,2.])` and the output is
[q1^2, -2*sin(q1*q2)^2 + cos(q1)]
so q1,q2,p1,p2 are still in the expression and are not replaced by the numerical values of the list $z$.sagenotdeadFri, 24 May 2019 19:10:20 +0200https://ask.sagemath.org/question/46653/Substituting numerical values without partial evaluation.https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/Given an expression of the form:
<pre>
var('x y')
e = x*y + 1
</pre>
I would like to be able to substitute numerical values into an expression without it being simplified. I'm not too concerned about seeing it in the notebook, but I would like the latex display to be affected. In other words, I would like the equivalent of:
<pre>
latex(e(x=5,y=6)) => "5 \times 6 + 1"
</pre>
Any hope of doing this? I could substitute into the text of the latex, but then I
would get "5 6 + 1".nholtzMon, 26 Nov 2012 14:31:56 +0100https://ask.sagemath.org/question/9565/