src/HOL/Matrix/SparseMatrix.thy
author paulson
Tue Feb 01 18:01:57 2005 +0100 (2005-02-01)
changeset 15481 fc075ae929e4
parent 15236 f289e8ba2bb3
child 15580 900291ee0af8
permissions -rw-r--r--
the new subst tactic, by Lucas Dixon
obua@15009
     1
theory SparseMatrix = Matrix:
obua@15009
     2
obua@15009
     3
types 
obua@15009
     4
  'a spvec = "(nat * 'a) list"
obua@15009
     5
  'a spmat = "('a spvec) spvec"
obua@15009
     6
obua@15009
     7
consts
obua@15009
     8
  sparse_row_vector :: "('a::lordered_ring) spvec \<Rightarrow> 'a matrix"
obua@15009
     9
  sparse_row_matrix :: "('a::lordered_ring) spmat \<Rightarrow> 'a matrix"
obua@15009
    10
obua@15009
    11
defs
obua@15009
    12
  sparse_row_vector_def : "sparse_row_vector arr == foldl (% m x. m + (singleton_matrix 0 (fst x) (snd x))) 0 arr"
obua@15009
    13
  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
    14
obua@15009
    15
lemma sparse_row_vector_empty[simp]: "sparse_row_vector [] = 0"
obua@15009
    16
  by (simp add: sparse_row_vector_def)
obua@15009
    17
obua@15009
    18
lemma sparse_row_matrix_empty[simp]: "sparse_row_matrix [] = 0"
obua@15009
    19
  by (simp add: sparse_row_matrix_def)
obua@15009
    20
obua@15009
    21
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
    22
  by (induct l, auto)
obua@15009
    23
obua@15009
    24
lemma sparse_row_vector_cons[simp]: "sparse_row_vector (a#arr) = (singleton_matrix 0 (fst a) (snd a)) + (sparse_row_vector arr)"
obua@15009
    25
  apply (induct arr)
obua@15009
    26
  apply (auto simp add: sparse_row_vector_def)
obua@15009
    27
  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
    28
  done
obua@15009
    29
obua@15009
    30
lemma sparse_row_vector_append[simp]: "sparse_row_vector (a @ b) = (sparse_row_vector a) + (sparse_row_vector b)"
obua@15009
    31
  by (induct a, auto)
obua@15009
    32
obua@15009
    33
lemma nrows_spvec[simp]: "nrows (sparse_row_vector x) <= (Suc 0)"
obua@15009
    34
  apply (induct x)
obua@15009
    35
  apply (simp_all add: add_nrows)
obua@15009
    36
  done
obua@15009
    37
obua@15009
    38
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
    39
  apply (induct arr)
obua@15009
    40
  apply (auto simp add: sparse_row_matrix_def)
obua@15009
    41
  apply (simp add: foldl_distrstart[of "\<lambda>m x. m + (move_matrix (sparse_row_vector (snd x)) (int (fst x)) 0)" 
obua@15009
    42
    "% a m. (move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0) + m"])
obua@15009
    43
  done
obua@15009
    44
obua@15009
    45
lemma sparse_row_matrix_append: "sparse_row_matrix (arr@brr) = (sparse_row_matrix arr) + (sparse_row_matrix brr)"
obua@15009
    46
  apply (induct arr)
obua@15009
    47
  apply (auto simp add: sparse_row_matrix_cons)
obua@15009
    48
  done
obua@15009
    49
obua@15009
    50
consts
obua@15009
    51
  sorted_spvec :: "'a spvec \<Rightarrow> bool"
obua@15009
    52
  sorted_spmat :: "'a spmat \<Rightarrow> bool"
obua@15009
    53
obua@15009
    54
primrec
obua@15009
    55
  "sorted_spmat [] = True"
obua@15009
    56
  "sorted_spmat (a#as) = ((sorted_spvec (snd a)) & (sorted_spmat as))"
obua@15009
    57
obua@15009
    58
primrec
obua@15009
    59
  "sorted_spvec [] = True"
obua@15009
    60
sorted_spvec_step:  "sorted_spvec (a#as) = (case as of [] \<Rightarrow> True | b#bs \<Rightarrow> ((fst a < fst b) & (sorted_spvec as)))" 
obua@15009
    61
obua@15009
    62
declare sorted_spvec.simps [simp del]
obua@15009
    63
obua@15009
    64
lemma sorted_spvec_empty[simp]: "sorted_spvec [] = True"
obua@15009
    65
by (simp add: sorted_spvec.simps)
obua@15009
    66
obua@15009
    67
lemma sorted_spvec_cons1: "sorted_spvec (a#as) \<Longrightarrow> sorted_spvec as"
obua@15009
    68
apply (induct as)
obua@15009
    69
apply (auto simp add: sorted_spvec.simps)
obua@15009
    70
done
obua@15009
    71
obua@15009
    72
lemma sorted_spvec_cons2: "sorted_spvec (a#b#t) \<Longrightarrow> sorted_spvec (a#t)"
obua@15009
    73
apply (induct t)
obua@15009
    74
apply (auto simp add: sorted_spvec.simps)
obua@15009
    75
done
obua@15009
    76
obua@15009
    77
lemma sorted_spvec_cons3: "sorted_spvec(a#b#t) \<Longrightarrow> fst a < fst b"
obua@15009
    78
apply (auto simp add: sorted_spvec.simps)
obua@15009
    79
done
obua@15009
    80
obua@15009
    81
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
    82
apply (induct arr)
obua@15009
    83
apply (auto)
obua@15009
    84
apply (frule sorted_spvec_cons2,simp)+
obua@15009
    85
apply (frule sorted_spvec_cons3, simp)
obua@15009
    86
done
obua@15009
    87
obua@15009
    88
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
    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
  apply (simp add: sparse_row_matrix_cons neg_def)
obua@15009
    94
  done
obua@15009
    95
obua@15009
    96
consts
obua@15178
    97
  abs_spvec :: "('a::lordered_ring) spvec \<Rightarrow> 'a spvec"
obua@15178
    98
  minus_spvec ::  "('a::lordered_ring) spvec \<Rightarrow> 'a spvec"
obua@15009
    99
  smult_spvec :: "('a::lordered_ring) \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec" 
obua@15009
   100
  addmult_spvec :: "('a::lordered_ring) * 'a spvec * 'a spvec \<Rightarrow> 'a spvec"
obua@15009
   101
obua@15178
   102
primrec 
obua@15178
   103
  "minus_spvec [] = []"
obua@15178
   104
  "minus_spvec (a#as) = (fst a, -(snd a))#(minus_spvec as)"
obua@15178
   105
obua@15178
   106
primrec 
obua@15178
   107
  "abs_spvec [] = []"
obua@15178
   108
  "abs_spvec (a#as) = (fst a, abs (snd a))#(abs_spvec as)"
obua@15178
   109
obua@15178
   110
lemma sparse_row_vector_minus: 
obua@15178
   111
  "sparse_row_vector (minus_spvec v) = - (sparse_row_vector v)"
