1 (* testexamples for Poly, polynomials
2 author: Matthias Goldgruber 2003
3 (c) due to copyright terms
5 12345678901234567890123456789012345678901234567890123456789012345678901234567890
6 10 20 30 40 50 60 70 80
9 (*******************************************************************************
10 WN060104 'SPB' came into 'exp_IsacCore_Simp_Poly_Book.xml'
11 'SPO' came into 'exp_IsacCore_Simp_Poly_Other.xml'
12 *******************************************************************************)
13 "--------------------------------------------------------";
14 "--------------------------------------------------------";
15 "table of contents --------------------------------------";
16 "--------------------------------------------------------";
17 "-------- check is'_polyexp is_polyexp ------------------";
18 "-------- build Script Expand_binoms --------------------";
19 "-------- investigate new uniary minus ------------------";
20 "-------- Bsple aus Schalk I ----------------------------";
21 "-------- Script 'simplification for_polynomials' -------";
22 "-------- check pbl 'polynomial simplification' --------";
23 "-------- me 'poly. simpl.' Schalk I p.63 No.267b -------";
24 "-------- norm_Poly NOT COMPLETE ------------------------";
25 "-------- ord_make_polynomial ---------------------------";
26 "--------------------------------------------------------";
27 "--------------------------------------------------------";
28 "--------------------------------------------------------";
31 "-------- check is'_polyexp is_polyexp ------------------";
32 "-------- check is'_polyexp is_polyexp ------------------";
33 "-------- check is'_polyexp is_polyexp ------------------";
34 if is_polyexp @{term "x / x"}
35 then error "poly.sml: check is'_polyexp is_polyexp" else ();
38 (*===== inhibit exn ?===========================================================
39 "-------- build Script Expand_binoms -----------------------------";
40 "-------- build Script Expand_binoms -----------------------------";
41 "-------- build Script Expand_binoms -----------------------------";
42 val scr_expand_binoms =
43 "Script Expand_binoms t_t =" ^
45 "((Try (Repeat (Rewrite real_plus_binom_pow2 False))) @@ " ^
46 " (Try (Repeat (Rewrite real_plus_binom_times False))) @@ " ^
47 " (Try (Repeat (Rewrite real_minus_binom_pow2 False))) @@ " ^
48 " (Try (Repeat (Rewrite real_minus_binom_times False))) @@ " ^
49 " (Try (Repeat (Rewrite real_plus_minus_binom1 False))) @@ " ^
50 " (Try (Repeat (Rewrite real_plus_minus_binom2 False))) @@ " ^
52 " (Try (Repeat (Rewrite mult_1_left False))) @@ " ^
53 " (Try (Repeat (Rewrite mult_zero_left False))) @@ " ^
54 " (Try (Repeat (Rewrite add_0_left False))) @@ " ^
56 " (Try (Repeat (Calculate PLUS ))) @@ " ^
57 " (Try (Repeat (Calculate TIMES ))) @@ " ^
58 " (Try (Repeat (Calculate POWER))) @@ " ^
60 " (Try (Repeat (Rewrite sym_realpow_twoI False))) @@ " ^
61 " (Try (Repeat (Rewrite realpow_plus_1 False))) @@ " ^
62 " (Try (Repeat (Rewrite sym_real_mult_2 False))) @@ " ^
63 " (Try (Repeat (Rewrite real_mult_2_assoc False))) @@ " ^
65 " (Try (Repeat (Rewrite real_num_collect False))) @@ " ^
66 " (Try (Repeat (Rewrite real_num_collect_assoc False))) @@ " ^
68 " (Try (Repeat (Rewrite real_one_collect False))) @@ " ^
69 " (Try (Repeat (Rewrite real_one_collect_assoc False))) @@ " ^
71 " (Try (Repeat (Calculate PLUS ))) @@ " ^
72 " (Try (Repeat (Calculate TIMES ))) @@ " ^
73 " (Try (Repeat (Calculate POWER)))) " ^
75 val scr_expand_binoms =
76 "Script Expand_binoms t_t = t_t";
78 val ---------------------------------------------- = "11111";
79 parse thy scr_expand_binoms;
80 val ---------------------------------------------- = "22222";
81 (*----------------------------------------------
83 "-------- investigate new uniary minus ---------------------------";
84 "-------- investigate new uniary minus ---------------------------";
85 "-------- investigate new uniary minus ---------------------------";
86 val t = (#prop o rep_thm) real_diff_0; (*"0 - ?x = - ?x"*)
89 *** Const ( Trueprop, bool => prop)
90 *** . Const ( op =, [real, real] => bool)
91 *** . . Const ( op -, [real, real] => real)
92 *** . . . Const ( 0, real)
93 *** . . . Var ((x, 0), real)
94 *** . . Const ( uminus, real => real)
95 *** . . . Var ((x, 0), real) *)
97 val t = (term_of o the o (parse thy)) "-1";
100 *** Free ( -1, real) *)
101 val t = (term_of o the o (parse thy)) "- 1";
104 *** Const ( uminus, real => real)
105 *** . Free ( 1, real) *)
107 val t = (term_of o the o (parse thy)) "-x"; (*1-x syntyx error !!!*)
110 *** Free ( -x, real)*)
111 val t = (term_of o the o (parse thy)) "- x";
114 *** Free ( -x, real) !!!!!!!!!!!!!!!!!!!!!!!! is the same !!!*)
115 val t = (term_of o the o (parse thy)) "-(x)";
118 *** Free ( -x, real)*)
121 "-------- Bsple aus Schalk I -------------------------------------";
122 "-------- Bsple aus Schalk I -------------------------------------";
123 "-------- Bsple aus Schalk I -------------------------------------";
124 (*SPB Schalk I p.63 No.267b*)
126 "(5*x^^^2 + 3) * (2*x^^^7 + 3) - (3*x^^^5 + 8) * (6*x^^^4 - 1)";
127 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
129 "17 + 15 * x ^^^ 2 + -48 * x ^^^ 4 + 3 * x ^^^ 5 + 6 * x ^^^ 7 + -8 * x ^^^ 9"
131 else error "poly.sml: diff.behav. in make_polynomial 1";
133 (*SPB Schalk I p.63 No.275b*)
135 "(3*x^^^2 - 2*x*y + y^^^2) * (x^^^2 - 2*y^^^2)";
136 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
139 "3 * x ^^^ 4 + -2 * x ^^^ 3 * y + -5 * x ^^^ 2 * y ^^^ 2 + \
140 \4 * x * y ^^^ 3 +\n-2 * y ^^^ 4"
142 else error "poly.sml: diff.behav. in make_polynomial 2";
144 (*SPB Schalk I p.63 No.279b*)
146 "(x-a)*(x-b)*(x-c)*(x-d)";
147 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
151 "a * b * c * d + -1 * a * b * c * x + -1 * a * b * d * x + a * b * x ^^^ 2 +\n-1 * a * c * d * x +\na * c * x ^^^ 2 +\na * d * x ^^^ 2 +\n-1 * a * x ^^^ 3 +\n-1 * b * c * d * x +\nb * c * x ^^^ 2 +\nb * d * x ^^^ 2 +\n-1 * b * x ^^^ 3 +\nc * d * x ^^^ 2 +\n-1 * c * x ^^^ 3 +\n-1 * d * x ^^^ 3 +\nx ^^^ 4"
153 else error "poly.sml: diff.behav. in make_polynomial 3";
155 (*SPB Schalk I p.63 No.291*)
157 "(5+96*x^^^3+8*x*(-4+(7- 3*x)*4*x))*(5*(2- 3*x)- (-15*x*(-8*x- 5)))";
158 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
161 "50 + -770 * x + 4520 * x ^^^ 2 + -16320 * x ^^^ 3 + -26880 * x ^^^ 4"
163 else error "poly.sml: diff.behav. in make_polynomial 4";
165 (*SPB Schalk I p.64 No.295c*)
167 "(13*a^^^4*b^^^9*c - 12*a^^^3*b^^^6*c^^^9)^^^2";
168 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
171 "169 * a ^^^ 8 * b ^^^ 18 * c ^^^ 2 + -312 * a ^^^ 7 * b ^^^ 15 * c ^^^ 10\
172 \ +\n144 * a ^^^ 6 * b ^^^ 12 * c ^^^ 18"
174 else error "poly.sml: diff.behav. in make_polynomial 5";
176 (*SPB Schalk I p.64 No.299a*)
179 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
182 "x ^^^ 2 + -1 * y ^^^ 2"
184 else error "poly.sml: diff.behav. in make_polynomial 6";
186 (*SPB Schalk I p.64 No.300c*)
188 "(3*x^^^2*y - 1)*(3*x^^^2*y + 1)";
189 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
192 "-1 + 9 * x ^^^ 4 * y ^^^ 2"
194 else error "poly.sml: diff.behav. in make_polynomial 7";
196 (*SPB Schalk I p.64 No.302*)
198 "(13*x^^^2 + 5)*(13*x^^^2 - 5) - (5*x^^^2 + 3)*(5*x^^^2 - 3) - (12*x^^^2 + 4)*(12*x^^^2 - 4)";
199 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
200 if term2str t = "0" then ()
201 else error "poly.sml: diff.behav. in make_polynomial 8";
202 (* Bei Berechnung sollte 3 mal real_plus_minus_binom1_p aus expand_poly verwendet werden *)
205 (*SPB Schalk I p.64 No.306a*)
206 val t = str2term "((x^^^2 + 1)*(x^^^2 - 1))^^^2";
207 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
208 if (term2str t) = "1 + 2 * x ^^^ 4 + 2 * -2 * x ^^^ 4 + x ^^^ 8" then ()
209 else error "poly.sml: diff.behav. in make_polynomial: not confluent \
210 \2 * x ^^^ 4 + 2 * -2 * x ^^^ 4 = -2 * x ^^^ 4 works again";
213 (*WN071729 when reducing "rls reduce_012_" for Schaerding,
214 the above resulted in the term below ... but reduces from then correctly*)
215 val t = str2term "1 + 2 * x ^^^ 4 + 2 * -2 * x ^^^ 4 + x ^^^ 8";
216 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
217 if (term2str t) = "1 + -2 * x ^^^ 4 + x ^^^ 8" then ()
218 else error "poly.sml: diff.behav. in make_polynomial 9b";
220 (*SPB Schalk I p.64 No.296a*)
221 val t = str2term "(x - a)^^^3";
222 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
223 if (term2str t) = "-1 * a ^^^ 3 + 3 * a ^^^ 2 * x + -3 * a * x ^^^ 2 + x ^^^ 3"
224 then () else error "poly.sml: diff.behav. in make_polynomial 10";
226 (*SPB Schalk I p.64 No.296c*)
227 val t = str2term "(-3*x - 4*y)^^^3";
228 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
230 "-27 * x ^^^ 3 + -108 * x ^^^ 2 * y + -144 * x * y ^^^ 2 + -64 * y ^^^ 3"
231 then () else error "poly.sml: diff.behav. in make_polynomial 11";
233 (*SPB Schalk I p.62 No.242c*)
234 val t = str2term "x^^^(-4)*(x^^^(-4)*y^^^(-2))^^^(-1)*y^^^(-2)";
235 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
236 if (term2str t) = "1" then ()
237 else error "poly.sml: diff.behav. in make_polynomial 12";
239 (*SPB Schalk I p.60 No.209a*)
240 val t = str2term "a^^^(7-x) * a^^^x";
241 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
242 if term2str t = "a ^^^ 7" then ()
243 else error "poly.sml: diff.behav. in make_polynomial 13";
245 (*SPB Schalk I p.60 No.209d*)
246 val t = str2term "d^^^x * d^^^(x+1) * d^^^(2 - 2*x)";
247 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
248 if term2str t = "d ^^^ 3" then ()
249 else error "poly.sml: diff.behav. in make_polynomial 14";
252 (*---------------------------------------------------------------------*)
253 (*------------------ Bsple bei denen es Probleme gibt------------------*)
254 (*---------------------------------------------------------------------*)
256 (*Schalk I p.64 No.303*)
257 val t = str2term "(a + 2*b)*(a^^^2 + 4*b^^^2)*(a - 2*b) - (a - 6*b)*(a^^^2 + 36*b^^^2)*(a + 6*b)";
258 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
259 if term2str t = "1280 * b ^^^ 4" then ()
260 else error "poly.sml: diff.behav. in make_polynomial 14b";
261 (* Richtig - aber Binomische Formel wurde nicht verwendet! *)
264 (*--------------------------------------------------------------------*)
265 (*----------------------- Eigene Beispiele ---------------------------*)
266 (*--------------------------------------------------------------------*)
268 val t = str2term "a^^^2*a^^^(-2)";
269 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
270 if term2str t = "1" then ()
271 else error "poly.sml: diff.behav. in make_polynomial 15";
273 val t = str2term "a + a + a";
274 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
275 if term2str t = "3 * a" then ()
276 else error "poly.sml: diff.behav. in make_polynomial 16";
278 val t = str2term "a + b + b + b";
279 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
280 if term2str t = "a + 3 * b" then ()
281 else error "poly.sml: diff.behav. in make_polynomial 17";
283 val t = str2term "a^^^2*b*b^^^(-1)";
284 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
285 if term2str t = "a ^^^ 2" then ()
286 else error "poly.sml: diff.behav. in make_polynomial 18";
288 val t = str2term "a^^^2*a^^^(-2)";
289 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
290 if (term2str t) = "1" then ()
291 else error "poly.sml: diff.behav. in make_polynomial 19";
293 val t = str2term "b + a - b";
294 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
295 if (term2str t) = "a" then ()
296 else error "poly.sml: diff.behav. in make_polynomial 20";
298 val t = str2term "b * a * a";
299 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
300 if term2str t = "a ^^^ 2 * b" then ()
301 else error "poly.sml: diff.behav. in make_polynomial 21";
303 val t = str2term "(a^^^2)^^^3";
304 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
305 if term2str t = "a ^^^ 6" then ()
306 else error "poly.sml: diff.behav. in make_polynomial 22";
308 val t = str2term "x^^^2 * y^^^2 + x * x^^^2 * y";
309 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
310 if term2str t = "x ^^^ 3 * y + x ^^^ 2 * y ^^^ 2" then ()
311 else error "poly.sml: diff.behav. in make_polynomial 23";
313 val t = (term_of o the o (parse thy)) "a^^^2 * (-a)^^^2";
314 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
315 if (term2str t) = "a ^^^ 4" then ()
316 else error "poly.sml: diff.behav. in make_polynomial 24";
318 val t = str2term "a * b * b^^^(-1) + a";
319 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
320 if (term2str t) = "2 * a" then ()
321 else error "poly.sml: diff.behav. in make_polynomial 25";
323 val t = str2term "a*c*b^^^(2*n) + 3*a + 5*b^^^(2*n)*c*b";
324 val SOME (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
325 if (term2str t) = "3 * a + 5 * b ^^^ (1 + 2 * n) * c + a * b ^^^ (2 * n) * c"
326 then () else error "poly.sml: diff.behav. in make_polynomial 26";
329 (*MG.27.6.03 -------------vvv-: Verschachtelte Terme -----------*)
331 val t = str2term "(1 + (x*y*a) + x)^^^(1 + (x*y*a) + x)";
332 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
334 if term2str t = "(1 + x + a * x * y) ^^^ (1 + x + a * x * y)"
335 then () else error "poly.sml: diff.behav. in make_polynomial 27";(*SPO*)
336 val t = str2term "(1 + x*(y*z)*zz)^^^(1 + x*(y*z)*zz)";
337 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
339 if term2str t = "(1 + x * y * z * zz) ^^^ (1 + x * y * z * zz)"
340 then () else error "poly.sml: diff.behav. in make_polynomial 28";
342 "-------- Script 'simplification for_polynomials' ----------------";
343 "-------- Script 'simplification for_polynomials' ----------------";
344 "-------- Script 'simplification for_polynomials' ----------------";
346 "Script SimplifyScript (t_::real) = \
347 \ ((Rewrite_Set norm_Poly False) t_)";
348 val sc = ((inst_abs thy) o term_of o the o (parse thy)) str;
352 "-------- check pbl 'polynomial simplification' -----------------";
353 "-------- check pbl 'polynomial simplification' -----------------";
354 "-------- check pbl 'polynomial simplification' -----------------";
355 val fmz = ["TERM ((5*x^^^2 + 3) * (2*x^^^7 + 3) \
356 \- (3*x^^^5 + 8) * (6*x^^^4 - 1))",
359 case refine fmz ["polynomial","simplification"]of
360 [Matches (["polynomial", "simplification"], _)] => ()
361 | _ => error "poly.sml diff.behav. in check pbl, refine";
362 (*...if there is an error, then ...*)
366 val pbt = get_pbt ["polynomial","simplification"];
369 > val NoMatch' {Given=gi, Where=wh, Find=fi,...} = match_pbl fmz pbt;
370 ... then trace_rewrite:*)
375 trace_rewrite:=false;
376 (*... if there is no rewrite, then there is something wrong with prls*)
380 val prls = (#prls o get_pbt) ["polynomial","simplification"];
382 val t = str2term "((5*x^^^2 + 3) * (2*x^^^7 + 3) \
383 \- (3*x^^^5 + 8) * (6*x^^^4 - 1)) is_polyexp";
385 val SOME (t',_) = rewrite_set_ thy false prls t;
386 trace_rewrite:=false;
387 if t' = HOLogic.true_const then ()
388 else error "poly.sml: diff.behav. in check pbl 'polynomial..";
389 (*... if this works, but (*1*) does still NOT work, check types:*)
394 > val NoMatch' {Given=gi, Where=wh, Find=fi,...} = match_pbl fmz pbt;
395 val wh = [False "(t_::real => real) (is_polyexp::real)"]
396 ......................^^^^^^^^^^^^...............^^^^*)
397 val Matches' _ = match_pbl fmz pbt;
401 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
402 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
403 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
404 val fmz = ["TERM ((5*x^^^2 + 3) * (2*x^^^7 + 3) \
405 \- (3*x^^^5 + 8) * (6*x^^^4 - 1))",
408 ("Poly",["polynomial","simplification"],
409 ["simplification","for_polynomials"]);
410 val p = e_pos'; val c = [];
411 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
412 val (p,_,f,nxt,_,pt) = me nxt p c pt;
413 val (p,_,f,nxt,_,pt) = me nxt p c pt;
414 (writeln o (itms2str_ ctxt)) (get_obj g_pbl pt (fst p));
415 val (p,_,f,nxt,_,pt) = me nxt p c pt;
416 val (p,_,f,nxt,_,pt) = me nxt p c pt;
417 val (p,_,f,nxt,_,pt) = me nxt p c pt;
418 val (p,_,f,nxt,_,pt) = me nxt p c pt;
419 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
420 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
421 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
423 "17 + 15 * x ^^^ 2 + -48 * x ^^^ 4 + 3 * x ^^^ 5 + 6 * x ^^^ 7 + -8 * x ^^^ 9"
424 then () else error "poly.sml diff.behav. in me Schalk I p.63 No.267b";
427 "-------- interSteps for Schalk 299a -----------------------------";
428 "-------- interSteps for Schalk 299a -----------------------------";
429 "-------- interSteps for Schalk 299a -----------------------------";
432 [(["TERM ((x - y)*(x + y))", "normalform N"],
433 ("Poly",["polynomial","simplification"],
434 ["simplification","for_polynomials"]))];
437 autoCalculate 1 CompleteCalc;
438 val ((pt,p),_) = get_calc 1; show_pt pt;
440 interSteps 1 ([1],Res)(*<ERROR> syserror in detailstep </ERROR>*);
441 val ((pt,p),_) = get_calc 1; show_pt pt;
442 if existpt' ([1,1], Frm) pt then ()
443 else error "poly.sml: interSteps doesnt work again 1";
445 interSteps 1 ([1,1],Res)(*<ERROR> syserror in detailstep </ERROR>*);
446 val ((pt,p),_) = get_calc 1; show_pt pt;
447 if existpt' ([1,1,1], Frm) pt then ()
448 else error "poly.sml: interSteps doesnt work again 2";
451 "-------- norm_Poly NOT COMPLETE ---------------------------------";
452 "-------- norm_Poly NOT COMPLETE ---------------------------------";
453 "-------- norm_Poly NOT COMPLETE ---------------------------------";
455 val SOME (f',_) = rewrite_set_ thy false norm_Poly
456 (str2term "L = k - 2 * q + (k - 2 * q) + (k - 2 * q) + (k - 2 * q) + senkrecht + oben")(*see poly.sml: -- norm_Poly NOT COMPLETE -- TODO MG*);
457 trace_rewrite:=false;
460 "-------- ord_make_polynomial ------------------------------------";
461 "-------- ord_make_polynomial ------------------------------------";
462 "-------- ord_make_polynomial ------------------------------------";
463 val t1 = str2term "2 * b + (3 * a + 3 * b)";
464 val t2 = str2term "3 * a + 3 * b + 2 * b";
466 if ord_make_polynomial true Poly.thy [] (t1, t2) then ()
467 else error "poly.sml: diff.behav. in ord_make_polynomial";
469 (*WN071202: ^^^ why then is there no rewriting ...*)
470 val term = str2term "2*b + (3*a + 3*b)";
471 val NONE = rewrite_set_ Isac.thy false order_add_mult term;
473 (*or why is there no rewriting this way...*)
474 val t1 = str2term "2 * b + (3 * a + 3 * b)";
475 val t2 = str2term "3 * a + (2 * b + 3 * b)";
478 ----------------------------------------------*)
479 ===== inhibit exn ?===========================================================*)