Most programming languages have a built-in implementation of exponentiation for integers and reals only.



Task

Demonstrate how to implement matrix exponentiation as an operator.




 
{require lib_matrix}
 
{def M.exp
 {lambda {:m :n}
  {if {= :n 0}
   then {M.new [ [1,0],[0,1] ]}
   else {S.reduce M.multiply {S.map {{lambda {:m _} :m} :m} {S.serie 1 :n}}}}}}
-> M.exp
 
'{def M 
 {M.new [[3,2],
         [2,1]]}}
-> M
 
{S.map {lambda {:i} {br}M{sup :i} = {M.exp {M} :i}} 
       0 1 2 3 4 10} 
->
M^0 = [[1,0],[0,1]] 
M^1 = [[3,2],[2,1]] 
M^2 = [[13,8],[8,5]] 
M^3 = [[55,34],[34,21]] 
M^4 = [[233,144],[144,89]] 
M^10 = [[1346269,832040],[832040,514229]] 
 