Method on @sym: ztrans (f, n, z)
Method on @sym: ztrans (f)
Method on @sym: ztrans (f, z)

Symbolic one-sided Z-transform.

The one-sided Z-transform of a function f of n is a function X of z defined by the Laurent series below.

syms n nonnegative integer
syms f(n)
syms z nonzero complex
X(z) = ztrans (f)
  ⇒ X(z) = (symfun)
        ∞
       ___
       ╲
        ╲    -n
        ╱   z  ⋅f(n)
       ╱
       ‾‾‾
      n = 0

Example:

syms n
f = n^2;
ztrans (f)
  ⇒ (sym)
      ⎧        1
      ⎪   -1 - ─
      ⎪        z          1
      ⎪───────────   for ─── < 1
      ⎪          3       │z│
      ⎪  ⎛     1⎞
      ⎪z⋅⎜-1 + ─⎟
      ⎪  ⎝     z⎠
      ⎨
      ⎪  ∞
      ⎪ ___
      ⎪ ╲
      ⎪  ╲    2  -n
      ⎪  ╱   n ⋅z     otherwise
      ⎪ ╱
      ⎪ ‾‾‾
      ⎩n = 0

By default the output is a function of z (or w if z is an independent variable of f). This can be overridden by specifying z. For example:

syms n z w
ztrans (exp (n))
  ⇒ (sym)
      ⎧     1             ℯ
      ⎪   ─────      for ─── < 1
      ⎪       ℯ          │z│
      ⎪   1 - ─
      ⎪       z
      ⎪
      ⎪  ∞
      ⎨ ___
      ⎪ ╲
      ⎪  ╲    -n  n
      ⎪  ╱   z  ⋅ℯ    otherwise
      ⎪ ╱
      ⎪ ‾‾‾
      ⎪n = 0
      ⎩
ztrans (exp (z))
  ⇒ (sym)
      ⎧     1             ℯ
      ⎪   ─────      for ─── < 1
      ⎪       ℯ          │w│
      ⎪   1 - ─
      ⎪       w
      ⎪
      ⎪  ∞
      ⎨ ___
      ⎪ ╲
      ⎪  ╲    -z  z
      ⎪  ╱   w  ⋅ℯ    otherwise
      ⎪ ╱
      ⎪ ‾‾‾
      ⎪z = 0
      ⎩
ztrans (exp (n), w)
  ⇒ (sym)
      ⎧     1             ℯ
      ⎪   ─────      for ─── < 1
      ⎪       ℯ          │w│
      ⎪   1 - ─
      ⎪       w
      ⎪
      ⎪  ∞
      ⎨ ___
      ⎪ ╲
      ⎪  ╲    -n  n
      ⎪  ╱   w  ⋅ℯ    otherwise
      ⎪ ╱
      ⎪ ‾‾‾
      ⎪n = 0
      ⎩

If not specified by n, the independent variable is chosen by looking for a symbol named n. If no such symbol is found, see ‘@sym/symvar’ is used, which choses a variable close to x:

syms a n y
ztrans (n * exp (y))
  ⇒ (sym)
      ⎛⎧    1             1     ⎞
      ⎜⎪──────────   for ─── < 1⎟
      ⎜⎪         2       │z│    ⎟
      ⎜⎪  ⎛    1⎞               ⎟
      ⎜⎪z⋅⎜1 - ─⎟               ⎟
      ⎜⎪  ⎝    z⎠               ⎟
      ⎜⎪                        ⎟  y
      ⎜⎨  ∞                     ⎟⋅ℯ
      ⎜⎪ ___                    ⎟
      ⎜⎪ ╲                      ⎟
      ⎜⎪  ╲      -n             ⎟
      ⎜⎪  ╱   n⋅z     otherwise ⎟
      ⎜⎪ ╱                      ⎟
      ⎜⎪ ‾‾‾                    ⎟
      ⎝⎩n = 0                   ⎠
ztrans (a * exp (y))
  ⇒ (sym)
        ⎛⎧     1             ℯ     ⎞
        ⎜⎪   ─────      for ─── < 1⎟
        ⎜⎪       ℯ          │z│    ⎟
        ⎜⎪   1 - ─                 ⎟
        ⎜⎪       z                 ⎟
        ⎜⎪                         ⎟
        ⎜⎪  ∞                      ⎟
      a⋅⎜⎨ ___                     ⎟
        ⎜⎪ ╲                       ⎟
        ⎜⎪  ╲    -y  y             ⎟
        ⎜⎪  ╱   z  ⋅ℯ    otherwise ⎟
        ⎜⎪ ╱                       ⎟
        ⎜⎪ ‾‾‾                     ⎟
        ⎜⎪y = 0                    ⎟
        ⎝⎩                         ⎠

