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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
_debug_info = 1; () = evalfile ("inc.sl");
print ("Testing Matrix Multiplications ...");
#ifexists Double_Type
static define dot_prod (a, b)
{
(a # b)[0]; % transpose not needed for 1-d arrays
}
static define sum (a)
{
variable ones = Double_Type [length (a)] + 1;
dot_prod (a, ones);
}
if (1+2+3+4+5 != sum([1,2,3,4,5]))
failed ("sum");
#ifexists Complex_Type
if (1+2i != sum ([1,2i]))
failed ("sum complex");
#endif
define mult (a, b)
{
variable dims_a, dims_b;
variable nr_a, nr_b, nc_a, nc_b;
variable i, j;
variable c;
(dims_a,,) = array_info (a);
(dims_b,,) = array_info (b);
nr_a = dims_a[0];
nc_a = dims_a[1];
nr_b = dims_b[0];
nc_b = dims_b[1];
c = _typeof ([a[0,0]]#[b[0,0]])[nr_a, nc_b];
for (i = 0; i < nr_a; i++)
{
for (j = 0; j < nc_b; j++)
c[i,j] = dot_prod (a[i,*], b[*,j]);
}
return c;
}
static define arr_cmp (a, b)
{
variable i = length (where (b != a));
if (i == 0)
return 0;
i = where (b != a);
a = a[i];
b = b[i];
reshape (a, [length(a)]);
reshape (b, [length(b)]);
vmessage ("%S != %S\n", a[0], b[0]);
return 1;
}
static define test (a, b)
{
if (0 != arr_cmp (mult (a,b), a#b))
failed ("%S # %S", a, b);
}
variable A, B;
#ifexists Complex_Type
A = [1+2i];
B = [3+4i];
reshape (A, [1, 1]);
reshape (B, [1, 1]);
test (A,B);
#endif
% Test intgers
A = _reshape ([[1, 2, 3], [4, 5, 6]], [2,3]);
B = _reshape ([[7,8,9],[1,2,4]], [2,3]);
B = transpose (B);
test (A, B);
B *= 1f;
test (A, B);
B *= 1.0;
test (A,B);
A *= 1f;
test (A,B);
#ifexists Complex_Type
B += 2i;
test (A,B);
A += 3i;
test (A,B);
B = Real(B);
test (A,B);
% Now try an empty array
if (Complex_Type != _typeof (Complex_Type[0,0,0] # Complex_Type[0]))
failed ("[]#[]");
#endif
% And finally, do a 3-d array:
A = _reshape ([1:2*3*4], [2,3,4]);
B = _reshape ([1:4*5*6], [4,5,6]);
static variable C = A#B;
% C should be a [2,3,5,6] matrix. Let's check via brute force
static define multiply_3d (a, b, c)
{
variable i, j, k, l, m;
variable dims_a, dims_b;
(dims_a,,) = array_info(a);
(dims_b,,) = array_info(b);
_for (0, dims_a[0]-1, 1)
{
i = ();
_for (0, dims_a[1]-1, 1)
{
j = ();
_for (0, dims_b[1]-1, 1)
{
l = ();
_for (0, dims_b[2]-1, 1)
{
m = ();
variable sum = 0;
_for (0, dims_b[0]-1, 1)
{
k = ();
sum += a[i,j,k] * b[k, l, m];
}
if (sum != c[i,j,l,m])
failed ("multiply_3d");
}
}
}
}
}
multiply_3d (A, B, C);
print ("Ok\n");
#else
print ("Not available\n");
#endif
exit (0);
|