(* Calcul matriciel Variante du programme 83 page 333, paramétré par le type des éléments *) module Matrix (X : sig type t val zero : t val one : t val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t end) = struct type t = X.t array array let init_matrix n m f = Array.init n (fun i -> Array.init m (fun j -> f i j)) let id n = init_matrix n n (fun i j -> if i = j then X.one else X.zero) let size a = (Array.length a, Array.length a.(0)) let add a b = let (n, m) as s = size a in if size b <> s then invalid_arg "add"; init_matrix n m (fun i j -> X.add a.(i).(j) b.(i).(j)) let sub a b = let (n, m) as s = size a in if size b <> s then invalid_arg "sub"; init_matrix n m (fun i j -> X.sub a.(i).(j) b.(i).(j)) let mul a b = let n, p = size a in let q, m = size b in if q <> p then invalid_arg "mul"; let product i j = let s = ref X.zero in for k = 0 to p - 1 do s := X.add !s (X.mul a.(i).(k) b.(k).(j)) done; !s in init_matrix n m product end
This document was generated using caml2html