f, n and z can be scalars or matrices of the same size. Scalar inputs are first expanded to matrices to match the size of the non-scalar inputs. Then ztrans will be applied elementwise.

syms n m k c z w u v
f = [n^2 exp(n); 1/factorial(k) kroneckerDelta(c)];
ztrans (f, [n m; k c], [z w; u v])
  ⇒ (sym 2×2 matrix)
      ⎡⎧        1                                              ⎤
      ⎢⎪   -1 - ─                  ⎛⎧    1           1     ⎞   ⎥
      ⎢⎪        z          1       ⎜⎪  ─────    for ─── < 1⎟   ⎥
      ⎢⎪───────────   for ─── < 1  ⎜⎪      1        │w│    ⎟   ⎥
      ⎢⎪          3       │z│      ⎜⎪  1 - ─               ⎟   ⎥
      ⎢⎪  ⎛     1⎞                 ⎜⎪      w               ⎟   ⎥
      ⎢⎪z⋅⎜-1 + ─⎟                 ⎜⎪                      ⎟   ⎥
      ⎢⎪  ⎝     z⎠                 ⎜⎪  ∞                   ⎟  n⎥
      ⎢⎨                           ⎜⎨ ___                  ⎟⋅ℯ ⎥
      ⎢⎪  ∞                        ⎜⎪ ╲                    ⎟   ⎥
      ⎢⎪ ___                       ⎜⎪  ╲    -m             ⎟   ⎥
      ⎢⎪ ╲                         ⎜⎪  ╱   w     otherwise ⎟   ⎥
      ⎢⎪  ╲    2  -n               ⎜⎪ ╱                    ⎟   ⎥
      ⎢⎪  ╱   n ⋅z     otherwise   ⎜⎪ ‾‾‾                  ⎟   ⎥
      ⎢⎪ ╱                         ⎜⎪m = 0                 ⎟   ⎥
      ⎢⎪ ‾‾‾                       ⎝⎩                      ⎠   ⎥
      ⎢⎩n = 0                                                  ⎥
      ⎢                                                        ⎥
      ⎢             1                                          ⎥
      ⎢             ─                                          ⎥
      ⎢             u                                          ⎥
      ⎣            ℯ                            1              ⎦
ztrans (f, [n m; k c], z)
  ⇒ (sym 2×2 matrix)
      ⎡⎧        1                                              ⎤
      ⎢⎪   -1 - ─                  ⎛⎧    1           1     ⎞   ⎥
      ⎢⎪        z          1       ⎜⎪  ─────    for ─── < 1⎟   ⎥
      ⎢⎪───────────   for ─── < 1  ⎜⎪      1        │z│    ⎟   ⎥
      ⎢⎪          3       │z│      ⎜⎪  1 - ─               ⎟   ⎥
      ⎢⎪  ⎛     1⎞                 ⎜⎪      z               ⎟   ⎥
      ⎢⎪z⋅⎜-1 + ─⎟                 ⎜⎪                      ⎟   ⎥
      ⎢⎪  ⎝     z⎠                 ⎜⎪  ∞                   ⎟  n⎥
      ⎢⎨                           ⎜⎨ ___                  ⎟⋅ℯ ⎥
      ⎢⎪  ∞                        ⎜⎪ ╲                    ⎟   ⎥
      ⎢⎪ ___                       ⎜⎪  ╲    -m             ⎟   ⎥
      ⎢⎪ ╲                         ⎜⎪  ╱   z     otherwise ⎟   ⎥
      ⎢⎪  ╲    2  -n               ⎜⎪ ╱                    ⎟   ⎥
      ⎢⎪  ╱   n ⋅z     otherwise   ⎜⎪ ‾‾‾                  ⎟   ⎥
      ⎢⎪ ╱                         ⎜⎪m = 0                 ⎟   ⎥
      ⎢⎪ ‾‾‾                       ⎝⎩                      ⎠   ⎥
      ⎢⎩n = 0                                                  ⎥
      ⎢                                                        ⎥
      ⎢             1                                          ⎥
      ⎢             ─                                          ⎥
      ⎢             z                                          ⎥
      ⎣            ℯ                            1              ⎦

As of sympy 1.10.1, sympy cannot find the closed form of one-sided Z-transform involving trigonometric functions and Heaviside step function.

syms n
ztrans (cos (n))
  ⇒ (sym)
        ∞
       ___
       ╲
        ╲    -n
        ╱   z  ⋅cos(n)
       ╱
       ‾‾‾
      n = 0
ztrans (heaviside (n - 3))
  ⇒ (sym)
        ∞
       ___
       ╲
        ╲    -n
        ╱   z  ⋅θ(n - 3)
       ╱
       ‾‾‾
      n = 0

Package: symbolic