src/HOL/Matrix/SparseMatrix.thy
author nipkow
Sat Jun 27 09:43:41 2009 +0200 (2009-06-27)
changeset 31816 ffaf6dd53045
parent 29667 53103fc8ffa3
child 31817 9b34b1449cb7
permissions -rw-r--r--
replaced recdefs by funs
wenzelm@16487
     1
(*  Title:      HOL/Matrix/SparseMatrix.thy
wenzelm@16487
     2
    ID:         $Id$
wenzelm@16487
     3
    Author:     Steven Obua
wenzelm@16487
     4
*)
wenzelm@16487
     5
haftmann@27484
     6
theory SparseMatrix
wenzelm@28637
     7
imports Matrix
haftmann@27484
     8
begin
obua@15009
     9
obua@15009
    10
types 
obua@15009
    11
  'a spvec = "(nat * 'a) list"
obua@15009
    12
  'a spmat = "('a spvec) spvec"
obua@15009
    13
haftmann@27653
    14
definition sparse_row_vector :: "('a::ab_group_add) spvec \<Rightarrow> 'a matrix" where
haftmann@27484
    15
  sparse_row_vector_def: "sparse_row_vector arr = foldl (% m x. m + (singleton_matrix 0 (fst x) (snd x))) 0 arr"
obua@15009
    16
haftmann@27653
    17
definition sparse_row_matrix :: "('a::ab_group_add) spmat \<Rightarrow> 'a matrix" where
haftmann@27484
    18
  sparse_row_matrix_def: "sparse_row_matrix arr = foldl (% m r. m + (move_matrix (sparse_row_vector (snd r)) (int (fst r)) 0)) 0 arr"
obua@15009
    19
haftmann@27484
    20
code_datatype sparse_row_vector sparse_row_matrix
haftmann@27484
    21
haftmann@27484
    22
lemma sparse_row_vector_empty [simp]: "sparse_row_vector [] = 0"
obua@15009
    23
  by (simp add: sparse_row_vector_def)
obua@15009
    24
haftmann@27484
    25
lemma sparse_row_matrix_empty [simp]: "sparse_row_matrix [] = 0"
obua@15009
    26
  by (simp add: sparse_row_matrix_def)
obua@15009
    27
haftmann@28562
    28
lemmas [code] = sparse_row_vector_empty [symmetric]
haftmann@27484
    29
obua@15009
    30
lemma foldl_distrstart[rule_format]: "! a x y. (f (g x y) a = g x (f y a)) \<Longrightarrow> ! x y. (foldl f (g x y) l = g x (foldl f y l))"
obua@15009
    31
  by (induct l, auto)
obua@15009
    32
haftmann@27653
    33
lemma sparse_row_vector_cons[simp]:
haftmann@27653
    34
  "sparse_row_vector (a # arr) = (singleton_matrix 0 (fst a) (snd a)) + (sparse_row_vector arr)"
obua@15009
    35
  apply (induct arr)
obua@15009
    36
  apply (auto simp add: sparse_row_vector_def)
haftmann@27653
    37
  apply (simp add: foldl_distrstart [of "\<lambda>m x. m + singleton_matrix 0 (fst x) (snd x)" "\<lambda>x m. singleton_matrix 0 (fst x) (snd x) + m"])
obua@15009
    38
  done
obua@15009
    39
haftmann@27653
    40
lemma sparse_row_vector_append[simp]:
haftmann@27653
    41
  "sparse_row_vector (a @ b) = (sparse_row_vector a) + (sparse_row_vector b)"
haftmann@27653
    42
  by (induct a) auto
obua@15009
    43
obua@15009
    44
lemma nrows_spvec[simp]: "nrows (sparse_row_vector x) <= (Suc 0)"
obua@15009
    45
  apply (induct x)
obua@15009
    46
  apply (simp_all add: add_nrows)
obua@15009
    47
  done
obua@15009
    48
obua@15009
    49
lemma sparse_row_matrix_cons: "sparse_row_matrix (a#arr) = ((move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0)) + sparse_row_matrix arr"
obua@15009
    50
  apply (induct arr)
obua@15009
    51
  apply (auto simp add: sparse_row_matrix_def)
obua@15009
    52
  apply (simp add: foldl_distrstart[of "\<lambda>m x. m + (move_matrix (sparse_row_vector (snd x)) (int (fst x)) 0)" 
obua@15009
    53
    "% a m. (move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0) + m"])
obua@15009
    54
  done
obua@15009
    55
obua@15009
    56
lemma sparse_row_matrix_append: "sparse_row_matrix (arr@brr) = (sparse_row_matrix arr) + (sparse_row_matrix brr)"
obua@15009
    57
  apply (induct arr)
obua@15009
    58
  apply (auto simp add: sparse_row_matrix_cons)
obua@15009
    59
  done
obua@15009
    60
haftmann@27653
    61
primrec sorted_spvec :: "'a spvec \<Rightarrow> bool" where
haftmann@27653
    62
  "sorted_spvec [] = True"
haftmann@27653
    63
  | sorted_spvec_step: "sorted_spvec (a#as) = (case as of [] \<Rightarrow> True | b#bs \<Rightarrow> ((fst a < fst b) & (sorted_spvec as)))" 
obua@15009
    64
haftmann@27653
    65
primrec sorted_spmat :: "'a spmat \<Rightarrow> bool" where
obua@15009
    66
  "sorted_spmat [] = True"
haftmann@27653
    67
  | "sorted_spmat (a#as) = ((sorted_spvec (snd a)) & (sorted_spmat as))"
obua@15009
    68
obua@15009
    69
declare sorted_spvec.simps [simp del]
obua@15009
    70
obua@15009
    71
lemma sorted_spvec_empty[simp]: "sorted_spvec [] = True"
obua@15009
    72
by (simp add: sorted_spvec.simps)
obua@15009
    73
obua@15009
    74
lemma sorted_spvec_cons1: "sorted_spvec (a#as) \<Longrightarrow> sorted_spvec as"
obua@15009
    75
apply (induct as)
obua@15009
    76
apply (auto simp add: sorted_spvec.simps)
obua@15009
    77
done
obua@15009
    78
obua@15009
    79
lemma sorted_spvec_cons2: "sorted_spvec (a#b#t) \<Longrightarrow> sorted_spvec (a#t)"
obua@15009
    80
apply (induct t)
obua@15009
    81
apply (auto simp add: sorted_spvec.simps)
obua@15009
    82
done
obua@15009
    83
obua@15009
    84
lemma sorted_spvec_cons3: "sorted_spvec(a#b#t) \<Longrightarrow> fst a < fst b"
obua@15009
    85
apply (auto simp add: sorted_spvec.simps)
obua@15009
    86
done
obua@15009
    87
obua@15009
    88
lemma sorted_sparse_row_vector_zero[rule_format]: "m <= n \<longrightarrow> sorted_spvec ((n,a)#arr) \<longrightarrow> Rep_matrix (sparse_row_vector arr) j m = 0"
obua@15009
    89
apply (induct arr)
obua@15009
    90
apply (auto)
obua@15009
    91
apply (frule sorted_spvec_cons2,simp)+
obua@15009
    92
apply (frule sorted_spvec_cons3, simp)
obua@15009
    93
done
obua@15009
    94
obua@15009
    95
lemma sorted_sparse_row_matrix_zero[rule_format]: "m <= n \<longrightarrow> sorted_spvec ((n,a)#arr) \<longrightarrow> Rep_matrix (sparse_row_matrix arr) m j = 0"
obua@15009
    96
  apply (induct arr)
obua@15009
    97
  apply (auto)
obua@15009
    98
  apply (frule sorted_spvec_cons2, simp)
obua@15009
    99
  apply (frule sorted_spvec_cons3, simp)
obua@15009
   100
  apply (simp add: sparse_row_matrix_cons neg_def)
obua@15009
   101
  done
obua@15009
   102
haftmann@27653
   103
primrec minus_spvec :: "('a::ab_group_add) spvec \<Rightarrow> 'a spvec" where
obua@15178
   104
  "minus_spvec [] = []"
haftmann@27484
   105
  | "minus_spvec (a#as) = (fst a, -(snd a))#(minus_spvec as)"
obua@15178
   106
haftmann@27653
   107
primrec abs_spvec ::  "('a::lordered_ab_group_add_abs) spvec \<Rightarrow> 'a spvec" where
obua@15178
   108
  "abs_spvec [] = []"
haftmann@27484
   109
  | "abs_spvec (a#as) = (fst a, abs (snd a))#(abs_spvec as)"
obua@15178
   110
obua@15178
   111
lemma sparse_row_vector_minus: 
obua@15178
   112
  "sparse_row_vector (minus_spvec v) = - (sparse_row_vector v)"
obua@15178
   113
  apply (induct v)
obua@15178
   114
  apply (simp_all add: sparse_row_vector_cons)
obua@15178
   115
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15178
   116
  apply (rule ext)+
obua@15178
   117
  apply simp
obua@15178
   118
  done
obua@15178
   119
haftmann@27653
   120
instance matrix :: (lordered_ab_group_add_abs) lordered_ab_group_add_abs
haftmann@27653
   121
apply default
haftmann@27653
   122
unfolding abs_matrix_def .. (*FIXME move*)
haftmann@27653
   123
obua@15178
   124
lemma sparse_row_vector_abs:
haftmann@27653
   125
  "sorted_spvec (v :: 'a::lordered_ring spvec) \<Longrightarrow> sparse_row_vector (abs_spvec v) = abs (sparse_row_vector v)"
