Ask Your Question
1

From numerical variables to symbolic variables

asked 2019-05-24 19:10:20 +0100

sagenotdead gravatar image

updated 2019-05-24 21:02:07 +0100

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$.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2019-05-24 20:49:39 +0100

tmonteil gravatar image

updated 2019-05-24 20:49:53 +0100

When you write:

dqdt[0].subs({qs[i]:z[i] for i in range(0,N)})

this returns another object, it does not change dqdt.

So, instead, you can write:

dqdt[0] = dqdt[0].subs({qs[i]:z[i] for i in range(0,N)})
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-05-24 19:10:20 +0100

Seen: 276 times

Last updated: May 24 '19