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

Edit detail for Manipulating Expressions revision 5 of 6

1 2 3 4 5 6
Editor: Bill page
Time: 2014/08/11 13:25:43 GMT+0
Note: work-round rendering InputForm as LaTeX failure

added:
)set output tex off
)set output algebra on

added:
)set output tex on
)set output algebra off

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

fricas
ex1:=integrate(log(x)+x, x)

\label{eq1}{{2 \  x \ {\log \left({x}\right)}}+{{x}^{2}}-{2 \  x}}\over 2(1)
Type: Union(Expression(Integer),...)
fricas
)set output tex off
 
fricas
)set output algebra on
%::InputForm
(2) (/ (+ (* (* 2 x) (log x)) (+ (^ x 2) (* - 2 x))) 2)
Type: InputForm?
fricas
)set output tex on
 
fricas
)set output algebra off
ex2:=interpret((%::InputForm).2.2)

\label{eq2}2 \  x \ {\log \left({x}\right)}(2)
Type: Expression(Integer)

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

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

Then manipulating expressions looks like this:

fricas
op(1,ex1)
fricas
Compiling function op with type (PositiveInteger,Expression(Integer)
      ) -> Any

\label{eq3}{2 \  x \ {\log \left({x}\right)}}+{{x}^{2}}-{2 \  x}(3)
Type: Expression(Integer)
fricas
op(1,%)
fricas
Compiling function op with type (PositiveInteger,Any) -> Any

\label{eq4}2 \  x \ {\log \left({x}\right)}(4)
Type: Expression(Integer)
fricas
(op(1,op(1,ex1))-op(2,op(1,ex1)))/op(2,ex1)

\label{eq5}{{2 \  x \ {\log \left({x}\right)}}-{{x}^{2}}+{2 \  x}}\over 2(5)
Type: Expression(Integer)

Rules and Pattern Matching (from WesterProblemSet?)

Trigonometric manipulations---these are typically difficult for students

fricas
r:= cos(3*x)/cos(x)

\label{eq6}{\cos \left({3 \  x}\right)}\over{\cos \left({x}\right)}(6)
Type: Expression(Integer)

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

fricas
real(complexNormalize(r))

\label{eq7}-{2 \ {{\sin \left({x}\right)}^{2}}}+{2 \ {{\cos \left({x}\right)}^{2}}}- 1(7)
Type: Expression(Integer)

=> 2 cos(2 x) - 1

fricas
real(normalize(simplify(complexNormalize(r))))

\label{eq8}{2 \ {\cos \left({2 \  x}\right)}}- 1(8)
Type: Expression(Integer)

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

fricas
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)

\label{eq9}\begin{array}{@{}l}
\displaystyle
\left\{{{\cos \left({n \  x}\right)}\mbox{\rm = =}{-{{\sin \left({x}\right)}\ {\sin \left({{\left(n - 1 \right)}\  x}\right)}}+{{\cos \left({x}\right)}\ {\cos \left({{\left(n - 1 \right)}\  x}\right)}}}}, \right.
\
\
\displaystyle
\left.\:{{\sin \left({n \  x}\right)}\mbox{\rm = =}{{{\cos \left({x}\right)}\ {\sin \left({{\left(n - 1 \right)}\  x}\right)}}+{{\cos \left({{\left(n - 1 \right)}\  x}\right)}\ {\sin \left({x}\right)}}}}\right\} (9)
Type: Ruleset(Integer,Integer,Expression(Integer))

fricas
sincosAngles r

\label{eq10}-{3 \ {{\sin \left({x}\right)}^{2}}}+{{\cos \left({x}\right)}^{2}}(10)
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,

fricas
isMult(3*x)

\label{eq11}\left[{coef = 3}, \:{var = x}\right](11)
Type: Union(Record(coef: Integer,var: Kernel(Expression(Integer))),...)

but

fricas
isMult(x*y)

\label{eq12}\mbox{\tt "failed"}(12)
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

fricas
isPower(x**y)
There are no library operations named ** Use HyperDoc Browse or issue )what op ** to learn if there is any operation containing " ** " in its name.
Cannot find a definition or applicable library operation named ** with argument type(s) Variable(x) Variable(y)
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need.

whereas

fricas
isPower(x**10)
There are no library operations named ** Use HyperDoc Browse or issue )what op ** to learn if there is any operation containing " ** " in its name.
Cannot find a definition or applicable library operation named ** with argument type(s) Variable(x) PositiveInteger
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need.

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

We have:

fricas
isTimes(x*y*z)

\label{eq13}\left[ z , \: y , \: x \right](13)
Type: Union(List(Polynomial(Integer)),...)
fricas
isPlus(x+y+z*y)

\label{eq14}\left[{y \  z}, \: y , \: x \right](14)
Type: Union(List(Polynomial(Integer)),...)

Whereas

fricas
isTimes((x+y)*z)

\label{eq15}\mbox{\tt "failed"}(15)
Type: Union("failed",...)

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

fricas
((x+y)*z)::MPOLY([x,y,z],INT)

\label{eq16}{z \  x}+{z \  y}(16)
Type: MultivariatePolynomial?([x,y,z],Integer)

If you say:

fricas
isPlus((x+y)*z)

\label{eq17}\left[{y \  z}, \:{x \  z}\right](17)
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.