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