1 (* testexamples for Poly, polynomials
2 author: Matthias Goldgruber 2003
3 (c) due to copyright terms
5 use"../smltest/IsacKnowledge/poly.sml";
7 ****************************************************************.*)
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 "table of contents -----------------------------------------------";
15 "-----------------------------------------------------------------";
16 "-------- investigate new uniary minus ---------------------------";
17 "-------- Bsple aus Schalk I -------------------------------------";
18 "-------- Script 'simplification for_polynomials' ----------------";
19 "-------- check pbl 'polynomial simplification' -----------------";
20 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
21 "-------- norm_Poly NOT COMPLETE ---------------------------------";
22 "-------- ord_make_polynomial ------------------------------------";
23 "-----------------------------------------------------------------";
24 "-----------------------------------------------------------------";
25 "-----------------------------------------------------------------";
28 "-------- investigate new uniary minus ---------------------------";
29 "-------- investigate new uniary minus ---------------------------";
30 "-------- investigate new uniary minus ---------------------------";
31 val t = (#prop o rep_thm) real_diff_0; (*"0 - ?x = - ?x"*)
34 *** Const ( Trueprop, bool => prop)
35 *** . Const ( op =, [real, real] => bool)
36 *** . . Const ( op -, [real, real] => real)
37 *** . . . Const ( 0, real)
38 *** . . . Var ((x, 0), real)
39 *** . . Const ( uminus, real => real)
40 *** . . . Var ((x, 0), real) *)
42 val t = (term_of o the o (parse thy)) "-1";
45 *** Free ( -1, real) *)
46 val t = (term_of o the o (parse thy)) "- 1";
49 *** Const ( uminus, real => real)
50 *** . Free ( 1, real) *)
52 val t = (term_of o the o (parse thy)) "-x"; (*1-x syntyx error !!!*)
55 *** Free ( -x, real)*)
56 val t = (term_of o the o (parse thy)) "- x";
59 *** Free ( -x, real) !!!!!!!!!!!!!!!!!!!!!!!! is the same !!!*)
60 val t = (term_of o the o (parse thy)) "-(x)";
63 *** Free ( -x, real)*)
66 "-------- Bsple aus Schalk I -------------------------------------";
67 "-------- Bsple aus Schalk I -------------------------------------";
68 "-------- Bsple aus Schalk I -------------------------------------";
69 (*SPB Schalk I p.63 No.267b*)
71 "(5*x^^^2 + 3) * (2*x^^^7 + 3) - (3*x^^^5 + 8) * (6*x^^^4 - 1)";
72 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
74 "17 + 15 * x ^^^ 2 + -48 * x ^^^ 4 + 3 * x ^^^ 5 + 6 * x ^^^ 7 + -8 * x ^^^ 9"
76 else raise error "poly.sml: diff.behav. in make_polynomial 1";
78 (*SPB Schalk I p.63 No.275b*)
80 "(3*x^^^2 - 2*x*y + y^^^2) * (x^^^2 - 2*y^^^2)";
81 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
84 "3 * x ^^^ 4 + -2 * x ^^^ 3 * y + -5 * x ^^^ 2 * y ^^^ 2 + \
85 \4 * x * y ^^^ 3 +\n-2 * y ^^^ 4"
87 else raise error "poly.sml: diff.behav. in make_polynomial 2";
89 (*SPB Schalk I p.63 No.279b*)
91 "(x-a)*(x-b)*(x-c)*(x-d)";
92 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
96 "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"
98 else raise error "poly.sml: diff.behav. in make_polynomial 3";
100 (*SPB Schalk I p.63 No.291*)
102 "(5+96*x^^^3+8*x*(-4+(7- 3*x)*4*x))*(5*(2- 3*x)- (-15*x*(-8*x- 5)))";
103 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
106 "50 + -770 * x + 4520 * x ^^^ 2 + -16320 * x ^^^ 3 + -26880 * x ^^^ 4"
108 else raise error "poly.sml: diff.behav. in make_polynomial 4";
110 (*SPB Schalk I p.64 No.295c*)
112 "(13*a^^^4*b^^^9*c - 12*a^^^3*b^^^6*c^^^9)^^^2";
113 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
116 "169 * a ^^^ 8 * b ^^^ 18 * c ^^^ 2 + -312 * a ^^^ 7 * b ^^^ 15 * c ^^^ 10\
117 \ +\n144 * a ^^^ 6 * b ^^^ 12 * c ^^^ 18"
119 else raise error "poly.sml: diff.behav. in make_polynomial 5";
121 (*SPB Schalk I p.64 No.299a*)
124 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
127 "x ^^^ 2 + -1 * y ^^^ 2"
129 else raise error "poly.sml: diff.behav. in make_polynomial 6";
131 (*SPB Schalk I p.64 No.300c*)
133 "(3*x^^^2*y - 1)*(3*x^^^2*y + 1)";
134 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
137 "-1 + 9 * x ^^^ 4 * y ^^^ 2"
139 else raise error "poly.sml: diff.behav. in make_polynomial 7";
141 (*SPB Schalk I p.64 No.302*)
143 "(13*x^^^2 + 5)*(13*x^^^2 - 5) - (5*x^^^2 + 3)*(5*x^^^2 - 3) - (12*x^^^2 + 4)*(12*x^^^2 - 4)";
144 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
145 if term2str t = "0" then ()
146 else raise error "poly.sml: diff.behav. in make_polynomial 8";
147 (* Bei Berechnung sollte 3 mal real_plus_minus_binom1_p aus expand_poly verwendet werden *)
150 (*SPB Schalk I p.64 No.306a*)
151 val t = str2term "((x^^^2 + 1)*(x^^^2 - 1))^^^2";
152 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
153 if (term2str t) = "1 + 2 * x ^^^ 4 + 2 * -2 * x ^^^ 4 + x ^^^ 8" then ()
154 else raise error "poly.sml: diff.behav. in make_polynomial: not confluent \
155 \2 * x ^^^ 4 + 2 * -2 * x ^^^ 4 = -2 * x ^^^ 4 works again";
158 (*WN071729 when reducing "rls reduce_012_" for Schaerding,
159 the above resulted in the term below ... but reduces from then correctly*)
160 val t = str2term "1 + 2 * x ^^^ 4 + 2 * -2 * x ^^^ 4 + x ^^^ 8";
161 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
162 if (term2str t) = "1 + -2 * x ^^^ 4 + x ^^^ 8" then ()
163 else raise error "poly.sml: diff.behav. in make_polynomial 9b";
165 (*SPB Schalk I p.64 No.296a*)
166 val t = str2term "(x - a)^^^3";
167 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
168 if (term2str t) = "-1 * a ^^^ 3 + 3 * a ^^^ 2 * x + -3 * a * x ^^^ 2 + x ^^^ 3"
169 then () else raise error "poly.sml: diff.behav. in make_polynomial 10";
171 (*SPB Schalk I p.64 No.296c*)
172 val t = str2term "(-3*x - 4*y)^^^3";
173 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
175 "-27 * x ^^^ 3 + -108 * x ^^^ 2 * y + -144 * x * y ^^^ 2 + -64 * y ^^^ 3"
176 then () else raise error "poly.sml: diff.behav. in make_polynomial 11";
178 (*SPB Schalk I p.62 No.242c*)
179 val t = str2term "x^^^(-4)*(x^^^(-4)*y^^^(-2))^^^(-1)*y^^^(-2)";
180 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
181 if (term2str t) = "1" then ()
182 else raise error "poly.sml: diff.behav. in make_polynomial 12";
184 (*SPB Schalk I p.60 No.209a*)
185 val t = str2term "a^^^(7-x) * a^^^x";
186 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
187 if term2str t = "a ^^^ 7" then ()
188 else raise error "poly.sml: diff.behav. in make_polynomial 13";
190 (*SPB Schalk I p.60 No.209d*)
191 val t = str2term "d^^^x * d^^^(x+1) * d^^^(2 - 2*x)";
192 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
193 if term2str t = "d ^^^ 3" then ()
194 else raise error "poly.sml: diff.behav. in make_polynomial 14";
197 (*---------------------------------------------------------------------*)
198 (*------------------ Bsple bei denen es Probleme gibt------------------*)
199 (*---------------------------------------------------------------------*)
201 (*Schalk I p.64 No.303*)
202 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)";
203 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
204 if term2str t = "1280 * b ^^^ 4" then ()
205 else raise error "poly.sml: diff.behav. in make_polynomial 14b";
206 (* Richtig - aber Binomische Formel wurde nicht verwendet! *)
209 (*--------------------------------------------------------------------*)
210 (*----------------------- Eigene Beispiele ---------------------------*)
211 (*--------------------------------------------------------------------*)
213 val t = str2term "a^^^2*a^^^(-2)";
214 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
215 if term2str t = "1" then ()
216 else raise error "poly.sml: diff.behav. in make_polynomial 15";
218 val t = str2term "a + a + a";
219 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
220 if term2str t = "3 * a" then ()
221 else raise error "poly.sml: diff.behav. in make_polynomial 16";
223 val t = str2term "a + b + b + b";
224 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
225 if term2str t = "a + 3 * b" then ()
226 else raise error "poly.sml: diff.behav. in make_polynomial 17";
228 val t = str2term "a^^^2*b*b^^^(-1)";
229 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
230 if term2str t = "a ^^^ 2" then ()
231 else raise error "poly.sml: diff.behav. in make_polynomial 18";
233 val t = str2term "a^^^2*a^^^(-2)";
234 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
235 if (term2str t) = "1" then ()
236 else raise error "poly.sml: diff.behav. in make_polynomial 19";
238 val t = str2term "b + a - b";
239 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
240 if (term2str t) = "a" then ()
241 else raise error "poly.sml: diff.behav. in make_polynomial 20";
243 val t = str2term "b * a * a";
244 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
245 if term2str t = "a ^^^ 2 * b" then ()
246 else raise error "poly.sml: diff.behav. in make_polynomial 21";
248 val t = str2term "(a^^^2)^^^3";
249 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
250 if term2str t = "a ^^^ 6" then ()
251 else raise error "poly.sml: diff.behav. in make_polynomial 22";
253 val t = str2term "x^^^2 * y^^^2 + x * x^^^2 * y";
254 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
255 if term2str t = "x ^^^ 3 * y + x ^^^ 2 * y ^^^ 2" then ()
256 else raise error "poly.sml: diff.behav. in make_polynomial 23";
258 val t = (term_of o the o (parse thy)) "a^^^2 * (-a)^^^2";
259 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
260 if (term2str t) = "a ^^^ 4" then ()
261 else raise error "poly.sml: diff.behav. in make_polynomial 24";
263 val t = str2term "a * b * b^^^(-1) + a";
264 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
265 if (term2str t) = "2 * a" then ()
266 else raise error "poly.sml: diff.behav. in make_polynomial 25";
268 val t = str2term "a*c*b^^^(2*n) + 3*a + 5*b^^^(2*n)*c*b";
269 val Some (t,_) = rewrite_set_ thy false make_polynomial t; term2str t;
270 if (term2str t) = "3 * a + 5 * b ^^^ (1 + 2 * n) * c + a * b ^^^ (2 * n) * c"
271 then () else raise error "poly.sml: diff.behav. in make_polynomial 26";
274 (*MG.27.6.03 -------------vvv-: Verschachtelte Terme -----------*)
276 val t = str2term "(1 + (x*y*a) + x)^^^(1 + (x*y*a) + x)";
277 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
279 if term2str t = "(1 + x + a * x * y) ^^^ (1 + x + a * x * y)"
280 then () else raise error "poly.sml: diff.behav. in make_polynomial 27";(*SPO*)
281 val t = str2term "(1 + x*(y*z)*zz)^^^(1 + x*(y*z)*zz)";
282 val Some (t,_) = rewrite_set_ thy false make_polynomial t;
284 if term2str t = "(1 + x * y * z * zz) ^^^ (1 + x * y * z * zz)"
285 then () else raise error "poly.sml: diff.behav. in make_polynomial 28";
287 "-------- Script 'simplification for_polynomials' ----------------";
288 "-------- Script 'simplification for_polynomials' ----------------";
289 "-------- Script 'simplification for_polynomials' ----------------";
291 "Script SimplifyScript (t_::real) = \
292 \ ((Rewrite_Set norm_Poly False) t_)";
293 val sc = ((inst_abs thy) o term_of o the o (parse thy)) str;
297 "-------- check pbl 'polynomial simplification' -----------------";
298 "-------- check pbl 'polynomial simplification' -----------------";
299 "-------- check pbl 'polynomial simplification' -----------------";
300 val fmz = ["term ((5*x^^^2 + 3) * (2*x^^^7 + 3) \
301 \- (3*x^^^5 + 8) * (6*x^^^4 - 1))",
304 case refine fmz ["polynomial","simplification"]of
305 [Matches (["polynomial", "simplification"], _)] => ()
306 | _ => raise error "poly.sml diff.behav. in check pbl, refine";
307 (*...if there is an error, then ...*)
311 val pbt = get_pbt ["polynomial","simplification"];
314 > val NoMatch' {Given=gi, Where=wh, Find=fi,...} = match_pbl fmz pbt;
315 ... then trace_rewrite:*)
320 trace_rewrite:=false;
321 (*... if there is no rewrite, then there is something wrong with prls*)
325 val prls = (#prls o get_pbt) ["polynomial","simplification"];
327 val t = str2term "((5*x^^^2 + 3) * (2*x^^^7 + 3) \
328 \- (3*x^^^5 + 8) * (6*x^^^4 - 1)) is_polyexp";
330 val Some (t',_) = rewrite_set_ thy false prls t;
331 trace_rewrite:=false;
332 if t' = HOLogic.true_const then ()
333 else raise error "poly.sml: diff.behav. in check pbl 'polynomial..";
334 (*... if this works, but (*1*) does still NOT work, check types:*)
339 > val NoMatch' {Given=gi, Where=wh, Find=fi,...} = match_pbl fmz pbt;
340 val wh = [False "(t_::real => real) (is_polyexp::real)"]
341 ......................^^^^^^^^^^^^...............^^^^*)
342 val Matches' _ = match_pbl fmz pbt;
346 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
347 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
348 "-------- me 'polynomial simplification' Schalk I p.63 No.267b ---";
349 val fmz = ["term ((5*x^^^2 + 3) * (2*x^^^7 + 3) \
350 \- (3*x^^^5 + 8) * (6*x^^^4 - 1))",
353 ("Poly.thy",["polynomial","simplification"],
354 ["simplification","for_polynomials"]);
355 val p = e_pos'; val c = [];
356 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
357 val (p,_,f,nxt,_,pt) = me nxt p c pt;
358 val (p,_,f,nxt,_,pt) = me nxt p c pt;
359 (writeln o (itms2str thy)) (get_obj g_pbl pt (fst p));
360 val (p,_,f,nxt,_,pt) = me nxt p c pt;
361 val (p,_,f,nxt,_,pt) = me nxt p c pt;
362 val (p,_,f,nxt,_,pt) = me nxt p c pt;
363 val (p,_,f,nxt,_,pt) = me nxt p c pt;
364 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
365 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
366 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
368 "17 + 15 * x ^^^ 2 + -48 * x ^^^ 4 + 3 * x ^^^ 5 + 6 * x ^^^ 7 + -8 * x ^^^ 9"
369 then () else raise error "poly.sml diff.behav. in me Schalk I p.63 No.267b";
372 "-------- interSteps for Schalk 299a -----------------------------";
373 "-------- interSteps for Schalk 299a -----------------------------";
374 "-------- interSteps for Schalk 299a -----------------------------";
377 [(["term ((x - y)*(x + y))", "normalform N"],
378 ("Poly.thy",["polynomial","simplification"],
379 ["simplification","for_polynomials"]))];
382 autoCalculate 1 CompleteCalc;
383 val ((pt,p),_) = get_calc 1; show_pt pt;
385 interSteps 1 ([1],Res)(*<ERROR> syserror in detailstep </ERROR>*);
386 val ((pt,p),_) = get_calc 1; show_pt pt;
387 if existpt' ([1,1], Frm) pt then ()
388 else raise error "poly.sml: interSteps doesnt work again 1";
390 interSteps 1 ([1,1],Res)(*<ERROR> syserror in detailstep </ERROR>*);
391 val ((pt,p),_) = get_calc 1; show_pt pt;
392 if existpt' ([1,1,1], Frm) pt then ()
393 else raise error "poly.sml: interSteps doesnt work again 2";
396 "-------- norm_Poly NOT COMPLETE ---------------------------------";
397 "-------- norm_Poly NOT COMPLETE ---------------------------------";
398 "-------- norm_Poly NOT COMPLETE ---------------------------------";
400 val Some (f',_) = rewrite_set_ thy false norm_Poly
401 (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*);
402 trace_rewrite:=false;
405 "-------- ord_make_polynomial ------------------------------------";
406 "-------- ord_make_polynomial ------------------------------------";
407 "-------- ord_make_polynomial ------------------------------------";
408 val t1 = str2term "2 * b + (3 * a + 3 * b)";
409 val t2 = str2term "3 * a + 3 * b + 2 * b";
411 if ord_make_polynomial true Poly.thy [] (t1, t2) then ()
412 else raise error "poly.sml: diff.behav. in ord_make_polynomial";
414 (*WN071202: ^^^ why then is there no rewriting ...*)
415 val term = str2term "2*b + (3*a + 3*b)";
416 val None = rewrite_set_ Isac.thy false order_add_mult term;
418 (*or why is there no rewriting this way...*)
419 val t1 = str2term "2 * b + (3 * a + 3 * b)";
420 val t2 = str2term "3 * a + (2 * b + 3 * b)";