# Some linear algebra and APL results

## 1 Affine transformation

$\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]$
Equation 1.1 Affine equations

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
5 |     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


## 3 Norm in ${ℂ}^{n}$

The norm of inner product space, $‖\stackrel{‾}{a}‖=\sqrt{⟨\stackrel{‾}{a},\stackrel{‾}{a}⟩}$, is:

  |2*∘÷⍨+.×⍨


## 4 Use linear transformation to define polynomial functions

Due to the "superposition" principle, for ${ℙ}_{4}$ that basis $B=\left[1,x,{x}^{2},{x}^{3}\right]$, $L\left(p\right)={x}^{\prime }+{x}^{\prime \prime }$ find $M$ that $\left[L\left(p\right){\right]}_{B}=M{\left[p\right]}_{B}$.

Then M is just:

$M=\left[\begin{array}{cccc}0& 1& 2& 0\\ 0& 0& 2& 6\\ 0& 0& 0& 3\\ 0& 0& 0& 0\end{array}\right]$

This may comes handy when some extent of symbolic computation is needed.

## 5 Determinant, Generalized

There is already a general determinant from APLcart.info. However, I am not able to explain the mathematics behind it. I could only tell it involves taking the matrix product of upper matrix and diagonal replaced by accumulative sum minus the total sum and itself, negate it, and repeat with the result. Sorry, so far I am only familiar with cofactor expansion method

Actually, Iversion had assigned a special combinator for determinant: monadic -.×. This would not work in most of today's APL implementations, but there is the similar operator in J. Since he has recorded the definition, we are still able to figure out what it means.

Let's first review the cofactor expansion method with a 3 by 3 matrix:

$|\begin{array}{ccc}a& b& c\\ d& e& f\\ g& h& i\end{array}|=a|\begin{array}{cc}e& f\\ h& i\end{array}|-b|\begin{array}{cc}d& f\\ g& i\end{array}|+c|\begin{array}{cc}d& e\\ g& h\end{array}|$

Then, here is a play on trick of visualizing the method in APL(⎕IO←0 and remember the evaluation order in APL):

      ⎕←perm←3 6⍴0 0 1 1 2 2 1 2 2 0 0 1 2 1 0 2 1 0
0 0 1 1 2 2
1 2 2 0 0 1
2 1 0 2 1 0
(⊣,'-',⊢)/(⊣,'×',⊢)/1 0 1⍉(3 3⍴'abcdefghi')[perm;]
┌───────────────────────────────────┐
│a×e×i-a×h×f-d×h×c-d×b×i-g×b×f-g×e×c│
└───────────────────────────────────┘

Now, the only problem left is what is perm? It is not the unique index value since any permutation that can correctly arrange the parity should work.

According to Iverson, matrix determinant can be characterized as:

  | det ← {-/×/1 0 1⍉⍵[perm⍴⍵;]}
| mod ← {0=⊃⍴⍵:⍵ ⋄ ⍵[0;],[0]Z+⍵[(1↑⍴Z)⍴0;]≤Z←∇ 1 0↓⍵}
|perm ← {(mod Z)[;(⍳⍴P)+P-(⍴P←2|+⌿Z←rep ⍵)⍴0 1]}
| rep ← {(⊢⊤(⍳×/))⌽1+⍳1↑⍵}


Replacing - and × by other functions can give other interesting results, but that would not be covered here. A similar implementation in Dyalog can be found at dfns.

## Bibliography

[Iverson] Two combinatoric operators. . Proceedings of the eighth international conference on APL. 22 September 1976. . 233–237. Association for Computing Machinery. New York, NY, United States. doi:10.1145/800114.803681.

[dfns] Alternant. . https://dfns.dyalog.com/n_alt.htm.