Navigation

In this tutorial we present examples from Wester’s article concerning
comparison and critique of mathematical abilities of several computer
algebra systems (see [Wester1999]). All the examples are related to
polynomial and algebraic computations and SymPy specific remarks were
added to all of them.

To obtain a factorization of a polynomial use factor() function.
By default factor() returns the result in unevaluated form, so the
content of the input polynomial is left unexpanded, as in the following
example:

>>> factor(6*x-10)2⋅(3⋅x - 5)

To achieve the same effect in a more systematic way use primitive()
function, which returns the content and the primitive part of the input
polynomial:

>>> primitive(6*x-10)(2, 3⋅x - 5)

Note

The content and the primitive part can be computed only over a ring. To
simplify coefficients of a polynomial over a field use monic().

To test if two polynomials have a root in common we can use resultant()
function. The theory says that the resultant of two polynomials vanishes if
there is a common zero of those polynomials. For example:

As the result we got a splitting factorization of f with monic factors
(this is a general rule when computing in a field with SymPy). The gaussian
keyword is useful for improving code readability, however the same result can
be computed using more general syntax:

Unfortunately nothing interesting happened. This is because by default SymPy
treats \(\sqrt{2}\) as a generator, obtaining a bivariate polynomial for the
numerator. To make cancel() recognize algebraic properties of \(\sqrt{2}\),
one needs to use extension keyword:

>>> cancel(f/g,extension=True) 2x - 2⋅x - 3──────────── ___ x - ╲╱ 2

Setting extension=True tells cancel() to find minimal algebraic
number domain for the coefficients of f/g. The automatically inferred
domain is \(\mathbb{Q}(\sqrt{2})\). If one doesn’t want to rely on automatic
inference, the same result can be obtained by setting the extension
keyword with an explicit algebraic number:

Currently SymPy can factor polynomials into irreducibles over various domains,
which can result in a splitting factorization (into linear factors). However,
there is currently no systematic way to infer a splitting field (algebraic
number field) automatically. In future the following syntax will be
implemented:

Note this is different from extension=True, because the later only tells how
expression parsing should be done, not what should be the domain of computation.
One can simulate the split keyword for several classes of polynomials using
solve() function.

We want to compute factorization of f. To do this we use factor as
usually, however we note that the polynomial in consideration is already
in expanded form, so we can tell the factorization routine to skip
expanding f:

>>> factor(f,expand=False) 20⎛ 2 3⎞⎝x - 2⋅y + 3⋅z ⎠

The default in sympy.polys is to expand all expressions given as
arguments to polynomial manipulation functions and Poly class.
If we know that expanding is unnecessary, then by setting expand=False
we can save quite a lot of time for complicated inputs. This can be really
important when computing with expressions like:

To compute a reduced Gröbner basis for a set of polynomials use
groebner() function. The function accepts various monomial
orderings, e.g.: lex, grlex and grevlex, or a user
defined one, via order keyword. The lex ordering is the
most interesting because it has elimination property, which means
that if the system of polynomial equations to groebner() is
zero-dimensional (has finite number of solutions) the last element
of the basis is a univariate polynomial. Consider the following example: