login  home  contents  what's new  discussion  bug reports help  links  subscribe  changes  refresh  edit

Alasdair McAndrew? writes:

    I'd be grateful for a little help here!  (Then I'll see if I can use the
z-transform to solve some difference equations.)


## Ref:

On 29 May 2007 14:21:26 +0200 Martin Rubey wrote:

It seems that you hit a bug, but fortunately, there is an easy workaround. The problem is with rules of the form:

  rule ...a...b... | p(a,b) == ...


It seems that in this case, the predicate p is never tested, who knows why?

The workaround is to use the "suchThat" function.

fricas
Expr ==> Expression Integer
Type: Void
fricas
zt:=operator 'zt
 (1)
Type: BasicOperator?
fricas
ztransrules :=  ruleset([                                            _
suchThat(rule zt(a^n,n,z) == z/(z-a),                              _
[a, n], l +-> freeOf?(l.1, l.2)),                    _
suchThat(rule zt(cos(a*n),n,z) == z*(z-cos(a))/(1-2*z*cos(a)+z^2), _
[a, n], l +-> freeOf?(l.1, l.2)),                    _
suchThat(rule zt(sin(a*n),n,z) == z*sin(a)/(1-2*z*cos(a)+z^2),     _
[a, n], l +-> freeOf?(l.1, l.2))                     _
])$Ruleset(Integer, Integer, Expr)  (2) Type: Ruleset(Integer,Integer,Expression(Integer)) We only need to use a rules to handle the case of a^n, sin and cos, but similar rules could be written for the rest or we can do the same thing by common recursive methods: fricas ztrans(f:Expr,n:Symbol,z:Symbol):Expr == freeOf?(f,n) => f*z/(z-1) fs:= isPlus f; not (fs case "failed") => reduce(+,map(x+->ztrans(x,n,z),fs::List Expr)) fp:= isTimes f; not (fp case "failed") => reduce(*,select(x+->freeOf?(x,n),fp::List Expr))* _ ztrans(reduce(*,remove(x+->freeOf?(x,n),fp::List Expr)),n,z) fx:=isPower f; if not (fx case "failed") then fr:=fx::Record(val:Expr,exponent:Integer) k:=fr.exponent if fr.val=n and k>0 then return (-1)^k*limit(D(z/(z-exp(-x)),[x for i in 1..k]),x=0)::Expression Integer ztransrules zt(f,n,z) Function declaration ztrans : (Expression(Integer), Symbol, Symbol) -> Expression(Integer) has been added to workspace. Type: Void On Tue, 29 May 2007 09:19:42 +1000 Alasdair McAndrew? wrote: 1) The commands: fricas ztrans(2+3^n,n,z) fricas Compiling function ztrans with type (Expression(Integer), Symbol, Symbol) -> Expression(Integer)  (3) Type: Expression(Integer) fricas --should return the result: 2*z/(z-1) + z/(z-3)  (4) Type: Fraction(Polynomial(Integer)) fricas ztrans(0,n,z)  (5) Type: Expression(Integer) fricas --should return the result: 0  (6) Type: NonNegativeInteger? fricas ztrans((-1)^n,n,z)  (7) Type: Expression(Integer) fricas --should return the result: z/(z+1)  (8) Type: Fraction(Polynomial(Integer)) fricas ztrans(1,n,z)  (9) Type: Expression(Integer) fricas --should return the result: z/(z-1)  (10) Type: Fraction(Polynomial(Integer)) fricas ztrans(n,n,z)  (11) Type: Expression(Integer) fricas --should return the result: z/(z-1)^2  (12) Type: Fraction(Polynomial(Integer)) fricas ztrans(n^2,n,z)  (13) Type: Expression(Integer) fricas --should return the result: z*(z+1)/(z-1)^3  (14) Type: Fraction(Polynomial(Integer)) fricas ztrans(n^3,n,z)  (15) Type: Expression(Integer) fricas --should return the result: z*(z^2+4*z+1)/(z-1)^4  (16) Type: Fraction(Polynomial(Integer)) fricas ztrans(b^n,n,z)  (17) Type: Expression(Integer) fricas --should return the result: z/(z-b)  (18) Type: Fraction(Polynomial(Integer)) fricas ztrans(cos(b*n),n,z)  (19) Type: Expression(Integer) fricas --should return the result: z*(z-cos(b))/(1-2*z*cos(b)+z^2)  (20) Type: Expression(Integer) fricas ztrans(sin(b*n),n,z)  (21) Type: Expression(Integer) fricas --should return the result: z*sin(b)/(1-2*z*cos(b)+z^2)  (22) Type: Expression(Integer) 2) How do I force answers to be returned in factored form? fricas ( x+->factor(numer(x))/factor(denom(x)) ) ztrans(2+3^n,n,z)  (23) Type: Fraction(Factored(SparseMultivariatePolynomial?(Integer,Kernel(Expression(Integer))))) Bill Page wrote: Reduce does z-transforms http://www.uni-koeln.de/REDUCE/3.6/doc/ztrans/ztrans.html  load_package ztrans; *** binomial already defined as operator *** ~f already defined as operator reduce  ztrans((-1)^n*n^2,n,z); reduce  ztrans(cos(n*omega*t),n,z); reduce  ztrans(cos(b*(n+2))/(n+2),n,z); reduce  ztrans(n*cos(b*n)/factorial(n),n,z); reduce  ztrans(sum(1/factorial(k),k,0,n),n,z); reduce  operator f$ ztrans((1+n)^2*f(n),n,z); reduce

Inverse z-transforms

 invztrans((z^2-2*z)/(z^2-4*z+1),z,n); reduce
 invztrans(z/((z-a)*(z-b)),z,n); reduce
 invztrans(z/((z-a)*(z-b)*(z-c)),z,n); reduce
 invztrans(z*log(z/(z-a)),z,n); reduce
 invztrans(e^(1/(a*z)),z,n); reduce
 invztrans(z*(z-cosh(a))/(z^2-2*z*cosh(a)+1),z,n); reduce

 Subject:   Be Bold !! ( 15 subscribers )