# Affine Transformations

Here we use sympy to derive the calculations for various affine transformation
operations.


```python
from sympy import *
```


```python
def mat3x2(name=None):
    a, b, c, d, e, f = symbols(f'{name}_a {name}_b {name}_c {name}_d {name}_e {name}_f' if name else 'a b c d e f')
    return Matrix([
        [a, b, c],
        [d, e, f],
        [0, 0, 1]
    ])
mat3x2()
```




$\displaystyle \left[\begin{matrix}a & b & c\\d & e & f\\0 & 0 & 1\end{matrix}\right]$




```python
# What is the formula for chaining affine transformations?
mat3x2('A') * mat3x2('B')
```




$\displaystyle \left[\begin{matrix}A_{a} B_{a} + A_{b} B_{d} & A_{a} B_{b} + A_{b} B_{e} & A_{a} B_{c} + A_{b} B_{f} + A_{c}\\A_{d} B_{a} + A_{e} B_{d} & A_{d} B_{b} + A_{e} B_{e} & A_{d} B_{c} + A_{e} B_{f} + A_{f}\\0 & 0 & 1\end{matrix}\right]$




```python
# How do we invert an affine transformation?
mat3x2() ** -1
```




$\displaystyle \left[\begin{matrix}\frac{e}{a e - b d} & - \frac{b}{a e - b d} & \frac{b f - c e}{a e - b d}\\- \frac{d}{a e - b d} & \frac{a}{a e - b d} & \frac{- a f + c d}{a e - b d}\\0 & 0 & 1\end{matrix}\right]$




```python
# How do we construct an affine transform from scale, rotation, and translation?

def rot(name='theta'):
    theta = symbols(name)
    s = sin(theta)
    c = cos(theta)
    return Matrix([
        [c, -s, 0],
        [s, c, 0],
        [0, 0, 1],
    ])


def scale(name='S'):
    x, y = symbols(f'{name}_x {name}_y')
    return Matrix([
        [x, 0, 0],
        [0, y, 0],
        [0, 0, 1]
    ])


def xlate(name='T'):
    x, y = symbols(f'{name}_x {name}_y')
    return Matrix([
        [1, 0, x],
        [0, 1, y],
        [0, 0, 1]
    ])

xlate() * rot() * scale()
```




$\displaystyle \left[\begin{matrix}S_{x} \cos{\left(\theta \right)} & - S_{y} \sin{\left(\theta \right)} & T_{x}\\S_{x} \sin{\left(\theta \right)} & S_{y} \cos{\left(\theta \right)} & T_{y}\\0 & 0 & 1\end{matrix}\right]$



## Transforming Coordinates


```python
coord = Matrix([
    symbols(f'x_{n} y_{n}') + (1,)
    for n in range(10)
])
coord
```




$\displaystyle \left[\begin{matrix}x_{0} & y_{0} & 1\\x_{1} & y_{1} & 1\\x_{2} & y_{2} & 1\\x_{3} & y_{3} & 1\\x_{4} & y_{4} & 1\\x_{5} & y_{5} & 1\\x_{6} & y_{6} & 1\\x_{7} & y_{7} & 1\\x_{8} & y_{8} & 1\\x_{9} & y_{9} & 1\end{matrix}\right]$




```python
xformed = (coord * mat3x2().T)
xformed.col_del(2)
xformed
```




$\displaystyle \left[\begin{matrix}a x_{0} + b y_{0} + c & d x_{0} + e y_{0} + f\\a x_{1} + b y_{1} + c & d x_{1} + e y_{1} + f\\a x_{2} + b y_{2} + c & d x_{2} + e y_{2} + f\\a x_{3} + b y_{3} + c & d x_{3} + e y_{3} + f\\a x_{4} + b y_{4} + c & d x_{4} + e y_{4} + f\\a x_{5} + b y_{5} + c & d x_{5} + e y_{5} + f\\a x_{6} + b y_{6} + c & d x_{6} + e y_{6} + f\\a x_{7} + b y_{7} + c & d x_{7} + e y_{7} + f\\a x_{8} + b y_{8} + c & d x_{8} + e y_{8} + f\\a x_{9} + b y_{9} + c & d x_{9} + e y_{9} + f\end{matrix}\right]$



105cfc263ed7af15eb76e16a16addd841820399c
ipynb
Jupyter Notebook
notebooks/Transforms.ipynb
lordmauve/wasabigeom
1c6693147cf58c8edd95fa0fd7716a1c22f9171f
BSD-3-Clause
2020-10-29T16:16:17.000Z
2022-01-24T06:26:14.000Z
notebooks/Transforms.ipynb
lordmauve/wasabi-geom
8032bc4fbabbe145d881e3a284a309422fc87c7b
BSD-3-Clause
2020-10-03T07:27:15.000Z
2021-09-24T17:07:11.000Z
notebooks/Transforms.ipynb
lordmauve/wasabigeom
1c6693147cf58c8edd95fa0fd7716a1c22f9171f
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES