1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
module mulmat
/*
Matrix Multiplication.
This program performs matrix multiplication on matrices of integers.
Lists are used to simulate matrices
The initial matrices (Mat1 & Mat2) can have arbitrary size (Size). The
second matrix is transposed first, in order to avoid traversing a matrix
by column, which is very inefficient. The result of the program shows the
initial matrices and the resulting matrix. Run the program with the
"Show Constructors" option on (Application Options).
*/
import StdInt, StdString
::Row :== [Int] // A row is a list of integers.
::Col :== [Int] // A column is a list of integers.
::Mat :== [Row] // A matrix is a list of rows.
::TMat :== [Col] // A transposed matrix is a list of columns.
::Index_new :== Int // An index is an integer.
Size :== 6 // The size of the matrices.
// The initial matrices
Mat1::Mat
Mat1 = [[ 1, 2, 3, 4, 5, 6 ] //
,[ 0, 1, 2, 3, 4, 5 ] //
,[ -1, 0, 1, 2, 3, 4 ] // The product of these matrices:
,[ -2,-1, 0, 1, 2, 3 ] //
,[ -3,-2,-1, 0, 1, 2 ] //
,[ -4,-3,-2,-1, 0, 1 ]] // [ 0 -9 0 5 1 7 ]
// [ 0 -8 -1 4 1 6 ]
Mat2::Mat // [ 0 -7 -2 3 1 5 ]
Mat2 = [[ 0, 1, 0, 0, 0,-1 ] // [ 0 -6 -3 2 1 4 ]
,[ 1, 0, 1, 1, 0, 1 ] // [ 0 -5 -4 1 1 3 ]
,[ -1, 0, 1,-1, 0, 0 ] // [ 0 -4 -5 0 1 2 ]
,[ -1,-1,-1, 0,-1, 0 ] //
,[ 1, 0, 1, 0, 1, 0 ] //
,[ 0,-1,-1, 1, 0, 1 ]] //
// Multiplying two matrices.
MulMat::Mat Mat -> Mat
MulMat m1 m2 = TMulMat m1 (Transpose m2)
TMulMat::Mat TMat -> Mat
TMulMat [r] m2 = [ MulRow r m2 ]
TMulMat [r:rs] m2 = [ MulRow r m2 : TMulMat rs m2 ]
MulRow::Row TMat -> Row
MulRow r [c] = [ Inprodukt r c ]
MulRow r [c:cs] = [ Inprodukt r c : MulRow r cs ]
Inprodukt::Row Col -> Int
Inprodukt [] [] = 0
Inprodukt [a:as] [b:bs] = a * b + Inprodukt as bs
// Transposing a matrix.
Transpose::Mat -> TMat
Transpose m = Transp m 1
Transp::Mat Index_new -> TMat
Transp m i | i == Size = [ Column m i ]
= [ Column m i : Transp m (i + 1) ]
Column::Mat Index_new -> Col
Column [] i = []
Column [r:rs] i = [ Select r i : Column rs i ]
Select::Row Index_new -> Int
Select [a:as] 1 = a
Select [a:as] i = Select as (i - 1)
// The Start rule: show the initial matrices and their product.
Start::(Mat,String,Mat,String,Mat)
Start = (m1,"\ntimes\n",m2,"\nbecomes\n",MulMat m1 m2)
where
m1 = Mat1; m2 = Mat2
|