Murat Kasimov

More about me

Я language (β)

/Я language (β)/Articles/Vectors, matrices and tensors for free/

Source code for snippetsTwitter discussionReddit discussionLinkedin discussionFacebook discussion

It's recommended to refresh knowledge about representing objects and Kleisli morphisms before.

Alone is a covariant functor from Arrow into Arrow that could be represented/co-represented with Unit object:

> ( Alone `har'st` A : Alone Latin ) `yo` Upper : Alone Letter > ( Alone `har'st` A : Alone Latin ) `yor` Unit : Letter > ( A : Unit `AR_` Latin ) `ryo` Enter : Alone Letter > ( A Unit : Latin ) `ryu` Enter : Alone Letter

We can use this simplest functor in a variety of functor composition schemes which are functors as well:

> ( T'TT'I `ha` Alone `ha` Alone `har'st` A : Alone `T'TT'I` Alone `T'I_` Latin ) > ( TT'T'I `ha` Alone `ha` Alone `har'st` A : Alone `TT'T'I` Alone `T'I_` Latin ) > ( Clasp `ha` This `ha` Alone `har'st` A : Alone `S'T'I'TT'I` Alone `T'I___` Latin ) > ( Clasp `ha` That `ha` Alone `har'st` B : Alone `S'T'I'TT'I` Alone `T'I___` Latin ) > ( Clasp `har__` Alone `har'st` A `hjd_` Alone `har'st` B : Alone `P'T'I'TT'I` Alone `T'I___` Latin ) > ( Alone `har'st` A `hjd_'tb` Alone `har'st` B : Alone `P'T'I'TT'I` Alone `T'I___` Latin )

We are particularly interested in the last one - product composition. It's also a covariant functor from Arrow into Arrow but representing object is slightly different - Sum of Units:

> A `ryo` Enter @ Alone `yor` Unit = A Unit > A `has` B `ryo` Enter @ ( Alone `P'T'I'TT'I` Alone ) `yor` This `har` Unit = A Unit > A `has` B `ryo` Enter @ ( Alone `P'T'I'TT'I` Alone ) `yor` That `har` Unit = B Unit

Since natural transformations for covariant functor compositions defined inductively we can add more functors and representing object would be adjusted accordingly:

> A `has` B `has` C `ryo` Enter @ ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone ) `yor` This `ha` This `har` Unit = A Unit > A `has` B `has` C `ryo` Enter @ ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone ) `yor` That `ha` This `har` Unit = B Unit > A `has` B `has` C `ryo` Enter @ ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone ) `yor` That `har` Unit = C Unit

The good news is that we have literals for this kind of functor compositions - vectors:

> A `ryo` Enter @ ( Vector 1 ) `yor` Unit = A Unit > A `has` B `ryo` Enter @ ( Vector 2 ) `yor` This `har` Unit = A Unit > A `has` B `ryo` Enter @ ( Vector 2 ) `yor` That `har` Unit = B Unit > A `has` B `has` C `ryo` Enter @ ( Vector 3 ) `yor` This `ha` This `har` Unit = A Unit > A `has` B `has` C `ryo` Enter @ ( Vector 3 ) `yor` That `ha` This `har` Unit = B Unit > A `has` B `has` C `ryo` Enter @ ( Vector 3 ) `yor` That `har` Unit = C Unit

Challenge: initialise vectors with 4/5/6 scalar values.

It's time to add more dimensions and start using matrices which are functor compositions of vectors:

: ( Matrix n m ) ~ ( Vector n `T'TT'I` Vector m )

Representing object for underlying functors are combined into a product:

> `yor` : Matrix 1 1 o `AR_______` Unit `P` Unit `AR_` o > `yor` : Matrix 1 2 o `AR_______` Unit `P_` Unit `S` Unit `AR_` o > `yor` : Matrix 2 2 o `AR_______` Unit `S` Unit `P_` Unit `S` Unit `AR_` o > `yor` : Matrix 2 3 o `AR_______` Unit `S` Unit `P_` Unit `S` Unit `S` Unit `AR_` o > `yor` : Matrix 3 3 o `AR_______` Unit `S` Unit `S` Unit `P_` Unit `S` Unit `S` Unit `AR_` o

Challenge: write some code to represent/co-represent matrices.

Here we initialise matrices vector by vector:

