src/Tools/isac/Knowledge/Inverse_Z_Transform.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 21 Nov 2013 11:46:00 +0100
changeset 55276 ce872d7781d2
parent 52155 e4ddf21390fd
child 55339 cccd24e959ba
permissions -rwxr-xr-x
Isabelle2013 --> 2013-1: replace an Isabelle constant newly introduced
     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{*Define the Field Descriptions for the specification*}
    24 consts
    25   filterExpression  :: "bool => una"
    26   stepResponse      :: "bool => una"
    27 
    28 
    29 ML {*
    30 val inverse_z = prep_rls(
    31   Rls {id = "inverse_z", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
    32 	  erls = Erls, srls = Erls, calc = [], errpatts = [],
    33 	  rules = 
    34 	   [
    35     Thm ("rule4",num_str @{thm rule4})
    36 	   ], 
    37 	 scr = EmptyScr}:rls);
    38 *}
    39 
    40 
    41 text {*store the rule set for math engine*}
    42 
    43 setup {* KEStore_Elems.add_rlss [("inverse_z", (Context.theory_name @{theory}, inverse_z))] *}
    44 
    45 subsection{*Define the Specification*}
    46 ML {*
    47 val thy = @{theory};
    48 
    49 store_pbt
    50  (prep_pbt thy "pbl_SP" [] e_pblID
    51  (["SignalProcessing"], [], e_rls, NONE, []));
    52 store_pbt
    53  (prep_pbt thy "pbl_SP_Ztrans" [] e_pblID
    54  (["Z_Transform","SignalProcessing"], [], e_rls, NONE, []));
    55 store_pbt
    56  (prep_pbt thy "pbl_SP_Ztrans_inv" [] e_pblID
    57  (["Inverse", "Z_Transform", "SignalProcessing"],
    58   (*^ capital letter breaks coding standard
    59     because "inverse" = Const ("Rings.inverse_class.inverse", ..*)
    60   [("#Given" ,["filterExpression (X_eq::bool)"]),
    61    ("#Find"  ,["stepResponse (n_eq::bool)"])
    62   ],
    63   append_rls "e_rls" e_rls [(*for preds in where_*)], NONE, 
    64   [["SignalProcessing","Z_Transform","Inverse"]]));
    65 *}
    66 ML {*
    67   store_pbt
    68    (prep_pbt thy "pbl_SP_Ztrans_inv" [] e_pblID
    69    (["Inverse", "Z_Transform", "SignalProcessing"],
    70     [("#Given" ,["filterExpression X_eq"]),
    71      ("#Find"  ,["stepResponse n_eq"])
    72     ],
    73     append_rls "e_rls" e_rls [(*for preds in where_*)], NONE, 
    74     [["SignalProcessing","Z_Transform","Inverse"]]));
    75 *}
    76 
    77 subsection {*Define Name and Signature for the Method*}
    78 consts
    79   InverseZTransform :: "[bool, bool] => bool"
    80     ("((Script InverseZTransform (_ =))// (_))" 9)
    81 
    82 subsection {*Setup Parent Nodes in Hierarchy of Method*}
    83 ML {*
    84 store_met
    85  (prep_met thy "met_SP" [] e_metID
    86  (["SignalProcessing"], [],
    87    {rew_ord'="tless_true", rls'= e_rls, calc = [], srls = e_rls, prls = e_rls,
    88     crls = e_rls, errpats = [], nrls = e_rls}, "empty_script"));
    89 store_met
    90  (prep_met thy "met_SP_Ztrans" [] e_metID
    91  (["SignalProcessing", "Z_Transform"], [],
    92    {rew_ord'="tless_true", rls'= e_rls, calc = [], srls = e_rls, prls = e_rls,
    93     crls = e_rls, errpats = [], nrls = e_rls}, "empty_script"));
    94 val thy = @{theory}; (*latest version of thy required*)
    95 store_met
    96  (prep_met thy "met_SP_Ztrans_inv" [] e_metID
    97  (["SignalProcessing", "Z_Transform", "Inverse"], 
    98   [("#Given" ,["filterExpression (X_eq::bool)"]),
    99    ("#Find"  ,["stepResponse (n_eq::bool)"])
   100   ],
   101    {rew_ord'="tless_true", rls'= e_rls, calc = [], srls = e_rls, prls = e_rls,
   102     crls = e_rls, errpats = [], nrls = e_rls},
   103 "Script InverseZTransform (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  ));
   119 *}
   120 ML {*
   121   store_met(
   122     prep_met thy "met_SP_Ztrans_inv" [] e_metID
   123     (["SignalProcessing",
   124       "Z_Transform",
   125       "Inverse"], 
   126      [
   127        ("#Given" ,["filterExpression X_eq"]),
   128        ("#Find"  ,["stepResponse n_eq"])
   129      ],
   130      {
   131        rew_ord'="tless_true",
   132        rls'= e_rls, calc = [],
   133        srls = srls_partial_fraction, 
   134        prls = e_rls,
   135        crls = e_rls, errpats = [], nrls = e_rls
   136      },
   137      "Script InverseZTransform (X_eq::bool) =                        "^
   138      (*(1/z) instead of z ^^^ -1*)
   139      "(let X = Take X_eq;                                            "^
   140      "      X' = Rewrite ruleZY False X;                             "^
   141      (*z * denominator*)
   142      "      (num_orig::real) = get_numerator (rhs X');               "^
   143      "      X' = (Rewrite_Set norm_Rational False) X';               "^
   144      (*simplify*)
   145      "      (X'_z::real) = lhs X';                                   "^
   146      "      (zzz::real) = argument_in X'_z;                          "^
   147      "      (funterm::real) = rhs X';                                "^
   148      (*drop X' z = for equation solving*)
   149      "      (denom::real) = get_denominator funterm;                 "^
   150      (*get_denominator*)
   151      "      (num::real) = get_numerator funterm;                     "^
   152      (*get_numerator*)
   153      "      (equ::bool) = (denom = (0::real));                       "^
   154      "      (L_L::bool list) = (SubProblem (PolyEq',                 "^
   155      "         [abcFormula,degree_2,polynomial,univariate,equation], "^
   156      "         [no_met])                                             "^
   157      "         [BOOL equ, REAL zzz]);                                "^
   158      "      (facs::real) = factors_from_solution L_L;                "^
   159      "      (eql::real) = Take (num_orig / facs);                    "^ 
   160 
   161      "      (eqr::real) = (Try (Rewrite_Set ansatz_rls False)) eql;  "^
   162 
   163      "      (eq::bool) = Take (eql = eqr);                           "^
   164      (*Maybe possible to use HOLogic.mk_eq ??*)
   165      "      eq = (Try (Rewrite_Set equival_trans False)) eq;         "^ 
   166 
   167      "      eq = drop_questionmarks eq;                              "^
   168      "      (z1::real) = (rhs (NTH 1 L_L));                          "^
   169      (* 
   170       * prepare equation for a - eq_a
   171       * therefor substitute z with solution 1 - z1
   172       *)
   173      "      (z2::real) = (rhs (NTH 2 L_L));                          "^
   174  
   175      "      (eq_a::bool) = Take eq;                                  "^
   176      "      eq_a = (Substitute [zzz=z1]) eq;                         "^
   177      "      eq_a = (Rewrite_Set norm_Rational False) eq_a;           "^
   178      "      (sol_a::bool list) =                                     "^
   179      "                 (SubProblem (Isac',                           "^
   180      "                              [univariate,equation],[no_met])  "^
   181      "                              [BOOL eq_a, REAL (A::real)]);    "^
   182      "      (a::real) = (rhs(NTH 1 sol_a));                          "^
   183 
   184      "      (eq_b::bool) = Take eq;                                  "^
   185      "      eq_b =  (Substitute [zzz=z2]) eq_b;                      "^
   186      "      eq_b = (Rewrite_Set norm_Rational False) eq_b;           "^
   187      "      (sol_b::bool list) =                                     "^
   188      "                 (SubProblem (Isac',                           "^
   189      "                              [univariate,equation],[no_met])  "^
   190      "                              [BOOL eq_b, REAL (B::real)]);    "^
   191      "      (b::real) = (rhs(NTH 1 sol_b));                          "^
   192 
   193      "      eqr = drop_questionmarks eqr;                            "^
   194      "      (pbz::real) = Take eqr;                                  "^
   195      "      pbz = ((Substitute [A=a, B=b]) pbz);                     "^
   196 
   197      "      pbz = Rewrite ruleYZ False pbz;                          "^
   198      "      pbz = drop_questionmarks pbz;                            "^
   199 
   200      "      (X_z::bool) = Take (X_z = pbz);                          "^
   201      "      (n_eq::bool) = (Rewrite_Set inverse_z False) X_z;        "^
   202      "      n_eq = drop_questionmarks n_eq                           "^
   203      "in n_eq)" 
   204     )
   205            );
   206 
   207 store_met (prep_met thy "met_SP_Ztrans_inv_sub" [] e_metID
   208   (["SignalProcessing", "Z_Transform", "Inverse_sub"], 
   209    [("#Given" ,["filterExpression X_eq"]),
   210     ("#Find"  ,["stepResponse n_eq"])],
   211    {rew_ord'="tless_true",
   212     rls'= e_rls, calc = [],
   213     srls = Rls {id="srls_partial_fraction", 
   214       preconds = [],
   215       rew_ord = ("termlessI",termlessI),
   216       erls = append_rls "erls_in_srls_partial_fraction" e_rls
   217        [(*for asm in NTH_CONS ...*)
   218         Calc ("Orderings.ord_class.less",eval_equ "#less_"),
   219         (*2nd NTH_CONS pushes n+-1 into asms*)
   220         Calc("Groups.plus_class.plus", eval_binop "#add_")], 
   221         srls = Erls, calc = [], errpatts = [],
   222         rules = [
   223           Thm ("NTH_CONS",num_str @{thm NTH_CONS}),
   224           Calc("Groups.plus_class.plus", eval_binop "#add_"),
   225           Thm ("NTH_NIL",num_str @{thm NTH_NIL}),
   226           Calc("Tools.lhs", eval_lhs "eval_lhs_"),
   227           Calc("Tools.rhs", eval_rhs"eval_rhs_"),
   228           Calc("Atools.argument'_in", eval_argument_in "Atools.argument'_in"),
   229           Calc("Rational.get_denominator", eval_get_denominator "#get_denominator"),
   230           Calc("Rational.get_numerator", eval_get_numerator "#get_numerator"),
   231           Calc("Partial_Fractions.factors_from_solution",
   232             eval_factors_from_solution "#factors_from_solution"),
   233           Calc("Partial_Fractions.drop_questionmarks", eval_drop_questionmarks "#drop_?")],
   234           scr = EmptyScr},
   235     prls = e_rls, crls = e_rls, errpats = [], nrls = norm_Rational},
   236    "Script InverseZTransform (X_eq::bool) =            "^(*([], Frm), Problem (Isac, [Inverse, Z_Transform, SignalProcessing])*)
   237    "(let X = Take X_eq;                                "^(*([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))*)
   238    "  X' = Rewrite ruleZY False X;                     "^(*([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   239    "  (X'_z::real) = lhs X';                           "^(*            ?X' z*)
   240    "  (zzz::real) = argument_in X'_z;                  "^(*            z *)
   241    "  (funterm::real) = rhs X';                        "^(*            3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
   242 
   243    "  (pbz::real) = (SubProblem (Isac',                "^(**)
   244    "    [partial_fraction,rational,simplification],    "^
   245    "    [simplification,of_rationals,to_partial_fraction]) "^
   246    "    [REAL funterm, REAL zzz]);                     "^(*([2], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   247 
   248    "  (pbz_eq::bool) = Take (X'_z = pbz);              "^(*([3], Frm), ?X' z = 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
   249    "  pbz_eq = Rewrite ruleYZ False pbz_eq;            "^(*([3], Res), ?X' z = 4 * (?z / (z - 1 / 2)) + -4 * (?z / (z - -1 / 4))*)
   250    "  pbz_eq = drop_questionmarks pbz_eq;              "^(*               4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
   251    "  (X_zeq::bool) = Take (X_z = rhs pbz_eq);         "^(*([4], Frm), X_z = 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
   252    "  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]*)
   253    "  n_eq = drop_questionmarks n_eq                   "^(*            X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
   254    "in n_eq)"                                            (*([], Res), X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
   255   ));
   256 
   257 *}
   258 
   259 end
   260