
obua@14593: (*  Title:      HOL/Matrix/Matrix.thy
obua@14593:     ID:         $Id$
obua@14593:     Author:     Steven Obua
obua@14593: *)
obua@14593: 
wenzelm@17915: theory Matrix
haftmann@27484: imports Main
wenzelm@17915: begin
obua@14940: 
haftmann@27484: types 'a infmatrix = "nat \<Rightarrow> nat \<Rightarrow> 'a"
haftmann@27484: 
haftmann@27484: definition nonzero_positions :: "(nat \<Rightarrow> nat \<Rightarrow> 'a::zero) \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
haftmann@27484:   "nonzero_positions A = {pos. A (fst pos) (snd pos) ~= 0}"
haftmann@27484: 
haftmann@27484: typedef 'a matrix = "{(f::(nat \<Rightarrow> nat \<Rightarrow> 'a::zero)). finite (nonzero_positions f)}"
haftmann@27484: proof -
haftmann@27484:   have "(\<lambda>j i. 0) \<in> {(f::(nat \<Rightarrow> nat \<Rightarrow> 'a::zero)). finite (nonzero_positions f)}"
haftmann@27484:     by (simp add: nonzero_positions_def)
haftmann@27484:   then show ?thesis by auto
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: declare Rep_matrix_inverse[simp]
haftmann@27484: 
haftmann@27484: lemma finite_nonzero_positions : "finite (nonzero_positions (Rep_matrix A))"
haftmann@27484: apply (rule Abs_matrix_induct)
haftmann@27484: by (simp add: Abs_matrix_inverse matrix_def)
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   nrows :: "('a::zero) matrix \<Rightarrow> nat"
haftmann@27484:   "nrows A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image fst) (nonzero_positions (Rep_matrix A))))"
haftmann@27484:   ncols :: "('a::zero) matrix \<Rightarrow> nat"
haftmann@27484:   "ncols A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image snd) (nonzero_positions (Rep_matrix A))))"
haftmann@27484: 
haftmann@27484: lemma nrows:
haftmann@27484:   assumes hyp: "nrows A \<le> m"
haftmann@27484:   shows "(Rep_matrix A m n) = 0" (is ?concl)
haftmann@27484: proof cases
haftmann@27484:   assume "nonzero_positions(Rep_matrix A) = {}"
haftmann@27484:   then show "(Rep_matrix A m n) = 0" by (simp add: nonzero_positions_def)
haftmann@27484: next
haftmann@27484:   assume a: "nonzero_positions(Rep_matrix A) \<noteq> {}"
haftmann@27484:   let ?S = "fst`(nonzero_positions(Rep_matrix A))"
haftmann@27484:   have c: "finite (?S)" by (simp add: finite_nonzero_positions)
haftmann@27484:   from hyp have d: "Max (?S) < m" by (simp add: a nrows_def)
haftmann@27484:   have "m \<notin> ?S"
haftmann@27484:     proof -
haftmann@27484:       have "m \<in> ?S \<Longrightarrow> m <= Max(?S)" by (simp add: Max_ge [OF c])
haftmann@27484:       moreover from d have "~(m <= Max ?S)" by (simp)
haftmann@27484:       ultimately show "m \<notin> ?S" by (auto)
haftmann@27484:     qed
haftmann@27484:   thus "Rep_matrix A m n = 0" by (simp add: nonzero_positions_def image_Collect)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   transpose_infmatrix :: "'a infmatrix \<Rightarrow> 'a infmatrix"
haftmann@27484:   "transpose_infmatrix A j i == A i j"
haftmann@27484:   transpose_matrix :: "('a::zero) matrix \<Rightarrow> 'a matrix"
haftmann@27484:   "transpose_matrix == Abs_matrix o transpose_infmatrix o Rep_matrix"
haftmann@27484: 
haftmann@27484: declare transpose_infmatrix_def[simp]
haftmann@27484: 
haftmann@27484: lemma transpose_infmatrix_twice[simp]: "transpose_infmatrix (transpose_infmatrix A) = A"
haftmann@27484: by ((rule ext)+, simp)
haftmann@27484: 
haftmann@27484: lemma transpose_infmatrix: "transpose_infmatrix (% j i. P j i) = (% j i. P i j)"
haftmann@27484:   apply (rule ext)+
haftmann@27484:   by (simp add: transpose_infmatrix_def)
haftmann@27484: 
haftmann@27484: lemma transpose_infmatrix_closed[simp]: "Rep_matrix (Abs_matrix (transpose_infmatrix (Rep_matrix x))) = transpose_infmatrix (Rep_matrix x)"
haftmann@27484: apply (rule Abs_matrix_inverse)
haftmann@27484: apply (simp add: matrix_def nonzero_positions_def image_def)
haftmann@27484: proof -
haftmann@27484:   let ?A = "{pos. Rep_matrix x (snd pos) (fst pos) \<noteq> 0}"
haftmann@27484:   let ?swap = "% pos. (snd pos, fst pos)"
haftmann@27484:   let ?B = "{pos. Rep_matrix x (fst pos) (snd pos) \<noteq> 0}"
haftmann@27484:   have swap_image: "?swap`?A = ?B"
haftmann@27484:     apply (simp add: image_def)
haftmann@27484:     apply (rule set_ext)
haftmann@27484:     apply (simp)
haftmann@27484:     proof
haftmann@27484:       fix y
haftmann@27484:       assume hyp: "\<exists>a b. Rep_matrix x b a \<noteq> 0 \<and> y = (b, a)"
haftmann@27484:       thus "Rep_matrix x (fst y) (snd y) \<noteq> 0"
haftmann@27484:         proof -
haftmann@27484:           from hyp obtain a b where "(Rep_matrix x b a \<noteq> 0 & y = (b,a))" by blast
haftmann@27484:           then show "Rep_matrix x (fst y) (snd y) \<noteq> 0" by (simp)
haftmann@27484:         qed
haftmann@27484:     next
haftmann@27484:       fix y
haftmann@27484:       assume hyp: "Rep_matrix x (fst y) (snd y) \<noteq> 0"
haftmann@27484:       show "\<exists> a b. (Rep_matrix x b a \<noteq> 0 & y = (b,a))"
haftmann@27484: 	by (rule exI[of _ "snd y"], rule exI[of _ "fst y"]) (simp add: hyp)
haftmann@27484:     qed
haftmann@27484:   then have "finite (?swap`?A)"
haftmann@27484:     proof -
haftmann@27484:       have "finite (nonzero_positions (Rep_matrix x))" by (simp add: finite_nonzero_positions)
haftmann@27484:       then have "finite ?B" by (simp add: nonzero_positions_def)
haftmann@27484:       with swap_image show "finite (?swap`?A)" by (simp)
haftmann@27484:     qed
haftmann@27484:   moreover
haftmann@27484:   have "inj_on ?swap ?A" by (simp add: inj_on_def)
haftmann@27484:   ultimately show "finite ?A"by (rule finite_imageD[of ?swap ?A])
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma infmatrixforward: "(x::'a infmatrix) = y \<Longrightarrow> \<forall> a b. x a b = y a b" by auto
haftmann@27484: 
haftmann@27484: lemma transpose_infmatrix_inject: "(transpose_infmatrix A = transpose_infmatrix B) = (A = B)"
haftmann@27484: apply (auto)
haftmann@27484: apply (rule ext)+
haftmann@27484: apply (simp add: transpose_infmatrix)
haftmann@27484: apply (drule infmatrixforward)
haftmann@27484: apply (simp)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma transpose_matrix_inject: "(transpose_matrix A = transpose_matrix B) = (A = B)"
haftmann@27484: apply (simp add: transpose_matrix_def)
haftmann@27484: apply (subst Rep_matrix_inject[THEN sym])+
haftmann@27484: apply (simp only: transpose_infmatrix_closed transpose_infmatrix_inject)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma transpose_matrix[simp]: "Rep_matrix(transpose_matrix A) j i = Rep_matrix A i j"
haftmann@27484: by (simp add: transpose_matrix_def)
haftmann@27484: 
haftmann@27484: lemma transpose_transpose_id[simp]: "transpose_matrix (transpose_matrix A) = A"
haftmann@27484: by (simp add: transpose_matrix_def)
haftmann@27484: 
haftmann@27484: lemma nrows_transpose[simp]: "nrows (transpose_matrix A) = ncols A"
haftmann@27484: by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def)
haftmann@27484: 
haftmann@27484: lemma ncols_transpose[simp]: "ncols (transpose_matrix A) = nrows A"
haftmann@27484: by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def)
haftmann@27484: 
haftmann@27484: lemma ncols: "ncols A <= n \<Longrightarrow> Rep_matrix A m n = 0"
haftmann@27484: proof -
haftmann@27484:   assume "ncols A <= n"
haftmann@27484:   then have "nrows (transpose_matrix A) <= n" by (simp)
haftmann@27484:   then have "Rep_matrix (transpose_matrix A) n m = 0" by (rule nrows)
haftmann@27484:   thus "Rep_matrix A m n = 0" by (simp add: transpose_matrix_def)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma ncols_le: "(ncols A <= n) = (! j i. n <= i \<longrightarrow> (Rep_matrix A j i) = 0)" (is "_ = ?st")
haftmann@27484: apply (auto)
haftmann@27484: apply (simp add: ncols)
haftmann@27484: proof (simp add: ncols_def, auto)
haftmann@27484:   let ?P = "nonzero_positions (Rep_matrix A)"
haftmann@27484:   let ?p = "snd`?P"
haftmann@27484:   have a:"finite ?p" by (simp add: finite_nonzero_positions)
haftmann@27484:   let ?m = "Max ?p"
haftmann@27484:   assume "~(Suc (?m) <= n)"
haftmann@27484:   then have b:"n <= ?m" by (simp)
haftmann@27484:   fix a b
haftmann@27484:   assume "(a,b) \<in> ?P"
haftmann@27484:   then have "?p \<noteq> {}" by (auto)
haftmann@27484:   with a have "?m \<in>  ?p" by (simp)
haftmann@27484:   moreover have "!x. (x \<in> ?p \<longrightarrow> (? y. (Rep_matrix A y x) \<noteq> 0))" by (simp add: nonzero_positions_def image_def)
haftmann@27484:   ultimately have "? y. (Rep_matrix A y ?m) \<noteq> 0" by (simp)
haftmann@27484:   moreover assume ?st
haftmann@27484:   ultimately show "False" using b by (simp)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma less_ncols: "(n < ncols A) = (? j i. n <= i & (Rep_matrix A j i) \<noteq> 0)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have a: "!! (a::nat) b. (a < b) = (~(b <= a))" by arith
haftmann@27484:   show ?concl by (simp add: a ncols_le)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma le_ncols: "(n <= ncols A) = (\<forall> m. (\<forall> j i. m <= i \<longrightarrow> (Rep_matrix A j i) = 0) \<longrightarrow> n <= m)" (is ?concl)
haftmann@27484: apply (auto)
haftmann@27484: apply (subgoal_tac "ncols A <= m")
haftmann@27484: apply (simp)
haftmann@27484: apply (simp add: ncols_le)
haftmann@27484: apply (drule_tac x="ncols A" in spec)
haftmann@27484: by (simp add: ncols)
haftmann@27484: 
haftmann@27484: lemma nrows_le: "(nrows A <= n) = (! j i. n <= j \<longrightarrow> (Rep_matrix A j i) = 0)" (is ?s)
haftmann@27484: proof -
haftmann@27484:   have "(nrows A <= n) = (ncols (transpose_matrix A) <= n)" by (simp)
haftmann@27484:   also have "\<dots> = (! j i. n <= i \<longrightarrow> (Rep_matrix (transpose_matrix A) j i = 0))" by (rule ncols_le)
haftmann@27484:   also have "\<dots> = (! j i. n <= i \<longrightarrow> (Rep_matrix A i j) = 0)" by (simp)
haftmann@27484:   finally show "(nrows A <= n) = (! j i. n <= j \<longrightarrow> (Rep_matrix A j i) = 0)" by (auto)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma less_nrows: "(m < nrows A) = (? j i. m <= j & (Rep_matrix A j i) \<noteq> 0)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have a: "!! (a::nat) b. (a < b) = (~(b <= a))" by arith
haftmann@27484:   show ?concl by (simp add: a nrows_le)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma le_nrows: "(n <= nrows A) = (\<forall> m. (\<forall> j i. m <= j \<longrightarrow> (Rep_matrix A j i) = 0) \<longrightarrow> n <= m)" (is ?concl)
haftmann@27484: apply (auto)
haftmann@27484: apply (subgoal_tac "nrows A <= m")
haftmann@27484: apply (simp)
haftmann@27484: apply (simp add: nrows_le)
haftmann@27484: apply (drule_tac x="nrows A" in spec)
haftmann@27484: by (simp add: nrows)
haftmann@27484: 
haftmann@27484: lemma nrows_notzero: "Rep_matrix A m n \<noteq> 0 \<Longrightarrow> m < nrows A"
haftmann@27484: apply (case_tac "nrows A <= m")
haftmann@27484: apply (simp_all add: nrows)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma ncols_notzero: "Rep_matrix A m n \<noteq> 0 \<Longrightarrow> n < ncols A"
haftmann@27484: apply (case_tac "ncols A <= n")
haftmann@27484: apply (simp_all add: ncols)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma finite_natarray1: "finite {x. x < (n::nat)}"
haftmann@27484: apply (induct n)
haftmann@27484: apply (simp)
haftmann@27484: proof -
haftmann@27484:   fix n
haftmann@27484:   have "{x. x < Suc n} = insert n {x. x < n}"  by (rule set_ext, simp, arith)
haftmann@27484:   moreover assume "finite {x. x < n}"
haftmann@27484:   ultimately show "finite {x. x < Suc n}" by (simp)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma finite_natarray2: "finite {pos. (fst pos) < (m::nat) & (snd pos) < (n::nat)}"
haftmann@27484:   apply (induct m)
haftmann@27484:   apply (simp+)
haftmann@27484:   proof -
haftmann@27484:     fix m::nat
haftmann@27484:     let ?s0 = "{pos. fst pos < m & snd pos < n}"
haftmann@27484:     let ?s1 = "{pos. fst pos < (Suc m) & snd pos < n}"
haftmann@27484:     let ?sd = "{pos. fst pos = m & snd pos < n}"
haftmann@27484:     assume f0: "finite ?s0"
haftmann@27484:     have f1: "finite ?sd"
haftmann@27484:     proof -
haftmann@27484:       let ?f = "% x. (m, x)"
haftmann@27484:       have "{pos. fst pos = m & snd pos < n} = ?f ` {x. x < n}" by (rule set_ext, simp add: image_def, auto)
haftmann@27484:       moreover have "finite {x. x < n}" by (simp add: finite_natarray1)
haftmann@27484:       ultimately show "finite {pos. fst pos = m & snd pos < n}" by (simp)
haftmann@27484:     qed
haftmann@27484:     have su: "?s0 \<union> ?sd = ?s1" by (rule set_ext, simp, arith)
haftmann@27484:     from f0 f1 have "finite (?s0 \<union> ?sd)" by (rule finite_UnI)
haftmann@27484:     with su show "finite ?s1" by (simp)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma RepAbs_matrix:
haftmann@27484:   assumes aem: "? m. ! j i. m <= j \<longrightarrow> x j i = 0" (is ?em) and aen:"? n. ! j i. (n <= i \<longrightarrow> x j i = 0)" (is ?en)
haftmann@27484:   shows "(Rep_matrix (Abs_matrix x)) = x"
haftmann@27484: apply (rule Abs_matrix_inverse)
haftmann@27484: apply (simp add: matrix_def nonzero_positions_def)
haftmann@27484: proof -
haftmann@27484:   from aem obtain m where a: "! j i. m <= j \<longrightarrow> x j i = 0" by (blast)
haftmann@27484:   from aen obtain n where b: "! j i. n <= i \<longrightarrow> x j i = 0" by (blast)
haftmann@27484:   let ?u = "{pos. x (fst pos) (snd pos) \<noteq> 0}"
haftmann@27484:   let ?v = "{pos. fst pos < m & snd pos < n}"
haftmann@27484:   have c: "!! (m::nat) a. ~(m <= a) \<Longrightarrow> a < m" by (arith)
haftmann@27484:   from a b have "(?u \<inter> (-?v)) = {}"
haftmann@27484:     apply (simp)
haftmann@27484:     apply (rule set_ext)
haftmann@27484:     apply (simp)
haftmann@27484:     apply auto
haftmann@27484:     by (rule c, auto)+
haftmann@27484:   then have d: "?u \<subseteq> ?v" by blast
haftmann@27484:   moreover have "finite ?v" by (simp add: finite_natarray2)
haftmann@27484:   ultimately show "finite ?u" by (rule finite_subset)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   apply_infmatrix :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a infmatrix \<Rightarrow> 'b infmatrix"
haftmann@27484:   "apply_infmatrix f == % A. (% j i. f (A j i))"
haftmann@27484:   apply_matrix :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a::zero) matrix \<Rightarrow> ('b::zero) matrix"
haftmann@27484:   "apply_matrix f == % A. Abs_matrix (apply_infmatrix f (Rep_matrix A))"
haftmann@27484:   combine_infmatrix :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a infmatrix \<Rightarrow> 'b infmatrix \<Rightarrow> 'c infmatrix"
haftmann@27484:   "combine_infmatrix f == % A B. (% j i. f (A j i) (B j i))"
haftmann@27484:   combine_matrix :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a::zero) matrix \<Rightarrow> ('b::zero) matrix \<Rightarrow> ('c::zero) matrix"
haftmann@27484:   "combine_matrix f == % A B. Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))"
haftmann@27484: 
haftmann@27484: lemma expand_apply_infmatrix[simp]: "apply_infmatrix f A j i = f (A j i)"
haftmann@27484: by (simp add: apply_infmatrix_def)
haftmann@27484: 
haftmann@27484: lemma expand_combine_infmatrix[simp]: "combine_infmatrix f A B j i = f (A j i) (B j i)"
haftmann@27484: by (simp add: combine_infmatrix_def)
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484: commutative :: "('a \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> bool"
haftmann@27484: "commutative f == ! x y. f x y = f y x"
haftmann@27484: associative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool"
haftmann@27484: "associative f == ! x y z. f (f x y) z = f x (f y z)"
haftmann@27484: 
haftmann@27484: text{*
haftmann@27484: To reason about associativity and commutativity of operations on matrices,
haftmann@27484: let's take a step back and look at the general situtation: Assume that we have
haftmann@27484: sets $A$ and $B$ with $B \subset A$ and an abstraction $u: A \rightarrow B$. This abstraction has to fulfill $u(b) = b$ for all $b \in B$, but is arbitrary otherwise.
haftmann@27484: Each function $f: A \times A \rightarrow A$ now induces a function $f': B \times B \rightarrow B$ by $f' = u \circ f$.
haftmann@27484: It is obvious that commutativity of $f$ implies commutativity of $f'$: $f' x y = u (f x y) = u (f y x) = f' y x.$
haftmann@27484: *}
haftmann@27484: 
haftmann@27484: lemma combine_infmatrix_commute:
haftmann@27484:   "commutative f \<Longrightarrow> commutative (combine_infmatrix f)"
haftmann@27484: by (simp add: commutative_def combine_infmatrix_def)
haftmann@27484: 
haftmann@27484: lemma combine_matrix_commute:
haftmann@27484: "commutative f \<Longrightarrow> commutative (combine_matrix f)"
haftmann@27484: by (simp add: combine_matrix_def commutative_def combine_infmatrix_def)
haftmann@27484: 
haftmann@27484: text{*
haftmann@27484: On the contrary, given an associative function $f$ we cannot expect $f'$ to be associative. A counterexample is given by $A=\ganz$, $B=\{-1, 0, 1\}$,
haftmann@27484: as $f$ we take addition on $\ganz$, which is clearly associative. The abstraction is given by  $u(a) = 0$ for $a \notin B$. Then we have
haftmann@27484: \[ f' (f' 1 1) -1 = u(f (u (f 1 1)) -1) = u(f (u 2) -1) = u (f 0 -1) = -1, \]
haftmann@27484: but on the other hand we have
haftmann@27484: \[ f' 1 (f' 1 -1) = u (f 1 (u (f 1 -1))) = u (f 1 0) = 1.\]
haftmann@27484: A way out of this problem is to assume that $f(A\times A)\subset A$ holds, and this is what we are going to do:
haftmann@27484: *}
haftmann@27484: 
haftmann@27484: lemma nonzero_positions_combine_infmatrix[simp]: "f 0 0 = 0 \<Longrightarrow> nonzero_positions (combine_infmatrix f A B) \<subseteq> (nonzero_positions A) \<union> (nonzero_positions B)"
haftmann@27484: by (rule subsetI, simp add: nonzero_positions_def combine_infmatrix_def, auto)
haftmann@27484: 
haftmann@27484: lemma finite_nonzero_positions_Rep[simp]: "finite (nonzero_positions (Rep_matrix A))"
haftmann@27484: by (insert Rep_matrix [of A], simp add: matrix_def)
haftmann@27484: 
haftmann@27484: lemma combine_infmatrix_closed [simp]:
haftmann@27484:   "f 0 0 = 0 \<Longrightarrow> Rep_matrix (Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) = combine_infmatrix f (Rep_matrix A) (Rep_matrix B)"
haftmann@27484: apply (rule Abs_matrix_inverse)
haftmann@27484: apply (simp add: matrix_def)
haftmann@27484: apply (rule finite_subset[of _ "(nonzero_positions (Rep_matrix A)) \<union> (nonzero_positions (Rep_matrix B))"])
haftmann@27484: by (simp_all)
haftmann@27484: 
haftmann@27484: text {* We need the next two lemmas only later, but it is analog to the above one, so we prove them now: *}
haftmann@27484: lemma nonzero_positions_apply_infmatrix[simp]: "f 0 = 0 \<Longrightarrow> nonzero_positions (apply_infmatrix f A) \<subseteq> nonzero_positions A"
haftmann@27484: by (rule subsetI, simp add: nonzero_positions_def apply_infmatrix_def, auto)
haftmann@27484: 
haftmann@27484: lemma apply_infmatrix_closed [simp]:
haftmann@27484:   "f 0 = 0 \<Longrightarrow> Rep_matrix (Abs_matrix (apply_infmatrix f (Rep_matrix A))) = apply_infmatrix f (Rep_matrix A)"
haftmann@27484: apply (rule Abs_matrix_inverse)
haftmann@27484: apply (simp add: matrix_def)
haftmann@27484: apply (rule finite_subset[of _ "nonzero_positions (Rep_matrix A)"])
haftmann@27484: by (simp_all)
haftmann@27484: 
haftmann@27484: lemma combine_infmatrix_assoc[simp]: "f 0 0 = 0 \<Longrightarrow> associative f \<Longrightarrow> associative (combine_infmatrix f)"
haftmann@27484: by (simp add: associative_def combine_infmatrix_def)
haftmann@27484: 
haftmann@27484: lemma comb: "f = g \<Longrightarrow> x = y \<Longrightarrow> f x = g y"
haftmann@27484: by (auto)
haftmann@27484: 
haftmann@27484: lemma combine_matrix_assoc: "f 0 0 = 0 \<Longrightarrow> associative f \<Longrightarrow> associative (combine_matrix f)"
haftmann@27484: apply (simp(no_asm) add: associative_def combine_matrix_def, auto)
haftmann@27484: apply (rule comb [of Abs_matrix Abs_matrix])
haftmann@27484: by (auto, insert combine_infmatrix_assoc[of f], simp add: associative_def)
haftmann@27484: 
haftmann@27484: lemma Rep_apply_matrix[simp]: "f 0 = 0 \<Longrightarrow> Rep_matrix (apply_matrix f A) j i = f (Rep_matrix A j i)"
haftmann@27484: by (simp add: apply_matrix_def)
haftmann@27484: 
haftmann@27484: lemma Rep_combine_matrix[simp]: "f 0 0 = 0 \<Longrightarrow> Rep_matrix (combine_matrix f A B) j i = f (Rep_matrix A j i) (Rep_matrix B j i)"
haftmann@27484:   by(simp add: combine_matrix_def)
haftmann@27484: 
haftmann@27484: lemma combine_nrows_max: "f 0 0 = 0  \<Longrightarrow> nrows (combine_matrix f A B) <= max (nrows A) (nrows B)"
haftmann@27484: by (simp add: nrows_le)
haftmann@27484: 
haftmann@27484: lemma combine_ncols_max: "f 0 0 = 0 \<Longrightarrow> ncols (combine_matrix f A B) <= max (ncols A) (ncols B)"
haftmann@27484: by (simp add: ncols_le)
haftmann@27484: 
haftmann@27484: lemma combine_nrows: "f 0 0 = 0 \<Longrightarrow> nrows A <= q \<Longrightarrow> nrows B <= q \<Longrightarrow> nrows(combine_matrix f A B) <= q"
haftmann@27484:   by (simp add: nrows_le)
haftmann@27484: 
haftmann@27484: lemma combine_ncols: "f 0 0 = 0 \<Longrightarrow> ncols A <= q \<Longrightarrow> ncols B <= q \<Longrightarrow> ncols(combine_matrix f A B) <= q"
haftmann@27484:   by (simp add: ncols_le)
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   zero_r_neutral :: "('a \<Rightarrow> 'b::zero \<Rightarrow> 'a) \<Rightarrow> bool"
haftmann@27484:   "zero_r_neutral f == ! a. f a 0 = a"
haftmann@27484:   zero_l_neutral :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool"
haftmann@27484:   "zero_l_neutral f == ! a. f 0 a = a"
haftmann@27484:   zero_closed :: "(('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> bool"
haftmann@27484:   "zero_closed f == (!x. f x 0 = 0) & (!y. f 0 y = 0)"
haftmann@27484: 
haftmann@27484: consts foldseq :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a"
haftmann@27484: primrec
haftmann@27484:   "foldseq f s 0 = s 0"
haftmann@27484:   "foldseq f s (Suc n) = f (s 0) (foldseq f (% k. s(Suc k)) n)"
haftmann@27484: 
haftmann@27484: consts foldseq_transposed ::  "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a"
haftmann@27484: primrec
haftmann@27484:   "foldseq_transposed f s 0 = s 0"
haftmann@27484:   "foldseq_transposed f s (Suc n) = f (foldseq_transposed f s n) (s (Suc n))"
haftmann@27484: 
haftmann@27484: lemma foldseq_assoc : "associative f \<Longrightarrow> foldseq f = foldseq_transposed f"
haftmann@27484: proof -
haftmann@27484:   assume a:"associative f"
haftmann@27484:   then have sublemma: "!! n. ! N s. N <= n \<longrightarrow> foldseq f s N = foldseq_transposed f s N"
haftmann@27484:   proof -
haftmann@27484:     fix n
haftmann@27484:     show "!N s. N <= n \<longrightarrow> foldseq f s N = foldseq_transposed f s N"
haftmann@27484:     proof (induct n)
haftmann@27484:       show "!N s. N <= 0 \<longrightarrow> foldseq f s N = foldseq_transposed f s N" by simp
haftmann@27484:     next
haftmann@27484:       fix n
haftmann@27484:       assume b:"! N s. N <= n \<longrightarrow> foldseq f s N = foldseq_transposed f s N"
haftmann@27484:       have c:"!!N s. N <= n \<Longrightarrow> foldseq f s N = foldseq_transposed f s N" by (simp add: b)
haftmann@27484:       show "! N t. N <= Suc n \<longrightarrow> foldseq f t N = foldseq_transposed f t N"
haftmann@27484:       proof (auto)
haftmann@27484:         fix N t
haftmann@27484:         assume Nsuc: "N <= Suc n"
haftmann@27484:         show "foldseq f t N = foldseq_transposed f t N"
haftmann@27484:         proof cases
haftmann@27484:           assume "N <= n"
haftmann@27484:           then show "foldseq f t N = foldseq_transposed f t N" by (simp add: b)
haftmann@27484:         next
haftmann@27484:           assume "~(N <= n)"
haftmann@27484:           with Nsuc have Nsuceq: "N = Suc n" by simp
haftmann@27484:           have neqz: "n \<noteq> 0 \<Longrightarrow> ? m. n = Suc m & Suc m <= n" by arith
haftmann@27484:           have assocf: "!! x y z. f x (f y z) = f (f x y) z" by (insert a, simp add: associative_def)
haftmann@27484:           show "foldseq f t N = foldseq_transposed f t N"
haftmann@27484:             apply (simp add: Nsuceq)
haftmann@27484:             apply (subst c)
haftmann@27484:             apply (simp)
haftmann@27484:             apply (case_tac "n = 0")
haftmann@27484:             apply (simp)
haftmann@27484:             apply (drule neqz)
haftmann@27484:             apply (erule exE)
haftmann@27484:             apply (simp)
haftmann@27484:             apply (subst assocf)
haftmann@27484:             proof -
haftmann@27484:               fix m
haftmann@27484:               assume "n = Suc m & Suc m <= n"
haftmann@27484:               then have mless: "Suc m <= n" by arith
haftmann@27484:               then have step1: "foldseq_transposed f (% k. t (Suc k)) m = foldseq f (% k. t (Suc k)) m" (is "?T1 = ?T2")
haftmann@27484:                 apply (subst c)
haftmann@27484:                 by simp+
haftmann@27484:               have step2: "f (t 0) ?T2 = foldseq f t (Suc m)" (is "_ = ?T3") by simp
haftmann@27484:               have step3: "?T3 = foldseq_transposed f t (Suc m)" (is "_ = ?T4")
haftmann@27484:                 apply (subst c)
haftmann@27484:                 by (simp add: mless)+
haftmann@27484:               have step4: "?T4 = f (foldseq_transposed f t m) (t (Suc m))" (is "_=?T5") by simp
haftmann@27484:               from step1 step2 step3 step4 show sowhat: "f (f (t 0) ?T1) (t (Suc (Suc m))) = f ?T5 (t (Suc (Suc m)))" by simp
haftmann@27484:             qed
haftmann@27484:           qed
haftmann@27484:         qed
haftmann@27484:       qed
haftmann@27484:     qed
haftmann@27484:     show "foldseq f = foldseq_transposed f" by ((rule ext)+, insert sublemma, auto)
haftmann@27484:   qed
haftmann@27484: 
haftmann@27484: lemma foldseq_distr: "\<lbrakk>associative f; commutative f\<rbrakk> \<Longrightarrow> foldseq f (% k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)"
haftmann@27484: proof -
haftmann@27484:   assume assoc: "associative f"
haftmann@27484:   assume comm: "commutative f"
haftmann@27484:   from assoc have a:"!! x y z. f (f x y) z = f x (f y z)" by (simp add: associative_def)
haftmann@27484:   from comm have b: "!! x y. f x y = f y x" by (simp add: commutative_def)
haftmann@27484:   from assoc comm have c: "!! x y z. f x (f y z) = f y (f x z)" by (simp add: commutative_def associative_def)
haftmann@27484:   have "!! n. (! u v. foldseq f (%k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n))"
haftmann@27484:     apply (induct_tac n)
haftmann@27484:     apply (simp+, auto)
haftmann@27484:     by (simp add: a b c)
haftmann@27484:   then show "foldseq f (% k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)" by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: theorem "\<lbrakk>associative f; associative g; \<forall>a b c d. g (f a b) (f c d) = f (g a c) (g b d); ? x y. (f x) \<noteq> (f y); ? x y. (g x) \<noteq> (g y); f x x = x; g x x = x\<rbrakk> \<Longrightarrow> f=g | (! y. f y x = y) | (! y. g y x = y)"
haftmann@27484: oops
haftmann@27484: (* Model found
haftmann@27484: 
haftmann@27484: Trying to find a model that refutes: \<lbrakk>associative f; associative g;
haftmann@27484:  \<forall>a b c d. g (f a b) (f c d) = f (g a c) (g b d); \<exists>x y. f x \<noteq> f y;
haftmann@27484:  \<exists>x y. g x \<noteq> g y; f x x = x; g x x = x\<rbrakk>
haftmann@27484: \<Longrightarrow> f = g \<or> (\<forall>y. f y x = y) \<or> (\<forall>y. g y x = y)
haftmann@27484: Searching for a model of size 1, translating term... invoking SAT solver... no model found.
haftmann@27484: Searching for a model of size 2, translating term... invoking SAT solver... no model found.
haftmann@27484: Searching for a model of size 3, translating term... invoking SAT solver...
haftmann@27484: Model found:
haftmann@27484: Size of types: 'a: 3
haftmann@27484: x: a1
haftmann@27484: g: (a0\<mapsto>(a0\<mapsto>a1, a1\<mapsto>a0, a2\<mapsto>a1), a1\<mapsto>(a0\<mapsto>a0, a1\<mapsto>a1, a2\<mapsto>a0), a2\<mapsto>(a0\<mapsto>a1, a1\<mapsto>a0, a2\<mapsto>a1))
haftmann@27484: f: (a0\<mapsto>(a0\<mapsto>a0, a1\<mapsto>a0, a2\<mapsto>a0), a1\<mapsto>(a0\<mapsto>a1, a1\<mapsto>a1, a2\<mapsto>a1), a2\<mapsto>(a0\<mapsto>a0, a1\<mapsto>a0, a2\<mapsto>a0))
haftmann@27484: *)
haftmann@27484: 
haftmann@27484: lemma foldseq_zero:
haftmann@27484: assumes fz: "f 0 0 = 0" and sz: "! i. i <= n \<longrightarrow> s i = 0"
haftmann@27484: shows "foldseq f s n = 0"
haftmann@27484: proof -
haftmann@27484:   have "!! n. ! s. (! i. i <= n \<longrightarrow> s i = 0) \<longrightarrow> foldseq f s n = 0"
haftmann@27484:     apply (induct_tac n)
haftmann@27484:     apply (simp)
haftmann@27484:     by (simp add: fz)
haftmann@27484:   then show "foldseq f s n = 0" by (simp add: sz)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_significant_positions:
haftmann@27484:   assumes p: "! i. i <= N \<longrightarrow> S i = T i"
haftmann@27484:   shows "foldseq f S N = foldseq f T N" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have "!! m . ! s t. (! i. i<=m \<longrightarrow> s i = t i) \<longrightarrow> foldseq f s m = foldseq f t m"
haftmann@27484:     apply (induct_tac m)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (auto)
haftmann@27484:     proof -
haftmann@27484:       fix n
haftmann@27484:       fix s::"nat\<Rightarrow>'a"
haftmann@27484:       fix t::"nat\<Rightarrow>'a"
haftmann@27484:       assume a: "\<forall>s t. (\<forall>i\<le>n. s i = t i) \<longrightarrow> foldseq f s n = foldseq f t n"
haftmann@27484:       assume b: "\<forall>i\<le>Suc n. s i = t i"
haftmann@27484:       have c:"!! a b. a = b \<Longrightarrow> f (t 0) a = f (t 0) b" by blast
haftmann@27484:       have d:"!! s t. (\<forall>i\<le>n. s i = t i) \<Longrightarrow> foldseq f s n = foldseq f t n" by (simp add: a)
haftmann@27484:       show "f (t 0) (foldseq f (\<lambda>k. s (Suc k)) n) = f (t 0) (foldseq f (\<lambda>k. t (Suc k)) n)" by (rule c, simp add: d b)
haftmann@27484:     qed
haftmann@27484:   with p show ?concl by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_tail: "M <= N \<Longrightarrow> foldseq f S N = foldseq f (% k. (if k < M then (S k) else (foldseq f (% k. S(k+M)) (N-M)))) M" (is "?p \<Longrightarrow> ?concl")
haftmann@27484: proof -
haftmann@27484:   have suc: "!! a b. \<lbrakk>a <= Suc b; a \<noteq> Suc b\<rbrakk> \<Longrightarrow> a <= b" by arith
haftmann@27484:   have a:"!! a b c . a = b \<Longrightarrow> f c a = f c b" by blast
haftmann@27484:   have "!! n. ! m s. m <= n \<longrightarrow> foldseq f s n = foldseq f (% k. (if k < m then (s k) else (foldseq f (% k. s(k+m)) (n-m)))) m"
haftmann@27484:     apply (induct_tac n)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (auto)
haftmann@27484:     apply (case_tac "m = Suc na")
haftmann@27484:     apply (simp)
haftmann@27484:     apply (rule a)
haftmann@27484:     apply (rule foldseq_significant_positions)
haftmann@27484:     apply (auto)
haftmann@27484:     apply (drule suc, simp+)
haftmann@27484:     proof -
haftmann@27484:       fix na m s
haftmann@27484:       assume suba:"\<forall>m\<le>na. \<forall>s. foldseq f s na = foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (na - m))m"
haftmann@27484:       assume subb:"m <= na"
haftmann@27484:       from suba have subc:"!! m s. m <= na \<Longrightarrow>foldseq f s na = foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (na - m))m" by simp
haftmann@27484:       have subd: "foldseq f (\<lambda>k. if k < m then s (Suc k) else foldseq f (\<lambda>k. s (Suc (k + m))) (na - m)) m =
haftmann@27484:         foldseq f (% k. s(Suc k)) na"
haftmann@27484:         by (rule subc[of m "% k. s(Suc k)", THEN sym], simp add: subb)
haftmann@27484:       from subb have sube: "m \<noteq> 0 \<Longrightarrow> ? mm. m = Suc mm & mm <= na" by arith
haftmann@27484:       show "f (s 0) (foldseq f (\<lambda>k. if k < m then s (Suc k) else foldseq f (\<lambda>k. s (Suc (k + m))) (na - m)) m) =
haftmann@27484:         foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (Suc na - m)) m"
haftmann@27484:         apply (simp add: subd)
haftmann@27484:         apply (case_tac "m=0")
haftmann@27484:         apply (simp)
haftmann@27484:         apply (drule sube)
haftmann@27484:         apply (auto)
haftmann@27484:         apply (rule a)
haftmann@27484:         by (simp add: subc if_def)
haftmann@27484:     qed
haftmann@27484:   then show "?p \<Longrightarrow> ?concl" by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_zerotail:
haftmann@27484:   assumes
haftmann@27484:   fz: "f 0 0 = 0"
haftmann@27484:   and sz: "! i.  n <= i \<longrightarrow> s i = 0"
haftmann@27484:   and nm: "n <= m"
haftmann@27484:   shows
haftmann@27484:   "foldseq f s n = foldseq f s m"
haftmann@27484: proof -
haftmann@27484:   show "foldseq f s n = foldseq f s m"
haftmann@27484:     apply (simp add: foldseq_tail[OF nm, of f s])
haftmann@27484:     apply (rule foldseq_significant_positions)
haftmann@27484:     apply (auto)
haftmann@27484:     apply (subst foldseq_zero)
haftmann@27484:     by (simp add: fz sz)+
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_zerotail2:
haftmann@27484:   assumes "! x. f x 0 = x"
haftmann@27484:   and "! i. n < i \<longrightarrow> s i = 0"
haftmann@27484:   and nm: "n <= m"
haftmann@27484:   shows
haftmann@27484:   "foldseq f s n = foldseq f s m" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have "f 0 0 = 0" by (simp add: prems)
haftmann@27484:   have b:"!! m n. n <= m \<Longrightarrow> m \<noteq> n \<Longrightarrow> ? k. m-n = Suc k" by arith
haftmann@27484:   have c: "0 <= m" by simp
haftmann@27484:   have d: "!! k. k \<noteq> 0 \<Longrightarrow> ? l. k = Suc l" by arith
haftmann@27484:   show ?concl
haftmann@27484:     apply (subst foldseq_tail[OF nm])
haftmann@27484:     apply (rule foldseq_significant_positions)
haftmann@27484:     apply (auto)
haftmann@27484:     apply (case_tac "m=n")
haftmann@27484:     apply (simp+)
haftmann@27484:     apply (drule b[OF nm])
haftmann@27484:     apply (auto)
haftmann@27484:     apply (case_tac "k=0")
haftmann@27484:     apply (simp add: prems)
haftmann@27484:     apply (drule d)
haftmann@27484:     apply (auto)
haftmann@27484:     by (simp add: prems foldseq_zero)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_zerostart:
haftmann@27484:   "! x. f 0 (f 0 x) = f 0 x \<Longrightarrow>  ! i. i <= n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))"
haftmann@27484: proof -
haftmann@27484:   assume f00x: "! x. f 0 (f 0 x) = f 0 x"
haftmann@27484:   have "! s. (! i. i<=n \<longrightarrow> s i = 0) \<longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))"
haftmann@27484:     apply (induct n)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (rule allI, rule impI)
haftmann@27484:     proof -
haftmann@27484:       fix n
haftmann@27484:       fix s
haftmann@27484:       have a:"foldseq f s (Suc (Suc n)) = f (s 0) (foldseq f (% k. s(Suc k)) (Suc n))" by simp
haftmann@27484:       assume b: "! s. ((\<forall>i\<le>n. s i = 0) \<longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n)))"
haftmann@27484:       from b have c:"!! s. (\<forall>i\<le>n. s i = 0) \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" by simp
haftmann@27484:       assume d: "! i. i <= Suc n \<longrightarrow> s i = 0"
haftmann@27484:       show "foldseq f s (Suc (Suc n)) = f 0 (s (Suc (Suc n)))"
haftmann@27484:         apply (subst a)
haftmann@27484:         apply (subst c)
haftmann@27484:         by (simp add: d f00x)+
haftmann@27484:     qed
haftmann@27484:   then show "! i. i <= n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_zerostart2:
haftmann@27484:   "! x. f 0 x = x \<Longrightarrow>  ! i. i < n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s n = s n"
haftmann@27484: proof -
haftmann@27484:   assume a:"! i. i<n \<longrightarrow> s i = 0"
haftmann@27484:   assume x:"! x. f 0 x = x"
haftmann@27484:   from x have f00x: "! x. f 0 (f 0 x) = f 0 x" by blast
haftmann@27484:   have b: "!! i l. i < Suc l = (i <= l)" by arith
haftmann@27484:   have d: "!! k. k \<noteq> 0 \<Longrightarrow> ? l. k = Suc l" by arith
haftmann@27484:   show "foldseq f s n = s n"
haftmann@27484:   apply (case_tac "n=0")
haftmann@27484:   apply (simp)
haftmann@27484:   apply (insert a)
haftmann@27484:   apply (drule d)
haftmann@27484:   apply (auto)
haftmann@27484:   apply (simp add: b)
haftmann@27484:   apply (insert f00x)
haftmann@27484:   apply (drule foldseq_zerostart)
haftmann@27484:   by (simp add: x)+
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_almostzero:
haftmann@27484:   assumes f0x:"! x. f 0 x = x" and fx0: "! x. f x 0 = x" and s0:"! i. i \<noteq> j \<longrightarrow> s i = 0"
haftmann@27484:   shows "foldseq f s n = (if (j <= n) then (s j) else 0)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   from s0 have a: "! i. i < j \<longrightarrow> s i = 0" by simp
haftmann@27484:   from s0 have b: "! i. j < i \<longrightarrow> s i = 0" by simp
haftmann@27484:   show ?concl
haftmann@27484:     apply auto
haftmann@27484:     apply (subst foldseq_zerotail2[of f, OF fx0, of j, OF b, of n, THEN sym])
haftmann@27484:     apply simp
haftmann@27484:     apply (subst foldseq_zerostart2)
haftmann@27484:     apply (simp add: f0x a)+
haftmann@27484:     apply (subst foldseq_zero)
haftmann@27484:     by (simp add: s0 f0x)+
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_distr_unary:
haftmann@27484:   assumes "!! a b. g (f a b) = f (g a) (g b)"
haftmann@27484:   shows "g(foldseq f s n) = foldseq f (% x. g(s x)) n" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have "! s. g(foldseq f s n) = foldseq f (% x. g(s x)) n"
haftmann@27484:     apply (induct_tac n)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (auto)
haftmann@27484:     apply (drule_tac x="% k. s (Suc k)" in spec)
haftmann@27484:     by (simp add: prems)
haftmann@27484:   then show ?concl by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   mult_matrix_n :: "nat \<Rightarrow> (('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> ('c \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> 'a matrix \<Rightarrow> 'b matrix \<Rightarrow> 'c matrix"
haftmann@27484:   "mult_matrix_n n fmul fadd A B == Abs_matrix(% j i. foldseq fadd (% k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) n)"
haftmann@27484:   mult_matrix :: "(('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> ('c \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> 'a matrix \<Rightarrow> 'b matrix \<Rightarrow> 'c matrix"
haftmann@27484:   "mult_matrix fmul fadd A B == mult_matrix_n (max (ncols A) (nrows B)) fmul fadd A B"
haftmann@27484: 
haftmann@27484: lemma mult_matrix_n:
haftmann@27484:   assumes prems: "ncols A \<le>  n" (is ?An) "nrows B \<le> n" (is ?Bn) "fadd 0 0 = 0" "fmul 0 0 = 0"
haftmann@27484:   shows c:"mult_matrix fmul fadd A B = mult_matrix_n n fmul fadd A B" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   show ?concl using prems
haftmann@27484:     apply (simp add: mult_matrix_def mult_matrix_n_def)
haftmann@27484:     apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
haftmann@27484:     by (rule foldseq_zerotail, simp_all add: nrows_le ncols_le prems)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma mult_matrix_nm:
haftmann@27484:   assumes prems: "ncols A <= n" "nrows B <= n" "ncols A <= m" "nrows B <= m" "fadd 0 0 = 0" "fmul 0 0 = 0"
haftmann@27484:   shows "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B"
haftmann@27484: proof -
haftmann@27484:   from prems have "mult_matrix_n n fmul fadd A B = mult_matrix fmul fadd A B" by (simp add: mult_matrix_n)
haftmann@27484:   also from prems have "\<dots> = mult_matrix_n m fmul fadd A B" by (simp add: mult_matrix_n[THEN sym])
haftmann@27484:   finally show "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B" by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   r_distributive :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool"
haftmann@27484:   "r_distributive fmul fadd == ! a u v. fmul a (fadd u v) = fadd (fmul a u) (fmul a v)"
haftmann@27484:   l_distributive :: "('a \<Rightarrow> 'b \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool"
haftmann@27484:   "l_distributive fmul fadd == ! a u v. fmul (fadd u v) a = fadd (fmul u a) (fmul v a)"
haftmann@27484:   distributive :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool"
haftmann@27484:   "distributive fmul fadd == l_distributive fmul fadd & r_distributive fmul fadd"
haftmann@27484: 
haftmann@27484: lemma max1: "!! a x y. (a::nat) <= x \<Longrightarrow> a <= max x y" by (arith)
haftmann@27484: lemma max2: "!! b x y. (b::nat) <= y \<Longrightarrow> b <= max x y" by (arith)
haftmann@27484: 
haftmann@27484: lemma r_distributive_matrix:
haftmann@27484:  assumes prems:
haftmann@27484:   "r_distributive fmul fadd"
haftmann@27484:   "associative fadd"
haftmann@27484:   "commutative fadd"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:  shows "r_distributive (mult_matrix fmul fadd) (combine_matrix fadd)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   from prems show ?concl
haftmann@27484:     apply (simp add: r_distributive_def mult_matrix_def, auto)
haftmann@27484:     proof -
haftmann@27484:       fix a::"'a matrix"
haftmann@27484:       fix u::"'b matrix"
haftmann@27484:       fix v::"'b matrix"
haftmann@27484:       let ?mx = "max (ncols a) (max (nrows u) (nrows v))"
haftmann@27484:       from prems show "mult_matrix_n (max (ncols a) (nrows (combine_matrix fadd u v))) fmul fadd a (combine_matrix fadd u v) =
haftmann@27484:         combine_matrix fadd (mult_matrix_n (max (ncols a) (nrows u)) fmul fadd a u) (mult_matrix_n (max (ncols a) (nrows v)) fmul fadd a v)"
haftmann@27484:         apply (subst mult_matrix_nm[of _ _ _ ?mx fadd fmul])
haftmann@27484:         apply (simp add: max1 max2 combine_nrows combine_ncols)+
haftmann@27484:         apply (subst mult_matrix_nm[of _ _ v ?mx fadd fmul])
haftmann@27484:         apply (simp add: max1 max2 combine_nrows combine_ncols)+
haftmann@27484:         apply (subst mult_matrix_nm[of _ _ u ?mx fadd fmul])
haftmann@27484:         apply (simp add: max1 max2 combine_nrows combine_ncols)+
haftmann@27484:         apply (simp add: mult_matrix_n_def r_distributive_def foldseq_distr[of fadd])
haftmann@27484:         apply (simp add: combine_matrix_def combine_infmatrix_def)
haftmann@27484:         apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
haftmann@27484:         apply (simplesubst RepAbs_matrix)
haftmann@27484:         apply (simp, auto)
haftmann@27484:         apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero)
haftmann@27484:         apply (rule exI[of _ "ncols v"], simp add: ncols_le foldseq_zero)
haftmann@27484:         apply (subst RepAbs_matrix)
haftmann@27484:         apply (simp, auto)
haftmann@27484:         apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero)
haftmann@27484:         apply (rule exI[of _ "ncols u"], simp add: ncols_le foldseq_zero)
haftmann@27484:         done
haftmann@27484:     qed
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma l_distributive_matrix:
haftmann@27484:  assumes prems:
haftmann@27484:   "l_distributive fmul fadd"
haftmann@27484:   "associative fadd"
haftmann@27484:   "commutative fadd"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:  shows "l_distributive (mult_matrix fmul fadd) (combine_matrix fadd)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   from prems show ?concl
haftmann@27484:     apply (simp add: l_distributive_def mult_matrix_def, auto)
haftmann@27484:     proof -
haftmann@27484:       fix a::"'b matrix"
haftmann@27484:       fix u::"'a matrix"
haftmann@27484:       fix v::"'a matrix"
haftmann@27484:       let ?mx = "max (nrows a) (max (ncols u) (ncols v))"
haftmann@27484:       from prems show "mult_matrix_n (max (ncols (combine_matrix fadd u v)) (nrows a)) fmul fadd (combine_matrix fadd u v) a =
haftmann@27484:                combine_matrix fadd (mult_matrix_n (max (ncols u) (nrows a)) fmul fadd u a) (mult_matrix_n (max (ncols v) (nrows a)) fmul fadd v a)"
haftmann@27484:         apply (subst mult_matrix_nm[of v _ _ ?mx fadd fmul])
haftmann@27484:         apply (simp add: max1 max2 combine_nrows combine_ncols)+
haftmann@27484:         apply (subst mult_matrix_nm[of u _ _ ?mx fadd fmul])
haftmann@27484:         apply (simp add: max1 max2 combine_nrows combine_ncols)+
haftmann@27484:         apply (subst mult_matrix_nm[of _ _ _ ?mx fadd fmul])
haftmann@27484:         apply (simp add: max1 max2 combine_nrows combine_ncols)+
haftmann@27484:         apply (simp add: mult_matrix_n_def l_distributive_def foldseq_distr[of fadd])
haftmann@27484:         apply (simp add: combine_matrix_def combine_infmatrix_def)
haftmann@27484:         apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
haftmann@27484:         apply (simplesubst RepAbs_matrix)
haftmann@27484:         apply (simp, auto)
haftmann@27484:         apply (rule exI[of _ "nrows v"], simp add: nrows_le foldseq_zero)
haftmann@27484:         apply (rule exI[of _ "ncols a"], simp add: ncols_le foldseq_zero)
haftmann@27484:         apply (subst RepAbs_matrix)
haftmann@27484:         apply (simp, auto)
haftmann@27484:         apply (rule exI[of _ "nrows u"], simp add: nrows_le foldseq_zero)
haftmann@27484:         apply (rule exI[of _ "ncols a"], simp add: ncols_le foldseq_zero)
haftmann@27484:         done
haftmann@27484:     qed
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: instantiation matrix :: (zero) zero
haftmann@27484: begin
haftmann@27484: 
haftmann@28562: definition zero_matrix_def [code del]: "0 = Abs_matrix (\<lambda>j i. 0)"
haftmann@27484: 
haftmann@27484: instance ..
haftmann@27484: 
haftmann@27484: end
haftmann@27484: 
haftmann@27484: lemma Rep_zero_matrix_def[simp]: "Rep_matrix 0 j i = 0"
haftmann@27484:   apply (simp add: zero_matrix_def)
haftmann@27484:   apply (subst RepAbs_matrix)
haftmann@27484:   by (auto)
haftmann@27484: 
haftmann@27484: lemma zero_matrix_def_nrows[simp]: "nrows 0 = 0"
haftmann@27484: proof -
haftmann@27484:   have a:"!! (x::nat). x <= 0 \<Longrightarrow> x = 0" by (arith)
haftmann@27484:   show "nrows 0 = 0" by (rule a, subst nrows_le, simp)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma zero_matrix_def_ncols[simp]: "ncols 0 = 0"
haftmann@27484: proof -
haftmann@27484:   have a:"!! (x::nat). x <= 0 \<Longrightarrow> x = 0" by (arith)
haftmann@27484:   show "ncols 0 = 0" by (rule a, subst ncols_le, simp)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma combine_matrix_zero_l_neutral: "zero_l_neutral f \<Longrightarrow> zero_l_neutral (combine_matrix f)"
haftmann@27484:   by (simp add: zero_l_neutral_def combine_matrix_def combine_infmatrix_def)
haftmann@27484: 
haftmann@27484: lemma combine_matrix_zero_r_neutral: "zero_r_neutral f \<Longrightarrow> zero_r_neutral (combine_matrix f)"
haftmann@27484:   by (simp add: zero_r_neutral_def combine_matrix_def combine_infmatrix_def)
haftmann@27484: 
haftmann@27484: lemma mult_matrix_zero_closed: "\<lbrakk>fadd 0 0 = 0; zero_closed fmul\<rbrakk> \<Longrightarrow> zero_closed (mult_matrix fmul fadd)"
haftmann@27484:   apply (simp add: zero_closed_def mult_matrix_def mult_matrix_n_def)
haftmann@27484:   apply (auto)
haftmann@27484:   by (subst foldseq_zero, (simp add: zero_matrix_def)+)+
haftmann@27484: 
haftmann@27484: lemma mult_matrix_n_zero_right[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul a 0 = 0\<rbrakk> \<Longrightarrow> mult_matrix_n n fmul fadd A 0 = 0"
haftmann@27484:   apply (simp add: mult_matrix_n_def)
haftmann@27484:   apply (subst foldseq_zero)
haftmann@27484:   by (simp_all add: zero_matrix_def)
haftmann@27484: 
haftmann@27484: lemma mult_matrix_n_zero_left[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul 0 a = 0\<rbrakk> \<Longrightarrow> mult_matrix_n n fmul fadd 0 A = 0"
haftmann@27484:   apply (simp add: mult_matrix_n_def)
haftmann@27484:   apply (subst foldseq_zero)
haftmann@27484:   by (simp_all add: zero_matrix_def)
haftmann@27484: 
haftmann@27484: lemma mult_matrix_zero_left[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul 0 a = 0\<rbrakk> \<Longrightarrow> mult_matrix fmul fadd 0 A = 0"
haftmann@27484: by (simp add: mult_matrix_def)
haftmann@27484: 
haftmann@27484: lemma mult_matrix_zero_right[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul a 0 = 0\<rbrakk> \<Longrightarrow> mult_matrix fmul fadd A 0 = 0"
haftmann@27484: by (simp add: mult_matrix_def)
haftmann@27484: 
haftmann@27484: lemma apply_matrix_zero[simp]: "f 0 = 0 \<Longrightarrow> apply_matrix f 0 = 0"
haftmann@27484:   apply (simp add: apply_matrix_def apply_infmatrix_def)
haftmann@27484:   by (simp add: zero_matrix_def)
haftmann@27484: 
haftmann@27484: lemma combine_matrix_zero: "f 0 0 = 0 \<Longrightarrow> combine_matrix f 0 0 = 0"
haftmann@27484:   apply (simp add: combine_matrix_def combine_infmatrix_def)
haftmann@27484:   by (simp add: zero_matrix_def)
haftmann@27484: 
haftmann@27484: lemma transpose_matrix_zero[simp]: "transpose_matrix 0 = 0"
haftmann@27484: apply (simp add: transpose_matrix_def transpose_infmatrix_def zero_matrix_def RepAbs_matrix)
haftmann@27484: apply (subst Rep_matrix_inject[symmetric], (rule ext)+)
haftmann@27484: apply (simp add: RepAbs_matrix)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma apply_zero_matrix_def[simp]: "apply_matrix (% x. 0) A = 0"
haftmann@27484:   apply (simp add: apply_matrix_def apply_infmatrix_def)
haftmann@27484:   by (simp add: zero_matrix_def)
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   singleton_matrix :: "nat \<Rightarrow> nat \<Rightarrow> ('a::zero) \<Rightarrow> 'a matrix"
haftmann@27484:   "singleton_matrix j i a == Abs_matrix(% m n. if j = m & i = n then a else 0)"
haftmann@27484:   move_matrix :: "('a::zero) matrix \<Rightarrow> int \<Rightarrow> int \<Rightarrow> 'a matrix"
haftmann@27484:   "move_matrix A y x == Abs_matrix(% j i. if (neg ((int j)-y)) | (neg ((int i)-x)) then 0 else Rep_matrix A (nat ((int j)-y)) (nat ((int i)-x)))"
haftmann@27484:   take_rows :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix"
haftmann@27484:   "take_rows A r == Abs_matrix(% j i. if (j < r) then (Rep_matrix A j i) else 0)"
haftmann@27484:   take_columns :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix"
haftmann@27484:   "take_columns A c == Abs_matrix(% j i. if (i < c) then (Rep_matrix A j i) else 0)"
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   column_of_matrix :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix"
haftmann@27484:   "column_of_matrix A n == take_columns (move_matrix A 0 (- int n)) 1"
haftmann@27484:   row_of_matrix :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix"
haftmann@27484:   "row_of_matrix A m == take_rows (move_matrix A (- int m) 0) 1"
haftmann@27484: 
haftmann@27484: lemma Rep_singleton_matrix[simp]: "Rep_matrix (singleton_matrix j i e) m n = (if j = m & i = n then e else 0)"
haftmann@27484: apply (simp add: singleton_matrix_def)
haftmann@27484: apply (auto)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply (rule exI[of _ "Suc m"], simp)
haftmann@27484: apply (rule exI[of _ "Suc n"], simp+)
haftmann@27484: by (subst RepAbs_matrix, rule exI[of _ "Suc j"], simp, rule exI[of _ "Suc i"], simp+)+
haftmann@27484: 
haftmann@27484: lemma apply_singleton_matrix[simp]: "f 0 = 0 \<Longrightarrow> apply_matrix f (singleton_matrix j i x) = (singleton_matrix j i (f x))"
haftmann@27484: apply (subst Rep_matrix_inject[symmetric])
haftmann@27484: apply (rule ext)+
haftmann@27484: apply (simp)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma singleton_matrix_zero[simp]: "singleton_matrix j i 0 = 0"
haftmann@27484:   by (simp add: singleton_matrix_def zero_matrix_def)
haftmann@27484: 
haftmann@27484: lemma nrows_singleton[simp]: "nrows(singleton_matrix j i e) = (if e = 0 then 0 else Suc j)"
haftmann@27484: proof-
haftmann@27484: have th: "\<not> (\<forall>m. m \<le> j)" "\<exists>n. \<not> n \<le> i" by arith+
haftmann@27484: from th show ?thesis 
haftmann@27484: apply (auto)
haftmann@27484: apply (rule le_anti_sym)
haftmann@27484: apply (subst nrows_le)
haftmann@27484: apply (simp add: singleton_matrix_def, auto)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply auto
haftmann@27484: apply (simp add: Suc_le_eq)
haftmann@27484: apply (rule not_leE)
haftmann@27484: apply (subst nrows_le)
haftmann@27484: by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma ncols_singleton[simp]: "ncols(singleton_matrix j i e) = (if e = 0 then 0 else Suc i)"
haftmann@27484: proof-
haftmann@27484: have th: "\<not> (\<forall>m. m \<le> j)" "\<exists>n. \<not> n \<le> i" by arith+
haftmann@27484: from th show ?thesis 
haftmann@27484: apply (auto)
haftmann@27484: apply (rule le_anti_sym)
haftmann@27484: apply (subst ncols_le)
haftmann@27484: apply (simp add: singleton_matrix_def, auto)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply auto
haftmann@27484: apply (simp add: Suc_le_eq)
haftmann@27484: apply (rule not_leE)
haftmann@27484: apply (subst ncols_le)
haftmann@27484: by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma combine_singleton: "f 0 0 = 0 \<Longrightarrow> combine_matrix f (singleton_matrix j i a) (singleton_matrix j i b) = singleton_matrix j i (f a b)"
haftmann@27484: apply (simp add: singleton_matrix_def combine_matrix_def combine_infmatrix_def)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply (rule exI[of _ "Suc j"], simp)
haftmann@27484: apply (rule exI[of _ "Suc i"], simp)
haftmann@27484: apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply (rule exI[of _ "Suc j"], simp)
haftmann@27484: apply (rule exI[of _ "Suc i"], simp)
haftmann@27484: by simp
haftmann@27484: 
haftmann@27484: lemma transpose_singleton[simp]: "transpose_matrix (singleton_matrix j i a) = singleton_matrix i j a"
haftmann@27484: apply (subst Rep_matrix_inject[symmetric], (rule ext)+)
haftmann@27484: apply (simp)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma Rep_move_matrix[simp]:
haftmann@27484:   "Rep_matrix (move_matrix A y x) j i =
haftmann@27484:   (if (neg ((int j)-y)) | (neg ((int i)-x)) then 0 else Rep_matrix A (nat((int j)-y)) (nat((int i)-x)))"
haftmann@27484: apply (simp add: move_matrix_def)
haftmann@27484: apply (auto)
haftmann@27484: by (subst RepAbs_matrix,
haftmann@27484:   rule exI[of _ "(nrows A)+(nat (abs y))"], auto, rule nrows, arith,
haftmann@27484:   rule exI[of _ "(ncols A)+(nat (abs x))"], auto, rule ncols, arith)+
haftmann@27484: 
haftmann@27484: lemma move_matrix_0_0[simp]: "move_matrix A 0 0 = A"
haftmann@27484: by (simp add: move_matrix_def)
haftmann@27484: 
haftmann@27484: lemma move_matrix_ortho: "move_matrix A j i = move_matrix (move_matrix A j 0) 0 i"
haftmann@27484: apply (subst Rep_matrix_inject[symmetric])
haftmann@27484: apply (rule ext)+
haftmann@27484: apply (simp)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma transpose_move_matrix[simp]:
haftmann@27484:   "transpose_matrix (move_matrix A x y) = move_matrix (transpose_matrix A) y x"
haftmann@27484: apply (subst Rep_matrix_inject[symmetric], (rule ext)+)
haftmann@27484: apply (simp)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma move_matrix_singleton[simp]: "move_matrix (singleton_matrix u v x) j i = 
haftmann@27484:   (if (j + int u < 0) | (i + int v < 0) then 0 else (singleton_matrix (nat (j + int u)) (nat (i + int v)) x))"
haftmann@27484:   apply (subst Rep_matrix_inject[symmetric])
haftmann@27484:   apply (rule ext)+
haftmann@27484:   apply (case_tac "j + int u < 0")
haftmann@27484:   apply (simp, arith)
haftmann@27484:   apply (case_tac "i + int v < 0")
haftmann@27484:   apply (simp add: neg_def, arith)
haftmann@27484:   apply (simp add: neg_def)
haftmann@27484:   apply arith
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma Rep_take_columns[simp]:
haftmann@27484:   "Rep_matrix (take_columns A c) j i =
haftmann@27484:   (if i < c then (Rep_matrix A j i) else 0)"
haftmann@27484: apply (simp add: take_columns_def)
haftmann@27484: apply (simplesubst RepAbs_matrix)
haftmann@27484: apply (rule exI[of _ "nrows A"], auto, simp add: nrows_le)
haftmann@27484: apply (rule exI[of _ "ncols A"], auto, simp add: ncols_le)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma Rep_take_rows[simp]:
haftmann@27484:   "Rep_matrix (take_rows A r) j i =
haftmann@27484:   (if j < r then (Rep_matrix A j i) else 0)"
haftmann@27484: apply (simp add: take_rows_def)
haftmann@27484: apply (simplesubst RepAbs_matrix)
haftmann@27484: apply (rule exI[of _ "nrows A"], auto, simp add: nrows_le)
haftmann@27484: apply (rule exI[of _ "ncols A"], auto, simp add: ncols_le)
haftmann@27484: done
haftmann@27484: 
haftmann@27484: lemma Rep_column_of_matrix[simp]:
haftmann@27484:   "Rep_matrix (column_of_matrix A c) j i = (if i = 0 then (Rep_matrix A j c) else 0)"
haftmann@27484:   by (simp add: column_of_matrix_def)
haftmann@27484: 
haftmann@27484: lemma Rep_row_of_matrix[simp]:
haftmann@27484:   "Rep_matrix (row_of_matrix A r) j i = (if j = 0 then (Rep_matrix A r i) else 0)"
haftmann@27484:   by (simp add: row_of_matrix_def)
haftmann@27484: 
haftmann@27484: lemma column_of_matrix: "ncols A <= n \<Longrightarrow> column_of_matrix A n = 0"
haftmann@27484: apply (subst Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: by (simp add: ncols)
haftmann@27484: 
haftmann@27484: lemma row_of_matrix: "nrows A <= n \<Longrightarrow> row_of_matrix A n = 0"
haftmann@27484: apply (subst Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: by (simp add: nrows)
haftmann@27484: 
haftmann@27484: lemma mult_matrix_singleton_right[simp]:
haftmann@27484:   assumes prems:
haftmann@27484:   "! x. fmul x 0 = 0"
haftmann@27484:   "! x. fmul 0 x = 0"
haftmann@27484:   "! x. fadd 0 x = x"
haftmann@27484:   "! x. fadd x 0 = x"
haftmann@27484:   shows "(mult_matrix fmul fadd A (singleton_matrix j i e)) = apply_matrix (% x. fmul x e) (move_matrix (column_of_matrix A j) 0 (int i))"
haftmann@27484:   apply (simp add: mult_matrix_def)
haftmann@27484:   apply (subst mult_matrix_nm[of _ _ _ "max (ncols A) (Suc j)"])
haftmann@27484:   apply (auto)
haftmann@27484:   apply (simp add: prems)+
haftmann@27484:   apply (simp add: mult_matrix_n_def apply_matrix_def apply_infmatrix_def)
haftmann@27484:   apply (rule comb[of "Abs_matrix" "Abs_matrix"], auto, (rule ext)+)
haftmann@27484:   apply (subst foldseq_almostzero[of _ j])
haftmann@27484:   apply (simp add: prems)+
haftmann@27484:   apply (auto)
nipkow@29700:   apply (metis comm_monoid_add.mult_1 le_anti_sym le_diff_eq not_neg_nat zero_le_imp_of_nat zle_int)
nipkow@29700:   done
haftmann@27484: 
haftmann@27484: lemma mult_matrix_ext:
haftmann@27484:   assumes
haftmann@27484:   eprem:
haftmann@27484:   "? e. (! a b. a \<noteq> b \<longrightarrow> fmul a e \<noteq> fmul b e)"
haftmann@27484:   and fprems:
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "! a. fadd a 0 = a"
haftmann@27484:   "! a. fadd 0 a = a"
haftmann@27484:   and contraprems:
haftmann@27484:   "mult_matrix fmul fadd A = mult_matrix fmul fadd B"
haftmann@27484:   shows
haftmann@27484:   "A = B"
haftmann@27484: proof(rule contrapos_np[of "False"], simp)
haftmann@27484:   assume a: "A \<noteq> B"
haftmann@27484:   have b: "!! f g. (! x y. f x y = g x y) \<Longrightarrow> f = g" by ((rule ext)+, auto)
haftmann@27484:   have "? j i. (Rep_matrix A j i) \<noteq> (Rep_matrix B j i)"
haftmann@27484:     apply (rule contrapos_np[of "False"], simp+)
haftmann@27484:     apply (insert b[of "Rep_matrix A" "Rep_matrix B"], simp)
haftmann@27484:     by (simp add: Rep_matrix_inject a)
haftmann@27484:   then obtain J I where c:"(Rep_matrix A J I) \<noteq> (Rep_matrix B J I)" by blast
haftmann@27484:   from eprem obtain e where eprops:"(! a b. a \<noteq> b \<longrightarrow> fmul a e \<noteq> fmul b e)" by blast
haftmann@27484:   let ?S = "singleton_matrix I 0 e"
haftmann@27484:   let ?comp = "mult_matrix fmul fadd"
haftmann@27484:   have d: "!!x f g. f = g \<Longrightarrow> f x = g x" by blast
haftmann@27484:   have e: "(% x. fmul x e) 0 = 0" by (simp add: prems)
haftmann@27484:   have "~(?comp A ?S = ?comp B ?S)"
haftmann@27484:     apply (rule notI)
haftmann@27484:     apply (simp add: fprems eprops)
haftmann@27484:     apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484:     apply (drule d[of _ _ "J"], drule d[of _ _ "0"])
haftmann@27484:     by (simp add: e c eprops)
haftmann@27484:   with contraprems show "False" by simp
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: constdefs
haftmann@27484:   foldmatrix :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a infmatrix) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a"
haftmann@27484:   "foldmatrix f g A m n == foldseq_transposed g (% j. foldseq f (A j) n) m"
haftmann@27484:   foldmatrix_transposed :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a infmatrix) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a"
haftmann@27484:   "foldmatrix_transposed f g A m n == foldseq g (% j. foldseq_transposed f (A j) n) m"
haftmann@27484: 
haftmann@27484: lemma foldmatrix_transpose:
haftmann@27484:   assumes
haftmann@27484:   "! a b c d. g(f a b) (f c d) = f (g a c) (g b d)"
haftmann@27484:   shows
haftmann@27484:   "foldmatrix f g A m n = foldmatrix_transposed g f (transpose_infmatrix A) n m" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have forall:"!! P x. (! x. P x) \<Longrightarrow> P x" by auto
haftmann@27484:   have tworows:"! A. foldmatrix f g A 1 n = foldmatrix_transposed g f (transpose_infmatrix A) n 1"
haftmann@27484:     apply (induct n)
haftmann@27484:     apply (simp add: foldmatrix_def foldmatrix_transposed_def prems)+
haftmann@27484:     apply (auto)
haftmann@27484:     by (drule_tac x="(% j i. A j (Suc i))" in forall, simp)
haftmann@27484:   show "foldmatrix f g A m n = foldmatrix_transposed g f (transpose_infmatrix A) n m"
haftmann@27484:     apply (simp add: foldmatrix_def foldmatrix_transposed_def)
haftmann@27484:     apply (induct m, simp)
haftmann@27484:     apply (simp)
haftmann@27484:     apply (insert tworows)
haftmann@27484:     apply (drule_tac x="% j i. (if j = 0 then (foldseq_transposed g (\<lambda>u. A u i) m) else (A (Suc m) i))" in spec)
haftmann@27484:     by (simp add: foldmatrix_def foldmatrix_transposed_def)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma foldseq_foldseq:
haftmann@27484: assumes
haftmann@27484:   "associative f"
haftmann@27484:   "associative g"
haftmann@27484:   "! a b c d. g(f a b) (f c d) = f (g a c) (g b d)"
haftmann@27484: shows
haftmann@27484:   "foldseq g (% j. foldseq f (A j) n) m = foldseq f (% j. foldseq g ((transpose_infmatrix A) j) m) n"
haftmann@27484:   apply (insert foldmatrix_transpose[of g f A m n])
haftmann@27484:   by (simp add: foldmatrix_def foldmatrix_transposed_def foldseq_assoc[THEN sym] prems)
haftmann@27484: 
haftmann@27484: lemma mult_n_nrows:
haftmann@27484: assumes
haftmann@27484: "! a. fmul 0 a = 0"
haftmann@27484: "! a. fmul a 0 = 0"
haftmann@27484: "fadd 0 0 = 0"
haftmann@27484: shows "nrows (mult_matrix_n n fmul fadd A B) \<le> nrows A"
haftmann@27484: apply (subst nrows_le)
haftmann@27484: apply (simp add: mult_matrix_n_def)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply (rule_tac x="nrows A" in exI)
haftmann@27484: apply (simp add: nrows prems foldseq_zero)
haftmann@27484: apply (rule_tac x="ncols B" in exI)
haftmann@27484: apply (simp add: ncols prems foldseq_zero)
haftmann@27484: by (simp add: nrows prems foldseq_zero)
haftmann@27484: 
haftmann@27484: lemma mult_n_ncols:
haftmann@27484: assumes
haftmann@27484: "! a. fmul 0 a = 0"
haftmann@27484: "! a. fmul a 0 = 0"
haftmann@27484: "fadd 0 0 = 0"
haftmann@27484: shows "ncols (mult_matrix_n n fmul fadd A B) \<le> ncols B"
haftmann@27484: apply (subst ncols_le)
haftmann@27484: apply (simp add: mult_matrix_n_def)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply (rule_tac x="nrows A" in exI)
haftmann@27484: apply (simp add: nrows prems foldseq_zero)
haftmann@27484: apply (rule_tac x="ncols B" in exI)
haftmann@27484: apply (simp add: ncols prems foldseq_zero)
haftmann@27484: by (simp add: ncols prems foldseq_zero)
haftmann@27484: 
haftmann@27484: lemma mult_nrows:
haftmann@27484: assumes
haftmann@27484: "! a. fmul 0 a = 0"
haftmann@27484: "! a. fmul a 0 = 0"
haftmann@27484: "fadd 0 0 = 0"
haftmann@27484: shows "nrows (mult_matrix fmul fadd A B) \<le> nrows A"
haftmann@27484: by (simp add: mult_matrix_def mult_n_nrows prems)
haftmann@27484: 
haftmann@27484: lemma mult_ncols:
haftmann@27484: assumes
haftmann@27484: "! a. fmul 0 a = 0"
haftmann@27484: "! a. fmul a 0 = 0"
haftmann@27484: "fadd 0 0 = 0"
haftmann@27484: shows "ncols (mult_matrix fmul fadd A B) \<le> ncols B"
haftmann@27484: by (simp add: mult_matrix_def mult_n_ncols prems)
haftmann@27484: 
haftmann@27484: lemma nrows_move_matrix_le: "nrows (move_matrix A j i) <= nat((int (nrows A)) + j)"
haftmann@27484:   apply (auto simp add: nrows_le)
haftmann@27484:   apply (rule nrows)
haftmann@27484:   apply (arith)
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma ncols_move_matrix_le: "ncols (move_matrix A j i) <= nat((int (ncols A)) + i)"
haftmann@27484:   apply (auto simp add: ncols_le)
haftmann@27484:   apply (rule ncols)
haftmann@27484:   apply (arith)
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma mult_matrix_assoc:
haftmann@27484:   assumes prems:
haftmann@27484:   "! a. fmul1 0 a = 0"
haftmann@27484:   "! a. fmul1 a 0 = 0"
haftmann@27484:   "! a. fmul2 0 a = 0"
haftmann@27484:   "! a. fmul2 a 0 = 0"
haftmann@27484:   "fadd1 0 0 = 0"
haftmann@27484:   "fadd2 0 0 = 0"
haftmann@27484:   "! a b c d. fadd2 (fadd1 a b) (fadd1 c d) = fadd1 (fadd2 a c) (fadd2 b d)"
haftmann@27484:   "associative fadd1"
haftmann@27484:   "associative fadd2"
haftmann@27484:   "! a b c. fmul2 (fmul1 a b) c = fmul1 a (fmul2 b c)"
haftmann@27484:   "! a b c. fmul2 (fadd1 a b) c = fadd1 (fmul2 a c) (fmul2 b c)"
haftmann@27484:   "! a b c. fmul1 c (fadd2 a b) = fadd2 (fmul1 c a) (fmul1 c b)"
haftmann@27484:   shows "mult_matrix fmul2 fadd2 (mult_matrix fmul1 fadd1 A B) C = mult_matrix fmul1 fadd1 A (mult_matrix fmul2 fadd2 B C)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have comb_left:  "!! A B x y. A = B \<Longrightarrow> (Rep_matrix (Abs_matrix A)) x y = (Rep_matrix(Abs_matrix B)) x y" by blast
haftmann@27484:   have fmul2fadd1fold: "!! x s n. fmul2 (foldseq fadd1 s n)  x = foldseq fadd1 (% k. fmul2 (s k) x) n"
haftmann@27484:     by (rule_tac g1 = "% y. fmul2 y x" in ssubst [OF foldseq_distr_unary], simp_all!)
haftmann@27484:   have fmul1fadd2fold: "!! x s n. fmul1 x (foldseq fadd2 s n) = foldseq fadd2 (% k. fmul1 x (s k)) n"
haftmann@27484:       by (rule_tac g1 = "% y. fmul1 x y" in ssubst [OF foldseq_distr_unary], simp_all!)
haftmann@27484:   let ?N = "max (ncols A) (max (ncols B) (max (nrows B) (nrows C)))"
haftmann@27484:   show ?concl
haftmann@27484:     apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484:     apply (rule ext)+
haftmann@27484:     apply (simp add: mult_matrix_def)
haftmann@27484:     apply (simplesubst mult_matrix_nm[of _ "max (ncols (mult_matrix_n (max (ncols A) (nrows B)) fmul1 fadd1 A B)) (nrows C)" _ "max (ncols B) (nrows C)"])
haftmann@27484:     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows prems)+
haftmann@27484:     apply (simplesubst mult_matrix_nm[of _ "max (ncols A) (nrows (mult_matrix_n (max (ncols B) (nrows C)) fmul2 fadd2 B C))" _ "max (ncols A) (nrows B)"])     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows prems)+
haftmann@27484:     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
haftmann@27484:     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows prems)+
haftmann@27484:     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
haftmann@27484:     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows prems)+
haftmann@27484:     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
haftmann@27484:     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows prems)+
haftmann@27484:     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
haftmann@27484:     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows prems)+
haftmann@27484:     apply (simp add: mult_matrix_n_def)
haftmann@27484:     apply (rule comb_left)
haftmann@27484:     apply ((rule ext)+, simp)
haftmann@27484:     apply (simplesubst RepAbs_matrix)
haftmann@27484:     apply (rule exI[of _ "nrows B"])
haftmann@27484:     apply (simp add: nrows prems foldseq_zero)
haftmann@27484:     apply (rule exI[of _ "ncols C"])
haftmann@27484:     apply (simp add: prems ncols foldseq_zero)
haftmann@27484:     apply (subst RepAbs_matrix)
haftmann@27484:     apply (rule exI[of _ "nrows A"])
haftmann@27484:     apply (simp add: nrows prems foldseq_zero)
haftmann@27484:     apply (rule exI[of _ "ncols B"])
haftmann@27484:     apply (simp add: prems ncols foldseq_zero)
haftmann@27484:     apply (simp add: fmul2fadd1fold fmul1fadd2fold prems)
haftmann@27484:     apply (subst foldseq_foldseq)
haftmann@27484:     apply (simp add: prems)+
haftmann@27484:     by (simp add: transpose_infmatrix)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma
haftmann@27484:   assumes prems:
haftmann@27484:   "! a. fmul1 0 a = 0"
haftmann@27484:   "! a. fmul1 a 0 = 0"
haftmann@27484:   "! a. fmul2 0 a = 0"
haftmann@27484:   "! a. fmul2 a 0 = 0"
haftmann@27484:   "fadd1 0 0 = 0"
haftmann@27484:   "fadd2 0 0 = 0"
haftmann@27484:   "! a b c d. fadd2 (fadd1 a b) (fadd1 c d) = fadd1 (fadd2 a c) (fadd2 b d)"
haftmann@27484:   "associative fadd1"
haftmann@27484:   "associative fadd2"
haftmann@27484:   "! a b c. fmul2 (fmul1 a b) c = fmul1 a (fmul2 b c)"
haftmann@27484:   "! a b c. fmul2 (fadd1 a b) c = fadd1 (fmul2 a c) (fmul2 b c)"
haftmann@27484:   "! a b c. fmul1 c (fadd2 a b) = fadd2 (fmul1 c a) (fmul1 c b)"
haftmann@27484:   shows
haftmann@27484:   "(mult_matrix fmul1 fadd1 A) o (mult_matrix fmul2 fadd2 B) = mult_matrix fmul2 fadd2 (mult_matrix fmul1 fadd1 A B)"
haftmann@27484: apply (rule ext)+
haftmann@27484: apply (simp add: comp_def )
haftmann@27484: by (simp add: mult_matrix_assoc prems)
haftmann@27484: 
haftmann@27484: lemma mult_matrix_assoc_simple:
haftmann@27484:   assumes prems:
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   "associative fadd"
haftmann@27484:   "commutative fadd"
haftmann@27484:   "associative fmul"
haftmann@27484:   "distributive fmul fadd"
haftmann@27484:   shows "mult_matrix fmul fadd (mult_matrix fmul fadd A B) C = mult_matrix fmul fadd A (mult_matrix fmul fadd B C)" (is ?concl)
haftmann@27484: proof -
haftmann@27484:   have "!! a b c d. fadd (fadd a b) (fadd c d) = fadd (fadd a c) (fadd b d)"
haftmann@27484:     by (simp! add: associative_def commutative_def)
haftmann@27484:   then show ?concl
haftmann@27484:     apply (subst mult_matrix_assoc)
haftmann@27484:     apply (simp_all!)
haftmann@27484:     by (simp add: associative_def distributive_def l_distributive_def r_distributive_def)+
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma transpose_apply_matrix: "f 0 = 0 \<Longrightarrow> transpose_matrix (apply_matrix f A) = apply_matrix f (transpose_matrix A)"
haftmann@27484: apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: by simp
haftmann@27484: 
haftmann@27484: lemma transpose_combine_matrix: "f 0 0 = 0 \<Longrightarrow> transpose_matrix (combine_matrix f A B) = combine_matrix f (transpose_matrix A) (transpose_matrix B)"
haftmann@27484: apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: by simp
haftmann@27484: 
haftmann@27484: lemma Rep_mult_matrix:
haftmann@27484:   assumes
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   shows
haftmann@27484:   "Rep_matrix(mult_matrix fmul fadd A B) j i =
haftmann@27484:   foldseq fadd (% k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) (max (ncols A) (nrows B))"
haftmann@27484: apply (simp add: mult_matrix_def mult_matrix_n_def)
haftmann@27484: apply (subst RepAbs_matrix)
haftmann@27484: apply (rule exI[of _ "nrows A"], simp! add: nrows foldseq_zero)
haftmann@27484: apply (rule exI[of _ "ncols B"], simp! add: ncols foldseq_zero)
haftmann@27484: by simp
haftmann@27484: 
haftmann@27484: lemma transpose_mult_matrix:
haftmann@27484:   assumes
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   "! x y. fmul y x = fmul x y"
haftmann@27484:   shows
haftmann@27484:   "transpose_matrix (mult_matrix fmul fadd A B) = mult_matrix fmul fadd (transpose_matrix B) (transpose_matrix A)"
haftmann@27484:   apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484:   apply (rule ext)+
haftmann@27484:   by (simp! add: Rep_mult_matrix max_ac)
haftmann@27484: 
haftmann@27484: lemma column_transpose_matrix: "column_of_matrix (transpose_matrix A) n = transpose_matrix (row_of_matrix A n)"
haftmann@27484: apply (simp add:  Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: by simp
haftmann@27484: 
haftmann@27484: lemma take_columns_transpose_matrix: "take_columns (transpose_matrix A) n = transpose_matrix (take_rows A n)"
haftmann@27484: apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: by simp
haftmann@27484: 
haftmann@27580: instantiation matrix :: ("{zero, ord}") ord
haftmann@27484: begin
haftmann@27484: 
haftmann@27484: definition
haftmann@27484:   le_matrix_def: "A \<le> B \<longleftrightarrow> (\<forall>j i. Rep_matrix A j i \<le> Rep_matrix B j i)"
haftmann@27484: 
haftmann@27484: definition
wenzelm@28637:   less_def: "A < (B\<Colon>'a matrix) \<longleftrightarrow> A \<le> B \<and> \<not> B \<le> A"
haftmann@27484: 
haftmann@27484: instance ..
haftmann@27484: 
haftmann@27484: end
haftmann@27484: 
haftmann@27580: instance matrix :: ("{zero, order}") order
haftmann@27484: apply intro_classes
haftmann@27484: apply (simp_all add: le_matrix_def less_def)
haftmann@27484: apply (auto)
haftmann@27484: apply (drule_tac x=j in spec, drule_tac x=j in spec)
haftmann@27484: apply (drule_tac x=i in spec, drule_tac x=i in spec)
haftmann@27484: apply (simp)
haftmann@27484: apply (simp add: Rep_matrix_inject[THEN sym])
haftmann@27484: apply (rule ext)+
haftmann@27484: apply (drule_tac x=xa in spec, drule_tac x=xa in spec)
haftmann@27484: apply (drule_tac x=xb in spec, drule_tac x=xb in spec)
wenzelm@28637: apply simp
wenzelm@28637: done
haftmann@27484: 
haftmann@27484: lemma le_apply_matrix:
haftmann@27484:   assumes
haftmann@27484:   "f 0 = 0"
haftmann@27484:   "! x y. x <= y \<longrightarrow> f x <= f y"
haftmann@27484:   "(a::('a::{ord, zero}) matrix) <= b"
haftmann@27484:   shows
haftmann@27484:   "apply_matrix f a <= apply_matrix f b"
haftmann@27484:   by (simp! add: le_matrix_def)
haftmann@27484: 
haftmann@27484: lemma le_combine_matrix:
haftmann@27484:   assumes
haftmann@27484:   "f 0 0 = 0"
haftmann@27484:   "! a b c d. a <= b & c <= d \<longrightarrow> f a c <= f b d"
haftmann@27484:   "A <= B"
haftmann@27484:   "C <= D"
haftmann@27484:   shows
haftmann@27484:   "combine_matrix f A C <= combine_matrix f B D"
haftmann@27484: by (simp! add: le_matrix_def)
haftmann@27484: 
haftmann@27484: lemma le_left_combine_matrix:
haftmann@27484:   assumes
haftmann@27484:   "f 0 0 = 0"
haftmann@27484:   "! a b c. a <= b \<longrightarrow> f c a <= f c b"
haftmann@27484:   "A <= B"
haftmann@27484:   shows
haftmann@27484:   "combine_matrix f C A <= combine_matrix f C B"
haftmann@27484:   by (simp! add: le_matrix_def)
haftmann@27484: 
haftmann@27484: lemma le_right_combine_matrix:
haftmann@27484:   assumes
haftmann@27484:   "f 0 0 = 0"
haftmann@27484:   "! a b c. a <= b \<longrightarrow> f a c <= f b c"
haftmann@27484:   "A <= B"
haftmann@27484:   shows
haftmann@27484:   "combine_matrix f A C <= combine_matrix f B C"
haftmann@27484:   by (simp! add: le_matrix_def)
haftmann@27484: 
haftmann@27484: lemma le_transpose_matrix: "(A <= B) = (transpose_matrix A <= transpose_matrix B)"
haftmann@27484:   by (simp add: le_matrix_def, auto)
haftmann@27484: 
haftmann@27484: lemma le_foldseq:
haftmann@27484:   assumes
haftmann@27484:   "! a b c d . a <= b & c <= d \<longrightarrow> f a c <= f b d"
haftmann@27484:   "! i. i <= n \<longrightarrow> s i <= t i"
haftmann@27484:   shows
haftmann@27484:   "foldseq f s n <= foldseq f t n"
haftmann@27484: proof -
haftmann@27484:   have "! s t. (! i. i<=n \<longrightarrow> s i <= t i) \<longrightarrow> foldseq f s n <= foldseq f t n" by (induct_tac n, simp_all!)
haftmann@27484:   then show "foldseq f s n <= foldseq f t n" by (simp!)
haftmann@27484: qed
haftmann@27484: 
haftmann@27484: lemma le_left_mult:
haftmann@27484:   assumes
haftmann@27484:   "! a b c d. a <= b & c <= d \<longrightarrow> fadd a c <= fadd b d"
haftmann@27484:   "! c a b.   0 <= c & a <= b \<longrightarrow> fmul c a <= fmul c b"
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   "0 <= C"
haftmann@27484:   "A <= B"
haftmann@27484:   shows
haftmann@27484:   "mult_matrix fmul fadd C A <= mult_matrix fmul fadd C B"
haftmann@27484:   apply (simp! add: le_matrix_def Rep_mult_matrix)
haftmann@27484:   apply (auto)
haftmann@27484:   apply (simplesubst foldseq_zerotail[of _ _ _ "max (ncols C) (max (nrows A) (nrows B))"], simp_all add: nrows ncols max1 max2)+
haftmann@27484:   apply (rule le_foldseq)
haftmann@27484:   by (auto)
haftmann@27484: 
haftmann@27484: lemma le_right_mult:
haftmann@27484:   assumes
haftmann@27484:   "! a b c d. a <= b & c <= d \<longrightarrow> fadd a c <= fadd b d"
haftmann@27484:   "! c a b. 0 <= c & a <= b \<longrightarrow> fmul a c <= fmul b c"
haftmann@27484:   "! a. fmul 0 a = 0"
haftmann@27484:   "! a. fmul a 0 = 0"
haftmann@27484:   "fadd 0 0 = 0"
haftmann@27484:   "0 <= C"
haftmann@27484:   "A <= B"
haftmann@27484:   shows
haftmann@27484:   "mult_matrix fmul fadd A C <= mult_matrix fmul fadd B C"
haftmann@27484:   apply (simp! add: le_matrix_def Rep_mult_matrix)
haftmann@27484:   apply (auto)
haftmann@27484:   apply (simplesubst foldseq_zerotail[of _ _ _ "max (nrows C) (max (ncols A) (ncols B))"], simp_all add: nrows ncols max1 max2)+
haftmann@27484:   apply (rule le_foldseq)
haftmann@27484:   by (auto)
haftmann@27484: 
haftmann@27484: lemma spec2: "! j i. P j i \<Longrightarrow> P j i" by blast
haftmann@27484: lemma neg_imp: "(\<not> Q \<longrightarrow> \<not> P) \<Longrightarrow> P \<longrightarrow> Q" by blast
haftmann@27484: 
haftmann@27484: lemma singleton_matrix_le[simp]: "(singleton_matrix j i a <= singleton_matrix j i b) = (a <= (b::_::order))"
haftmann@27484:   by (auto simp add: le_matrix_def)
haftmann@27484: 
haftmann@27484: lemma singleton_le_zero[simp]: "(singleton_matrix j i x <= 0) = (x <= (0::'a::{order,zero}))"
haftmann@27484:   apply (auto)
haftmann@27484:   apply (simp add: le_matrix_def)
haftmann@27484:   apply (drule_tac j=j and i=i in spec2)
haftmann@27484:   apply (simp)
haftmann@27484:   apply (simp add: le_matrix_def)
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma singleton_ge_zero[simp]: "(0 <= singleton_matrix j i x) = ((0::'a::{order,zero}) <= x)"
haftmann@27484:   apply (auto)
haftmann@27484:   apply (simp add: le_matrix_def)
haftmann@27484:   apply (drule_tac j=j and i=i in spec2)
haftmann@27484:   apply (simp)
haftmann@27484:   apply (simp add: le_matrix_def)
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma move_matrix_le_zero[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (move_matrix A j i <= 0) = (A <= (0::('a::{order,zero}) matrix))"
haftmann@27484:   apply (auto simp add: le_matrix_def neg_def)
haftmann@27484:   apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
haftmann@27484:   apply (auto)
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma move_matrix_zero_le[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (0 <= move_matrix A j i) = ((0::('a::{order,zero}) matrix) <= A)"
haftmann@27484:   apply (auto simp add: le_matrix_def neg_def)
haftmann@27484:   apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
haftmann@27484:   apply (auto)
haftmann@27484:   done
haftmann@27484: 
haftmann@27484: lemma move_matrix_le_move_matrix_iff[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (move_matrix A j i <= move_matrix B j i) = (A <= (B::('a::{order,zero}) matrix))"
haftmann@27484:   apply (auto simp add: le_matrix_def neg_def)
haftmann@27484:   apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
haftmann@27484:   apply (auto)
haftmann@27484:   done  
haftmann@27484: 
haftmann@27580: instantiation matrix :: ("{lattice, zero}") lattice
haftmann@25764: begin
haftmann@25764: 
haftmann@28562: definition [code del]: "inf = combine_matrix inf"
haftmann@25764: 
haftmann@28562: definition [code del]: "sup = combine_matrix sup"
haftmann@25764: 
haftmann@25764: instance
haftmann@22452:   by default (auto simp add: inf_le1 inf_le2 le_infI le_matrix_def inf_matrix_def sup_matrix_def)
haftmann@22452: 
haftmann@25764: end
haftmann@25764: 
haftmann@25764: instantiation matrix :: ("{plus, zero}") plus
haftmann@25764: begin
haftmann@25764: 
haftmann@25764: definition
haftmann@28562:   plus_matrix_def [code del]: "A + B = combine_matrix (op +) A B"
haftmann@25764: 
haftmann@25764: instance ..
haftmann@25764: 
haftmann@25764: end
haftmann@25764: 
haftmann@25764: instantiation matrix :: ("{uminus, zero}") uminus
haftmann@25764: begin
haftmann@25764: 
haftmann@25764: definition
haftmann@28562:   minus_matrix_def [code del]: "- A = apply_matrix uminus A"
haftmann@25764: 
haftmann@25764: instance ..
haftmann@25764: 
haftmann@25764: end
haftmann@25764: 
haftmann@25764: instantiation matrix :: ("{minus, zero}") minus
haftmann@25764: begin
obua@14593: 
haftmann@25764: definition
haftmann@28562:   diff_matrix_def [code del]: "A - B = combine_matrix (op -) A B"
haftmann@25764: 
haftmann@25764: instance ..
haftmann@25764: 
haftmann@25764: end
haftmann@25764: 
haftmann@25764: instantiation matrix :: ("{plus, times, zero}") times
haftmann@25764: begin
haftmann@25764: 
haftmann@25764: definition
haftmann@28562:   times_matrix_def [code del]: "A * B = mult_matrix (op *) (op +) A B"
obua@14940: 
haftmann@25764: instance ..
haftmann@25764: 
haftmann@25764: end
haftmann@25764: 
haftmann@27653: instantiation matrix :: ("{lattice, uminus, zero}") abs
haftmann@25764: begin
obua@14940: 
haftmann@25764: definition
haftmann@28562:   abs_matrix_def [code del]: "abs (A \<Colon> 'a matrix) = sup A (- A)"
haftmann@25764: 
haftmann@25764: instance ..
haftmann@25764: 
haftmann@25764: end
haftmann@23879: 
haftmann@27653: instance matrix :: (monoid_add) monoid_add
haftmann@27653: proof
haftmann@27653:   fix A B C :: "'a matrix"
obua@14940:   show "A + B + C = A + (B + C)"    
obua@14940:     apply (simp add: plus_matrix_def)
obua@14940:     apply (rule combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec])
obua@14940:     apply (simp_all add: add_assoc)
obua@14940:     done
haftmann@27653:   show "0 + A = A"
haftmann@27653:     apply (simp add: plus_matrix_def)
haftmann@27653:     apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec])
haftmann@27653:     apply (simp)
haftmann@27653:     done
haftmann@27653:   show "A + 0 = A"
haftmann@27653:     apply (simp add: plus_matrix_def)
haftmann@27653:     apply (rule combine_matrix_zero_r_neutral [simplified zero_r_neutral_def, THEN spec])
haftmann@27653:     apply (simp)
haftmann@27653:     done
haftmann@27653: qed
haftmann@27653: 
haftmann@27653: instance matrix :: (comm_monoid_add) comm_monoid_add
haftmann@27653: proof
haftmann@27653:   fix A B :: "'a matrix"
obua@14940:   show "A + B = B + A"
obua@14940:     apply (simp add: plus_matrix_def)
obua@14940:     apply (rule combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec])
obua@14940:     apply (simp_all add: add_commute)
obua@14940:     done
obua@14940:   show "0 + A = A"
obua@14940:     apply (simp add: plus_matrix_def)
obua@14940:     apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec])
obua@14940:     apply (simp)
obua@14940:     done
haftmann@27653: qed
haftmann@27653: 
haftmann@27653: instance matrix :: (group_add) group_add
haftmann@27653: proof
haftmann@27653:   fix A B :: "'a matrix"
haftmann@27653:   show "- A + A = 0" 
haftmann@27653:     by (simp add: plus_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
haftmann@27653:   show "A - B = A + - B"
haftmann@27653:     by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def Rep_matrix_inject [symmetric] ext diff_minus)
haftmann@27653: qed
haftmann@27653: 
haftmann@27653: instance matrix :: (ab_group_add) ab_group_add
haftmann@27653: proof
haftmann@27653:   fix A B :: "'a matrix"
obua@14940:   show "- A + A = 0" 
obua@14940:     by (simp add: plus_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
obua@14940:   show "A - B = A + - B" 
obua@14940:     by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
haftmann@27653: qed
haftmann@27653: 
haftmann@27653: instance matrix :: (pordered_ab_group_add) pordered_ab_group_add
haftmann@27653: proof
haftmann@27653:   fix A B C :: "'a matrix"
obua@14940:   assume "A <= B"
obua@14940:   then show "C + A <= C + B"
obua@14940:     apply (simp add: plus_matrix_def)
obua@14940:     apply (rule le_left_combine_matrix)
obua@14940:     apply (simp_all)
obua@14940:     done
obua@14940: qed
haftmann@27653:   
haftmann@27653: instance matrix :: (lordered_ab_group_add) lordered_ab_group_add_meet ..
haftmann@27653: instance matrix :: (lordered_ab_group_add) lordered_ab_group_add_join ..
obua@14593: 
haftmann@27653: instance matrix :: (ring) ring
obua@14940: proof
haftmann@27653:   fix A B C :: "'a matrix"
obua@14940:   show "A * B * C = A * (B * C)"
obua@14940:     apply (simp add: times_matrix_def)
obua@14940:     apply (rule mult_matrix_assoc)
nipkow@29667:     apply (simp_all add: associative_def algebra_simps)
obua@14940:     done
obua@14940:   show "(A + B) * C = A * C + B * C"
obua@14940:     apply (simp add: times_matrix_def plus_matrix_def)
obua@14940:     apply (rule l_distributive_matrix[simplified l_distributive_def, THEN spec, THEN spec, THEN spec])
nipkow@29667:     apply (simp_all add: associative_def commutative_def algebra_simps)
obua@14940:     done
obua@14940:   show "A * (B + C) = A * B + A * C"
obua@14940:     apply (simp add: times_matrix_def plus_matrix_def)
obua@14940:     apply (rule r_distributive_matrix[simplified r_distributive_def, THEN spec, THEN spec, THEN spec])
nipkow@29667:     apply (simp_all add: associative_def commutative_def algebra_simps)
haftmann@27653:     done
haftmann@27653: qed  
haftmann@27653: 
haftmann@27653: instance matrix :: (pordered_ring) pordered_ring
haftmann@27653: proof
haftmann@27653:   fix A B C :: "'a matrix"
obua@14940:   assume a: "A \<le> B"
obua@14940:   assume b: "0 \<le> C"
obua@14940:   from a b show "C * A \<le> C * B"
obua@14940:     apply (simp add: times_matrix_def)
obua@14940:     apply (rule le_left_mult)
obua@14940:     apply (simp_all add: add_mono mult_left_mono)
obua@14940:     done
obua@14940:   from a b show "A * C \<le> B * C"
obua@14940:     apply (simp add: times_matrix_def)
obua@14940:     apply (rule le_right_mult)
obua@14940:     apply (simp_all add: add_mono mult_right_mono)
obua@14940:     done
haftmann@27653: qed
haftmann@27653: 
haftmann@27653: instance matrix :: (lordered_ring) lordered_ring
haftmann@27653: proof
haftmann@27653:   fix A B C :: "('a :: lordered_ring) matrix"
haftmann@27653:   show "abs A = sup A (-A)" 
haftmann@27653:     by (simp add: abs_matrix_def)
haftmann@27653: qed
obua@14593: 
haftmann@25303: lemma Rep_matrix_add[simp]:
haftmann@27653:   "Rep_matrix ((a::('a::monoid_add)matrix)+b) j i  = (Rep_matrix a j i) + (Rep_matrix b j i)"
haftmann@27653:   by (simp add: plus_matrix_def)
obua@14593: 
haftmann@27653: lemma Rep_matrix_mult: "Rep_matrix ((a::('a::ring) matrix) * b) j i = 
obua@14940:   foldseq (op +) (% k.  (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))"
obua@14940: apply (simp add: times_matrix_def)
obua@14940: apply (simp add: Rep_mult_matrix)
obua@14940: done
obua@14593: 
haftmann@27653: lemma apply_matrix_add: "! x y. f (x+y) = (f x) + (f y) \<Longrightarrow> f 0 = (0::'a)
haftmann@27653:   \<Longrightarrow> apply_matrix f ((a::('a::monoid_add) matrix) + b) = (apply_matrix f a) + (apply_matrix f b)"
obua@14940: apply (subst Rep_matrix_inject[symmetric])
obua@14593: apply (rule ext)+
obua@14940: apply (simp)
obua@14940: done
obua@14593: 
haftmann@27653: lemma singleton_matrix_add: "singleton_matrix j i ((a::_::monoid_add)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)"
obua@14940: apply (subst Rep_matrix_inject[symmetric])
obua@14940: apply (rule ext)+
obua@14940: apply (simp)
obua@14940: done
obua@14593: 
haftmann@27653: lemma nrows_mult: "nrows ((A::('a::ring) matrix) * B) <= nrows A"
obua@14593: by (simp add: times_matrix_def mult_nrows)
obua@14593: 
haftmann@27653: lemma ncols_mult: "ncols ((A::('a::ring) matrix) * B) <= ncols B"
obua@14593: by (simp add: times_matrix_def mult_ncols)
obua@14593: 
haftmann@22422: definition
haftmann@22422:   one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix" where
haftmann@22422:   "one_matrix n = Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
obua@14593: 
obua@14593: lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
obua@14593: apply (simp add: one_matrix_def)
paulson@15481: apply (simplesubst RepAbs_matrix)
obua@14593: apply (rule exI[of _ n], simp add: split_if)+
nipkow@16733: by (simp add: split_if)
obua@14593: 
wenzelm@20633: lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
obua@14593: proof -
obua@14593:   have "?r <= n" by (simp add: nrows_le)
obua@14940:   moreover have "n <= ?r" by (simp add:le_nrows, arith)
obua@14593:   ultimately show "?r = n" by simp
obua@14593: qed
obua@14593: 
wenzelm@20633: lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
obua@14593: proof -
obua@14593:   have "?r <= n" by (simp add: ncols_le)
obua@14593:   moreover have "n <= ?r" by (simp add: le_ncols, arith)
obua@14593:   ultimately show "?r = n" by simp
obua@14593: qed
obua@14593: 
haftmann@27653: lemma one_matrix_mult_right[simp]: "ncols A <= n \<Longrightarrow> (A::('a::{ring_1}) matrix) * (one_matrix n) = A"
obua@14593: apply (subst Rep_matrix_inject[THEN sym])
obua@14593: apply (rule ext)+
obua@14593: apply (simp add: times_matrix_def Rep_mult_matrix)
obua@14593: apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
obua@14593: apply (simp_all)
obua@14593: by (simp add: max_def ncols)
obua@14593: 
haftmann@27653: lemma one_matrix_mult_left[simp]: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = (A::('a::ring_1) matrix)"
obua@14593: apply (subst Rep_matrix_inject[THEN sym])
obua@14593: apply (rule ext)+
obua@14593: apply (simp add: times_matrix_def Rep_mult_matrix)
obua@14593: apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
obua@14593: apply (simp_all)
obua@14593: by (simp add: max_def nrows)
obua@14593: 
haftmann@27653: lemma transpose_matrix_mult: "transpose_matrix ((A::('a::comm_ring) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)"
obua@14940: apply (simp add: times_matrix_def)
obua@14940: apply (subst transpose_mult_matrix)
obua@14940: apply (simp_all add: mult_commute)
obua@14940: done
obua@14940: 
haftmann@27653: lemma transpose_matrix_add: "transpose_matrix ((A::('a::monoid_add) matrix)+B) = transpose_matrix A + transpose_matrix B"
obua@14940: by (simp add: plus_matrix_def transpose_combine_matrix)
obua@14940: 
haftmann@27653: lemma transpose_matrix_diff: "transpose_matrix ((A::('a::group_add) matrix)-B) = transpose_matrix A - transpose_matrix B"
obua@14940: by (simp add: diff_matrix_def transpose_combine_matrix)
obua@14940: 
haftmann@27653: lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::group_add) matrix)) = - transpose_matrix (A::'a matrix)"
obua@14940: by (simp add: minus_matrix_def transpose_apply_matrix)
obua@14940: 
obua@14940: constdefs 
haftmann@27653:   right_inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940:   "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" 
haftmann@27653:   left_inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940:   "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" 
haftmann@27653:   inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@14940:   "inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)"
obua@14593: 
obua@14593: lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
obua@14593: apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
obua@14593: by (simp add: right_inverse_matrix_def)
obua@14593: 
obua@14940: lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y"
obua@14940: apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) 
obua@14940: by (simp add: left_inverse_matrix_def)
obua@14940: 
obua@14940: lemma left_right_inverse_matrix_unique: 
obua@14940:   assumes "left_inverse_matrix A Y" "right_inverse_matrix A X"
obua@14940:   shows "X = Y"
obua@14940: proof -
obua@14940:   have "Y = Y * one_matrix (nrows A)" 
obua@14940:     apply (subst one_matrix_mult_right)
obua@14940:     apply (insert prems)
obua@14940:     by (simp_all add: left_inverse_matrix_def)
obua@14940:   also have "\<dots> = Y * (A * X)" 
obua@14940:     apply (insert prems)
obua@14940:     apply (frule right_inverse_matrix_dim)
obua@14940:     by (simp add: right_inverse_matrix_def)
obua@14940:   also have "\<dots> = (Y * A) * X" by (simp add: mult_assoc)
obua@14940:   also have "\<dots> = X" 
obua@14940:     apply (insert prems)
obua@14940:     apply (frule left_inverse_matrix_dim)
obua@14940:     apply (simp_all add:  left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left)
obua@14940:     done
obua@14940:   ultimately show "X = Y" by (simp)
obua@14940: qed
obua@14940: 
obua@14940: lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y"
obua@14940:   by (auto simp add: inverse_matrix_def left_right_inverse_matrix_unique)
obua@14940: 
obua@14940: lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)"
obua@14940:   by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def)
obua@14940: 
obua@14940: lemma zero_imp_mult_zero: "(a::'a::ring) = 0 | b = 0 \<Longrightarrow> a * b = 0"
obua@14940: by auto
obua@14940: 
obua@14940: lemma Rep_matrix_zero_imp_mult_zero:
obua@14940:   "! j i k. (Rep_matrix A j k = 0) | (Rep_matrix B k i) = 0  \<Longrightarrow> A * B = (0::('a::lordered_ring) matrix)"
obua@14940: apply (subst Rep_matrix_inject[symmetric])
obua@14940: apply (rule ext)+
obua@14940: apply (auto simp add: Rep_matrix_mult foldseq_zero zero_imp_mult_zero)
obua@14940: done
obua@14940: 
haftmann@27653: lemma add_nrows: "nrows (A::('a::monoid_add) matrix) <= u \<Longrightarrow> nrows B <= u \<Longrightarrow> nrows (A + B) <= u"
obua@14940: apply (simp add: plus_matrix_def)
obua@14940: apply (rule combine_nrows)
obua@14940: apply (simp_all)
obua@14940: done
obua@14940: 
haftmann@27653: lemma move_matrix_row_mult: "move_matrix ((A::('a::ring) matrix) * B) j 0 = (move_matrix A j 0) * B"
obua@14940: apply (subst Rep_matrix_inject[symmetric])
obua@14940: apply (rule ext)+
obua@14940: apply (auto simp add: Rep_matrix_mult foldseq_zero)
obua@14940: apply (rule_tac foldseq_zerotail[symmetric])
obua@14940: apply (auto simp add: nrows zero_imp_mult_zero max2)
obua@14940: apply (rule order_trans)
obua@14940: apply (rule ncols_move_matrix_le)
obua@14940: apply (simp add: max1)
obua@14940: done
obua@14940: 
haftmann@27653: lemma move_matrix_col_mult: "move_matrix ((A::('a::ring) matrix) * B) 0 i = A * (move_matrix B 0 i)"
obua@14940: apply (subst Rep_matrix_inject[symmetric])
obua@14940: apply (rule ext)+
obua@14940: apply (auto simp add: Rep_matrix_mult foldseq_zero)
obua@14940: apply (rule_tac foldseq_zerotail[symmetric])
obua@14940: apply (auto simp add: ncols zero_imp_mult_zero max1)
obua@14940: apply (rule order_trans)
obua@14940: apply (rule nrows_move_matrix_le)
obua@14940: apply (simp add: max2)
obua@14940: done
obua@14940: 
haftmann@27653: lemma move_matrix_add: "((move_matrix (A + B) j i)::(('a::monoid_add) matrix)) = (move_matrix A j i) + (move_matrix B j i)" 
obua@14940: apply (subst Rep_matrix_inject[symmetric])
obua@14940: apply (rule ext)+
obua@14940: apply (simp)
obua@14940: done
obua@14940: 
haftmann@27653: lemma move_matrix_mult: "move_matrix ((A::('a::ring) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)"
obua@14940: by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult)
obua@14940: 
obua@14940: constdefs
haftmann@27653:   scalar_mult :: "('a::ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix"
obua@14940:   "scalar_mult a m == apply_matrix (op * a) m"
obua@14940: 
obua@14940: lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" 
nipkow@23477: by (simp add: scalar_mult_def)
obua@14940: 
obua@14940: lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)"
nipkow@29667: by (simp add: scalar_mult_def apply_matrix_add algebra_simps)
obua@14940: 
obua@14940: lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" 
nipkow@23477: by (simp add: scalar_mult_def)
obua@14940: 
obua@14940: lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)"
nipkow@23477: apply (subst Rep_matrix_inject[symmetric])
nipkow@23477: apply (rule ext)+
nipkow@23477: apply (auto)
nipkow@23477: done
obua@14940: 
haftmann@27653: lemma Rep_minus[simp]: "Rep_matrix (-(A::_::group_add)) x y = - (Rep_matrix A x y)"
nipkow@23477: by (simp add: minus_matrix_def)
obua@14940: 
haftmann@27653: lemma Rep_abs[simp]: "Rep_matrix (abs (A::_::lordered_ab_group_add)) x y = abs (Rep_matrix A x y)"
nipkow@23477: by (simp add: abs_lattice sup_matrix_def)
obua@14940: 
obua@14593: end




