# Revision history [back]

### 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')

qs = [q1,q2]
ps = [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)
dqdt = list(dqdt)
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
print(dqdt)
return dqdt


I call dynq(0.,[1.,0.5]) 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$.

### 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')

qs = [q1,q2]
ps = [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)
dqdt = list(dqdt)
list(jacHp)
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
print(dqdt)
return dqdt


I call dynq(0.,[1.,0.5]) 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$.

### 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')

qs = [q1,q2]
ps = [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)
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({ps[i]:z[i+N] for i in range(0,N)})
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({ps[i]:z[i+N] for i in range(0,N)})
print(dqdt)
return dqdt


I call dynq(0.,[1.,0.5])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$.

### 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')

qs zs = [q1,q2]
ps = [p1,p2]
[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)
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({ps[i]:z[i+N] for i in range(0,N)})
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({ps[i]:z[i+N] for i in range(0,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$.

### 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)
dqdt.subs({qs[i]:z[i] dqdt.subs({zs[i]:z[i] for i in range(0,N)})
range(0,2*N)})
dqdt.subs({ps[i]:z[i+N] dqdt.subs({zs[i]:z[i] for i in range(0,N)})
dqdt.subs({qs[i]:z[i] for i in range(0,N)})
dqdt.subs({ps[i]:z[i+N] for i in range(0,N)})
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$.