src/Tools/isac/Knowledge/Partial_Fractions.thy
author Walther Neuper <walther.neuper@jku.at>
Tue, 01 Oct 2019 10:47:25 +0200
changeset 59635 9fc1bb69813c
parent 59603 30cd47104ad7
child 59773 d88bb023c380
permissions -rw-r--r--
lucin: drop unused bool argument in tactic Rewrite*Inst
neuper@42376
     1
(* Partial_Fractions 
jan@42344
     2
   author: Jan Rocnik, isac team
jan@42344
     3
   Copyright (c) isac team 2011
jan@42344
     4
   Use is subject to license terms.
jan@42344
     5
*)
wneuper@59344
     6
(* Partial Fraction Decomposition *)
jan@42344
     7
neuper@42289
     8
neuper@42376
     9
theory Partial_Fractions imports RootRatEq begin
jan@42353
    10
wneuper@59472
    11
ML\<open>
neuper@42376
    12
(*
jan@42353
    13
signature PARTFRAC =
jan@42353
    14
sig
jan@42353
    15
  val ansatz_rls : rls
jan@42353
    16
  val ansatz_rls_ : theory -> term -> (term * term list) option
jan@42353
    17
end
neuper@42376
    18
*)
wneuper@59472
    19
\<close>
jan@42353
    20
wneuper@59472
    21
subsection \<open>eval_ functions\<close>
jan@42344
    22
consts
neuper@42359
    23
  factors_from_solution :: "bool list => real"
wneuper@59512
    24
  AA                    :: real
wneuper@59512
    25
  BB                    :: real
jan@42344
    26
wneuper@59472
    27
text \<open>these might be used for variants of fac_from_sol\<close>
wneuper@59472
    28
ML \<open>
jan@42344
    29
fun mk_minus_1 T = Free("-1", T); (*TODO DELETE WITH numbers_to_string*)
jan@42344
    30
fun flip_sign t = (*TODO improve for use in factors_from_solution: -(-1) etc*)
jan@42344
    31
  let val minus_1 = t |> type_of |> mk_minus_1
jan@42344
    32
  in HOLogic.mk_binop "Groups.times_class.times" (minus_1, t) end;
wneuper@59472
    33
\<close>
neuper@42376
    34
wneuper@59472
    35
text \<open>from solutions (e.g. [z = 1, z = -2]) make linear factors (e.g. (z - 1)*(z - -2))\<close>
wneuper@59472
    36
ML \<open>
jan@42344
    37
fun fac_from_sol s =
jan@42344
    38
  let val (lhs, rhs) = HOLogic.dest_eq s
jan@42367
    39
  in HOLogic.mk_binop "Groups.minus_class.minus" (lhs, rhs) end;
jan@42344
    40
jan@42344
    41
fun mk_prod prod [] =
wneuper@59416
    42
      if prod = Rule.e_term then error "mk_prod called with []" else prod
jan@42344
    43
  | mk_prod prod (t :: []) =
wneuper@59416
    44
      if prod = Rule.e_term then t else HOLogic.mk_binop "Groups.times_class.times" (prod, t)
jan@42344
    45
  | mk_prod prod (t1 :: t2 :: ts) =
wneuper@59416
    46
        if prod = Rule.e_term 
jan@42344
    47
        then 
jan@42344
    48
           let val p = HOLogic.mk_binop "Groups.times_class.times" (t1, t2)
jan@42344
    49
           in mk_prod p ts end 
jan@42344
    50
        else 
jan@42344
    51
           let val p = HOLogic.mk_binop "Groups.times_class.times" (prod, t1)
jan@42344
    52
           in mk_prod p (t2 :: ts) end 
jan@42344
    53
jan@42344
    54
fun factors_from_solution sol = 
jan@42344
    55
  let val ts = HOLogic.dest_list sol
wneuper@59416
    56
  in mk_prod Rule.e_term (map fac_from_sol ts) end;
jan@42344
    57
neuper@42376
    58
(*("factors_from_solution", ("Partial_Fractions.factors_from_solution", 
neuper@42376
    59
     eval_factors_from_solution ""))*)
jan@42352
    60
fun eval_factors_from_solution (thmid:string) _
jan@42352
    61
     (t as Const ("Partial_Fractions.factors_from_solution", _) $ sol) thy =
jan@42352
    62
       ((let val prod = factors_from_solution sol
wneuper@59416
    63
         in SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy prod) "",
wneuper@59390
    64
              HOLogic.Trueprop $ (TermC.mk_equality (t, prod)))
