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