author | paulson <lp15@cam.ac.uk> |
Thu, 22 Aug 2024 22:26:28 +0100 | |
changeset 80736 | c8bcb14fcfa8 |
parent 73463 | 552a9dd5b4a2 |
child 80756 | 4d592706086e |
permissions | -rw-r--r-- |
47455 | 1 |
(* Title: HOL/Matrix_LP/Matrix.thy |
14593 | 2 |
Author: Steven Obua |
3 |
*) |
|
4 |
||
17915 | 5 |
theory Matrix |
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
63566
diff
changeset
|
6 |
imports Main "HOL-Library.Lattice_Algebras" |
17915 | 7 |
begin |
14940 | 8 |
|
42463 | 9 |
type_synonym 'a infmatrix = "nat \<Rightarrow> nat \<Rightarrow> 'a" |
27484 | 10 |
|
44174
d1d79f0e1ea6
make more HOL theories work with separate set type
huffman
parents:
42463
diff
changeset
|
11 |
definition nonzero_positions :: "(nat \<Rightarrow> nat \<Rightarrow> 'a::zero) \<Rightarrow> (nat \<times> nat) set" where |
27484 | 12 |
"nonzero_positions A = {pos. A (fst pos) (snd pos) ~= 0}" |
13 |
||
45694
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
44174
diff
changeset
|
14 |
definition "matrix = {(f::(nat \<Rightarrow> nat \<Rightarrow> 'a::zero)). finite (nonzero_positions f)}" |
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
44174
diff
changeset
|
15 |
|
61260 | 16 |
typedef (overloaded) 'a matrix = "matrix :: (nat \<Rightarrow> nat \<Rightarrow> 'a::zero) set" |
45694
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
44174
diff
changeset
|
17 |
unfolding matrix_def |
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
44174
diff
changeset
|
18 |
proof |
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
44174
diff
changeset
|
19 |
show "(\<lambda>j i. 0) \<in> {(f::(nat \<Rightarrow> nat \<Rightarrow> 'a::zero)). finite (nonzero_positions f)}" |
27484 | 20 |
by (simp add: nonzero_positions_def) |
21 |
qed |
|
22 |
||
23 |
declare Rep_matrix_inverse[simp] |
|
24 |
||
80736 | 25 |
lemma matrix_eqI: |
26 |
fixes A B :: "'a::zero matrix" |
|
27 |
assumes "\<And>m n. Rep_matrix A m n = Rep_matrix B m n" |
|
28 |
shows "A=B" |
|
29 |
using Rep_matrix_inject assms by blast |
|
30 |
||
27484 | 31 |
lemma finite_nonzero_positions : "finite (nonzero_positions (Rep_matrix A))" |
45694
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
44174
diff
changeset
|
32 |
by (induct A) (simp add: Abs_matrix_inverse matrix_def) |
27484 | 33 |
|
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
34 |
definition nrows :: "('a::zero) matrix \<Rightarrow> nat" where |
27484 | 35 |
"nrows A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image fst) (nonzero_positions (Rep_matrix A))))" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
36 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
37 |
definition ncols :: "('a::zero) matrix \<Rightarrow> nat" where |
27484 | 38 |
"ncols A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image snd) (nonzero_positions (Rep_matrix A))))" |
39 |
||
40 |
lemma nrows: |
|
41 |
assumes hyp: "nrows A \<le> m" |
|
35612 | 42 |
shows "(Rep_matrix A m n) = 0" |
27484 | 43 |
proof cases |
44 |
assume "nonzero_positions(Rep_matrix A) = {}" |
|
45 |
then show "(Rep_matrix A m n) = 0" by (simp add: nonzero_positions_def) |
|
46 |
next |
|
47 |
assume a: "nonzero_positions(Rep_matrix A) \<noteq> {}" |
|
48 |
let ?S = "fst`(nonzero_positions(Rep_matrix A))" |
|
49 |
have c: "finite (?S)" by (simp add: finite_nonzero_positions) |
|
50 |
from hyp have d: "Max (?S) < m" by (simp add: a nrows_def) |
|
51 |
have "m \<notin> ?S" |
|
52 |
proof - |
|
80736 | 53 |
have "m \<in> ?S \<Longrightarrow> m \<le> Max(?S)" by (simp add: Max_ge [OF c]) |
54 |
moreover from d have "~(m \<le> Max ?S)" by (simp) |
|
27484 | 55 |
ultimately show "m \<notin> ?S" by (auto) |
56 |
qed |
|
57 |
thus "Rep_matrix A m n = 0" by (simp add: nonzero_positions_def image_Collect) |
|
58 |
qed |
|
59 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
60 |
definition transpose_infmatrix :: "'a infmatrix \<Rightarrow> 'a infmatrix" where |
27484 | 61 |
"transpose_infmatrix A j i == A i j" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
62 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
63 |
definition transpose_matrix :: "('a::zero) matrix \<Rightarrow> 'a matrix" where |
27484 | 64 |
"transpose_matrix == Abs_matrix o transpose_infmatrix o Rep_matrix" |
65 |
||
66 |
declare transpose_infmatrix_def[simp] |
|
67 |
||
68 |
lemma transpose_infmatrix_twice[simp]: "transpose_infmatrix (transpose_infmatrix A) = A" |
|
69 |
by ((rule ext)+, simp) |
|
70 |
||
80736 | 71 |
lemma transpose_infmatrix: "transpose_infmatrix (\<lambda>j i. P j i) = (\<lambda>j i. P i j)" |
27484 | 72 |
apply (rule ext)+ |
46985 | 73 |
by simp |
27484 | 74 |
|
75 |
lemma transpose_infmatrix_closed[simp]: "Rep_matrix (Abs_matrix (transpose_infmatrix (Rep_matrix x))) = transpose_infmatrix (Rep_matrix x)" |
|
76 |
apply (rule Abs_matrix_inverse) |
|
77 |
apply (simp add: matrix_def nonzero_positions_def image_def) |
|
78 |
proof - |
|
79 |
let ?A = "{pos. Rep_matrix x (snd pos) (fst pos) \<noteq> 0}" |
|
80736 | 80 |
let ?swap = "\<lambda>pos. (snd pos, fst pos)" |
27484 | 81 |
let ?B = "{pos. Rep_matrix x (fst pos) (snd pos) \<noteq> 0}" |
82 |
have swap_image: "?swap`?A = ?B" |
|
83 |
apply (simp add: image_def) |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
38526
diff
changeset
|
84 |
apply (rule set_eqI) |
27484 | 85 |
apply (simp) |
86 |
proof |
|
87 |
fix y |
|
88 |
assume hyp: "\<exists>a b. Rep_matrix x b a \<noteq> 0 \<and> y = (b, a)" |
|
89 |
thus "Rep_matrix x (fst y) (snd y) \<noteq> 0" |
|
90 |
proof - |
|
91 |
from hyp obtain a b where "(Rep_matrix x b a \<noteq> 0 & y = (b,a))" by blast |
|
92 |
then show "Rep_matrix x (fst y) (snd y) \<noteq> 0" by (simp) |
|
93 |
qed |
|
94 |
next |
|
95 |
fix y |
|
96 |
assume hyp: "Rep_matrix x (fst y) (snd y) \<noteq> 0" |
|
97 |
show "\<exists> a b. (Rep_matrix x b a \<noteq> 0 & y = (b,a))" |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
32491
diff
changeset
|
98 |
by (rule exI[of _ "snd y"], rule exI[of _ "fst y"]) (simp add: hyp) |
27484 | 99 |
qed |
100 |
then have "finite (?swap`?A)" |
|
101 |
proof - |
|
102 |
have "finite (nonzero_positions (Rep_matrix x))" by (simp add: finite_nonzero_positions) |
|
103 |
then have "finite ?B" by (simp add: nonzero_positions_def) |
|
104 |
with swap_image show "finite (?swap`?A)" by (simp) |
|
105 |
qed |
|
106 |
moreover |
|
107 |
have "inj_on ?swap ?A" by (simp add: inj_on_def) |
|
108 |
ultimately show "finite ?A"by (rule finite_imageD[of ?swap ?A]) |
|
109 |
qed |
|
110 |
||
80736 | 111 |
lemma infmatrixforward: "(x::'a infmatrix) = y \<Longrightarrow> \<forall> a b. x a b = y a b" |
112 |
by auto |
|
27484 | 113 |
|
114 |
lemma transpose_infmatrix_inject: "(transpose_infmatrix A = transpose_infmatrix B) = (A = B)" |
|
80736 | 115 |
by (metis transpose_infmatrix_twice) |
27484 | 116 |
|
117 |
lemma transpose_matrix_inject: "(transpose_matrix A = transpose_matrix B) = (A = B)" |
|
80736 | 118 |
unfolding transpose_matrix_def o_def |
119 |
by (metis Rep_matrix_inject transpose_infmatrix_closed transpose_infmatrix_inject) |
|
27484 | 120 |
|
121 |
lemma transpose_matrix[simp]: "Rep_matrix(transpose_matrix A) j i = Rep_matrix A i j" |
|
80736 | 122 |
by (simp add: transpose_matrix_def) |
27484 | 123 |
|
124 |
lemma transpose_transpose_id[simp]: "transpose_matrix (transpose_matrix A) = A" |
|
80736 | 125 |
by (simp add: transpose_matrix_def) |
27484 | 126 |
|
127 |
lemma nrows_transpose[simp]: "nrows (transpose_matrix A) = ncols A" |
|
80736 | 128 |
by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def) |
27484 | 129 |
|
130 |
lemma ncols_transpose[simp]: "ncols (transpose_matrix A) = nrows A" |
|
80736 | 131 |
by (metis nrows_transpose transpose_transpose_id) |
27484 | 132 |
|
80736 | 133 |
lemma ncols: "ncols A \<le> n \<Longrightarrow> Rep_matrix A m n = 0" |
134 |
by (metis nrows nrows_transpose transpose_matrix) |
|
27484 | 135 |
|
80736 | 136 |
lemma ncols_le: "(ncols A \<le> n) \<longleftrightarrow> (\<forall>j i. n \<le> i \<longrightarrow> (Rep_matrix A j i) = 0)" (is "_ = ?st") |
27484 | 137 |
apply (auto) |
138 |
apply (simp add: ncols) |
|
139 |
proof (simp add: ncols_def, auto) |
|
140 |
let ?P = "nonzero_positions (Rep_matrix A)" |
|
141 |
let ?p = "snd`?P" |
|
142 |
have a:"finite ?p" by (simp add: finite_nonzero_positions) |
|
143 |
let ?m = "Max ?p" |
|
80736 | 144 |
assume "~(Suc (?m) \<le> n)" |
145 |
then have b:"n \<le> ?m" by (simp) |
|
27484 | 146 |
fix a b |
147 |
assume "(a,b) \<in> ?P" |
|
148 |
then have "?p \<noteq> {}" by (auto) |
|
149 |
with a have "?m \<in> ?p" by (simp) |
|
67613 | 150 |
moreover have "\<forall>x. (x \<in> ?p \<longrightarrow> (\<exists>y. (Rep_matrix A y x) \<noteq> 0))" by (simp add: nonzero_positions_def image_def) |
151 |
ultimately have "\<exists>y. (Rep_matrix A y ?m) \<noteq> 0" by (simp) |
|
27484 | 152 |
moreover assume ?st |
153 |
ultimately show "False" using b by (simp) |
|
154 |
qed |
|
155 |
||
80736 | 156 |
lemma less_ncols: "(n < ncols A) = (\<exists>j i. n \<le> i & (Rep_matrix A j i) \<noteq> 0)" |
27484 | 157 |
proof - |
80736 | 158 |
have a: "!! (a::nat) b. (a < b) = (~(b \<le> a))" by arith |
35612 | 159 |
show ?thesis by (simp add: a ncols_le) |
27484 | 160 |
qed |
161 |
||
80736 | 162 |
lemma le_ncols: "(n \<le> ncols A) = (\<forall> m. (\<forall> j i. m \<le> i \<longrightarrow> (Rep_matrix A j i) = 0) \<longrightarrow> n \<le> m)" |
27484 | 163 |
apply (auto) |
80736 | 164 |
apply (subgoal_tac "ncols A \<le> m") |
27484 | 165 |
apply (simp) |
166 |
apply (simp add: ncols_le) |
|
167 |
apply (drule_tac x="ncols A" in spec) |
|
168 |
by (simp add: ncols) |
|
169 |
||
80736 | 170 |
lemma nrows_le: "(nrows A \<le> n) = (\<forall>j i. n \<le> j \<longrightarrow> (Rep_matrix A j i) = 0)" (is ?s) |
27484 | 171 |
proof - |
80736 | 172 |
have "(nrows A \<le> n) = (ncols (transpose_matrix A) \<le> n)" by (simp) |
173 |
also have "\<dots> = (\<forall>j i. n \<le> i \<longrightarrow> (Rep_matrix (transpose_matrix A) j i = 0))" by (rule ncols_le) |
|
174 |
also have "\<dots> = (\<forall>j i. n \<le> i \<longrightarrow> (Rep_matrix A i j) = 0)" by (simp) |
|
175 |
finally show "(nrows A \<le> n) = (\<forall>j i. n \<le> j \<longrightarrow> (Rep_matrix A j i) = 0)" by (auto) |
|
27484 | 176 |
qed |
177 |
||
80736 | 178 |
lemma less_nrows: "(m < nrows A) = (\<exists>j i. m \<le> j & (Rep_matrix A j i) \<noteq> 0)" |
27484 | 179 |
proof - |
80736 | 180 |
have a: "!! (a::nat) b. (a < b) = (~(b \<le> a))" by arith |
35612 | 181 |
show ?thesis by (simp add: a nrows_le) |
27484 | 182 |
qed |
183 |
||
80736 | 184 |
lemma le_nrows: "(n \<le> nrows A) = (\<forall> m. (\<forall> j i. m \<le> j \<longrightarrow> (Rep_matrix A j i) = 0) \<longrightarrow> n \<le> m)" |
185 |
by (meson order.trans nrows nrows_le) |
|
27484 | 186 |
|
187 |
lemma nrows_notzero: "Rep_matrix A m n \<noteq> 0 \<Longrightarrow> m < nrows A" |
|
80736 | 188 |
by (meson leI nrows) |
27484 | 189 |
|
190 |
lemma ncols_notzero: "Rep_matrix A m n \<noteq> 0 \<Longrightarrow> n < ncols A" |
|
80736 | 191 |
by (meson leI ncols) |
27484 | 192 |
|
193 |
lemma finite_natarray1: "finite {x. x < (n::nat)}" |
|
80736 | 194 |
by (induct n) auto |
27484 | 195 |
|
50027
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents:
49834
diff
changeset
|
196 |
lemma finite_natarray2: "finite {(x, y). x < (m::nat) & y < (n::nat)}" |
80736 | 197 |
by simp |
27484 | 198 |
|
199 |
lemma RepAbs_matrix: |
|
80736 | 200 |
assumes aem: "\<exists>m. \<forall>j i. m \<le> j \<longrightarrow> x j i = 0" (is ?em) and aen:"\<exists>n. \<forall>j i. (n \<le> i \<longrightarrow> x j i = 0)" (is ?en) |
27484 | 201 |
shows "(Rep_matrix (Abs_matrix x)) = x" |
202 |
apply (rule Abs_matrix_inverse) |
|
203 |
apply (simp add: matrix_def nonzero_positions_def) |
|
204 |
proof - |
|
80736 | 205 |
from aem obtain m where a: "\<forall>j i. m \<le> j \<longrightarrow> x j i = 0" by (blast) |
206 |
from aen obtain n where b: "\<forall>j i. n \<le> i \<longrightarrow> x j i = 0" by (blast) |
|
50027
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents:
49834
diff
changeset
|
207 |
let ?u = "{(i, j). x i j \<noteq> 0}" |
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents:
49834
diff
changeset
|
208 |
let ?v = "{(i, j). i < m & j < n}" |
80736 | 209 |
have c: "!! (m::nat) a. ~(m \<le> a) \<Longrightarrow> a < m" by (arith) |
27484 | 210 |
from a b have "(?u \<inter> (-?v)) = {}" |
211 |
apply (simp) |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
38526
diff
changeset
|
212 |
apply (rule set_eqI) |
27484 | 213 |
apply (simp) |
214 |
apply auto |
|
215 |
by (rule c, auto)+ |
|
216 |
then have d: "?u \<subseteq> ?v" by blast |
|
217 |
moreover have "finite ?v" by (simp add: finite_natarray2) |
|
50027
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents:
49834
diff
changeset
|
218 |
moreover have "{pos. x (fst pos) (snd pos) \<noteq> 0} = ?u" by auto |
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents:
49834
diff
changeset
|
219 |
ultimately show "finite {pos. x (fst pos) (snd pos) \<noteq> 0}" |
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents:
49834
diff
changeset
|
220 |
by (metis (lifting) finite_subset) |
27484 | 221 |
qed |
222 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
223 |
definition apply_infmatrix :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a infmatrix \<Rightarrow> 'b infmatrix" where |
80736 | 224 |
"apply_infmatrix f == \<lambda>A. (\<lambda>j i. f (A j i))" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
225 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
226 |
definition apply_matrix :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a::zero) matrix \<Rightarrow> ('b::zero) matrix" where |
80736 | 227 |
"apply_matrix f == \<lambda>A. Abs_matrix (apply_infmatrix f (Rep_matrix A))" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
228 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
229 |
definition combine_infmatrix :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a infmatrix \<Rightarrow> 'b infmatrix \<Rightarrow> 'c infmatrix" where |
80736 | 230 |
"combine_infmatrix f == \<lambda>A B. (\<lambda>j i. f (A j i) (B j i))" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
231 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
232 |
definition combine_matrix :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a::zero) matrix \<Rightarrow> ('b::zero) matrix \<Rightarrow> ('c::zero) matrix" where |
80736 | 233 |
"combine_matrix f == \<lambda>A B. Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))" |
27484 | 234 |
|
235 |
lemma expand_apply_infmatrix[simp]: "apply_infmatrix f A j i = f (A j i)" |
|
80736 | 236 |
by (simp add: apply_infmatrix_def) |
27484 | 237 |
|
238 |
lemma expand_combine_infmatrix[simp]: "combine_infmatrix f A B j i = f (A j i) (B j i)" |
|
80736 | 239 |
by (simp add: combine_infmatrix_def) |
27484 | 240 |
|
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
241 |
definition commutative :: "('a \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> bool" where |
80736 | 242 |
"commutative f == \<forall>x y. f x y = f y x" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
243 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
244 |
definition associative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool" where |
80736 | 245 |
"associative f == \<forall>x y z. f (f x y) z = f x (f y z)" |
27484 | 246 |
|
63167 | 247 |
text\<open> |
27484 | 248 |
To reason about associativity and commutativity of operations on matrices, |
249 |
let's take a step back and look at the general situtation: Assume that we have |
|
250 |
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. |
|
251 |
Each function $f: A \times A \rightarrow A$ now induces a function $f': B \times B \rightarrow B$ by $f' = u \circ f$. |
|
252 |
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.$ |
|
63167 | 253 |
\<close> |
27484 | 254 |
|
255 |
lemma combine_infmatrix_commute: |
|
256 |
"commutative f \<Longrightarrow> commutative (combine_infmatrix f)" |
|
257 |
by (simp add: commutative_def combine_infmatrix_def) |
|
258 |
||
259 |
lemma combine_matrix_commute: |
|
260 |
"commutative f \<Longrightarrow> commutative (combine_matrix f)" |
|
261 |
by (simp add: combine_matrix_def commutative_def combine_infmatrix_def) |
|
262 |
||
63167 | 263 |
text\<open> |
73463 | 264 |
On the contrary, given an associative function $f$ we cannot expect $f'$ to be associative. A counterexample is given by $A=\bbbZ$, $B=\{-1, 0, 1\}$, |
265 |
as $f$ we take addition on $\bbbZ$, which is clearly associative. The abstraction is given by $u(a) = 0$ for $a \notin B$. Then we have |
|
27484 | 266 |
\[ f' (f' 1 1) -1 = u(f (u (f 1 1)) -1) = u(f (u 2) -1) = u (f 0 -1) = -1, \] |
267 |
but on the other hand we have |
|
268 |
\[ f' 1 (f' 1 -1) = u (f 1 (u (f 1 -1))) = u (f 1 0) = 1.\] |
|
269 |
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: |
|
63167 | 270 |
\<close> |
27484 | 271 |
|
272 |
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)" |
|
80736 | 273 |
by (smt (verit) UnCI expand_combine_infmatrix mem_Collect_eq nonzero_positions_def subsetI) |
27484 | 274 |
|
275 |
lemma finite_nonzero_positions_Rep[simp]: "finite (nonzero_positions (Rep_matrix A))" |
|
80736 | 276 |
by (simp add: finite_nonzero_positions) |
27484 | 277 |
|
278 |
lemma combine_infmatrix_closed [simp]: |
|
279 |
"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)" |
|
280 |
apply (rule Abs_matrix_inverse) |
|
281 |
apply (simp add: matrix_def) |
|
282 |
apply (rule finite_subset[of _ "(nonzero_positions (Rep_matrix A)) \<union> (nonzero_positions (Rep_matrix B))"]) |
|
283 |
by (simp_all) |
|
284 |
||
63167 | 285 |
text \<open>We need the next two lemmas only later, but it is analog to the above one, so we prove them now:\<close> |
27484 | 286 |
lemma nonzero_positions_apply_infmatrix[simp]: "f 0 = 0 \<Longrightarrow> nonzero_positions (apply_infmatrix f A) \<subseteq> nonzero_positions A" |
287 |
by (rule subsetI, simp add: nonzero_positions_def apply_infmatrix_def, auto) |
|
288 |
||
289 |
lemma apply_infmatrix_closed [simp]: |
|
290 |
"f 0 = 0 \<Longrightarrow> Rep_matrix (Abs_matrix (apply_infmatrix f (Rep_matrix A))) = apply_infmatrix f (Rep_matrix A)" |
|
291 |
apply (rule Abs_matrix_inverse) |
|
292 |
apply (simp add: matrix_def) |
|
293 |
apply (rule finite_subset[of _ "nonzero_positions (Rep_matrix A)"]) |
|
294 |
by (simp_all) |
|
295 |
||
296 |
lemma combine_infmatrix_assoc[simp]: "f 0 0 = 0 \<Longrightarrow> associative f \<Longrightarrow> associative (combine_infmatrix f)" |
|
80736 | 297 |
by (simp add: associative_def combine_infmatrix_def) |
27484 | 298 |
|
299 |
lemma comb: "f = g \<Longrightarrow> x = y \<Longrightarrow> f x = g y" |
|
80736 | 300 |
by (auto) |
27484 | 301 |
|
302 |
lemma combine_matrix_assoc: "f 0 0 = 0 \<Longrightarrow> associative f \<Longrightarrow> associative (combine_matrix f)" |
|
303 |
apply (simp(no_asm) add: associative_def combine_matrix_def, auto) |
|
304 |
apply (rule comb [of Abs_matrix Abs_matrix]) |
|
305 |
by (auto, insert combine_infmatrix_assoc[of f], simp add: associative_def) |
|
306 |
||
307 |
lemma Rep_apply_matrix[simp]: "f 0 = 0 \<Longrightarrow> Rep_matrix (apply_matrix f A) j i = f (Rep_matrix A j i)" |
|
308 |
by (simp add: apply_matrix_def) |
|
309 |
||
310 |
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)" |
|
311 |
by(simp add: combine_matrix_def) |
|
312 |
||
80736 | 313 |
lemma combine_nrows_max: "f 0 0 = 0 \<Longrightarrow> nrows (combine_matrix f A B) \<le> max (nrows A) (nrows B)" |
27484 | 314 |
by (simp add: nrows_le) |
315 |
||
80736 | 316 |
lemma combine_ncols_max: "f 0 0 = 0 \<Longrightarrow> ncols (combine_matrix f A B) \<le> max (ncols A) (ncols B)" |
27484 | 317 |
by (simp add: ncols_le) |
318 |
||
80736 | 319 |
lemma combine_nrows: "f 0 0 = 0 \<Longrightarrow> nrows A \<le> q \<Longrightarrow> nrows B \<le> q \<Longrightarrow> nrows(combine_matrix f A B) \<le> q" |
27484 | 320 |
by (simp add: nrows_le) |
321 |
||
80736 | 322 |
lemma combine_ncols: "f 0 0 = 0 \<Longrightarrow> ncols A \<le> q \<Longrightarrow> ncols B \<le> q \<Longrightarrow> ncols(combine_matrix f A B) \<le> q" |
27484 | 323 |
by (simp add: ncols_le) |
324 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
325 |
definition zero_r_neutral :: "('a \<Rightarrow> 'b::zero \<Rightarrow> 'a) \<Rightarrow> bool" where |
67613 | 326 |
"zero_r_neutral f == \<forall>a. f a 0 = a" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
327 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
328 |
definition zero_l_neutral :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool" where |
67613 | 329 |
"zero_l_neutral f == \<forall>a. f 0 a = a" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
330 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
331 |
definition zero_closed :: "(('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> bool" where |
67613 | 332 |
"zero_closed f == (\<forall>x. f x 0 = 0) & (\<forall>y. f 0 y = 0)" |
27484 | 333 |
|
38273 | 334 |
primrec foldseq :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a" |
335 |
where |
|
27484 | 336 |
"foldseq f s 0 = s 0" |
80736 | 337 |
| "foldseq f s (Suc n) = f (s 0) (foldseq f (\<lambda>k. s(Suc k)) n)" |
27484 | 338 |
|
38273 | 339 |
primrec foldseq_transposed :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a" |
340 |
where |
|
27484 | 341 |
"foldseq_transposed f s 0 = s 0" |
38273 | 342 |
| "foldseq_transposed f s (Suc n) = f (foldseq_transposed f s n) (s (Suc n))" |
27484 | 343 |
|
344 |
lemma foldseq_assoc : "associative f \<Longrightarrow> foldseq f = foldseq_transposed f" |
|
345 |
proof - |
|
346 |
assume a:"associative f" |
|
80736 | 347 |
then have sublemma: "\<And>n. \<forall>N s. N \<le> n \<longrightarrow> foldseq f s N = foldseq_transposed f s N" |
27484 | 348 |
proof - |
349 |
fix n |
|
80736 | 350 |
show "\<forall>N s. N \<le> n \<longrightarrow> foldseq f s N = foldseq_transposed f s N" |
27484 | 351 |
proof (induct n) |
80736 | 352 |
show "\<forall>N s. N \<le> 0 \<longrightarrow> foldseq f s N = foldseq_transposed f s N" by simp |
27484 | 353 |
next |
354 |
fix n |
|
80736 | 355 |
assume b: "\<forall>N s. N \<le> n \<longrightarrow> foldseq f s N = foldseq_transposed f s N" |
356 |
have c:"\<And>N s. N \<le> n \<Longrightarrow> foldseq f s N = foldseq_transposed f s N" by (simp add: b) |
|
357 |
show "\<forall>N t. N \<le> Suc n \<longrightarrow> foldseq f t N = foldseq_transposed f t N" |
|
27484 | 358 |
proof (auto) |
359 |
fix N t |
|
80736 | 360 |
assume Nsuc: "N \<le> Suc n" |
27484 | 361 |
show "foldseq f t N = foldseq_transposed f t N" |
362 |
proof cases |
|
80736 | 363 |
assume "N \<le> n" |
27484 | 364 |
then show "foldseq f t N = foldseq_transposed f t N" by (simp add: b) |
365 |
next |
|
80736 | 366 |
assume "~(N \<le> n)" |
27484 | 367 |
with Nsuc have Nsuceq: "N = Suc n" by simp |
80736 | 368 |
have neqz: "n \<noteq> 0 \<Longrightarrow> \<exists>m. n = Suc m & Suc m \<le> n" by arith |
27484 | 369 |
have assocf: "!! x y z. f x (f y z) = f (f x y) z" by (insert a, simp add: associative_def) |
370 |
show "foldseq f t N = foldseq_transposed f t N" |
|
371 |
apply (simp add: Nsuceq) |
|
372 |
apply (subst c) |
|
373 |
apply (simp) |
|
374 |
apply (case_tac "n = 0") |
|
375 |
apply (simp) |
|
376 |
apply (drule neqz) |
|
377 |
apply (erule exE) |
|
378 |
apply (simp) |
|
379 |
apply (subst assocf) |
|
380 |
proof - |
|
381 |
fix m |
|
80736 | 382 |
assume "n = Suc m & Suc m \<le> n" |
383 |
then have mless: "Suc m \<le> n" by arith |
|
384 |
then have step1: "foldseq_transposed f (\<lambda>k. t (Suc k)) m = foldseq f (\<lambda>k. t (Suc k)) m" (is "?T1 = ?T2") |
|
27484 | 385 |
apply (subst c) |
386 |
by simp+ |
|
387 |
have step2: "f (t 0) ?T2 = foldseq f t (Suc m)" (is "_ = ?T3") by simp |
|
388 |
have step3: "?T3 = foldseq_transposed f t (Suc m)" (is "_ = ?T4") |
|
389 |
apply (subst c) |
|
390 |
by (simp add: mless)+ |
|
391 |
have step4: "?T4 = f (foldseq_transposed f t m) (t (Suc m))" (is "_=?T5") by simp |
|
392 |
from step1 step2 step3 step4 show sowhat: "f (f (t 0) ?T1) (t (Suc (Suc m))) = f ?T5 (t (Suc (Suc m)))" by simp |
|
393 |
qed |
|
394 |
qed |
|
395 |
qed |
|
396 |
qed |
|
397 |
qed |
|
398 |
show "foldseq f = foldseq_transposed f" by ((rule ext)+, insert sublemma, auto) |
|
399 |
qed |
|
400 |
||
80736 | 401 |
lemma foldseq_distr: "\<lbrakk>associative f; commutative f\<rbrakk> \<Longrightarrow> foldseq f (\<lambda>k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)" |
27484 | 402 |
proof - |
403 |
assume assoc: "associative f" |
|
404 |
assume comm: "commutative f" |
|
405 |
from assoc have a:"!! x y z. f (f x y) z = f x (f y z)" by (simp add: associative_def) |
|
406 |
from comm have b: "!! x y. f x y = f y x" by (simp add: commutative_def) |
|
407 |
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) |
|
80736 | 408 |
have "\<And>n. (\<forall>u v. foldseq f (\<lambda>k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n))" |
27484 | 409 |
apply (induct_tac n) |
410 |
apply (simp+, auto) |
|
411 |
by (simp add: a b c) |
|
80736 | 412 |
then show "foldseq f (\<lambda>k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)" by simp |
27484 | 413 |
qed |
414 |
||
67613 | 415 |
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); \<exists>x y. (f x) \<noteq> (f y); \<exists>x y. (g x) \<noteq> (g y); f x x = x; g x x = x\<rbrakk> \<Longrightarrow> f=g | (\<forall>y. f y x = y) | (\<forall>y. g y x = y)" |
27484 | 416 |
oops |
417 |
(* Model found |
|
418 |
||
419 |
Trying to find a model that refutes: \<lbrakk>associative f; associative g; |
|
420 |
\<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; |
|
421 |
\<exists>x y. g x \<noteq> g y; f x x = x; g x x = x\<rbrakk> |
|
422 |
\<Longrightarrow> f = g \<or> (\<forall>y. f y x = y) \<or> (\<forall>y. g y x = y) |
|
423 |
Searching for a model of size 1, translating term... invoking SAT solver... no model found. |
|
424 |
Searching for a model of size 2, translating term... invoking SAT solver... no model found. |
|
425 |
Searching for a model of size 3, translating term... invoking SAT solver... |
|
426 |
Model found: |
|
427 |
Size of types: 'a: 3 |
|
428 |
x: a1 |
|
429 |
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)) |
|
430 |
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)) |
|
431 |
*) |
|
432 |
||
433 |
lemma foldseq_zero: |
|
80736 | 434 |
assumes fz: "f 0 0 = 0" and sz: "\<forall>i. i \<le> n \<longrightarrow> s i = 0" |
27484 | 435 |
shows "foldseq f s n = 0" |
436 |
proof - |
|
80736 | 437 |
have "\<And>n. \<forall>s. (\<forall>i. i \<le> n \<longrightarrow> s i = 0) \<longrightarrow> foldseq f s n = 0" |
27484 | 438 |
apply (induct_tac n) |
439 |
apply (simp) |
|
440 |
by (simp add: fz) |
|
441 |
then show "foldseq f s n = 0" by (simp add: sz) |
|
442 |
qed |
|
443 |
||
444 |
lemma foldseq_significant_positions: |
|
80736 | 445 |
assumes p: "\<forall>i. i \<le> N \<longrightarrow> S i = T i" |
35612 | 446 |
shows "foldseq f S N = foldseq f T N" |
27484 | 447 |
proof - |
67613 | 448 |
have "\<And>m. \<forall>s t. (\<forall>i. i<=m \<longrightarrow> s i = t i) \<longrightarrow> foldseq f s m = foldseq f t m" |
27484 | 449 |
apply (induct_tac m) |
450 |
apply (simp) |
|
451 |
apply (simp) |
|
452 |
apply (auto) |
|
453 |
proof - |
|
454 |
fix n |
|
455 |
fix s::"nat\<Rightarrow>'a" |
|
456 |
fix t::"nat\<Rightarrow>'a" |
|
457 |
assume a: "\<forall>s t. (\<forall>i\<le>n. s i = t i) \<longrightarrow> foldseq f s n = foldseq f t n" |
|
458 |
assume b: "\<forall>i\<le>Suc n. s i = t i" |
|
459 |
have c:"!! a b. a = b \<Longrightarrow> f (t 0) a = f (t 0) b" by blast |
|
460 |
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) |
|
461 |
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) |
|
462 |
qed |
|
35612 | 463 |
with p show ?thesis by simp |
27484 | 464 |
qed |
465 |
||
35612 | 466 |
lemma foldseq_tail: |
80736 | 467 |
assumes "M \<le> N" |
468 |
shows "foldseq f S N = foldseq f (\<lambda>k. (if k < M then (S k) else (foldseq f (\<lambda>k. S(k+M)) (N-M)))) M" |
|
27484 | 469 |
proof - |
80736 | 470 |
have suc: "\<And>a b. \<lbrakk>a \<le> Suc b; a \<noteq> Suc b\<rbrakk> \<Longrightarrow> a \<le> b" by arith |
67613 | 471 |
have a: "\<And>a b c . a = b \<Longrightarrow> f c a = f c b" by blast |
80736 | 472 |
have "\<And>n. \<forall>m s. m \<le> n \<longrightarrow> foldseq f s n = foldseq f (\<lambda>k. (if k < m then (s k) else (foldseq f (\<lambda>k. s(k+m)) (n-m)))) m" |
27484 | 473 |
apply (induct_tac n) |
474 |
apply (simp) |
|
475 |
apply (simp) |
|
476 |
apply (auto) |
|
477 |
apply (case_tac "m = Suc na") |
|
478 |
apply (simp) |
|
479 |
apply (rule a) |
|
480 |
apply (rule foldseq_significant_positions) |
|
481 |
apply (auto) |
|
482 |
apply (drule suc, simp+) |
|
483 |
proof - |
|
484 |
fix na m s |
|
485 |
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" |
|
80736 | 486 |
assume subb:"m \<le> na" |
487 |
from suba have subc:"!! m s. m \<le> 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 |
|
27484 | 488 |
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 = |
80736 | 489 |
foldseq f (\<lambda>k. s(Suc k)) na" |
490 |
by (rule subc[of m "\<lambda>k. s(Suc k)", THEN sym], simp add: subb) |
|
491 |
from subb have sube: "m \<noteq> 0 \<Longrightarrow> \<exists>mm. m = Suc mm & mm \<le> na" by arith |
|
27484 | 492 |
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) = |
493 |
foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (Suc na - m)) m" |
|
494 |
apply (simp add: subd) |
|
38526 | 495 |
apply (cases "m = 0") |
63566 | 496 |
apply simp |
27484 | 497 |
apply (drule sube) |
63566 | 498 |
apply auto |
27484 | 499 |
apply (rule a) |
63566 | 500 |
apply (simp add: subc cong del: if_weak_cong) |
501 |
done |
|
27484 | 502 |
qed |
35612 | 503 |
then show ?thesis using assms by simp |
27484 | 504 |
qed |
505 |
||
506 |
lemma foldseq_zerotail: |
|
80736 | 507 |
assumes fz: "f 0 0 = 0" and sz: "\<forall>i. n \<le> i \<longrightarrow> s i = 0" and nm: "n \<le> m" |
508 |
shows "foldseq f s n = foldseq f s m" |
|
509 |
unfolding foldseq_tail[OF nm] |
|
510 |
by (metis (no_types, lifting) foldseq_zero fz le_add2 linorder_not_le sz) |
|
27484 | 511 |
|
512 |
lemma foldseq_zerotail2: |
|
67613 | 513 |
assumes "\<forall>x. f x 0 = x" |
514 |
and "\<forall>i. n < i \<longrightarrow> s i = 0" |
|
80736 | 515 |
and nm: "n \<le> m" |
35612 | 516 |
shows "foldseq f s n = foldseq f s m" |
27484 | 517 |
proof - |
35612 | 518 |
have "f 0 0 = 0" by (simp add: assms) |
80736 | 519 |
have b: "\<And>m n. n \<le> m \<Longrightarrow> m \<noteq> n \<Longrightarrow> \<exists>k. m-n = Suc k" by arith |
520 |
have c: "0 \<le> m" by simp |
|
67613 | 521 |
have d: "\<And>k. k \<noteq> 0 \<Longrightarrow> \<exists>l. k = Suc l" by arith |
35612 | 522 |
show ?thesis |
27484 | 523 |
apply (subst foldseq_tail[OF nm]) |
524 |
apply (rule foldseq_significant_positions) |
|
525 |
apply (auto) |
|
526 |
apply (case_tac "m=n") |
|
527 |
apply (simp+) |
|
528 |
apply (drule b[OF nm]) |
|
529 |
apply (auto) |
|
530 |
apply (case_tac "k=0") |
|
35612 | 531 |
apply (simp add: assms) |
27484 | 532 |
apply (drule d) |
533 |
apply (auto) |
|
35612 | 534 |
apply (simp add: assms foldseq_zero) |
535 |
done |
|
27484 | 536 |
qed |
537 |
||
538 |
lemma foldseq_zerostart: |
|
80736 | 539 |
"\<forall>x. f 0 (f 0 x) = f 0 x \<Longrightarrow> \<forall>i. i \<le> n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" |
27484 | 540 |
proof - |
67613 | 541 |
assume f00x: "\<forall>x. f 0 (f 0 x) = f 0 x" |
542 |
have "\<forall>s. (\<forall>i. i<=n \<longrightarrow> s i = 0) \<longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" |
|
27484 | 543 |
apply (induct n) |
544 |
apply (simp) |
|
545 |
apply (rule allI, rule impI) |
|
546 |
proof - |
|
547 |
fix n |
|
548 |
fix s |
|
80736 | 549 |
have a:"foldseq f s (Suc (Suc n)) = f (s 0) (foldseq f (\<lambda>k. s(Suc k)) (Suc n))" by simp |
67613 | 550 |
assume b: "\<forall>s. ((\<forall>i\<le>n. s i = 0) \<longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n)))" |
27484 | 551 |
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 |
80736 | 552 |
assume d: "\<forall>i. i \<le> Suc n \<longrightarrow> s i = 0" |
27484 | 553 |
show "foldseq f s (Suc (Suc n)) = f 0 (s (Suc (Suc n)))" |
554 |
apply (subst a) |
|
555 |
apply (subst c) |
|
556 |
by (simp add: d f00x)+ |
|
557 |
qed |
|
80736 | 558 |
then show "\<forall>i. i \<le> n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" by simp |
27484 | 559 |
qed |
560 |
||
561 |
lemma foldseq_zerostart2: |
|
67613 | 562 |
"\<forall>x. f 0 x = x \<Longrightarrow> \<forall>i. i < n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s n = s n" |
27484 | 563 |
proof - |
67613 | 564 |
assume a: "\<forall>i. i<n \<longrightarrow> s i = 0" |
565 |
assume x: "\<forall>x. f 0 x = x" |
|
566 |
from x have f00x: "\<forall>x. f 0 (f 0 x) = f 0 x" by blast |
|
80736 | 567 |
have b: "\<And>i l. i < Suc l = (i \<le> l)" by arith |
67613 | 568 |
have d: "\<And>k. k \<noteq> 0 \<Longrightarrow> \<exists>l. k = Suc l" by arith |
27484 | 569 |
show "foldseq f s n = s n" |
570 |
apply (case_tac "n=0") |
|
571 |
apply (simp) |
|
572 |
apply (insert a) |
|
573 |
apply (drule d) |
|
574 |
apply (auto) |
|
575 |
apply (simp add: b) |
|
576 |
apply (insert f00x) |
|
577 |
apply (drule foldseq_zerostart) |
|
578 |
by (simp add: x)+ |
|
579 |
qed |
|
580 |
||
581 |
lemma foldseq_almostzero: |
|
67613 | 582 |
assumes f0x: "\<forall>x. f 0 x = x" and fx0: "\<forall>x. f x 0 = x" and s0: "\<forall>i. i \<noteq> j \<longrightarrow> s i = 0" |
80736 | 583 |
shows "foldseq f s n = (if (j \<le> n) then (s j) else 0)" |
27484 | 584 |
proof - |
67613 | 585 |
from s0 have a: "\<forall>i. i < j \<longrightarrow> s i = 0" by simp |
586 |
from s0 have b: "\<forall>i. j < i \<longrightarrow> s i = 0" by simp |
|
35612 | 587 |
show ?thesis |
27484 | 588 |
apply auto |
589 |
apply (subst foldseq_zerotail2[of f, OF fx0, of j, OF b, of n, THEN sym]) |
|
590 |
apply simp |
|
591 |
apply (subst foldseq_zerostart2) |
|
592 |
apply (simp add: f0x a)+ |
|
593 |
apply (subst foldseq_zero) |
|
594 |
by (simp add: s0 f0x)+ |
|
595 |
qed |
|
596 |
||
597 |
lemma foldseq_distr_unary: |
|
598 |
assumes "!! a b. g (f a b) = f (g a) (g b)" |
|
80736 | 599 |
shows "g(foldseq f s n) = foldseq f (\<lambda>x. g(s x)) n" |
27484 | 600 |
proof - |
80736 | 601 |
have "\<forall>s. g(foldseq f s n) = foldseq f (\<lambda>x. g(s x)) n" |
27484 | 602 |
apply (induct_tac n) |
603 |
apply (simp) |
|
604 |
apply (simp) |
|
605 |
apply (auto) |
|
80736 | 606 |
apply (drule_tac x="\<lambda>k. s (Suc k)" in spec) |
35612 | 607 |
by (simp add: assms) |
608 |
then show ?thesis by simp |
|
27484 | 609 |
qed |
610 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
611 |
definition 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" where |
80736 | 612 |
"mult_matrix_n n fmul fadd A B == Abs_matrix(\<lambda>j i. foldseq fadd (\<lambda>k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) n)" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
613 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
614 |
definition 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" where |
27484 | 615 |
"mult_matrix fmul fadd A B == mult_matrix_n (max (ncols A) (nrows B)) fmul fadd A B" |
616 |
||
617 |
lemma mult_matrix_n: |
|
35612 | 618 |
assumes "ncols A \<le> n" (is ?An) "nrows B \<le> n" (is ?Bn) "fadd 0 0 = 0" "fmul 0 0 = 0" |
619 |
shows c:"mult_matrix fmul fadd A B = mult_matrix_n n fmul fadd A B" |
|
27484 | 620 |
proof - |
35612 | 621 |
show ?thesis using assms |
27484 | 622 |
apply (simp add: mult_matrix_def mult_matrix_n_def) |
623 |
apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+) |
|
35612 | 624 |
apply (rule foldseq_zerotail, simp_all add: nrows_le ncols_le assms) |
625 |
done |
|
27484 | 626 |
qed |
627 |
||
628 |
lemma mult_matrix_nm: |
|
80736 | 629 |
assumes "ncols A \<le> n" "nrows B \<le> n" "ncols A \<le> m" "nrows B \<le> m" "fadd 0 0 = 0" "fmul 0 0 = 0" |
27484 | 630 |
shows "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B" |
631 |
proof - |
|
35612 | 632 |
from assms have "mult_matrix_n n fmul fadd A B = mult_matrix fmul fadd A B" |
633 |
by (simp add: mult_matrix_n) |
|
634 |
also from assms have "\<dots> = mult_matrix_n m fmul fadd A B" |
|
635 |
by (simp add: mult_matrix_n[THEN sym]) |
|
27484 | 636 |
finally show "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B" by simp |
637 |
qed |
|
638 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
639 |
definition r_distributive :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool" where |
67613 | 640 |
"r_distributive fmul fadd == \<forall>a u v. fmul a (fadd u v) = fadd (fmul a u) (fmul a v)" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
641 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
642 |
definition l_distributive :: "('a \<Rightarrow> 'b \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool" where |
67613 | 643 |
"l_distributive fmul fadd == \<forall>a u v. fmul (fadd u v) a = fadd (fmul u a) (fmul v a)" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
644 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
645 |
definition distributive :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool" where |
27484 | 646 |
"distributive fmul fadd == l_distributive fmul fadd & r_distributive fmul fadd" |
647 |
||
80736 | 648 |
lemma max1: "!! a x y. (a::nat) \<le> x \<Longrightarrow> a \<le> max x y" by (arith) |
649 |
lemma max2: "!! b x y. (b::nat) \<le> y \<Longrightarrow> b \<le> max x y" by (arith) |
|
27484 | 650 |
|
651 |
lemma r_distributive_matrix: |
|
35612 | 652 |
assumes |
27484 | 653 |
"r_distributive fmul fadd" |
654 |
"associative fadd" |
|
655 |
"commutative fadd" |
|
656 |
"fadd 0 0 = 0" |
|
67613 | 657 |
"\<forall>a. fmul a 0 = 0" |
658 |
"\<forall>a. fmul 0 a = 0" |
|
35612 | 659 |
shows "r_distributive (mult_matrix fmul fadd) (combine_matrix fadd)" |
27484 | 660 |
proof - |
35612 | 661 |
from assms show ?thesis |
27484 | 662 |
apply (simp add: r_distributive_def mult_matrix_def, auto) |
663 |
proof - |
|
664 |
fix a::"'a matrix" |
|
665 |
fix u::"'b matrix" |
|
666 |
fix v::"'b matrix" |
|
667 |
let ?mx = "max (ncols a) (max (nrows u) (nrows v))" |
|
35612 | 668 |
from assms show "mult_matrix_n (max (ncols a) (nrows (combine_matrix fadd u v))) fmul fadd a (combine_matrix fadd u v) = |
27484 | 669 |
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)" |
670 |
apply (subst mult_matrix_nm[of _ _ _ ?mx fadd fmul]) |
|
671 |
apply (simp add: max1 max2 combine_nrows combine_ncols)+ |
|
672 |
apply (subst mult_matrix_nm[of _ _ v ?mx fadd fmul]) |
|
673 |
apply (simp add: max1 max2 combine_nrows combine_ncols)+ |
|
674 |
apply (subst mult_matrix_nm[of _ _ u ?mx fadd fmul]) |
|
675 |
apply (simp add: max1 max2 combine_nrows combine_ncols)+ |
|
676 |
apply (simp add: mult_matrix_n_def r_distributive_def foldseq_distr[of fadd]) |
|
677 |
apply (simp add: combine_matrix_def combine_infmatrix_def) |
|
678 |
apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+) |
|
679 |
apply (simplesubst RepAbs_matrix) |
|
680 |
apply (simp, auto) |
|
681 |
apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero) |
|
682 |
apply (rule exI[of _ "ncols v"], simp add: ncols_le foldseq_zero) |
|
683 |
apply (subst RepAbs_matrix) |
|
684 |
apply (simp, auto) |
|
685 |
apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero) |
|
686 |
apply (rule exI[of _ "ncols u"], simp add: ncols_le foldseq_zero) |
|
687 |
done |
|
688 |
qed |
|
689 |
qed |
|
690 |
||
691 |
lemma l_distributive_matrix: |
|
35612 | 692 |
assumes |
27484 | 693 |
"l_distributive fmul fadd" |
694 |
"associative fadd" |
|
695 |
"commutative fadd" |
|
696 |
"fadd 0 0 = 0" |
|
67613 | 697 |
"\<forall>a. fmul a 0 = 0" |
698 |
"\<forall>a. fmul 0 a = 0" |
|
35612 | 699 |
shows "l_distributive (mult_matrix fmul fadd) (combine_matrix fadd)" |
27484 | 700 |
proof - |
35612 | 701 |
from assms show ?thesis |
27484 | 702 |
apply (simp add: l_distributive_def mult_matrix_def, auto) |
703 |
proof - |
|
704 |
fix a::"'b matrix" |
|
705 |
fix u::"'a matrix" |
|
706 |
fix v::"'a matrix" |
|
707 |
let ?mx = "max (nrows a) (max (ncols u) (ncols v))" |
|
35612 | 708 |
from assms show "mult_matrix_n (max (ncols (combine_matrix fadd u v)) (nrows a)) fmul fadd (combine_matrix fadd u v) a = |
27484 | 709 |
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)" |
710 |
apply (subst mult_matrix_nm[of v _ _ ?mx fadd fmul]) |
|
711 |
apply (simp add: max1 max2 combine_nrows combine_ncols)+ |
|
712 |
apply (subst mult_matrix_nm[of u _ _ ?mx fadd fmul]) |
|
713 |
apply (simp add: max1 max2 combine_nrows combine_ncols)+ |
|
714 |
apply (subst mult_matrix_nm[of _ _ _ ?mx fadd fmul]) |
|
715 |
apply (simp add: max1 max2 combine_nrows combine_ncols)+ |
|
716 |
apply (simp add: mult_matrix_n_def l_distributive_def foldseq_distr[of fadd]) |
|
717 |
apply (simp add: combine_matrix_def combine_infmatrix_def) |
|
718 |
apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+) |
|
719 |
apply (simplesubst RepAbs_matrix) |
|
720 |
apply (simp, auto) |
|
721 |
apply (rule exI[of _ "nrows v"], simp add: nrows_le foldseq_zero) |
|
722 |
apply (rule exI[of _ "ncols a"], simp add: ncols_le foldseq_zero) |
|
723 |
apply (subst RepAbs_matrix) |
|
724 |
apply (simp, auto) |
|
725 |
apply (rule exI[of _ "nrows u"], simp add: nrows_le foldseq_zero) |
|
726 |
apply (rule exI[of _ "ncols a"], simp add: ncols_le foldseq_zero) |
|
727 |
done |
|
728 |
qed |
|
729 |
qed |
|
730 |
||
731 |
instantiation matrix :: (zero) zero |
|
732 |
begin |
|
733 |
||
37765 | 734 |
definition zero_matrix_def: "0 = Abs_matrix (\<lambda>j i. 0)" |
27484 | 735 |
|
736 |
instance .. |
|
737 |
||
738 |
end |
|
739 |
||
740 |
lemma Rep_zero_matrix_def[simp]: "Rep_matrix 0 j i = 0" |
|
741 |
apply (simp add: zero_matrix_def) |
|
742 |
apply (subst RepAbs_matrix) |
|
743 |
by (auto) |
|
744 |
||
745 |
lemma zero_matrix_def_nrows[simp]: "nrows 0 = 0" |
|
746 |
proof - |
|
80736 | 747 |
have a:"!! (x::nat). x \<le> 0 \<Longrightarrow> x = 0" by (arith) |
27484 | 748 |
show "nrows 0 = 0" by (rule a, subst nrows_le, simp) |
749 |
qed |
|
750 |
||
751 |
lemma zero_matrix_def_ncols[simp]: "ncols 0 = 0" |
|
752 |
proof - |
|
80736 | 753 |
have a:"!! (x::nat). x \<le> 0 \<Longrightarrow> x = 0" by (arith) |
27484 | 754 |
show "ncols 0 = 0" by (rule a, subst ncols_le, simp) |
755 |
qed |
|
756 |
||
757 |
lemma combine_matrix_zero_l_neutral: "zero_l_neutral f \<Longrightarrow> zero_l_neutral (combine_matrix f)" |
|
758 |
by (simp add: zero_l_neutral_def combine_matrix_def combine_infmatrix_def) |
|
759 |
||
760 |
lemma combine_matrix_zero_r_neutral: "zero_r_neutral f \<Longrightarrow> zero_r_neutral (combine_matrix f)" |
|
761 |
by (simp add: zero_r_neutral_def combine_matrix_def combine_infmatrix_def) |
|
762 |
||
763 |
lemma mult_matrix_zero_closed: "\<lbrakk>fadd 0 0 = 0; zero_closed fmul\<rbrakk> \<Longrightarrow> zero_closed (mult_matrix fmul fadd)" |
|
764 |
apply (simp add: zero_closed_def mult_matrix_def mult_matrix_n_def) |
|
765 |
apply (auto) |
|
766 |
by (subst foldseq_zero, (simp add: zero_matrix_def)+)+ |
|
767 |
||
67613 | 768 |
lemma mult_matrix_n_zero_right[simp]: "\<lbrakk>fadd 0 0 = 0; \<forall>a. fmul a 0 = 0\<rbrakk> \<Longrightarrow> mult_matrix_n n fmul fadd A 0 = 0" |
27484 | 769 |
apply (simp add: mult_matrix_n_def) |
770 |
apply (subst foldseq_zero) |
|
771 |
by (simp_all add: zero_matrix_def) |
|
772 |
||
67613 | 773 |
lemma mult_matrix_n_zero_left[simp]: "\<lbrakk>fadd 0 0 = 0; \<forall>a. fmul 0 a = 0\<rbrakk> \<Longrightarrow> mult_matrix_n n fmul fadd 0 A = 0" |
27484 | 774 |
apply (simp add: mult_matrix_n_def) |
775 |
apply (subst foldseq_zero) |
|
776 |
by (simp_all add: zero_matrix_def) |
|
777 |
||
67613 | 778 |
lemma mult_matrix_zero_left[simp]: "\<lbrakk>fadd 0 0 = 0; \<forall>a. fmul 0 a = 0\<rbrakk> \<Longrightarrow> mult_matrix fmul fadd 0 A = 0" |
27484 | 779 |
by (simp add: mult_matrix_def) |
780 |
||
67613 | 781 |
lemma mult_matrix_zero_right[simp]: "\<lbrakk>fadd 0 0 = 0; \<forall>a. fmul a 0 = 0\<rbrakk> \<Longrightarrow> mult_matrix fmul fadd A 0 = 0" |
27484 | 782 |
by (simp add: mult_matrix_def) |
783 |
||
784 |
lemma apply_matrix_zero[simp]: "f 0 = 0 \<Longrightarrow> apply_matrix f 0 = 0" |
|
785 |
apply (simp add: apply_matrix_def apply_infmatrix_def) |
|
786 |
by (simp add: zero_matrix_def) |
|
787 |
||
788 |
lemma combine_matrix_zero: "f 0 0 = 0 \<Longrightarrow> combine_matrix f 0 0 = 0" |
|
789 |
apply (simp add: combine_matrix_def combine_infmatrix_def) |
|
790 |
by (simp add: zero_matrix_def) |
|
791 |
||
792 |
lemma transpose_matrix_zero[simp]: "transpose_matrix 0 = 0" |
|
80736 | 793 |
by (simp add: transpose_matrix_def zero_matrix_def RepAbs_matrix transpose_infmatrix) |
27484 | 794 |
|
80736 | 795 |
lemma apply_zero_matrix_def[simp]: "apply_matrix (\<lambda>x. 0) A = 0" |
27484 | 796 |
apply (simp add: apply_matrix_def apply_infmatrix_def) |
797 |
by (simp add: zero_matrix_def) |
|
798 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
799 |
definition singleton_matrix :: "nat \<Rightarrow> nat \<Rightarrow> ('a::zero) \<Rightarrow> 'a matrix" where |
80736 | 800 |
"singleton_matrix j i a == Abs_matrix(\<lambda>m n. if j = m & i = n then a else 0)" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
801 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
802 |
definition move_matrix :: "('a::zero) matrix \<Rightarrow> int \<Rightarrow> int \<Rightarrow> 'a matrix" where |
80736 | 803 |
"move_matrix A y x == Abs_matrix(\<lambda>j i. if (((int j)-y) < 0) | (((int i)-x) < 0) then 0 else Rep_matrix A (nat ((int j)-y)) (nat ((int i)-x)))" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
804 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
805 |
definition take_rows :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where |
80736 | 806 |
"take_rows A r == Abs_matrix(\<lambda>j i. if (j < r) then (Rep_matrix A j i) else 0)" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
807 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
808 |
definition take_columns :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where |
80736 | 809 |
"take_columns A c == Abs_matrix(\<lambda>j i. if (i < c) then (Rep_matrix A j i) else 0)" |
27484 | 810 |
|
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
811 |
definition column_of_matrix :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where |
27484 | 812 |
"column_of_matrix A n == take_columns (move_matrix A 0 (- int n)) 1" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
813 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
814 |
definition row_of_matrix :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where |
27484 | 815 |
"row_of_matrix A m == take_rows (move_matrix A (- int m) 0) 1" |
816 |
||
817 |
lemma Rep_singleton_matrix[simp]: "Rep_matrix (singleton_matrix j i e) m n = (if j = m & i = n then e else 0)" |
|
818 |
apply (simp add: singleton_matrix_def) |
|
819 |
apply (auto) |
|
820 |
apply (subst RepAbs_matrix) |
|
821 |
apply (rule exI[of _ "Suc m"], simp) |
|
822 |
apply (rule exI[of _ "Suc n"], simp+) |
|
823 |
by (subst RepAbs_matrix, rule exI[of _ "Suc j"], simp, rule exI[of _ "Suc i"], simp+)+ |
|
824 |
||
825 |
lemma apply_singleton_matrix[simp]: "f 0 = 0 \<Longrightarrow> apply_matrix f (singleton_matrix j i x) = (singleton_matrix j i (f x))" |
|
80736 | 826 |
by (simp add: matrix_eqI) |
27484 | 827 |
|
828 |
lemma singleton_matrix_zero[simp]: "singleton_matrix j i 0 = 0" |
|
829 |
by (simp add: singleton_matrix_def zero_matrix_def) |
|
830 |
||
831 |
lemma nrows_singleton[simp]: "nrows(singleton_matrix j i e) = (if e = 0 then 0 else Suc j)" |
|
832 |
proof- |
|
80736 | 833 |
have th: "\<not> (\<forall>m. m \<le> j)" "\<exists>n. \<not> n \<le> i" by arith+ |
27484 | 834 |
from th show ?thesis |
835 |
apply (auto) |
|
33657 | 836 |
apply (rule le_antisym) |
27484 | 837 |
apply (subst nrows_le) |
838 |
apply (simp add: singleton_matrix_def, auto) |
|
839 |
apply (subst RepAbs_matrix) |
|
840 |
apply auto |
|
841 |
apply (simp add: Suc_le_eq) |
|
61824
dcbe9f756ae0
not_leE -> not_le_imp_less and other tidying
paulson <lp15@cam.ac.uk>
parents:
61260
diff
changeset
|
842 |
apply (rule not_le_imp_less) |
27484 | 843 |
apply (subst nrows_le) |
844 |
by simp |
|
845 |
qed |
|
846 |
||
847 |
lemma ncols_singleton[simp]: "ncols(singleton_matrix j i e) = (if e = 0 then 0 else Suc i)" |
|
848 |
proof- |
|
849 |
have th: "\<not> (\<forall>m. m \<le> j)" "\<exists>n. \<not> n \<le> i" by arith+ |
|
850 |
from th show ?thesis |
|
851 |
apply (auto) |
|
33657 | 852 |
apply (rule le_antisym) |
27484 | 853 |
apply (subst ncols_le) |
854 |
apply (simp add: singleton_matrix_def, auto) |
|
855 |
apply (subst RepAbs_matrix) |
|
856 |
apply auto |
|
857 |
apply (simp add: Suc_le_eq) |
|
61824
dcbe9f756ae0
not_leE -> not_le_imp_less and other tidying
paulson <lp15@cam.ac.uk>
parents:
61260
diff
changeset
|
858 |
apply (rule not_le_imp_less) |
27484 | 859 |
apply (subst ncols_le) |
860 |
by simp |
|
861 |
qed |
|
862 |
||
863 |
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)" |
|
864 |
apply (simp add: singleton_matrix_def combine_matrix_def combine_infmatrix_def) |
|
865 |
apply (subst RepAbs_matrix) |
|
866 |
apply (rule exI[of _ "Suc j"], simp) |
|
867 |
apply (rule exI[of _ "Suc i"], simp) |
|
868 |
apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+) |
|
869 |
apply (subst RepAbs_matrix) |
|
870 |
apply (rule exI[of _ "Suc j"], simp) |
|
871 |
apply (rule exI[of _ "Suc i"], simp) |
|
872 |
by simp |
|
873 |
||
874 |
lemma transpose_singleton[simp]: "transpose_matrix (singleton_matrix j i a) = singleton_matrix i j a" |
|
80736 | 875 |
by (simp add: matrix_eqI) |
27484 | 876 |
|
877 |
lemma Rep_move_matrix[simp]: |
|
878 |
"Rep_matrix (move_matrix A y x) j i = |
|
46702 | 879 |
(if (((int j)-y) < 0) | (((int i)-x) < 0) then 0 else Rep_matrix A (nat((int j)-y)) (nat((int i)-x)))" |
27484 | 880 |
apply (simp add: move_matrix_def) |
881 |
apply (auto) |
|
882 |
by (subst RepAbs_matrix, |
|
61945 | 883 |
rule exI[of _ "(nrows A)+(nat \<bar>y\<bar>)"], auto, rule nrows, arith, |
884 |
rule exI[of _ "(ncols A)+(nat \<bar>x\<bar>)"], auto, rule ncols, arith)+ |
|
27484 | 885 |
|
886 |
lemma move_matrix_0_0[simp]: "move_matrix A 0 0 = A" |
|
887 |
by (simp add: move_matrix_def) |
|
888 |
||
889 |
lemma move_matrix_ortho: "move_matrix A j i = move_matrix (move_matrix A j 0) 0 i" |
|
80736 | 890 |
by (simp add: matrix_eqI) |
27484 | 891 |
|
892 |
lemma transpose_move_matrix[simp]: |
|
893 |
"transpose_matrix (move_matrix A x y) = move_matrix (transpose_matrix A) y x" |
|
80736 | 894 |
by (simp add: matrix_eqI) |
27484 | 895 |
|
80736 | 896 |
lemma move_matrix_singleton[simp]: "move_matrix (singleton_matrix u v x) j i = |
27484 | 897 |
(if (j + int u < 0) | (i + int v < 0) then 0 else (singleton_matrix (nat (j + int u)) (nat (i + int v)) x))" |
80736 | 898 |
apply (intro matrix_eqI) |
899 |
apply (split if_split) |
|
900 |
apply (auto simp: split: if_split_asm) |
|
27484 | 901 |
done |
902 |
||
903 |
lemma Rep_take_columns[simp]: |
|
904 |
"Rep_matrix (take_columns A c) j i = |
|
905 |
(if i < c then (Rep_matrix A j i) else 0)" |
|
906 |
apply (simp add: take_columns_def) |
|
907 |
apply (simplesubst RepAbs_matrix) |
|
908 |
apply (rule exI[of _ "nrows A"], auto, simp add: nrows_le) |
|
909 |
apply (rule exI[of _ "ncols A"], auto, simp add: ncols_le) |
|
910 |
done |
|
911 |
||
912 |
lemma Rep_take_rows[simp]: |
|
913 |
"Rep_matrix (take_rows A r) j i = |
|
914 |
(if j < r then (Rep_matrix A j i) else 0)" |
|
915 |
apply (simp add: take_rows_def) |
|
916 |
apply (simplesubst RepAbs_matrix) |
|
917 |
apply (rule exI[of _ "nrows A"], auto, simp add: nrows_le) |
|
918 |
apply (rule exI[of _ "ncols A"], auto, simp add: ncols_le) |
|
919 |
done |
|
920 |
||
921 |
lemma Rep_column_of_matrix[simp]: |
|
922 |
"Rep_matrix (column_of_matrix A c) j i = (if i = 0 then (Rep_matrix A j c) else 0)" |
|
923 |
by (simp add: column_of_matrix_def) |
|
924 |
||
925 |
lemma Rep_row_of_matrix[simp]: |
|
926 |
"Rep_matrix (row_of_matrix A r) j i = (if j = 0 then (Rep_matrix A r i) else 0)" |
|
927 |
by (simp add: row_of_matrix_def) |
|
928 |
||
80736 | 929 |
lemma column_of_matrix: "ncols A \<le> n \<Longrightarrow> column_of_matrix A n = 0" |
930 |
by (simp add: matrix_eqI ncols) |
|
27484 | 931 |
|
80736 | 932 |
lemma row_of_matrix: "nrows A \<le> n \<Longrightarrow> row_of_matrix A n = 0" |
933 |
by (simp add: matrix_eqI nrows) |
|
27484 | 934 |
|
935 |
lemma mult_matrix_singleton_right[simp]: |
|
35612 | 936 |
assumes |
67613 | 937 |
"\<forall>x. fmul x 0 = 0" |
938 |
"\<forall>x. fmul 0 x = 0" |
|
939 |
"\<forall>x. fadd 0 x = x" |
|
940 |
"\<forall>x. fadd x 0 = x" |
|
80736 | 941 |
shows "(mult_matrix fmul fadd A (singleton_matrix j i e)) = apply_matrix (\<lambda>x. fmul x e) (move_matrix (column_of_matrix A j) 0 (int i))" |
27484 | 942 |
apply (simp add: mult_matrix_def) |
943 |
apply (subst mult_matrix_nm[of _ _ _ "max (ncols A) (Suc j)"]) |
|
944 |
apply (auto) |
|
35612 | 945 |
apply (simp add: assms)+ |
27484 | 946 |
apply (simp add: mult_matrix_n_def apply_matrix_def apply_infmatrix_def) |
947 |
apply (rule comb[of "Abs_matrix" "Abs_matrix"], auto, (rule ext)+) |
|
948 |
apply (subst foldseq_almostzero[of _ j]) |
|
35612 | 949 |
apply (simp add: assms)+ |
27484 | 950 |
apply (auto) |
29700 | 951 |
done |
27484 | 952 |
|
953 |
lemma mult_matrix_ext: |
|
954 |
assumes |
|
955 |
eprem: |
|
67613 | 956 |
"\<exists>e. (\<forall>a b. a \<noteq> b \<longrightarrow> fmul a e \<noteq> fmul b e)" |
27484 | 957 |
and fprems: |
67613 | 958 |
"\<forall>a. fmul 0 a = 0" |
959 |
"\<forall>a. fmul a 0 = 0" |
|
960 |
"\<forall>a. fadd a 0 = a" |
|
961 |
"\<forall>a. fadd 0 a = a" |
|
80736 | 962 |
and contraprems: "mult_matrix fmul fadd A = mult_matrix fmul fadd B" |
963 |
shows "A = B" |
|
27484 | 964 |
proof(rule contrapos_np[of "False"], simp) |
965 |
assume a: "A \<noteq> B" |
|
67613 | 966 |
have b: "\<And>f g. (\<forall>x y. f x y = g x y) \<Longrightarrow> f = g" by ((rule ext)+, auto) |
967 |
have "\<exists>j i. (Rep_matrix A j i) \<noteq> (Rep_matrix B j i)" |
|
80736 | 968 |
using Rep_matrix_inject a by blast |
27484 | 969 |
then obtain J I where c:"(Rep_matrix A J I) \<noteq> (Rep_matrix B J I)" by blast |
67613 | 970 |
from eprem obtain e where eprops:"(\<forall>a b. a \<noteq> b \<longrightarrow> fmul a e \<noteq> fmul b e)" by blast |
27484 | 971 |
let ?S = "singleton_matrix I 0 e" |
972 |
let ?comp = "mult_matrix fmul fadd" |
|
973 |
have d: "!!x f g. f = g \<Longrightarrow> f x = g x" by blast |
|
80736 | 974 |
have e: "(\<lambda>x. fmul x e) 0 = 0" by (simp add: assms) |
975 |
have "Rep_matrix (apply_matrix (\<lambda>x. fmul x e) (column_of_matrix A I)) \<noteq> |
|
976 |
Rep_matrix (apply_matrix (\<lambda>x. fmul x e) (column_of_matrix B I))" |
|
977 |
using fprems |
|
978 |
by (metis Rep_apply_matrix Rep_column_of_matrix eprops c) |
|
979 |
then have "~(?comp A ?S = ?comp B ?S)" |
|
980 |
by (simp add: fprems eprops Rep_matrix_inject) |
|
27484 | 981 |
with contraprems show "False" by simp |
982 |
qed |
|
983 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
984 |
definition foldmatrix :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a infmatrix) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a" where |
80736 | 985 |
"foldmatrix f g A m n == foldseq_transposed g (\<lambda>j. foldseq f (A j) n) m" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
986 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
987 |
definition foldmatrix_transposed :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a infmatrix) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a" where |
80736 | 988 |
"foldmatrix_transposed f g A m n == foldseq g (\<lambda>j. foldseq_transposed f (A j) n) m" |
27484 | 989 |
|
990 |
lemma foldmatrix_transpose: |
|
991 |
assumes |
|
67613 | 992 |
"\<forall>a b c d. g(f a b) (f c d) = f (g a c) (g b d)" |
27484 | 993 |
shows |
35612 | 994 |
"foldmatrix f g A m n = foldmatrix_transposed g f (transpose_infmatrix A) n m" |
27484 | 995 |
proof - |
67613 | 996 |
have forall:"\<And>P x. (\<forall>x. P x) \<Longrightarrow> P x" by auto |
997 |
have tworows:"\<forall>A. foldmatrix f g A 1 n = foldmatrix_transposed g f (transpose_infmatrix A) n 1" |
|
27484 | 998 |
apply (induct n) |
35612 | 999 |
apply (simp add: foldmatrix_def foldmatrix_transposed_def assms)+ |
27484 | 1000 |
apply (auto) |
80736 | 1001 |
by (drule_tac x="(\<lambda>j i. A j (Suc i))" in forall, simp) |
27484 | 1002 |
show "foldmatrix f g A m n = foldmatrix_transposed g f (transpose_infmatrix A) n m" |
1003 |
apply (simp add: foldmatrix_def foldmatrix_transposed_def) |
|
1004 |
apply (induct m, simp) |
|
1005 |
apply (simp) |
|
1006 |
apply (insert tworows) |
|
80736 | 1007 |
apply (drule_tac x="\<lambda>j i. (if j = 0 then (foldseq_transposed g (\<lambda>u. A u i) m) else (A (Suc m) i))" in spec) |
27484 | 1008 |
by (simp add: foldmatrix_def foldmatrix_transposed_def) |
1009 |
qed |
|
1010 |
||
1011 |
lemma foldseq_foldseq: |
|
1012 |
assumes |
|
1013 |
"associative f" |
|
1014 |
"associative g" |
|
67613 | 1015 |
"\<forall>a b c d. g(f a b) (f c d) = f (g a c) (g b d)" |
27484 | 1016 |
shows |
80736 | 1017 |
"foldseq g (\<lambda>j. foldseq f (A j) n) m = foldseq f (\<lambda>j. foldseq g ((transpose_infmatrix A) j) m) n" |
27484 | 1018 |
apply (insert foldmatrix_transpose[of g f A m n]) |
35612 | 1019 |
by (simp add: foldmatrix_def foldmatrix_transposed_def foldseq_assoc[THEN sym] assms) |
27484 | 1020 |
|
1021 |
lemma mult_n_nrows: |
|
1022 |
assumes |
|
67613 | 1023 |
"\<forall>a. fmul 0 a = 0" |
1024 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1025 |
"fadd 0 0 = 0" |
1026 |
shows "nrows (mult_matrix_n n fmul fadd A B) \<le> nrows A" |
|
1027 |
apply (subst nrows_le) |
|
1028 |
apply (simp add: mult_matrix_n_def) |
|
1029 |
apply (subst RepAbs_matrix) |
|
1030 |
apply (rule_tac x="nrows A" in exI) |
|
35612 | 1031 |
apply (simp add: nrows assms foldseq_zero) |
27484 | 1032 |
apply (rule_tac x="ncols B" in exI) |
35612 | 1033 |
apply (simp add: ncols assms foldseq_zero) |
1034 |
apply (simp add: nrows assms foldseq_zero) |
|
1035 |
done |
|
27484 | 1036 |
|
1037 |
lemma mult_n_ncols: |
|
1038 |
assumes |
|
67613 | 1039 |
"\<forall>a. fmul 0 a = 0" |
1040 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1041 |
"fadd 0 0 = 0" |
1042 |
shows "ncols (mult_matrix_n n fmul fadd A B) \<le> ncols B" |
|
1043 |
apply (subst ncols_le) |
|
1044 |
apply (simp add: mult_matrix_n_def) |
|
1045 |
apply (subst RepAbs_matrix) |
|
1046 |
apply (rule_tac x="nrows A" in exI) |
|
35612 | 1047 |
apply (simp add: nrows assms foldseq_zero) |
27484 | 1048 |
apply (rule_tac x="ncols B" in exI) |
35612 | 1049 |
apply (simp add: ncols assms foldseq_zero) |
1050 |
apply (simp add: ncols assms foldseq_zero) |
|
1051 |
done |
|
27484 | 1052 |
|
1053 |
lemma mult_nrows: |
|
1054 |
assumes |
|
67613 | 1055 |
"\<forall>a. fmul 0 a = 0" |
1056 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1057 |
"fadd 0 0 = 0" |
1058 |
shows "nrows (mult_matrix fmul fadd A B) \<le> nrows A" |
|
35612 | 1059 |
by (simp add: mult_matrix_def mult_n_nrows assms) |
27484 | 1060 |
|
1061 |
lemma mult_ncols: |
|
1062 |
assumes |
|
67613 | 1063 |
"\<forall>a. fmul 0 a = 0" |
1064 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1065 |
"fadd 0 0 = 0" |
1066 |
shows "ncols (mult_matrix fmul fadd A B) \<le> ncols B" |
|
35612 | 1067 |
by (simp add: mult_matrix_def mult_n_ncols assms) |
27484 | 1068 |
|
80736 | 1069 |
lemma nrows_move_matrix_le: "nrows (move_matrix A j i) \<le> nat((int (nrows A)) + j)" |
1070 |
apply (auto simp: nrows_le) |
|
27484 | 1071 |
apply (rule nrows) |
1072 |
apply (arith) |
|
1073 |
done |
|
1074 |
||
80736 | 1075 |
lemma ncols_move_matrix_le: "ncols (move_matrix A j i) \<le> nat((int (ncols A)) + i)" |
1076 |
apply (auto simp: ncols_le) |
|
27484 | 1077 |
apply (rule ncols) |
1078 |
apply (arith) |
|
1079 |
done |
|
1080 |
||
1081 |
lemma mult_matrix_assoc: |
|
35612 | 1082 |
assumes |
67613 | 1083 |
"\<forall>a. fmul1 0 a = 0" |
1084 |
"\<forall>a. fmul1 a 0 = 0" |
|
1085 |
"\<forall>a. fmul2 0 a = 0" |
|
1086 |
"\<forall>a. fmul2 a 0 = 0" |
|
27484 | 1087 |
"fadd1 0 0 = 0" |
1088 |
"fadd2 0 0 = 0" |
|
67613 | 1089 |
"\<forall>a b c d. fadd2 (fadd1 a b) (fadd1 c d) = fadd1 (fadd2 a c) (fadd2 b d)" |
27484 | 1090 |
"associative fadd1" |
1091 |
"associative fadd2" |
|
67613 | 1092 |
"\<forall>a b c. fmul2 (fmul1 a b) c = fmul1 a (fmul2 b c)" |
1093 |
"\<forall>a b c. fmul2 (fadd1 a b) c = fadd1 (fmul2 a c) (fmul2 b c)" |
|
1094 |
"\<forall>a b c. fmul1 c (fadd2 a b) = fadd2 (fmul1 c a) (fmul1 c b)" |
|
35612 | 1095 |
shows "mult_matrix fmul2 fadd2 (mult_matrix fmul1 fadd1 A B) C = mult_matrix fmul1 fadd1 A (mult_matrix fmul2 fadd2 B C)" |
27484 | 1096 |
proof - |
1097 |
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 |
|
80736 | 1098 |
have fmul2fadd1fold: "!! x s n. fmul2 (foldseq fadd1 s n) x = foldseq fadd1 (\<lambda>k. fmul2 (s k) x) n" |
1099 |
by (rule_tac g1 = "\<lambda>y. fmul2 y x" in ssubst [OF foldseq_distr_unary], insert assms, simp_all) |
|
1100 |
have fmul1fadd2fold: "!! x s n. fmul1 x (foldseq fadd2 s n) = foldseq fadd2 (\<lambda>k. fmul1 x (s k)) n" |
|
1101 |
using assms by (rule_tac g1 = "\<lambda>y. fmul1 x y" in ssubst [OF foldseq_distr_unary], simp_all) |
|
27484 | 1102 |
let ?N = "max (ncols A) (max (ncols B) (max (nrows B) (nrows C)))" |
35612 | 1103 |
show ?thesis |
80736 | 1104 |
apply (intro matrix_eqI) |
27484 | 1105 |
apply (simp add: mult_matrix_def) |
1106 |
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)"]) |
|
35612 | 1107 |
apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+ |
1108 |
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)"]) |
|
1109 |
apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+ |
|
27484 | 1110 |
apply (simplesubst mult_matrix_nm[of _ _ _ "?N"]) |
35612 | 1111 |
apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+ |
27484 | 1112 |
apply (simplesubst mult_matrix_nm[of _ _ _ "?N"]) |
35612 | 1113 |
apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+ |
27484 | 1114 |
apply (simplesubst mult_matrix_nm[of _ _ _ "?N"]) |
35612 | 1115 |
apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+ |
27484 | 1116 |
apply (simplesubst mult_matrix_nm[of _ _ _ "?N"]) |
35612 | 1117 |
apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+ |
27484 | 1118 |
apply (simp add: mult_matrix_n_def) |
1119 |
apply (rule comb_left) |
|
1120 |
apply ((rule ext)+, simp) |
|
1121 |
apply (simplesubst RepAbs_matrix) |
|
1122 |
apply (rule exI[of _ "nrows B"]) |
|
35612 | 1123 |
apply (simp add: nrows assms foldseq_zero) |
27484 | 1124 |
apply (rule exI[of _ "ncols C"]) |
35612 | 1125 |
apply (simp add: assms ncols foldseq_zero) |
27484 | 1126 |
apply (subst RepAbs_matrix) |
1127 |
apply (rule exI[of _ "nrows A"]) |
|
35612 | 1128 |
apply (simp add: nrows assms foldseq_zero) |
27484 | 1129 |
apply (rule exI[of _ "ncols B"]) |
35612 | 1130 |
apply (simp add: assms ncols foldseq_zero) |
1131 |
apply (simp add: fmul2fadd1fold fmul1fadd2fold assms) |
|
27484 | 1132 |
apply (subst foldseq_foldseq) |
35612 | 1133 |
apply (simp add: assms)+ |
1134 |
apply (simp add: transpose_infmatrix) |
|
1135 |
done |
|
27484 | 1136 |
qed |
1137 |
||
1138 |
lemma |
|
35612 | 1139 |
assumes |
67613 | 1140 |
"\<forall>a. fmul1 0 a = 0" |
1141 |
"\<forall>a. fmul1 a 0 = 0" |
|
1142 |
"\<forall>a. fmul2 0 a = 0" |
|
1143 |
"\<forall>a. fmul2 a 0 = 0" |
|
27484 | 1144 |
"fadd1 0 0 = 0" |
1145 |
"fadd2 0 0 = 0" |
|
67613 | 1146 |
"\<forall>a b c d. fadd2 (fadd1 a b) (fadd1 c d) = fadd1 (fadd2 a c) (fadd2 b d)" |
27484 | 1147 |
"associative fadd1" |
1148 |
"associative fadd2" |
|
67613 | 1149 |
"\<forall>a b c. fmul2 (fmul1 a b) c = fmul1 a (fmul2 b c)" |
1150 |
"\<forall>a b c. fmul2 (fadd1 a b) c = fadd1 (fmul2 a c) (fmul2 b c)" |
|
1151 |
"\<forall>a b c. fmul1 c (fadd2 a b) = fadd2 (fmul1 c a) (fmul1 c b)" |
|
27484 | 1152 |
shows |
1153 |
"(mult_matrix fmul1 fadd1 A) o (mult_matrix fmul2 fadd2 B) = mult_matrix fmul2 fadd2 (mult_matrix fmul1 fadd1 A B)" |
|
1154 |
apply (rule ext)+ |
|
1155 |
apply (simp add: comp_def ) |
|
35612 | 1156 |
apply (simp add: mult_matrix_assoc assms) |
1157 |
done |
|
27484 | 1158 |
|
1159 |
lemma mult_matrix_assoc_simple: |
|
35612 | 1160 |
assumes |
67613 | 1161 |
"\<forall>a. fmul 0 a = 0" |
1162 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1163 |
"fadd 0 0 = 0" |
1164 |
"associative fadd" |
|
1165 |
"commutative fadd" |
|
1166 |
"associative fmul" |
|
1167 |
"distributive fmul fadd" |
|
35612 | 1168 |
shows "mult_matrix fmul fadd (mult_matrix fmul fadd A B) C = mult_matrix fmul fadd A (mult_matrix fmul fadd B C)" |
27484 | 1169 |
proof - |
1170 |
have "!! a b c d. fadd (fadd a b) (fadd c d) = fadd (fadd a c) (fadd b d)" |
|
35612 | 1171 |
using assms by (simp add: associative_def commutative_def) |
1172 |
then show ?thesis |
|
27484 | 1173 |
apply (subst mult_matrix_assoc) |
35612 | 1174 |
using assms |
1175 |
apply simp_all |
|
1176 |
apply (simp_all add: associative_def distributive_def l_distributive_def r_distributive_def) |
|
1177 |
done |
|
27484 | 1178 |
qed |
1179 |
||
1180 |
lemma transpose_apply_matrix: "f 0 = 0 \<Longrightarrow> transpose_matrix (apply_matrix f A) = apply_matrix f (transpose_matrix A)" |
|
80736 | 1181 |
by (simp add: matrix_eqI) |
27484 | 1182 |
|
1183 |
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)" |
|
80736 | 1184 |
by (simp add: matrix_eqI) |
27484 | 1185 |
|
1186 |
lemma Rep_mult_matrix: |
|
1187 |
assumes |
|
67613 | 1188 |
"\<forall>a. fmul 0 a = 0" |
1189 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1190 |
"fadd 0 0 = 0" |
1191 |
shows |
|
1192 |
"Rep_matrix(mult_matrix fmul fadd A B) j i = |
|
80736 | 1193 |
foldseq fadd (\<lambda>k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) (max (ncols A) (nrows B))" |
27484 | 1194 |
apply (simp add: mult_matrix_def mult_matrix_n_def) |
1195 |
apply (subst RepAbs_matrix) |
|
35612 | 1196 |
apply (rule exI[of _ "nrows A"], insert assms, simp add: nrows foldseq_zero) |
1197 |
apply (rule exI[of _ "ncols B"], insert assms, simp add: ncols foldseq_zero) |
|
1198 |
apply simp |
|
1199 |
done |
|
27484 | 1200 |
|
1201 |
lemma transpose_mult_matrix: |
|
1202 |
assumes |
|
67613 | 1203 |
"\<forall>a. fmul 0 a = 0" |
1204 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1205 |
"fadd 0 0 = 0" |
67613 | 1206 |
"\<forall>x y. fmul y x = fmul x y" |
27484 | 1207 |
shows |
1208 |
"transpose_matrix (mult_matrix fmul fadd A B) = mult_matrix fmul fadd (transpose_matrix B) (transpose_matrix A)" |
|
35612 | 1209 |
using assms |
80736 | 1210 |
by (simp add: matrix_eqI Rep_mult_matrix ac_simps) |
27484 | 1211 |
|
1212 |
lemma column_transpose_matrix: "column_of_matrix (transpose_matrix A) n = transpose_matrix (row_of_matrix A n)" |
|
80736 | 1213 |
by (simp add: matrix_eqI) |
27484 | 1214 |
|
1215 |
lemma take_columns_transpose_matrix: "take_columns (transpose_matrix A) n = transpose_matrix (take_rows A n)" |
|
80736 | 1216 |
by (simp add: matrix_eqI) |
27484 | 1217 |
|
27580 | 1218 |
instantiation matrix :: ("{zero, ord}") ord |
27484 | 1219 |
begin |
1220 |
||
1221 |
definition |
|
1222 |
le_matrix_def: "A \<le> B \<longleftrightarrow> (\<forall>j i. Rep_matrix A j i \<le> Rep_matrix B j i)" |
|
1223 |
||
1224 |
definition |
|
61076 | 1225 |
less_def: "A < (B::'a matrix) \<longleftrightarrow> A \<le> B \<and> \<not> B \<le> A" |
27484 | 1226 |
|
1227 |
instance .. |
|
1228 |
||
1229 |
end |
|
1230 |
||
27580 | 1231 |
instance matrix :: ("{zero, order}") order |
80736 | 1232 |
proof |
1233 |
fix x y z :: "'a matrix" |
|
1234 |
assume "x \<le> y" "y \<le> z" |
|
1235 |
show "x \<le> z" |
|
1236 |
by (meson \<open>x \<le> y\<close> \<open>y \<le> z\<close> le_matrix_def order_trans) |
|
1237 |
next |
|
1238 |
fix x y :: "'a matrix" |
|
1239 |
assume "x \<le> y" "y \<le> x" |
|
1240 |
show "x = y" |
|
1241 |
by (meson \<open>x \<le> y\<close> \<open>y \<le> x\<close> le_matrix_def matrix_eqI order_antisym) |
|
1242 |
qed (auto simp: less_def le_matrix_def) |
|
27484 | 1243 |
|
1244 |
lemma le_apply_matrix: |
|
1245 |
assumes |
|
1246 |
"f 0 = 0" |
|
80736 | 1247 |
"\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y" |
1248 |
"(a::('a::{ord, zero}) matrix) \<le> b" |
|
1249 |
shows "apply_matrix f a \<le> apply_matrix f b" |
|
35612 | 1250 |
using assms by (simp add: le_matrix_def) |
27484 | 1251 |
|
1252 |
lemma le_combine_matrix: |
|
1253 |
assumes |
|
1254 |
"f 0 0 = 0" |
|
80736 | 1255 |
"\<forall>a b c d. a \<le> b & c \<le> d \<longrightarrow> f a c \<le> f b d" |
1256 |
"A \<le> B" |
|
1257 |
"C \<le> D" |
|
1258 |
shows "combine_matrix f A C \<le> combine_matrix f B D" |
|
35612 | 1259 |
using assms by (simp add: le_matrix_def) |
27484 | 1260 |
|
1261 |
lemma le_left_combine_matrix: |
|
1262 |
assumes |
|
1263 |
"f 0 0 = 0" |
|
80736 | 1264 |
"\<forall>a b c. a \<le> b \<longrightarrow> f c a \<le> f c b" |
1265 |
"A \<le> B" |
|
27484 | 1266 |
shows |
80736 | 1267 |
"combine_matrix f C A \<le> combine_matrix f C B" |
35612 | 1268 |
using assms by (simp add: le_matrix_def) |
27484 | 1269 |
|
1270 |
lemma le_right_combine_matrix: |
|
1271 |
assumes |
|
1272 |
"f 0 0 = 0" |
|
80736 | 1273 |
"\<forall>a b c. a \<le> b \<longrightarrow> f a c \<le> f b c" |
1274 |
"A \<le> B" |
|
27484 | 1275 |
shows |
80736 | 1276 |
"combine_matrix f A C \<le> combine_matrix f B C" |
35612 | 1277 |
using assms by (simp add: le_matrix_def) |
27484 | 1278 |
|
80736 | 1279 |
lemma le_transpose_matrix: "(A \<le> B) = (transpose_matrix A \<le> transpose_matrix B)" |
27484 | 1280 |
by (simp add: le_matrix_def, auto) |
1281 |
||
1282 |
lemma le_foldseq: |
|
1283 |
assumes |
|
80736 | 1284 |
"\<forall>a b c d . a \<le> b & c \<le> d \<longrightarrow> f a c \<le> f b d" |
1285 |
"\<forall>i. i \<le> n \<longrightarrow> s i \<le> t i" |
|
27484 | 1286 |
shows |
80736 | 1287 |
"foldseq f s n \<le> foldseq f t n" |
27484 | 1288 |
proof - |
80736 | 1289 |
have "\<forall>s t. (\<forall>i. i<=n \<longrightarrow> s i \<le> t i) \<longrightarrow> foldseq f s n \<le> foldseq f t n" |
35612 | 1290 |
by (induct n) (simp_all add: assms) |
80736 | 1291 |
then show "foldseq f s n \<le> foldseq f t n" using assms by simp |
27484 | 1292 |
qed |
1293 |
||
1294 |
lemma le_left_mult: |
|
1295 |
assumes |
|
80736 | 1296 |
"\<forall>a b c d. a \<le> b & c \<le> d \<longrightarrow> fadd a c \<le> fadd b d" |
1297 |
"\<forall>c a b. 0 \<le> c & a \<le> b \<longrightarrow> fmul c a \<le> fmul c b" |
|
67613 | 1298 |
"\<forall>a. fmul 0 a = 0" |
1299 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1300 |
"fadd 0 0 = 0" |
80736 | 1301 |
"0 \<le> C" |
1302 |
"A \<le> B" |
|
27484 | 1303 |
shows |
80736 | 1304 |
"mult_matrix fmul fadd C A \<le> mult_matrix fmul fadd C B" |
35612 | 1305 |
using assms |
1306 |
apply (simp add: le_matrix_def Rep_mult_matrix) |
|
27484 | 1307 |
apply (auto) |
1308 |
apply (simplesubst foldseq_zerotail[of _ _ _ "max (ncols C) (max (nrows A) (nrows B))"], simp_all add: nrows ncols max1 max2)+ |
|
1309 |
apply (rule le_foldseq) |
|
35612 | 1310 |
apply (auto) |
1311 |
done |
|
27484 | 1312 |
|
1313 |
lemma le_right_mult: |
|
1314 |
assumes |
|
80736 | 1315 |
"\<forall>a b c d. a \<le> b & c \<le> d \<longrightarrow> fadd a c \<le> fadd b d" |
1316 |
"\<forall>c a b. 0 \<le> c & a \<le> b \<longrightarrow> fmul a c \<le> fmul b c" |
|
67613 | 1317 |
"\<forall>a. fmul 0 a = 0" |
1318 |
"\<forall>a. fmul a 0 = 0" |
|
27484 | 1319 |
"fadd 0 0 = 0" |
80736 | 1320 |
"0 \<le> C" |
1321 |
"A \<le> B" |
|
27484 | 1322 |
shows |
80736 | 1323 |
"mult_matrix fmul fadd A C \<le> mult_matrix fmul fadd B C" |
35612 | 1324 |
using assms |
1325 |
apply (simp add: le_matrix_def Rep_mult_matrix) |
|
27484 | 1326 |
apply (auto) |
1327 |
apply (simplesubst foldseq_zerotail[of _ _ _ "max (nrows C) (max (ncols A) (ncols B))"], simp_all add: nrows ncols max1 max2)+ |
|
1328 |
apply (rule le_foldseq) |
|
35612 | 1329 |
apply (auto) |
1330 |
done |
|
27484 | 1331 |
|
67613 | 1332 |
lemma spec2: "\<forall>j i. P j i \<Longrightarrow> P j i" by blast |
27484 | 1333 |
lemma neg_imp: "(\<not> Q \<longrightarrow> \<not> P) \<Longrightarrow> P \<longrightarrow> Q" by blast |
1334 |
||
80736 | 1335 |
lemma singleton_matrix_le[simp]: "(singleton_matrix j i a \<le> singleton_matrix j i b) = (a \<le> (b::_::order))" |
1336 |
by (auto simp: le_matrix_def) |
|
27484 | 1337 |
|
80736 | 1338 |
lemma singleton_le_zero[simp]: "(singleton_matrix j i x \<le> 0) = (x \<le> (0::'a::{order,zero}))" |
27484 | 1339 |
apply (auto) |
1340 |
apply (simp add: le_matrix_def) |
|
1341 |
apply (drule_tac j=j and i=i in spec2) |
|
1342 |
apply (simp) |
|
1343 |
apply (simp add: le_matrix_def) |
|
1344 |
done |
|
1345 |
||
80736 | 1346 |
lemma singleton_ge_zero[simp]: "(0 \<le> singleton_matrix j i x) = ((0::'a::{order,zero}) \<le> x)" |
27484 | 1347 |
apply (auto) |
1348 |
apply (simp add: le_matrix_def) |
|
1349 |
apply (drule_tac j=j and i=i in spec2) |
|
1350 |
apply (simp) |
|
1351 |
apply (simp add: le_matrix_def) |
|
1352 |
done |
|
1353 |
||
80736 | 1354 |
lemma move_matrix_le_zero[simp]: "0 \<le> j \<Longrightarrow> 0 \<le> i \<Longrightarrow> (move_matrix A j i \<le> 0) = (A \<le> (0::('a::{order,zero}) matrix))" |
1355 |
apply (auto simp: le_matrix_def) |
|
27484 | 1356 |
apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2) |
1357 |
apply (auto) |
|
1358 |
done |
|
1359 |
||
80736 | 1360 |
lemma move_matrix_zero_le[simp]: "0 \<le> j \<Longrightarrow> 0 \<le> i \<Longrightarrow> (0 \<le> move_matrix A j i) = ((0::('a::{order,zero}) matrix) \<le> A)" |
1361 |
apply (auto simp: le_matrix_def) |
|
27484 | 1362 |
apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2) |
1363 |
apply (auto) |
|
1364 |
done |
|
1365 |
||
80736 | 1366 |
lemma move_matrix_le_move_matrix_iff[simp]: "0 \<le> j \<Longrightarrow> 0 \<le> i \<Longrightarrow> (move_matrix A j i \<le> move_matrix B j i) = (A \<le> (B::('a::{order,zero}) matrix))" |
1367 |
apply (auto simp: le_matrix_def) |
|
27484 | 1368 |
apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2) |
1369 |
apply (auto) |
|
1370 |
done |
|
1371 |
||
27580 | 1372 |
instantiation matrix :: ("{lattice, zero}") lattice |
25764 | 1373 |
begin |
1374 |
||
37765 | 1375 |
definition "inf = combine_matrix inf" |
25764 | 1376 |
|
37765 | 1377 |
definition "sup = combine_matrix sup" |
25764 | 1378 |
|
1379 |
instance |
|
80736 | 1380 |
by standard (auto simp: le_infI le_matrix_def inf_matrix_def sup_matrix_def) |
22452
8a86fd2a1bf0
adjusted to new lattice theory developement in Lattices.thy / FixedPoint.thy
haftmann
parents:
22422
diff
changeset
|
1381 |
|
25764 | 1382 |
end |
1383 |
||
1384 |
instantiation matrix :: ("{plus, zero}") plus |
|
1385 |
begin |
|
1386 |
||
1387 |
definition |
|
67399 | 1388 |
plus_matrix_def: "A + B = combine_matrix (+) A B" |
25764 | 1389 |
|
1390 |
instance .. |
|
1391 |
||
1392 |
end |
|
1393 |
||
1394 |
instantiation matrix :: ("{uminus, zero}") uminus |
|
1395 |
begin |
|
1396 |
||
1397 |
definition |
|
37765 | 1398 |
minus_matrix_def: "- A = apply_matrix uminus A" |
25764 | 1399 |
|
1400 |
instance .. |
|
1401 |
||
1402 |
end |
|
1403 |
||
1404 |
instantiation matrix :: ("{minus, zero}") minus |
|
1405 |
begin |
|
14593 | 1406 |
|
25764 | 1407 |
definition |
67399 | 1408 |
diff_matrix_def: "A - B = combine_matrix (-) A B" |
25764 | 1409 |
|
1410 |
instance .. |
|
1411 |
||
1412 |
end |
|
1413 |
||
1414 |
instantiation matrix :: ("{plus, times, zero}") times |
|
1415 |
begin |
|
1416 |
||
1417 |
definition |
|
69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
67613
diff
changeset
|
1418 |
times_matrix_def: "A * B = mult_matrix ((*)) (+) A B" |
14940 | 1419 |
|
25764 | 1420 |
instance .. |
1421 |
||
1422 |
end |
|
1423 |
||
27653 | 1424 |
instantiation matrix :: ("{lattice, uminus, zero}") abs |
25764 | 1425 |
begin |
14940 | 1426 |
|
25764 | 1427 |
definition |
61945 | 1428 |
abs_matrix_def: "\<bar>A :: 'a matrix\<bar> = sup A (- A)" |
25764 | 1429 |
|
1430 |
instance .. |
|
1431 |
||
1432 |
end |
|
23879 | 1433 |
|
27653 | 1434 |
instance matrix :: (monoid_add) monoid_add |
1435 |
proof |
|
1436 |
fix A B C :: "'a matrix" |
|
14940 | 1437 |
show "A + B + C = A + (B + C)" |
1438 |
apply (simp add: plus_matrix_def) |
|
1439 |
apply (rule combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec]) |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
54864
diff
changeset
|
1440 |
apply (simp_all add: add.assoc) |
14940 | 1441 |
done |
27653 | 1442 |
show "0 + A = A" |
1443 |
apply (simp add: plus_matrix_def) |
|
1444 |
apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec]) |
|
1445 |
apply (simp) |
|
1446 |
done |
|
1447 |
show "A + 0 = A" |
|
1448 |
apply (simp add: plus_matrix_def) |
|
1449 |
apply (rule combine_matrix_zero_r_neutral [simplified zero_r_neutral_def, THEN spec]) |
|
1450 |
apply (simp) |
|
1451 |
done |
|
1452 |
qed |
|
1453 |
||
1454 |
instance matrix :: (comm_monoid_add) comm_monoid_add |
|
1455 |
proof |
|
1456 |
fix A B :: "'a matrix" |
|
14940 | 1457 |
show "A + B = B + A" |
1458 |
apply (simp add: plus_matrix_def) |
|
1459 |
apply (rule combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec]) |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
54864
diff
changeset
|
1460 |
apply (simp_all add: add.commute) |
14940 | 1461 |
done |
1462 |
show "0 + A = A" |
|
1463 |
apply (simp add: plus_matrix_def) |
|
1464 |
apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec]) |
|
1465 |
apply (simp) |
|
1466 |
done |
|
27653 | 1467 |
qed |
1468 |
||
1469 |
instance matrix :: (group_add) group_add |
|
1470 |
proof |
|
1471 |
fix A B :: "'a matrix" |
|
1472 |
show "- A + A = 0" |
|
80736 | 1473 |
by (simp add: plus_matrix_def minus_matrix_def matrix_eqI) |
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
50027
diff
changeset
|
1474 |
show "A + - B = A - B" |
80736 | 1475 |
by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def matrix_eqI) |
27653 | 1476 |
qed |
1477 |
||
1478 |
instance matrix :: (ab_group_add) ab_group_add |
|
1479 |
proof |
|
1480 |
fix A B :: "'a matrix" |
|
14940 | 1481 |
show "- A + A = 0" |
80736 | 1482 |
by (simp add: plus_matrix_def minus_matrix_def matrix_eqI) |
14940 | 1483 |
show "A - B = A + - B" |
80736 | 1484 |
by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def matrix_eqI) |
27653 | 1485 |
qed |
1486 |
||
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34872
diff
changeset
|
1487 |
instance matrix :: (ordered_ab_group_add) ordered_ab_group_add |
27653 | 1488 |
proof |
1489 |
fix A B C :: "'a matrix" |
|
80736 | 1490 |
assume "A \<le> B" |
1491 |
then show "C + A \<le> C + B" |
|
14940 | 1492 |
apply (simp add: plus_matrix_def) |
1493 |
apply (rule le_left_combine_matrix) |
|
1494 |
apply (simp_all) |
|
1495 |
done |
|
1496 |
qed |
|
27653 | 1497 |
|
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34872
diff
changeset
|
1498 |
instance matrix :: (lattice_ab_group_add) semilattice_inf_ab_group_add .. |
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34872
diff
changeset
|
1499 |
instance matrix :: (lattice_ab_group_add) semilattice_sup_ab_group_add .. |
14593 | 1500 |
|
34872 | 1501 |
instance matrix :: (semiring_0) semiring_0 |
14940 | 1502 |
proof |
27653 | 1503 |
fix A B C :: "'a matrix" |
14940 | 1504 |
show "A * B * C = A * (B * C)" |
1505 |
apply (simp add: times_matrix_def) |
|
1506 |
apply (rule mult_matrix_assoc) |
|
29667 | 1507 |
apply (simp_all add: associative_def algebra_simps) |
14940 | 1508 |
done |
1509 |
show "(A + B) * C = A * C + B * C" |
|
1510 |
apply (simp add: times_matrix_def plus_matrix_def) |
|
1511 |
apply (rule l_distributive_matrix[simplified l_distributive_def, THEN spec, THEN spec, THEN spec]) |
|
29667 | 1512 |
apply (simp_all add: associative_def commutative_def algebra_simps) |
14940 | 1513 |
done |
1514 |
show "A * (B + C) = A * B + A * C" |
|
1515 |
apply (simp add: times_matrix_def plus_matrix_def) |
|
1516 |
apply (rule r_distributive_matrix[simplified r_distributive_def, THEN spec, THEN spec, THEN spec]) |
|
29667 | 1517 |
apply (simp_all add: associative_def commutative_def algebra_simps) |
27653 | 1518 |
done |
34872 | 1519 |
show "0 * A = 0" by (simp add: times_matrix_def) |
1520 |
show "A * 0 = 0" by (simp add: times_matrix_def) |
|
1521 |
qed |
|
1522 |
||
1523 |
instance matrix :: (ring) ring .. |
|
27653 | 1524 |
|
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34872
diff
changeset
|
1525 |
instance matrix :: (ordered_ring) ordered_ring |
27653 | 1526 |
proof |
1527 |
fix A B C :: "'a matrix" |
|
14940 | 1528 |
assume a: "A \<le> B" |
1529 |
assume b: "0 \<le> C" |
|
1530 |
from a b show "C * A \<le> C * B" |
|
1531 |
apply (simp add: times_matrix_def) |
|
1532 |
apply (rule le_left_mult) |
|
1533 |
apply (simp_all add: add_mono mult_left_mono) |
|
1534 |
done |
|
1535 |
from a b show "A * C \<le> B * C" |
|
1536 |
apply (simp add: times_matrix_def) |
|
1537 |
apply (rule le_right_mult) |
|
1538 |
apply (simp_all add: add_mono mult_right_mono) |
|
1539 |
done |
|
27653 | 1540 |
qed |
1541 |
||
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34872
diff
changeset
|
1542 |
instance matrix :: (lattice_ring) lattice_ring |
27653 | 1543 |
proof |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34872
diff
changeset
|
1544 |
fix A B C :: "('a :: lattice_ring) matrix" |
61945 | 1545 |
show "\<bar>A\<bar> = sup A (-A)" |
27653 | 1546 |
by (simp add: abs_matrix_def) |
1547 |
qed |
|
14593 | 1548 |
|
80736 | 1549 |
instance matrix :: (lattice_ab_group_add_abs) lattice_ab_group_add_abs |
1550 |
proof |
|
1551 |
show "\<And>a:: 'a matrix. \<bar>a\<bar> = sup a (- a)" |
|
1552 |
by (simp add: abs_matrix_def) |
|
1553 |
qed |
|
1554 |
||
25303
0699e20feabd
renamed lordered_*_* to lordered_*_add_*; further localization
haftmann
parents:
23879
diff
changeset
|
1555 |
lemma Rep_matrix_add[simp]: |
27653 | 1556 |
"Rep_matrix ((a::('a::monoid_add)matrix)+b) j i = (Rep_matrix a j i) + (Rep_matrix b j i)" |
1557 |
by (simp add: plus_matrix_def) |
|
14593 | 1558 |
|
34872 | 1559 |
lemma Rep_matrix_mult: "Rep_matrix ((a::('a::semiring_0) matrix) * b) j i = |
80736 | 1560 |
foldseq (+) (\<lambda>k. (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))" |
14940 | 1561 |
apply (simp add: times_matrix_def) |
1562 |
apply (simp add: Rep_mult_matrix) |
|
1563 |
done |
|
14593 | 1564 |
|
67613 | 1565 |
lemma apply_matrix_add: "\<forall>x y. f (x+y) = (f x) + (f y) \<Longrightarrow> f 0 = (0::'a) |
27653 | 1566 |
\<Longrightarrow> apply_matrix f ((a::('a::monoid_add) matrix) + b) = (apply_matrix f a) + (apply_matrix f b)" |
80736 | 1567 |
by (simp add: matrix_eqI) |
14593 | 1568 |
|
27653 | 1569 |
lemma singleton_matrix_add: "singleton_matrix j i ((a::_::monoid_add)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)" |
80736 | 1570 |
by (simp add: matrix_eqI) |
14593 | 1571 |
|
80736 | 1572 |
lemma nrows_mult: "nrows ((A::('a::semiring_0) matrix) * B) \<le> nrows A" |
14593 | 1573 |
by (simp add: times_matrix_def mult_nrows) |
1574 |
||
80736 | 1575 |
lemma ncols_mult: "ncols ((A::('a::semiring_0) matrix) * B) \<le> ncols B" |
14593 | 1576 |
by (simp add: times_matrix_def mult_ncols) |
1577 |
||
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
21312
diff
changeset
|
1578 |
definition |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
21312
diff
changeset
|
1579 |
one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix" where |
80736 | 1580 |
"one_matrix n = Abs_matrix (\<lambda>j i. if j = i & j < n then 1 else 0)" |
14593 | 1581 |
|
1582 |
lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)" |
|
1583 |
apply (simp add: one_matrix_def) |
|
15481 | 1584 |
apply (simplesubst RepAbs_matrix) |
62390 | 1585 |
apply (rule exI[of _ n], simp add: if_split)+ |
1586 |
by (simp add: if_split) |
|
14593 | 1587 |
|
20633 | 1588 |
lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _") |
14593 | 1589 |
proof - |
80736 | 1590 |
have "?r \<le> n" by (simp add: nrows_le) |
1591 |
moreover have "n \<le> ?r" by (simp add:le_nrows, arith) |
|
14593 | 1592 |
ultimately show "?r = n" by simp |
1593 |
qed |
|
1594 |
||
20633 | 1595 |
lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _") |
14593 | 1596 |
proof - |
80736 | 1597 |
have "?r \<le> n" by (simp add: ncols_le) |
1598 |
moreover have "n \<le> ?r" by (simp add: le_ncols, arith) |
|
14593 | 1599 |
ultimately show "?r = n" by simp |
1600 |
qed |
|
1601 |
||
80736 | 1602 |
lemma one_matrix_mult_right[simp]: "ncols A \<le> n \<Longrightarrow> (A::('a::{semiring_1}) matrix) * (one_matrix n) = A" |
1603 |
apply (intro matrix_eqI) |
|
1604 |
apply (simp add: times_matrix_def Rep_mult_matrix) |
|
1605 |
apply (subst foldseq_almostzero, auto simp: ncols) |
|
1606 |
done |
|
14593 | 1607 |
|
80736 | 1608 |
lemma one_matrix_mult_left[simp]: "nrows A \<le> n \<Longrightarrow> (one_matrix n) * A = (A::('a::semiring_1) matrix)" |
1609 |
apply (intro matrix_eqI) |
|
1610 |
apply (simp add: times_matrix_def Rep_mult_matrix) |
|
1611 |
apply (subst foldseq_almostzero, auto simp: nrows) |
|
1612 |
done |
|
14593 | 1613 |
|
27653 | 1614 |
lemma transpose_matrix_mult: "transpose_matrix ((A::('a::comm_ring) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)" |
14940 | 1615 |
apply (simp add: times_matrix_def) |
1616 |
apply (subst transpose_mult_matrix) |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
54864
diff
changeset
|
1617 |
apply (simp_all add: mult.commute) |
14940 | 1618 |
done |
1619 |
||
27653 | 1620 |
lemma transpose_matrix_add: "transpose_matrix ((A::('a::monoid_add) matrix)+B) = transpose_matrix A + transpose_matrix B" |
14940 | 1621 |
by (simp add: plus_matrix_def transpose_combine_matrix) |
1622 |
||
27653 | 1623 |
lemma transpose_matrix_diff: "transpose_matrix ((A::('a::group_add) matrix)-B) = transpose_matrix A - transpose_matrix B" |
14940 | 1624 |
by (simp add: diff_matrix_def transpose_combine_matrix) |
1625 |
||
27653 | 1626 |
lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::group_add) matrix)) = - transpose_matrix (A::'a matrix)" |
14940 | 1627 |
by (simp add: minus_matrix_def transpose_apply_matrix) |
1628 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
1629 |
definition right_inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where |
14940 | 1630 |
"right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
1631 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
1632 |
definition left_inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where |
14940 | 1633 |
"left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" |
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
1634 |
|
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
1635 |
definition inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where |
14940 | 1636 |
"inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)" |
14593 | 1637 |
|
1638 |
lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X" |
|
1639 |
apply (insert ncols_mult[of A X], insert nrows_mult[of A X]) |
|
1640 |
by (simp add: right_inverse_matrix_def) |
|
1641 |
||
14940 | 1642 |
lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y" |
1643 |
apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) |
|
1644 |
by (simp add: left_inverse_matrix_def) |
|
1645 |
||
1646 |
lemma left_right_inverse_matrix_unique: |
|
1647 |
assumes "left_inverse_matrix A Y" "right_inverse_matrix A X" |
|
1648 |
shows "X = Y" |
|
1649 |
proof - |
|
1650 |
have "Y = Y * one_matrix (nrows A)" |
|
1651 |
apply (subst one_matrix_mult_right) |
|
35612 | 1652 |
using assms |
1653 |
apply (simp_all add: left_inverse_matrix_def) |
|
1654 |
done |
|
14940 | 1655 |
also have "\<dots> = Y * (A * X)" |
35612 | 1656 |
apply (insert assms) |
14940 | 1657 |
apply (frule right_inverse_matrix_dim) |
1658 |
by (simp add: right_inverse_matrix_def) |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
54864
diff
changeset
|
1659 |
also have "\<dots> = (Y * A) * X" by (simp add: mult.assoc) |
14940 | 1660 |
also have "\<dots> = X" |
35612 | 1661 |
apply (insert assms) |
14940 | 1662 |
apply (frule left_inverse_matrix_dim) |
1663 |
apply (simp_all add: left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left) |
|
1664 |
done |
|
1665 |
ultimately show "X = Y" by (simp) |
|
1666 |
qed |
|
1667 |
||
1668 |
lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y" |
|
80736 | 1669 |
by (auto simp: inverse_matrix_def left_right_inverse_matrix_unique) |
14940 | 1670 |
|
1671 |
lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)" |
|
1672 |
by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def) |
|
1673 |
||
34872 | 1674 |
lemma zero_imp_mult_zero: "(a::'a::semiring_0) = 0 | b = 0 \<Longrightarrow> a * b = 0" |
14940 | 1675 |
by auto |
1676 |
||
1677 |
lemma Rep_matrix_zero_imp_mult_zero: |
|
67613 | 1678 |
"\<forall>j i k. (Rep_matrix A j k = 0) | (Rep_matrix B k i) = 0 \<Longrightarrow> A * B = (0::('a::lattice_ring) matrix)" |
80736 | 1679 |
by (simp add: matrix_eqI Rep_matrix_mult foldseq_zero zero_imp_mult_zero) |
14940 | 1680 |
|
80736 | 1681 |
lemma add_nrows: "nrows (A::('a::monoid_add) matrix) \<le> u \<Longrightarrow> nrows B \<le> u \<Longrightarrow> nrows (A + B) \<le> u" |
14940 | 1682 |
apply (simp add: plus_matrix_def) |
1683 |
apply (rule combine_nrows) |
|
1684 |
apply (simp_all) |
|
1685 |
done |
|
1686 |
||
34872 | 1687 |
lemma move_matrix_row_mult: "move_matrix ((A::('a::semiring_0) matrix) * B) j 0 = (move_matrix A j 0) * B" |
80736 | 1688 |
proof - |
1689 |
have "\<And>m. \<not> int m < j \<Longrightarrow> ncols (move_matrix A j 0) \<le> max (ncols A) (nrows B)" |
|
1690 |
by (smt (verit, best) max1 nat_int ncols_move_matrix_le) |
|
1691 |
then show ?thesis |
|
1692 |
apply (intro matrix_eqI) |
|
1693 |
apply (auto simp: Rep_matrix_mult foldseq_zero) |
|
1694 |
apply (rule_tac foldseq_zerotail[symmetric]) |
|
1695 |
apply (auto simp: nrows zero_imp_mult_zero max2) |
|
1696 |
done |
|
1697 |
qed |
|
14940 | 1698 |
|
34872 | 1699 |
lemma move_matrix_col_mult: "move_matrix ((A::('a::semiring_0) matrix) * B) 0 i = A * (move_matrix B 0 i)" |
80736 | 1700 |
proof - |
1701 |
have "\<And>n. \<not> int n < i \<Longrightarrow> nrows (move_matrix B 0 i) \<le> max (ncols A) (nrows B)" |
|
1702 |
by (smt (verit, del_insts) max2 nat_int nrows_move_matrix_le) |
|
1703 |
then show ?thesis |
|
1704 |
apply (intro matrix_eqI) |
|
1705 |
apply (auto simp: Rep_matrix_mult foldseq_zero) |
|
1706 |
apply (rule_tac foldseq_zerotail[symmetric]) |
|
1707 |
apply (auto simp: ncols zero_imp_mult_zero max1) |
|
1708 |
done |
|
1709 |
qed |
|
14940 | 1710 |
|
27653 | 1711 |
lemma move_matrix_add: "((move_matrix (A + B) j i)::(('a::monoid_add) matrix)) = (move_matrix A j i) + (move_matrix B j i)" |
80736 | 1712 |
by (simp add: matrix_eqI) |
14940 | 1713 |
|
34872 | 1714 |
lemma move_matrix_mult: "move_matrix ((A::('a::semiring_0) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)" |
14940 | 1715 |
by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult) |
1716 |
||
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35032
diff
changeset
|
1717 |
definition scalar_mult :: "('a::ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix" where |
69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
67613
diff
changeset
|
1718 |
"scalar_mult a m == apply_matrix ((*) a) m" |
14940 | 1719 |
|
1720 |
lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" |
|
80736 | 1721 |
by (simp add: scalar_mult_def) |
14940 | 1722 |
|
1723 |
lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)" |
|
80736 | 1724 |
by (simp add: scalar_mult_def apply_matrix_add algebra_simps) |
14940 | 1725 |
|
1726 |
lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" |
|
80736 | 1727 |
by (simp add: scalar_mult_def) |
14940 | 1728 |
|
1729 |
lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)" |
|
80736 | 1730 |
by (simp add: scalar_mult_def) |
14940 | 1731 |
|
27653 | 1732 |
lemma Rep_minus[simp]: "Rep_matrix (-(A::_::group_add)) x y = - (Rep_matrix A x y)" |
80736 | 1733 |
by (simp add: minus_matrix_def) |
14940 | 1734 |
|
61945 | 1735 |
lemma Rep_abs[simp]: "Rep_matrix \<bar>A::_::lattice_ab_group_add\<bar> x y = \<bar>Rep_matrix A x y\<bar>" |
80736 | 1736 |
by (simp add: abs_lattice sup_matrix_def) |
14940 | 1737 |
|
14593 | 1738 |
end |