src/Tools/isac/Knowledge/Partial_Fractions.thy
author Walther Neuper <neuper@ist.tugraz.at>
Mon, 22 Jul 2013 13:52:18 +0200
changeset 52070 77138c64f4f6
parent 48761 4162c4f6f897
child 52125 6f1d3415dc68
permissions -rwxr-xr-x
--- Test_Isac.thy runs all tests

NOTEs:
(1) inserted many "trace_rewrite := false" ...
(2) ...nevertheless needs manual interaction at "Tracing paused. Stop, or continue with next 100, 1000, 10000 messages?"
(3) state of tests see respective section in Test_Isac.thy
     1 (* Partial_Fractions 
     2    author: Jan Rocnik, isac team
     3    Copyright (c) isac team 2011
     4    Use is subject to license terms.
     5 
     6 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
     7         10        20        30        40        50        60        70        80         90      100
     8 *)
     9 header {* Partial Fraction Decomposition *}
    10 
    11 
    12 theory Partial_Fractions imports RootRatEq begin
    13 
    14 ML{*
    15 (*
    16 signature PARTFRAC =
    17 sig
    18   val ansatz_rls : rls
    19   val ansatz_rls_ : theory -> term -> (term * term list) option
    20 end
    21 *)
    22 *}
    23 
    24 subsection {* eval_ functions *}
    25 consts
    26   factors_from_solution :: "bool list => real"
    27   drop_questionmarks    :: "'a => 'a"
    28 
    29 text {* these might be used for variants of fac_from_sol *}
    30 ML {*
    31 fun mk_minus_1 T = Free("-1", T); (*TODO DELETE WITH numbers_to_string*)
    32 fun flip_sign t = (*TODO improve for use in factors_from_solution: -(-1) etc*)
    33   let val minus_1 = t |> type_of |> mk_minus_1
    34   in HOLogic.mk_binop "Groups.times_class.times" (minus_1, t) end;
    35 *}
    36 
    37 text {* from solutions (e.g. [z = 1, z = -2]) make linear factors (e.g. (z - 1)*(z - -2)) *}
    38 ML {*
    39 fun fac_from_sol s =
    40   let val (lhs, rhs) = HOLogic.dest_eq s
    41   in HOLogic.mk_binop "Groups.minus_class.minus" (lhs, rhs) end;
    42 
    43 fun mk_prod prod [] =
    44       if prod = e_term then error "mk_prod called with []" else prod
    45   | mk_prod prod (t :: []) =
    46       if prod = e_term then t else HOLogic.mk_binop "Groups.times_class.times" (prod, t)
    47   | mk_prod prod (t1 :: t2 :: ts) =
    48         if prod = e_term 
    49         then 
    50            let val p = HOLogic.mk_binop "Groups.times_class.times" (t1, t2)
    51            in mk_prod p ts end 
    52         else 
    53            let val p = HOLogic.mk_binop "Groups.times_class.times" (prod, t1)
    54            in mk_prod p (t2 :: ts) end 
    55 
    56 fun factors_from_solution sol = 
    57   let val ts = HOLogic.dest_list sol
    58   in mk_prod e_term (map fac_from_sol ts) end;
    59 
    60 (*("factors_from_solution", ("Partial_Fractions.factors_from_solution", 
    61      eval_factors_from_solution ""))*)
    62 fun eval_factors_from_solution (thmid:string) _
    63      (t as Const ("Partial_Fractions.factors_from_solution", _) $ sol) thy =
    64        ((let val prod = factors_from_solution sol
    65          in SOME (mk_thmid thmid "" (term_to_string''' thy prod) "",
    66               Trueprop $ (mk_equality (t, prod)))
    67          end)
    68        handle _ => NONE)
    69  | eval_factors_from_solution _ _ _ _ = NONE;
    70 *}
    71 
    72 text {* 'ansatz' introduces '?Vars' (questionable design); drop these again *}
    73 ML {*
    74 (*("drop_questionmarks", ("Partial_Fractions.drop_questionmarks", eval_drop_questionmarks ""))*)
    75 fun eval_drop_questionmarks (thmid:string) _
    76      (t as Const ("Partial_Fractions.drop_questionmarks", _) $ tm) thy =
    77         if contains_Var tm
    78         then
    79           let
    80             val tm' = var2free tm
    81             in SOME (mk_thmid thmid "" (term_to_string''' thy tm') "",
    82                  Trueprop $ (mk_equality (t, tm')))
    83             end
    84         else NONE
    85   | eval_drop_questionmarks _ _ _ _ = NONE;
    86 *}
    87 
    88 text {* store eval_ functions for calls from Scripts *}
    89 ML {*
    90 calclist':= overwritel (!calclist',
    91   [("drop_questionmarks", ("Partial_Fractions.drop'_questionmarks", eval_drop_questionmarks ""))
    92   ]);
    93 *}
    94 
    95 subsection {* 'ansatz' for partial fractions *}
    96 axiomatization where
    97   ansatz_2nd_order: "n / (a*b) = A/a + B/b" and
    98   ansatz_3rd_order: "n / (a*b*c) = A/a + B/b + C/c" and
    99   ansatz_4th_order: "n / (a*b*c*d) = A/a + B/b + C/c + D/d" and
   100   (*version 1*)
   101   equival_trans_2nd_order: "(n/(a*b) = A/a + B/b) = (n = A*b + B*a)" and
   102   equival_trans_3rd_order: "(n/(a*b*c) = A/a + B/b + C/c) = (n = A*b*c + B*a*c + C*a*b)" and
   103   equival_trans_4th_order: "(n/(a*b*c*d) = A/a + B/b + C/c + D/d) = 
   104     (n = A*b*c*d + B*a*c*d + C*a*b*d + D*a*b*c)" and
   105   (*version 2: not yet used, see partial_fractions.sml*)
   106   multiply_2nd_order: "(n/x = A/a + B/b) = (a*b*n/x = A*b + B*a)" and
   107   multiply_3rd_order: "(n/x = A/a + B/b + C/c) = (a*b*c*n/x = A*b*c + B*a*c + C*a*b)" and
   108   multiply_4th_order: 
   109     "(n/x = A/a + B/b + C/c + D/d) = (a*b*c*d*n/x = A*b*c*d + B*a*c*d + C*a*b*d + D*a*b*c)"
   110 
   111 text {* Probably the optimal formalization woudl be ...
   112 
   113   multiply_2nd_order: "x = a*b ==> (n/x = A/a + B/b) = (a*b*n/x = A*b + B*a)" and
   114   multiply_3rd_order: "x = a*b*c ==>
   115     (n/x = A/a + B/b + C/c) = (a*b*c*n/x = A*b*c + B*a*c + C*a*b)" and
   116   multiply_4th_order: "x = a*b*c*d ==>
   117     (n/x = A/a + B/b + C/c + D/d) = (a*b*c*d*n/x = A*b*c*d + B*a*c*d + C*a*b*d + D*a*b*c)"
   118 
   119 ... because it would allow to start the ansatz as follows
   120 (1) 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))
   121 (2) 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = AA / (z - 1 / 2) + BB / (z - -1 / 4)
   122 (3) (z - 1 / 2) * (z - -1 / 4) * 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = 
   123     (z - 1 / 2) * (z - -1 / 4) * AA / (z - 1 / 2) + BB / (z - -1 / 4)
   124 (4) 3 = A * (z - -1 / 4) + B * (z - 1 / 2)
   125 
   126 ... (1==>2) ansatz
   127     (2==>3) multiply_*
   128     (3==>4) norm_Rational
   129 TODOs for this version ar in partial_fractions.sml "--- progr.vers.2: "
   130 *}
   131 
   132 ML {*
   133 val ansatz_rls = prep_rls(
   134   Rls {id = "ansatz_rls", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
   135 	  erls = Erls, srls = Erls, calc = [], errpatts = [],
   136 	  rules = 
   137 	   [Thm ("ansatz_2nd_order",num_str @{thm ansatz_2nd_order}),
   138 	    Thm ("ansatz_3rd_order",num_str @{thm ansatz_3rd_order})
   139 	   ], 
   140 	 scr = EmptyScr}:rls);
   141 
   142 val equival_trans = prep_rls(
   143   Rls {id = "equival_trans", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
   144 	  erls = Erls, srls = Erls, calc = [], errpatts = [],
   145 	  rules = 
   146 	   [Thm ("equival_trans_2nd_order",num_str @{thm equival_trans_2nd_order}),
   147 	    Thm ("equival_trans_3rd_order",num_str @{thm equival_trans_3rd_order})
   148 	   ], 
   149 	 scr = EmptyScr}:rls);
   150 
   151 val multiply_ansatz = prep_rls(
   152   Rls {id = "multiply_ansatz", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
   153 	  erls = Erls,
   154 	  srls = Erls, calc = [], errpatts = [],
   155 	  rules = 
   156 	   [Thm ("multiply_2nd_order",num_str @{thm multiply_2nd_order})
   157 	   ], 
   158 	 scr = EmptyScr}:rls);
   159 *}
   160 
   161 text {*store the rule set for math engine*}
   162 ML {*
   163 ruleset' := overwritelthy @{theory} (!ruleset',
   164   [("ansatz_rls", ansatz_rls),
   165    ("multiply_ansatz", multiply_ansatz),
   166    ("equival_trans", equival_trans)
   167    ]);
   168 *}
   169 
   170 subsection {* Specification *}
   171 
   172 consts
   173   decomposedFunction :: "real => una"
   174 
   175 ML {*
   176 check_guhs_unique := false; (*WN120307 REMOVE after editing*)
   177 store_pbt
   178  (prep_pbt @{theory} "pbl_simp_rat_partfrac" [] e_pblID
   179  (["partial_fraction", "rational", "simplification"],
   180   [("#Given" ,["functionTerm t_t", "solveFor v_v"]),
   181    (* TODO: call this sub-problem with appropriate functionTerm: 
   182       leading coefficient of the denominator is 1: to be checked here! and..
   183      ("#Where" ,["((get_numerator t_t) has_degree_in v_v) < 
   184                   ((get_denominator t_t) has_degree_in v_v)"]), TODO*)
   185    ("#Find"  ,["decomposedFunction p_p'''"])
   186   ],
   187   append_rls "e_rls" e_rls [(*for preds in where_ TODO*)], 
   188   NONE, 
   189   [["simplification","of_rationals","to_partial_fraction"]]));
   190 *}
   191 
   192 subsection {* Method *}
   193 consts
   194   PartFracScript  :: "[real,real,  real] => real" 
   195     ("((Script PartFracScript (_ _ =))// (_))" 9)
   196 
   197 text {* rule set for functions called in the Script *}
   198 ML {*
   199   val srls_partial_fraction = Rls {id="srls_partial_fraction", 
   200     preconds = [],
   201     rew_ord = ("termlessI",termlessI),
   202     erls = append_rls "erls_in_srls_partial_fraction" e_rls
   203       [(*for asm in NTH_CONS ...*)
   204        Calc ("Orderings.ord_class.less",eval_equ "#less_"),
   205        (*2nd NTH_CONS pushes n+-1 into asms*)
   206        Calc("Groups.plus_class.plus", eval_binop "#add_")], 
   207     srls = Erls, calc = [], errpatts = [],
   208     rules = [
   209        Thm ("NTH_CONS",num_str @{thm NTH_CONS}),
   210        Calc("Groups.plus_class.plus", eval_binop "#add_"),
   211        Thm ("NTH_NIL",num_str @{thm NTH_NIL}),
   212        Calc("Tools.lhs", eval_lhs "eval_lhs_"),
   213        Calc("Tools.rhs", eval_rhs"eval_rhs_"),
   214        Calc("Atools.argument'_in", eval_argument_in "Atools.argument'_in"),
   215        Calc("Rational.get_denominator", eval_get_denominator "#get_denominator"),
   216        Calc("Rational.get_numerator", eval_get_numerator "#get_numerator"),
   217        Calc("Partial_Fractions.factors_from_solution",
   218          eval_factors_from_solution "#factors_from_solution"),
   219        Calc("Partial_Fractions.drop_questionmarks", eval_drop_questionmarks "#drop_?")],
   220     scr = EmptyScr};
   221 *}
   222 ML {*
   223 eval_drop_questionmarks;
   224 *}
   225 ML {*
   226 val ctxt = Proof_Context.init_global @{theory};
   227 val SOME t = parseNEW ctxt "eqr = drop_questionmarks eqr";
   228 *}
   229 ML {*
   230 parseNEW ctxt "decomposedFunction p_p'''";
   231 parseNEW ctxt "decomposedFunction";
   232 *}
   233 ML {*
   234 *}
   235 ML {* (* current version, error outcommented *)
   236 store_met
   237  (prep_met @{theory} "met_partial_fraction" [] e_metID
   238  (["simplification","of_rationals","to_partial_fraction"], 
   239   [("#Given" ,["functionTerm t_t", "solveFor v_v"]),
   240    (*("#Where" ,["((get_numerator t_t) has_degree_in v_v) < 
   241                   ((get_denominator t_t) has_degree_in v_v)"]), TODO*)
   242    ("#Find"  ,["decomposedFunction p_p'''"])],
   243    {rew_ord'="tless_true", rls'= e_rls, calc = [], srls = srls_partial_fraction, prls = e_rls,
   244     crls = e_rls, errpats = [], nrls = e_rls},                         (*f_f = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)), zzz: z*)
   245    "Script PartFracScript (f_f::real) (zzz::real) =   " ^(*([], Frm), Problem (Partial_Fractions, [partial_fraction, rational, simplification])*)
   246    "(let f_f = Take f_f;                              " ^(*([1], Frm), 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   247    "  (num_orig::real) = get_numerator f_f;           " ^(*           num_orig = 3*)
   248    "  f_f = (Rewrite_Set norm_Rational False) f_f;    " ^(*([1], Res), 24 / (-1 + -2 * z + 8 * z ^^^ 2)*)
   249    "  (denom::real) = get_denominator f_f;            " ^(*           denom = -1 + -2 * z + 8 * z ^^^ 2*)
   250    "  (equ::bool) = (denom = (0::real));              " ^(*           equ = -1 + -2 * z + 8 * z ^^^ 2 = 0*)
   251 
   252    "  (L_L::bool list) = (SubProblem (PolyEq',        " ^(*([2], Pbl), solve (-1 + -2 * z + 8 * z ^^^ 2 = 0, z)*)
   253    "    [abcFormula, degree_2, polynomial, univariate, equation], " ^
   254    "    [no_met]) [BOOL equ, REAL zzz]);              " ^(*([2], Res), [z = 1 / 2, z = -1 / 4]*)
   255    "  (facs::real) = factors_from_solution L_L;       " ^(*           facs: (z - 1 / 2) * (z - -1 / 4)*)
   256    "  (eql::real) = Take (num_orig / facs);           " ^(*([3], Frm), 33 / ((z - 1 / 2) * (z - -1 / 4)) *) 
   257    "  (eqr::real) = (Try (Rewrite_Set ansatz_rls False)) eql;  " ^(*([3], Res), ?A / (z - 1 / 2) + ?B / (z - -1 / 4)*)
   258    "  (eq::bool) = Take (eql = eqr);                  " ^(*([4], Frm), 3 / ((z - 1 / 2) * (z - -1 / 4)) = ?A / (z - 1 / 2) + ?B / (z - -1 / 4)*)
   259    "  eq = (Try (Rewrite_Set equival_trans False)) eq;" ^(*([4], Res), 3 = ?A * (z - -1 / 4) + ?B * (z - 1 / 2)*) 
   260    "  eq = drop_questionmarks eq;                     " ^(*           eq = 3 = A * (z - -1 / 4) + B * (z - 1 / 2)*)
   261    "  (z1::real) = (rhs (NTH 1 L_L));                 " ^(*           z1 = 1 / 2*)
   262    "  (z2::real) = (rhs (NTH 2 L_L));                 " ^(*           z2 = -1 / 4*)
   263    "  (eq_a::bool) = Take eq;                         " ^(*([5], Frm), 3 = A * (z - -1 / 4) + B * (z - 1 / 2)*)
   264    "  eq_a = (Substitute [zzz = z1]) eq;              " ^(*([5], Res), 3 = A * (1 / 2 - -1 / 4) + B * (1 / 2 - 1 / 2)*)
   265    "  eq_a = (Rewrite_Set norm_Rational False) eq_a;  " ^(*([6], Res), 3 = 3 * A / 4*)
   266 
   267    "  (sol_a::bool list) =                            " ^(*([7], Pbl), solve (3 = 3 * A / 4, A)*)
   268    "    (SubProblem (Isac', [univariate,equation], [no_met])   " ^
   269    "    [BOOL eq_a, REAL (A::real)]);                 " ^(*([7], Res), [A = 4]*)
   270    "  (a::real) = (rhs (NTH 1 sol_a));                " ^(*           a = 4*)
   271    "  (eq_b::bool) = Take eq;                         " ^(*([8], Frm), 3 = A * (z - -1 / 4) + B * (z - 1 / 2)*)
   272    "  eq_b = (Substitute [zzz = z2]) eq_b;            " ^(*([8], Res), 3 = A * (-1 / 4 - -1 / 4) + B * (-1 / 4 - 1 / 2)*)
   273    "  eq_b = (Rewrite_Set norm_Rational False) eq_b;  " ^(*([9], Res), 3 = -3 * B / 4*)
   274    "  (sol_b::bool list) =                            " ^(*([10], Pbl), solve (3 = -3 * B / 4, B)*)
   275    "    (SubProblem (Isac', [univariate,equation], [no_met])   " ^
   276    "    [BOOL eq_b, REAL (B::real)]);                 " ^(*([10], Res), [B = -4]*)
   277    "  (b::real) = (rhs (NTH 1 sol_b));                " ^(*           b = -4*)
   278    "  eqr = drop_questionmarks eqr;                   " ^(*           eqr = A / (z - 1 / 2) + B / (z - -1 / 4)*)
   279    "  (pbz::real) = Take eqr;                         " ^(*([11], Frm), A / (z - 1 / 2) + B / (z - -1 / 4)*)
   280    "  pbz = ((Substitute [A = a, B = b]) pbz)         " ^(*([11], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   281    "in pbz)"                                             (*([], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   282 ));
   283 *}
   284 ML {*
   285 (*
   286   val fmz =                                             
   287     ["functionTerm (3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))))", 
   288       "solveFor z", "functionTerm p_p"];
   289   val (dI',pI',mI') =
   290     ("Partial_Fractions", 
   291       ["partial_fraction", "rational", "simplification"],
   292       ["simplification","of_rationals","to_partial_fraction"]);
   293   val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
   294 *)
   295 *}
   296 
   297 
   298 
   299 subsection {**}
   300 
   301 end