obua@15178
   126
  apply (induct v)
haftmann@27653
   127
  apply simp_all
obua@15178
   128
  apply (frule_tac sorted_spvec_cons1, simp)
obua@15178
   129
  apply (simp only: Rep_matrix_inject[symmetric])
obua@15178
   130
  apply (rule ext)+
obua@15178
   131
  apply auto
nipkow@15236
   132
  apply (subgoal_tac "Rep_matrix (sparse_row_vector v) 0 a = 0")
obua@15178
   133
  apply (simp)
obua@15178
   134
  apply (rule sorted_sparse_row_vector_zero)
obua@15178
   135
  apply auto
obua@15178
   136
  done
obua@15178
   137
obua@15178
   138
lemma sorted_spvec_minus_spvec:
obua@15178
   139
  "sorted_spvec v \<Longrightarrow> sorted_spvec (minus_spvec v)"
obua@15178
   140
  apply (induct v)
obua@15178
   141
  apply (simp)
obua@15178
   142
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   143
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   144
  done
obua@15178
   145
obua@15178
   146
lemma sorted_spvec_abs_spvec:
obua@15178
   147
  "sorted_spvec v \<Longrightarrow> sorted_spvec (abs_spvec v)"
obua@15178
   148
  apply (induct v)
obua@15178
   149
  apply (simp)
obua@15178
   150
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   151
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   152
  done
obua@15178
   153
  
haftmann@27653
   154
definition
haftmann@27653
   155
  "smult_spvec y = map (% a. (fst a, y * snd a))"  
obua@15009
   156
obua@15009
   157
lemma smult_spvec_empty[simp]: "smult_spvec y [] = []"
obua@15009
   158
  by (simp add: smult_spvec_def)
obua@15009
   159
obua@15009
   160
lemma smult_spvec_cons: "smult_spvec y (a#arr) = (fst a, y * (snd a)) # (smult_spvec y arr)"
obua@15009
   161
  by (simp add: smult_spvec_def)
obua@15009
   162
nipkow@31816
   163
fun addmult_spvec :: "('a::ring) \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec" where
nipkow@31816
   164
  "addmult_spvec y arr [] = arr" |
nipkow@31816
   165
  "addmult_spvec y [] brr = smult_spvec y brr" |
nipkow@31816
   166
  "addmult_spvec y ((i,a)#arr) ((j,b)#brr) = (
nipkow@31816
   167
    if i < j then ((i,a)#(addmult_spvec y arr ((j,b)#brr))) 
nipkow@31816
   168
    else (if (j < i) then ((j, y * b)#(addmult_spvec y ((i,a)#arr) brr))
nipkow@31816
   169
    else ((i, a + y*b)#(addmult_spvec y arr brr))))"
nipkow@31816
   170
(* Steven used termination "measure (% (y, a, b). length a + (length b))" *)
obua@15009
   171
nipkow@31816
   172
lemma addmult_spvec_empty1[simp]: "addmult_spvec y [] a = smult_spvec y a"
haftmann@27484
   173
  by (induct a) auto
obua@15009
   174
nipkow@31816
   175
lemma addmult_spvec_empty2[simp]: "addmult_spvec y a [] = a"
haftmann@27484
   176
  by (induct a) auto
obua@15009
   177
obua@15009
   178
lemma sparse_row_vector_map: "(! x y. f (x+y) = (f x) + (f y)) \<Longrightarrow> (f::'a\<Rightarrow>('a::lordered_ring)) 0 = 0 \<Longrightarrow> 
obua@15009
   179
  sparse_row_vector (map (% x. (fst x, f (snd x))) a) = apply_matrix f (sparse_row_vector a)"
obua@15009
   180
  apply (induct a)
obua@15009
   181
  apply (simp_all add: apply_matrix_add)
obua@15009
   182
  done
obua@15009
   183
obua@15009
   184
lemma sparse_row_vector_smult: "sparse_row_vector (smult_spvec y a) = scalar_mult y (sparse_row_vector a)"
obua@15009
   185
  apply (induct a)
obua@15009
   186
  apply (simp_all add: smult_spvec_cons scalar_mult_add)
obua@15009
   187
  done
obua@15009
   188
nipkow@31816
   189
lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lordered_ring) a b) = 
obua@15009
   190
  (sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))"
obua@15009
   191
  apply (rule addmult_spvec.induct[of _ y])
obua@15009
   192
  apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+
obua@15009
   193
  done
obua@15009
   194
obua@15009
   195
lemma sorted_smult_spvec[rule_format]: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)"
obua@15009
   196
  apply (auto simp add: smult_spvec_def)
obua@15009
   197
  apply (induct a)
nipkow@15236
   198
  apply (auto simp add: sorted_spvec.simps split:list.split_asm)
obua@15009
   199
  done
obua@15009
   200
nipkow@31816
   201
lemma sorted_spvec_addmult_spvec_helper: "\<lbrakk>sorted_spvec (addmult_spvec y ((a, b) # arr) brr); aa < a; sorted_spvec ((a, b) # arr); 
nipkow@31816
   202
  sorted_spvec ((aa, ba) # brr)\<rbrakk> \<Longrightarrow> sorted_spvec ((aa, y * ba) # addmult_spvec y ((a, b) # arr) brr)"  
obua@15009
   203
  apply (induct brr)
obua@15009
   204
  apply (auto simp add: sorted_spvec.simps)
obua@15009
   205
  done
obua@15009
   206
obua@15009
   207
lemma sorted_spvec_addmult_spvec_helper2: 
nipkow@31816
   208
 "\<lbrakk>sorted_spvec (addmult_spvec y arr ((aa, ba) # brr)); a < aa; sorted_spvec ((a, b) # arr); sorted_spvec ((aa, ba) # brr)\<rbrakk>
nipkow@31816
   209
       \<Longrightarrow> sorted_spvec ((a, b) # addmult_spvec y arr ((aa, ba) # brr))"
obua@15009
   210
  apply (induct arr)
obua@15009
   211
  apply (auto simp add: smult_spvec_def sorted_spvec.simps)
obua@15009
   212
  done
obua@15009
   213
obua@15009
   214
lemma sorted_spvec_addmult_spvec_helper3[rule_format]:
nipkow@31816
   215
  "sorted_spvec (addmult_spvec y arr brr) \<longrightarrow> sorted_spvec ((aa, b) # arr) \<longrightarrow> sorted_spvec ((aa, ba) # brr)
nipkow@31816
   216
     \<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec y arr brr))"
nipkow@31816
   217
  apply (induct y arr brr rule: addmult_spvec.induct)
nipkow@31816
   218
  apply (simp_all add: sorted_spvec.simps smult_spvec_def split:list.split)
obua@15009
   219
  done
obua@15009
   220
nipkow@31816
   221
lemma sorted_addmult_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (addmult_spvec y a b)"
obua@15009
   222
  apply (rule addmult_spvec.induct[of _ y a b])
obua@15009
   223
  apply (simp_all add: sorted_smult_spvec)
obua@15009
   224
  apply (rule conjI, intro strip)
nipkow@31816
   225
  apply (case_tac "~(i < j)")
obua@15009
   226
  apply (simp_all)
obua@15009
   227
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   228
  apply (simp add: sorted_spvec_addmult_spvec_helper)
obua@15009
   229
  apply (intro strip | rule conjI)+
obua@15009
   230
  apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   231
  apply (simp add: sorted_spvec_addmult_spvec_helper2)
obua@15009
   232
  apply (intro strip)
obua@15009
   233
  apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   234
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   235
  apply (simp)
obua@15009
   236
  apply (simp_all add: sorted_spvec_addmult_spvec_helper3)
obua@15009
   237
  done
obua@15009
   238
nipkow@31816
   239
fun mult_spvec_spmat :: "('a::lordered_ring) spvec \<Rightarrow> 'a spvec \<Rightarrow> 'a spmat  \<Rightarrow> 'a spvec" where
nipkow@31816
   240
(* recdef mult_spvec_spmat "measure (% (c, arr, brr). (length arr) + (length brr))" *)
nipkow@31816
   241
  "mult_spvec_spmat c [] brr = c" |
nipkow@31816
   242
  "mult_spvec_spmat c arr [] = c" |
nipkow@31816
   243
  "mult_spvec_spmat c ((i,a)#arr) ((j,b)#brr) = (
