more antiquotations for Isabelle/HOL consts/types, without change of semantics;
2 author: Jan Rocnik, isac team
3 Copyright (c) isac team 2011
4 Use is subject to license terms.
6 (* Partial Fraction Decomposition *)
9 theory Partial_Fractions imports RootRatEq begin
16 val ansatz_rls_ : theory -> term -> (term * term list) option
21 subsection \<open>eval_ functions\<close>
23 factors_from_solution :: "bool list => real"
27 text \<open>these might be used for variants of fac_from_sol\<close>
29 fun mk_minus_1 T = Free("-1", T); (*TODO DELETE WITH numbers_to_string*)
30 fun flip_sign t = (*TODO improve for use in factors_from_solution: -(-1) etc*)
31 let val minus_1 = t |> type_of |> mk_minus_1
32 in HOLogic.mk_binop \<^const_name>\<open>times\<close> (minus_1, t) end;
35 text \<open>from solutions (e.g. [z = 1, z = -2]) make linear factors (e.g. (z - 1)*(z - -2))\<close>
38 let val (lhs, rhs) = HOLogic.dest_eq s
39 in HOLogic.mk_binop \<^const_name>\<open>minus\<close> (lhs, rhs) end;
42 if prod = TermC.empty then raise ERROR "mk_prod called with []" else prod
43 | mk_prod prod (t :: []) =
44 if prod = TermC.empty then t else HOLogic.mk_binop \<^const_name>\<open>times\<close> (prod, t)
45 | mk_prod prod (t1 :: t2 :: ts) =
48 let val p = HOLogic.mk_binop \<^const_name>\<open>times\<close> (t1, t2)
51 let val p = HOLogic.mk_binop \<^const_name>\<open>times\<close> (prod, t1)
52 in mk_prod p (t2 :: ts) end
54 fun factors_from_solution sol =
55 let val ts = HOLogic.dest_list sol
56 in mk_prod TermC.empty (map fac_from_sol ts) end;
58 (*("factors_from_solution", ("Partial_Fractions.factors_from_solution",
59 eval_factors_from_solution ""))
60 this code is limited (max 3 solutions) AND has too little checks *)
61 fun eval_factors_from_solution (thmid:string) _
62 (t as Const ("Partial_Fractions.factors_from_solution", _) $ sol) thy =
63 let val prod = factors_from_solution sol
64 in SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy prod) "",
65 HOLogic.Trueprop $ (TermC.mk_equality (t, prod)))
67 | eval_factors_from_solution _ _ _ _ = NONE;
70 subsection \<open>'ansatz' for partial fractions\<close>
72 ansatz_2nd_order: "n / (a*b) = AA/a + BB/b" and
73 ansatz_3rd_order: "n / (a*b*c) = AA/a + BB/b + C/c" and
74 ansatz_4th_order: "n / (a*b*c*d) = AA/a + BB/b + C/c + D/d" and
76 equival_trans_2nd_order: "(n/(a*b) = AA/a + BB/b) = (n = AA*b + BB*a)" and
77 equival_trans_3rd_order: "(n/(a*b*c) = AA/a + BB/b + C/c) = (n = AA*b*c + BB*a*c + C*a*b)" and
78 equival_trans_4th_order: "(n/(a*b*c*d) = AA/a + BB/b + C/c + D/d) =
79 (n = AA*b*c*d + BB*a*c*d + C*a*b*d + D*a*b*c)" and
80 (*version 2: not yet used, see partial_fractions.sml*)
81 multiply_2nd_order: "(n/x = AA/a + BB/b) = (a*b*n/x = AA*b + BB*a)" and
82 multiply_3rd_order: "(n/x = AA/a + BB/b + C/c) = (a*b*c*n/x = AA*b*c + BB*a*c + C*a*b)" and
84 "(n/x = AA/a + BB/b + C/c + D/d) = (a*b*c*d*n/x = AA*b*c*d + BB*a*c*d + C*a*b*d + D*a*b*c)"
86 text \<open>Probably the optimal formalization woudl be ...
88 multiply_2nd_order: "x = a*b ==> (n/x = AA/a + BB/b) = (a*b*n/x = AA*b + BB*a)" and
89 multiply_3rd_order: "x = a*b*c ==>
90 (n/x = AA/a + BB/b + C/c) = (a*b*c*n/x = AA*b*c + BB*a*c + C*a*b)" and
91 multiply_4th_order: "x = a*b*c*d ==>
92 (n/x = AA/a + BB/b + C/c + D/d) = (a*b*c*d*n/x = AA*b*c*d + BB*a*c*d + C*a*b*d + D*a*b*c)"
94 ... because it would allow to start the ansatz as follows
95 (1) 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))
96 (2) 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = AA / (z - 1 / 2) + BB / (z - -1 / 4)
97 (3) (z - 1 / 2) * (z - -1 / 4) * 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) =
98 (z - 1 / 2) * (z - -1 / 4) * AA / (z - 1 / 2) + BB / (z - -1 / 4)
99 (4) 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)
103 (3==>4) norm_Rational
104 TODOs for this version are in partial_fractions.sml "--- progr.vers.2: "
108 val ansatz_rls = prep_rls'(
109 Rule_Def.Repeat {id = "ansatz_rls", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
110 erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
112 [\<^rule_thm>\<open>ansatz_2nd_order\<close>,
113 \<^rule_thm>\<open>ansatz_3rd_order\<close>
115 scr = Rule.Empty_Prog});
117 val equival_trans = prep_rls'(
118 Rule_Def.Repeat {id = "equival_trans", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
119 erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
121 [\<^rule_thm>\<open>equival_trans_2nd_order\<close>,
122 \<^rule_thm>\<open>equival_trans_3rd_order\<close>
124 scr = Rule.Empty_Prog});
126 val multiply_ansatz = prep_rls'(
127 Rule_Def.Repeat {id = "multiply_ansatz", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
128 erls = Rule_Set.Empty,
129 srls = Rule_Set.Empty, calc = [], errpatts = [],
131 [\<^rule_thm>\<open>multiply_2nd_order\<close>
133 scr = Rule.Empty_Prog});
136 text \<open>store the rule set for math engine\<close>
138 ansatz_rls = ansatz_rls and
139 multiply_ansatz = multiply_ansatz and
140 equival_trans = equival_trans
142 subsection \<open>Specification\<close>
145 decomposedFunction :: "real => una"
148 Check_Unique.on := false; (*WN120307 REMOVE after editing*)
150 problem pbl_simp_rat_partfrac : "partial_fraction/rational/simplification" =
151 \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_ TODO*)]\<close>
152 Method: "simplification/of_rationals/to_partial_fraction"
153 Given: "functionTerm t_t" "solveFor v_v"
154 (* TODO: call this sub-problem with appropriate functionTerm:
155 leading coefficient of the denominator is 1: to be checked here! and..
156 Where: "((get_numerator t_t) has_degree_in v_v) <
157 ((get_denominator t_t) has_degree_in v_v)" TODO*)
158 Find: "decomposedFunction p_p'''"
160 subsection \<open>MethodC\<close>
161 text \<open>rule set for functions called in the Program\<close>
163 val srls_partial_fraction = Rule_Def.Repeat {id="srls_partial_fraction",
165 rew_ord = ("termlessI",termlessI),
166 erls = Rule_Set.append_rules "erls_in_srls_partial_fraction" Rule_Set.empty
167 [(*for asm in NTH_CONS ...*)
168 \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
169 (*2nd NTH_CONS pushes n+-1 into asms*)
170 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")],
171 srls = Rule_Set.Empty, calc = [], errpatts = [],
173 \<^rule_thm>\<open>NTH_CONS\<close>,
174 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
175 \<^rule_thm>\<open>NTH_NIL\<close>,
176 \<^rule_eval>\<open>Prog_Expr.lhs\<close> (Prog_Expr.eval_lhs "eval_lhs_"),
177 \<^rule_eval>\<open>Prog_Expr.rhs\<close> (Prog_Expr.eval_rhs"eval_rhs_"),
178 \<^rule_eval>\<open>Prog_Expr.argument_in\<close> (Prog_Expr.eval_argument_in "Prog_Expr.argument_in"),
179 \<^rule_eval>\<open>get_denominator\<close> (eval_get_denominator "#get_denominator"),
180 \<^rule_eval>\<open>get_numerator\<close> (eval_get_numerator "#get_numerator"),
181 \<^rule_eval>\<open>factors_from_solution\<close>
182 (eval_factors_from_solution "#factors_from_solution")
184 scr = Rule.Empty_Prog};
187 (* current version, error outcommented *)
188 partial_function (tailrec) partial_fraction :: "real \<Rightarrow> real \<Rightarrow> real"
190 "partial_fraction f_f zzz = \<comment> \<open>([1], Frm), 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))\<close>
191 (let f_f = Take f_f; \<comment> \<open>num_orig = 3\<close>
192 num_orig = get_numerator f_f; \<comment> \<open>([1], Res), 24 / (-1 + -2 * z + 8 * z \<up> 2)\<close>
193 f_f = (Rewrite_Set ''norm_Rational'') f_f; \<comment> \<open>denom = -1 + -2 * z + 8 * z \<up> 2\<close>
194 denom = get_denominator f_f; \<comment> \<open>equ = -1 + -2 * z + 8 * z \<up> 2 = 0\<close>
195 equ = denom = (0::real);
196 \<comment> \<open>----- ([2], Pbl), solve (-1 + -2 * z + 8 * z \<up> 2 = 0, z)\<close>
197 L_L = SubProblem (''Partial_Fractions'', \<comment> \<open>([2], Res), [z = 1 / 2, z = -1 / 4\<close>
198 [''abcFormula'', ''degree_2'', ''polynomial'', ''univariate'', ''equation''],
199 [''no_met'']) [BOOL equ, REAL zzz]; \<comment> \<open>facs: (z - 1 / 2) * (z - -1 / 4)\<close>
200 facs = factors_from_solution L_L; \<comment> \<open>([3], Frm), 33 / ((z - 1 / 2) * (z - -1 / 4))\<close>
201 eql = Take (num_orig / facs); \<comment> \<open>([3], Res), ?A / (z - 1 / 2) + ?B / (z - -1 / 4)\<close>
202 eqr = (Try (Rewrite_Set ''ansatz_rls'')) eql;
203 \<comment> \<open>([4], Frm), 3 / ((z - 1 / 2) * (z - -1 / 4)) = ?A / (z - 1 / 2) + ?B / (z - -1 / 4)\<close>
204 eq = Take (eql = eqr); \<comment> \<open>([4], Res), 3 = ?A * (z - -1 / 4) + ?B * (z - 1 / 2)\<close>
205 eq = (Try (Rewrite_Set ''equival_trans'')) eq;
206 \<comment> \<open>eq = 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)\<close>
207 z1 = rhs (NTH 1 L_L); \<comment> \<open>z2 = -1 / 4\<close>
208 z2 = rhs (NTH 2 L_L); \<comment> \<open>([5], Frm), 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)\<close>
209 eq_a = Take eq; \<comment> \<open>([5], Res), 3 = AA * (1 / 2 - -1 / 4) + BB * (1 / 2 - 1 / 2)\<close>
210 eq_a = Substitute [zzz = z1] eq; \<comment> \<open>([6], Res), 3 = 3 * AA / 4\<close>
211 eq_a = (Rewrite_Set ''norm_Rational'') eq_a;
212 \<comment> \<open>----- ([7], Pbl), solve (3 = 3 * AA / 4, AA)\<close>
213 \<comment> \<open>([7], Res), [AA = 4]\<close>
214 sol_a = SubProblem (''Isac_Knowledge'', [''univariate'',''equation''], [''no_met''])
215 [BOOL eq_a, REAL (AA::real)] ; \<comment> \<open>a = 4\<close>
216 a = rhs (NTH 1 sol_a); \<comment> \<open>([8], Frm), 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)\<close>
217 eq_b = Take eq; \<comment> \<open>([8], Res), 3 = AA * (-1 / 4 - -1 / 4) + BB * (-1 / 4 - 1 / 2)\<close>
218 eq_b = Substitute [zzz = z2] eq_b; \<comment> \<open>([9], Res), 3 = -3 * BB / 4\<close>
219 eq_b = (Rewrite_Set ''norm_Rational'') eq_b; \<comment> \<open>([10], Pbl), solve (3 = -3 * BB / 4, BB)\<close>
220 sol_b = SubProblem (''Isac_Knowledge'', \<comment> \<open>([10], Res), [BB = -4]\<close>
221 [''univariate'',''equation''], [''no_met''])
222 [BOOL eq_b, REAL (BB::real)]; \<comment> \<open>b = -4\<close>
223 b = rhs (NTH 1 sol_b); \<comment> \<open>eqr = AA / (z - 1 / 2) + BB / (z - -1 / 4)\<close>
224 pbz = Take eqr; \<comment> \<open>([11], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)\<close>
225 pbz = Substitute [AA = a, BB = b] pbz \<comment> \<open>([], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)\<close>
228 method met_partial_fraction : "simplification/of_rationals/to_partial_fraction" =
229 \<open>{rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = srls_partial_fraction, prls = Rule_Set.empty,
230 crls = Rule_Set.empty, errpats = [], nrls = Rule_Set.empty}\<close>
231 (*f_f = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)), zzz: z*)
232 (*([], Frm), Problem (Partial_Fractions, [partial_fraction, rational, simplification])*)
233 Program: partial_fraction.simps
234 Given: "functionTerm t_t" "solveFor v_v"
235 (* Where: "((get_numerator t_t) has_degree_in v_v) <
236 ((get_denominator t_t) has_degree_in v_v)" TODO *)
237 Find: "decomposedFunction p_p'''"
242 ["functionTerm (3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))))",
243 "solveFor z", "functionTerm p_p"];
245 ("Partial_Fractions",
246 ["partial_fraction", "rational", "simplification"],
247 ["simplification", "of_rationals", "to_partial_fraction"]);
248 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];