src/Tools/isac/Knowledge/Inverse_Z_Transform.thy
author wenzelm
Sat, 12 Jun 2021 18:22:07 +0200
changeset 60298 09106b85d3b4
parent 60294 6623f5cdcb19
child 60303 815b0dc8b589
permissions -rw-r--r--
use more antiquotations;
     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> \<up> n * u [n]" and
    13   rule5: "|| z || < || \<alpha> || ==> z / (z - \<alpha>) = -(\<alpha> \<up> n) * u [-n - 1]" and
    14   rule6: "|| z || > 1 ==> z/(z - 1) \<up> 2 = n * u [n]" (*and
    15   rule42: "(a * (z/(z-b)) + c * (z/(z-d))) = (a * b \<up> n * u [n] + c * d \<up> 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_Def.Repeat {id = "inverse_z", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
    31 	  erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
    32 	  rules = 
    33 	   [
    34     \<^rule_thm>\<open>rule4\<close>
    35 	   ], 
    36 	 scr = Rule.Empty_Prog});
    37 \<close>
    38 
    39 
    40 text \<open>store the rule set for math engine\<close>
    41 
    42 rule_set_knowledge inverse_z = inverse_z
    43 
    44 subsection\<open>Define the Specification\<close>
    45 
    46 setup \<open>KEStore_Elems.add_pbts
    47   [(Problem.prep_input @{theory} "pbl_SP" [] Problem.id_empty (["SignalProcessing"], [], Rule_Set.empty, NONE, [])),
    48     (Problem.prep_input @{theory} "pbl_SP_Ztrans" [] Problem.id_empty
    49       (["Z_Transform", "SignalProcessing"], [], Rule_Set.empty, NONE, [])),
    50     (Problem.prep_input @{theory} "pbl_SP_Ztrans_inv" [] Problem.id_empty
    51       (["Inverse", "Z_Transform", "SignalProcessing"],
    52         [("#Given" , ["filterExpression X_eq"]),
    53           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
    54         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], NONE, 
    55         [["SignalProcessing", "Z_Transform", "Inverse"]]))]\<close>
    56 
    57 subsection \<open>Setup Parent Nodes in Hierarchy of MethodC\<close>
    58 
    59 setup \<open>KEStore_Elems.add_mets
    60     [MethodC.prep_input @{theory} "met_SP" [] MethodC.id_empty
    61       (["SignalProcessing"], [],
    62         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
    63           errpats = [], nrls = Rule_Set.empty}, @{thm refl}),
    64     MethodC.prep_input @{theory} "met_SP_Ztrans" [] MethodC.id_empty
    65       (["SignalProcessing", "Z_Transform"], [],
    66         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
    67           errpats = [], nrls = Rule_Set.empty}, @{thm refl})]
    68 \<close>
    69 
    70 partial_function (tailrec) inverse_ztransform :: "bool \<Rightarrow> real \<Rightarrow> bool"
    71   where
    72 "inverse_ztransform X_eq X_z = (
    73   let
    74     X = Take X_eq;
    75     X' = Rewrite ''ruleZY''  X;                                              \<comment> \<open>z * denominator\<close>
    76     X' = (Rewrite_Set ''norm_Rational'' ) X';                                       \<comment> \<open>simplify\<close>
    77     funterm = Take (rhs X');                                \<comment> \<open>drop X' z = for equation solving\<close>
    78     denom = (Rewrite_Set ''partial_fraction'' ) funterm;                     \<comment> \<open>get_denominator\<close>
    79     equ = (denom = (0::real));
    80     fun_arg = Take (lhs X');
    81     arg = (Rewrite_Set ''partial_fraction'' ) X';                          \<comment> \<open>get_argument TODO\<close>
    82     (L_L::bool list) = \<comment> \<open>'bool list' inhibits:
    83                 WARNING: Additional type variable(s) in specification of inverse_ztransform: 'a\<close>
    84       SubProblem (''Test'', [''LINEAR'',''univariate'',''equation'',''test''], [''Test'',''solve_linear''])
    85         [BOOL equ, REAL X_z]
    86   in X) "
    87 setup \<open>KEStore_Elems.add_mets
    88     [MethodC.prep_input @{theory} "met_SP_Ztrans_inv" [] MethodC.id_empty
    89       (["SignalProcessing", "Z_Transform", "Inverse"],
    90         [("#Given" ,["filterExpression X_eq", "functionName X_z"]),
    91           ("#Find"  ,["stepResponse n_eq"])], \<comment> \<open>TODO: unused, "u [n]" is introduced by rule1..6\<close>
    92         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
    93           errpats = [], nrls = Rule_Set.empty},
    94         @{thm inverse_ztransform.simps})]
    95 \<close>
    96 
    97 partial_function (tailrec) inverse_ztransform2 :: "bool \<Rightarrow> real \<Rightarrow> bool"
    98   where
    99 "inverse_ztransform2 X_eq X_z = (
   100   let
   101     X = Take X_eq;
   102     X' = Rewrite ''ruleZY''  X;
   103     X_z = lhs X';
   104     zzz = argument_in X_z;
   105     funterm = rhs X';
   106     pbz = SubProblem (''Isac_Knowledge'',
   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''  pbz_eq;
   112     X_zeq = Take (X_z = rhs pbz_eq);
   113     n_eq = (Rewrite_Set ''inverse_z'' ) X_zeq
   114   in n_eq)"
   115 setup \<open>KEStore_Elems.add_mets
   116     [MethodC.prep_input @{theory} "met_SP_Ztrans_inv_sub" [] MethodC.id_empty
   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_Set.empty, calc = [],
   121           srls = Rule_Def.Repeat {id="srls_partial_fraction", 
   122               preconds = [], rew_ord = ("termlessI",termlessI),
   123               erls = Rule_Set.append_rules "erls_in_srls_partial_fraction" Rule_Set.empty
   124                   [(*for asm in NTH_CONS ...*)
   125                     \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
   126                     (*2nd NTH_CONS pushes n+-1 into asms*)
   127                     \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")], 
   128               srls = Rule_Set.Empty, calc = [], errpatts = [],
   129               rules = [\<^rule_thm>\<open>NTH_CONS\<close>,
   130                   \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_"),
   131                   \<^rule_thm>\<open>NTH_NIL\<close>,
   132                   \<^rule_eval>\<open>Prog_Expr.lhs\<close> (Prog_Expr.eval_lhs "eval_lhs_"),
   133                   \<^rule_eval>\<open>Prog_Expr.rhs\<close> (Prog_Expr.eval_rhs"eval_rhs_"),
   134                   \<^rule_eval>\<open>Prog_Expr.argument_in\<close> (Prog_Expr.eval_argument_in "Prog_Expr.argument_in"),
   135                   \<^rule_eval>\<open>get_denominator\<close> (eval_get_denominator "#get_denominator"),
   136                   \<^rule_eval>\<open>get_numerator\<close> (eval_get_numerator "#get_numerator"),
   137                   \<^rule_eval>\<open>factors_from_solution\<close> (eval_factors_from_solution "#factors_from_solution")
   138                   ], scr = Rule.Empty_Prog},
   139           prls = Rule_Set.empty, crls = Rule_Set.empty, errpats = [], nrls = norm_Rational},
   140         @{thm inverse_ztransform2.simps})]
   141 \<close>
   142 ML \<open>
   143 \<close> ML \<open>
   144 \<close> ML \<open>
   145 \<close>
   146 
   147 end
   148