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/Substitution of several variableshttps://ask.sagemath.org/question/32720/substitution-of-several-variables/
Let $f=f(x_1(t),x_2(t))$ be defined as follows:
sage: var('t mu')
sage: x=list()
sage: x.append( function('x1')(t) )
sage: x.append( function('x2')(t) )
sage: f = x[0] - mu*x[0]^2*x[1]
sage: f
$f = x_1(t) - \mu x_1(t)^2 x_2(t)$.
Now, I need to to substitute the term containing $x_1^2(t) x_2(t)$ in $f$ by some new auxiliary variable $x_3(t)$, that is, to obtain $f = x_1(t) - \mu x_3(t) $. However, this code doesn't work:
sage: x.append( function('x3')(t) )
sage: f.subs({x[0]^2*x[1] : x[2]})
Some ideas? Thanks.
This code is motivated by symbolic manipulation with ODEs (think of $f$ as being the right-hand side term in the autonomous ODE system $\dot{x}(t) = f(x(t))$ ).mforetsSat, 05 Mar 2016 12:36:51 +0100https://ask.sagemath.org/question/32720/Has ticket #6480 been fixed?https://ask.sagemath.org/question/27217/has-ticket-6480-been-fixed/The ticket is:
>.subs_expr() method doesn't work for argument of D derivative operator
Here is an example (from Sage 6.2, which I'm currently using):
var('x,y,f,F,Fx,Fy')
y = function('y',x)
f = function('f',x,y)
d1 = diff(f,x,1)
d1.subs_expr(diff(y,x,1)==F)
F*D[1](f)(x, y(x)) + D[0](f)(x, y(x))
So far, all good. But when I try:
d1.subs_expr(diff(y,x,1)==F,diff(f,x,1)==Fx,diff(f,y,1)==Fy)
I receive the error:
TypeError: argument symb must be a symbol
Maybe my syntax is wrong, so either my question is moot, in which case what am I doing wrong? Or maybe my syntax is correct and the fault is still a ticket for `subs_expr`. Advice as always would be very welcome!AlasdairSun, 28 Jun 2015 02:47:35 +0200https://ask.sagemath.org/question/27217/