(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)
(** %\subsection*{ examples :  Matrices.v }%*)
(** - Some preliminary notions about matrices *)
Set Automatic Coercions Import.
Set Implicit Arguments.
Unset Strict Implicit.
Require Export Map2.
Require Export vecspace_Fn.
Definition Matrix_general_type (A : Setoid) (n m : Nat) :=
MAP2 (fin n) (fin m) A.
Definition matrix (F : field) := Matrix_general_type F.
Section add.
Let matrix_addition_fun (F : field) (m n : Nat) (M N : matrix F m n) :
matrix F m n.
intros.
apply (Build_Map2 (Ap2:=fun i j => M i j +' N i j)).
red in |- *.
intros.
apply SGROUP_comp; auto with algebra.
apply (Ap2_comp_proof M); auto with algebra.
apply (Ap2_comp_proof N); auto with algebra.
Defined.
Definition matrix_addition (F : field) (m n : Nat) :
MAP2 (matrix F m n) (matrix F m n) (matrix F m n).
intros.
apply (Build_Map2 (Ap2:=matrix_addition_fun (F:=F) (m:=m) (n:=n))).
red in |- *.
simpl in |- *; intros.
apply SGROUP_comp; auto with algebra.
Defined.
End add.
Section mult.
Definition matrix_scmult_fun (F : field) (m n : Nat)
(c : F) (M : matrix F m n) : matrix F m n.
intros.
apply (Build_Map2 (Ap2:=fun i j => c rX M i j)).
red in |- *.
intros.
apply RING_comp; auto with algebra.
apply (Ap2_comp_proof M); auto with algebra.
Defined.
Definition matrix_scmult (F : field) (m n : Nat) :
MAP2 F (matrix F m n) (matrix F m n).
intros.
apply (Build_Map2 (Ap2:=matrix_scmult_fun (F:=F) (m:=m) (n:=n))).
red in |- *.
simpl in |- *; intros.
apply RING_comp; auto with algebra.
Defined.
End mult.
Section transpose.
Definition transpose :
forall (F : field) (m n : Nat), matrix F m n -> matrix F n m.
intros.
red in |- *.
red in |- *.
red in X; red in X.
destruct X.
apply (Build_Map2 (Ap2:=fun i j => Ap2 j i)); auto with algebra.
red in Ap2_comp_proof; red in |- *.
intros.
auto with algebra.
Defined.
Definition transpose_map :
forall (F : field) (m n : Nat), MAP (matrix F m n) (matrix F n m).
intros.
apply (Build_Map (Ap:=transpose (F:=F) (m:=m) (n:=n))).
red in |- *.
unfold transpose in |- *.
intros; destruct x; destruct y.
simpl in |- *.
auto with algebra.
Defined.

(* Specifications: *)
Lemma transpose_defprop :
forall (F : field) (m n : Nat) (M : matrix F m n) (i : fin n) (j : fin m),
transpose M i j =' M j i in _.