34 lines
838 B
Text
34 lines
838 B
Text
F matrix_mul(m1, m2)
|
||
assert(m1[0].len == m2.len)
|
||
V r = [[0.0] * m2[0].len] * m1.len
|
||
L(j) 0 .< m1.len
|
||
L(i) 0 .< m2[0].len
|
||
V s = 0.0
|
||
L(k) 0 .< m2.len
|
||
s += m1[j][k] * m2[k][i]
|
||
r[j][i] = s
|
||
R r
|
||
|
||
F to_str(m)
|
||
V result = ‘([’
|
||
L(r) m
|
||
I result.len > 2
|
||
result ‘’= "]\n ["
|
||
L(val) r
|
||
result ‘’= ‘#5.2’.format(val)
|
||
R result‘])’
|
||
|
||
V a = [[1.0, 1.0, 1.0, 1.0],
|
||
[2.0, 4.0, 8.0, 16.0],
|
||
[3.0, 9.0, 27.0, 81.0],
|
||
[4.0, 16.0, 64.0, 256.0]]
|
||
|
||
V b = [[ 4.0, -3.0 , 4/3.0, -1/4.0],
|
||
[-13/3.0, 19/4.0, -7/3.0, 11/24.0],
|
||
[ 3/2.0, -2.0 , 7/6.0, -1/4.0],
|
||
[ -1/6.0, 1/4.0, -1/6.0, 1/24.0]]
|
||
|
||
print(to_str(a))
|
||
print(to_str(b))
|
||
print(to_str(matrix_mul(a, b)))
|
||
print(to_str(matrix_mul(b, a)))
|