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