jan@42352
    65
         end)
jan@42352
    66
       handle _ => NONE)
jan@42352
    67
 | eval_factors_from_solution _ _ _ _ = NONE;
wneuper@59472
    68
\<close>
jan@42344
    69
wneuper@59472
    70
subsection \<open>'ansatz' for partial fractions\<close>
jan@42353
    71
axiomatization where
wneuper@59512
    72
  ansatz_2nd_order: "n / (a*b) = AA/a + BB/b" and
wneuper@59512
    73
  ansatz_3rd_order: "n / (a*b*c) = AA/a + BB/b + C/c" and
wneuper@59512
    74
  ansatz_4th_order: "n / (a*b*c*d) = AA/a + BB/b + C/c + D/d" and
neuper@42386
    75
  (*version 1*)
wneuper@59512
    76
  equival_trans_2nd_order: "(n/(a*b) = AA/a + BB/b) = (n = AA*b + BB*a)" and
wneuper@59512
    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
wneuper@59512
    78
  equival_trans_4th_order: "(n/(a*b*c*d) = AA/a + BB/b + C/c + D/d) = 
wneuper@59512
    79
    (n = AA*b*c*d + BB*a*c*d + C*a*b*d + D*a*b*c)" and
neuper@42386
    80
  (*version 2: not yet used, see partial_fractions.sml*)
wneuper@59512
    81
  multiply_2nd_order: "(n/x = AA/a + BB/b) = (a*b*n/x = AA*b + BB*a)" and
wneuper@59512
    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
neuper@42387
    83
  multiply_4th_order: 
wneuper@59512
    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)"
neuper@42387
    85
wneuper@59472
    86
text \<open>Probably the optimal formalization woudl be ...
neuper@42387
    87
wneuper@59512
    88
  multiply_2nd_order: "x = a*b ==> (n/x = AA/a + BB/b) = (a*b*n/x = AA*b + BB*a)" and
neuper@42386
    89
  multiply_3rd_order: "x = a*b*c ==>
wneuper@59512
    90
    (n/x = AA/a + BB/b + C/c) = (a*b*c*n/x = AA*b*c + BB*a*c + C*a*b)" and
neuper@42386
    91
  multiply_4th_order: "x = a*b*c*d ==>
wneuper@59512
    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)"
jan@42353
    93
neuper@42387
    94
... because it would allow to start the ansatz as follows
neuper@42387
    95
(1) 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))
neuper@42387
    96
(2) 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = AA / (z - 1 / 2) + BB / (z - -1 / 4)
neuper@42387
    97
(3) (z - 1 / 2) * (z - -1 / 4) * 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))) = 
neuper@42387
    98
    (z - 1 / 2) * (z - -1 / 4) * AA / (z - 1 / 2) + BB / (z - -1 / 4)
wneuper@59512
    99
(4) 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)
neuper@42387
   100
neuper@42387
   101
... (1==>2) ansatz
neuper@42387
   102
    (2==>3) multiply_*
neuper@42387
   103
    (3==>4) norm_Rational
wneuper@59550
   104
TODOs for this version are in partial_fractions.sml "--- progr.vers.2: "
wneuper@59472
   105
\<close>
neuper@42387
   106
wneuper@59472
   107
ML \<open>
s1210629013@55444
   108
val ansatz_rls = prep_rls'(
wneuper@59416
   109
  Rule.Rls {id = "ansatz_rls", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   110
	  erls = Rule.Erls, srls = Rule.Erls, calc = [], errpatts = [],
jan@42353
   111
	  rules = 
wneuper@59416
   112
	   [Rule.Thm ("ansatz_2nd_order",TermC.num_str @{thm ansatz_2nd_order}),
wneuper@59416
   113
	    Rule.Thm ("ansatz_3rd_order",TermC.num_str @{thm ansatz_3rd_order})
jan@42353
   114
	   ], 
wneuper@59416
   115
	 scr = Rule.EmptyScr});
jan@42353
   116
s1210629013@55444
   117
val equival_trans = prep_rls'(
wneuper@59416
   118
  Rule.Rls {id = "equival_trans", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   119
	  erls = Rule.Erls, srls = Rule.Erls, calc = [], errpatts = [],
jan@42354
   120
	  rules = 
wneuper@59416
   121
	   [Rule.Thm ("equival_trans_2nd_order",TermC.num_str @{thm equival_trans_2nd_order}),
wneuper@59416
   122
	    Rule.Thm ("equival_trans_3rd_order",TermC.num_str @{thm equival_trans_3rd_order})
jan@42354
   123
	   ], 
wneuper@59416
   124
	 scr = Rule.EmptyScr});
