RosettaCodeData/Task/Matrix-exponentiation-operator/OCaml/matrix-exponentiation-operator-2.ocaml

14 lines
381 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
let matpow a n =
let p, q = dim a in
if p <> q then failwith "bad dimensions" else
pow (eye p) matmul a n;;
matpow (matrix 2 2 [ 1.0; 1.0; 1.0; 0.0 ]) 10;;
(* - : float array array = [|[|89.; 55.|]; [|55.; 34.|]|] *)
(* use as infix operator *)
let ( ^^ ) = matpow;;
[| [| 1.0; 1.0|]; [| 1.0; 0.0 |] |] ^^ 10;;
(* - : float array array = [|[|89.; 55.|]; [|55.; 34.|]|] *)