src/Tools/isac/Knowledge/Inverse_Z_Transform.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Tue, 09 Apr 2019 11:38:26 +0200
changeset 59537 ce64b1de1174
parent 59513 deb1efba3119
child 59538 c8a2648e20ae
permissions -rw-r--r--
improve handling of new variable on rhs; open problem with Inverse_Z_Transform
     1 (* Title:  Test_Z_Transform
     2    Author: Jan Rocnik
     3    (c) copyright due to lincense terms.
     4 12345678901234567890123456789012345678901234567890123456789012345678901234567890
     5         10        20        30        40        50        60        70        80
     6 *)
     7 
     8 theory Inverse_Z_Transform imports PolyEq DiffApp Partial_Fractions begin
     9 
    10 axiomatization where 
    11   rule1: "1 = \<delta>[n]" and
    12   rule2: "|| z || > 1 ==> z / (z - 1) = u [n]" and
    13   rule3: "|| z || < 1 ==> z / (z - 1) = -u [-n - 1]" and 
    14   rule4: "c * (z / (z - \<alpha>)) = c * \<alpha>^^^n * u [n]" and
    15   rule5: "|| z || < || \<alpha> || ==> z / (z - \<alpha>) = -(\<alpha>^^^n) * u [-n - 1]" and
    16   rule6: "|| z || > 1 ==> z/(z - 1)^^^2 = n * u [n]" (*and
    17   rule42: "(a * (z/(z-b)) + c * (z/(z-d))) = (a * b^^^n * u [n] + c * d^^^n * u [n])"*)
    18 
    19 axiomatization where
    20 (*ruleZY: "(X z = a / b) = (d_d z X = a / (z * b))"         ..looks better, but types are flawed*)
    21   ruleZY: "(X z = a / b) = (X' z = a / (z * b))" and
    22   ruleYZ: "a / (z - b) + c / (z - d) = a * (z / (z - b)) + c * (z / (z - d))" and
    23   ruleYZa: "(a / b + c / d) = (a * (z / b) + c * (z / d))"        \<comment> \<open>that is what students learn\<close>
    24 
    25 subsection\<open>Define the Field Descriptions for the specification\<close>
    26 consts
    27   filterExpression  :: "bool => una"
    28   stepResponse      :: "bool => una"
    29 
    30 
    31 ML \<open>
    32 val inverse_z = prep_rls'(
    33   Rule.Rls {id = "inverse_z", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
    34 	  erls = Rule.Erls, srls = Rule.Erls, calc = [], errpatts = [],
    35 	  rules = 
    36 	   [
    37     Rule.Thm ("rule4", @{thm rule4})
    38 	   ], 
    39 	 scr = Rule.EmptyScr});
    40 \<close>
    41 
    42 
    43 text \<open>store the rule set for math engine\<close>
    44 
    45 setup \<open>KEStore_Elems.add_rlss [("inverse_z", (Context.theory_name @{theory}, inverse_z))]\<close>
    46 
    47 subsection\<open>Define the Specification\<close>
    48 ML \<open>
    49 val thy = @{theory};
    50 \<close>
    51 setup \<open>KEStore_Elems.add_pbts
    52   [(Specify.prep_pbt thy "pbl_SP" [] Celem.e_pblID (["SignalProcessing"], [], Rule.e_rls, NONE, [])),
    53     (Specify.prep_pbt thy "pbl_SP_Ztrans" [] Celem.e_pblID
    54       (["Z_Transform","SignalProcessing"], [], Rule.e_rls, NONE, [])),
    55     (Specify.prep_pbt thy "pbl_SP_Ztrans_inv" [] Celem.e_pblID
    56       (["Inverse", "Z_Transform", "SignalProcessing"],
    57         (*^ capital letter breaks coding standard
    58           because "inverse" = Const ("Rings.inverse_class.inverse", ..*)
    59         [("#Given" ,["filterExpression (X_eq::bool)"]),
    60           ("#Find"  ,["stepResponse (n_eq::bool)"])],
    61         Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_*)], NONE, 
    62         [["SignalProcessing","Z_Transform","Inverse"]])),
    63     (Specify.prep_pbt thy "pbl_SP_Ztrans_inv" [] Celem.e_pblID
    64       (["Inverse", "Z_Transform", "SignalProcessing"],
    65         [("#Given" ,["filterExpression X_eq"]),
    66           ("#Find"  ,["stepResponse n_eq"])],
    67         Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_*)], NONE, 
    68         [["SignalProcessing","Z_Transform","Inverse"]]))]\<close>
    69 
    70 subsection \<open>Define Name and Signature for the Method\<close>
    71 consts
    72   InverseZTransform :: "[bool, bool] => bool"
    73     ("((Script InverseZTransform (_ =))// (_))" 9)
    74 
    75 subsection \<open>Setup Parent Nodes in Hierarchy of Method\<close>
    76 ML \<open>val thy = @{theory}; (*latest version of thy required*)\<close>
    77 setup \<open>KEStore_Elems.add_mets
    78     [Specify.prep_met thy "met_SP" [] Celem.e_metID
    79       (["SignalProcessing"], [],
    80         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    81           errpats = [], nrls = Rule.e_rls}, "empty_script"),
    82     Specify.prep_met thy "met_SP_Ztrans" [] Celem.e_metID
    83       (["SignalProcessing", "Z_Transform"], [],
    84         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
    85           errpats = [], nrls = Rule.e_rls}, "empty_script")]
    86 \<close>
    87 (*ok
    88 partial_function (tailrec) inverse_ztransform :: "bool \<Rightarrow> bool"
    89   where
    90 "inverse_ztransform X_eq =                                           \<comment> \<open>(1/z) instead of z ^^^ -1\<close>                
    91  (let X = Take X_eq;                                                                
    92       X' = Rewrite ''ruleZY'' False X;                                         \<comment> \<open>z * denominator\<close>                          
    93       X' = (Rewrite_Set ''norm_Rational'' False) X';                                  \<comment> \<open>simplify\<close>                   
    94       funterm = Take (rhs X');                                \<comment> \<open>drop X' z = for equation solving\<close>                 
    95       denom = (Rewrite_Set ''partial_fraction'' False) funterm;                \<comment> \<open>get_denominator\<close> 
    96       equ = (denom = (0::real));                                                    
    97       fun_arg = Take (lhs X');                                                      
    98       arg = (Rewrite_Set ''partial_fraction'' False) X';                     \<comment> \<open>get_argument TODO\<close>      
    99       L_L = SubProblem (''Test'', [''LINEAR'',''univariate'',''equation'',''test''],         
   100                 [''Test'',''solve_linear'']) [BOOL equ, REAL z]              \<comment> \<open>PROG --> as arg\<close>
   101   in X) "
   102 *)
   103 setup \<open>KEStore_Elems.add_mets
   104     [Specify.prep_met thy "met_SP_Ztrans_inv" [] Celem.e_metID
   105       (["SignalProcessing", "Z_Transform", "Inverse"], 
   106         [("#Given" ,["filterExpression (X_eq::bool)"]),
   107           ("#Find"  ,["stepResponse (n_eq::bool)"])],
   108         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
   109           errpats = [], nrls = Rule.e_rls},
   110         "Script InverseZTransform (X_eq::bool) =" ^ (*(1/z) instead of z ^^^ -1*)
   111           " (let X = Take X_eq;" ^
   112           "      X' = Rewrite ''ruleZY'' False X;" ^ (*z * denominator*)
   113           "      X' = (Rewrite_Set ''norm_Rational'' False) X';" ^ (*simplify*)
   114           "      funterm = Take (rhs X');" ^ (*drop X' z = for equation solving*)
   115           "      denom = (Rewrite_Set ''partial_fraction'' False) funterm;" ^ (*get_denominator*)
   116           "      equ = (denom = (0::real));" ^
   117           "      fun_arg = Take (lhs X');" ^
   118           "      arg = (Rewrite_Set ''partial_fraction'' False) X';" ^ (*get_argument TODO*)
   119           "      (L_L::bool list) =                                    " ^
   120           "            (SubProblem (''Test'',                            " ^
   121           "                         [''LINEAR'',''univariate'',''equation'',''test'']," ^
   122           "                         [''Test'',''solve_linear''])              " ^
   123           "                        [BOOL equ, REAL z])              " ^
   124           "  in X)")]
   125 \<close>
   126 (*
   127 Type unification failed: Clash of types "bool" and "_ itself"
   128 Type error in application: incompatible operand type
   129 Operator:  Let (Take X_eq) :: (??'a itself \<Rightarrow> ??'b) \<Rightarrow> ??'b
   130 Operand:
   131   \<lambda>X. let X' = Rewrite ''ruleZY'' ...
   132 :
   133 :partial_function (tailrec) inverse_ztransform2 :: "bool \<Rightarrow> bool"
   134   where
   135 "inverse_ztransform X_eq =                                          \<comment> \<open>(1/z) instead of z ^^^ -1\<close>
   136   (let X = Take X_eq;
   137       X' = Rewrite ''ruleZY'' False X;                                        \<comment> \<open>z * denominator\<close>
   138       (num_orig::real) = get_numerator (rhs X');                                  
   139       X' = (Rewrite_Set ''norm_Rational'' False) X';                                 \<comment> \<open>simplify\<close>
   140       (X'_z::real) = lhs X';                                                      
   141       (zzz::real) = argument_in X'_z;                                             
   142       (funterm::real) = rhs X';                              \<comment> \<open>drop X' z = for equation solving\<close>
   143       (denom::real) = get_denominator funterm;                                \<comment> \<open>get_denominator\<close>
   144       (num::real) = get_numerator funterm;                                      \<comment> \<open>get_numerator\<close>
   145       (equ::bool) = (denom = (0::real));                                          
   146       (L_L::bool list) = (SubProblem (''Partial_Fractions'',                                 
   147          [''abcFormula'',''degree_2'',''polynomial'',''univariate'',''equation''],
   148          [''no_met''])                                                            
   149          [BOOL equ, REAL zzz]);                                                   
   150       (facs::real) = factors_from_solution L_L;                                   
   151       (eql::real) = Take (num_orig / facs); \<comment> \<open>---\<close>
   152       (eqr::real) = (Try (Rewrite_Set ''ansatz_rls'' False)) eql; \<comment> \<open>---\<close>
   153       (eq::bool) = Take (eql = eqr);                    \<comment> \<open>Maybe possible to use HOLogic.mk_eq ??\<close>
   154       eq = (Try (Rewrite_Set ''equival_trans'' False)) eq; \<comment> \<open>---\<close>
   155       (z1::real) = (rhs (NTH 1 L_L)); \<comment> \<open>prepare equation for a - eq_a therefor substitute z with solution 1 - z1\<close>
   156       (z2::real) = (rhs (NTH 2 L_L)); \<comment> \<open>---\<close>
   157       (eq_a::bool) = Take eq;                                                     
   158       eq_a = (Substitute [zzz=z1]) eq;                                            
   159       eq_a = (Rewrite_Set ''norm_Rational'' False) eq_a;                          
   160       (sol_a::bool list) =                                                        
   161                  (SubProblem (''Isac'',                                           
   162                               [''univariate'',''equation''],[''no_met''])         
   163                               [BOOL eq_a, REAL (A::real)]);                       
   164       (a::real) = (rhs(NTH 1 sol_a)); \<comment> \<open>---\<close>
   165       (eq_b::bool) = Take eq;                                                     
   166       eq_b =  (Substitute [zzz=z2]) eq_b;                                         
   167       eq_b = (Rewrite_Set ''norm_Rational'' False) eq_b;                          
   168       (sol_b::bool list) =                                                        
   169                  (SubProblem (''Isac'',                                           
   170                               [''univariate'',''equation''],[''no_met''])         
   171                               [BOOL eq_b, REAL (B::real)]);                       
   172       (b::real) = (rhs(NTH 1 sol_b)); \<comment> \<open>---\<close>
   173       (pbz::real) = Take eqr;                                                     
   174       pbz = ((Substitute [A=a, B=b]) pbz); \<comment> \<open>---\<close>
   175       pbz = Rewrite ''ruleYZ'' False pbz;                                         
   176       (X_z::bool) = Take (X_z = pbz);                                             
   177       (n_eq::bool) = (Rewrite_Set ''inverse_z'' False) X_z
   178 in n_eq)"
   179 *)
   180 setup \<open>KEStore_Elems.add_mets
   181     [Specify.prep_met thy "met_SP_Ztrans_inv" [] Celem.e_metID
   182       (["SignalProcessing", "Z_Transform", "Inverse"], 
   183         [("#Given" ,["filterExpression X_eq"]),
   184           ("#Find"  ,["stepResponse n_eq"])],
   185         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = srls_partial_fraction, prls = Rule.e_rls,
   186           crls = Rule.e_rls, errpats = [], nrls = Rule.e_rls},
   187         "Script InverseZTransform (X_eq::bool) =                        "^
   188            (*(1/z) instead of z ^^^ -1*)
   189            "(let X = Take X_eq;                                            "^
   190            "      X' = Rewrite ''ruleZY'' False X;                             "^
   191            (*z * denominator*)
   192            "      (num_orig::real) = get_numerator (rhs X');               "^
   193            "      X' = (Rewrite_Set ''norm_Rational'' False) X';               "^
   194            (*simplify*)
   195            "      (X'_z::real) = lhs X';                                   "^
   196            "      (zzz::real) = argument_in X'_z;                          "^
   197            "      (funterm::real) = rhs X';                                "^
   198            (*drop X' z = for equation solving*)
   199            "      (denom::real) = get_denominator funterm;                 "^
   200            (*get_denominator*)
   201            "      (num::real) = get_numerator funterm;                     "^
   202            (*get_numerator*)
   203            "      (equ::bool) = (denom = (0::real));                       "^
   204            "      (L_L::bool list) = (SubProblem (''Partial_Fractions'',                 "^
   205            "         [''abcFormula'',''degree_2'',''polynomial'',''univariate'',''equation''], "^
   206            "         [''no_met''])                                             "^
   207            "         [BOOL equ, REAL zzz]);                                "^
   208            "      (facs::real) = factors_from_solution L_L;                "^
   209            "      (eql::real) = Take (num_orig / facs);                    "^ 
   210       
   211            "      (eqr::real) = (Try (Rewrite_Set ''ansatz_rls'' False)) eql;  "^
   212       
   213            "      (eq::bool) = Take (eql = eqr);                           "^
   214            (*Maybe possible to use HOLogic.mk_eq ??*)
   215            "      eq = (Try (Rewrite_Set ''equival_trans'' False)) eq;         "^ 
   216       
   217            "      (z1::real) = (rhs (NTH 1 L_L));                          "^
   218            (* 
   219             * prepare equation for a - eq_a
   220             * therefor substitute z with solution 1 - z1
   221             *)
   222            "      (z2::real) = (rhs (NTH 2 L_L));                          "^
   223        
   224            "      (eq_a::bool) = Take eq;                                  "^
   225            "      eq_a = (Substitute [zzz=z1]) eq;                         "^
   226            "      eq_a = (Rewrite_Set ''norm_Rational'' False) eq_a;           "^
   227            "      (sol_a::bool list) =                                     "^
   228            "                 (SubProblem (''Isac'',                           "^
   229            "                              [''univariate'',''equation''],[''no_met''])  "^
   230            "                              [BOOL eq_a, REAL (A::real)]);    "^
   231            "      (a::real) = (rhs(NTH 1 sol_a));                          "^
   232       
   233            "      (eq_b::bool) = Take eq;                                  "^
   234            "      eq_b =  (Substitute [zzz=z2]) eq_b;                      "^
   235            "      eq_b = (Rewrite_Set ''norm_Rational'' False) eq_b;           "^
   236            "      (sol_b::bool list) =                                     "^
   237            "                 (SubProblem (''Isac'',                           "^
   238            "                              [''univariate'',''equation''],[''no_met''])  "^
   239            "                              [BOOL eq_b, REAL (B::real)]);    "^
   240            "      (b::real) = (rhs(NTH 1 sol_b));                          "^
   241       
   242            "      (pbz::real) = Take eqr;                                  "^
   243            "      pbz = ((Substitute [A=a, B=b]) pbz);                     "^
   244       
   245            "      pbz = Rewrite ''ruleYZ'' False pbz;                          "^
   246       
   247            "      (X_z::bool) = Take (X_z = pbz);                          "^
   248            "      (n_eq::bool) = (Rewrite_Set ''inverse_z'' False) X_z     "^
   249            "in n_eq)")]
   250 \<close>
   251 (* same error as in         inverse_ztransform2
   252 :partial_function (tailrec) inverse_ztransform3 :: "bool \<Rightarrow> bool"
   253   where
   254 "inverse_ztransform X_eq =                                               
   255 (*([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))*)                            
   256 (let X = Take X_eq;                                                                 
   257 (*([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)                    
   258   X' = Rewrite ''ruleZY'' False X;                                                  
   259 (*            ?X' z*)                                                               
   260   (X'_z::real) = lhs X';                                                            
   261 (*            z *)                                                                  
   262   (zzz::real) = argument_in X'_z;                                                   
   263 (*            3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)                            
   264   (funterm::real) = rhs X';                                                         
   265 (*-----*)                                                                           
   266   (pbz::real) = (SubProblem (''Isac'',                                              
   267     [''partial_fraction'',''rational'',''simplification''],                         
   268     [''simplification'',''of_rationals'',''to_partial_fraction''])                  
   269 (*([2], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)                                 
   270     [REAL funterm, REAL zzz]);                                                      
   271 (*-----*)                                                                           
   272 (*([3], Frm), ?X' z = 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)                         
   273   (pbz_eq::bool) = Take (X'_z = pbz);                                               
   274 (*([3], Res), ?X' z = 4 * (?z / (z - 1 / 2)) + -4 * (?z / (z - -1 / 4))*)           
   275   pbz_eq = Rewrite ''ruleYZ'' False pbz_eq;                                         
   276 (*               4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)                  
   277 (*([4], Frm), X_z = 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)               
   278   (X_zeq::bool) = Take (X_z = rhs pbz_eq);                                          
   279 (*([4], Res), X_z = 4 * (1 / 2) ^^^ ?n * ?u [?n] + -4 * (-1 / 4) ^^^ ?n * ?u [?n]*)
   280   n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq                                  
   281 (*            X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
   282 in n_eq)                                                                            "
   283 *)
   284 setup \<open>KEStore_Elems.add_mets
   285     [Specify.prep_met thy "met_SP_Ztrans_inv_sub" [] Celem.e_metID
   286       (["SignalProcessing", "Z_Transform", "Inverse_sub"], 
   287         [("#Given" ,["filterExpression X_eq"]),
   288           ("#Find"  ,["stepResponse n_eq"])],
   289         {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [],
   290           srls = Rule.Rls {id="srls_partial_fraction", 
   291               preconds = [], rew_ord = ("termlessI",termlessI),
   292               erls = Rule.append_rls "erls_in_srls_partial_fraction" Rule.e_rls
   293                   [(*for asm in NTH_CONS ...*)
   294                     Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
   295                     (*2nd NTH_CONS pushes n+-1 into asms*)
   296                     Rule.Calc("Groups.plus_class.plus", eval_binop "#add_")], 
   297               srls = Rule.Erls, calc = [], errpatts = [],
   298               rules = [Rule.Thm ("NTH_CONS", @{thm NTH_CONS}),
   299                   Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"),
   300                   Rule.Thm ("NTH_NIL", @{thm NTH_NIL}),
   301                   Rule.Calc ("Tools.lhs", Tools.eval_lhs "eval_lhs_"),
   302                   Rule.Calc ("Tools.rhs", Tools.eval_rhs"eval_rhs_"),
   303                   Rule.Calc ("Atools.argument'_in", eval_argument_in "Atools.argument'_in"),
   304                   Rule.Calc ("Rational.get_denominator", eval_get_denominator "#get_denominator"),
   305                   Rule.Calc ("Rational.get_numerator", eval_get_numerator "#get_numerator"),
   306                   Rule.Calc ("Partial_Fractions.factors_from_solution",
   307                     eval_factors_from_solution "#factors_from_solution")
   308                   ], scr = Rule.EmptyScr},
   309           prls = Rule.e_rls, crls = Rule.e_rls, errpats = [], nrls = norm_Rational},
   310         (*([], Frm), Problem (Isac, [Inverse, Z_Transform, SignalProcessing])*)
   311         "Script InverseZTransform (X_eq::bool) =            "^
   312           (*([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))*)
   313           "(let X = Take X_eq;                                "^
   314           (*([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   315           "  X' = Rewrite ''ruleZY'' False X;                     "^
   316           (*            ?X' z*)
   317           "  (X'_z::real) = lhs X';                           "^
   318           (*            z *)
   319           "  (zzz::real) = argument_in X'_z;                  "^
   320           (*            3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   321           "  (funterm::real) = rhs X';                        "^
   322 
   323           "  (pbz::real) = (SubProblem (''Isac'',                "^
   324           "    [''partial_fraction'',''rational'',''simplification''],    "^
   325           "    [''simplification'',''of_rationals'',''to_partial_fraction'']) "^
   326           (*([2], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   327           "    [REAL funterm, REAL zzz]);                     "^
   328 
   329           (*([3], Frm), ?X' z = 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   330           "  (pbz_eq::bool) = Take (X'_z = pbz);              "^
   331           (*([3], Res), ?X' z = 4 * (?z / (z - 1 / 2)) + -4 * (?z / (z - -1 / 4))*)
   332           "  pbz_eq = Rewrite ''ruleYZ'' False pbz_eq;            "^
   333           (*               4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
   334           (*([4], Frm), X_z = 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
   335           "  (X_zeq::bool) = Take (X_z = rhs pbz_eq);         "^
   336           (*([4], Res), X_z = 4 * (1 / 2) ^^^ ?n * ?u [?n] + -4 * (-1 / 4) ^^^ ?n * ?u [?n]*)
   337           "  n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq "^
   338           (*            X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
   339           (*([], Res), X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
   340           "in n_eq)")]
   341 \<close>
   342 
   343 end
   344