src/HOL/Matrix/FloatSparseMatrixBuilder.ML
author obua
Fri, 03 Sep 2004 17:46:47 +0200
changeset 15179 b8ef323fd41e
child 15196 c7d69df58482
permissions -rw-r--r--
Matrix theory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15179
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     1
structure FloatSparseMatrixBuilder :
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     2
sig
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     3
    include MATRIX_BUILDER
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     4
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     5
    structure cplex : CPLEX
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     6
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     7
    type float = IntInf.int*IntInf.int
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     8
    type floatfunc = float -> float
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
     9
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    10
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    11
    val float2cterm : IntInf.int * IntInf.int -> cterm
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    12
       
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    13
    val approx_value : int -> floatfunc -> string -> cterm * cterm
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    14
    val approx_vector : int -> floatfunc -> vector -> cterm * cterm
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    15
    val approx_matrix : int -> floatfunc -> matrix -> cterm * cterm
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    16
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    17
    val mk_spvec_entry : int -> float -> term
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    18
    val empty_spvec : term
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    19
    val cons_spvec : term -> term -> term
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    20
    val empty_spmat : term
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    21
    val mk_spmat_entry : int -> term -> term
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    22
    val cons_spmat : term -> term -> term
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    23
    val sign_term : term -> cterm
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    24
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    25
    val v_elem_at : vector -> int -> string option
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    26
    val m_elem_at : matrix -> int -> vector option
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    27
    val v_only_elem : vector -> int option
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    28
    val v_fold : ('a * (int * string) -> 'a) -> 'a -> vector -> 'a
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    29
    val m_fold : ('a * (int * vector) -> 'a) -> 'a -> matrix -> 'a
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    30
				   
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    31
    val transpose_matrix : matrix -> matrix
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    32
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    33
    val cut_vector : int -> vector -> vector
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    34
    val cut_matrix : vector -> (int option) -> matrix -> matrix
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    35
    
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    36
    (* cplexProg c A b *)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    37
    val cplexProg : vector -> matrix -> vector -> (cplex.cplexProg * (string -> int))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    38
    (* dual_cplexProg c A b *)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    39
    val dual_cplexProg : vector -> matrix -> vector -> (cplex.cplexProg * (string -> int))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    40
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    41
    val real_spmatT : typ
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    42
    val real_spvecT : typ
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    43
end 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    44
= 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    45
struct
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    46
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    47
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    48
structure Inttab = TableFun(type key = int val ord = (rev_order o int_ord));
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    49
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    50
type vector = string Inttab.table
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    51
type matrix = vector Inttab.table
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    52
type float = IntInf.int*IntInf.int
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    53
type floatfunc = float -> float
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    54
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    55
val th = theory "Float"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    56
val sg = sign_of th
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    57
	 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    58
fun readtype s = Sign.intern_tycon sg s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    59
fun readterm s = Sign.intern_const sg s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    60
		 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    61
val ty_list = readtype "list"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    62
val term_Nil = readterm "Nil"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    63
val term_Cons = readterm "Cons"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    64
		
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    65
val spvec_elemT = HOLogic.mk_prodT (HOLogic.natT, HOLogic.realT)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    66
val spvecT = Type (ty_list, [spvec_elemT])
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    67
val spmat_elemT = HOLogic.mk_prodT (HOLogic.natT, spvecT)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    68
val spmatT = Type (ty_list, [spmat_elemT])
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    69
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    70
val real_spmatT = spmatT
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    71
val real_spvecT = spvecT
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    72
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    73
val empty_matrix_const = Const (term_Nil, spmatT)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    74
val empty_vector_const = Const (term_Nil, spvecT)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    75
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    76
val Cons_spvec_const = Const (term_Cons, spvec_elemT --> spvecT --> spvecT)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    77
val Cons_spmat_const = Const (term_Cons, spmat_elemT --> spmatT --> spmatT) 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    78
			 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    79
