src/HOL/Matrix/Matrix.thy
author nipkow
Mon, 21 Feb 2005 15:04:10 +0100
changeset 15539 333a88244569
parent 15481 fc075ae929e4
child 16733 236dfafbeb63
permissions -rw-r--r--
comprehensive cleanup, replacing sumr by setsum
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
     1
(*  Title:      HOL/Matrix/Matrix.thy
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
     2
    ID:         $Id$
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
     3
    Author:     Steven Obua
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
     4
*)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
     5
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
     6
theory Matrix=MatrixGeneral:
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
     7
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
     8
instance matrix :: (minus) minus 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
     9
by intro_classes
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    10
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    11
instance matrix :: (plus) plus
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    12
by (intro_classes)
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    13
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    14
instance matrix :: ("{plus,times}") times
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    15
by (intro_classes)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    16
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    17
defs (overloaded)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    18
  plus_matrix_def: "A + B == combine_matrix (op +) A B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    19
  diff_matrix_def: "A - B == combine_matrix (op -) A B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    20
  minus_matrix_def: "- A == apply_matrix uminus A"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    21
  times_matrix_def: "A * B == mult_matrix (op *) (op +) A B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    22
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    23
lemma is_meet_combine_matrix_meet: "is_meet (combine_matrix meet)"
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
    24
  by (simp_all add: is_meet_def le_matrix_def meet_left_le meet_right_le meet_imp_le)
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
    25
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
    26
