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

Edit detail for Manipulating Expressions revision 3 of 6

1 2 3 4 5 6
Editor: Bill Page
Time: 2008/08/21 07:40:59 GMT-7
Note: rewrite rules

removed:
-

added:
Rules and Pattern Matching (from WesterProblemSet)

  Trigonometric manipulations---these are typically difficult for students
\begin{axiom}
r:= cos(3*x)/cos(x)
\end{axiom}
 => cos(x)^2 - 3 sin(x)^2 or similar
\begin{axiom}
real(complexNormalize(r))
\end{axiom}
=> 2 cos(2 x) - 1
\begin{axiom}
real(normalize(simplify(complexNormalize(r))))
\end{axiom}

Use rewrite rules => cos(x)^2 - 3 sin(x)^2
\begin{axiom}
sincosAngles:= rule ( _
   cos((n | integer?(n)) * x) == _
      cos((n - 1)*x) * cos(x) - sin((n - 1)*x) * sin(x); _
   sin((n | integer?(n)) * x) == _
      sin((n - 1)*x) * cos(x) + cos((n - 1)*x) * sin(x) )
\end{axiom}
\begin{axiom}
sincosAngles r
\end{axiom}


The domain InputForm? can be quite useful for manipulating parts of expressions. For example

axiom
ex1:=integrate(log(x)+x, x)
LatexWiki Image(1)
Type: Union(Expression Integer,...)
axiom
%::InputForm
LatexWiki Image(2)
Type: InputForm?
axiom
ex2:=interpret((%::InputForm).2.2)
LatexWiki Image(3)
Type: Expression Integer

If you would like to do this with a more common type of expression and hide the details, you can define

axiom
op(n,x) == interpret((x::InputForm).(n+1))
Type: Void

Then manipulating expressions looks like this:

axiom
op(1,ex1)
axiom
Compiling function op with type (PositiveInteger,Expression Integer)
       -> Any
LatexWiki Image(4)
Type: Expression Integer
axiom
op(1,%)
axiom
Compiling function op with type (PositiveInteger,Any) -> Any
LatexWiki Image(5)
Type: Expression Integer
axiom
(op(1,op(1,ex1))-op(2,op(1,ex1)))/op(2,ex1)
LatexWiki Image(6)
Type: Expression Integer

Rules and Pattern Matching (from WesterProblemSet?)

Trigonometric manipulations---these are typically difficult for students

axiom
r:= cos(3*x)/cos(x)
LatexWiki Image(7)
Type: Expression Integer

=> cos(x)^2 - 3 sin(x)^2 or similar

axiom
real(complexNormalize(r))
LatexWiki Image(8)
Type: Expression Integer

=> 2 cos(2 x) - 1

axiom
real(normalize(simplify(complexNormalize(r))))
LatexWiki Image(9)
Type: Expression Integer

Use rewrite rules => cos(x)^2 - 3 sin(x)^2

axiom
sincosAngles:= rule ( _
   cos((n | integer?(n)) * x) == _
      cos((n - 1)*x) * cos(x) - sin((n - 1)*x) * sin(x); _
   sin((n | integer?(n)) * x) == _
      sin((n - 1)*x) * cos(x) + cos((n - 1)*x) * sin(x) )
LatexWiki Image(10)
Type: Ruleset(Integer,Integer,Expression Integer)

axiom
sincosAngles r
LatexWiki Image(11)
Type: Expression Integer

Other Operations

The domain FunctionSpace? includes the following operations:

    isExpt(p,f:Symbol) returns [x, n] if p = x**n and n <> 0 and x = f(a)
    isExpt(p,op:BasicOperator) returns [x, n] if p = x**n and n <> 0 and x = op(a)
    isExpt(p) returns [x, n] if p = x**n and n <> 0
    isMult(p) returns [n, x] if p = n * x and n <> 0
    isPlus(p) returns [m1,...,mn] if p = m1 +...+ mn and n > 1
    isPower(p) returns [x, n] if p = x**n and n <> 0
    isTimes(p) returns [a1,...,an] if p = a1*...*an and n > 1

If these conditions are not met, then the above operations return "failed".

For example,

axiom
isMult(3*x)
LatexWiki Image(12)
Type: Union(Record(coef: Integer,var: Kernel Expression Integer),...)

but

axiom
isMult(x*y)
LatexWiki Image(13)
Type: Union("failed",...)

In the context of Expression Integer, or Polynomial Integer the parameter n must be an Integer. The Symbol y is not an Integer.

Not exactly analogously

axiom
isPower(x**y)
LatexWiki Image(14)
Type: Union(Record(val: Expression Integer,exponent: Integer),...)

whereas

axiom
isPower(x**10)
LatexWiki Image(15)
Type: Union(Record(val: Expression Integer,exponent: Integer),...)

In the first case the Integer is assume to be 1.

We have:

axiom
isTimes(x*y*z)
LatexWiki Image(16)
Type: Union(List Polynomial Integer,...)
axiom
isPlus(x+y+z*y)
LatexWiki Image(17)
Type: Union(List Polynomial Integer,...)

Whereas

axiom
isTimes((x+y)*z)
LatexWiki Image(18)
Type: Union("failed",...)

That is because the expression is internally treated as a MultivariatePolynomial like this:

axiom
((x+y)*z)::MPOLY([x,y,z],INT)
LatexWiki Image(19)
Type: MultivariatePolynomial?([x,y,z]?,Integer)

If you say:

axiom
isPlus((x+y)*z)
LatexWiki Image(20)
Type: Union(List Polynomial Integer,...)

perhaps the result makes sense?

For some of the details of these operations I consulted the actual algebra code at:

http://axiom-wiki.newsynthesis.org/axiom--test--1/src/algebra/FspaceSpad

Click on pdf or dvi to see the documentation.

You can also enter expressions like isTimes in the search box on the upper right and see all the places in the algebra where this operation is defined and used.