# 9.21 Expression¶

Expression is a constructor that creates domains whose objects can have very general symbolic forms. Here are some examples:

This is an object of type Expression Integer.

sin(x) + 3*cos(x)^2


 sin(x)+3cos(x)2

Type: Expression Integer

This is an object of type Expression Float.

tan(x) - 3.45*x


 tan(x)-3.45x

Type: Expression Float

This object contains symbolic function applications, sums, products, square roots, and a quotient.

(tan sqrt 7 - sin sqrt 11)^2 / (4 - cos(x - y))


 -tan(7)2+2sin(11)tan(7)-sin(11)2cos(y-x)-4

Type: Expression Integer

As you can see, Expression actually takes an argument domain. The coefficients of the terms within the expression belong to the argument domain. Integer and Float, along with Complex Integer and Complex Float are the most common coefficient domains.

The choice of whether to use a Complex coefficient domain or not is important since FriCAS can perform some simplifications on real-valued objects

log(exp x)@Expression(Integer)


x

Type: Expression Integer

... which are not valid on complex ones.

log(exp x)@Expression(Complex Integer)


log(ex)

Type: Expression Complex Integer

Many potential coefficient domains, such as AlgebraicNumber, are not usually used because Expression can subsume them.

sqrt 3 + sqrt(2 + sqrt(-5))


 -5+2+3

Type: AlgebraicNumber

% :: Expression Integer


 -5+2+3

Type: Expression Integer

Note that we sometimes talk about an object of type Expression. This is not really correct because we should say, for example, an object of type Expression Integer or an object of type Expression Float. By a similar abuse of language, when we refer to an expression in this section we will mean an object of type Expression R for some domain R.

The FriCAS documentation contains many examples of the use of Expression. For the rest of this section, we’ll give you some pointers to those examples plus give you some idea of how to manipulate expressions.

It is important for you to know that Expression creates domains that have category Field. Thus you can invert any non-zero expression and you shouldn’t expect an operation like factor to give you much information. You can imagine expressions as being represented as quotients of multivariate polynomials where the variables are kernels (see KernelXmpPage ). A kernel can either be a symbol such as x or a symbolic function application like sin(x + 4). The second example is actually a nested kernel since the argument to sin contains the kernel x.

height mainKernel sin(x + 4)


 2

Type: PositiveInteger

Actually, the argument to sin is an expression, and so the structure of Expression is recursive. KernelXmpPage demonstrates how to extract the kernels in an expression.

Use the HyperDoc Browse facility to see what operations are applicable to expression. At the time of this writing, there were 262 operations with 147 distinct name in Expression Integer. For example, numernumerExpression and denomdenomExpression extract the numerator and denominator of an expression.

e := (sin(x) - 4)^2 / ( 1 - 2*y*sqrt(- y) )


 -sin(x)2+8sin(x)-162y-y-1

Type: Expression Integer

numer e


 -sin(x)2+8sin(x)-16

Type: SparseMultivariatePolynomial(Integer,Kernel Expression Integer)

denom e


 2y-y-1

Type: SparseMultivariatePolynomial(Integer,Kernel Expression Integer)

Use DDExpression to compute partial derivatives.

D(e, x)


 (4ycos(x)sin(x)-16ycos(x))-y-2cos(x)sin(x)+8cos(x)4y-y+4y3-1

Type: Expression Integer

See ugIntroCalcDerivPage in Section ugIntroCalcDerivNumber for more examples of expressions and derivatives.

D(e, [x, y], [1, 2])


 (((-2304y7+960y4)cos(x)sin(x)+(9216y7-3840y4)cos(x))-y+(-960y9+2160y6-180y3-3)cos(x)sin(x)+(3840y9-8640y6+720y3+12)cos(x))((256y12-1792y9+1120y6-112y3+1)-y-1024y11+1792y8-448y5+16y2)

Type: Expression Integer

See ugIntroCalcLimitsPage in Section ugIntroCalcLimitsNumber and ugIntroSeriesPage in Section ugIntroSeriesNumber for more examples of expressions and calculus. Differential equations involving expressions are discussed in ugProblemDEQPage in Section ugProblemDEQNumber . Chapter 8 has many advanced examples: see ugProblemIntegrationPage in Section ugProblemIntegrationNumber for a discussion of FriCAS’s integration facilities.

When an expression involves no symbol kernels (for example, x), it may be possible to numerically evaluate the expression.

If you suspect the evaluation will create a complex number, use complexNumeric.

complexNumeric(cos(2 - 3*%i))


 -4.1896256909688072301+9.109227893755336598i

Type: Complex Float

If you know it will be real, use numeric.

numeric(tan 3.8)


 0.773556

Type: Float

The numeric operation will display an error message if the evaluation yields a calue with an non-zero imaginary part. Both of these operations have an optional second argument n which specifies that the accuracy of the approximation be up to n decimal places.

When an expression involves no symbolic application kernels, it may be possible to convert it a polynomial or rational function in the variables that are present.

e2 := cos(x^2 - y + 3)


 cos(y-x2-3)

Type: Expression Integer

e3 := asin(e2) - %pi/2


 -y+x2+3

Type: Expression Integer

e3 :: Polynomial Integer


 -y+x2+3

Type: Polynomial Integer

This also works for the polynomial types where specific variables and their ordering are given.

e3 :: DMP([x, y], Integer)


 x2-y+3

Type: DistributedMultivariatePolynomial([x,y],Integer)

Finally, a certain amount of simplication takes place as expressions are constructed.

sin %pi


 0

Type: Expression Integer

cos(%pi / 4)


 22

Type: Expression Integer

For simplications that involve multiple terms of the expression, use simplify.

tan(x)^6 + 3*tan(x)^4 + 3*tan(x)^2 + 1


 tan(x)6+3tan(x)4+3tan(x)2+1

Type: Expression Integer

simplify %


 1cos(x)6

Type: Expression Integer

See ugUserRulesPage in Section ugUserRulesNumber for examples of how to write your own rewrite rules for expressions.