src/Tools/isac/Knowledge/Inverse_Z_Transform.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Sat, 22 Jun 2019 13:15:52 +0200
changeset 59550 2e7631381921
parent 59545 4035ec339062
child 59551 6ea6d9c377a0
permissions -rw-r--r--
funpack: repair remaining test/../partial_fractions.sml, inverse_z_transform.sml
     1 (* Title:  Inverse_Z_Transform
     2    Author: Jan Rocnik
     3    (c) copyright due to lincense terms.
     4 *)
     5 
     6 theory Inverse_Z_Transform imports PolyEq DiffApp Partial_Fractions begin
     7 
     8 axiomatization where       \<comment> \<open>TODO: new variables on the rhs enforce replacement by substitution\<close>
     9   rule1: "1 = \<delta>[n]" and
    10   rule2: "|| z || > 1 ==> z / (z - 1) = u [n]" and
    11   rule3: "|| z || < 1 ==> z / (z - 1) = -u [-n - 1]" and 
    12   rule4: "c * (z / (z - \<alpha>)) = c * \<alpha>^^^n * u [n]" and
    13   rule5: "|| z || < || \<alpha> || ==> z / (z - \<alpha>) = -(\<alpha>^^^n) * u [-n - 1]" and
    14   rule6: "|| z || > 1 ==> z/(z - 1)^^^2 = n * u [n]" (*and
    15   rule42: "(a * (z/(z-b)) + c * (z/(z-d))) = (a * b^^^n * u [n] + c * d^^^n * u [n])"*)
    16 
    17 axiomatization where
    18 (*ruleZY: "(X z = a / b) = (d_d z X = a / (z * b))"         ..looks better, but types are flawed*)
    19   ruleZY: "(X z = a / b) = (X' z = a / (z * b))" and
    20   ruleYZ: "a / (z - b) + c / (z - d) = a * (z / (z - b)) + c * (z / (z - d))" and
    21   ruleYZa: "(a / b + c / d) = (a * (z / b) + c * (z / d))"        \<comment> \<open>that is what students learn\<close>
    22 
    23 subsection\<open>Define the Field Descriptions for the specification\<close>
    24 consts
    25   filterExpression  :: "bool => una"
    26   stepResponse      :: "bool => una"    \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6 above\<close>
    27 
    28 ML \<open>
    29 val inverse_z = prep_rls'(
    30   Rule.Rls {id = "inverse_z", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
    31 	  erls = Rule.Erls, srls = Rule.Erls, calc = [], errpatts = [],
    32 	  rules = 
    33 	   [
    34     Rule.Thm ("rule4", @{thm rule4})
    35 	   ], 
    36 	 scr = Rule.EmptyScr});
    37 \<close>
    38 
    39 
    40 text \<open>store the rule set for math engine\<close>
    41 
    42 setup \<open>KEStore_Elems.add_rlss [("inverse_z", (Context.theory_name @{theory}, inverse_z))]\<close>
    43 
    44 subsection\<open>Define the Specification\<close>
    45 ML \<open>
    46 val thy = @{theory};
    47 \<close>
    48 setup \<open>KEStore_Elems.add_pbts
    49   [(Specify.prep_pbt thy "pbl_SP" [] Celem.e_pblID (["SignalProcessing"], [], Rule.e_rls, NONE, [])),
    50     (Specify.prep_pbt thy "pbl_SP_Ztrans" [] Celem.e_pblID
    51       (["Z_Transform","SignalProcessing"], [], Rule.e_rls, NONE, [])),
    52     (Specify.prep_pbt thy "pbl_SP_Ztrans_inv" [] Celem.e_pblID
    53       (["Inverse", "Z_Transform", "SignalProcessing"],
    54         [("#Given" , ["filterExpression X_eq"]),
    55           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
    56         Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_*)], NONE, 
    57         [["SignalProcessing","Z_Transform","Inverse"]]))]\<close>
    58 
    59 subsection \<open>Define Name and Signature for the Method\<close>
    60 consts
    61   InverseZTransform1 :: "[bool, bool] => bool"
    62     ("((Script InverseZTransform1 (_ =))// (_))" 9)
    63   InverseZTransform2 :: "[bool, real, bool] => bool"
    64     ("((Script InverseZTransform2 (_ _ =))// (_))" 9)
    65 
    66 subsection \<open>Setup Parent Nodes in Hierarchy of Method\<close>
    67 ML \<open>val thy = @{theory}; (*latest version of thy required*)\<close>
    68 setup \<open>KEStore_Elems.add_mets
    69     [Specify.prep_met thy "met_SP" [] Celem.e_metID
    70       (["SignalProcessing"], [],
    71         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    72           errpats = [], nrls = Rule.e_rls}, @{thm refl}),
    73     Specify.prep_met thy "met_SP_Ztrans" [] Celem.e_metID
    74       (["SignalProcessing", "Z_Transform"], [],
    75         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    76           errpats = [], nrls = Rule.e_rls}, @{thm refl})]
    77 \<close>
    78 
    79 partial_function (tailrec) inverse_ztransform :: "bool \<Rightarrow> real \<Rightarrow> bool"
    80   where
    81 "inverse_ztransform X_eq X_z =                                       \<comment> \<open>(1/z) instead of z ^^^ -1\<close>
    82  (let X = Take X_eq;
    83       X' = Rewrite ''ruleZY'' False X;                                         \<comment> \<open>z * denominator\<close>
    84       X' = (Rewrite_Set ''norm_Rational'' False) X';                                  \<comment> \<open>simplify\<close>
    85       funterm = Take (rhs X');                                \<comment> \<open>drop X' z = for equation solving\<close>
    86       denom = (Rewrite_Set ''partial_fraction'' False) funterm;                \<comment> \<open>get_denominator\<close>
    87       equ = (denom = (0::real));
    88       fun_arg = Take (lhs X');
    89       arg = (Rewrite_Set ''partial_fraction'' False) X';                     \<comment> \<open>get_argument TODO\<close>
    90       (L_L::bool list) = \<comment> \<open>'bool list' inhibits (?!?):
    91                   WARNING: Additional type variable(s) in specification of inverse_ztransform: 'a\<close>
    92         SubProblem (''Test'', [''LINEAR'',''univariate'',''equation'',''test''],
    93           [''Test'',''solve_linear'']) [BOOL equ, REAL X_z]
    94   in X) "
    95 setup \<open>KEStore_Elems.add_mets
    96     [Specify.prep_met thy "met_SP_Ztrans_inv" [] Celem.e_metID
    97       (["SignalProcessing", "Z_Transform", "Inverse"],
    98         [("#Given" ,["filterExpression X_eq", "functionName X_z"]),
    99           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
   100         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
   101           errpats = [], nrls = Rule.e_rls},
   102         @{thm inverse_ztransform.simps}
   103 	    (*"Script InverseZTransform1 (X_eq::bool) =" ^ (*(1/z) instead of z ^^^ -1*)
   104           " (let X = Take X_eq;" ^
   105           "      X' = Rewrite ''ruleZY'' False X;" ^ (*z * denominator*)
   106           "      X' = (Rewrite_Set ''norm_Rational'' False) X';" ^ (*simplify*)
   107           "      funterm = Take (rhs X');" ^ (*drop X' z = for equation solving*)
   108           "      denom = (Rewrite_Set ''partial_fraction'' False) funterm;" ^ (*get_denominator*)
   109           "      equ = (denom = (0::real));" ^
   110           "      fun_arg = Take (lhs X');" ^
   111           "      arg = (Rewrite_Set ''partial_fraction'' False) X';" ^ (*get_argument TODO*)
   112           "      (L_L::bool list) =                                    " ^
   113           "            (SubProblem (''Test'',                            " ^
   114           "                         [''LINEAR'',''univariate'',''equation'',''test'']," ^
   115           "                         [''Test'',''solve_linear''])              " ^
   116           "                        [BOOL equ, REAL z])              " ^
   117           "  in X)"*))]
   118 \<close>
   119 
   120 partial_function (tailrec) inverse_ztransform2 :: "bool \<Rightarrow> real \<Rightarrow> bool"
   121   where
   122 "inverse_ztransform2 X_eq X_z =
   123   (let X = Take X_eq;
   124     X' = Rewrite ''ruleZY'' False X;
   125     X'_z = lhs X';
   126     zzz = argument_in X'_z;
   127     funterm = rhs X';
   128     pbz = SubProblem (''Isac'',
   129       [''partial_fraction'',''rational'',''simplification''],
   130       [''simplification'',''of_rationals'',''to_partial_fraction''])
   131       [REAL funterm, REAL zzz];
   132     pbz_eq = Take (X'_z = pbz);
   133     pbz_eq = Rewrite ''ruleYZ'' False pbz_eq;
   134     X_zeq = Take (X_z = rhs pbz_eq);
   135     n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq
   136   in n_eq)"
   137 setup \<open>KEStore_Elems.add_mets
   138     [Specify.prep_met thy "met_SP_Ztrans_inv_sub" [] Celem.e_metID
   139       (["SignalProcessing", "Z_Transform", "Inverse_sub"],
   140         [("#Given" ,["filterExpression X_eq", "functionName X_z"]),
   141           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
   142         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [],
   143           srls = Rule.Rls {id="srls_partial_fraction", 
   144               preconds = [], rew_ord = ("termlessI",termlessI),
   145               erls = Rule.append_rls "erls_in_srls_partial_fraction" Rule.e_rls
   146                   [(*for asm in NTH_CONS ...*)
   147                     Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
   148                     (*2nd NTH_CONS pushes n+-1 into asms*)
   149                     Rule.Calc("Groups.plus_class.plus", eval_binop "#add_")], 
   150               srls = Rule.Erls, calc = [], errpatts = [],
   151               rules = [Rule.Thm ("NTH_CONS", @{thm NTH_CONS}),
   152                   Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"),
   153                   Rule.Thm ("NTH_NIL", @{thm NTH_NIL}),
   154                   Rule.Calc ("Tools.lhs", Tools.eval_lhs "eval_lhs_"),
   155                   Rule.Calc ("Tools.rhs", Tools.eval_rhs"eval_rhs_"),
   156                   Rule.Calc ("Atools.argument'_in", eval_argument_in "Atools.argument'_in"),
   157                   Rule.Calc ("Rational.get_denominator", eval_get_denominator "#get_denominator"),
   158                   Rule.Calc ("Rational.get_numerator", eval_get_numerator "#get_numerator"),
   159                   Rule.Calc ("Partial_Fractions.factors_from_solution",
   160                     eval_factors_from_solution "#factors_from_solution")
   161                   ], scr = Rule.EmptyScr},
   162           prls = Rule.e_rls, crls = Rule.e_rls, errpats = [], nrls = norm_Rational},
   163         @{thm inverse_ztransform2.simps}
   164 	    (*" Script InverseZTransform2 (X_eq::bool) (X_z::real) =               "^ (*([], Frm), Problem (Isac, [Inverse, Z_Transform, SignalProcessing])*)
   165         " (let X = Take X_eq;                                                "^ (*([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))*)
   166         "   X' = Rewrite ''ruleZY'' False X;                                 "^ (*([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   167         "   (X'_z::real) = lhs X';                                           "^ (*            ?X' z*)
   168         "   (zzz::real) = argument_in X'_z;                                  "^ (*            z *)
   169         "   (funterm::real) = rhs X';                                        "^ (*            3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   170         "   (pbz::real) = (SubProblem (''Isac'',                             "^ (*([2], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   171         "     [''partial_fraction'',''rational'',''simplification''],        "^
   172         "     [''simplification'',''of_rationals'',''to_partial_fraction'']) "^
   173         "     [REAL funterm, REAL zzz]);                                     "^
   174         "   (pbz_eq::bool) = Take (X'_z = pbz);                              "^ (*([3], Frm), ?X' z = 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   175         "   pbz_eq = Rewrite ''ruleYZ'' False pbz_eq;                        "^ (*([3], Res), ?X' z = 4 * (?z / (z - 1 / 2)) + -4 * (?z / (z - -1 / 4))*)
   176         "   (X_zeq::bool) = Take (X_z = rhs pbz_eq);                         "^ (*([4], Frm), X_z = 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
   177         "   n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq                   "^ (*([4], Res), X_z = 4 * (1 / 2) ^^^ ?n * ?u [?n] + -4 * (-1 / 4) ^^^ ?n * ?u [?n]*)
   178         " in n_eq)                                                           "*))](*            X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
   179 \<close>
   180 ML \<open>
   181 \<close> ML \<open>
   182 \<close>
   183 
   184 end
   185