# Some linear algebra and APL results

1. Affine transformation
2. Solve linear equations

## 1. Affine transformation

Equation 1. Affine equations

$\left[\begin{array}{c}u\\ v\\ 1\end{array}\right]=\left[\begin{array}{ccc}a& b& c\\ d& e& f\\ 0& 0& 1\end{array}\right]\left[\begin{array}{c}x\\ y\\ 1\end{array}\right]$

Where $\left(u,v\right)$ is the new coordinate and $\left(x,y\right)$ is the old coordinate.

The APL code mapping an image by this transformation is like:

 affine←{
⍝ affine transformation
⎕IO←0 ⋄ s←⍴⍵
g←¯1↓⍉⌊0.5+⍺+.×⍤2 1⊢1,⍨⍉s⊤⍳×/s
m←∧⌿(s>⍤0 1⊢g)∧0<g
s⍴(0,,⍵)[m×1+s⊥g]
}


## 2. Solve linear equations

For coefficient matrix $A$, variable matrix $X$ and constant matrix $B$, then

$\begin{array}{rl}AX& =B\\ X& =\left({A}^{-1}\right)B\end{array}$

Then the APL code is just (⌹A)+.×B, or even simpler, B⌹A. As the APL2 specification writes:

If Z←L⌹R is executable, Z is determined to minimize the value of the least squares expression:

 +/,(L-R+.×Z)*2