Matrix multiplication: Difference between revisions

Content added Content deleted
(add fermat)
(Added 11l)
Line 6: Line 6:
They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.
They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Nim}}

<lang 11l>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)))</lang>

{{out}}
<pre>
([ 1.00 1.00 1.00 1.00]
[ 2.00 4.00 8.00 16.00]
[ 3.00 9.00 27.00 81.00]
[ 4.00 16.00 64.00 256.00])
([ 4.00 -3.00 1.33 -0.25]
[ -4.33 4.75 -2.33 0.46]
[ 1.50 -2.00 1.17 -0.25]
[ -0.17 0.25 -0.17 0.04])
([ 1.00 0.00 -0.00 -0.00]
[ 0.00 1.00 -0.00 -0.00]
[ 0.00 0.00 1.00 0.00]
[ 0.00 0.00 0.00 1.00])
([ 1.00 0.00 0.00 0.00]
[ 0.00 1.00 -0.00 0.00]
[ 0.00 0.00 1.00 0.00]
[ 0.00 0.00 -0.00 1.00])
</pre>


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==