In [11]:
from sympy import init_session, init_printing, Matrix
init_session()
init_printing()


IPython console for SymPy 0.7.6 (Python 2.7.8-64-bit) (ground types: python)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at http://www.sympy.org

Twiss Matrices


In [37]:
B, Bi, R, mu     = symbols('B, B_{inv}, R, mu')
a0, a1, b0, b1   = symbols('alpha_0, alpha_1, beta_0, beta_1', real=True, positive=True)
# V, h, phi, eta, R, C = symbols('V, h, phi, eta, R, C')
# z, dp                = symbols('z, dp', real=True)
# Hc                   = symbols('H_c')


B  = Matrix([[1/sqrt(b0), 0], [a0/sqrt(b0), sqrt(b0)]])
R  = Matrix([[cos(mu), sin(mu)], [-sin(mu), cos(mu)]])
Bi = Matrix([[sqrt(b1), 0], [-a1/sqrt(b1), 1/sqrt(b1)]])

In [39]:
expand_trig(simplify(trigsimp(Bi * R * B)))


Out[39]:
$$\left[\begin{matrix}\frac{\sqrt{\beta_{1}}}{\sqrt{\beta_{0}}} \left(\alpha_{0} \sin{\left (\mu \right )} + \cos{\left (\mu \right )}\right) & \sqrt{\beta_{0}} \sqrt{\beta_{1}} \sin{\left (\mu \right )}\\\frac{1}{\sqrt{\beta_{0}} \sqrt{\beta_{1}}} \left(- \alpha_{0} \alpha_{1} \sin{\left (\mu \right )} + \alpha_{0} \cos{\left (\mu \right )} - \alpha_{1} \cos{\left (\mu \right )} - \sin{\left (\mu \right )}\right) & \frac{\sqrt{\beta_{0}}}{\sqrt{\beta_{1}}} \left(- \alpha_{1} \sin{\left (\mu \right )} + \cos{\left (\mu \right )}\right)\end{matrix}\right]$$

In [41]:
simplify(Bi * Matrix([[1, 0],[0, 1]]) * B)


Out[41]:
$$\left[\begin{matrix}\frac{\sqrt{\beta_{1}}}{\sqrt{\beta_{0}}} & 0\\\frac{\alpha_{0} - \alpha_{1}}{\sqrt{\beta_{0}} \sqrt{\beta_{1}}} & \frac{\sqrt{\beta_{0}}}{\sqrt{\beta_{1}}}\end{matrix}\right]$$

In [42]:
simplify(Bi * Matrix([[0, 1],[-1, 0]]) * B)


Out[42]:
$$\left[\begin{matrix}\frac{\alpha_{0} \sqrt{\beta_{1}}}{\sqrt{\beta_{0}}} & \sqrt{\beta_{0}} \sqrt{\beta_{1}}\\- \frac{\alpha_{0} \alpha_{1} + 1}{\sqrt{\beta_{0}} \sqrt{\beta_{1}}} & - \frac{\alpha_{1} \sqrt{\beta_{0}}}{\sqrt{\beta_{1}}}\end{matrix}\right]$$

Hamiltonian


In [ ]:
e, beta, c           = symbols('e, beta, c')
V, h, phi, eta, R, C = symbols('V, h, phi, eta, R, C')
z, dp                = symbols('z, dp', real=True)
Hc                   = symbols('H_c')

zs = C/h/2.

def H(z, dp):
    return -eta*beta*c*dp**2 + e*V/C * cos(h*z/R + phi)
H(z, dp)

In [ ]:
def ps(z):
    return solve(Eq(H(z,dp), 0), dp)[0]
ps(z)

In [55]:
def ps(z):
    return sqrt(cos(z))
#     return -sqrt(e*V/(eta*beta*c*C) * h*z/R)
#     return -sqrt(e*V/(eta*beta*c*C) * cos(h*z/R+phi))
ps(z)


Out[55]:
$$\sqrt{\cos{\left (z \right )}}$$

In [ ]:
integrate(ps(z), (z, -zs, zs))