obua@15178
   112
  apply (induct v)
obua@15178
   113
  apply (simp_all add: sparse_row_vector_cons)
obua@15178
   114
  apply (simp add: Rep_matrix_inject[symmetric])
obua@15178
   115
  apply (rule ext)+
obua@15178
   116
  apply simp
obua@15178
   117
  done
obua@15178
   118
obua@15178
   119
lemma sparse_row_vector_abs:
obua@15178
   120
  "sorted_spvec v \<Longrightarrow> sparse_row_vector (abs_spvec v) = abs (sparse_row_vector v)"
obua@15178
   121
  apply (induct v)
obua@15178
   122
  apply (simp_all add: sparse_row_vector_cons)
obua@15178
   123
  apply (frule_tac sorted_spvec_cons1, simp)
obua@15178
   124
  apply (simp only: Rep_matrix_inject[symmetric])
obua@15178
   125
  apply (rule ext)+
obua@15178
   126
  apply auto
nipkow@15236
   127
  apply (subgoal_tac "Rep_matrix (sparse_row_vector v) 0 a = 0")
obua@15178
   128
  apply (simp)
obua@15178
   129
  apply (rule sorted_sparse_row_vector_zero)
obua@15178
   130
  apply auto
obua@15178
   131
  done
obua@15178
   132
obua@15178
   133
lemma sorted_spvec_minus_spvec:
obua@15178
   134
  "sorted_spvec v \<Longrightarrow> sorted_spvec (minus_spvec v)"
obua@15178
   135
  apply (induct v)
obua@15178
   136
  apply (simp)
obua@15178
   137
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   138
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   139
  done
obua@15178
   140
obua@15178
   141
lemma sorted_spvec_abs_spvec:
obua@15178
   142
  "sorted_spvec v \<Longrightarrow> sorted_spvec (abs_spvec v)"
obua@15178
   143
  apply (induct v)
obua@15178
   144
  apply (simp)
obua@15178
   145
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   146
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   147
  done
obua@15178
   148
  
obua@15009
   149
defs
obua@15009
   150
  smult_spvec_def: "smult_spvec y arr == map (% a. (fst a, y * snd a)) arr"  
obua@15009
   151
obua@15009
   152
lemma smult_spvec_empty[simp]: "smult_spvec y [] = []"
obua@15009
   153
  by (simp add: smult_spvec_def)
obua@15009
   154
obua@15009
   155
lemma smult_spvec_cons: "smult_spvec y (a#arr) = (fst a, y * (snd a)) # (smult_spvec y arr)"
obua@15009
   156
  by (simp add: smult_spvec_def)
obua@15009
   157
obua@15009
   158
recdef addmult_spvec "measure (% (y, a, b). length a + (length b))"
obua@15009
   159
  "addmult_spvec (y, arr, []) = arr"
obua@15009
   160
  "addmult_spvec (y, [], brr) = smult_spvec y brr"
obua@15009
   161
  "addmult_spvec (y, a#arr, b#brr) = (
obua@15009
   162
    if (fst a) < (fst b) then (a#(addmult_spvec (y, arr, b#brr))) 
obua@15009
   163
    else (if (fst b < fst a) then ((fst b, y * (snd b))#(addmult_spvec (y, a#arr, brr)))
obua@15009
   164
    else ((fst a, (snd a)+ y*(snd b))#(addmult_spvec (y, arr,brr)))))"
obua@15009
   165
obua@15009
   166
lemma addmult_spvec_empty1[simp]: "addmult_spvec (y, [], a) = smult_spvec y a"
obua@15009
   167
  by (induct a, auto)
obua@15009
   168
obua@15009
   169
lemma addmult_spvec_empty2[simp]: "addmult_spvec (y, a, []) = a"
obua@15009
   170
  by (induct a, auto)
obua@15009
   171
obua@15009
   172
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
   173
  sparse_row_vector (map (% x. (fst x, f (snd x))) a) = apply_matrix f (sparse_row_vector a)"
obua@15009
   174
  apply (induct a)
obua@15009
   175
  apply (simp_all add: apply_matrix_add)
obua@15009
   176
  done
obua@15009
   177
obua@15009
   178
lemma sparse_row_vector_smult: "sparse_row_vector (smult_spvec y a) = scalar_mult y (sparse_row_vector a)"
obua@15009
   179
  apply (induct a)
obua@15009
   180
  apply (simp_all add: smult_spvec_cons scalar_mult_add)
obua@15009
   181
  done
obua@15009
   182
obua@15009
   183
lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lordered_ring, a, b)) = 
obua@15009
   184
  (sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))"
obua@15009
   185
  apply (rule addmult_spvec.induct[of _ y])
obua@15009
   186
  apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+
obua@15009
   187
  done
obua@15009
   188
obua@15009
   189
lemma sorted_smult_spvec[rule_format]: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)"
obua@15009
   190
  apply (auto simp add: smult_spvec_def)
obua@15009
   191
  apply (induct a)
nipkow@15236
   192
  apply (auto simp add: sorted_spvec.simps split:list.split_asm)
obua@15009
   193
  done
obua@15009
   194
obua@15009
   195
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
   196
  sorted_spvec ((aa, ba) # brr)\<rbrakk> \<Longrightarrow> sorted_spvec ((aa, y * ba) # addmult_spvec (y, (a, b) # arr, brr))"  
obua@15009
   197
  apply (induct brr)
obua@15009
   198
  apply (auto simp add: sorted_spvec.simps)
obua@15009
   199
  apply (simp split: list.split)
obua@15009
   200
  apply (auto)
obua@15009
   201
  apply (simp split: list.split)
obua@15009
   202
  apply (auto)
obua@15009
   203
  done
obua@15009
   204
obua@15009
   205
lemma sorted_spvec_addmult_spvec_helper2: 
obua@15009
   206
 "\<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
   207
       \<Longrightarrow> sorted_spvec ((a, b) # addmult_spvec (y, arr, (aa, ba) # brr))"
obua@15009
   208
  apply (induct arr)
obua@15009
   209
  apply (auto simp add: smult_spvec_def sorted_spvec.simps)
obua@15009
   210
  apply (simp split: list.split)
obua@15009
   211
  apply (auto)
obua@15009
   212
  done
obua@15009
   213
obua@15009
   214
lemma sorted_spvec_addmult_spvec_helper3[rule_format]:
obua@15009
   215
  "sorted_spvec (addmult_spvec (y, arr, brr)) \<longrightarrow> sorted_spvec ((aa, b) # arr) \<longrightarrow> sorted_spvec ((aa, ba) # brr)
obua@15009
   216
     \<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec (y, arr, brr)))"
obua@15009
   217
  apply (rule addmult_spvec.induct[of _ y arr brr])
obua@15009
   218
  apply (simp_all add: sorted_spvec.simps smult_spvec_def)
obua@15009
   219
  done
obua@15009
   220
obua@15009
   221
lemma sorted_addmult_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (addmult_spvec (y, a, b))"
obua@15009
   222
  apply (rule addmult_spvec.induct[of _ y a b])