nipkow@31816
   244
     if (i < j) then mult_spvec_spmat c arr ((j,b)#brr)
nipkow@31816
   245
     else if (j < i) then mult_spvec_spmat c ((i,a)#arr) brr 
nipkow@31816
   246
     else mult_spvec_spmat (addmult_spvec a c b) arr brr)"
obua@15009
   247
obua@15009
   248
lemma sparse_row_mult_spvec_spmat[rule_format]: "sorted_spvec (a::('a::lordered_ring) spvec) \<longrightarrow> sorted_spvec B \<longrightarrow> 
nipkow@31816
   249
  sparse_row_vector (mult_spvec_spmat c a B) = (sparse_row_vector c) + (sparse_row_vector a) * (sparse_row_matrix B)"
obua@15009
   250
proof -
obua@15009
   251
  have comp_1: "!! a b. a < b \<Longrightarrow> Suc 0 <= nat ((int b)-(int a))" by arith
obua@15009
   252
  have not_iff: "!! a b. a = b \<Longrightarrow> (~ a) = (~ b)" by simp
obua@15009
   253
  have max_helper: "!! a b. ~ (a <= max (Suc a) b) \<Longrightarrow> False"
obua@15009
   254
    by arith
obua@15009
   255
  {
obua@15009
   256
    fix a 
obua@15009
   257
    fix v
obua@15009
   258
    assume a:"a < nrows(sparse_row_vector v)"
obua@15009
   259
    have b:"nrows(sparse_row_vector v) <= 1" by simp
obua@15009
   260
    note dummy = less_le_trans[of a "nrows (sparse_row_vector v)" 1, OF a b]   
obua@15009
   261
    then have "a = 0" by simp
obua@15009
   262
  }
obua@15009
   263
  note nrows_helper = this
obua@15009
   264
  show ?thesis
obua@15009
   265
    apply (rule mult_spvec_spmat.induct)
obua@15009
   266
    apply simp+
obua@15009
   267
    apply (rule conjI)
obua@15009
   268
    apply (intro strip)
obua@15009
   269
    apply (frule_tac as=brr in sorted_spvec_cons1)
nipkow@29667
   270
    apply (simp add: algebra_simps sparse_row_matrix_cons)
paulson@15481
   271
    apply (simplesubst Rep_matrix_zero_imp_mult_zero) 
obua@15009
   272
    apply (simp)
obua@15009
   273
    apply (intro strip)
obua@15009
   274
    apply (rule disjI2)
obua@15009
   275
    apply (intro strip)
obua@15009
   276
    apply (subst nrows)
obua@15009
   277
    apply (rule  order_trans[of _ 1])
obua@15009
   278
    apply (simp add: comp_1)+
obua@15009
   279
    apply (subst Rep_matrix_zero_imp_mult_zero)
obua@15009
   280
    apply (intro strip)
nipkow@31816
   281
    apply (case_tac "k <= j")
nipkow@31816
   282
    apply (rule_tac m1 = k and n1 = i and a1 = a in ssubst[OF sorted_sparse_row_vector_zero])
obua@15009
   283
    apply (simp_all)
obua@15009
   284
    apply (rule impI)
obua@15009
   285
    apply (rule disjI2)
obua@15009
   286
    apply (rule nrows)
obua@15009
   287
    apply (rule order_trans[of _ 1])
obua@15009
   288
    apply (simp_all add: comp_1)
obua@15009
   289
    
obua@15009
   290
    apply (intro strip | rule conjI)+
obua@15009
   291
    apply (frule_tac as=arr in sorted_spvec_cons1)
nipkow@29667
   292
    apply (simp add: algebra_simps)
obua@15009
   293
    apply (subst Rep_matrix_zero_imp_mult_zero)
obua@15009
   294
    apply (simp)
obua@15009
   295
    apply (rule disjI2)
obua@15009
   296
    apply (intro strip)
obua@15009
   297
    apply (simp add: sparse_row_matrix_cons neg_def)
nipkow@31816
   298
    apply (case_tac "i <= j")  
obua@15009
   299
    apply (erule sorted_sparse_row_matrix_zero)  
obua@15009
   300
    apply (simp_all)
obua@15009
   301
    apply (intro strip)
nipkow@31816
   302
    apply (case_tac "i=j")
obua@15009
   303
    apply (simp_all)
obua@15009
   304
    apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   305
    apply (frule_tac as=brr in sorted_spvec_cons1)
nipkow@29667
   306
    apply (simp add: sparse_row_matrix_cons algebra_simps sparse_row_vector_addmult_spvec)
obua@15009
   307
    apply (rule_tac B1 = "sparse_row_matrix brr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
obua@15009
   308
    apply (auto)
obua@15009
   309
    apply (rule sorted_sparse_row_matrix_zero)
obua@15009
   310
    apply (simp_all)
obua@15009
   311
    apply (rule_tac A1 = "sparse_row_vector arr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
obua@15009
   312
    apply (auto)
nipkow@31816
   313
    apply (rule_tac m=k and n = j and a = a and arr=arr in sorted_sparse_row_vector_zero)
obua@15009
   314
    apply (simp_all)
obua@15009
   315
    apply (simp add: neg_def)
obua@15009
   316
    apply (drule nrows_notzero)
obua@15009
   317
    apply (drule nrows_helper)
obua@15009
   318
    apply (arith)
obua@15009
   319
    
obua@15009
   320
    apply (subst Rep_matrix_inject[symmetric])
obua@15009
   321
    apply (rule ext)+
obua@15009
   322
    apply (simp)
obua@15009
   323
    apply (subst Rep_matrix_mult)
nipkow@31816
   324
    apply (rule_tac j1=j in ssubst[OF foldseq_almostzero])
obua@15009
   325
    apply (simp_all)
webertj@20432
   326
    apply (intro strip, rule conjI)
obua@15009
   327
    apply (intro strip)
webertj@20432
   328
    apply (drule_tac max_helper)
webertj@20432
   329
    apply (simp)
webertj@20432
   330
    apply (auto)
obua@15009
   331
    apply (rule zero_imp_mult_zero)
obua@15009
   332
    apply (rule disjI2)
obua@15009
   333
    apply (rule nrows)
obua@15009
   334
    apply (rule order_trans[of _ 1])
webertj@20432
   335
    apply (simp)
webertj@20432
   336
    apply (simp)
obua@15009
   337
    done
obua@15009
   338
qed
obua@15009
   339
obua@15009
   340
lemma sorted_mult_spvec_spmat[rule_format]: 
nipkow@31816
   341
  "sorted_spvec (c::('a::lordered_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat c a B)"
obua@15009
   342
  apply (rule mult_spvec_spmat.induct[of _ c a B])
obua@15009
   343
  apply (simp_all add: sorted_addmult_spvec)
obua@15009
   344
  done
obua@15009
   345
obua@15009
   346
consts 
obua@15009
   347
  mult_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
obua@15009
   348
obua@15009
   349
primrec 
obua@15009
   350
  "mult_spmat [] A = []"
nipkow@31816
   351
  "mult_spmat (a#as) A = (fst a, mult_spvec_spmat [] (snd a) A)#(mult_spmat as A)"
obua@15009
   352
obua@15009
   353
lemma sparse_row_mult_spmat[rule_format]: 
obua@15009
   354
  "sorted_spmat A \<longrightarrow> sorted_spvec B \<longrightarrow> sparse_row_matrix (mult_spmat A B) = (sparse_row_matrix A) * (sparse_row_matrix B)"
obua@15009
   355
  apply (induct A)
nipkow@29667
   356
  apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat algebra_simps move_matrix_mult)
obua@15009
   357
  done
obua@15009
   358
obua@15009
   359
lemma sorted_spvec_mult_spmat[rule_format]:
obua@15009
   360
  "sorted_spvec (A::('a::lordered_ring) spmat) \<longrightarrow> sorted_spvec (mult_spmat A B)"
obua@15009
   361
  apply (induct A)
obua@15009
   362
  apply (auto)
obua@15009
   363
  apply (drule sorted_spvec_cons1, simp)
nipkow@15236
   364
  apply (case_tac A)
obua@15009
   365
  apply (auto simp add: sorted_spvec.simps)
obua@15009
   366
  done
obua@15009
   367
obua@15009
   368
lemma sorted_spmat_mult_spmat[rule_format]:
obua@15009
   369
  "sorted_spmat (B::('a::lordered_ring) spmat) \<longrightarrow> sorted_spmat (mult_spmat A B)"
obua@15009
   370
  apply (induct A)
obua@15009
   371
  apply (auto simp add: sorted_mult_spvec_spmat) 
obua@15009
   372
  done
obua@15009
   373
obua@15009
   374
nipkow@31816
   375
fun add_spvec :: "('a::lordered_ab_group_add) spvec \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec" where
nipkow@31816
   376
(* "measure (% (a, b). length a + (length b))" *)
nipkow@31816
   377
  "add_spvec arr [] = arr" |
nipkow@31816
   378
  "add_spvec [] brr = brr" |
nipkow@31816
   379
  "add_spvec ((i,a)#arr) ((j,b)#brr) = (
nipkow@31816
   380
  if i < j then (i,a)#(add_spvec arr ((j,b)#brr)) 
nipkow@31816
   381
     else if (j < i) then (j,b) # add_spvec ((i,a)#arr) brr
nipkow@31816
   382
     else (i, a+b) # add_spvec arr brr)"
obua@15009
   383
nipkow@31816
   384
lemma add_spvec_empty1[simp]: "add_spvec [] a = a"
nipkow@31816
   385
by (cases a, auto)
obua@15009
   386
nipkow@31816
   387
lemma sparse_row_vector_add: "sparse_row_vector (add_spvec a b) = (sparse_row_vector a) + (sparse_row_vector b)"
obua@15009
   388
  apply (rule add_spvec.induct[of _ a b])
obua@15009
   389
  apply (simp_all add: singleton_matrix_add)
obua@15009
   390
  done
obua@15009
   391
nipkow@31816
   392
fun add_spmat :: "('a::lordered_ab_group_add) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat" where
nipkow@31816
   393
(* "measure (% (A,B). (length A)+(length B))" *)
nipkow@31816
   394
  "add_spmat [] bs = bs" |
nipkow@31816
   395
  "add_spmat as [] = as" |
nipkow@31816
   396
  "add_spmat ((i,a)#as) ((j,b)#bs) = (
nipkow@31816
   397
  if i < j then 
nipkow@31816
   398
    (i,a) # add_spmat as ((j,b)#bs)
nipkow@31816
   399
  else if j < i then
nipkow@31816
   400
    (j,b) # add_spmat ((i,a)#as) bs
obua@15009
   401
  else
nipkow@31816
   402
    (i, add_spvec a b) # add_spmat as bs)"
obua@15009
   403
nipkow@31816
   404
lemma add_spmat_Nil2[simp]: "add_spmat as [] = as"
nipkow@31816
   405
by(cases as) auto
nipkow@31816
   406
nipkow@31816
   407
lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat A B) = (sparse_row_matrix A) + (sparse_row_matrix B)"
obua@15009
   408
  apply (rule add_spmat.induct)
obua@15009
   409
  apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add)