neuper@42386
   125
s1210629013@55444
   126
val multiply_ansatz = prep_rls'(
wneuper@59416
   127
  Rule.Rls {id = "multiply_ansatz", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   128
	  erls = Rule.Erls,
wneuper@59416
   129
	  srls = Rule.Erls, calc = [], errpatts = [],
neuper@42386
   130
	  rules = 
wneuper@59416
   131
	   [Rule.Thm ("multiply_2nd_order",TermC.num_str @{thm multiply_2nd_order})
neuper@42386
   132
	   ], 
wneuper@59416
   133
	 scr = Rule.EmptyScr});
wneuper@59472
   134
\<close>
jan@42354
   135
wneuper@59472
   136
text \<open>store the rule set for math engine\<close>
wneuper@59472
   137
setup \<open>KEStore_Elems.add_rlss 
neuper@52125
   138
  [("ansatz_rls", (Context.theory_name @{theory}, ansatz_rls)), 
neuper@52125
   139
  ("multiply_ansatz", (Context.theory_name @{theory}, multiply_ansatz)), 
wneuper@59472
   140
  ("equival_trans", (Context.theory_name @{theory}, equival_trans))]\<close>
jan@42344
   141
wneuper@59472
   142
subsection \<open>Specification\<close>
jan@42344
   143
neuper@42376
   144
consts
neuper@42376
   145
  decomposedFunction :: "real => una"
neuper@42376
   146
wneuper@59472
   147
ML \<open>
wneuper@59406
   148
Celem.check_guhs_unique := false; (*WN120307 REMOVE after editing*)
wneuper@59472
   149
\<close>
wneuper@59472
   150
