Debian Codegraph @

plugins/micromega/sos.ml

raw symbols SCIP
1 (* ========================================================================= *)
2 (* - This code originates from John Harrison's HOL LIGHT 2.30 *)
3 (* (see file LICENSE.sos for license, copyright and disclaimer) *)
4 (* - Laurent Théry ([email protected]) has isolated the HOL *)
5 (* independent bits *)
6 (* - Frédéric Besson ([email protected]) is using it to feed micromega *)
7 (* ========================================================================= *)
8
9 (* ========================================================================= *)
10 (* Nonlinear universal reals procedure using SOS decomposition. *)
11 (* ========================================================================= *)
12
13 open NumCompat
14 open Q.Notations
15 open Sos_types
16 open Sos_lib
17
18 (*
19 prioritize_real();;
20 *)
21
22 let debugging = ref false
23
24 exception Sanity
25
26 (* ------------------------------------------------------------------------- *)
27 (* Turn a rational into a decimal string with d sig digits. *)
28 (* ------------------------------------------------------------------------- *)
29
30 let decimalize =
31 let rec normalize y =
32 if Q.abs y </ Q.one // Q.ten then normalize (Q.ten */ y) - 1
33 else if Q.abs y >=/ Q.one then normalize (y // Q.ten) + 1
34 else 0
35 in
36 fun d x ->
37 if x =/ Q.zero then "0.0"
38 else
39 let y = Q.abs x in
40 let e = normalize y in
41 let z = (Q.pow10 (-e) */ y) +/ Q.one in
42 let k = Q.round (Q.pow10 d */ z) in
43 (if x </ Q.zero then "-0." else "0.")
44 ^ implode (List.tl (explode (Q.to_string k)))
45 ^ if e = 0 then "" else "e" ^ string_of_int e
46
47 (* ------------------------------------------------------------------------- *)
48 (* Iterations over numbers, and lists indexed by numbers. *)
49 (* ------------------------------------------------------------------------- *)
50
51 let rec itern k l f a =
52 match l with [] -> a | h :: t -> itern (k + 1) t f (f h k a)
53
54 let rec iter (m, n) f a = if n < m then a else iter (m + 1, n) f (f m a)
55
56 (* ------------------------------------------------------------------------- *)
57 (* The main types. *)
58 (* ------------------------------------------------------------------------- *)
59
60 type vector = int * (int, Q.t) func
61 type matrix = (int * int) * (int * int, Q.t) func
62 type monomial = (vname, int) func
63 type poly = (monomial, Q.t) func
64
65 (* ------------------------------------------------------------------------- *)
66 (* Assignment avoiding zeros. *)
67 (* ------------------------------------------------------------------------- *)
68
69 let ( |--> ) x y a = if y =/ Q.zero then a else (x |-> y) a
70
71 (* ------------------------------------------------------------------------- *)
72 (* This can be generic. *)
73 (* ------------------------------------------------------------------------- *)
74
75 let element (d, v) i = tryapplyd v i Q.zero
76 let mapa f (d, v) = (d, foldl (fun a i c -> (i |--> f c) a) undefined v)
77 let is_zero (d, v) = match v with Empty -> true | _ -> false
78
79 (* ------------------------------------------------------------------------- *)
80 (* Vectors. Conventionally indexed 1..n. *)
81 (* ------------------------------------------------------------------------- *)
82
83 let vector_0 n : vector = (n, undefined)
84 let dim (v : vector) = fst v
85
86 let vector_const c n =
87 if c =/ Q.zero then vector_0 n
88 else ((n, List.fold_right (fun k -> k |-> c) (1 -- n) undefined) : vector)
89
90 let vector_cmul c (v : vector) =
91 let n = dim v in
92 if c =/ Q.zero then vector_0 n else (n, mapf (fun x -> c */ x) (snd v))
93
94 let vector_of_list l =
95 let n = List.length l in
96 ((n, List.fold_right2 ( |-> ) (1 -- n) l undefined) : vector)
97
98 (* ------------------------------------------------------------------------- *)
99 (* Matrices; again rows and columns indexed from 1. *)
100 (* ------------------------------------------------------------------------- *)
101
102 let matrix_0 (m, n) : matrix = ((m, n), undefined)
103 let dimensions (m : matrix) = fst m
104
105 let matrix_cmul c (m : matrix) =
106 let i, j = dimensions m in
107 if c =/ Q.zero then matrix_0 (i, j)
108 else ((i, j), mapf (fun x -> c */ x) (snd m))
109
110 let matrix_neg (m : matrix) : matrix = (dimensions m, mapf Q.neg (snd m))
111
112 let matrix_add (m1 : matrix) (m2 : matrix) =
113 let d1 = dimensions m1 and d2 = dimensions m2 in
114 if d1 <> d2 then failwith "matrix_add: incompatible dimensions"
115 else ((d1, combine ( +/ ) (fun x -> x =/ Q.zero) (snd m1) (snd m2)) : matrix)
116
117 let row k (m : matrix) =
118 let i, j = dimensions m in
119 ( ( j
120 , foldl
121 (fun a (i, j) c -> if i = k then (j |-> c) a else a)
122 undefined (snd m) )
123 : vector )
124
125 let column k (m : matrix) =
126 let i, j = dimensions m in
127 ( ( i
128 , foldl
129 (fun a (i, j) c -> if j = k then (i |-> c) a else a)
130 undefined (snd m) )
131 : vector )
132
133 let diagonal (v : vector) =
134 let n = dim v in
135 (((n, n), foldl (fun a i c -> ((i, i) |-> c) a) undefined (snd v)) : matrix)
136
137 (* ------------------------------------------------------------------------- *)
138 (* Monomials. *)
139 (* ------------------------------------------------------------------------- *)
140 let monomial_1 = (undefined : monomial)
141 let monomial_var x : monomial = x |=> 1
142
143 let (monomial_mul : monomial -> monomial -> monomial) =
144 combine ( + ) (fun x -> false)
145
146 let monomial_degree x (m : monomial) = tryapplyd m x 0
147 let monomial_multidegree (m : monomial) = foldl (fun a x k -> k + a) 0 m
148 let monomial_variables m = dom m
149
150 (* ------------------------------------------------------------------------- *)
151 (* Polynomials. *)
152 (* ------------------------------------------------------------------------- *)
153 let poly_0 = (undefined : poly)
154 let poly_isconst (p : poly) = foldl (fun a m c -> m = monomial_1 && a) true p
155 let poly_var x : poly = monomial_var x |=> Q.one
156 let poly_const c = if c =/ Q.zero then poly_0 else monomial_1 |=> c
157
158 let poly_cmul c (p : poly) =
159 if c =/ Q.zero then poly_0 else mapf (fun x -> c */ x) p
160
161 let poly_neg (p : poly) : poly = mapf Q.neg p
162
163 let poly_add (p1 : poly) (p2 : poly) : poly =
164 combine ( +/ ) (fun x -> x =/ Q.zero) p1 p2
165
166 let poly_sub p1 p2 = poly_add p1 (poly_neg p2)
167
168 let poly_cmmul (c, m) (p : poly) =
169 if c =/ Q.zero then poly_0
170 else if m = monomial_1 then mapf (fun d -> c */ d) p
171 else foldl (fun a m' d -> (monomial_mul m m' |-> c */ d) a) poly_0 p
172
173 let poly_mul (p1 : poly) (p2 : poly) =
174 foldl (fun a m c -> poly_add (poly_cmmul (c, m) p2) a) poly_0 p1
175
176 let poly_square p = poly_mul p p
177
178 let rec poly_pow p k =
179 if k = 0 then poly_const Q.one
180 else if k = 1 then p
181 else
182 let q = poly_square (poly_pow p (k / 2)) in
183 if k mod 2 = 1 then poly_mul p q else q
184
185 let degree x (p : poly) = foldl (fun a m c -> max (monomial_degree x m) a) 0 p
186
187 let multidegree (p : poly) =
188 foldl (fun a m c -> max (monomial_multidegree m) a) 0 p
189
190 let poly_variables (p : poly) =
191 foldr (fun m c -> union (monomial_variables m)) p []
192
193 (* ------------------------------------------------------------------------- *)
194 (* Order monomials for human presentation. *)
195 (* ------------------------------------------------------------------------- *)
196
197 let humanorder_varpow (x1, k1) (x2, k2) = x1 < x2 || (x1 = x2 && k1 > k2)
198
199 let humanorder_monomial =
200 let rec ord l1 l2 =
201 match (l1, l2) with
202 | _, [] -> true
203 | [], _ -> false
204 | h1 :: t1, h2 :: t2 -> humanorder_varpow h1 h2 || (h1 = h2 && ord t1 t2)
205 in
206 fun m1 m2 ->
207 m1 = m2
208 || ord
209 (sort humanorder_varpow (graph m1))
210 (sort humanorder_varpow (graph m2))
211
212 (* ------------------------------------------------------------------------- *)
213 (* Conversions to strings. *)
214 (* ------------------------------------------------------------------------- *)
215
216 let string_of_vname (v : vname) : string = (v : string)
217
218 let string_of_varpow x k =
219 if k = 1 then string_of_vname x else string_of_vname x ^ "^" ^ string_of_int k
220
221 let string_of_monomial m =
222 if m = monomial_1 then "1"
223 else
224 let vps =
225 List.fold_right
226 (fun (x, k) a -> string_of_varpow x k :: a)
227 (sort humanorder_varpow (graph m))
228 []
229 in
230 String.concat "*" vps
231
232 let string_of_cmonomial (c, m) =
233 if m = monomial_1 then Q.to_string c
234 else if c =/ Q.one then string_of_monomial m
235 else Q.to_string c ^ "*" ^ string_of_monomial m
236
237 let string_of_poly (p : poly) =
238 if p = poly_0 then "<<0>>"
239 else
240 let cms =
241 sort (fun (m1, _) (m2, _) -> humanorder_monomial m1 m2) (graph p)
242 in
243 let s =
244 List.fold_left
245 (fun a (m, c) ->
246 if c </ Q.zero then a ^ " - " ^ string_of_cmonomial (Q.neg c, m)
247 else a ^ " + " ^ string_of_cmonomial (c, m))
248 "" cms
249 in
250 let s1 = String.sub s 0 3 and s2 = String.sub s 3 (String.length s - 3) in
251 "<<" ^ (if s1 = " + " then s2 else "-" ^ s2) ^ ">>"
252
253 (* ------------------------------------------------------------------------- *)
254 (* Printers. *)
255 (* ------------------------------------------------------------------------- *)
256
257 (*
258 let print_vector v = Format.print_string(string_of_vector 0 20 v);;
259
260 let print_matrix m = Format.print_string(string_of_matrix 20 m);;
261
262 let print_monomial m = Format.print_string(string_of_monomial m);;
263
264 let print_poly m = Format.print_string(string_of_poly m);;
265
266 #install_printer print_vector;;
267 #install_printer print_matrix;;
268 #install_printer print_monomial;;
269 #install_printer print_poly;;
270 *)
271
272 (* ------------------------------------------------------------------------- *)
273 (* Conversion from term. *)
274 (* ------------------------------------------------------------------------- *)
275
276 let rec poly_of_term t =
277 match t with
278 | Zero -> poly_0
279 | Const n -> poly_const n
280 | Var x -> poly_var x
281 | Opp t1 -> poly_neg (poly_of_term t1)
282 | Add (l, r) -> poly_add (poly_of_term l) (poly_of_term r)
283 | Sub (l, r) -> poly_sub (poly_of_term l) (poly_of_term r)
284 | Mul (l, r) -> poly_mul (poly_of_term l) (poly_of_term r)
285 | Pow (t, n) -> poly_pow (poly_of_term t) n
286
287 (* ------------------------------------------------------------------------- *)
288 (* String of vector (just a list of space-separated numbers). *)
289 (* ------------------------------------------------------------------------- *)
290
291 let sdpa_of_vector (v : vector) =
292 let n = dim v in
293 let strs = List.map (o (decimalize 20) (element v)) (1 -- n) in
294 String.concat " " strs ^ "\n"
295
296 (* ------------------------------------------------------------------------- *)
297 (* String for a matrix numbered k, in SDPA sparse format. *)
298 (* ------------------------------------------------------------------------- *)
299
300 let sdpa_of_matrix k (m : matrix) =
301 let pfx = string_of_int k ^ " 1 " in
302 let ms =
303 foldr (fun (i, j) c a -> if i > j then a else ((i, j), c) :: a) (snd m) []
304 in
305 let mss = sort (increasing fst) ms in
306 List.fold_right
307 (fun ((i, j), c) a ->
308 pfx ^ string_of_int i ^ " " ^ string_of_int j ^ " " ^ decimalize 20 c
309 ^ "\n" ^ a)
310 mss ""
311
312 (* ------------------------------------------------------------------------- *)
313 (* String in SDPA sparse format for standard SDP problem: *)
314 (* *)
315 (* X = v_1 * [M_1] + ... + v_m * [M_m] - [M_0] must be PSD *)
316 (* Minimize obj_1 * v_1 + ... obj_m * v_m *)
317 (* ------------------------------------------------------------------------- *)
318
319 let sdpa_of_problem comment obj mats =
320 let m = List.length mats - 1 and n, _ = dimensions (List.hd mats) in
321 "\"" ^ comment ^ "\"\n" ^ string_of_int m ^ "\n" ^ "1\n" ^ string_of_int n
322 ^ "\n" ^ sdpa_of_vector obj
323 ^ List.fold_right2
324 (fun k m a -> sdpa_of_matrix (k - 1) m ^ a)
325 (1 -- List.length mats)
326 mats ""
327
328 (* ------------------------------------------------------------------------- *)
329 (* More parser basics. *)
330 (* ------------------------------------------------------------------------- *)
331
332 let word s =
333 end_itlist
334 (fun p1 p2 -> p1 ++ p2 >> fun (s, t) -> s ^ t)
335 (List.map a (explode s))
336
337 let token s =
338 many (some isspace) ++ word s ++ many (some isspace) >> fun ((_, t), _) -> t
339
340 let decimal =
341 let ( || ) = parser_or in
342 let numeral = some isnum in
343 let decimalint = atleast 1 numeral >> o Q.of_string implode in
344 let decimalfrac =
345 atleast 1 numeral
346 >> fun s -> Q.of_string (implode s) // Q.pow10 (List.length s)
347 in
348 let decimalsig =
349 decimalint ++ possibly (a "." ++ decimalfrac >> snd)
350 >> function h, [x] -> h +/ x | h, _ -> h
351 in
352 let signed prs = a "-" ++ prs >> o Q.neg snd || a "+" ++ prs >> snd || prs in
353 let exponent = (a "e" || a "E") ++ signed decimalint >> snd in
354 signed decimalsig ++ possibly exponent
355 >> function h, [x] -> h */ Q.power 10 x | h, _ -> h
356
357 let mkparser p s =
358 let x, rst = p (explode s) in
359 if rst = [] then x else failwith "mkparser: unparsed input"
360
361 (* ------------------------------------------------------------------------- *)
362 (* Parse back a vector. *)
363 (* ------------------------------------------------------------------------- *)
364
365 let _parse_sdpaoutput, parse_csdpoutput =
366 let ( || ) = parser_or in
367 let vector =
368 token "{" ++ listof decimal (token ",") "decimal" ++ token "}"
369 >> fun ((_, v), _) -> vector_of_list v
370 in
371 let rec skipupto dscr prs inp =
372 (dscr ++ prs >> snd || some (fun c -> true) ++ skipupto dscr prs >> snd) inp
373 in
374 let ignore inp = ((), []) in
375 let sdpaoutput =
376 skipupto (word "xVec" ++ token "=") (vector ++ ignore >> fst)
377 in
378 let csdpoutput =
379 (decimal ++ many (a " " ++ decimal >> snd) >> fun (h, t) -> h :: t)
380 ++ (a " " ++ a "\n" ++ ignore)
381 >> o vector_of_list fst
382 in
383 (mkparser sdpaoutput, mkparser csdpoutput)
384
385 (* ------------------------------------------------------------------------- *)
386 (* The default parameters. Unfortunately this goes to a fixed file. *)
387 (* ------------------------------------------------------------------------- *)
388
389 let _sdpa_default_parameters =
390 "100 unsigned int maxIteration;\n\
391 1.0E-7 double 0.0 < epsilonStar;\n\
392 1.0E2 double 0.0 < lambdaStar;\n\
393 2.0 double 1.0 < omegaStar;\n\
394 -1.0E5 double lowerBound;\n\
395 1.0E5 double upperBound;\n\
396 0.1 double 0.0 <= betaStar < 1.0;\n\
397 0.2 double 0.0 <= betaBar < 1.0, betaStar <= betaBar;\n\
398 0.9 double 0.0 < gammaStar < 1.0;\n\
399 1.0E-7 double 0.0 < epsilonDash;\n"
400
401 (* ------------------------------------------------------------------------- *)
402 (* These were suggested by Makoto Yamashita for problems where we are *)
403 (* right at the edge of the semidefinite cone, as sometimes happens. *)
404 (* ------------------------------------------------------------------------- *)
405
406 let sdpa_alt_parameters =
407 "1000 unsigned int maxIteration;\n\
408 1.0E-7 double 0.0 < epsilonStar;\n\
409 1.0E4 double 0.0 < lambdaStar;\n\
410 2.0 double 1.0 < omegaStar;\n\
411 -1.0E5 double lowerBound;\n\
412 1.0E5 double upperBound;\n\
413 0.1 double 0.0 <= betaStar < 1.0;\n\
414 0.2 double 0.0 <= betaBar < 1.0, betaStar <= betaBar;\n\
415 0.9 double 0.0 < gammaStar < 1.0;\n\
416 1.0E-7 double 0.0 < epsilonDash;\n"
417
418 let _sdpa_params = sdpa_alt_parameters
419
420 (* ------------------------------------------------------------------------- *)
421 (* CSDP parameters; so far I'm sticking with the defaults. *)
422 (* ------------------------------------------------------------------------- *)
423
424 let csdp_default_parameters =
425 "axtol=1.0e-8\n\
426 atytol=1.0e-8\n\
427 objtol=1.0e-8\n\
428 pinftol=1.0e8\n\
429 dinftol=1.0e8\n\
430 maxiter=100\n\
431 minstepfrac=0.9\n\
432 maxstepfrac=0.97\n\
433 minstepp=1.0e-8\n\
434 minstepd=1.0e-8\n\
435 usexzgap=1\n\
436 tweakgap=0\n\
437 affine=0\n\
438 printlevel=1\n"
439
440 let csdp_params = csdp_default_parameters
441
442 (* ------------------------------------------------------------------------- *)
443 (* Now call CSDP on a problem and parse back the output. *)
444 (* ------------------------------------------------------------------------- *)
445
446 let run_csdp dbg obj mats =
447 let input_file = Filename.temp_file "sos" ".dat-s" in
448 let output_file =
449 String.sub input_file 0 (String.length input_file - 6) ^ ".out"
450 and params_file = Filename.concat temp_path "param.csdp" in
451 file_of_string input_file (sdpa_of_problem "" obj mats);
452 file_of_string params_file csdp_params;
453 let rv =
454 Sys.command
455 ( "cd " ^ temp_path ^ "; csdp " ^ input_file ^ " " ^ output_file
456 ^ if dbg then "" else "> /dev/null" )
457 in
458 let op = string_of_file output_file in
459 let res = parse_csdpoutput op in
460 if dbg then () else (Sys.remove input_file; Sys.remove output_file);
461 (rv, res)
462
463 (* ------------------------------------------------------------------------- *)
464 (* Try some apparently sensible scaling first. Note that this is purely to *)
465 (* get a cleaner translation to floating-point, and doesn't affect any of *)
466 (* the results, in principle. In practice it seems a lot better when there *)
467 (* are extreme numbers in the original problem. *)
468 (* ------------------------------------------------------------------------- *)
469
470 let scale_then =
471 let common_denominator amat acc =
472 foldl (fun a m c -> Z.lcm (Q.den c) a) acc amat
473 and maximal_element amat acc =
474 foldl (fun maxa m c -> Q.max maxa (Q.abs c)) acc amat
475 in
476 fun solver obj mats ->
477 let cd1 = Q.of_bigint @@ List.fold_right common_denominator mats Z.one
478 and cd2 = Q.of_bigint @@ common_denominator (snd obj) Z.one in
479 let mats' = List.map (mapf (fun x -> cd1 */ x)) mats
480 and obj' = vector_cmul cd2 obj in
481 let max1 = List.fold_right maximal_element mats' Q.zero
482 and max2 = maximal_element (snd obj') Q.zero in
483 let scal1 = Q.pow2 (20 - int_of_float (log (Q.to_float max1) /. log 2.0))
484 and scal2 = Q.pow2 (20 - int_of_float (log (Q.to_float max2) /. log 2.0)) in
485 let mats'' = List.map (mapf (fun x -> x */ scal1)) mats'
486 and obj'' = vector_cmul scal2 obj' in
487 solver obj'' mats''
488
489 (* ------------------------------------------------------------------------- *)
490 (* Round a vector to "nice" rationals. *)
491 (* ------------------------------------------------------------------------- *)
492
493 let nice_rational n x = Q.round (n */ x) // n
494 let nice_vector n = mapa (nice_rational n)
495
496 (* ------------------------------------------------------------------------- *)
497 (* Reduce linear program to SDP (diagonal matrices) and test with CSDP. This *)
498 (* one tests A [-1;x1;..;xn] >= 0 (i.e. left column is negated constants). *)
499 (* ------------------------------------------------------------------------- *)
500
501 let linear_program_basic a =
502 let m, n = dimensions a in
503 let mats = List.map (fun j -> diagonal (column j a)) (1 -- n)
504 and obj = vector_const Q.one m in
505 let rv, res = run_csdp false obj mats in
506 if rv = 1 || rv = 2 then false
507 else if rv = 0 then true
508 else failwith "linear_program: An error occurred in the SDP solver"
509
510 (* ------------------------------------------------------------------------- *)
511 (* Test whether a point is in the convex hull of others. Rather than use *)
512 (* computational geometry, express as linear inequalities and call CSDP. *)
513 (* This is a bit lazy of me, but it's easy and not such a bottleneck so far. *)
514 (* ------------------------------------------------------------------------- *)
515
516 let in_convex_hull pts pt =
517 let pts1 = (1 :: pt) :: List.map (fun x -> 1 :: x) pts in
518 let pts2 = List.map (fun p -> List.map (fun x -> -x) p @ p) pts1 in
519 let n = List.length pts + 1 and v = 2 * (List.length pt + 1) in
520 let m = v + n - 1 in
521 let mat =
522 ( (m, n)
523 , itern 1 pts2
524 (fun pts j -> itern 1 pts (fun x i -> (i, j) |-> Q.of_int x))
525 (iter (1, n) (fun i -> (v + i, i + 1) |-> Q.one) undefined) )
526 in
527 linear_program_basic mat
528
529 (* ------------------------------------------------------------------------- *)
530 (* Filter down a set of points to a minimal set with the same convex hull. *)
531 (* ------------------------------------------------------------------------- *)
532
533 let minimal_convex_hull =
534 let augment1 = function
535 | [] -> assert false
536 | m :: ms -> if in_convex_hull ms m then ms else ms @ [m]
537 in
538 let augment m ms = funpow 3 augment1 (m :: ms) in
539 fun mons ->
540 let mons' = List.fold_right augment (List.tl mons) [List.hd mons] in
541 funpow (List.length mons') augment1 mons'
542
543 (* ------------------------------------------------------------------------- *)
544 (* Stuff for "equations" (generic A->num functions). *)
545 (* ------------------------------------------------------------------------- *)
546
547 let equation_cmul c eq =
548 if c =/ Q.zero then Empty else mapf (fun d -> c */ d) eq
549
550 let equation_add eq1 eq2 = combine ( +/ ) (fun x -> x =/ Q.zero) eq1 eq2
551
552 let equation_eval assig eq =
553 let value v = apply assig v in
554 foldl (fun a v c -> a +/ (value v */ c)) Q.zero eq
555
556 (* ------------------------------------------------------------------------- *)
557 (* Eliminate all variables, in an essentially arbitrary order. *)
558 (* ------------------------------------------------------------------------- *)
559
560 let eliminate_all_equations one =
561 let choose_variable eq =
562 let v, _ = choose eq in
563 if v = one then
564 let eq' = undefine v eq in
565 if is_undefined eq' then failwith "choose_variable"
566 else
567 let w, _ = choose eq' in
568 w
569 else v
570 in
571 let rec eliminate dun eqs =
572 match eqs with
573 | [] -> dun
574 | eq :: oeqs ->
575 if is_undefined eq then eliminate dun oeqs
576 else
577 let v = choose_variable eq in
578 let a = apply eq v in
579 let eq' = equation_cmul (Q.minus_one // a) (undefine v eq) in
580 let elim e =
581 let b = tryapplyd e v Q.zero in
582 if b =/ Q.zero then e
583 else equation_add e (equation_cmul (Q.neg b // a) eq)
584 in
585 eliminate ((v |-> eq') (mapf elim dun)) (List.map elim oeqs)
586 in
587 fun eqs ->
588 let assig = eliminate undefined eqs in
589 let vs = foldl (fun a x f -> subtract (dom f) [one] @ a) [] assig in
590 (setify vs, assig)
591
592 (* ------------------------------------------------------------------------- *)
593 (* Hence produce the "relevant" monomials: those whose squares lie in the *)
594 (* Newton polytope of the monomials in the input. (This is enough according *)
595 (* to Reznik: "Extremal PSD forms with few terms", Duke Math. Journal, *)
596 (* vol 45, pp. 363--374, 1978. *)
597 (* *)
598 (* These are ordered in sort of decreasing degree. In particular the *)
599 (* constant monomial is last; this gives an order in diagonalization of the *)
600 (* quadratic form that will tend to display constants. *)
601 (* ------------------------------------------------------------------------- *)
602
603 let newton_polytope pol =
604 let vars = poly_variables pol in
605 let mons =
606 List.map (fun m -> List.map (fun x -> monomial_degree x m) vars) (dom pol)
607 and ds = List.map (fun x -> (degree x pol + 1) / 2) vars in
608 let all =
609 List.fold_right (fun n -> allpairs (fun h t -> h :: t) (0 -- n)) ds [[]]
610 and mons' = minimal_convex_hull mons in
611 let all' =
612 List.filter
613 (fun m -> in_convex_hull mons' (List.map (fun x -> 2 * x) m))
614 all
615 in
616 List.map
617 (fun m ->
618 List.fold_right2
619 (fun v i a -> if i = 0 then a else (v |-> i) a)
620 vars m monomial_1)
621 (List.rev all')
622
623 (* ------------------------------------------------------------------------- *)
624 (* Diagonalize (Cholesky/LDU) the matrix corresponding to a quadratic form. *)
625 (* ------------------------------------------------------------------------- *)
626
627 let diag m =
628 let nn = dimensions m in
629 let n = fst nn in
630 if snd nn <> n then failwith "diagonalize: non-square matrix"
631 else
632 let rec diagonalize i m =
633 if is_zero m then []
634 else
635 let a11 = element m (i, i) in
636 if a11 </ Q.zero then failwith "diagonalize: not PSD"
637 else if a11 =/ Q.zero then
638 if is_zero (row i m) then diagonalize (i + 1) m
639 else failwith "diagonalize: not PSD"
640 else
641 let v = row i m in
642 let v' = mapa (fun a1k -> a1k // a11) v in
643 let m' =
644 ( (n, n)
645 , iter
646 (i + 1, n)
647 (fun j ->
648 iter
649 (i + 1, n)
650 (fun k ->
651 (j, k)
652 |--> element m (j, k) -/ (element v j */ element v' k)))
653 undefined )
654 in
655 (a11, v') :: diagonalize (i + 1) m'
656 in
657 diagonalize 1 m
658
659 (* ------------------------------------------------------------------------- *)
660 (* Adjust a diagonalization to collect rationals at the start. *)
661 (* ------------------------------------------------------------------------- *)
662
663 let deration d =
664 if d = [] then (Q.zero, d)
665 else
666 let adj (c, l) =
667 let a =
668 Q.make
669 (foldl (fun a i c -> Z.lcm a (Q.den c)) Z.one (snd l))
670 (foldl (fun a i c -> Z.gcd a (Q.num c)) Z.zero (snd l))
671 in
672 (c // (a */ a), mapa (fun x -> a */ x) l)
673 in
674 let d' = List.map adj d in
675 let a =
676 Q.make
677 (List.fold_right (o Z.lcm (o Q.den fst)) d' Z.one)
678 (List.fold_right (o Z.gcd (o Q.num fst)) d' Z.zero)
679 in
680 (Q.one // a, List.map (fun (c, l) -> (a */ c, l)) d')
681
682 (* ------------------------------------------------------------------------- *)
683 (* Enumeration of monomials with given multidegree bound. *)
684 (* ------------------------------------------------------------------------- *)
685
686 let rec enumerate_monomials d vars =
687 if d < 0 then []
688 else if d = 0 then [undefined]
689 else if vars = [] then [monomial_1]
690 else
691 let alts =
692 List.map
693 (fun k ->
694 let oths = enumerate_monomials (d - k) (List.tl vars) in
695 List.map
696 (fun ks -> if k = 0 then ks else (List.hd vars |-> k) ks)
697 oths)
698 (0 -- d)
699 in
700 end_itlist ( @ ) alts
701
702 (* ------------------------------------------------------------------------- *)
703 (* Enumerate products of distinct input polys with degree <= d. *)
704 (* We ignore any constant input polynomials. *)
705 (* Give the output polynomial and a record of how it was derived. *)
706 (* ------------------------------------------------------------------------- *)
707
708 let rec enumerate_products d pols =
709 if d = 0 then [(poly_const Q.one, Rational_lt Q.one)]
710 else if d < 0 then []
711 else
712 match pols with
713 | [] -> [(poly_const Q.one, Rational_lt Q.one)]
714 | (p, b) :: ps ->
715 let e = multidegree p in
716 if e = 0 then enumerate_products d ps
717 else
718 enumerate_products d ps
719 @ List.map
720 (fun (q, c) -> (poly_mul p q, Product (b, c)))
721 (enumerate_products (d - e) ps)
722
723 (* ------------------------------------------------------------------------- *)
724 (* Multiply equation-parametrized poly by regular poly and add accumulator. *)
725 (* ------------------------------------------------------------------------- *)
726
727 let epoly_pmul p q acc =
728 foldl
729 (fun a m1 c ->
730 foldl
731 (fun b m2 e ->
732 let m = monomial_mul m1 m2 in
733 let es = tryapplyd b m undefined in
734 (m |-> equation_add (equation_cmul c e) es) b)
735 a q)
736 acc p
737
738 (* ------------------------------------------------------------------------- *)
739 (* Convert regular polynomial. Note that we treat (0,0,0) as -1. *)
740 (* ------------------------------------------------------------------------- *)
741
742 let epoly_of_poly p =
743 foldl (fun a m c -> (m |-> ((0, 0, 0) |=> Q.neg c)) a) undefined p
744
745 (* ------------------------------------------------------------------------- *)
746 (* String for block diagonal matrix numbered k. *)
747 (* ------------------------------------------------------------------------- *)
748
749 let sdpa_of_blockdiagonal k m =
750 let pfx = string_of_int k ^ " " in
751 let ents =
752 foldl (fun a (b, i, j) c -> if i > j then a else ((b, i, j), c) :: a) [] m
753 in
754 let entss = sort (increasing fst) ents in
755 List.fold_right
756 (fun ((b, i, j), c) a ->
757 pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j
758 ^ " " ^ decimalize 20 c ^ "\n" ^ a)
759 entss ""
760
761 (* ------------------------------------------------------------------------- *)
762 (* SDPA for problem using block diagonal (i.e. multiple SDPs) *)
763 (* ------------------------------------------------------------------------- *)
764
765 let sdpa_of_blockproblem comment nblocks blocksizes obj mats =
766 let m = List.length mats - 1 in
767 "\"" ^ comment ^ "\"\n" ^ string_of_int m ^ "\n" ^ string_of_int nblocks
768 ^ "\n"
769 ^ String.concat " " (List.map string_of_int blocksizes)
770 ^ "\n" ^ sdpa_of_vector obj
771 ^ List.fold_right2
772 (fun k m a -> sdpa_of_blockdiagonal (k - 1) m ^ a)
773 (1 -- List.length mats)
774 mats ""
775
776 (* ------------------------------------------------------------------------- *)
777 (* Hence run CSDP on a problem in block diagonal form. *)
778 (* ------------------------------------------------------------------------- *)
779
780 let run_csdp dbg nblocks blocksizes obj mats =
781 let input_file = Filename.temp_file "sos" ".dat-s" in
782 let output_file =
783 String.sub input_file 0 (String.length input_file - 6) ^ ".out"
784 and params_file = Filename.concat temp_path "param.csdp" in
785 file_of_string input_file
786 (sdpa_of_blockproblem "" nblocks blocksizes obj mats);
787 file_of_string params_file csdp_params;
788 let rv =
789 Sys.command
790 ( "cd " ^ temp_path ^ "; csdp " ^ input_file ^ " " ^ output_file
791 ^ if dbg then "" else "> /dev/null" )
792 in
793 let op = string_of_file output_file in
794 let res = parse_csdpoutput op in
795 if dbg then () else (Sys.remove input_file; Sys.remove output_file);
796 (rv, res)
797
798 let csdp nblocks blocksizes obj mats =
799 let rv, res = run_csdp !debugging nblocks blocksizes obj mats in
800 if rv = 1 || rv = 2 then failwith "csdp: Problem is infeasible"
801 else if rv = 3 then ()
802 (*Format.print_string "csdp warning: Reduced accuracy";
803 Format.print_newline() *)
804 else if rv <> 0 then failwith ("csdp: error " ^ string_of_int rv)
805 else ();
806 res
807
808 (* ------------------------------------------------------------------------- *)
809 (* 3D versions of matrix operations to consider blocks separately. *)
810 (* ------------------------------------------------------------------------- *)
811
812 let bmatrix_add = combine ( +/ ) (fun x -> x =/ Q.zero)
813
814 let bmatrix_cmul c bm =
815 if c =/ Q.zero then undefined else mapf (fun x -> c */ x) bm
816
817 let bmatrix_neg = bmatrix_cmul Q.minus_one
818
819 (* ------------------------------------------------------------------------- *)
820 (* Smash a block matrix into components. *)
821 (* ------------------------------------------------------------------------- *)
822
823 let blocks blocksizes bm =
824 List.map
825 (fun (bs, b0) ->
826 let m =
827 foldl
828 (fun a (b, i, j) c -> if b = b0 then ((i, j) |-> c) a else a)
829 undefined bm
830 in
831 (((bs, bs), m) : matrix))
832 (List.combine blocksizes (1 -- List.length blocksizes))
833
834 (* ------------------------------------------------------------------------- *)
835 (* Positiv- and Nullstellensatz. Flag "linf" forces a linear representation. *)
836 (* ------------------------------------------------------------------------- *)
837
838 let real_positivnullstellensatz_general linf d eqs leqs pol =
839 let vars =
840 List.fold_right (o union poly_variables)
841 ((pol :: eqs) @ List.map fst leqs)
842 []
843 in
844 let monoid =
845 if linf then
846 (poly_const Q.one, Rational_lt Q.one)
847 :: List.filter (fun (p, c) -> multidegree p <= d) leqs
848 else enumerate_products d leqs
849 in
850 let nblocks = List.length monoid in
851 let mk_idmultiplier k p =
852 let e = d - multidegree p in
853 let mons = enumerate_monomials e vars in
854 let nons = List.combine mons (1 -- List.length mons) in
855 ( mons
856 , List.fold_right
857 (fun (m, n) -> m |-> ((-k, -n, n) |=> Q.one))
858 nons undefined )
859 in
860 let mk_sqmultiplier k (p, c) =
861 let e = (d - multidegree p) / 2 in
862 let mons = enumerate_monomials e vars in
863 let nons = List.combine mons (1 -- List.length mons) in
864 ( mons
865 , List.fold_right
866 (fun (m1, n1) ->
867 List.fold_right
868 (fun (m2, n2) a ->
869 let m = monomial_mul m1 m2 in
870 if n1 > n2 then a
871 else
872 let c = if n1 = n2 then Q.one else Q.two in
873 let e = tryapplyd a m undefined in
874 (m |-> equation_add ((k, n1, n2) |=> c) e) a)
875 nons)
876 nons undefined )
877 in
878 let sqmonlist, sqs =
879 List.split (List.map2 mk_sqmultiplier (1 -- List.length monoid) monoid)
880 and idmonlist, ids =
881 List.split (List.map2 mk_idmultiplier (1 -- List.length eqs) eqs)
882 in
883 let blocksizes = List.map List.length sqmonlist in
884 let bigsum =
885 List.fold_right2
886 (fun p q a -> epoly_pmul p q a)
887 eqs ids
888 (List.fold_right2
889 (fun (p, c) s a -> epoly_pmul p s a)
890 monoid sqs
891 (epoly_of_poly (poly_neg pol)))
892 in
893 let eqns = foldl (fun a m e -> e :: a) [] bigsum in
894 let pvs, assig = eliminate_all_equations (0, 0, 0) eqns in
895 let qvars = (0, 0, 0) :: pvs in
896 let allassig = List.fold_right (fun v -> v |-> (v |=> Q.one)) pvs assig in
897 let mk_matrix v =
898 foldl
899 (fun m (b, i, j) ass ->
900 if b < 0 then m
901 else
902 let c = tryapplyd ass v Q.zero in
903 if c =/ Q.zero then m else ((b, j, i) |-> c) (((b, i, j) |-> c) m))
904 undefined allassig
905 in
906 let diagents =
907 foldl
908 (fun a (b, i, j) e -> if b > 0 && i = j then equation_add e a else a)
909 undefined allassig
910 in
911 let mats = List.map mk_matrix qvars
912 and obj =
913 ( List.length pvs
914 , itern 1 pvs (fun v i -> i |--> tryapplyd diagents v Q.zero) undefined )
915 in
916 let raw_vec =
917 if pvs = [] then vector_0 0
918 else scale_then (csdp nblocks blocksizes) obj mats
919 in
920 let find_rounding d =
921 if !debugging then (
922 Format.print_string ("Trying rounding with limit " ^ Q.to_string d);
923 Format.print_newline () )
924 else ();
925 let vec = nice_vector d raw_vec in
926 let blockmat =
927 iter
928 (1, dim vec)
929 (fun i a ->
930 bmatrix_add (bmatrix_cmul (element vec i) (List.nth mats i)) a)
931 (bmatrix_neg (List.nth mats 0))
932 in
933 let allmats = blocks blocksizes blockmat in
934 (vec, List.map diag allmats)
935 in
936 let vec, ratdias =
937 if pvs = [] then find_rounding Q.one
938 else
939 tryfind find_rounding
940 (List.map Q.of_int (1 -- 31) @ List.map Q.pow2 (5 -- 66))
941 in
942 let newassigs =
943 List.fold_right
944 (fun k -> List.nth pvs (k - 1) |-> element vec k)
945 (1 -- dim vec)
946 ((0, 0, 0) |=> Q.minus_one)
947 in
948 let finalassigs =
949 foldl (fun a v e -> (v |-> equation_eval newassigs e) a) newassigs allassig
950 in
951 let poly_of_epoly p =
952 foldl (fun a v e -> (v |--> equation_eval finalassigs e) a) undefined p
953 in
954 let mk_sos mons =
955 let mk_sq (c, m) =
956 ( c
957 , List.fold_right
958 (fun k a -> (List.nth mons (k - 1) |--> element m k) a)
959 (1 -- List.length mons)
960 undefined )
961 in
962 List.map mk_sq
963 in
964 let sqs = List.map2 mk_sos sqmonlist ratdias
965 and cfs = List.map poly_of_epoly ids in
966 let msq =
967 List.filter
968 (fun (a, b) -> b <> [])
969 (List.map2 (fun a b -> (a, b)) monoid sqs)
970 in
971 let eval_sq sqs =
972 List.fold_right
973 (fun (c, q) -> poly_add (poly_cmul c (poly_mul q q)))
974 sqs poly_0
975 in
976 let sanity =
977 List.fold_right
978 (fun ((p, c), s) -> poly_add (poly_mul p (eval_sq s)))
979 msq
980 (List.fold_right2
981 (fun p q -> poly_add (poly_mul p q))
982 cfs eqs (poly_neg pol))
983 in
984 if not (is_undefined sanity) then raise Sanity
985 else (cfs, List.map (fun (a, b) -> (snd a, b)) msq)
986
987 (* ------------------------------------------------------------------------- *)
988 (* The ordering so we can create canonical HOL polynomials. *)
989 (* ------------------------------------------------------------------------- *)
990
991 let dest_monomial mon = sort (increasing fst) (graph mon)
992
993 let monomial_order =
994 let rec lexorder l1 l2 =
995 match (l1, l2) with
996 | [], [] -> true
997 | vps, [] -> false
998 | [], vps -> true
999 | (x1, n1) :: vs1, (x2, n2) :: vs2 ->
1000 if x1 < x2 then true
1001 else if x2 < x1 then false
1002 else if n1 < n2 then false
1003 else if n2 < n1 then true
1004 else lexorder vs1 vs2
1005 in
1006 fun m1 m2 ->
1007 if m2 = monomial_1 then true
1008 else if m1 = monomial_1 then false
1009 else
1010 let mon1 = dest_monomial m1 and mon2 = dest_monomial m2 in
1011 let deg1 = List.fold_right (o ( + ) snd) mon1 0
1012 and deg2 = List.fold_right (o ( + ) snd) mon2 0 in
1013 if deg1 < deg2 then false
1014 else if deg1 > deg2 then true
1015 else lexorder mon1 mon2
1016
1017 (* ------------------------------------------------------------------------- *)
1018 (* Map back polynomials and their composites to HOL. *)
1019 (* ------------------------------------------------------------------------- *)
1020
1021 let term_of_varpow x k = if k = 1 then Var x else Pow (Var x, k)
1022
1023 let term_of_monomial m =
1024 if m = monomial_1 then Const Q.one
1025 else
1026 let m' = dest_monomial m in
1027 let vps = List.fold_right (fun (x, k) a -> term_of_varpow x k :: a) m' [] in
1028 end_itlist (fun s t -> Mul (s, t)) vps
1029
1030 let term_of_cmonomial (m, c) =
1031 if m = monomial_1 then Const c
1032 else if c =/ Q.one then term_of_monomial m
1033 else Mul (Const c, term_of_monomial m)
1034
1035 let term_of_poly p =
1036 if p = poly_0 then Zero
1037 else
1038 let cms =
1039 List.map term_of_cmonomial
1040 (sort (fun (m1, _) (m2, _) -> monomial_order m1 m2) (graph p))
1041 in
1042 end_itlist (fun t1 t2 -> Add (t1, t2)) cms
1043
1044 let term_of_sqterm (c, p) = Product (Rational_lt c, Square (term_of_poly p))
1045
1046 let term_of_sos (pr, sqs) =
1047 if sqs = [] then pr
1048 else
1049 Product
1050 (pr, end_itlist (fun a b -> Sum (a, b)) (List.map term_of_sqterm sqs))
1051
1052 (* ------------------------------------------------------------------------- *)
1053 (* Some combinatorial helper functions. *)
1054 (* ------------------------------------------------------------------------- *)
1055
1056 let rec allpermutations l =
1057 if l = [] then [[]]
1058 else
1059 List.fold_right
1060 (fun h acc ->
1061 List.map (fun t -> h :: t) (allpermutations (subtract l [h])) @ acc)
1062 l []
1063
1064 let changevariables_monomial zoln (m : monomial) =
1065 foldl (fun a x k -> (List.assoc x zoln |-> k) a) monomial_1 m
1066
1067 let changevariables zoln pol =
1068 foldl (fun a m c -> (changevariables_monomial zoln m |-> c) a) poly_0 pol
1069
1070 (* ------------------------------------------------------------------------- *)
1071 (* Return to original non-block matrices. *)
1072 (* ------------------------------------------------------------------------- *)
1073
1074 let sdpa_of_vector (v : vector) =
1075 let n = dim v in
1076 let strs = List.map (o (decimalize 20) (element v)) (1 -- n) in
1077 String.concat " " strs ^ "\n"
1078
1079 let sdpa_of_matrix k (m : matrix) =
1080 let pfx = string_of_int k ^ " 1 " in
1081 let ms =
1082 foldr (fun (i, j) c a -> if i > j then a else ((i, j), c) :: a) (snd m) []
1083 in
1084 let mss = sort (increasing fst) ms in
1085 List.fold_right
1086 (fun ((i, j), c) a ->
1087 pfx ^ string_of_int i ^ " " ^ string_of_int j ^ " " ^ decimalize 20 c
1088 ^ "\n" ^ a)
1089 mss ""
1090
1091 let sdpa_of_problem comment obj mats =
1092 let m = List.length mats - 1 and n, _ = dimensions (List.hd mats) in
1093 "\"" ^ comment ^ "\"\n" ^ string_of_int m ^ "\n" ^ "1\n" ^ string_of_int n
1094 ^ "\n" ^ sdpa_of_vector obj
1095 ^ List.fold_right2
1096 (fun k m a -> sdpa_of_matrix (k - 1) m ^ a)
1097 (1 -- List.length mats)
1098 mats ""
1099
1100 let run_csdp dbg obj mats =
1101 let input_file = Filename.temp_file "sos" ".dat-s" in
1102 let output_file =
1103 String.sub input_file 0 (String.length input_file - 6) ^ ".out"
1104 and params_file = Filename.concat temp_path "param.csdp" in
1105 file_of_string input_file (sdpa_of_problem "" obj mats);
1106 file_of_string params_file csdp_params;
1107 let rv =
1108 Sys.command
1109 ( "cd " ^ temp_path ^ "; csdp " ^ input_file ^ " " ^ output_file
1110 ^ if dbg then "" else "> /dev/null" )
1111 in
1112 let op = string_of_file output_file in
1113 let res = parse_csdpoutput op in
1114 if dbg then () else (Sys.remove input_file; Sys.remove output_file);
1115 (rv, res)
1116
1117 let csdp obj mats =
1118 let rv, res = run_csdp !debugging obj mats in
1119 if rv = 1 || rv = 2 then failwith "csdp: Problem is infeasible"
1120 else if rv = 3 then ()
1121 (* (Format.print_string "csdp warning: Reduced accuracy";
1122 Format.print_newline()) *)
1123 else if rv <> 0 then failwith ("csdp: error " ^ string_of_int rv)
1124 else ();
1125 res
1126
1127 (* ------------------------------------------------------------------------- *)
1128 (* Sum-of-squares function with some lowbrow symmetry reductions. *)
1129 (* ------------------------------------------------------------------------- *)
1130
1131 let sumofsquares_general_symmetry tool pol =
1132 let vars = poly_variables pol and lpps = newton_polytope pol in
1133 let n = List.length lpps in
1134 let sym_eqs =
1135 let invariants =
1136 List.filter
1137 (fun vars' ->
1138 is_undefined
1139 (poly_sub pol (changevariables (List.combine vars vars') pol)))
1140 (allpermutations vars)
1141 in
1142 let lpns = List.combine lpps (1 -- List.length lpps) in
1143 let lppcs =
1144 List.filter
1145 (fun (m, (n1, n2)) -> n1 <= n2)
1146 (allpairs (fun (m1, n1) (m2, n2) -> ((m1, m2), (n1, n2))) lpns lpns)
1147 in
1148 let clppcs =
1149 end_itlist ( @ )
1150 (List.map
1151 (fun ((m1, m2), (n1, n2)) ->
1152 List.map
1153 (fun vars' ->
1154 ( ( changevariables_monomial (List.combine vars vars') m1
1155 , changevariables_monomial (List.combine vars vars') m2 )
1156 , (n1, n2) ))
1157 invariants)
1158 lppcs)
1159 in
1160 let clppcs_dom = setify (List.map fst clppcs) in
1161 let clppcs_cls =
1162 List.map (fun d -> List.filter (fun (e, _) -> e = d) clppcs) clppcs_dom
1163 in
1164 let eqvcls = List.map (o setify (List.map snd)) clppcs_cls in
1165 let mk_eq cls acc =
1166 match cls with
1167 | [] -> raise Sanity
1168 | [h] -> acc
1169 | h :: t -> List.map (fun k -> (k |-> Q.minus_one) (h |=> Q.one)) t @ acc
1170 in
1171 List.fold_right mk_eq eqvcls []
1172 in
1173 let eqs =
1174 foldl
1175 (fun a x y -> y :: a)
1176 []
1177 (itern 1 lpps
1178 (fun m1 n1 ->
1179 itern 1 lpps (fun m2 n2 f ->
1180 let m = monomial_mul m1 m2 in
1181 if n1 > n2 then f
1182 else
1183 let c = if n1 = n2 then Q.one else Q.two in
1184 (m |-> ((n1, n2) |-> c) (tryapplyd f m undefined)) f))
1185 (foldl (fun a m c -> (m |-> ((0, 0) |=> c)) a) undefined pol))
1186 @ sym_eqs
1187 in
1188 let pvs, assig = eliminate_all_equations (0, 0) eqs in
1189 let allassig = List.fold_right (fun v -> v |-> (v |=> Q.one)) pvs assig in
1190 let qvars = (0, 0) :: pvs in
1191 let diagents =
1192 end_itlist equation_add (List.map (fun i -> apply allassig (i, i)) (1 -- n))
1193 in
1194 let mk_matrix v : matrix =
1195 ( (n, n)
1196 , foldl
1197 (fun m (i, j) ass ->
1198 let c = tryapplyd ass v Q.zero in
1199 if c =/ Q.zero then m else ((j, i) |-> c) (((i, j) |-> c) m))
1200 undefined allassig )
1201 in
1202 let mats = List.map mk_matrix qvars
1203 and obj =
1204 ( List.length pvs
1205 , itern 1 pvs (fun v i -> i |--> tryapplyd diagents v Q.zero) undefined )
1206 in
1207 let raw_vec = if pvs = [] then vector_0 0 else tool obj mats in
1208 let find_rounding d =
1209 if !debugging then (
1210 Format.print_string ("Trying rounding with limit " ^ Q.to_string d);
1211 Format.print_newline () )
1212 else ();
1213 let vec = nice_vector d raw_vec in
1214 let mat =
1215 iter
1216 (1, dim vec)
1217 (fun i a ->
1218 matrix_add (matrix_cmul (element vec i) (List.nth mats i)) a)
1219 (matrix_neg (List.nth mats 0))
1220 in
1221 deration (diag mat)
1222 in
1223 let rat, dia =
1224 if pvs = [] then
1225 let mat = matrix_neg (List.nth mats 0) in
1226 deration (diag mat)
1227 else
1228 tryfind find_rounding
1229 (List.map Q.of_int (1 -- 31) @ List.map Q.pow2 (5 -- 66))
1230 in
1231 let poly_of_lin (d, v) =
1232 (d, foldl (fun a i c -> (List.nth lpps (i - 1) |-> c) a) undefined (snd v))
1233 in
1234 let lins = List.map poly_of_lin dia in
1235 let sqs =
1236 List.map (fun (d, l) -> poly_mul (poly_const d) (poly_pow l 2)) lins
1237 in
1238 let sos = poly_cmul rat (end_itlist poly_add sqs) in
1239 if is_undefined (poly_sub sos pol) then (rat, lins) else raise Sanity
1240
1241 let sumofsquares = sumofsquares_general_symmetry csdp