src/HOL/Matrix/MatrixLP.ML
author obua
Fri, 03 Sep 2004 20:20:44 +0200
changeset 15180 6d3f59785197
parent 15178 5f621aa35c25
permissions -rw-r--r--
float2real is now globally available
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     1
use "MatrixLP_gensimp.ML";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     2
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     3
signature MATRIX_LP = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     4
sig
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     5
  val lp_dual_estimate : string -> int -> thm
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     6
  val simplify : thm -> thm
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     7
end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     8
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     9
structure MatrixLP : MATRIX_LP =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    10
struct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    11
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    12
val _ = SimprocsCodegen.use_simprocs_code sg geninput
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    13
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    14
val simp_le_spmat = simp_SparseMatrix_le_spmat_'_mult__'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real'''''_'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real'''''_bool'
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    15
val simp_add_spmat = simp_SparseMatrix_add_spmat_'_mult__'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real'''''_'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real'''''_List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real'''''
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    16
val simp_diff_spmat = simp_SparseMatrix_diff_spmat_'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''_List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''_List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''' 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    17
val simp_abs_spmat = simp_SparseMatrix_abs_spmat_'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''_List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''' 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    18
val simp_mult_spmat = simp_SparseMatrix_mult_spmat_'List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''_List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real''''_List_list_'_mult__'nat'_'List_list_'_mult__'nat'_'RealDef_real'''''
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    19
val simp_sorted_sparse_matrix = simp_SparseMatrix_sorted_sparse_matrix
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    20
val simp_sorted_spvec = simp_SparseMatrix_sorted_spvec
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    21
	   
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    22
fun single_arg simp ct = snd (simp (snd (Thm.dest_comb ct)))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    23
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    24
fun double_arg simp ct =  
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    25
	let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    26
	    val (a, y) = Thm.dest_comb ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    27
	    val (_, x) = Thm.dest_comb a
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    28
	in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    29
	    snd (simp x y)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    30
	end      
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    31
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    32
fun spmc ct =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    33
    (case term_of ct of	 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    34
	 ((Const ("SparseMatrix.le_spmat", _)) $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    35
	 single_arg simp_le_spmat ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    36
       | ((Const ("SparseMatrix.add_spmat", _)) $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    37
	 single_arg simp_add_spmat ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    38
       | ((Const ("SparseMatrix.diff_spmat", _)) $ _ $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    39
	 double_arg simp_diff_spmat ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    40
       | ((Const ("SparseMatrix.abs_spmat", _)) $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    41
	 single_arg simp_abs_spmat ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    42
       | ((Const ("SparseMatrix.mult_spmat", _)) $ _ $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    43
	 double_arg simp_mult_spmat ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    44
       | ((Const ("SparseMatrix.sorted_sparse_matrix", _)) $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    45
	 single_arg simp_sorted_sparse_matrix ct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    46
       | ((Const ("SparseMatrix.sorted_spvec", ty)) $ _) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    47
	 single_arg simp_sorted_spvec ct 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    48
      | _ => raise Fail_conv)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    49
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    50
fun lp_dual_estimate lptfile prec =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    51
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    52
	val th = inst_real (thm "SparseMatrix.spm_linprog_dual_estimate_1")
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    53
	val sg = sign_of (theory "MatrixLP")
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    54
	val (y, (A1, A2), (c1, c2), b, r) = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    55
	    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    56
		open fspmlp
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    57
		val l = load lptfile prec false
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    58
	    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    59
		(y l, A l, c l, b l, r l)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    60
	    end		
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    61
	fun var s x = (cterm_of sg (Var ((s,0), FloatSparseMatrixBuilder.real_spmatT)), x)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    62
	val th = Thm.instantiate ([], [var "A1" A1, var "A2" A2, var "y" y, var "c1" c1, var "c2" c2, var "r" r, var "b" b]) th
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    63
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    64
	th
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    65
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    66
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    67
fun simplify th = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    68
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    69
	val simp_th = botc spmc (cprop_of th)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    70
	val th = strip_shyps (equal_elim simp_th th)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    71
	fun removeTrue th = removeTrue (implies_elim th TrueI) handle _ => th
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    72
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    73
	removeTrue th
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    74
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    75
15180
6d3f59785197 float2real is now globally available
obua
parents: 15178
diff changeset
    76
end
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    77
15180
6d3f59785197 float2real is now globally available
obua
parents: 15178
diff changeset
    78
fun float2real (x,y) = (Real.fromInt x) * (Math.pow (2.0, Real.fromInt y))
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    79
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    80
val result = ref ([]:thm list)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    81
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    82
fun run c = timeit (fn () => (result := [MatrixLP.simplify c]; ()))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    83