RosettaCodeData/Task/Matrix-exponentiation-operator/Icon/matrix-exponentiation-operator.icon
2026-04-30 12:34:36 -04:00

32 lines
584 B
Text

import util
procedure main()
M := Matrix([[3,2], [2,1]])
every i := 0 to 5 do {
write("M^",i,":")
writeMatrix(M^i)
}
end
class Matrix(M) # Operator overloading needs a class
# Extend the "^" binary operator
method __powr__(n)
if not (integer(n) >= 0) then fail
M1 := m_identity(*M,*M[1])
# Brute-force approach:
every 1 to n do M1 := m_multiply(M1,M)
return M1
end
initially (a)
M := m_copy(a)
end
procedure writeMatrix(M)
every r := !M do {
every writes(!r," ")
write()
}
write()
end