setup \<open>KEStore_Elems.add_pbts
wneuper@59406
   151
  [(Specify.prep_pbt @{theory} "pbl_simp_rat_partfrac" [] Celem.e_pblID
s1210629013@55339
   152
      (["partial_fraction", "rational", "simplification"],
s1210629013@55339
   153
        [("#Given" ,["functionTerm t_t", "solveFor v_v"]),
s1210629013@55339
   154
          (* TODO: call this sub-problem with appropriate functionTerm: 
s1210629013@55339
   155
            leading coefficient of the denominator is 1: to be checked here! and..
s1210629013@55339
   156
            ("#Where" ,["((get_numerator t_t) has_degree_in v_v) < 
s1210629013@55339
   157
               ((get_denominator t_t) has_degree_in v_v)"]), TODO*)
s1210629013@55339
   158
          ("#Find"  ,["decomposedFunction p_p'''"])],
wneuper@59416
   159
        Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_ TODO*)], 
s1210629013@55339
   160
        NONE, 
wneuper@59472
   161
        [["simplification","of_rationals","to_partial_fraction"]]))]\<close>
jan@42354
   162
wneuper@59472
   163
subsection \<open>Method\<close>
wneuper@59585
   164
text \<open>rule set for functions called in the Program\<close>
wneuper@59472
   165
ML \<open>
wneuper@59416
   166
  val srls_partial_fraction = Rule.Rls {id="srls_partial_fraction", 
neuper@42376
   167
    preconds = [],
neuper@42376
   168
    rew_ord = ("termlessI",termlessI),
wneuper@59416
   169
    erls = Rule.append_rls "erls_in_srls_partial_fraction" Rule.e_rls
neuper@42376
   170
      [(*for asm in NTH_CONS ...*)
walther@59603
   171
       Rule.Calc ("Orderings.ord_class.less", Prog_Expr.eval_equ "#less_"),
neuper@42376
   172
       (*2nd NTH_CONS pushes n+-1 into asms*)
walther@59603
   173
       Rule.Calc("Groups.plus_class.plus", (**)eval_binop "#add_")], 
wneuper@59416
   174
    srls = Rule.Erls, calc = [], errpatts = [],
neuper@42376
   175
    rules = [
wneuper@59416
   176
       Rule.Thm ("NTH_CONS",TermC.num_str @{thm NTH_CONS}),
walther@59603
   177
       Rule.Calc("Groups.plus_class.plus", (**)eval_binop "#add_"),
wneuper@59416
   178
       Rule.Thm ("NTH_NIL",TermC.num_str @{thm NTH_NIL}),
walther@59603
   179
       Rule.Calc("Prog_Expr.lhs", Prog_Expr.eval_lhs "eval_lhs_"),
walther@59603
   180
       Rule.Calc("Prog_Expr.rhs", Prog_Expr.eval_rhs"eval_rhs_"),
walther@59603
   181
       Rule.Calc("Prog_Expr.argument'_in", Prog_Expr.eval_argument_in "Prog_Expr.argument'_in"),
wneuper@59416
   182
       Rule.Calc("Rational.get_denominator", eval_get_denominator "#get_denominator"),
wneuper@59416
   183
       Rule.Calc("Rational.get_numerator", eval_get_numerator "#get_numerator"),
wneuper@59416
   184
       Rule.Calc("Partial_Fractions.factors_from_solution",
wneuper@59512
   185
         eval_factors_from_solution "#factors_from_solution")
wneuper@59512
   186
       ],
wneuper@59416
   187
    scr = Rule.EmptyScr};
wneuper@59472
   188
\<close>
neuper@42415
   189
s1210629013@55380
   190
(* current version, error outcommented *)
wneuper@59504
   191
partial_function (tailrec) partial_fraction :: "real \<Rightarrow> real \<Rightarrow> real"
wneuper@59504
   192
  where
wneuper@59504
   193
"partial_fraction f_f zzz =              \<comment> \<open>([1], Frm), 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))\<close>
wneuper@59504
   194
(let f_f = Take f_f;                                                             \<comment> \<open>num_orig = 3\<close>
wneuper@59504
   195
  num_orig = get_numerator f_f;                  \<comment> \<open>([1], Res), 24 / (-1 + -2 * z + 8 * z ^^^ 2)\<close>
walther@59635
   196
  f_f = (Rewrite_Set ''norm_Rational'') f_f;                \<comment> \<open>denom = -1 + -2 * z + 8 * z ^^^ 2\<close>
wneuper@59504
   197
  denom = get_denominator f_f;                            \<comment> \<open>equ = -1 + -2 * z + 8 * z ^^^ 2 = 0\<close>
wneuper@59504
   198
  equ = denom = (0::real);
wneuper@59504
   199
  \<comment> \<open>-----                                  ([2], Pbl), solve (-1 + -2 * z + 8 * z ^^^ 2 = 0, z)\<close>
wneuper@59513
   200
  L_L = SubProblem (''Partial_Fractions'',                 \<comment> \<open>([2], Res), [z = 1 / 2, z = -1 / 4\<close>
wneuper@59504
   201
    [''abcFormula'', ''degree_2'', ''polynomial'', ''univariate'', ''equation''],
wneuper@59504
   202
    [''no_met'']) [BOOL equ, REAL zzz];                      \<comment> \<open>facs: (z - 1 / 2) * (z - -1 / 4)\<close>
wneuper@59504
   203
  facs = factors_from_solution L_L;             \<comment> \<open>([3], Frm), 33 / ((z - 1 / 2) * (z - -1 / 4))\<close>
wneuper@59504
   204
  eql = Take (num_orig / facs);              \<comment> \<open>([3], Res), ?A / (z - 1 / 2) + ?B / (z - -1 / 4)\<close>
walther@59635
   205
  eqr = (Try (Rewrite_Set ''ansatz_rls'')) eql;
wneuper@59504
   206
          \<comment> \<open>([4], Frm), 3 / ((z - 1 / 2) * (z - -1 / 4)) = ?A / (z - 1 / 2) + ?B / (z - -1 / 4)\<close>
wneuper@59536
   207
  eq = Take (eql = eqr);                 \<comment> \<open>([4], Res), 3 = ?A * (z - -1 / 4) + ?B * (z - 1 / 2)\<close>
walther@59635
   208
  eq = (Try (Rewrite_Set ''equival_trans'')) eq; 
wneuper@59536
   209
                                                \<comment> \<open>eq = 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)\<close>
wneuper@59504
   210
  z1 = rhs (NTH 1 L_L);                                                           \<comment> \<open>z2 = -1 / 4\<close>
wneuper@59536
   211
  z2 = rhs (NTH 2 L_L);                  \<comment> \<open>([5], Frm), 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)\<close>
wneuper@59536
   212
  eq_a = Take eq;                \<comment> \<open>([5], Res), 3 = AA * (1 / 2 - -1 / 4) + BB * (1 / 2 - 1 / 2)\<close>
wneuper@59536
   213
  eq_a = Substitute [zzz = z1] eq;                                 \<comment> \<open>([6], Res), 3 = 3 * AA / 4\<close>
walther@59635
   214
  eq_a = (Rewrite_Set ''norm_Rational'') eq_a;
wneuper@59536
   215
\<comment> \<open>-----                                                  ([7], Pbl), solve (3 = 3 * AA / 4, AA)\<close>
wneuper@59536
   216
                                                                         \<comment> \<open>([7], Res), [AA = 4]\<close>
wneuper@59592
   217
  sol_a = SubProblem (''Isac_Knowledge'', [''univariate'',''equation''], [''no_met''])
wneuper@59536
   218
      [BOOL eq_a, REAL (AA::real)] ;                                                    \<comment> \<open>a = 4\<close>
wneuper@59536
   219
  a = rhs (NTH 1 sol_a);                 \<comment> \<open>([8], Frm), 3 = AA * (z - -1 / 4) + BB * (z - 1 / 2)\<close>
wneuper@59536
   220
  eq_b = Take eq;              \<comment> \<open>([8], Res), 3 = AA * (-1 / 4 - -1 / 4) + BB * (-1 / 4 - 1 / 2)\<close>
wneuper@59536
   221
  eq_b = Substitute [zzz = z2] eq_b;                              \<comment> \<open>([9], Res), 3 = -3 * BB / 4\<close>
walther@59635
   222
  eq_b = (Rewrite_Set ''norm_Rational'') eq_b;       \<comment> \<open>([10], Pbl), solve (3 = -3 * BB / 4, BB)\<close>
walther@59635
   223
  sol_b = SubProblem (''Isac_Knowledge'',                              \<comment> \<open>([10], Res), [BB = -4]\<close>
wneuper@59504
   224
      [''univariate'',''equation''], [''no_met''])
wneuper@59536
   225
    [BOOL eq_b, REAL (BB::real)];                                                      \<comment> \<open>b = -4\<close>
wneuper@59536
   226
  b = rhs (NTH 1 sol_b);                           \<comment> \<open>eqr = AA / (z - 1 / 2) + BB / (z - -1 / 4)\<close>
wneuper@59504
   227
  pbz = Take eqr;                            \<comment> \<open>([11], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)\<close>
wneuper@59512
   228
  pbz = Substitute [AA = a, BB = b] pbz        \<comment> \<open>([], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)\<close>
wneuper@59504
   229
in pbz)                                                                                "
wneuper@59472
   230
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   231
    [Specify.prep_met @{theory} "met_partial_fraction" [] Celem.e_metID
s1210629013@55373
   232
      (["simplification","of_rationals","to_partial_fraction"], 
s1210629013@55373
   233
        [("#Given" ,["functionTerm t_t", "solveFor v_v"]),
s1210629013@55373
   234
          (*("#Where" ,["((get_numerator t_t) has_degree_in v_v) < 
s1210629013@55373
   235
            ((get_denominator t_t) has_degree_in v_v)"]), TODO*)
s1210629013@55373
   236
          ("#Find"  ,["decomposedFunction p_p'''"])],
s1210629013@55373
   237
        (*f_f = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)), zzz: z*)
wneuper@59416
   238
        {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = srls_partial_fraction, prls = Rule.e_rls,
wneuper@59416
   239
          crls = Rule.e_rls, errpats = [], nrls = Rule.e_rls},
s1210629013@55373
   240
        (*([], Frm), Problem (Partial_Fractions, [partial_fraction, rational, simplification])*)
wneuper@59551
   241
        @{thm partial_fraction.simps})]
wneuper@59472
   242
\<close>
wneuper@59472
   243
ML \<open>
neuper@42376
   244
(*
neuper@42376
   245
  val fmz =                                             
neuper@42376
   246
    ["functionTerm (3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z))))", 
neuper@42376
   247
      "solveFor z", "functionTerm p_p"];
neuper@42376
   248
  val (dI',pI',mI') =
neuper@42376
   249
    ("Partial_Fractions", 
neuper@42376
   250
      ["partial_fraction", "rational", "simplification"],
neuper@42376
   251
      ["simplification","of_rationals","to_partial_fraction"]);
neuper@42376
   252
  val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
neuper@42376
   253
*)
wneuper@59472
   254
\<close>
neuper@42289
   255
neuper@42289
   256
end