Algebraic Calculations

Symbolic Computation
One of the important features of the Wolfram System is that it can do symbolic, as well as numerical calculations. This means that it can handle algebraic formulas as well as numbers.
Here is a typical numerical computation:
This is a symbolic computation:
Numerical computation
Symbolic computation
Numerical and symbolic computations.
You can type any algebraic expression into the Wolfram System:
The Wolfram System automatically carries out basic algebraic simplifications. Here it combines and to get :
You can type in any algebraic expression, using the operators listed in "Arithmetic". You can use spaces to denote multiplication. Be careful not to forget the space in xy. If you type in xy with no space, the Wolfram Language will interpret this as a single symbol, with the name xy, not as a product of the two symbols x and y.
The Wolfram System rearranges and combines terms using the standard rules of algebra:
Here is another algebraic expression:
The function Expand multiplies out products and powers:
Factor does essentially the inverse of Expand:
When you type in more complicated expressions, it is important that you put parentheses in the right places. Thus, for example, you have to give the expression in the form x^(4y). If you leave out the parentheses, you get instead. It never hurts to put in too many parentheses, but to find out exactly when you need to use parentheses, look at "Operator Input Forms".
Here is a more complicated formula, requiring several parentheses:
When you type in an expression, the Wolfram System automatically applies its large repertoire of rules for transforming expressions. These rules include the standard rules of algebra, such as , together with much more sophisticated rules involving higher mathematical functions.
The Wolfram System uses standard rules of algebra to replace by :
The Wolfram System knows no rules for this expression, so it leaves the expression in the original form you gave:
The notion of transformation rules is a very general one. In fact, you can think of the whole of the Wolfram System as simply a system for applying a collection of transformation rules to many different kinds of expressions.
The general principle that the Wolfram System follows is simple to state. It takes any expression you input, and gets results by applying a succession of transformation rules, stopping when it knows no more transformation rules that can be applied.
Take any expression, and apply transformation rules until the result no longer changes.
The fundamental principle of the Wolfram System.
Transforming Algebraic Expressions
There are often many different ways to write the same algebraic expression. As one example, the expression can be written as . The Wolfram System provides a large collection of functions for converting between different forms of algebraic expressions.
Expand[expr]
multiply out products and powers, writing the result as a sum of terms
Factor[expr]
write expr as a product of minimal factors
Two common functions for transforming algebraic expressions.
Expand gives the "expanded form", with products and powers multiplied out:
Factor recovers the original form:
It is easy to generate complicated expressions with Expand:
Factor often gives you simpler expressions:
There are some cases, though, where Factor can give you more complicated expressions:
In this case, Expand gives the "simpler" form:
Simplifying Algebraic Expressions
There are many situations where you want to write a particular algebraic expression in the simplest possible form. Although it is difficult to know exactly what one means in all cases by the "simplest form", a worthwhile practical procedure is to look at many different forms of an expression, and pick out the one that involves the smallest number of parts.
Simplify[expr]
try to find the simplest form of expr by applying various standard algebraic transformations
FullSimplify[expr]
try to find the simplest form by applying a wide range of transformations
Simplifying algebraic expressions.
Simplify writes in factored form:
Simplify leaves in expanded form, since for this expression, the factored form is larger:
You can often use Simplify to "clean up" complicated expressions that you get as the results of computations.
Here is the integral of . Integrals are discussed in more detail in "Integration":
Differentiating the result from Integrate should give back your original expression. In this case, as is common, you get a more complicated version of the expression:
Simplify succeeds in getting back the original, simpler, form of the expression:
Simplify is set up to try various standard algebraic transformations on the expressions you give. Sometimes, however, it can take more sophisticated transformations to make progress in finding the simplest form of an expression.
FullSimplify tries a much wider range of transformations, involving not only algebraic functions, but also many other kinds of functions.
Simplify does nothing to this expression:
FullSimplify, however, transforms it to a simpler form:
For fairly small expressions, FullSimplify will often succeed in making some remarkable simplifications. But for larger expressions, it can become unmanageably slow.
The reason for this is that to do its job, FullSimplify effectively has to try combining every part of an expression with every other, and for large expressions the number of cases that it has to consider can be astronomically large.
Simplify also has a difficult task to do, but it is set up to avoid some of the most timeconsuming transformations that are tried by FullSimplify. For simple algebraic calculations, therefore, you may often find it convenient to apply Simplify quite routinely to your results.
In more complicated calculations, however, even Simplify, let alone FullSimplify, may end up needing to try a very large number of different forms, and therefore taking a long time. In such cases, you typically need to do more controlled simplification, and use your knowledge of the form you want to get to guide the process.
Some transformations used by Simplify and FullSimplify, for instance reduction with respect to equational assumptions, need to pick an order of variables. Therefore, results of simplification may depend on the names of symbols.
Reduction with respect to the equational assumption using the variable order leads to a simplification:
With the variable order , the expression is not simplified:
Putting Expressions into Different Forms
Complicated algebraic expressions can usually be written in many different ways. The Wolfram Language provides a variety of functions for converting expressions from one form to another.
In many applications, the most common of these functions are Expand, Factor, and Simplify. However, particularly when you have rational expressions that contain quotients, you may need to use other functions.
Expand[expr]
multiply out products and powers
ExpandAll[expr]
apply Expand everywhere
Factor[expr]
reduce to a product of factors
Together[expr]
put all terms over a common denominator
Apart[expr]
separate into terms with simple denominators
Cancel[expr]
cancel common factors between numerators and denominators
Simplify[expr]
try a sequence of algebraic transformations and give the smallest form of expr found
Functions for transforming algebraic expressions.
Here is a rational expression that can be written in many different forms:
Expand expands out the numerator, but leaves the denominator in factored form:
ExpandAll expands out everything, including the denominator:
Together collects all the terms together over a common denominator:
Apart breaks the expression apart into terms with simple denominators:
Factor factors everything, in this case reproducing the original form:
According to Simplify, this is the simplest way to write the original expression:
Getting expressions into the form you want is something of an art. In most cases, it is best simply to experiment, trying different transformations until you get what you want. Often you will be able to use palettes in the front end to do this.
When you have an expression with a single variable, you can choose to write it as a sum of terms, a product, and so on. If you have an expression with several variables, there is an even wider selection of possible forms. You can, for example, choose to group terms in the expression so that one or another of the variables is "dominant".
Collect[expr,x]
group together powers of x
FactorTerms[expr,x]
pull out factors that do not depend on x
Rearranging expressions in several variables.
Here is an algebraic expression in two variables:
This groups together terms in v that involve the same power of x:
This groups together powers of y:
This factors out the piece that does not depend on y:
As you have seen, even when you restrict yourself to polynomials and rational expressions, there are many different ways to write any particular expression. If you consider more complicated expressions, involving, for example, higher mathematical functions, the variety of possible forms becomes still greater. As a result, it is totally infeasible to have a specific function built into the Wolfram Language to produce each possible form. Rather, the Wolfram Language allows you to construct arbitrary sets of transformation rules for converting between different forms. Many Wolfram Language packages include such rules; the details of how to construct them for yourself are given in "Transformation Rules and Definitions".
There are nevertheless a few additional builtin Wolfram Language functions for transforming expressions.
TrigExpand[expr]
expand out trigonometric expressions into a sum of terms
TrigFactor[expr]
factor trigonometric expressions into products of terms
TrigReduce[expr]
reduce trigonometric expressions using multiple angles
TrigToExp[expr]
convert trigonometric functions to exponentials
ExpToTrig[expr]
convert exponentials to trigonometric functions
FunctionExpand[expr]
expand out special and other functions
ComplexExpand[expr]
perform expansions assuming that all variables are real
PowerExpand[expr]
transform (xy)p into xpyp, etc.
Some other functions for transforming expressions.
This expands out the trigonometric expression, writing it so that all functions have argument x:
This uses trigonometric identities to generate a factored form of the expression:
This reduces the expression by using multiple angles:
This expands the sine assuming that x and y are both real:
This does the expansion allowing x and y to be complex:
The transformations on expressions done by functions like Expand and Factor are always correct, whatever values the symbolic variables in the expressions may have. Sometimes, however, it is useful to perform transformations that are only correct for some possible values of symbolic variables. One such transformation is performed by PowerExpand.
The Wolfram Language does not automatically expand out noninteger powers of products:
PowerExpand does the expansion:
Simplifying with Assumptions
Simplify[expr,assum]
simplify expr with assumptions
Simplifying with assumptions.
The Wolfram Language does not automatically simplify this, since it is only true for some values of x:
is equal to for , but not otherwise:
This tells Simplify to make the assumption x>0, so that simplification can proceed:
No automatic simplification can be done on this expression:
If and are assumed to be positive, the expression can however be simplified:
Here is a simple example involving trigonometric functions:
Element[x,dom]
state that x is an element of the domain dom
Element[{x1,x2,},dom]
state that all the xi are elements of the domain dom
Reals
real numbers
Integers
integers
Primes
prime numbers
Some domains used in assumptions.
This simplifies assuming that is a real number:
This simplifies the sine assuming that is an integer:
With the assumptions given, Fermat's little theorem can be used:
This uses the fact that , but not , is real when is real:
Picking Out Pieces of Algebraic Expressions
Coefficient[expr,form]
coefficient of form in expr
Exponent[expr,form]
maximum power of form in expr
Part[expr,n]
or
expr[[n]]
n th term of expr
Functions to pick out pieces of polynomials.
Here is an algebraic expression:
This gives the coefficient of x in e:
Exponent[expr,y] gives the highest power of y that appears in expr:
This gives the fourth term in e:
You may notice that the function Part[expr,n] used to pick out the n th term in a sum is the same as the function described in "Manipulating Elements of Lists" for picking out elements in lists. This is no coincidence. In fact, as discussed in "Manipulating Expressions like Lists", every Wolfram Language expression can be manipulated structurally much like a list. However, as discussed in "Manipulating Expressions like Lists", you must be careful, because the Wolfram Language often shows algebraic expressions in a form that is different from the way it treats them internally.
Coefficient works even with polynomials that are not explicitly expanded out:
Numerator[expr]
numerator of expr
Denominator[expr]
denominator of expr
Functions to pick out pieces of rational expressions.
Here is a rational expression:
Denominator picks out the denominator:
Denominator gives 1 for expressions that are not explicit quotients:
Controlling the Display of Large Expressions
When you do symbolic calculations, it is quite easy to end up with extremely complicated expressions. Often, you will not even want to see the complete result of a computation.
If you end your input with a semicolon, the Wolfram Language will do the computation you asked for, but will not display the result. You can nevertheless use % or Out[n] to refer to the result.
By default, the Wolfram System front end will display any outputs which are excessively large in a shortened form inside an interface which allows you to refine the display of the output.
The Wolfram System shows this output with 5138 of the terms omitted:
The Show Less and Show More buttons allow you to decrease or increase the level of detail to which the Wolfram System shows the expression. The Show Full Output button removes the interface entirely and displays the full result, but the result may take considerable time to display. The default threshold size at which this feature starts working may be set using the Set Size Limit option, which opens the Preferences dialog to the panel with the appropriate setting.
The large output suppression feature is implemented using the Wolfram Language function Short. You can use Short directly for finer control over the display of expressions. You can also use it for outputs that are not large enough to be suppressed by the default suppression scheme.
Ending your input with ; stops the Wolfram Language from displaying the complicated result of the computation:
You can still refer to the result as %. //Short displays a oneline outline of the result. The <<n>> stands for n terms that have been left out:
This shows a threeline version of the expression. More parts are now visible:
This gives the total number of terms in the sum:
command;
execute command, but do not print the result
expr//Short
show a oneline outline form of expr
Short[expr,n]
show an nline outline of expr
Some ways to shorten your output.