obua@15009
   410
  done
obua@15009
   411
haftmann@28562
   412
lemmas [code] = sparse_row_add_spmat [symmetric]
haftmann@28562
   413
lemmas [code] = sparse_row_vector_add [symmetric]
haftmann@27484
   414
nipkow@31816
   415
lemma sorted_add_spvec_helper1[rule_format]: "add_spvec ((a,b)#arr) brr = (ab, bb) # list \<longrightarrow> (ab = a | (brr \<noteq> [] & ab = fst (hd brr)))"
obua@15009
   416
  proof - 
nipkow@31816
   417
    have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spvec x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))"
nipkow@31816
   418
      by (rule add_spvec.induct[of _ _ brr]) (auto split:if_splits)
obua@15009
   419
    then show ?thesis
obua@15009
   420
      by (case_tac brr, auto)
obua@15009
   421
  qed
obua@15009
   422
nipkow@31816
   423
lemma sorted_add_spmat_helper1[rule_format]: "add_spmat ((a,b)#arr) brr = (ab, bb) # list \<longrightarrow> (ab = a | (brr \<noteq> [] & ab = fst (hd brr)))"
obua@15009
   424
  proof - 
nipkow@31816
   425
    have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spmat x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))"
nipkow@31816
   426
      by (rule add_spmat.induct[of _ _ brr], auto split:if_splits)
obua@15009
   427
    then show ?thesis
obua@15009
   428
      by (case_tac brr, auto)
obua@15009
   429
  qed
obua@15009
   430
nipkow@31816
   431
lemma sorted_add_spvec_helper[rule_format]: "add_spvec arr brr = (ab, bb) # list \<longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))"
obua@15009
   432
  apply (rule add_spvec.induct[of _ arr brr])
obua@15009
   433
  apply (auto)
obua@15009
   434
  done
obua@15009
   435
nipkow@31816
   436
lemma sorted_add_spmat_helper[rule_format]: "add_spmat arr brr = (ab, bb) # list \<longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))"
obua@15009
   437
  apply (rule add_spmat.induct[of _ arr brr])
obua@15009
   438
  apply (auto)
obua@15009
   439
  done
obua@15009
   440
nipkow@31816
   441
lemma add_spvec_commute: "add_spvec a b = add_spvec b a"
obua@15009
   442
  by (rule add_spvec.induct[of _ a b], auto)
obua@15009
   443
nipkow@31816
   444
lemma add_spmat_commute: "add_spmat a b = add_spmat b a"
obua@15009
   445
  apply (rule add_spmat.induct[of _ a b])
obua@15009
   446
  apply (simp_all add: add_spvec_commute)
obua@15009
   447
  done
obua@15009
   448
  
nipkow@31816
   449
lemma sorted_add_spvec_helper2: "add_spvec ((a,b)#arr) brr = (ab, bb) # list \<Longrightarrow> aa < a \<Longrightarrow> sorted_spvec ((aa, ba) # brr) \<Longrightarrow> aa < ab"
obua@15009
   450
  apply (drule sorted_add_spvec_helper1)
obua@15009
   451
  apply (auto)
obua@15009
   452
  apply (case_tac brr)
obua@15009
   453
  apply (simp_all)
obua@15009
   454
  apply (drule_tac sorted_spvec_cons3)
obua@15009
   455
  apply (simp)
obua@15009
   456
  done
obua@15009
   457
nipkow@31816
   458
lemma sorted_add_spmat_helper2: "add_spmat ((a,b)#arr) brr = (ab, bb) # list \<Longrightarrow> aa < a \<Longrightarrow> sorted_spvec ((aa, ba) # brr) \<Longrightarrow> aa < ab"
obua@15009
   459
  apply (drule sorted_add_spmat_helper1)
obua@15009
   460
  apply (auto)
obua@15009
   461
  apply (case_tac brr)
obua@15009
   462
  apply (simp_all)
obua@15009
   463
  apply (drule_tac sorted_spvec_cons3)
obua@15009
   464
  apply (simp)
obua@15009
   465
  done
obua@15009
   466
nipkow@31816
   467
lemma sorted_spvec_add_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (add_spvec a b)"
obua@15009
   468
  apply (rule add_spvec.induct[of _ a b])
obua@15009
   469
  apply (simp_all)
obua@15009
   470
  apply (rule conjI)
nipkow@31816
   471
  apply (clarsimp)
obua@15009
   472
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   473
  apply (simp)
obua@15009
   474
  apply (subst sorted_spvec_step)
nipkow@31816
   475
  apply (clarsimp simp: sorted_add_spvec_helper2 split: list.split)
obua@15009
   476
  apply (clarify)
obua@15009
   477
  apply (rule conjI)
obua@15009
   478
  apply (clarify)
obua@15009
   479
  apply (frule_tac as=arr in sorted_spvec_cons1, simp)
obua@15009
   480
  apply (subst sorted_spvec_step)
nipkow@31816
   481
  apply (clarsimp simp: sorted_add_spvec_helper2 add_spvec_commute split: list.split)
obua@15009
   482
  apply (clarify)
obua@15009
   483
  apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   484
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   485
  apply (simp)
obua@15009
   486
  apply (subst sorted_spvec_step)
obua@15009
   487
  apply (simp split: list.split)
nipkow@31816
   488
  apply (clarsimp)
obua@15009
   489
  apply (drule_tac sorted_add_spvec_helper)
nipkow@31816
   490
  apply (auto simp: neq_Nil_conv)
obua@15009
   491
  apply (drule sorted_spvec_cons3)
obua@15009
   492
  apply (simp)
obua@15009
   493
  apply (drule sorted_spvec_cons3)
obua@15009
   494
  apply (simp)
obua@15009
   495
  done
obua@15009
   496
nipkow@31816
   497
lemma sorted_spvec_add_spmat[rule_format]: "sorted_spvec A \<longrightarrow> sorted_spvec B \<longrightarrow> sorted_spvec (add_spmat A B)"
obua@15009
   498
  apply (rule add_spmat.induct[of _ A B])
obua@15009
   499
  apply (simp_all)
obua@15009
   500
  apply (rule conjI)
obua@15009
   501
  apply (intro strip)
obua@15009
   502
  apply (simp)
obua@15009
   503
  apply (frule_tac as=bs in sorted_spvec_cons1)
obua@15009
   504
  apply (simp)
obua@15009
   505
  apply (subst sorted_spvec_step)
obua@15009
   506
  apply (simp split: list.split)
obua@15009
   507
  apply (clarify, simp)
obua@15009
   508
  apply (simp add: sorted_add_spmat_helper2)
obua@15009
   509
  apply (clarify)
obua@15009
   510
  apply (rule conjI)
obua@15009
   511
  apply (clarify)
obua@15009
   512
  apply (frule_tac as=as in sorted_spvec_cons1, simp)
obua@15009
   513
  apply (subst sorted_spvec_step)
nipkow@31816
   514
  apply (clarsimp simp: sorted_add_spmat_helper2 add_spmat_commute split: list.split)
nipkow@31816
   515
  apply (clarsimp)
obua@15009
   516
  apply (frule_tac as=as in sorted_spvec_cons1)
obua@15009
   517
  apply (frule_tac as=bs in sorted_spvec_cons1)
obua@15009
   518
  apply (simp)
obua@15009
   519
  apply (subst sorted_spvec_step)
obua@15009
   520
  apply (simp split: list.split)
obua@15009
   521
  apply (clarify, simp)
obua@15009
   522
  apply (drule_tac sorted_add_spmat_helper)
nipkow@31816
   523
  apply (auto simp:neq_Nil_conv)
obua@15009
   524
  apply (drule sorted_spvec_cons3)
obua@15009
   525
  apply (simp)
obua@15009
   526
  apply (drule sorted_spvec_cons3)
obua@15009
   527
  apply (simp)
obua@15009
   528
  done
obua@15009
   529
nipkow@31816
   530
lemma sorted_spmat_add_spmat[rule_format]: "sorted_spmat A \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spmat (add_spmat A B)"
obua@15009
   531
  apply (rule add_spmat.induct[of _ A B])
obua@15009
   532
  apply (simp_all add: sorted_spvec_add_spvec)
obua@15009
   533
  done
obua@15009
   534
nipkow@31816
   535
fun le_spvec :: "('a::lordered_ab_group_add) spvec \<Rightarrow> 'a spvec \<Rightarrow> bool" where
nipkow@31816
   536
(* "measure (% (a,b). (length a) + (length b))" *)
nipkow@31816
   537
  "le_spvec [] [] = True" |
nipkow@31816
   538
  "le_spvec ((_,a)#as) [] = (a <= 0 & le_spvec as [])" |
nipkow@31816
   539
  "le_spvec [] ((_,b)#bs) = (0 <= b & le_spvec [] bs)" |
nipkow@31816
   540
  "le_spvec ((i,a)#as) ((j,b)#bs) = (
