src/Tools/isac/Knowledge/Inverse_Z_Transform.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Sat, 22 Jun 2019 14:34:06 +0200
changeset 59551 6ea6d9c377a0
parent 59550 2e7631381921
child 59592 99c8d2ff63eb
permissions -rw-r--r--
funpack: remove code unnecessary after switch to partial_function, partially
     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>Setup Parent Nodes in Hierarchy of Method\<close>
    60 ML \<open>val thy = @{theory}; (*latest version of thy required*)\<close>
    61 setup \<open>KEStore_Elems.add_mets
    62     [Specify.prep_met thy "met_SP" [] Celem.e_metID
    63       (["SignalProcessing"], [],
    64         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    65           errpats = [], nrls = Rule.e_rls}, @{thm refl}),
    66     Specify.prep_met thy "met_SP_Ztrans" [] Celem.e_metID
    67       (["SignalProcessing", "Z_Transform"], [],
    68         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    69           errpats = [], nrls = Rule.e_rls}, @{thm refl})]
    70 \<close>
    71 
    72 partial_function (tailrec) inverse_ztransform :: "bool \<Rightarrow> real \<Rightarrow> bool"
    73   where
    74 "inverse_ztransform X_eq X_z =                                       \<comment> \<open>(1/z) instead of z ^^^ -1\<close>
    75  (let X = Take X_eq;
    76       X' = Rewrite ''ruleZY'' False X;                                         \<comment> \<open>z * denominator\<close>
    77       X' = (Rewrite_Set ''norm_Rational'' False) X';                                  \<comment> \<open>simplify\<close>
    78       funterm = Take (rhs X');                                \<comment> \<open>drop X' z = for equation solving\<close>
    79       denom = (Rewrite_Set ''partial_fraction'' False) funterm;                \<comment> \<open>get_denominator\<close>
    80       equ = (denom = (0::real));
    81       fun_arg = Take (lhs X');
    82       arg = (Rewrite_Set ''partial_fraction'' False) X';                     \<comment> \<open>get_argument TODO\<close>
    83       (L_L::bool list) = \<comment> \<open>'bool list' inhibits (?!?):
    84                   WARNING: Additional type variable(s) in specification of inverse_ztransform: 'a\<close>
    85         SubProblem (''Test'', [''LINEAR'',''univariate'',''equation'',''test''],
    86           [''Test'',''solve_linear'']) [BOOL equ, REAL X_z]
    87   in X) "
    88 setup \<open>KEStore_Elems.add_mets
    89     [Specify.prep_met thy "met_SP_Ztrans_inv" [] Celem.e_metID
    90       (["SignalProcessing", "Z_Transform", "Inverse"],
    91         [("#Given" ,["filterExpression X_eq", "functionName X_z"]),
    92           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
    93         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    94           errpats = [], nrls = Rule.e_rls},
    95         @{thm inverse_ztransform.simps})]
    96 \<close>
    97 
    98 partial_function (tailrec) inverse_ztransform2 :: "bool \<Rightarrow> real \<Rightarrow> bool"
    99   where
   100 "inverse_ztransform2 X_eq X_z =
   101   (let X = Take X_eq;
   102     X' = Rewrite ''ruleZY'' False X;
   103     X'_z = lhs X';
   104     zzz = argument_in X'_z;
   105     funterm = rhs X';
   106     pbz = SubProblem (''Isac'',
   107       [''partial_fraction'',''rational'',''simplification''],
   108       [''simplification'',''of_rationals'',''to_partial_fraction''])
   109       [REAL funterm, REAL zzz];
   110     pbz_eq = Take (X'_z = pbz);
   111     pbz_eq = Rewrite ''ruleYZ'' False pbz_eq;
   112     X_zeq = Take (X_z = rhs pbz_eq);
   113     n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq
   114   in n_eq)"
   115 setup \<open>KEStore_Elems.add_mets
   116     [Specify.prep_met thy "met_SP_Ztrans_inv_sub" [] Celem.e_metID
   117       (["SignalProcessing", "Z_Transform", "Inverse_sub"],
   118         [("#Given" ,["filterExpression X_eq", "functionName X_z"]),
   119           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
   120         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [],
   121           srls = Rule.Rls {id="srls_partial_fraction", 
   122               preconds = [], rew_ord = ("termlessI",termlessI),
   123               erls = Rule.append_rls "erls_in_srls_partial_fraction" Rule.e_rls
   124                   [(*for asm in NTH_CONS ...*)
   125                     Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
   126                     (*2nd NTH_CONS pushes n+-1 into asms*)
   127                     Rule.Calc("Groups.plus_class.plus", eval_binop "#add_")], 
   128               srls = Rule.Erls, calc = [], errpatts = [],
   129               rules = [Rule.Thm ("NTH_CONS", @{thm NTH_CONS}),
   130                   Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"),
   131                   Rule.Thm ("NTH_NIL", @{thm NTH_NIL}),
   132                   Rule.Calc ("Tools.lhs", Tools.eval_lhs "eval_lhs_"),
   133                   Rule.Calc ("Tools.rhs", Tools.eval_rhs"eval_rhs_"),
   134                   Rule.Calc ("Atools.argument'_in", eval_argument_in "Atools.argument'_in"),
   135                   Rule.Calc ("Rational.get_denominator", eval_get_denominator "#get_denominator"),
   136                   Rule.Calc ("Rational.get_numerator", eval_get_numerator "#get_numerator"),
   137                   Rule.Calc ("Partial_Fractions.factors_from_solution",
   138                     eval_factors_from_solution "#factors_from_solution")
   139                   ], scr = Rule.EmptyScr},
   140           prls = Rule.e_rls, crls = Rule.e_rls, errpats = [], nrls = norm_Rational},
   141         @{thm inverse_ztransform2.simps})]
   142 \<close>
   143 ML \<open>
   144 \<close> ML \<open>
   145 \<close>
   146 
   147 end
   148