> ... = Pass `hu___` A `has` B `has` C `has` D `has` E `ryo` Enter > `has___` Pass `hu___` F `has` G `has` H `has` I `has` K `ryo` Enter > `has___` Pass `hu___` L `has` M `has` N `has` O `has` P `ryo` Enter > `has___` Pass `hu___` Q `has` R `has` S `has` T `has` U `ryo` Enter > `__ryo` Enter @ ( Matrix 4 5 ) `ha` Forge

Matrix with the same set of elements but different dimensions:

> ... = Pass `hu___` A `has` B `has` C `has` D `ryo` Enter > `has___` Pass `hu___` E `has` F `has` G `has` H `ryo` Enter > `has___` Pass `hu___` I `has` K `has` L `has` M `ryo` Enter > `has___` Pass `hu___` N `has` O `has` P `has` Q `ryo` Enter > `has___` Pass `hu___` R `has` S `has` T `has` U `ryo` Enter > `__ryo` Enter @ ( Matrix 5 4 ) `ha` Forge

Transposing and traversing are similar operations in that sense that we swap functors in both cases:

> ( `yokl` Apply `ha` Adapt ) : Vector n `T'TT` Vector m `T'I_` i `AR___` Vector m `T'TT` Vector n `T'I_` i > ( `yokl` Apply `ha` Allot ) : Vector n `T'TT` State e `T'I_` i `AR____` State e `T'TT` Vector n `T'I_` i > ( `yokl` Apply `ha` Await ) : Vector n `T'TT` World `T'I_` i `AR______` World `T'TT` Vector n `T'I_` i

So far we covered scalars, vectors and matrices - these could be generalised via tensors:

: Tensor [ ] ~ Scalar : Tensor [ n ] ~ Vector n : Tensor [ n , m ] ~ Matrix n m

This is how it's going to look like in terms of functors:

: Tensor [ 1 ] ~ Alone : Tensor [ 2 ] ~ Alone `P'T'I'TT'I` Alone : Tensor [ 3 ] ~ Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone : Tensor [ 1 , 1 ] ~ ( Alone ) `T'TT'I` ( Alone ) : Tensor [ 1 , 2 ] ~ ( Alone ) `T'TT'I` ( Alone `P'T'I'TT'I` Alone ) : Tensor [ 2 , 2 ] ~ ( Alone `P'T'I'TT'I` Alone ) `T'TT'I` ( Alone `P'T'I'TT'I` Alone ) : Tensor [ 2 , 3 ] ~ ( Alone `P'T'I'TT'I` Alone ) `T'TT'I` ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone ) : Tensor [ 3 , 3 ] ~ ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone ) `T'TT'I` ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone ) : Tensor [ 1 , 2 , 3 ] ~ ( Alone ) `T'TT'I_` ( Alone `P'T'I'TT'I` Alone ) `T'TT'I` ( Alone `P'T'I'TT'I` Alone `P'T'I'TT'I` Alone )

Challenge: write some code to represent/co-represent tensors of rank 3.

Take a look the these snippets below. What kind of operations in linear algebra do these transformations correspond to?

> `yp` : Vector 2 ( i ) `P` Vector 2 ( ii ) `AR_______` Vector 2 ( i `P` ii ) > `yp` : Matrix 3 4 ( i ) `P` Matrix 3 4 ( ii ) `AR_______` Matrix 3 4 ( i `P` ii ) > `yp` : Tensor [ 2 , 3 , 4 ] ( i ) `P` Tensor [ 2 , 3 , 4 ] ( ii ) `AR_______` Tensor [ 2 , 3 , 4 ] ( i `P` ii )

Challenge: why the code below doesn't make sense?

> `ys` : Vector 2 ( i ) `P` Vector 2 ( ii ) `AR_______` Vector 2 ( i `S` ii ) > `ys` : Matrix 3 4 ( i ) `P` Matrix 3 4 ( ii ) `AR_______` Matrix 3 4 ( i `S` ii ) > `ys` : Tensor [ 2 , 3 , 4 ] ( i ) `P` Tensor [ 2 , 3 , 4 ] ( ii ) `AR_______` Tensor [ 2 , 3 , 4 ] ( i `S` ii ) > `ys` : Vector 2 ( i ) `P` Vector 2 ( ii ) `AR_______` Vector 2 ( i `S` ii ) > `yw` : Matrix 3 4 ( i ) `P` Matrix 3 4 ( ii ) `AR_______` Matrix 3 4 ( i `W` ii ) > `yw` : Tensor [ 2 , 3 , 4 ] ( i ) `P` Tensor [ 2 , 3 , 4 ] ( ii ) `AR_______` Tensor [ 2 , 3 , 4 ] ( i `W` ii )

For all challenges in this article use special executable for snippets.