nipkow@31816
   541
  if (i < j) then a <= 0 & le_spvec as ((j,b)#bs)
nipkow@31816
   542
  else if (j < i) then 0 <= b & le_spvec ((i,a)#as) bs
nipkow@31816
   543
  else a <= b & le_spvec as bs)"
obua@15009
   544
nipkow@31816
   545
fun le_spmat :: "('a::lordered_ab_group_add) spmat \<Rightarrow> 'a spmat \<Rightarrow> bool" where
nipkow@31816
   546
(* "measure (% (a,b). (length a) + (length b))" *)
nipkow@31816
   547
  "le_spmat [] [] = True" |
nipkow@31816
   548
  "le_spmat ((i,a)#as) [] = (le_spvec a [] & le_spmat as [])" |
nipkow@31816
   549
  "le_spmat [] ((j,b)#bs) = (le_spvec [] b & le_spmat [] bs)" |
nipkow@31816
   550
  "le_spmat ((i,a)#as) ((j,b)#bs) = (
nipkow@31816
   551
  if i < j then (le_spvec a [] & le_spmat as ((j,b)#bs))
nipkow@31816
   552
  else if j < i then (le_spvec [] b & le_spmat ((i,a)#as) bs)
nipkow@31816
   553
  else (le_spvec a b & le_spmat as bs))"
obua@15009
   554
obua@15009
   555
constdefs
obua@15009
   556
  disj_matrices :: "('a::zero) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@15009
   557
  "disj_matrices A B == (! j i. (Rep_matrix A j i \<noteq> 0) \<longrightarrow> (Rep_matrix B j i = 0)) & (! j i. (Rep_matrix B j i \<noteq> 0) \<longrightarrow> (Rep_matrix A j i = 0))"  
obua@15009
   558
wenzelm@24124
   559
declare [[simp_depth_limit = 6]]
obua@15009
   560
obua@15580
   561
lemma disj_matrices_contr1: "disj_matrices A B \<Longrightarrow> Rep_matrix A j i \<noteq> 0 \<Longrightarrow> Rep_matrix B j i = 0"
obua@15580
   562
   by (simp add: disj_matrices_def)
obua@15580
   563
obua@15580
   564
lemma disj_matrices_contr2: "disj_matrices A B \<Longrightarrow> Rep_matrix B j i \<noteq> 0 \<Longrightarrow> Rep_matrix A j i = 0"
obua@15580
   565
   by (simp add: disj_matrices_def)
obua@15580
   566
obua@15580
   567
obua@15009
   568
lemma disj_matrices_add: "disj_matrices A B \<Longrightarrow> disj_matrices C D \<Longrightarrow> disj_matrices A D \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
haftmann@25303
   569
  (A + B <= C + D) = (A <= C & B <= (D::('a::lordered_ab_group_add) matrix))"
obua@15009
   570
  apply (auto)
obua@15009
   571
  apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
obua@15009
   572
  apply (intro strip)
obua@15009
   573
  apply (erule conjE)+
obua@15009
   574
  apply (drule_tac j=j and i=i in spec2)+
obua@15009
   575
  apply (case_tac "Rep_matrix B j i = 0")
obua@15009
   576
  apply (case_tac "Rep_matrix D j i = 0")
obua@15009
   577
  apply (simp_all)
obua@15009
   578
  apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
obua@15009
   579
  apply (intro strip)
obua@15009
   580
  apply (erule conjE)+
obua@15009
   581
  apply (drule_tac j=j and i=i in spec2)+
obua@15009
   582
  apply (case_tac "Rep_matrix A j i = 0")
obua@15009
   583
  apply (case_tac "Rep_matrix C j i = 0")
obua@15009
   584
  apply (simp_all)
obua@15009
   585
  apply (erule add_mono)
obua@15009
   586
  apply (assumption)
obua@15009
   587
  done
obua@15009
   588
obua@15009
   589
lemma disj_matrices_zero1[simp]: "disj_matrices 0 B"
obua@15009
   590
by (simp add: disj_matrices_def)
obua@15009
   591
obua@15009
   592
lemma disj_matrices_zero2[simp]: "disj_matrices A 0"
obua@15009
   593
by (simp add: disj_matrices_def)
obua@15009
   594
obua@15009
   595
lemma disj_matrices_commute: "disj_matrices A B = disj_matrices B A"
obua@15009
   596
by (auto simp add: disj_matrices_def)
obua@15009
   597
obua@15009
   598
lemma disj_matrices_add_le_zero: "disj_matrices A B \<Longrightarrow>
haftmann@25303
   599
  (A + B <= 0) = (A <= 0 & (B::('a::lordered_ab_group_add) matrix) <= 0)"
obua@15009
   600
by (rule disj_matrices_add[of A B 0 0, simplified])
obua@15009
   601
 
obua@15009
   602
lemma disj_matrices_add_zero_le: "disj_matrices A B \<Longrightarrow>
haftmann@25303
   603
  (0 <= A + B) = (0 <= A & 0 <= (B::('a::lordered_ab_group_add) matrix))"
obua@15009
   604
by (rule disj_matrices_add[of 0 0 A B, simplified])
obua@15009
   605
obua@15009
   606
lemma disj_matrices_add_x_le: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
haftmann@25303
   607
  (A <= B + C) = (A <= C & 0 <= (B::('a::lordered_ab_group_add) matrix))"
obua@15009
   608
by (auto simp add: disj_matrices_add[of 0 A B C, simplified])
obua@15009
   609
obua@15009
   610
lemma disj_matrices_add_le_x: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
haftmann@25303
   611
  (B + A <= C) = (A <= C &  (B::('a::lordered_ab_group_add) matrix) <= 0)"
obua@15009
   612
by (auto simp add: disj_matrices_add[of B A 0 C,simplified] disj_matrices_commute)
obua@15009
   613
obua@15009
   614
lemma disj_sparse_row_singleton: "i <= j \<Longrightarrow> sorted_spvec((j,y)#v) \<Longrightarrow> disj_matrices (sparse_row_vector v) (singleton_matrix 0 i x)"
obua@15009
   615
  apply (simp add: disj_matrices_def)
obua@15009
   616
  apply (rule conjI)
obua@15009
   617
  apply (rule neg_imp)
obua@15009
   618
  apply (simp)
obua@15009
   619
  apply (intro strip)
obua@15009
   620
  apply (rule sorted_sparse_row_vector_zero)
obua@15009
   621
  apply (simp_all)
obua@15009
   622
  apply (intro strip)
obua@15009
   623
  apply (rule sorted_sparse_row_vector_zero)
obua@15009
   624
  apply (simp_all)
obua@15009
   625
  done 
obua@15009
   626
haftmann@25303
   627
lemma disj_matrices_x_add: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (A::('a::lordered_ab_group_add) matrix) (B+C)"
obua@15009
   628
  apply (simp add: disj_matrices_def)
obua@15009
   629
  apply (auto)
obua@15009
   630
  apply (drule_tac j=j and i=i in spec2)+
obua@15009
   631
  apply (case_tac "Rep_matrix B j i = 0")
obua@15009
   632
  apply (case_tac "Rep_matrix C j i = 0")
obua@15009
   633
  apply (simp_all)
obua@15009
   634
  done
obua@15009
   635
haftmann@25303
   636
lemma disj_matrices_add_x: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (B+C) (A::('a::lordered_ab_group_add) matrix)" 
obua@15009
   637
  by (simp add: disj_matrices_x_add disj_matrices_commute)
obua@15009
   638
obua@15009
   639
lemma disj_singleton_matrices[simp]: "disj_matrices (singleton_matrix j i x) (singleton_matrix u v y) = (j \<noteq> u | i \<noteq> v | x = 0 | y = 0)" 
obua@15009
   640
  by (auto simp add: disj_matrices_def)
obua@15009
   641
obua@15009
   642
lemma disj_move_sparse_vec_mat[simplified disj_matrices_commute]: 
obua@15009
   643
  "j <= a \<Longrightarrow> sorted_spvec((a,c)#as) \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector b) (int j) i) (sparse_row_matrix as)"
obua@15009
   644
  apply (auto simp add: neg_def disj_matrices_def)
obua@15009
   645
  apply (drule nrows_notzero)
obua@15009
   646
  apply (drule less_le_trans[OF _ nrows_spvec])
obua@15009
   647
  apply (subgoal_tac "ja = j")
obua@15009
   648
  apply (simp add: sorted_sparse_row_matrix_zero)
obua@15009
   649
  apply (arith)
obua@15009
   650
  apply (rule nrows)
obua@15009
   651
  apply (rule order_trans[of _ 1 _])
obua@15009
   652
  apply (simp)
obua@15009
   653
  apply (case_tac "nat (int ja - int j) = 0")
obua@15009
   654
  apply (case_tac "ja = j")
obua@15009
   655
  apply (simp add: sorted_sparse_row_matrix_zero)
obua@15009
   656
  apply arith+
obua@15009
   657
  done
obua@15009
   658
obua@15009
   659
lemma disj_move_sparse_row_vector_twice:
obua@15009
   660
  "j \<noteq> u \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector a) j i) (move_matrix (sparse_row_vector b) u v)"
obua@15009
   661
  apply (auto simp add: neg_def disj_matrices_def)
obua@15009
   662
  apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+
obua@15009
   663
  done
obua@15009
   664
nipkow@31816
   665
lemma le_spvec_iff_sparse_row_le[rule_format]: "(sorted_spvec a) \<longrightarrow> (sorted_spvec b) \<longrightarrow> (le_spvec a b) = (sparse_row_vector a <= sparse_row_vector b)"
obua@15178
   666
  apply (rule le_spvec.induct)
obua@15178
   667
  apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le 
obua@15178
   668
    disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
obua@15178
   669
  apply (rule conjI, intro strip)
obua@15178
   670
  apply (simp add: sorted_spvec_cons1)
obua@15178
   671
  apply (subst disj_matrices_add_x_le)
obua@15178
   672
  apply (simp add: disj_sparse_row_singleton[OF less_imp_le] disj_matrices_x_add disj_matrices_commute)
obua@15178
   673
  apply (simp add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
obua@15178
   674
  apply (simp, blast)
obua@15178
   675
  apply (intro strip, rule conjI, intro strip)
obua@15178
   676
  apply (simp add: sorted_spvec_cons1)
obua@15178
   677
  apply (subst disj_matrices_add_le_x)
obua@15178
   678
  apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_sparse_row_singleton[OF less_imp_le] disj_matrices_commute disj_matrices_x_add)
obua@15178
   679
  apply (blast)
obua@15178
   680
  apply (intro strip)
obua@15178
   681
  apply (simp add: sorted_spvec_cons1)
nipkow@31816
   682
  apply (case_tac "a=b", simp_all)
obua@15178
   683
  apply (subst disj_matrices_add)
obua@15178
   684
  apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
obua@15009
   685
  done
obua@15009
   686
nipkow@31816
   687
lemma le_spvec_empty2_sparse_row[rule_format]: "sorted_spvec b \<longrightarrow> le_spvec b [] = (sparse_row_vector b <= 0)"
obua@15009
   688
  apply (induct b)
obua@15009
   689
  apply (simp_all add: sorted_spvec_cons1)
obua@15009
   690
  apply (intro strip)
obua@15009
   691
  apply (subst disj_matrices_add_le_zero)
nipkow@31816
   692
  apply (auto simp add: disj_matrices_commute disj_sparse_row_singleton[OF order_refl] sorted_spvec_cons1)
obua@15009
   693
  done
obua@15009
   694
nipkow@31816
   695
lemma le_spvec_empty1_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec [] b = (0 <= sparse_row_vector b))"
obua@15009
   696
  apply (induct b)
obua@15009
   697
  apply (simp_all add: sorted_spvec_cons1)
obua@15009
   698
  apply (intro strip)
obua@15009
   699
  apply (subst disj_matrices_add_zero_le)
nipkow@31816
   700
  apply (auto simp add: disj_matrices_commute disj_sparse_row_singleton[OF order_refl] sorted_spvec_cons1)
obua@15009
   701
  done
obua@15009
   702
obua@15009
   703
lemma le_spmat_iff_sparse_row_le[rule_format]: "(sorted_spvec A) \<longrightarrow> (sorted_spmat A) \<longrightarrow> (sorted_spvec B) \<longrightarrow> (sorted_spmat B) \<longrightarrow> 
nipkow@31816
   704
  le_spmat A B = (sparse_row_matrix A <= sparse_row_matrix B)"
obua@15009
   705
  apply (rule le_spmat.induct)
obua@15009
   706
  apply (simp add: sparse_row_matrix_cons disj_matrices_add_le_zero disj_matrices_add_zero_le disj_move_sparse_vec_mat[OF order_refl] 
obua@15009
   707
    disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+ 
obua@15009
   708
  apply (rule conjI, intro strip)
obua@15009
   709
  apply (simp add: sorted_spvec_cons1)
obua@15009
   710
  apply (subst disj_matrices_add_x_le)
obua@15009
   711
  apply (rule disj_matrices_add_x)
obua@15009
   712
  apply (simp add: disj_move_sparse_row_vector_twice)
obua@15009
   713
  apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
obua@15009
   714
  apply (simp add: disj_move_sparse_vec_mat[OF order_refl] disj_matrices_commute)
obua@15009
   715
  apply (simp, blast)
obua@15009
   716
  apply (intro strip, rule conjI, intro strip)
obua@15009
   717
  apply (simp add: sorted_spvec_cons1)
obua@15009
   718
  apply (subst disj_matrices_add_le_x)
obua@15009
   719
  apply (simp add: disj_move_sparse_vec_mat[OF order_refl])
obua@15009
   720
  apply (rule disj_matrices_x_add)
obua@15009
   721
  apply (simp add: disj_move_sparse_row_vector_twice)
obua@15009
   722
  apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
obua@15009
   723
  apply (simp, blast)
obua@15009
   724
  apply (intro strip)
nipkow@31816
   725
  apply (case_tac "i=j")
obua@15009
   726
  apply (simp_all)
obua@15009
   727
  apply (subst disj_matrices_add)
obua@15009
   728
  apply (simp_all add: disj_matrices_commute disj_move_sparse_vec_mat[OF order_refl])
obua@15009
   729
  apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le)
obua@15009
   730
  done
obua@15009
   731
wenzelm@24124
   732
declare [[simp_depth_limit = 999]]
obua@15178
   733
obua@15178
   734
consts 
obua@15178
   735
   abs_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat"
obua@15178
   736
   minus_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat"
obua@15178
   737
obua@15178
   738
primrec
obua@15178
   739
  "abs_spmat [] = []"
obua@15178
   740
  "abs_spmat (a#as) = (fst a, abs_spvec (snd a))#(abs_spmat as)"
obua@15178
   741
obua@15178
   742
primrec
obua@15178
   743
  "minus_spmat [] = []"
obua@15178
   744
  "minus_spmat (a#as) = (fst a, minus_spvec (snd a))#(minus_spmat as)"
obua@15178
   745
obua@15178
   746
lemma sparse_row_matrix_minus:
obua@15178
   747
  "sparse_row_matrix (minus_spmat A) = - (sparse_row_matrix A)"
obua@15178
   748
  apply (induct A)
obua@15178
   749
  apply (simp_all add: sparse_row_vector_minus sparse_row_matrix_cons)
obua@15178
   750
  apply (subst Rep_matrix_inject[symmetric])
obua@15178
   751
  apply (rule ext)+
obua@15178
   752
  apply simp
obua@15178
   753
  done
obua@15009
   754
obua@15178
   755
lemma Rep_sparse_row_vector_zero: "x \<noteq> 0 \<Longrightarrow> Rep_matrix (sparse_row_vector v) x y = 0"
obua@15178
   756
proof -
obua@15178
   757
  assume x:"x \<noteq> 0"
obua@15178
   758
  have r:"nrows (sparse_row_vector v) <= Suc 0" by (rule nrows_spvec)
obua@15178
   759
  show ?thesis
obua@15178
   760
    apply (rule nrows)
obua@15178
   761
    apply (subgoal_tac "Suc 0 <= x")
obua@15178
   762
    apply (insert r)
obua@15178
   763
    apply (simp only:)
obua@15178
   764
    apply (insert x)
obua@15178
   765
    apply arith
obua@15178
   766
    done
obua@15178
   767
qed
obua@15178
   768
    
obua@15178
   769
lemma sparse_row_matrix_abs:
obua@15178
   770
  "sorted_spvec A \<Longrightarrow> sorted_spmat A \<Longrightarrow> sparse_row_matrix (abs_spmat A) = abs (sparse_row_matrix A)"
obua@15178
   771
  apply (induct A)
obua@15178
   772
  apply (simp_all add: sparse_row_vector_abs sparse_row_matrix_cons)
obua@15178
   773
  apply (frule_tac sorted_spvec_cons1, simp)
obua@15580
   774
  apply (simplesubst Rep_matrix_inject[symmetric])
obua@15178
   775
  apply (rule ext)+
obua@15178
   776
  apply auto
obua@15178
   777
  apply (case_tac "x=a")
obua@15178
   778
  apply (simp)
paulson@15481
   779
  apply (simplesubst sorted_sparse_row_matrix_zero)
obua@15178
   780
  apply auto
paulson@15481
   781
  apply (simplesubst Rep_sparse_row_vector_zero)
obua@15178
   782
  apply (simp_all add: neg_def)
obua@15178
   783
  done
obua@15178
   784
obua@15178
   785
lemma sorted_spvec_minus_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (minus_spmat A)"
obua@15178
   786
  apply (induct A)
obua@15178
   787
  apply (simp)
obua@15178
   788
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   789
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   790
  done 
obua@15178
   791
obua@15178
   792
lemma sorted_spvec_abs_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (abs_spmat A)" 
obua@15178
   793
  apply (induct A)
obua@15178
   794
  apply (simp)
obua@15178
   795
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   796
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   797
  done
obua@15178
   798
obua@15178
   799
lemma sorted_spmat_minus_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (minus_spmat A)"
obua@15178
   800
  apply (induct A)
obua@15178
   801
  apply (simp_all add: sorted_spvec_minus_spvec)
obua@15178
   802
  done
obua@15178
   803
obua@15178
   804
lemma sorted_spmat_abs_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (abs_spmat A)"
obua@15178
   805
  apply (induct A)
obua@15178
   806
  apply (simp_all add: sorted_spvec_abs_spvec)
obua@15178
   807
  done
obua@15009
   808
obua@15178
   809
constdefs
obua@15178
   810
  diff_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
nipkow@31816
   811
  "diff_spmat A B == add_spmat A (minus_spmat B)"
obua@15178
   812
obua@15178
   813
lemma sorted_spmat_diff_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat B \<Longrightarrow> sorted_spmat (diff_spmat A B)"
obua@15178
   814
  by (simp add: diff_spmat_def sorted_spmat_minus_spmat sorted_spmat_add_spmat)
obua@15178
   815
obua@15178
   816
lemma sorted_spvec_diff_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec B \<Longrightarrow> sorted_spvec (diff_spmat A B)"
obua@15178
   817
  by (simp add: diff_spmat_def sorted_spvec_minus_spmat sorted_spvec_add_spmat)
obua@15178
   818
obua@15178
   819
lemma sparse_row_diff_spmat: "sparse_row_matrix (diff_spmat A B ) = (sparse_row_matrix A) - (sparse_row_matrix B)"
obua@15178
   820
  by (simp add: diff_spmat_def sparse_row_add_spmat sparse_row_matrix_minus)
obua@15178
   821
obua@15178
   822
constdefs
obua@15178
   823
  sorted_sparse_matrix :: "'a spmat \<Rightarrow> bool"
obua@15178
   824
  "sorted_sparse_matrix A == (sorted_spvec A) & (sorted_spmat A)"
obua@15178
   825
obua@15178
   826
lemma sorted_sparse_matrix_imp_spvec: "sorted_sparse_matrix A \<Longrightarrow> sorted_spvec A"
obua@15178
   827
  by (simp add: sorted_sparse_matrix_def)
obua@15178
   828
obua@15178
   829
lemma sorted_sparse_matrix_imp_spmat: "sorted_sparse_matrix A \<Longrightarrow> sorted_spmat A"
obua@15178
   830
  by (simp add: sorted_sparse_matrix_def)
obua@15178
   831
obua@15178
   832
lemmas sorted_sp_simps = 
obua@15178
   833
  sorted_spvec.simps
obua@15178
   834
  sorted_spmat.simps
obua@15178
   835
  sorted_sparse_matrix_def
obua@15178
   836
obua@15178
   837
lemma bool1: "(\<not> True) = False"  by blast
obua@15178
   838
lemma bool2: "(\<not> False) = True"  by blast
obua@15178
   839
lemma bool3: "((P\<Colon>bool) \<and> True) = P" by blast
obua@15178
   840
lemma bool4: "(True \<and> (P\<Colon>bool)) = P" by blast
obua@15178
   841
lemma bool5: "((P\<Colon>bool) \<and> False) = False" by blast
obua@15178
   842
lemma bool6: "(False \<and> (P\<Colon>bool)) = False" by blast
obua@15178
   843
lemma bool7: "((P\<Colon>bool) \<or> True) = True" by blast
obua@15178
   844
lemma bool8: "(True \<or> (P\<Colon>bool)) = True" by blast
obua@15178
   845
lemma bool9: "((P\<Colon>bool) \<or> False) = P" by blast
obua@15178
   846
lemma bool10: "(False \<or> (P\<Colon>bool)) = P" by blast
obua@15178
   847
lemmas boolarith = bool1 bool2 bool3 bool4 bool5 bool6 bool7 bool8 bool9 bool10
obua@15178
   848
obua@15178
   849
lemma if_case_eq: "(if b then x else y) = (case b of True => x | False => y)" by simp
obua@15178
   850
obua@15580
   851
consts
haftmann@25303
   852
  pprt_spvec :: "('a::{lordered_ab_group_add}) spvec \<Rightarrow> 'a spvec"
haftmann@25303
   853
  nprt_spvec :: "('a::{lordered_ab_group_add}) spvec \<Rightarrow> 'a spvec"
haftmann@25303
   854
  pprt_spmat :: "('a::{lordered_ab_group_add}) spmat \<Rightarrow> 'a spmat"
haftmann@25303
   855
  nprt_spmat :: "('a::{lordered_ab_group_add}) spmat \<Rightarrow> 'a spmat"
obua@15580
   856
obua@15580
   857
primrec
obua@15580
   858
  "pprt_spvec [] = []"
obua@15580
   859
  "pprt_spvec (a#as) = (fst a, pprt (snd a)) # (pprt_spvec as)"
obua@15580
   860
obua@15580
   861
primrec
obua@15580
   862
  "nprt_spvec [] = []"
obua@15580
   863
  "nprt_spvec (a#as) = (fst a, nprt (snd a)) # (nprt_spvec as)"
obua@15580
   864
obua@15580
   865
primrec 
obua@15580
   866
  "pprt_spmat [] = []"
obua@15580
   867
  "pprt_spmat (a#as) = (fst a, pprt_spvec (snd a))#(pprt_spmat as)"
obua@15580
   868
  (*case (pprt_spvec (snd a)) of [] \<Rightarrow> (pprt_spmat as) | y#ys \<Rightarrow> (fst a, y#ys)#(pprt_spmat as))"*)
obua@15580
   869
obua@15580
   870
primrec 
obua@15580
   871
  "nprt_spmat [] = []"
obua@15580
   872
  "nprt_spmat (a#as) = (fst a, nprt_spvec (snd a))#(nprt_spmat as)"
obua@15580
   873
  (*case (nprt_spvec (snd a)) of [] \<Rightarrow> (nprt_spmat as) | y#ys \<Rightarrow> (fst a, y#ys)#(nprt_spmat as))"*)
obua@15580
   874
obua@15580
   875
obua@15580
   876
lemma pprt_add: "disj_matrices A (B::(_::lordered_ring) matrix) \<Longrightarrow> pprt (A+B) = pprt A + pprt B"
haftmann@22452
   877
  apply (simp add: pprt_def sup_matrix_def)
obua@15580
   878
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15580
   879
  apply (rule ext)+
obua@15580
   880
  apply simp
obua@15580
   881
  apply (case_tac "Rep_matrix A x xa \<noteq> 0")
obua@15580
   882
  apply (simp_all add: disj_matrices_contr1)
obua@15580
   883
  done
obua@15580
   884
obua@15580
   885
lemma nprt_add: "disj_matrices A (B::(_::lordered_ring) matrix) \<Longrightarrow> nprt (A+B) = nprt A + nprt B"
haftmann@22452
   886
  apply (simp add: nprt_def inf_matrix_def)
obua@15580
   887
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15580
   888
  apply (rule ext)+
obua@15580
   889
  apply simp
obua@15580
   890
  apply (case_tac "Rep_matrix A x xa \<noteq> 0")
obua@15580
   891
  apply (simp_all add: disj_matrices_contr1)
obua@15580
   892
  done
obua@15580
   893
obua@15580
   894
lemma pprt_singleton[simp]: "pprt (singleton_matrix j i (x::_::lordered_ring)) = singleton_matrix j i (pprt x)"
haftmann@22452
   895
  apply (simp add: pprt_def sup_matrix_def)
obua@15580
   896
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15580
   897
  apply (rule ext)+
obua@15580
   898
  apply simp
obua@15580
   899
  done
obua@15580
   900
obua@15580
   901
lemma nprt_singleton[simp]: "nprt (singleton_matrix j i (x::_::lordered_ring)) = singleton_matrix j i (nprt x)"
haftmann@22452
   902
  apply (simp add: nprt_def inf_matrix_def)
obua@15580
   903
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15580
   904
  apply (rule ext)+
obua@15580
   905
  apply simp
obua@15580
   906
  done
obua@15580
   907
obua@15580
   908
lemma less_imp_le: "a < b \<Longrightarrow> a <= (b::_::order)" by (simp add: less_def)
obua@15580
   909
haftmann@27653
   910
lemma sparse_row_vector_pprt: "sorted_spvec (v :: 'a::lordered_ring spvec) \<Longrightarrow> sparse_row_vector (pprt_spvec v) = pprt (sparse_row_vector v)"
obua@15580
   911
  apply (induct v)
obua@15580
   912
  apply (simp_all)
obua@15580
   913
  apply (frule sorted_spvec_cons1, auto)
obua@15580
   914
  apply (subst pprt_add)
obua@15580
   915
  apply (subst disj_matrices_commute)
obua@15580
   916
  apply (rule disj_sparse_row_singleton)
obua@15580
   917
  apply auto
obua@15580
   918
  done
obua@15580
   919
haftmann@27653
   920
lemma sparse_row_vector_nprt: "sorted_spvec (v :: 'a::lordered_ring spvec) \<Longrightarrow> sparse_row_vector (nprt_spvec v) = nprt (sparse_row_vector v)"
obua@15580
   921
  apply (induct v)
obua@15580
   922
  apply (simp_all)
obua@15580
   923
  apply (frule sorted_spvec_cons1, auto)
obua@15580
   924
  apply (subst nprt_add)
obua@15580
   925
  apply (subst disj_matrices_commute)
obua@15580
   926
  apply (rule disj_sparse_row_singleton)
obua@15580
   927
  apply auto
obua@15580
   928
  done
obua@15580
   929
  
obua@15580
   930
  
obua@15580
   931
lemma pprt_move_matrix: "pprt (move_matrix (A::('a::lordered_ring) matrix) j i) = move_matrix (pprt A) j i"
obua@15580
   932
  apply (simp add: pprt_def)
haftmann@22452
   933
  apply (simp add: sup_matrix_def)
obua@15580
   934
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15580
   935
  apply (rule ext)+
obua@15580
   936
  apply (simp)
obua@15580
   937
  done
obua@15580
   938
obua@15580
   939
lemma nprt_move_matrix: "nprt (move_matrix (A::('a::lordered_ring) matrix) j i) = move_matrix (nprt A) j i"
obua@15580
   940
  apply (simp add: nprt_def)
haftmann@22452
   941
  apply (simp add: inf_matrix_def)
obua@15580
   942
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15580
   943
  apply (rule ext)+
obua@15580
   944
  apply (simp)
obua@15580
   945
  done
obua@15580
   946
haftmann@27653
   947
lemma sparse_row_matrix_pprt: "sorted_spvec (m :: 'a::lordered_ring spmat) \<Longrightarrow> sorted_spmat m \<Longrightarrow> sparse_row_matrix (pprt_spmat m) = pprt (sparse_row_matrix m)"
obua@15580
   948
  apply (induct m)
obua@15580
   949
  apply simp
obua@15580
   950
  apply simp
obua@15580
   951
  apply (frule sorted_spvec_cons1)
obua@15580
   952
  apply (simp add: sparse_row_matrix_cons sparse_row_vector_pprt)
obua@15580
   953
  apply (subst pprt_add)
obua@15580
   954
  apply (subst disj_matrices_commute)
obua@15580
   955
  apply (rule disj_move_sparse_vec_mat)
obua@15580
   956
  apply auto
obua@15580
   957
  apply (simp add: sorted_spvec.simps)
obua@15580
   958
  apply (simp split: list.split)
obua@15580
   959
  apply auto
obua@15580
   960
  apply (simp add: pprt_move_matrix)
obua@15580
   961
  done
obua@15580
   962
haftmann@27653
   963
lemma sparse_row_matrix_nprt: "sorted_spvec (m :: 'a::lordered_ring spmat) \<Longrightarrow> sorted_spmat m \<Longrightarrow> sparse_row_matrix (nprt_spmat m) = nprt (sparse_row_matrix m)"
obua@15580
   964
  apply (induct m)
obua@15580
   965
  apply simp
obua@15580
   966
  apply simp
obua@15580
   967
  apply (frule sorted_spvec_cons1)
obua@15580
   968
  apply (simp add: sparse_row_matrix_cons sparse_row_vector_nprt)
obua@15580
   969
  apply (subst nprt_add)
obua@15580
   970
  apply (subst disj_matrices_commute)
obua@15580
   971
  apply (rule disj_move_sparse_vec_mat)
obua@15580
   972
  apply auto
obua@15580
   973
  apply (simp add: sorted_spvec.simps)
obua@15580
   974
  apply (simp split: list.split)
obua@15580
   975
  apply auto
obua@15580
   976
  apply (simp add: nprt_move_matrix)
obua@15580
   977
  done
obua@15580
   978
obua@15580
   979
lemma sorted_pprt_spvec: "sorted_spvec v \<Longrightarrow> sorted_spvec (pprt_spvec v)"
obua@15580
   980
  apply (induct v)
obua@15580
   981
  apply (simp)
obua@15580
   982
  apply (frule sorted_spvec_cons1)
obua@15580
   983
  apply simp
obua@15580
   984
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15580
   985
  done
obua@15580
   986
obua@15580
   987
lemma sorted_nprt_spvec: "sorted_spvec v \<Longrightarrow> sorted_spvec (nprt_spvec v)"
obua@15580
   988
  apply (induct v)
obua@15580
   989
  apply (simp)
obua@15580
   990
  apply (frule sorted_spvec_cons1)
obua@15580
   991
  apply simp
obua@15580
   992
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15580
   993
  done
obua@15580
   994
obua@15580
   995
lemma sorted_spvec_pprt_spmat: "sorted_spvec m \<Longrightarrow> sorted_spvec (pprt_spmat m)"
obua@15580
   996
  apply (induct m)
obua@15580
   997
  apply (simp)
obua@15580
   998
  apply (frule sorted_spvec_cons1)
obua@15580
   999
  apply simp
obua@15580
  1000
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15580
  1001
  done
obua@15580
  1002
obua@15580
  1003
lemma sorted_spvec_nprt_spmat: "sorted_spvec m \<Longrightarrow> sorted_spvec (nprt_spmat m)"
obua@15580
  1004
  apply (induct m)
obua@15580
  1005
  apply (simp)
obua@15580
  1006
  apply (frule sorted_spvec_cons1)
obua@15580
  1007
  apply simp
obua@15580
  1008
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15580
  1009
  done
obua@15580
  1010
obua@15580
  1011
lemma sorted_spmat_pprt_spmat: "sorted_spmat m \<Longrightarrow> sorted_spmat (pprt_spmat m)"
obua@15580
  1012
  apply (induct m)
obua@15580
  1013
  apply (simp_all add: sorted_pprt_spvec)
obua@15580
  1014
  done
obua@15580
  1015
obua@15580
  1016
lemma sorted_spmat_nprt_spmat: "sorted_spmat m \<Longrightarrow> sorted_spmat (nprt_spmat m)"
obua@15580
  1017
  apply (induct m)
obua@15580
  1018
  apply (simp_all add: sorted_nprt_spvec)
obua@15580
  1019
  done
obua@15580
  1020
obua@15580
  1021
constdefs
obua@15580
  1022
  mult_est_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
obua@15580
  1023
  "mult_est_spmat r1 r2 s1 s2 == 
nipkow@31816
  1024
  add_spmat (mult_spmat (pprt_spmat s2) (pprt_spmat r2)) (add_spmat (mult_spmat (pprt_spmat s1) (nprt_spmat r2)) 
nipkow@31816
  1025
  (add_spmat (mult_spmat (nprt_spmat s2) (pprt_spmat r1)) (mult_spmat (nprt_spmat s1) (nprt_spmat r1))))"  
obua@15580
  1026
obua@15580
  1027
lemmas sparse_row_matrix_op_simps =
obua@15580
  1028
  sorted_sparse_matrix_imp_spmat sorted_sparse_matrix_imp_spvec
obua@15580
  1029
  sparse_row_add_spmat sorted_spvec_add_spmat sorted_spmat_add_spmat
obua@15580
  1030
  sparse_row_diff_spmat sorted_spvec_diff_spmat sorted_spmat_diff_spmat
obua@15580
  1031
  sparse_row_matrix_minus sorted_spvec_minus_spmat sorted_spmat_minus_spmat
obua@15580
  1032
  sparse_row_mult_spmat sorted_spvec_mult_spmat sorted_spmat_mult_spmat
obua@15580
  1033
  sparse_row_matrix_abs sorted_spvec_abs_spmat sorted_spmat_abs_spmat
obua@15580
  1034
  le_spmat_iff_sparse_row_le
obua@15580
  1035
  sparse_row_matrix_pprt sorted_spvec_pprt_spmat sorted_spmat_pprt_spmat
obua@15580
  1036
  sparse_row_matrix_nprt sorted_spvec_nprt_spmat sorted_spmat_nprt_spmat
obua@15580
  1037
obua@15580
  1038
lemma zero_eq_Numeral0: "(0::_::number_ring) = Numeral0" by simp
obua@15580
  1039
obua@15580
  1040
lemmas sparse_row_matrix_arith_simps[simplified zero_eq_Numeral0] = 
obua@15580
  1041
  mult_spmat.simps mult_spvec_spmat.simps 
obua@15580
  1042
  addmult_spvec.simps 
obua@15580
  1043
  smult_spvec_empty smult_spvec_cons
obua@15580
  1044
  add_spmat.simps add_spvec.simps
obua@15580
  1045
  minus_spmat.simps minus_spvec.simps
obua@15580
  1046
  abs_spmat.simps abs_spvec.simps
obua@15580
  1047
  diff_spmat_def
obua@15580
  1048
  le_spmat.simps le_spvec.simps
obua@15580
  1049
  pprt_spmat.simps pprt_spvec.simps
obua@15580
  1050
  nprt_spmat.simps nprt_spvec.simps
obua@15580
  1051
  mult_est_spmat_def
obua@15580
  1052
obua@15580
  1053
obua@15580
  1054
(*lemma spm_linprog_dual_estimate_1:
obua@15178
  1055
  assumes  
obua@15178
  1056
  "sorted_sparse_matrix A1"
obua@15178
  1057
  "sorted_sparse_matrix A2"
obua@15178
  1058
  "sorted_sparse_matrix c1"
obua@15178
  1059
  "sorted_sparse_matrix c2"
obua@15178
  1060
  "sorted_sparse_matrix y"
obua@15178
  1061
  "sorted_spvec b"
obua@15178
  1062
  "sorted_spvec r"
obua@15178
  1063
  "le_spmat ([], y)"
obua@15178
  1064
  "A * x \<le> sparse_row_matrix (b::('a::lordered_ring) spmat)"
obua@15178
  1065
  "sparse_row_matrix A1 <= A"
obua@15178
  1066
  "A <= sparse_row_matrix A2"
obua@15178
  1067
  "sparse_row_matrix c1 <= c"
obua@15178
  1068
  "c <= sparse_row_matrix c2"
obua@15178
  1069
  "abs x \<le> sparse_row_matrix r"
obua@15178
  1070
  shows
obua@15178
  1071
  "c * x \<le> sparse_row_matrix (add_spmat (mult_spmat y b, mult_spmat (add_spmat (add_spmat (mult_spmat y (diff_spmat A2 A1), 
obua@15178
  1072
  abs_spmat (diff_spmat (mult_spmat y A1) c1)), diff_spmat c2 c1)) r))"
obua@15178
  1073
  by (insert prems, simp add: sparse_row_matrix_op_simps linprog_dual_estimate_1[where A=A])
obua@15580
  1074
*)
obua@15009
  1075
obua@15009
  1076
end