[update] The API shown here will most definitely change.

During my internship at LKB, Paris I’m expected to prepare a number of optical setups. I thought that it would be interesting to do the work in SymPy. That’s why I started working on the gaussopt module in sympy.physics (for the foreseeable future it won’t be upstream). I plan to make it small and basic (containing only the most basic functionality).

For the moment the only things on the blueprint are ray transfer matrices, geometric and gaussian beams.

To define a ray transfer matrix for free space or for a thin lens you just use the build in classes (a general matrix is also available):

```In : from sympy.physics.gaussopt import *

In : distance = Symbol('distance', positive=True)

In : focus = Symbol('focus', real=True)

In : fs = FreeSpace(distance)

In : tl = ThinLens(focus)

In : fs
Out:
⎡1  distance⎤
⎢           ⎥
⎣0     1    ⎦

In : tl
Out:
⎡  1    0⎤
⎢        ⎥
⎢  -1    ⎥
⎢─────  1⎥
⎣focus   ⎦
```

The module supports geometric beams but gaussian beams are more interesting, so I’ll show only them. The formalism that supports them is based on the complex beam parameter. Here is how you define a beam at its waist:

```In : waist = Symbol('waist', positive=True)

In : wavelen = Symbol('wavelen', positive=True)

In : z_rayleigh = waist**2 * pi / wavelen

In : p = BeamParameter(0, z_rayleigh, wavelen)

In : p.q
Out:
2
ⅈ⋅π⋅waist
──────────
wavelen

In : p.w
Out: waist

In : p.w_0
Out: waist

In : p.R
Out: 0

In : p.z
Out: 0

In : p.z_r
Out:
2
π⋅waist
────────
wavelen
```

For the different properties shown here the names should be suggestive enough. The pretty_printer supports Greek letters for the wavelen but this is not so important here.

Now I would like to calculate how a telescopic system will act on the beam.  For example

```In : tl1 = ThinLens(focus/10)

In : p_out = tl1*( fs*( tl*p))
```

The last line takes about 60 seconds and produces very UNsimplified expression. I suppose that I should do some implicit simplification. Also notice the parentheses. I should check if they are really required.

simplify(p_out) takes too much time so I’ll cheat a bit (from symbolics to numerics):

```In : p_out1 = p_out.subs({focus:1, distance:1*11/10,
wavelen:532e-9, waist:5e-3})

In : N(p_out1.q)
Out: -0.110000000001061 + 1.47631233721549⋅ⅈ

In : N(p_out1.w_0)
Out: 0.000500000000000380

In : N(p_out1.z)
Out: -0.110000000001061
```

So with balanced telescopic system of power 10 the waist diminished 10 times and will be obtained at 11 cm after the second lens. Nice.

It would be nice to post some more realistic problems later on.