(* file: mtrtst.joy                          MATRICES AND VECTORS *)

"mtrlib" libload.
"numlib" libload.

"output from test file  mtrtst.joy  (* MATRICES and VECTORS *)\n"
putchars.
"user input is indented by one tab\n"
putchars.
2 setecho.
                                   (* VECTORS              *)

5  "hello"  n-e-vector.
5  1  n-e-vector.
                                   (* vectors and scalars  *)

5   [1 2 3]   [+]sv-bin-v.
3.13   [10 20 30]   [*]sv-bin-v.
5   [1 2 3 4 5 6 7 8 9 10]    [-]sv-bin-v.
[1 2 3 4 5 6 7 8 9 10]   5    [-]vs-bin-v.
[{1 2 3}{4 5 6}{7 8 9}]    {2 4 6 8}    [and]vs-bin-v.
3.14   [[id] [dup *] [dup dup * *]]    [i]sv-bin-v.
[1 2 3 4]   [dup] [*] concat   [i]vs-bin-v.
[1.1 2.2 3.3 4.4 5.5]   v-negate-v.
[1.1 2.2 3.3 4.4 5.5]   v-invert-v.
   
                                   (* two vectors          *)

[1 2 3 4]   [11 22 33]      vv-samesize.
[1 2 3 4]   [11 22 33 44]   vv-samesize.

[1 2 3]   [10 20 30]   [+]vv-bin-v.
[1 2 3]   [10 20 30]    [*]vv-bin-v.
[{1 2 3}{2 3 4}]   [{1 3 4}{4 5 6}]   [or]vv-bin-v.
[{1 2 3}{2 3 4}]   [{1 3 4}{4 5 6}]   [and]vv-bin-v.
[true false false true]  [true true false false]  [and]vv-bin-v.

(* inner or scalar product *)
[1 2 3]  [1 2 3]   [*][+]vv-2bin-s.
[1.1 2.2 3.3]  [3.3 4.4 5.5]   [*][+]vv-2bin-s.
[{1 2 3}{4 5 6}]   [{2 3}{2 6}]   [and][or]vv-2bin-s.
[{1 2 3}{4 5 6}]   [{2 3}{2 6}]   [or][and]vv-2bin-s.
[{1 2 3}{4 5 6}]   [{2 3}{2 6}]   [and][and]vv-2bin-s.
[{1 2 3}{4 5 6}]   [{2 3}{2 6}]   [or][or]vv-2bin-s.
[true false true]  [false true true]  [and][or]vv-2bin-s.

                                   (* vectors to matrices *)
[1 2 3 4]  v-1row-m.
[1 2 3 4]  v-1col-m.
[1 2 3 4]  v-zdiag-m.
[1 2 3 4]  0  v-e-diag-m.
["Peter" "Paul" "Mary"]   ""  v-e-diag-m.
[{1 2} {3 4 5} {6}]  {} v-e-diag-m.
4  1  n-e-vector  v-zdiag-m.
4  1  n-e-vector  0  v-e-diag-m.
4 'i  n-e-vector 'o  v-e-diag-m.

[1 2 3]   [10 20]   [+]vv-bin-m.
[1.1 2.2]   [3.3 4.4 5.5 6.6]    [+]vv-bin-m.
[1.1 2.2]   [4.4 5.5 6.6 7.7]    [*]vv-bin-m.
[{1 2 3}{4 5}]   [{1}{2 3}{3 4}]   [or]vv-bin-m.
[{1 2 3}{4 5}]   [{1}{2 3}{3 4}]   [and]vv-bin-m.
[1 2 3 4]  [[id] [dup *] [dup dup * *]]   [dupd i]vv-bin-m.

(* sindeg   cosdeg   tandeg  are defined in numlib.joy *)
[0.0 30.0 45.0]  [ [sindeg] [cosdeg] [tandeg] ]  [dupd i]vv-bin-m.

[peter paul]   [[smith] [jones]]   [cons]vv-bin-m.
[peter paul]   [[smith] [jones]]   [cons]vv-bin-m  size.
(* compare: *)
[peter paul]   [ smith   jones ]   cartproduct.
[peter paul]   [ smith   jones ]   cartproduct  size.

                                   (* MATRICES             *)

[[1 2] [3 4]]   [[5 6] [7 8]]   mm-vercat-m.
[[1 2] [3 4]]   [[5 6] [7 8]]   mm-horcat-m.
[[1 2 3] [4 5 6] [7 8 9] [10 11 12]]   m-transpose-m.

                                   (* matrices and scalars *)

[[1 2] [3 4] [5 6]]  10      [+]ms-cbin-m.
[[1 2] [3 4] [5 6]]  10      [-]ms-bin-m. (* not commutative *)
[[1 2] [3 4] [5 6]]  10      [*]ms-cbin-m.
[[1 2] [3 4] [5 6]]   0      [*]ms-cbin-m.
[[1 2] [3 4] [5 6]]   7   [popd]ms-bin-m.
[[1 2] [3 4] [5 6]]  [succ]  [i]ms-bin-m.
[[{1 2} {3 4}] [{5 6} {7 8}] ]   {1 3 5 7}  [and]ms-bin-m.

5  [[1 2 3] [4 5 6] [7 8 9]]        [-]sm-bin-m.
   [[1 2 3] [4 5 6] [7 8 9]]   5    [-]ms-bin-m.
0  [[1 2 3] [4 5 6] [7 8 9]]      [pop]sm-bin-m.

                                   (* two matrices         *)

[[1 2 3] [4 5 6]]  [[10 20 30]  [40 50 60]]      mm-add-m.
[[1 2 3] [4 5 6]]  [[10 20 30]  [40 50 60]]   [+]mm-bin-m.
[[1 2 3] [4 5 6]]  [[10 20 30]  [40 50 60]]   [*]mm-bin-m.
[[true  true ] [false false]]
[[true  false] [true  false]]                [or]mm-bin-m.
[[true  true ] [false false]]
[[true  false] [true  false]]               [and]mm-bin-m.
[[true  true ] [false false]]
[[true  false] [true  false]]               [xor]mm-bin-m.

DEFINE     (* 2 by 2 identity matrix *)
  id2  ==  2 1 n-e-vector  0 v-e-diag-m.

id2.
id2  m-transpose-m.
id2  [[1 2 3] [4 5 6]]              mm-mul-m.
[[1 2] [3 4] [5 6]]  id2            mm-mul-m.
[[1 2] [3 4] [5 6]]  id2     [*][+]mm-2bin-m.

[[1.1 2.2] [3.3 4.4] [5.5 6.6]]          dup  m-print
[[1.1 2.2] [3.3 4.4]]                    dup  m-print
                                    mm-mul-m  m-print.

[[{1 2} {3 4}] [{4 5} {6 7}]]            dup  m-print
[[{1 3 5} {6}] [{2} {3 4 5}]]            dup  m-print
                          [and][or]mm-2bin-m  m-print.

(* END of test file  mtrtst.joy *)