Matrix-exponentiation operator: Difference between revisions

Added 11l
(add fermat)
(Added 11l)
Line 7:
Demonstrate how to implement matrix exponentiation as an operator.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F matrix_mul(m1, m2)
assert(m1[0].len == m2.len)
V r = [[0] * m2[0].len] * m1.len
L(j) 0 .< m1.len
L(i) 0 .< m2[0].len
V s = 0
L(k) 0 .< m2.len
s += m1[j][k] * m2[k][i]
r[j][i] = s
R r
 
F identity(size)
V rsize = 0 .< size
R rsize.map(j -> @rsize.map(i -> Int(i == @j)))
 
F matrixExp(m, pow)
assert(pow >= 0 & Int(pow) == pow, ‘Only non-negative, integer powers allowed’)
V accumulator = identity(m.len)
L(i) 0 .< pow
accumulator = matrix_mul(accumulator, m)
R accumulator
 
F printtable(data)
L(row) data
print(row.map(cell -> ‘#<5’.format(cell)).join(‘ ’))
 
V m = [[3, 2], [2, 1]]
L(i) 5
print("\n#.:".format(i))
printtable(matrixExp(m, i))
 
print("\n10:")
printtable(matrixExp(m, 10))</lang>
 
{{out}}
<pre>
 
0:
1 0
0 1
 
1:
3 2
2 1
 
2:
13 8
8 5
 
3:
55 34
34 21
 
4:
233 144
144 89
 
10:
1346269 832040
832040 514229
</pre>
 
=={{header|Ada}}==
1,481

edits