obua@15009
   223
  apply (simp_all add: sorted_smult_spvec)
obua@15009
   224
  apply (rule conjI, intro strip)
obua@15009
   225
  apply (case_tac "~(a < aa)")
obua@15009
   226
  apply (simp_all)
obua@15009
   227
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   228
  apply (simp add: sorted_spvec_addmult_spvec_helper)
obua@15009
   229
  apply (intro strip | rule conjI)+
obua@15009
   230
  apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   231
  apply (simp add: sorted_spvec_addmult_spvec_helper2)
obua@15009
   232
  apply (intro strip)
obua@15009
   233
  apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   234
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   235
  apply (simp)
obua@15009
   236
  apply (case_tac "a=aa")
obua@15009
   237
  apply (simp_all add: sorted_spvec_addmult_spvec_helper3)
obua@15009
   238
  done
obua@15009
   239
obua@15009
   240
consts 
obua@15009
   241
  mult_spvec_spmat :: "('a::lordered_ring) spvec * 'a spvec * 'a spmat  \<Rightarrow> 'a spvec"
obua@15009
   242
obua@15009
   243
recdef mult_spvec_spmat "measure (% (c, arr, brr). (length arr) + (length brr))"
obua@15009
   244
  "mult_spvec_spmat (c, [], brr) = c"
obua@15009
   245
  "mult_spvec_spmat (c, arr, []) = c"
obua@15009
   246
  "mult_spvec_spmat (c, a#arr, b#brr) = (
