The computation of the exact zeroes of an univariate polynomial is a problem that is close to what we learned at school, but it is also a problem that can be used to explain some useful tricks.
We begin with a polynomial of degree 2, that we assign to the variable poly:
poly: x^2 - x - 12;
We obtain:
2 x - x - 12
To compute the zeroes, we use the function solve:
solutions: solve (poly=0, x);
[x = - 3, x = 4]
The answer is written as a list of two equations. When we convert these equation into terms, we can multiply them to obtain the initial polynomial.
We do this in several steps. First we rewrite the equations as terms:
map( lambda( [eq], lhs(eq) - rhs(eq)), solutions);
This is a bit tricky. map is used to apply a function to all elements of a list. The function is written as a lambda expression, in our example as:
lambda ([eq], lhs(eq) - rhs(eq))
A lambda-expression begins with the symbol lambda which is followed by parentheses. Within the parentheses the list of the formal parameters and a symbolic expression follow. The list of formal parameters is written in square brackets.
In our example we know that an equation will be assigned to the formal parameter when the function is invoked. For equations we can use the functions lhs and rhs to access the left hand side resp. the right hand side of an equation. We subtract the right hand side from the left hand side to obtain a term.
Evaluation of the map results in a list of two terms:
[x + 3, x - 4]
To verify that these terms are the factors of the polynomial poly, we have to multiply them. This requires again a trick: The function apply is used to apply an arithmetic operation to all elements of a list. To transform a list of terms into a product, we apply multiplication to the elements of that list:
apply("*", %);
(x - 4) (x + 3)
This is a polynomial in factored form. Expansion gives us the initial polynomial:
expand(%);
2 x - x - 12