val float_const = Const (readterm "float", HOLogic.mk_prodT (HOLogic.intT, HOLogic.intT) --> HOLogic.realT)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    80
		  
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    81
val zero = IntInf.fromInt 0
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    82
val minus_one = IntInf.fromInt ~1
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    83
val two = IntInf.fromInt 2
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    84
	  
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    85
fun mk_intinf ty n =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    86
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    87
	fun mk_bit n = if n = zero then HOLogic.false_const else HOLogic.true_const
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    88
								 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    89
	fun bin_of n = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    90
	    if n = zero then HOLogic.pls_const
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    91
	    else if n = minus_one then HOLogic.min_const
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    92
	    else 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    93
		let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    94
		    val (q,r) = IntInf.divMod (n, two)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    95
		in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    96
		    HOLogic.bit_const $ bin_of q $ mk_bit r
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    97
		end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    98
    in 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
    99
	HOLogic.number_of_const ty $ (bin_of n)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   100
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   101
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   102
fun mk_float (a,b) = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   103
    float_const $ (HOLogic.mk_prod ((mk_intinf HOLogic.intT a), (mk_intinf HOLogic.intT b)))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   104
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   105
fun float2cterm (a,b) = cterm_of sg (mk_float (a,b))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   106
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   107
fun approx_value_term prec f value = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   108
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   109
	val (flower, fupper) = ExactFloatingPoint.approx_decstr_by_bin prec value
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   110
	val (flower, fupper) = (f flower, f fupper)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   111
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   112
	(mk_float flower, mk_float fupper)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   113
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   114
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   115
fun approx_value prec pprt value = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   116
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   117
	val (flower, fupper) = approx_value_term prec pprt value			       
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   118
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   119
	(cterm_of sg flower, cterm_of sg fupper)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   120
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   121
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   122
fun sign_term t = cterm_of sg t
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   123
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   124
val empty_spvec = empty_vector_const
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   125
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   126
val empty_spmat = empty_matrix_const
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   127
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   128
fun mk_spvec_entry i f = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   129
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   130
	val term_i = mk_intinf HOLogic.natT (IntInf.fromInt i)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   131
	val term_f = mk_float f
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   132
    in 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   133
	HOLogic.mk_prod (term_i, term_f)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   134
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   135
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   136
fun mk_spmat_entry i e = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   137
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   138
	val term_i = mk_intinf HOLogic.natT (IntInf.fromInt i)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   139
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   140
	HOLogic.mk_prod (term_i, e)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   141
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   142
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   143
fun cons_spvec h t = Cons_spvec_const $ h $ t
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   144
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   145
fun cons_spmat h t = Cons_spmat_const $ h $ t 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   146
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   147
fun approx_vector_term prec pprt vector = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   148
    let 	 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   149
	fun app ((vlower, vupper), (index, s)) = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   150
	    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   151
		val (flower, fupper) = approx_value_term prec pprt s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   152
		val index = mk_intinf HOLogic.natT (IntInf.fromInt index)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   153
		val elower = HOLogic.mk_prod (index, flower)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   154
		val eupper = HOLogic.mk_prod (index, fupper)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   155
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   156
		(Cons_spvec_const $ elower $ vlower,
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   157
		 Cons_spvec_const $ eupper $ vupper)	
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   158
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   159
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   160
	Inttab.foldl app ((empty_vector_const, empty_vector_const), vector)	
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   161
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   162
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   163
fun approx_matrix_term prec pprt matrix =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   164
    let 	 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   165
	fun app ((mlower, mupper), (index, vector)) = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   166
	    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   167
		val (vlower, vupper) = approx_vector_term prec pprt vector  
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   168
		val index = mk_intinf HOLogic.natT (IntInf.fromInt index)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   169
		val elower = HOLogic.mk_prod (index, vlower)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   170
		val eupper = HOLogic.mk_prod (index, vupper)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   171
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   172
		(Cons_spmat_const $ elower $ mlower,
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   173
		 Cons_spmat_const $ eupper $ mupper)		
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   174
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   175
		
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   176
	val (mlower, mupper) = Inttab.foldl app ((empty_matrix_const, empty_matrix_const), matrix)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   177
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   178
	Inttab.foldl app ((empty_matrix_const, empty_matrix_const), matrix)	
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   179
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   180
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   181
fun approx_vector prec pprt vector =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   182
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   183
	val (l, u) = approx_vector_term prec pprt vector
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   184
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   185
	(cterm_of sg l, cterm_of sg u)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   186
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   187
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   188
fun approx_matrix prec pprt matrix = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   189
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   190
	val (l, u) = approx_matrix_term prec pprt matrix
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   191
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   192
	(cterm_of sg l, cterm_of sg u)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   193
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   194
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   195
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   196
exception Nat_expected of int;
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   197
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   198
val zero_interval = approx_value_term 1 I "0"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   199
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   200
fun set_elem vector index str = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   201
    if index < 0 then 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   202
	raise (Nat_expected index)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   203
    else if (approx_value_term 1 I str) = zero_interval then 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   204
	vector
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   205
    else  
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   206
	Inttab.update ((index, str), vector)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   207
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   208
fun set_vector matrix index vector = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   209
    if index < 0 then
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   210
	raise (Nat_expected index)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   211
    else if Inttab.is_empty vector then
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   212
	matrix
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   213
    else
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   214
	Inttab.update ((index, vector), matrix)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   215
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   216
val empty_matrix = Inttab.empty
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   217
val empty_vector = Inttab.empty
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   218
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   219
(* dual stuff *)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   220
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   221
structure cplex = Cplex
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   222
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   223
fun transpose_matrix matrix = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   224
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   225
	fun upd m j i x =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   226
	    case Inttab.lookup (m, j) of
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   227
		Some v => Inttab.update ((j, Inttab.update ((i, x), v)), m)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   228
	      | None => Inttab.update ((j, Inttab.update ((i, x), Inttab.empty)), m) 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   229
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   230
	fun updv j (m, (i, s)) = upd m i j s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   231
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   232
	fun updm (m, (j, v)) = Inttab.foldl (updv j) (m, v)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   233
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   234
	Inttab.foldl updm (empty_matrix, matrix)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   235
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   236
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   237
exception No_name of string;
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   238
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   239
exception Superfluous_constr_right_hand_sides
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   240
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   241
fun cplexProg c A b = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   242
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   243
	val ytable = ref Inttab.empty
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   244
	fun indexof s = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   245
	    if String.size s = 0 then raise (No_name s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   246
	    else case Int.fromString (String.extract(s, 1, NONE)) of 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   247
		     SOME i => i | NONE => raise (No_name s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   248
				     
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   249
	fun nameof i = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   250
	    let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   251
		val s = "x"^(Int.toString i)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   252
		val _ = ytable := (Inttab.update ((i, s), !ytable))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   253
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   254
		s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   255
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   256
	    
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   257
	fun split_numstr s = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   258
	    if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   259
	    else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   260
	    else (true, s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   261
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   262
	fun mk_term index s =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   263
	    let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   264
		val (p, s) = split_numstr s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   265
		val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   266
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   267
		if p then prod else cplex.cplexNeg prod
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   268
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   269
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   270
	fun vec2sum vector =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   271
	    cplex.cplexSum (Inttab.foldl (fn (list, (index, s)) => (mk_term index s)::list) ([], vector))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   272
		       		       
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   273
	fun mk_constr index vector c = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   274
	    let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   275
		val s = case Inttab.lookup (c, index) of Some s => s | None => "0"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   276
		val (p, s) = split_numstr s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   277
		val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   278
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   279
		(None, cplex.cplexConstr (cplex.cplexLeq, (vec2sum vector, num)))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   280
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   281
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   282
	fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   283
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   284
	val (list, b) = Inttab.foldl 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   285
			    (fn ((list, c), (index, v)) => ((mk_constr index v c)::list, delete index c))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   286
			    (([], b), A)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   287
	val _ = if Inttab.is_empty b then () else raise Superfluous_constr_right_hand_sides
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   288
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   289
	fun mk_free y = cplex.cplexBounds (cplex.cplexNeg cplex.cplexInf, cplex.cplexLeq, 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   290
					   cplex.cplexVar y, cplex.cplexLeq,
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   291
					   cplex.cplexInf)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   292
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   293
	val yvars = Inttab.foldl (fn (l, (i, y)) => (mk_free y)::l) ([], !ytable)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   294
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   295
	val prog = cplex.cplexProg ("original", cplex.cplexMaximize (vec2sum c), list, yvars)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   296
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   297
	(prog, indexof)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   298
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   299
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   300
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   301
fun dual_cplexProg c A b = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   302
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   303
	fun indexof s = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   304
	    if String.size s = 0 then raise (No_name s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   305
	    else case Int.fromString (String.extract(s, 1, NONE)) of 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   306
		     SOME i => i | NONE => raise (No_name s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   307
				     
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   308
	fun nameof i = "y"^(Int.toString i)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   309
	    
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   310
	fun split_numstr s = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   311
	    if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   312
	    else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   313
	    else (true, s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   314
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   315
	fun mk_term index s =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   316
	    let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   317
		val (p, s) = split_numstr s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   318
		val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   319
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   320
		if p then prod else cplex.cplexNeg prod
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   321
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   322
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   323
	fun vec2sum vector =
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   324
	    cplex.cplexSum (Inttab.foldl (fn (list, (index, s)) => (mk_term index s)::list) ([], vector))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   325
		       		       
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   326
	fun mk_constr index vector c = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   327
	    let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   328
		val s = case Inttab.lookup (c, index) of Some s => s | None => "0"
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   329
		val (p, s) = split_numstr s
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   330
		val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   331
	    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   332
		(None, cplex.cplexConstr (cplex.cplexEq, (vec2sum vector, num)))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   333
	    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   334
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   335
	fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   336
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   337
	val (list, c) = Inttab.foldl 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   338
			    (fn ((list, c), (index, v)) => ((mk_constr index v c)::list, delete index c))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   339
			    (([], c), transpose_matrix A)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   340
	val _ = if Inttab.is_empty c then () else raise Superfluous_constr_right_hand_sides
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   341
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   342
	val prog = cplex.cplexProg ("dual", cplex.cplexMinimize (vec2sum b), list, [])
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   343
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   344
	(prog, indexof)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   345
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   346
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   347
fun cut_vector size v = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   348
    let
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   349
	val count = ref 0 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   350
	fun app (v, (i, s)) = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   351
	    if (!count < size) then
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   352
		(count := !count +1 ; Inttab.update ((i,s),v))
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   353
	    else
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   354
		v
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   355
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   356
	Inttab.foldl app (empty_vector, v)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   357
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   358
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   359
fun cut_matrix vfilter vsize m = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   360
    let 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   361
	fun app (m, (i, v)) = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   362
	    if (Inttab.lookup (vfilter, i) = None) then 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   363
		m 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   364
	    else 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   365
		case vsize of
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   366
		    None => Inttab.update ((i,v), m)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   367
		  | Some s => Inttab.update((i, cut_vector s v),m)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   368
    in
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   369
	Inttab.foldl app (empty_matrix, m)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   370
    end
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   371
		 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   372
fun v_elem_at v i = Inttab.lookup (v,i)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   373
fun m_elem_at m i = Inttab.lookup (m,i)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   374
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   375
fun v_only_elem v = 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   376
    case Inttab.min_key v of
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   377
	None => None
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   378
      | Some vmin => (case Inttab.max_key v of
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   379
			  None => Some vmin
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   380
			| Some vmax => if vmin = vmax then Some vmin else None)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   381
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   382
fun v_fold f a v = Inttab.foldl f (a,v) 
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   383
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   384
fun m_fold f a m = Inttab.foldl f (a,m)
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   385
b8ef323fd41e Matrix theory
obua
parents:
diff changeset
   386
end;