obua@15009
   247
     if ((fst a) < (fst b)) then (mult_spvec_spmat (c, arr, b#brr))
obua@15009
   248
     else (if ((fst b) < (fst a)) then (mult_spvec_spmat (c, a#arr, brr)) 
obua@15009
   249
     else (mult_spvec_spmat (addmult_spvec (snd a, c, snd b), arr, brr))))"
obua@15009
   250
obua@15009
   251
lemma sparse_row_mult_spvec_spmat[rule_format]: "sorted_spvec (a::('a::lordered_ring) spvec) \<longrightarrow> sorted_spvec B \<longrightarrow> 
obua@15009
   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
obua@15009
   268
    apply (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)
obua@15009
   273
    apply (simp add: ring_eq_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 (intro strip)
obua@15009
   277
    apply (rule disjI2)
obua@15009
   278
    apply (intro strip)
obua@15009
   279
    apply (subst nrows)
obua@15009
   280
    apply (rule  order_trans[of _ 1])
obua@15009
   281
    apply (simp add: comp_1)+
obua@15009
   282
    apply (subst Rep_matrix_zero_imp_mult_zero)
obua@15009
   283
    apply (intro strip)
obua@15009
   284
    apply (case_tac "k <= aa")
obua@15009
   285
    apply (rule_tac m1 = k and n1 = a and a1 = b in ssubst[OF sorted_sparse_row_vector_zero])
obua@15009
   286
    apply (simp_all)
obua@15009
   287
    apply (rule impI)
obua@15009
   288
    apply (rule disjI2)
obua@15009
   289
    apply (rule nrows)
obua@15009
   290
    apply (rule order_trans[of _ 1])
obua@15009
   291
    apply (simp_all add: comp_1)
obua@15009
   292
    
obua@15009
   293
    apply (intro strip | rule conjI)+
obua@15009
   294
    apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   295
    apply (simp add: ring_eq_simps)
obua@15009
   296
    apply (subst Rep_matrix_zero_imp_mult_zero)
obua@15009
   297
    apply (simp)
obua@15009
   298
    apply (rule disjI2)
obua@15009
   299
    apply (intro strip)
obua@15009
   300
    apply (simp add: sparse_row_matrix_cons neg_def)
obua@15009
   301
    apply (case_tac "a <= aa")  
obua@15009
   302
    apply (erule sorted_sparse_row_matrix_zero)  
obua@15009
   303
    apply (simp_all)
obua@15009
   304
    apply (intro strip)
obua@15009
   305
    apply (case_tac "a=aa")
obua@15009
   306
    apply (simp_all)
obua@15009
   307
    apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   308
    apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   309
    apply (simp add: sparse_row_matrix_cons ring_eq_simps sparse_row_vector_addmult_spvec)
obua@15009
   310
    apply (rule_tac B1 = "sparse_row_matrix brr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
obua@15009
   311
    apply (auto)
obua@15009
   312
    apply (rule sorted_sparse_row_matrix_zero)
obua@15009
   313
    apply (simp_all)
obua@15009
   314
    apply (rule_tac A1 = "sparse_row_vector arr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
obua@15009
   315
    apply (auto)
obua@15009
   316
    apply (rule_tac m=k and n = aa and a = b and arr=arr in sorted_sparse_row_vector_zero)
obua@15009
   317
    apply (simp_all)
obua@15009
   318
    apply (simp add: neg_def)
obua@15009
   319
    apply (drule nrows_notzero)
obua@15009
   320
    apply (drule nrows_helper)
obua@15009
   321
    apply (arith)
obua@15009
   322
    
obua@15009
   323
    apply (subst Rep_matrix_inject[symmetric])
obua@15009
   324
    apply (rule ext)+
obua@15009
   325
    apply (simp)
obua@15009
   326
    apply (subst Rep_matrix_mult)
obua@15009
   327
    apply (rule_tac j1=aa in ssubst[OF foldseq_almostzero])
obua@15009
   328
    apply (simp_all)
obua@15009
   329
    apply (intro strip, rule conjI)
obua@15009
   330
    apply (intro strip)
obua@15009
   331
    apply (drule_tac max_helper)
obua@15009
   332
    apply (simp)
obua@15009
   333
    apply (auto)
obua@15009
   334
    apply (rule zero_imp_mult_zero)
obua@15009
   335
    apply (rule disjI2)
obua@15009
   336
    apply (rule nrows)
obua@15009
   337
    apply (rule order_trans[of _ 1])
obua@15009
   338
    apply (simp)
obua@15009
   339
    apply (simp)
obua@15009
   340
    done
obua@15009
   341
qed
obua@15009
   342
obua@15009
   343
lemma sorted_mult_spvec_spmat[rule_format]: 
obua@15009
   344
  "sorted_spvec (c::('a::lordered_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat (c, a, B))"
obua@15009
   345
  apply (rule mult_spvec_spmat.induct[of _ c a B])
obua@15009
   346
  apply (simp_all add: sorted_addmult_spvec)
obua@15009
   347
  done
obua@15009
   348
obua@15009
   349
consts 
obua@15009
   350
  mult_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
obua@15009
   351
obua@15009
   352
primrec 
obua@15009
   353
  "mult_spmat [] A = []"
obua@15009
   354
  "mult_spmat (a#as) A = (fst a, mult_spvec_spmat ([], snd a, A))#(mult_spmat as A)"
obua@15009
   355
obua@15009
   356
lemma sparse_row_mult_spmat[rule_format]: 
obua@15009
   357
  "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
   358
  apply (induct A)
obua@15009
   359
  apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat ring_eq_simps move_matrix_mult)
obua@15009
   360
  done
obua@15009
   361
obua@15009
   362
lemma sorted_spvec_mult_spmat[rule_format]:
obua@15009
   363
  "sorted_spvec (A::('a::lordered_ring) spmat) \<longrightarrow> sorted_spvec (mult_spmat A B)"
obua@15009
   364
  apply (induct A)
obua@15009
   365
  apply (auto)
obua@15009
   366
  apply (drule sorted_spvec_cons1, simp)
nipkow@15236
   367
  apply (case_tac A)
obua@15009
   368
  apply (auto simp add: sorted_spvec.simps)
obua@15009
   369
  done
obua@15009
   370
obua@15009
   371
lemma sorted_spmat_mult_spmat[rule_format]:
obua@15009
   372
  "sorted_spmat (B::('a::lordered_ring) spmat) \<longrightarrow> sorted_spmat (mult_spmat A B)"
obua@15009
   373
  apply (induct A)
obua@15009
   374
  apply (auto simp add: sorted_mult_spvec_spmat) 
obua@15009
   375
  done
obua@15009
   376
obua@15009
   377
consts
obua@15009
   378
  add_spvec :: "('a::lordered_ab_group) spvec * 'a spvec \<Rightarrow> 'a spvec"
obua@15009
   379
  add_spmat :: "('a::lordered_ab_group) spmat * 'a spmat \<Rightarrow> 'a spmat"
obua@15009
   380
obua@15009
   381
recdef add_spvec "measure (% (a, b). length a + (length b))"
obua@15009
   382
  "add_spvec (arr, []) = arr"
obua@15009
   383
  "add_spvec ([], brr) = brr"
obua@15009
   384
  "add_spvec (a#arr, b#brr) = (
obua@15009
   385
  if (fst a) < (fst b) then (a#(add_spvec (arr, b#brr))) 
obua@15009
   386
     else (if (fst b < fst a) then (b#(add_spvec (a#arr, brr)))
obua@15009
   387
     else ((fst a, (snd a)+(snd b))#(add_spvec (arr,brr)))))"
obua@15009
   388
obua@15009
   389
lemma add_spvec_empty1[simp]: "add_spvec ([], a) = a"
obua@15009
   390
  by (induct a, auto)
obua@15009
   391
obua@15009
   392
lemma add_spvec_empty2[simp]: "add_spvec (a, []) = a"
obua@15009
   393
  by (induct a, auto)
obua@15009
   394
obua@15009
   395
lemma sparse_row_vector_add: "sparse_row_vector (add_spvec (a,b)) = (sparse_row_vector a) + (sparse_row_vector b)"
obua@15009
   396
  apply (rule add_spvec.induct[of _ a b])
obua@15009
   397
  apply (simp_all add: singleton_matrix_add)
obua@15009
   398
  done
obua@15009
   399
obua@15009
   400
recdef add_spmat "measure (% (A,B). (length A)+(length B))"
obua@15009
   401
  "add_spmat ([], bs) = bs"
obua@15009
   402
  "add_spmat (as, []) = as"
obua@15009
   403
  "add_spmat (a#as, b#bs) = (
obua@15009
   404
  if fst a < fst b then 
obua@15009
   405
    (a#(add_spmat (as, b#bs)))
obua@15009
   406
  else (if fst b < fst a then
obua@15009
   407
    (b#(add_spmat (a#as, bs)))
obua@15009
   408
  else
obua@15009
   409
    ((fst a, add_spvec (snd a, snd b))#(add_spmat (as, bs)))))"
obua@15009
   410
obua@15009
   411
lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat (A, B)) = (sparse_row_matrix A) + (sparse_row_matrix B)"
obua@15009
   412
  apply (rule add_spmat.induct)
obua@15009
   413
  apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add)
obua@15009
   414
  done
obua@15009
   415
obua@15009
   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 - 
obua@15009
   418
    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
   419
      by (rule add_spvec.induct[of _ _ brr], auto)
obua@15009
   420
    then show ?thesis
obua@15009
   421
      by (case_tac brr, auto)
obua@15009
   422
  qed
obua@15009
   423
obua@15009
   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 - 
obua@15009
   426
    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
   427
      by (rule add_spmat.induct[of _ _ brr], auto)
obua@15009
   428
    then show ?thesis
obua@15009
   429
      by (case_tac brr, auto)
obua@15009
   430
  qed
obua@15009
   431
obua@15009
   432
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
   433
  apply (rule add_spvec.induct[of _ arr brr])
obua@15009
   434
  apply (auto)
obua@15009
   435
  done
obua@15009
   436
obua@15009
   437
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
   438
  apply (rule add_spmat.induct[of _ arr brr])
obua@15009
   439
  apply (auto)
obua@15009
   440
  done
obua@15009
   441
obua@15009
   442
lemma add_spvec_commute: "add_spvec (a, b) = add_spvec (b, a)"
obua@15009
   443
  by (rule add_spvec.induct[of _ a b], auto)
obua@15009
   444
obua@15009
   445
lemma add_spmat_commute: "add_spmat (a, b) = add_spmat (b, a)"
obua@15009
   446
  apply (rule add_spmat.induct[of _ a b])
obua@15009
   447
  apply (simp_all add: add_spvec_commute)
obua@15009
   448
  done
obua@15009
   449
  
obua@15009
   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
obua@15009
   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
obua@15009
   468
lemma sorted_spvec_add_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (add_spvec (a, b))"
obua@15009
   469
  apply (rule add_spvec.induct[of _ a b])
obua@15009
   470
  apply (simp_all)
obua@15009
   471
  apply (rule conjI)
obua@15009
   472
  apply (intro strip)
obua@15009
   473
  apply (simp)
obua@15009
   474
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   475
  apply (simp)
obua@15009
   476
  apply (subst sorted_spvec_step)
obua@15009
   477
  apply (simp split: list.split)
obua@15009
   478
  apply (clarify, simp)
obua@15009
   479
  apply (simp add: sorted_add_spvec_helper2)
obua@15009
   480
  apply (clarify)
obua@15009
   481
  apply (rule conjI)
obua@15009
   482
  apply (case_tac "a=aa")
obua@15009
   483
  apply (simp)
obua@15009
   484
  apply (clarify)
obua@15009
   485
  apply (frule_tac as=arr in sorted_spvec_cons1, simp)
obua@15009
   486
  apply (subst sorted_spvec_step)
obua@15009
   487
  apply (simp split: list.split)
obua@15009
   488
  apply (clarify, simp)
obua@15009
   489
  apply (simp add: sorted_add_spvec_helper2 add_spvec_commute)
obua@15009
   490
  apply (case_tac "a=aa")
obua@15009
   491
  apply (simp_all)
obua@15009
   492
  apply (clarify)
obua@15009
   493
  apply (frule_tac as=arr in sorted_spvec_cons1)
obua@15009
   494
  apply (frule_tac as=brr in sorted_spvec_cons1)
obua@15009
   495
  apply (simp)
obua@15009
   496
  apply (subst sorted_spvec_step)
obua@15009
   497
  apply (simp split: list.split)
obua@15009
   498
  apply (clarify, simp)
obua@15009
   499
  apply (drule_tac sorted_add_spvec_helper)
obua@15009
   500
  apply (auto)
obua@15009
   501
  apply (case_tac arr)
obua@15009
   502
  apply (simp_all)
obua@15009
   503
  apply (drule sorted_spvec_cons3)
obua@15009
   504
  apply (simp)
obua@15009
   505
  apply (case_tac brr)
obua@15009
   506
  apply (simp_all)
obua@15009
   507
  apply (drule sorted_spvec_cons3)
obua@15009
   508
  apply (simp)
obua@15009
   509
  done
obua@15009
   510
obua@15009
   511
lemma sorted_spvec_add_spmat[rule_format]: "sorted_spvec A \<longrightarrow> sorted_spvec B \<longrightarrow> sorted_spvec (add_spmat (A, B))"
obua@15009
   512
  apply (rule add_spmat.induct[of _ A B])
obua@15009
   513
  apply (simp_all)
obua@15009
   514
  apply (rule conjI)
obua@15009
   515
  apply (intro strip)
obua@15009
   516
  apply (simp)
obua@15009
   517
  apply (frule_tac as=bs in sorted_spvec_cons1)
obua@15009
   518
  apply (simp)
obua@15009
   519
  apply (subst sorted_spvec_step)
obua@15009
   520
  apply (simp split: list.split)
obua@15009
   521
  apply (clarify, simp)
obua@15009
   522
  apply (simp add: sorted_add_spmat_helper2)
obua@15009
   523
  apply (clarify)
obua@15009
   524
  apply (rule conjI)
obua@15009
   525
  apply (case_tac "a=aa")
obua@15009
   526
  apply (simp)
obua@15009
   527
  apply (clarify)
obua@15009
   528
  apply (frule_tac as=as in sorted_spvec_cons1, simp)
obua@15009
   529
  apply (subst sorted_spvec_step)
obua@15009
   530
  apply (simp split: list.split)
obua@15009
   531
  apply (clarify, simp)
obua@15009
   532
  apply (simp add: sorted_add_spmat_helper2 add_spmat_commute)
obua@15009
   533
  apply (case_tac "a=aa")
obua@15009
   534
  apply (simp_all)
obua@15009
   535
  apply (clarify)
obua@15009
   536
  apply (frule_tac as=as in sorted_spvec_cons1)
obua@15009
   537
  apply (frule_tac as=bs in sorted_spvec_cons1)
obua@15009
   538
  apply (simp)
obua@15009
   539
  apply (subst sorted_spvec_step)
obua@15009
   540
  apply (simp split: list.split)
obua@15009
   541
  apply (clarify, simp)
obua@15009
   542
  apply (drule_tac sorted_add_spmat_helper)
obua@15009
   543
  apply (auto)
obua@15009
   544
  apply (case_tac as)
obua@15009
   545
  apply (simp_all)
obua@15009
   546
  apply (drule sorted_spvec_cons3)
obua@15009
   547
  apply (simp)
obua@15009
   548
  apply (case_tac bs)
obua@15009
   549
  apply (simp_all)
obua@15009
   550
  apply (drule sorted_spvec_cons3)
obua@15009
   551
  apply (simp)
obua@15009
   552
  done
obua@15009
   553
obua@15009
   554
lemma sorted_spmat_add_spmat[rule_format]: "sorted_spmat A \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spmat (add_spmat (A, B))"
obua@15009
   555
  apply (rule add_spmat.induct[of _ A B])
obua@15009
   556
  apply (simp_all add: sorted_spvec_add_spvec)
obua@15009
   557
  done
obua@15009
   558
obua@15009
   559
consts
obua@15009
   560
  le_spvec :: "('a::lordered_ab_group) spvec * 'a spvec \<Rightarrow> bool" 
obua@15009
   561
  le_spmat :: "('a::lordered_ab_group) spmat * 'a spmat \<Rightarrow> bool" 
obua@15009
   562
obua@15009
   563
recdef le_spvec "measure (% (a,b). (length a) + (length b))" 
obua@15009
   564
  "le_spvec ([], []) = True"
obua@15009
   565
  "le_spvec (a#as, []) = ((snd a <= 0) & (le_spvec (as, [])))"
obua@15009
   566
  "le_spvec ([], b#bs) = ((0 <= snd b) & (le_spvec ([], bs)))"
obua@15009
   567
  "le_spvec (a#as, b#bs) = (
obua@15009
   568
  if (fst a < fst b) then 
obua@15009
   569
    ((snd a <= 0) & (le_spvec (as, b#bs)))
obua@15009
   570
  else (if (fst b < fst a) then
obua@15009
   571
    ((0 <= snd b) & (le_spvec (a#as, bs)))
obua@15009
   572
  else 
obua@15009
   573
    ((snd a <= snd b) & (le_spvec (as, bs)))))"
obua@15009
   574
obua@15009
   575
recdef le_spmat "measure (% (a,b). (length a) + (length b))"
obua@15009
   576
  "le_spmat ([], []) = True"
obua@15009
   577
  "le_spmat (a#as, []) = (le_spvec (snd a, []) & (le_spmat (as, [])))"
obua@15009
   578
  "le_spmat ([], b#bs) = (le_spvec ([], snd b) & (le_spmat ([], bs)))"
obua@15009
   579
  "le_spmat (a#as, b#bs) = (
obua@15009
   580
  if fst a < fst b then
obua@15009
   581
    (le_spvec(snd a,[]) & le_spmat(as, b#bs))
obua@15009
   582
  else (if (fst b < fst a) then 
obua@15009
   583
    (le_spvec([], snd b) & le_spmat(a#as, bs))
obua@15009
   584
  else
obua@15009
   585
    (le_spvec(snd a, snd b) & le_spmat (as, bs))))"
obua@15009
   586
obua@15009
   587
constdefs
obua@15009
   588
  disj_matrices :: "('a::zero) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool"
obua@15009
   589
  "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
   590
obua@15009
   591
ML {* simp_depth_limit := 2 *}
obua@15009
   592
obua@15009
   593
lemma disj_matrices_add: "disj_matrices A B \<Longrightarrow> disj_matrices C D \<Longrightarrow> disj_matrices A D \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
obua@15009
   594
  (A + B <= C + D) = (A <= C & B <= (D::('a::lordered_ab_group) matrix))"
obua@15009
   595
  apply (auto)
obua@15009
   596
  apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
obua@15009
   597
  apply (intro strip)
obua@15009
   598
  apply (erule conjE)+
obua@15009
   599
  apply (drule_tac j=j and i=i in spec2)+
obua@15009
   600
  apply (case_tac "Rep_matrix B j i = 0")
obua@15009
   601
  apply (case_tac "Rep_matrix D j i = 0")
obua@15009
   602
  apply (simp_all)
obua@15009
   603
  apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
obua@15009
   604
  apply (intro strip)
obua@15009
   605
  apply (erule conjE)+
obua@15009
   606
  apply (drule_tac j=j and i=i in spec2)+
obua@15009
   607
  apply (case_tac "Rep_matrix A j i = 0")
obua@15009
   608
  apply (case_tac "Rep_matrix C j i = 0")
obua@15009
   609
  apply (simp_all)
obua@15009
   610
  apply (erule add_mono)
obua@15009
   611
  apply (assumption)
obua@15009
   612
  done
obua@15009
   613
obua@15009
   614
lemma disj_matrices_zero1[simp]: "disj_matrices 0 B"
obua@15009
   615
by (simp add: disj_matrices_def)
obua@15009
   616
obua@15009
   617
lemma disj_matrices_zero2[simp]: "disj_matrices A 0"
obua@15009
   618
by (simp add: disj_matrices_def)
obua@15009
   619
obua@15009
   620
lemma disj_matrices_commute: "disj_matrices A B = disj_matrices B A"
obua@15009
   621
by (auto simp add: disj_matrices_def)
obua@15009
   622
obua@15009
   623
lemma disj_matrices_add_le_zero: "disj_matrices A B \<Longrightarrow>
obua@15009
   624
  (A + B <= 0) = (A <= 0 & (B::('a::lordered_ab_group) matrix) <= 0)"
obua@15009
   625
by (rule disj_matrices_add[of A B 0 0, simplified])
obua@15009
   626
 
obua@15009
   627
lemma disj_matrices_add_zero_le: "disj_matrices A B \<Longrightarrow>
obua@15009
   628
  (0 <= A + B) = (0 <= A & 0 <= (B::('a::lordered_ab_group) matrix))"
obua@15009
   629
by (rule disj_matrices_add[of 0 0 A B, simplified])
obua@15009
   630
obua@15009
   631
lemma disj_matrices_add_x_le: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
obua@15009
   632
  (A <= B + C) = (A <= C & 0 <= (B::('a::lordered_ab_group) matrix))"
obua@15009
   633
by (auto simp add: disj_matrices_add[of 0 A B C, simplified])
obua@15009
   634
obua@15009
   635
lemma disj_matrices_add_le_x: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
obua@15009
   636
  (B + A <= C) = (A <= C &  (B::('a::lordered_ab_group) matrix) <= 0)"
obua@15009
   637
by (auto simp add: disj_matrices_add[of B A 0 C,simplified] disj_matrices_commute)
obua@15009
   638
obua@15009
   639
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
   640
  apply (simp add: disj_matrices_def)
obua@15009
   641
  apply (rule conjI)
obua@15009
   642
  apply (rule neg_imp)
obua@15009
   643
  apply (simp)
obua@15009
   644
  apply (intro strip)
obua@15009
   645
  apply (rule sorted_sparse_row_vector_zero)
obua@15009
   646
  apply (simp_all)
obua@15009
   647
  apply (intro strip)
obua@15009
   648
  apply (rule sorted_sparse_row_vector_zero)
obua@15009
   649
  apply (simp_all)
obua@15009
   650
  done 
obua@15009
   651
obua@15009
   652
lemma disj_matrices_x_add: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (A::('a::lordered_ab_group) matrix) (B+C)"
obua@15009
   653
  apply (simp add: disj_matrices_def)
obua@15009
   654
  apply (auto)
obua@15009
   655
  apply (drule_tac j=j and i=i in spec2)+
obua@15009
   656
  apply (case_tac "Rep_matrix B j i = 0")
obua@15009
   657
  apply (case_tac "Rep_matrix C j i = 0")
obua@15009
   658
  apply (simp_all)
obua@15009
   659
  done
obua@15009
   660
obua@15009
   661
lemma disj_matrices_add_x: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (B+C) (A::('a::lordered_ab_group) matrix)" 
obua@15009
   662
  by (simp add: disj_matrices_x_add disj_matrices_commute)
obua@15009
   663
obua@15009
   664
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
   665
  by (auto simp add: disj_matrices_def)
obua@15009
   666
obua@15009
   667
lemma disj_move_sparse_vec_mat[simplified disj_matrices_commute]: 
obua@15009
   668
  "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
   669
  apply (auto simp add: neg_def disj_matrices_def)
obua@15009
   670
  apply (drule nrows_notzero)
obua@15009
   671
  apply (drule less_le_trans[OF _ nrows_spvec])
obua@15009
   672
  apply (subgoal_tac "ja = j")
obua@15009
   673
  apply (simp add: sorted_sparse_row_matrix_zero)
obua@15009
   674
  apply (arith)
obua@15009
   675
  apply (rule nrows)
obua@15009
   676
  apply (rule order_trans[of _ 1 _])
obua@15009
   677
  apply (simp)
obua@15009
   678
  apply (case_tac "nat (int ja - int j) = 0")
obua@15009
   679
  apply (case_tac "ja = j")
obua@15009
   680
  apply (simp add: sorted_sparse_row_matrix_zero)
obua@15009
   681
  apply arith+
obua@15009
   682
  done
obua@15009
   683
obua@15009
   684
lemma disj_move_sparse_row_vector_twice:
obua@15009
   685
  "j \<noteq> u \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector a) j i) (move_matrix (sparse_row_vector b) u v)"
obua@15009
   686
  apply (auto simp add: neg_def disj_matrices_def)
obua@15009
   687
  apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+
obua@15009
   688
  done
obua@15009
   689
obua@15178
   690
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
   691
  apply (rule le_spvec.induct)
obua@15178
   692
  apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le 
obua@15178
   693
    disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
obua@15178
   694
  apply (rule conjI, intro strip)
obua@15178
   695
  apply (simp add: sorted_spvec_cons1)
obua@15178
   696
  apply (subst disj_matrices_add_x_le)
obua@15178
   697
  apply (simp add: disj_sparse_row_singleton[OF less_imp_le] disj_matrices_x_add disj_matrices_commute)
obua@15178
   698
  apply (simp add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
obua@15178
   699
  apply (simp, blast)
obua@15178
   700
  apply (intro strip, rule conjI, intro strip)
obua@15178
   701
  apply (simp add: sorted_spvec_cons1)
obua@15178
   702
  apply (subst disj_matrices_add_le_x)
obua@15178
   703
  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
   704
  apply (blast)
obua@15178
   705
  apply (intro strip)
obua@15178
   706
  apply (simp add: sorted_spvec_cons1)
obua@15178
   707
  apply (case_tac "a=aa", simp_all)
obua@15178
   708
  apply (subst disj_matrices_add)
obua@15178
   709
  apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
obua@15009
   710
  done
obua@15009
   711
obua@15009
   712
lemma le_spvec_empty2_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec (b,[]) = (sparse_row_vector b <= 0))"
obua@15009
   713
  apply (induct b)
obua@15009
   714
  apply (simp_all add: sorted_spvec_cons1)
obua@15009
   715
  apply (intro strip)
obua@15009
   716
  apply (subst disj_matrices_add_le_zero)
obua@15009
   717
  apply (simp add: disj_matrices_commute disj_sparse_row_singleton sorted_spvec_cons1)
obua@15009
   718
  apply (rule_tac y = "snd a" in disj_sparse_row_singleton[OF order_refl])
obua@15009
   719
  apply (simp_all)
obua@15009
   720
  done
obua@15009
   721
obua@15009
   722
lemma le_spvec_empty1_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec ([],b) = (0 <= sparse_row_vector b))"
obua@15009
   723
  apply (induct b)
obua@15009
   724
  apply (simp_all add: sorted_spvec_cons1)
obua@15009
   725
  apply (intro strip)
obua@15009
   726
  apply (subst disj_matrices_add_zero_le)
obua@15009
   727
  apply (simp add: disj_matrices_commute disj_sparse_row_singleton sorted_spvec_cons1)
obua@15009
   728
  apply (rule_tac y = "snd a" in disj_sparse_row_singleton[OF order_refl])
obua@15009
   729
  apply (simp_all)
obua@15009
   730
  done
obua@15009
   731
obua@15009
   732
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
   733
  le_spmat(A, B) = (sparse_row_matrix A <= sparse_row_matrix B)"
obua@15009
   734
  apply (rule le_spmat.induct)
obua@15009
   735
  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
   736
    disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+ 
obua@15009
   737
  apply (rule conjI, intro strip)
obua@15009
   738
  apply (simp add: sorted_spvec_cons1)
obua@15009
   739
  apply (subst disj_matrices_add_x_le)
obua@15009
   740
  apply (rule disj_matrices_add_x)
obua@15009
   741
  apply (simp add: disj_move_sparse_row_vector_twice)
obua@15009
   742
  apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
obua@15009
   743
  apply (simp add: disj_move_sparse_vec_mat[OF order_refl] disj_matrices_commute)
obua@15009
   744
  apply (simp, blast)
obua@15009
   745
  apply (intro strip, rule conjI, intro strip)
obua@15009
   746
  apply (simp add: sorted_spvec_cons1)
obua@15009
   747
  apply (subst disj_matrices_add_le_x)
obua@15009
   748
  apply (simp add: disj_move_sparse_vec_mat[OF order_refl])
obua@15009
   749
  apply (rule disj_matrices_x_add)
obua@15009
   750
  apply (simp add: disj_move_sparse_row_vector_twice)
obua@15009
   751
  apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
obua@15009
   752
  apply (simp, blast)
obua@15009
   753
  apply (intro strip)
obua@15009
   754
  apply (case_tac "a=aa")
obua@15009
   755
  apply (simp_all)
obua@15009
   756
  apply (subst disj_matrices_add)
obua@15009
   757
  apply (simp_all add: disj_matrices_commute disj_move_sparse_vec_mat[OF order_refl])
obua@15009
   758
  apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le)
obua@15009
   759
  done
obua@15009
   760
obua@15178
   761
ML {* simp_depth_limit := 999 *}
obua@15178
   762
obua@15178
   763
consts 
obua@15178
   764
   abs_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat"
obua@15178
   765
   minus_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat"
obua@15178
   766
obua@15178
   767
primrec
obua@15178
   768
  "abs_spmat [] = []"
obua@15178
   769
  "abs_spmat (a#as) = (fst a, abs_spvec (snd a))#(abs_spmat as)"
obua@15178
   770
obua@15178
   771
primrec
obua@15178
   772
  "minus_spmat [] = []"
obua@15178
   773
  "minus_spmat (a#as) = (fst a, minus_spvec (snd a))#(minus_spmat as)"
obua@15178
   774
obua@15178
   775
lemma sparse_row_matrix_minus:
obua@15178
   776
  "sparse_row_matrix (minus_spmat A) = - (sparse_row_matrix A)"
obua@15178
   777
  apply (induct A)
obua@15178
   778
  apply (simp_all add: sparse_row_vector_minus sparse_row_matrix_cons)
obua@15178
   779
  apply (subst Rep_matrix_inject[symmetric])
obua@15178
   780
  apply (rule ext)+
obua@15178
   781
  apply simp
obua@15178
   782
  done
obua@15009
   783
obua@15178
   784
lemma Rep_sparse_row_vector_zero: "x \<noteq> 0 \<Longrightarrow> Rep_matrix (sparse_row_vector v) x y = 0"
obua@15178
   785
proof -
obua@15178
   786
  assume x:"x \<noteq> 0"
obua@15178
   787
  have r:"nrows (sparse_row_vector v) <= Suc 0" by (rule nrows_spvec)
obua@15178
   788
  show ?thesis
obua@15178
   789
    apply (rule nrows)
obua@15178
   790
    apply (subgoal_tac "Suc 0 <= x")
obua@15178
   791
    apply (insert r)
obua@15178
   792
    apply (simp only:)
obua@15178
   793
    apply (insert x)
obua@15178
   794
    apply arith
obua@15178
   795
    done
obua@15178
   796
qed
obua@15178
   797
    
obua@15178
   798
lemma sparse_row_matrix_abs:
obua@15178
   799
  "sorted_spvec A \<Longrightarrow> sorted_spmat A \<Longrightarrow> sparse_row_matrix (abs_spmat A) = abs (sparse_row_matrix A)"
obua@15178
   800
  apply (induct A)
obua@15178
   801
  apply (simp_all add: sparse_row_vector_abs sparse_row_matrix_cons)
obua@15178
   802
  apply (frule_tac sorted_spvec_cons1, simp)
obua@15178
   803
  apply (subst Rep_matrix_inject[symmetric])
obua@15178
   804
  apply (rule ext)+
obua@15178
   805
  apply auto
obua@15178
   806
  apply (case_tac "x=a")
obua@15178
   807
  apply (simp)
paulson@15481
   808
  apply (simplesubst sorted_sparse_row_matrix_zero)
obua@15178
   809
  apply auto
paulson@15481
   810
  apply (simplesubst Rep_sparse_row_vector_zero)
obua@15178
   811
  apply (simp_all add: neg_def)
obua@15178
   812
  done
obua@15178
   813
obua@15178
   814
lemma sorted_spvec_minus_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (minus_spmat A)"
obua@15178
   815
  apply (induct A)
obua@15178
   816
  apply (simp)
obua@15178
   817
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   818
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   819
  done 
obua@15178
   820
obua@15178
   821
lemma sorted_spvec_abs_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (abs_spmat A)" 
obua@15178
   822
  apply (induct A)
obua@15178
   823
  apply (simp)
obua@15178
   824
  apply (frule sorted_spvec_cons1, simp)
nipkow@15236
   825
  apply (simp add: sorted_spvec.simps split:list.split_asm)
obua@15178
   826
  done
obua@15178
   827
obua@15178
   828
lemma sorted_spmat_minus_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (minus_spmat A)"
obua@15178
   829
  apply (induct A)
obua@15178
   830
  apply (simp_all add: sorted_spvec_minus_spvec)
obua@15178
   831
  done
obua@15178
   832
obua@15178
   833
lemma sorted_spmat_abs_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (abs_spmat A)"
obua@15178
   834
  apply (induct A)
obua@15178
   835
  apply (simp_all add: sorted_spvec_abs_spvec)
obua@15178
   836
  done
obua@15009
   837
obua@15178
   838
constdefs
obua@15178
   839
  diff_spmat :: "('a::lordered_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
obua@15178
   840
  "diff_spmat A B == add_spmat (A, minus_spmat B)"
obua@15178
   841
obua@15178
   842
lemma sorted_spmat_diff_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat B \<Longrightarrow> sorted_spmat (diff_spmat A B)"
obua@15178
   843
  by (simp add: diff_spmat_def sorted_spmat_minus_spmat sorted_spmat_add_spmat)
obua@15178
   844
obua@15178
   845
lemma sorted_spvec_diff_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec B \<Longrightarrow> sorted_spvec (diff_spmat A B)"
obua@15178
   846
  by (simp add: diff_spmat_def sorted_spvec_minus_spmat sorted_spvec_add_spmat)
obua@15178
   847
obua@15178
   848
lemma sparse_row_diff_spmat: "sparse_row_matrix (diff_spmat A B ) = (sparse_row_matrix A) - (sparse_row_matrix B)"
obua@15178
   849
  by (simp add: diff_spmat_def sparse_row_add_spmat sparse_row_matrix_minus)
obua@15178
   850
obua@15178
   851
constdefs
obua@15178
   852
  sorted_sparse_matrix :: "'a spmat \<Rightarrow> bool"
obua@15178
   853
  "sorted_sparse_matrix A == (sorted_spvec A) & (sorted_spmat A)"
obua@15178
   854
obua@15178
   855
lemma sorted_sparse_matrix_imp_spvec: "sorted_sparse_matrix A \<Longrightarrow> sorted_spvec A"
obua@15178
   856
  by (simp add: sorted_sparse_matrix_def)
obua@15178
   857
obua@15178
   858
lemma sorted_sparse_matrix_imp_spmat: "sorted_sparse_matrix A \<Longrightarrow> sorted_spmat A"
obua@15178
   859
  by (simp add: sorted_sparse_matrix_def)
obua@15178
   860
obua@15178
   861
lemmas sparse_row_matrix_op_simps =
obua@15178
   862
  sorted_sparse_matrix_imp_spmat sorted_sparse_matrix_imp_spvec
obua@15178
   863
  sparse_row_add_spmat sorted_spvec_add_spmat sorted_spmat_add_spmat
obua@15178
   864
  sparse_row_diff_spmat sorted_spvec_diff_spmat sorted_spmat_diff_spmat
obua@15178
   865
  sparse_row_matrix_minus sorted_spvec_minus_spmat sorted_spmat_minus_spmat
obua@15178
   866
  sparse_row_mult_spmat sorted_spvec_mult_spmat sorted_spmat_mult_spmat
obua@15178
   867
  sparse_row_matrix_abs sorted_spvec_abs_spmat sorted_spmat_abs_spmat
obua@15178
   868
  le_spmat_iff_sparse_row_le
obua@15178
   869
obua@15178
   870
lemma zero_eq_Numeral0: "(0::_::number_ring) = Numeral0" by simp
obua@15009
   871
obua@15178
   872
lemmas sparse_row_matrix_arith_simps[simplified zero_eq_Numeral0] = 
obua@15178
   873
  mult_spmat.simps mult_spvec_spmat.simps 
obua@15178
   874
  addmult_spvec.simps 
obua@15178
   875
  smult_spvec_empty smult_spvec_cons
obua@15178
   876
  add_spmat.simps add_spvec.simps
obua@15178
   877
  minus_spmat.simps minus_spvec.simps
obua@15178
   878
  abs_spmat.simps abs_spvec.simps
obua@15178
   879
  diff_spmat_def
obua@15178
   880
  le_spmat.simps le_spvec.simps
obua@15178
   881
obua@15178
   882
lemmas sorted_sp_simps = 
obua@15178
   883
  sorted_spvec.simps
obua@15178
   884
  sorted_spmat.simps
obua@15178
   885
  sorted_sparse_matrix_def
obua@15178
   886
obua@15178
   887
lemma bool1: "(\<not> True) = False"  by blast
obua@15178
   888
lemma bool2: "(\<not> False) = True"  by blast
obua@15178
   889
lemma bool3: "((P\<Colon>bool) \<and> True) = P" by blast
obua@15178
   890
lemma bool4: "(True \<and> (P\<Colon>bool)) = P" by blast
obua@15178
   891
lemma bool5: "((P\<Colon>bool) \<and> False) = False" by blast
obua@15178
   892
lemma bool6: "(False \<and> (P\<Colon>bool)) = False" by blast
obua@15178
   893
lemma bool7: "((P\<Colon>bool) \<or> True) = True" by blast
obua@15178
   894
lemma bool8: "(True \<or> (P\<Colon>bool)) = True" by blast
obua@15178
   895
lemma bool9: "((P\<Colon>bool) \<or> False) = P" by blast
obua@15178
   896
lemma bool10: "(False \<or> (P\<Colon>bool)) = P" by blast
obua@15178
   897
lemmas boolarith = bool1 bool2 bool3 bool4 bool5 bool6 bool7 bool8 bool9 bool10
obua@15178
   898
obua@15178
   899
lemma if_case_eq: "(if b then x else y) = (case b of True => x | False => y)" by simp
obua@15178
   900
obua@15178
   901
lemma spm_linprog_dual_estimate_1:
obua@15178
   902
  assumes  
obua@15178
   903
  "sorted_sparse_matrix A1"
obua@15178
   904
  "sorted_sparse_matrix A2"
obua@15178
   905
  "sorted_sparse_matrix c1"
obua@15178
   906
  "sorted_sparse_matrix c2"
obua@15178
   907
  "sorted_sparse_matrix y"
obua@15178
   908
  "sorted_spvec b"
obua@15178
   909
  "sorted_spvec r"
obua@15178
   910
  "le_spmat ([], y)"
obua@15178
   911
  "A * x \<le> sparse_row_matrix (b::('a::lordered_ring) spmat)"
obua@15178
   912
  "sparse_row_matrix A1 <= A"
obua@15178
   913
  "A <= sparse_row_matrix A2"
obua@15178
   914
  "sparse_row_matrix c1 <= c"
obua@15178
   915
  "c <= sparse_row_matrix c2"
obua@15178
   916
  "abs x \<le> sparse_row_matrix r"
obua@15178
   917
  shows
obua@15178
   918
  "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
   919
  abs_spmat (diff_spmat (mult_spmat y A1) c1)), diff_spmat c2 c1)) r))"
obua@15178
   920
  by (insert prems, simp add: sparse_row_matrix_op_simps linprog_dual_estimate_1[where A=A])
obua@15009
   921
obua@15009
   922
end