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

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

fricas
ex1:=integrate(log(x)+x, x)
 (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)
 (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
 (3)
Type: Expression(Integer)
fricas
op(1,%)
fricas
Compiling function op with type (PositiveInteger,Any) -> Any
 (4)
Type: Expression(Integer)
fricas
(op(1,op(1,ex1))-op(2,op(1,ex1)))/op(2,ex1)
 (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)
 (6)
Type: Expression(Integer)

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

fricas
real(complexNormalize(r))
 (7)
Type: Expression(Integer)

=> 2 cos(2 x) - 1

fricas
real(normalize(simplify(complexNormalize(r))))
 (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)
 (9)
Type: Ruleset(Integer,Integer,Expression(Integer))

fricas
sincosAngles r
 (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)
 (11)
Type: Union(Record(coef: Integer,var: Kernel(Expression(Integer))),...)

but

fricas
isMult(x*y)
 (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)
 (13)
Type: Union(Record(val: Expression(Integer),exponent: Integer),...)

whereas

fricas
isPower(x^10)
 (14)
Type: Union(Record(val: Expression(Integer),exponent: Integer),...)

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

We have:

fricas
isTimes(x*y*z)
 (15)
Type: Union(List(Polynomial(Integer)),...)
fricas
isPlus(x+y+z*y)
 (16)
Type: Union(List(Polynomial(Integer)),...)

Whereas

fricas
isTimes((x+y)*z)
 (17)
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)
 (18)
Type: MultivariatePolynomial?([x,y,z],Integer)

If you say:

fricas
isPlus((x+y)*z)
 (19)
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:

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.