diff -r 1810b1ade437 -r 47ee18b6ae32 src/HOL/Matrix/Matrix.thy --- a/src/HOL/Matrix/Matrix.thy Mon Mar 01 12:30:55 2010 +0100 +++ b/src/HOL/Matrix/Matrix.thy Mon Mar 01 13:42:31 2010 +0100 @@ -24,10 +24,10 @@ apply (rule Abs_matrix_induct) by (simp add: Abs_matrix_inverse matrix_def) -constdefs - nrows :: "('a::zero) matrix \ nat" +definition nrows :: "('a::zero) matrix \ nat" where "nrows A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image fst) (nonzero_positions (Rep_matrix A))))" - ncols :: "('a::zero) matrix \ nat" + +definition ncols :: "('a::zero) matrix \ nat" where "ncols A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image snd) (nonzero_positions (Rep_matrix A))))" lemma nrows: @@ -50,10 +50,10 @@ thus "Rep_matrix A m n = 0" by (simp add: nonzero_positions_def image_Collect) qed -constdefs - transpose_infmatrix :: "'a infmatrix \ 'a infmatrix" +definition transpose_infmatrix :: "'a infmatrix \ 'a infmatrix" where "transpose_infmatrix A j i == A i j" - transpose_matrix :: "('a::zero) matrix \ 'a matrix" + +definition transpose_matrix :: "('a::zero) matrix \ 'a matrix" where "transpose_matrix == Abs_matrix o transpose_infmatrix o Rep_matrix" declare transpose_infmatrix_def[simp] @@ -256,14 +256,16 @@ ultimately show "finite ?u" by (rule finite_subset) qed -constdefs - apply_infmatrix :: "('a \ 'b) \ 'a infmatrix \ 'b infmatrix" +definition apply_infmatrix :: "('a \ 'b) \ 'a infmatrix \ 'b infmatrix" where "apply_infmatrix f == % A. (% j i. f (A j i))" - apply_matrix :: "('a \ 'b) \ ('a::zero) matrix \ ('b::zero) matrix" + +definition apply_matrix :: "('a \ 'b) \ ('a::zero) matrix \ ('b::zero) matrix" where "apply_matrix f == % A. Abs_matrix (apply_infmatrix f (Rep_matrix A))" - combine_infmatrix :: "('a \ 'b \ 'c) \ 'a infmatrix \ 'b infmatrix \ 'c infmatrix" + +definition combine_infmatrix :: "('a \ 'b \ 'c) \ 'a infmatrix \ 'b infmatrix \ 'c infmatrix" where "combine_infmatrix f == % A B. (% j i. f (A j i) (B j i))" - combine_matrix :: "('a \ 'b \ 'c) \ ('a::zero) matrix \ ('b::zero) matrix \ ('c::zero) matrix" + +definition combine_matrix :: "('a \ 'b \ 'c) \ ('a::zero) matrix \ ('b::zero) matrix \ ('c::zero) matrix" where "combine_matrix f == % A B. Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))" lemma expand_apply_infmatrix[simp]: "apply_infmatrix f A j i = f (A j i)" @@ -272,10 +274,10 @@ lemma expand_combine_infmatrix[simp]: "combine_infmatrix f A B j i = f (A j i) (B j i)" by (simp add: combine_infmatrix_def) -constdefs -commutative :: "('a \ 'a \ 'b) \ bool" +definition commutative :: "('a \ 'a \ 'b) \ bool" where "commutative f == ! x y. f x y = f y x" -associative :: "('a \ 'a \ 'a) \ bool" + +definition associative :: "('a \ 'a \ 'a) \ bool" where "associative f == ! x y z. f (f x y) z = f x (f y z)" text{* @@ -356,12 +358,13 @@ lemma combine_ncols: "f 0 0 = 0 \ ncols A <= q \ ncols B <= q \ ncols(combine_matrix f A B) <= q" by (simp add: ncols_le) -constdefs - zero_r_neutral :: "('a \ 'b::zero \ 'a) \ bool" +definition zero_r_neutral :: "('a \ 'b::zero \ 'a) \ bool" where "zero_r_neutral f == ! a. f a 0 = a" - zero_l_neutral :: "('a::zero \ 'b \ 'b) \ bool" + +definition zero_l_neutral :: "('a::zero \ 'b \ 'b) \ bool" where "zero_l_neutral f == ! a. f 0 a = a" - zero_closed :: "(('a::zero) \ ('b::zero) \ ('c::zero)) \ bool" + +definition zero_closed :: "(('a::zero) \ ('b::zero) \ ('c::zero)) \ bool" where "zero_closed f == (!x. f x 0 = 0) & (!y. f 0 y = 0)" consts foldseq :: "('a \ 'a \ 'a) \ (nat \ 'a) \ nat \ 'a" @@ -648,10 +651,10 @@ then show ?concl by simp qed -constdefs - mult_matrix_n :: "nat \ (('a::zero) \ ('b::zero) \ ('c::zero)) \ ('c \ 'c \ 'c) \ 'a matrix \ 'b matrix \ 'c matrix" +definition mult_matrix_n :: "nat \ (('a::zero) \ ('b::zero) \ ('c::zero)) \ ('c \ 'c \ 'c) \ 'a matrix \ 'b matrix \ 'c matrix" where "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)" - mult_matrix :: "(('a::zero) \ ('b::zero) \ ('c::zero)) \ ('c \ 'c \ 'c) \ 'a matrix \ 'b matrix \ 'c matrix" + +definition mult_matrix :: "(('a::zero) \ ('b::zero) \ ('c::zero)) \ ('c \ 'c \ 'c) \ 'a matrix \ 'b matrix \ 'c matrix" where "mult_matrix fmul fadd A B == mult_matrix_n (max (ncols A) (nrows B)) fmul fadd A B" lemma mult_matrix_n: @@ -673,12 +676,13 @@ finally show "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B" by simp qed -constdefs - r_distributive :: "('a \ 'b \ 'b) \ ('b \ 'b \ 'b) \ bool" +definition r_distributive :: "('a \ 'b \ 'b) \ ('b \ 'b \ 'b) \ bool" where "r_distributive fmul fadd == ! a u v. fmul a (fadd u v) = fadd (fmul a u) (fmul a v)" - l_distributive :: "('a \ 'b \ 'a) \ ('a \ 'a \ 'a) \ bool" + +definition l_distributive :: "('a \ 'b \ 'a) \ ('a \ 'a \ 'a) \ bool" where "l_distributive fmul fadd == ! a u v. fmul (fadd u v) a = fadd (fmul u a) (fmul v a)" - distributive :: "('a \ 'a \ 'a) \ ('a \ 'a \ 'a) \ bool" + +definition distributive :: "('a \ 'a \ 'a) \ ('a \ 'a \ 'a) \ bool" where "distributive fmul fadd == l_distributive fmul fadd & r_distributive fmul fadd" lemma max1: "!! a x y. (a::nat) <= x \ a <= max x y" by (arith) @@ -835,20 +839,22 @@ apply (simp add: apply_matrix_def apply_infmatrix_def) by (simp add: zero_matrix_def) -constdefs - singleton_matrix :: "nat \ nat \ ('a::zero) \ 'a matrix" +definition singleton_matrix :: "nat \ nat \ ('a::zero) \ 'a matrix" where "singleton_matrix j i a == Abs_matrix(% m n. if j = m & i = n then a else 0)" - move_matrix :: "('a::zero) matrix \ int \ int \ 'a matrix" + +definition move_matrix :: "('a::zero) matrix \ int \ int \ 'a matrix" where "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)))" - take_rows :: "('a::zero) matrix \ nat \ 'a matrix" + +definition take_rows :: "('a::zero) matrix \ nat \ 'a matrix" where "take_rows A r == Abs_matrix(% j i. if (j < r) then (Rep_matrix A j i) else 0)" - take_columns :: "('a::zero) matrix \ nat \ 'a matrix" + +definition take_columns :: "('a::zero) matrix \ nat \ 'a matrix" where "take_columns A c == Abs_matrix(% j i. if (i < c) then (Rep_matrix A j i) else 0)" -constdefs - column_of_matrix :: "('a::zero) matrix \ nat \ 'a matrix" +definition column_of_matrix :: "('a::zero) matrix \ nat \ 'a matrix" where "column_of_matrix A n == take_columns (move_matrix A 0 (- int n)) 1" - row_of_matrix :: "('a::zero) matrix \ nat \ 'a matrix" + +definition row_of_matrix :: "('a::zero) matrix \ nat \ 'a matrix" where "row_of_matrix A m == take_rows (move_matrix A (- int m) 0) 1" lemma Rep_singleton_matrix[simp]: "Rep_matrix (singleton_matrix j i e) m n = (if j = m & i = n then e else 0)" @@ -1042,10 +1048,10 @@ with contraprems show "False" by simp qed -constdefs - foldmatrix :: "('a \ 'a \ 'a) \ ('a \ 'a \ 'a) \ ('a infmatrix) \ nat \ nat \ 'a" +definition foldmatrix :: "('a \ 'a \ 'a) \ ('a \ 'a \ 'a) \ ('a infmatrix) \ nat \ nat \ 'a" where "foldmatrix f g A m n == foldseq_transposed g (% j. foldseq f (A j) n) m" - foldmatrix_transposed :: "('a \ 'a \ 'a) \ ('a \ 'a \ 'a) \ ('a infmatrix) \ nat \ nat \ 'a" + +definition foldmatrix_transposed :: "('a \ 'a \ 'a) \ ('a \ 'a \ 'a) \ ('a infmatrix) \ nat \ nat \ 'a" where "foldmatrix_transposed f g A m n == foldseq g (% j. foldseq_transposed f (A j) n) m" lemma foldmatrix_transpose: @@ -1691,12 +1697,13 @@ lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::group_add) matrix)) = - transpose_matrix (A::'a matrix)" by (simp add: minus_matrix_def transpose_apply_matrix) -constdefs - right_inverse_matrix :: "('a::{ring_1}) matrix \ 'a matrix \ bool" +definition right_inverse_matrix :: "('a::{ring_1}) matrix \ 'a matrix \ bool" where "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \ nrows X \ ncols A" - left_inverse_matrix :: "('a::{ring_1}) matrix \ 'a matrix \ bool" + +definition left_inverse_matrix :: "('a::{ring_1}) matrix \ 'a matrix \ bool" where "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \ ncols X \ nrows A" - inverse_matrix :: "('a::{ring_1}) matrix \ 'a matrix \ bool" + +definition inverse_matrix :: "('a::{ring_1}) matrix \ 'a matrix \ bool" where "inverse_matrix A X == (right_inverse_matrix A X) \ (left_inverse_matrix A X)" lemma right_inverse_matrix_dim: "right_inverse_matrix A X \ nrows A = ncols X" @@ -1781,8 +1788,7 @@ lemma move_matrix_mult: "move_matrix ((A::('a::semiring_0) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)" by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult) -constdefs - scalar_mult :: "('a::ring) \ 'a matrix \ 'a matrix" +definition scalar_mult :: "('a::ring) \ 'a matrix \ 'a matrix" where "scalar_mult a m == apply_matrix (op * a) m" lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0"