lemma is_join_combine_matrix_join: "is_join (combine_matrix join)"
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
    27
  by (simp_all add: is_join_def le_matrix_def join_left_le join_right_le join_imp_le)
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    28
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    29
instance matrix :: (lordered_ab_group) lordered_ab_group_meet
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    30
proof 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    31
  fix A B C :: "('a::lordered_ab_group) matrix"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    32
  show "A + B + C = A + (B + C)"    
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    33
    apply (simp add: plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    34
    apply (rule combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    35
    apply (simp_all add: add_assoc)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    36
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    37
  show "A + B = B + A"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    38
    apply (simp add: plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    39
    apply (rule combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    40
    apply (simp_all add: add_commute)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    41
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    42
  show "0 + A = A"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    43
    apply (simp add: plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    44
    apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    45
    apply (simp)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    46
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    47
  show "- A + A = 0" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    48
    by (simp add: plus_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    49
  show "A - B = A + - B" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    50
    by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    51
  show "\<exists>m\<Colon>'a matrix \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix. is_meet m"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    52
    by (auto intro: is_meet_combine_matrix_meet)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    53
  assume "A <= B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    54
  then show "C + A <= C + B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    55
    apply (simp add: plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    56
    apply (rule le_left_combine_matrix)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    57
    apply (simp_all)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    58
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    59
qed
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    60
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    61
defs (overloaded)
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    62
  abs_matrix_def: "abs (A::('a::lordered_ab_group) matrix) == join A (- A)"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    63
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    64
instance matrix :: (lordered_ring) lordered_ring
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    65
proof
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    66
  fix A B C :: "('a :: lordered_ring) matrix"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    67
  show "A * B * C = A * (B * C)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    68
    apply (simp add: times_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    69
    apply (rule mult_matrix_assoc)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    70
    apply (simp_all add: associative_def ring_eq_simps)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    71
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    72
  show "(A + B) * C = A * C + B * C"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    73
    apply (simp add: times_matrix_def plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    74
    apply (rule l_distributive_matrix[simplified l_distributive_def, THEN spec, THEN spec, THEN spec])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    75
    apply (simp_all add: associative_def commutative_def ring_eq_simps)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    76
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    77
  show "A * (B + C) = A * B + A * C"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    78
    apply (simp add: times_matrix_def plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    79
    apply (rule r_distributive_matrix[simplified r_distributive_def, THEN spec, THEN spec, THEN spec])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    80
    apply (simp_all add: associative_def commutative_def ring_eq_simps)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    81
    done  
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    82
  show "abs A = join A (-A)" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    83
    by (simp add: abs_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    84
  assume a: "A \<le> B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    85
  assume b: "0 \<le> C"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    86
  from a b show "C * A \<le> C * B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    87
    apply (simp add: times_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    88
    apply (rule le_left_mult)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    89
    apply (simp_all add: add_mono mult_left_mono)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    90
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    91
  from a b show "A * C \<le> B * C"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    92
    apply (simp add: times_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    93
    apply (rule le_right_mult)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    94
    apply (simp_all add: add_mono mult_right_mono)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    95
    done
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    96
qed
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
    97
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    98
lemma Rep_matrix_add[simp]: "Rep_matrix ((a::('a::lordered_ab_group)matrix)+b) j i  = (Rep_matrix a j i) + (Rep_matrix b j i)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
    99
by (simp add: plus_matrix_def)
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   100
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   101
lemma Rep_matrix_mult: "Rep_matrix ((a::('a::lordered_ring) matrix) * b) j i = 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   102
  foldseq (op +) (% k.  (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   103
apply (simp add: times_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   104
apply (simp add: Rep_mult_matrix)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   105
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   106
 
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   107
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   108
lemma apply_matrix_add: "! x y. f (x+y) = (f x) + (f y) \<Longrightarrow> f 0 = (0::'a) \<Longrightarrow> apply_matrix f ((a::('a::lordered_ab_group) matrix) + b) = (apply_matrix f a) + (apply_matrix f b)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   109
apply (subst Rep_matrix_inject[symmetric])
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   110
apply (rule ext)+
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   111
apply (simp)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   112
done
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   113
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   114
lemma singleton_matrix_add: "singleton_matrix j i ((a::_::lordered_ab_group)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   115
apply (subst Rep_matrix_inject[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   116
apply (rule ext)+
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   117
apply (simp)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   118
done
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   119
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   120
lemma nrows_mult: "nrows ((A::('a::lordered_ring) matrix) * B) <= nrows A"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   121
by (simp add: times_matrix_def mult_nrows)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   122
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   123
lemma ncols_mult: "ncols ((A::('a::lordered_ring) matrix) * B) <= ncols B"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   124
by (simp add: times_matrix_def mult_ncols)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   125
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   126
constdefs
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   127
  one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   128
  "one_matrix n == Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   129
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   130
lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   131
apply (simp add: one_matrix_def)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents: 15178
diff changeset
   132
apply (simplesubst RepAbs_matrix)
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   133
apply (rule exI[of _ n], simp add: split_if)+
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   134
by (simp add: split_if, arith)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   135
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   136
lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::axclass_0_neq_1)matrix) = n" (is "?r = _")
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   137
proof -
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   138
  have "?r <= n" by (simp add: nrows_le)
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   139
  moreover have "n <= ?r" by (simp add:le_nrows, arith)
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   140
  ultimately show "?r = n" by simp
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   141
qed
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   142
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   143
lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::axclass_0_neq_1)matrix) = n" (is "?r = _")
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   144
proof -
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   145
  have "?r <= n" by (simp add: ncols_le)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   146
  moreover have "n <= ?r" by (simp add: le_ncols, arith)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   147
  ultimately show "?r = n" by simp
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   148
qed
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   149
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   150
lemma one_matrix_mult_right[simp]: "ncols A <= n \<Longrightarrow> (A::('a::{lordered_ring,ring_1}) matrix) * (one_matrix n) = A"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   151
apply (subst Rep_matrix_inject[THEN sym])
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   152
apply (rule ext)+
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   153
apply (simp add: times_matrix_def Rep_mult_matrix)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   154
apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   155
apply (simp_all)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   156
by (simp add: max_def ncols)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   157
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   158
lemma one_matrix_mult_left[simp]: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = (A::('a::{lordered_ring, ring_1}) matrix)"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   159
apply (subst Rep_matrix_inject[THEN sym])
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   160
apply (rule ext)+
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   161
apply (simp add: times_matrix_def Rep_mult_matrix)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   162
apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   163
apply (simp_all)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   164
by (simp add: max_def nrows)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   165
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   166
lemma transpose_matrix_mult: "transpose_matrix ((A::('a::{lordered_ring,comm_ring}) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   167
apply (simp add: times_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   168
apply (subst transpose_mult_matrix)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   169
apply (simp_all add: mult_commute)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   170
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   171
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   172
lemma transpose_matrix_add: "transpose_matrix ((A::('a::lordered_ab_group) matrix)+B) = transpose_matrix A + transpose_matrix B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   173
by (simp add: plus_matrix_def transpose_combine_matrix)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   174
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   175
lemma transpose_matrix_diff: "transpose_matrix ((A::('a::lordered_ab_group) matrix)-B) = transpose_matrix A - transpose_matrix B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   176
by (simp add: diff_matrix_def transpose_combine_matrix)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   177
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   178
lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::lordered_ring) matrix)) = - transpose_matrix (A::('a::lordered_ring) matrix)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   179
by (simp add: minus_matrix_def transpose_apply_matrix)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   180
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   181
constdefs 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   182
  right_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   183
  "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   184
  left_inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   185
  "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   186
  inverse_matrix :: "('a::{lordered_ring, ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   187
  "inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)"
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   188
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   189
lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   190
apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   191
by (simp add: right_inverse_matrix_def)
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   192
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   193
lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   194
apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   195
by (simp add: left_inverse_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   196
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   197
lemma left_right_inverse_matrix_unique: 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   198
  assumes "left_inverse_matrix A Y" "right_inverse_matrix A X"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   199
  shows "X = Y"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   200
proof -
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   201
  have "Y = Y * one_matrix (nrows A)" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   202
    apply (subst one_matrix_mult_right)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   203
    apply (insert prems)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   204
    by (simp_all add: left_inverse_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   205
  also have "\<dots> = Y * (A * X)" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   206
    apply (insert prems)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   207
    apply (frule right_inverse_matrix_dim)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   208
    by (simp add: right_inverse_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   209
  also have "\<dots> = (Y * A) * X" by (simp add: mult_assoc)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   210
  also have "\<dots> = X" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   211
    apply (insert prems)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   212
    apply (frule left_inverse_matrix_dim)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   213
    apply (simp_all add:  left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   214
    done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   215
  ultimately show "X = Y" by (simp)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   216
qed
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   217
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   218
lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   219
  by (auto simp add: inverse_matrix_def left_right_inverse_matrix_unique)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   220
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   221
lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   222
  by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   223
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   224
lemma zero_imp_mult_zero: "(a::'a::ring) = 0 | b = 0 \<Longrightarrow> a * b = 0"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   225
by auto
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   226
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   227
lemma Rep_matrix_zero_imp_mult_zero:
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   228
  "! j i k. (Rep_matrix A j k = 0) | (Rep_matrix B k i) = 0  \<Longrightarrow> A * B = (0::('a::lordered_ring) matrix)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   229
apply (subst Rep_matrix_inject[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   230
apply (rule ext)+
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   231
apply (auto simp add: Rep_matrix_mult foldseq_zero zero_imp_mult_zero)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   232
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   233
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   234
lemma add_nrows: "nrows (A::('a::comm_monoid_add) matrix) <= u \<Longrightarrow> nrows B <= u \<Longrightarrow> nrows (A + B) <= u"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   235
apply (simp add: plus_matrix_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   236
apply (rule combine_nrows)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   237
apply (simp_all)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   238
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   239
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   240
lemma move_matrix_row_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) j 0 = (move_matrix A j 0) * B"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   241
apply (subst Rep_matrix_inject[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   242
apply (rule ext)+
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   243
apply (auto simp add: Rep_matrix_mult foldseq_zero)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   244
apply (rule_tac foldseq_zerotail[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   245
apply (auto simp add: nrows zero_imp_mult_zero max2)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   246
apply (rule order_trans)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   247
apply (rule ncols_move_matrix_le)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   248
apply (simp add: max1)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   249
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   250
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   251
lemma move_matrix_col_mult: "move_matrix ((A::('a::lordered_ring) matrix) * B) 0 i = A * (move_matrix B 0 i)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   252
apply (subst Rep_matrix_inject[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   253
apply (rule ext)+
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   254
apply (auto simp add: Rep_matrix_mult foldseq_zero)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   255
apply (rule_tac foldseq_zerotail[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   256
apply (auto simp add: ncols zero_imp_mult_zero max1)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   257
apply (rule order_trans)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   258
apply (rule nrows_move_matrix_le)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   259
apply (simp add: max2)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   260
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   261
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   262
lemma move_matrix_add: "((move_matrix (A + B) j i)::(('a::lordered_ab_group) matrix)) = (move_matrix A j i) + (move_matrix B j i)" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   263
apply (subst Rep_matrix_inject[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   264
apply (rule ext)+
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   265
apply (simp)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   266
done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   267
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   268
lemma move_matrix_mult: "move_matrix ((A::('a::lordered_ring) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   269
by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   270
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   271
constdefs
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   272
  scalar_mult :: "('a::lordered_ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   273
  "scalar_mult a m == apply_matrix (op * a) m"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   274
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   275
lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   276
  by (simp add: scalar_mult_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   277
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   278
lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   279
  by (simp add: scalar_mult_def apply_matrix_add ring_eq_simps)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   280
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   281
lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" 
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   282
  by (simp add: scalar_mult_def)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   283
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   284
lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)"
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   285
  apply (subst Rep_matrix_inject[symmetric])
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   286
  apply (rule ext)+
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   287
  apply (auto)
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   288
  done
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   289
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   290
lemma Rep_minus[simp]: "Rep_matrix (-(A::_::lordered_ab_group)) x y = - (Rep_matrix A x y)"
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   291
  by (simp add: minus_matrix_def)
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   292
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   293
lemma join_matrix: "join (A::('a::lordered_ring) matrix) B = combine_matrix join A B"  
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   294
  apply (insert join_unique[of "(combine_matrix join)::('a matrix \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix)", simplified is_join_combine_matrix_join])
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   295
  apply (simp)
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   296
  done
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   297
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   298
lemma meet_matrix: "meet (A::('a::lordered_ring) matrix) B = combine_matrix meet A B"
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   299
  apply (insert meet_unique[of "(combine_matrix meet)::('a matrix \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix)", simplified is_meet_combine_matrix_meet])
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   300
  apply (simp)
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   301
  done
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   302
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   303
lemma Rep_abs[simp]: "Rep_matrix (abs (A::_::lordered_ring)) x y = abs (Rep_matrix A x y)"
5f621aa35c25 Matrix theory, linear programming
obua
parents: 14940
diff changeset
   304
  by (simp add: abs_lattice join_matrix)
14940
b9ab8babd8b3 Further development of matrix theory
obua
parents: 14738
diff changeset
   305
14593
90c88e7ef62d first version of matrices for HOL/Isabelle
obua
parents:
diff changeset
   306
end