Re: Going mad with pattern matching
"Bill Page" <[email protected]>
| Newsgroups | gmane.comp.mathematics.axiom.user |
|---|---|
| Message-ID | <[email protected]> |
Quoting Alasdair McAndrew:
> ... I have two questions; maybe you can answer them:
> 1) How do I include linearity in the pattern matching rules? At
> present, the command
> ztrans(2+3^n,nz)
>
> which should return the result
> 2z/(z-1)+z/(z-3)
> produces
> (2+3^n)z/(z-1).
> That is, the pattern matcher incorrectly applies the rule
> zt(a | freeOf?(a,n),n,z) == a*z/(z-1)
> even though 2+3^n is not free of n!
> 2) How do I force answers to be returned in factored form?
My advice is to avoid the use of pattern matching if at all
possible. It is rather poorly documented for the use for
which you are trying to put it - matching arguments of
an n-ary operator and I have found that it often gives me
rather unpredictable results. It is also not easily portable
to Axiom library code.
In the following web page:
http://wiki.axiom-developer.org/SandBoxZtransform
have written a draft version of z-transfrom using more
conventional means and using only one rule.
The following is the source code from the web page:
------
zt:=operator 'zt
ztransrules := rule
zt((a | freeOf?(a,n))^n,n,z) == z/(z-a)
Expr ==> Expression Integer
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(*,select(x+->not 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)
---------
The functions 'isPlus', 'isTimes', 'isPower' are used in
other parts of Axiom, for example when manipulating
trigonometric functions, unfortunately I cannot find any
documentation in the Axiom book or tutorials. But see
an example here:
http://wiki.axiom-developer.org/SandBoxManip
Please feel free to modify this code to suite your
purposes.
Regards,
Bill Page.