/* tests for z-transform code
 * Copyright 2008 by Robert Dodier
 * I release this work under terms of the GNU General Public License
 */

/* for further examples, see also:
 * http://web.mit.edu/reduce_v20101007/reduce-algebra-20101007/packages/ztrans/ztrans.tst
 * and: http://www.reduce-algebra.com/docs/ztrans.pdf
 */

(if not get ('z_transform, 'present) then load (z_transform), 0);
0;

/*
 * Table 9.1.3 at: http://mathfaculty.fullerton.edu/MATHEWS/C2003/ZTRANSFORMINTROMOD.HTML
 */
 
apply_z_transform (z_transform (kron_delta (n, 0), n, z));
    1;

apply_z_transform (z_transform (kron_delta (0, n), n, z));
    1;

apply_z_transform (z_transform (1, n, z)), ratsimp, factor;
    z/(z - 1);

apply_z_transform (z_transform (b^n, n, z));
    z/(z - b);

apply_z_transform (z_transform (exp (a*n), n, z));
    z/(z - exp (a));

apply_z_transform (z_transform (n, n, z)), ratsimp, factor;
    z/(z - 1)^2;

apply_z_transform (z_transform (n^2, n, z)), ratsimp, factor;
    z*(z + 1)/(z - 1)^3;

apply_z_transform (z_transform (b^n * n, n, z));
    'subst (z/b, z, -z * 'diff (z/(z - 1), z, 1));
    
''%, nouns, ratsimp, factor;
    b*z / (z - b)^2;

apply_z_transform (z_transform (exp (a*n) * n, n, z));
    -z * 'diff (z/(z - %e^a), z, 1);
    
''%, nouns, ratsimp, factor;
    z*exp(a) / (z - exp(a))^2;

apply_z_transform (z_transform (sin (a*n), n, z)), ratsimp, factor;
    z*sin(a) / (z^2 - 2*cos(a)*z + 1);

apply_z_transform (z_transform (b^n * sin (a*n), n, z));
    'subst (z/b, z, z*sin(a) / (z^2 - 2*cos(a)*z + 1));

''%, nouns, ratsimp, factor;
    b*z*sin(a) / (z^2 - 2*cos(a)*b*z + b^2);

apply_z_transform (z_transform (cos (a*n), n, z)), ratsimp, factor;
    z * (z - cos(a)) / (z^2 - 2*cos(a)*z + 1);

apply_z_transform (z_transform (b^n * cos (a*n), n, z));
    'subst (z/b, z, z * (z - cos(a)) / (z^2 - 2*cos(a)*z + 1));
    
''%, nouns, ratsimp, factor;
    z * (z - b*cos(a)) / (z^2 - 2*cos(a)*b*z + b^2);

/*
 * Other examples.
 */

apply_z_transform (z_transform (kron_delta (m, x), m, u));
    1/u^x;

apply_z_transform (z_transform (kron_delta (y, m), m, u));
    1/u^y;

apply_z_transform (z_transform (convolution (f(m, u), g(m, u)), m, u));
    z_transform(f(m,u),m,u)*z_transform(g(m,u),m,u);

apply_z_transform (z_transform (kron_delta (m, x) + 1, m, u));
    1/u^x + u/(u - 1);

apply_z_transform (z_transform (10 * m - convolution (f(m, u), g(m, u)) / %pi, m, u));
    10*u/(u - 1)^2 - z_transform(f(m,u),m,u)*z_transform(g(m,u),m,u) / %pi;

apply_z_transform (z_transform (sin (m*omega) - %i * sin (m*omega) * c^m + %pi, m, u));
    sin(omega)*u / (u^2 - 2*cos(omega)*u + 1)
      - %i * 'subst (u/c, u, sin(omega)*u / (u^2 - 2*cos(omega)*u + 1))
      + %pi * u/(u - 1);

/* from the mailing list 2009-02-14 */

e1a : apply_z_transform (z_transform (a+b*(e^(-p*T))^(m) , m, z));
b*z/(z-1/e^(p*T))+a*z/(z-1);

e1b : apply_z_transform (z_transform (a+b*(e^(p*T))^(m) , m, z));
b*z/(z-e^(p*T))+a*z/(z-1);

e2a : apply_z_transform (z_transform (a+b*(%e^(-p*T))^(m) , m, z));
(b*z)/(z - exp(-p*T)) + (a*z)/(z - 1);

e2b : apply_z_transform (z_transform (a+b*(%e^(p*T))^(m) , m, z));
(b*z)/(z - exp(p*T)) + (a*z)/(z - 1);

is (equal (e2a, subst (e=%e, e1a)));
true;

is (equal (e2b, subst (e=%e, e1b)));
true;

/* more about negative exponents */

apply_z_transform (z_transform (b^-n, n, z));
    z/(z - 1/b);

apply_z_transform (z_transform ((b^-a)^m, m, u));
    u/(u - 1/b^a);

apply_z_transform (z_transform (b^-n * n, n, z));
    'subst (z*b, z, -z * 'diff (z/(z - 1), z, 1));

apply_z_transform (z_transform ((a + b)^-m * m, m, s));
    'subst (s*(a + b), s, -s * 'diff (s/(s - 1), s, 1));

apply_z_transform (z_transform (b^-n * sin (a*n), n, z));
    'subst (z*b, z, z*sin(a) / (z^2 - 2*cos(a)*z + 1));
    
apply_z_transform (z_transform ((%pi - 1)^-k * cos (a*k), k, y));
    'subst (y*(%pi - 1), y, y * (y - cos(a)) / (y^2 - 2*cos(a)*y + 1));
