1 (* Title: Test_Z_Transform
3 (c) copyright due to lincense terms.
4 12345678901234567890123456789012345678901234567890123456789012345678901234567890
5 10 20 30 40 50 60 70 80
8 theory Inverse_Z_Transform imports PolyEq DiffApp Partial_Fractions begin
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])"*)
20 ruleZY: "(X z = a / b) = (X' z = a / (z * b))" and
21 ruleYZ: "(a/b + c/d) = (a*(z/b) + c*(z/d))"
23 subsection\<open>Define the Field Descriptions for the specification\<close>
25 filterExpression :: "bool => una"
26 stepResponse :: "bool => una"
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 = [],
35 Rule.Thm ("rule4", @{thm rule4})
37 scr = Rule.EmptyScr});
41 text \<open>store the rule set for math engine\<close>
43 setup \<open>KEStore_Elems.add_rlss [("inverse_z", (Context.theory_name @{theory}, inverse_z))]\<close>
45 subsection\<open>Define the Specification\<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>
68 subsection \<open>Define Name and Signature for the Method\<close>
70 InverseZTransform :: "[bool, bool] => bool"
71 ("((Script InverseZTransform (_ =))// (_))" 9)
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")]
85 partial_function (tailrec) inverse_ztransform :: "bool \<Rightarrow> bool"
87 "inverse_ztransform X_eq = \<comment> \<open>(1/z) instead of z ^^^ -1\<close>
89 X' = Rewrite ''ruleZY'' False X; \<comment> \<open>z * denominator\<close>
90 X' = (Rewrite_Set ''norm_Rational'' False) X'; \<comment> \<open>simplify\<close>
91 funterm = Take (rhs X'); \<comment> \<open>drop X' z = for equation solving\<close>
92 denom = (Rewrite_Set ''partial_fraction'' False) funterm; \<comment> \<open>get_denominator\<close>
93 equ = (denom = (0::real));
94 fun_arg = Take (lhs X');
95 arg = (Rewrite_Set ''partial_fraction'' False) X'; \<comment> \<open>get_argument TODO\<close>
96 L_L = SubProblem (''Test'', [''LINEAR'',''univariate'',''equation'',''test''],
97 [''Test'',''solve_linear'']) [BOOL equ, STRING ''z''] \<comment> \<open>PROG string\<close>
99 setup \<open>KEStore_Elems.add_mets
100 [Specify.prep_met thy "met_SP_Ztrans_inv" [] Celem.e_metID
101 (["SignalProcessing", "Z_Transform", "Inverse"],
102 [("#Given" ,["filterExpression (X_eq::bool)"]),
103 ("#Find" ,["stepResponse (n_eq::bool)"])],
104 {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [], srls = Rule.e_rls, prls = Rule.e_rls, crls = Rule.e_rls,
105 errpats = [], nrls = Rule.e_rls},
106 "Script InverseZTransform (X_eq::bool) =" ^ (*(1/z) instead of z ^^^ -1*)
107 " (let X = Take X_eq;" ^
108 " X' = Rewrite ''ruleZY'' False X;" ^ (*z * denominator*)
109 " X' = (Rewrite_Set ''norm_Rational'' False) X';" ^ (*simplify*)
110 " funterm = Take (rhs X');" ^ (*drop X' z = for equation solving*)
111 " denom = (Rewrite_Set ''partial_fraction'' False) funterm;" ^ (*get_denominator*)
112 " equ = (denom = (0::real));" ^
113 " fun_arg = Take (lhs X');" ^
114 " arg = (Rewrite_Set ''partial_fraction'' False) X';" ^ (*get_argument TODO*)
115 " (L_L::bool list) = " ^
116 " (SubProblem (''Test'', " ^
117 " [''LINEAR'',''univariate'',''equation'',''test'']," ^
118 " [''Test'',''solve_linear'']) " ^
119 " [BOOL equ, REAL z]) " ^
123 Type unification failed: Clash of types "bool" and "_ itself"
124 Type error in application: incompatible operand type
125 Operator: Let (Take X_eq) :: (??'a itself \<Rightarrow> ??'b) \<Rightarrow> ??'b
127 \<lambda>X. let X' = Rewrite ''ruleZY'' ...
129 :partial_function (tailrec) inverse_ztransform2 :: "bool \<Rightarrow> bool"
131 "inverse_ztransform X_eq = \<comment> \<open>(1/z) instead of z ^^^ -1\<close>
133 X' = Rewrite ''ruleZY'' False X; \<comment> \<open>z * denominator\<close>
134 (num_orig::real) = get_numerator (rhs X');
135 X' = (Rewrite_Set ''norm_Rational'' False) X'; \<comment> \<open>simplify\<close>
136 (X'_z::real) = lhs X';
137 (zzz::real) = argument_in X'_z;
138 (funterm::real) = rhs X'; \<comment> \<open>drop X' z = for equation solving\<close>
139 (denom::real) = get_denominator funterm; \<comment> \<open>get_denominator\<close>
140 (num::real) = get_numerator funterm; \<comment> \<open>get_numerator\<close>
141 (equ::bool) = (denom = (0::real));
142 (L_L::bool list) = (SubProblem (''PolyEq'',
143 [''abcFormula'',''degree_2'',''polynomial'',''univariate'',''equation''],
145 [BOOL equ, REAL zzz]);
146 (facs::real) = factors_from_solution L_L;
147 (eql::real) = Take (num_orig / facs); \<comment> \<open>---\<close>
148 (eqr::real) = (Try (Rewrite_Set ''ansatz_rls'' False)) eql; \<comment> \<open>---\<close>
149 (eq::bool) = Take (eql = eqr); \<comment> \<open>Maybe possible to use HOLogic.mk_eq ??\<close>
150 eq = (Try (Rewrite_Set ''equival_trans'' False)) eq; \<comment> \<open>---\<close>
151 eq = drop_questionmarks eq;
152 (z1::real) = (rhs (NTH 1 L_L)); \<comment> \<open>prepare equation for a - eq_a therefor substitute z with solution 1 - z1\<close>
153 (z2::real) = (rhs (NTH 2 L_L)); \<comment> \<open>---\<close>
154 (eq_a::bool) = Take eq;
155 eq_a = (Substitute [zzz=z1]) eq;
156 eq_a = (Rewrite_Set ''norm_Rational'' False) eq_a;
158 (SubProblem (''Isac'',
159 [''univariate'',''equation''],[''no_met''])
160 [BOOL eq_a, REAL (A::real)]);
161 (a::real) = (rhs(NTH 1 sol_a)); \<comment> \<open>---\<close>
162 (eq_b::bool) = Take eq;
163 eq_b = (Substitute [zzz=z2]) eq_b;
164 eq_b = (Rewrite_Set ''norm_Rational'' False) eq_b;
166 (SubProblem (''Isac'',
167 [''univariate'',''equation''],[''no_met''])
168 [BOOL eq_b, REAL (B::real)]);
169 (b::real) = (rhs(NTH 1 sol_b)); \<comment> \<open>---\<close>
170 eqr = drop_questionmarks eqr;
171 (pbz::real) = Take eqr;
172 pbz = ((Substitute [A=a, B=b]) pbz); \<comment> \<open>---\<close>
173 pbz = Rewrite ''ruleYZ'' False pbz;
174 pbz = drop_questionmarks pbz; \<comment> \<open>---\<close>
175 (X_z::bool) = Take (X_z = pbz);
176 (n_eq::bool) = (Rewrite_Set ''inverse_z'' False) X_z;
177 n_eq = drop_questionmarks n_eq
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; "^
192 " (num_orig::real) = get_numerator (rhs X'); "^
193 " X' = (Rewrite_Set ''norm_Rational'' False) X'; "^
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; "^
201 " (num::real) = get_numerator funterm; "^
203 " (equ::bool) = (denom = (0::real)); "^
204 " (L_L::bool list) = (SubProblem (''PolyEq'', "^
205 " [''abcFormula'',''degree_2'',''polynomial'',''univariate'',''equation''], "^
207 " [BOOL equ, REAL zzz]); "^
208 " (facs::real) = factors_from_solution L_L; "^
209 " (eql::real) = Take (num_orig / facs); "^
211 " (eqr::real) = (Try (Rewrite_Set ''ansatz_rls'' False)) eql; "^
213 " (eq::bool) = Take (eql = eqr); "^
214 (*Maybe possible to use HOLogic.mk_eq ??*)
215 " eq = (Try (Rewrite_Set ''equival_trans'' False)) eq; "^
217 " eq = drop_questionmarks eq; "^
218 " (z1::real) = (rhs (NTH 1 L_L)); "^
220 * prepare equation for a - eq_a
221 * therefor substitute z with solution 1 - z1
223 " (z2::real) = (rhs (NTH 2 L_L)); "^
225 " (eq_a::bool) = Take eq; "^
226 " eq_a = (Substitute [zzz=z1]) eq; "^
227 " eq_a = (Rewrite_Set ''norm_Rational'' False) eq_a; "^
228 " (sol_a::bool list) = "^
229 " (SubProblem (''Isac'', "^
230 " [''univariate'',''equation''],[''no_met'']) "^
231 " [BOOL eq_a, REAL (A::real)]); "^
232 " (a::real) = (rhs(NTH 1 sol_a)); "^
234 " (eq_b::bool) = Take eq; "^
235 " eq_b = (Substitute [zzz=z2]) eq_b; "^
236 " eq_b = (Rewrite_Set ''norm_Rational'' False) eq_b; "^
237 " (sol_b::bool list) = "^
238 " (SubProblem (''Isac'', "^
239 " [''univariate'',''equation''],[''no_met'']) "^
240 " [BOOL eq_b, REAL (B::real)]); "^
241 " (b::real) = (rhs(NTH 1 sol_b)); "^
243 " eqr = drop_questionmarks eqr; "^
244 " (pbz::real) = Take eqr; "^
245 " pbz = ((Substitute [A=a, B=b]) pbz); "^
247 " pbz = Rewrite ''ruleYZ'' False pbz; "^
248 " pbz = drop_questionmarks pbz; "^
250 " (X_z::bool) = Take (X_z = pbz); "^
251 " (n_eq::bool) = (Rewrite_Set ''inverse_z'' False) X_z; "^
252 " n_eq = drop_questionmarks n_eq "^
255 (* same error as in inverse_ztransform2
256 :partial_function (tailrec) inverse_ztransform3 :: "bool \<Rightarrow> bool"
258 "inverse_ztransform X_eq =
259 (*([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))*)
261 (*([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
262 X' = Rewrite ''ruleZY'' False X;
264 (X'_z::real) = lhs X';
266 (zzz::real) = argument_in X'_z;
267 (* 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
268 (funterm::real) = rhs X';
270 (pbz::real) = (SubProblem (''Isac'',
271 [''partial_fraction'',''rational'',''simplification''],
272 [''simplification'',''of_rationals'',''to_partial_fraction''])
273 (*([2], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
274 [REAL funterm, REAL zzz]);
276 (*([3], Frm), ?X' z = 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
277 (pbz_eq::bool) = Take (X'_z = pbz);
278 (*([3], Res), ?X' z = 4 * (?z / (z - 1 / 2)) + -4 * (?z / (z - -1 / 4))*)
279 pbz_eq = Rewrite ''ruleYZ'' False pbz_eq;
280 (* 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
281 pbz_eq = drop_questionmarks pbz_eq;
282 (*([4], Frm), X_z = 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
283 (X_zeq::bool) = Take (X_z = rhs pbz_eq);
284 (*([4], Res), X_z = 4 * (1 / 2) ^^^ ?n * ?u [?n] + -4 * (-1 / 4) ^^^ ?n * ?u [?n]*)
285 n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq;
286 (* X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
287 n_eq = drop_questionmarks n_eq
288 (*([], Res), X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
291 setup \<open>KEStore_Elems.add_mets
292 [Specify.prep_met thy "met_SP_Ztrans_inv_sub" [] Celem.e_metID
293 (["SignalProcessing", "Z_Transform", "Inverse_sub"],
294 [("#Given" ,["filterExpression X_eq"]),
295 ("#Find" ,["stepResponse n_eq"])],
296 {rew_ord'="tless_true", rls'= Rule.e_rls, calc = [],
297 srls = Rule.Rls {id="srls_partial_fraction",
298 preconds = [], rew_ord = ("termlessI",termlessI),
299 erls = Rule.append_rls "erls_in_srls_partial_fraction" Rule.e_rls
300 [(*for asm in NTH_CONS ...*)
301 Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
302 (*2nd NTH_CONS pushes n+-1 into asms*)
303 Rule.Calc("Groups.plus_class.plus", eval_binop "#add_")],
304 srls = Rule.Erls, calc = [], errpatts = [],
305 rules = [Rule.Thm ("NTH_CONS", @{thm NTH_CONS}),
306 Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"),
307 Rule.Thm ("NTH_NIL", @{thm NTH_NIL}),
308 Rule.Calc ("Tools.lhs", Tools.eval_lhs "eval_lhs_"),
309 Rule.Calc ("Tools.rhs", Tools.eval_rhs"eval_rhs_"),
310 Rule.Calc ("Atools.argument'_in", eval_argument_in "Atools.argument'_in"),
311 Rule.Calc ("Rational.get_denominator", eval_get_denominator "#get_denominator"),
312 Rule.Calc ("Rational.get_numerator", eval_get_numerator "#get_numerator"),
313 Rule.Calc ("Partial_Fractions.factors_from_solution",
314 eval_factors_from_solution "#factors_from_solution"),
315 Rule.Calc("Partial_Fractions.drop_questionmarks", eval_drop_questionmarks "#drop_?")],
316 scr = Rule.EmptyScr},
317 prls = Rule.e_rls, crls = Rule.e_rls, errpats = [], nrls = norm_Rational},
318 (*([], Frm), Problem (Isac, [Inverse, Z_Transform, SignalProcessing])*)
319 "Script InverseZTransform (X_eq::bool) = "^
320 (*([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))*)
321 "(let X = Take X_eq; "^
322 (*([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
323 " X' = Rewrite ''ruleZY'' False X; "^
325 " (X'_z::real) = lhs X'; "^
327 " (zzz::real) = argument_in X'_z; "^
328 (* 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))*)
329 " (funterm::real) = rhs X'; "^
331 " (pbz::real) = (SubProblem (''Isac'', "^
332 " [''partial_fraction'',''rational'',''simplification''], "^
333 " [''simplification'',''of_rationals'',''to_partial_fraction'']) "^
334 (*([2], Res), 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
335 " [REAL funterm, REAL zzz]); "^
337 (*([3], Frm), ?X' z = 4 / (z - 1 / 2) + -4 / (z - -1 / 4)*)
338 " (pbz_eq::bool) = Take (X'_z = pbz); "^
339 (*([3], Res), ?X' z = 4 * (?z / (z - 1 / 2)) + -4 * (?z / (z - -1 / 4))*)
340 " pbz_eq = Rewrite ''ruleYZ'' False pbz_eq; "^
341 (* 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
342 " pbz_eq = drop_questionmarks pbz_eq; "^
343 (*([4], Frm), X_z = 4 * (z / (z - 1 / 2)) + -4 * (z / (z - -1 / 4))*)
344 " (X_zeq::bool) = Take (X_z = rhs pbz_eq); "^
345 (*([4], Res), X_z = 4 * (1 / 2) ^^^ ?n * ?u [?n] + -4 * (-1 / 4) ^^^ ?n * ?u [?n]*)
346 " n_eq = (Rewrite_Set ''inverse_z'' False) X_zeq; "^
347 (* X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)
348 " n_eq = drop_questionmarks n_eq "^
349 (*([], Res), X_z = 4 * (1 / 2) ^^^ n * u [n] + -4 * (-1 / 4) ^^^ n * u [n]*)