# Simple and quick way to factor a polynomial

**URL:** <https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403>\
**Category:** New to Julia\
**Created:** [November 18, 2023, 12:34pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403 "2023-11-18T12:34:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loves\_Reddit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loves_reddit/32/204014_2.png) [@Loves\_Reddit](https://discourse.julialang.org/u/Loves_Reddit)\
**Post date:** [November 18, 2023, 12:34pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/1 "2023-11-18T12:34:17Z")

</div>

Hello I am looking for a simple and quick way I can enter a polynomial like; ax^3 + bx^2 + cx + d and get an output in the format (l + m)(n+ p)(q + r)

I am unable to find any quick way online

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 18, 2023, 12:58pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/2 "2023-11-18T12:58:51Z")

</div>

Factoring a polynomial and finding the roots of the polynomial are very mathematically close. The polynomial in the question is usually referred to as a cubic polynomial and has a special place in the history of mathematics. A reading of:

> **[Cubic equation](https://en.wikipedia.org/wiki/Cubic_equation)**
>
> In algebra, a cubic equation in one variable is an equation of the form
> in which a is nonzero.
> The solutions of this equation are called roots of the cubic function defined by the left-hand side of the equation. If all of the coefficients a, b, c, and d of the cubic equation are real numbers, then it has at least one real root (this is true for all odd-degree polynomial functions). All of the roots of the cubic equation can be found by the following means:
> The coefficients do not need to be re...

will answer most of the question and provide formulas to find the factorization which can be translated into code.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 18, 2023, 1:17pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/3 "2023-11-18T13:17:58Z")

</div>

Here is some Julia code to solve a cubic and factor. It demonstrates:

- Taking factored symbolic polynomial and expanding it.
- Extracting the coefficients a,b,c,d.
- Finding roots using PolynomialRoots package
- Assembling a factored form from the root.
- Note there may be issues of numerical accuracy.

```julia
julia> using PolynomialRoots

julia> using Symbolics

julia> @variables x
1-element Vector{Num}:
 x

julia> (2-x)*(5-x)*(7-x)
(7 - x)*(5 - x)*(2 - x)

julia> expand((2-x)*(5-x)*(7-x))
70 - 59x + 14(x^2) - (x^3)

julia> a,b,c,d = -1,14,-59,70
(-1, 14, -59, 70)

julia> roots([d,c,b,a])
3-element Vector{ComplexF64}:
 7.0 + 0.0im
 5.0 - 0.0im
 2.0 + 0.0im

julia> prod(x.-roots([d,c,b,a]))
(-2.0 + x)*(-5.0 + x)*(-7.0 + x)

julia> a*prod(x.-roots([d,c,b,a])) .|> real |> expand
70.0 - 59.0x + 14.0(x^2) - (x^3)

```

ADDENDUM:

Thanks to nsajko, I’ve learned about AMRVW, and with it:

```julia
julia> using AMRVW

julia> AMRVW.roots(Float64[d,c,b,a])
3-element Vector{ComplexF64}:
 2.0000000000000004 + 0.0im
  5.000000000000002 + 0.0im
  6.999999999999995 + 0.0im

julia> AMRVW.real_polynomial_roots(Float64[d,c,b,a]).real_roots
3-element Vector{Float64}:
 2.0000000000000004
 5.000000000000002
 6.999999999999995

```

AMRVW seems to be the better package to go to for finding polynomial roots.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 18, 2023, 2:34pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/4 "2023-11-18T14:34:44Z")

</div>

“…enter a polynomial like; ax^3 + bx^2 +cx+d”…

- Are you talking about a cubic polynomial, or
- Is this just an example of a polynomial, where you in reality ask for finding the form “(l+m)(n+p)(q+r)” [whatever that means – presumably you mean (x-x1)(x-x2)(x-x3)??]" of arbitrary order?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [November 18, 2023, 8:10pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/5 "2023-11-18T20:10:01Z")

</div>

> [@Dan](#):
>
> Finding roots using PolynomialRoots package

For finding polynomial roots numerically, the AMRVW [package](https://juliahub.com/ui/Packages/General/AMRVW) should be able to give much more accurate results. As of the latest release it also has special [support](https://github.com/jverzani/AMRVW.jl/pull/20) for friendlier handling of real polynomials specifically, in case that helps. The PolynomialRoots package is buggy and not very actively maintained.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 18, 2023, 9:46pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/6 "2023-11-18T21:46:34Z")

</div>

If the main focus is finding the roots:

```julia
import Polynomials as pol

julia> p = pol.Polynomial([1,2,0,3])
Polynomial(1 + 2*x + 3*x^3)

julia> roots(p)
3-element Vector{ComplexF64}:
 -0.40231993806281435 + 0.0im
  0.20115996903140715 - 0.8877289372825564im
  0.20115996903140715 + 0.8877289372825564im

```

If you want a string expression, you can always do as follows:

```julia
import Polynomials as pol

julia> p = pol.Polynomial([1,2,0,3])
Polynomial(1 + 2*x + 3*x^3)

julia> p_root = roots(p)
3-element Vector{ComplexF64}:
 -0.40231993806281435 + 0.0im
  0.20115996903140715 - 0.8877289372825564im
  0.20115996903140715 + 0.8877289372825564im

julia> p_string = ""
""

julia> for x0 in p_root
       p_string = p_string*"(x - ($x0))"
       end

julia> p_string
"(x - (-0.40231993806281435 + 0.0im))(x - (0.20115996903140715 - 0.8877289372825564im))(x - (0.20115996903140715 + 0.8877289372825564im))"

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 18, 2023, 10:13pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/7 "2023-11-18T22:13:02Z")

</div>

If you are using `Polynomials.jl`, there is also the `FactoredPolynomial` representation, which might fell more direct:

```julia
julia> using Polynomials

julia> x = variable()
Polynomial(x)

julia> p = convert(FactoredPolynomial, x^3 - 6x^2 + 11x - 6)
FactoredPolynomial((x - 1.0000000000000002) * (x - 3.0) * (x - 1.9999999999999996))

```

You might want to round here, as this polynomial should have integer roots:

```julia
julia> map(x -> round(x, digits=1), p)
FactoredPolynomial((x - 2.0) * (x - 3.0) * (x - 1.0))

```

As for `AMRVW` or `PolynomialRoots` or just `roots` from `Polynomials`, the faster and more accurate is usually `PolynomialRoots` until the polynomial gets quite large in degree, but the differences are usually quite modest.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [November 18, 2023, 10:16pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/8 "2023-11-18T22:16:56Z")

</div>

> [@j\_verzani](#):
>
> the faster and more accurate is usually `PolynomialRoots` until the polynomial gets quite large in degree

Maybe this would be true if the algorithm was implemented correctly (I’m not an expert), but PolynomialRoots seems to suffer from serious bugs, if you take a look at its bug tracker and related threads on this forum.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [November 18, 2023, 11:39pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/9 "2023-11-18T23:39:13Z")

</div>

For genuine symbolic multivariate factorization:

```julia
julia> using Nemo

julia> R, (x, y, z) = PolynomialRing(ZZ, ["x", "y", "z"]) # declare the list of variables

julia> factor(-3*x^2*y-6*x^2*z-3*x*y^2-5*x*y*z+2*x*z^2+y^2*z+2*y*z^2)
-1 * (x + y) * (y + 2*z) * (3*x - z)

```

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 2, 2024, 11:35am UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/10 "2024-06-02T11:35:56Z")

</div>

It looks like something has changed:

```julia
julia> using Nemo

julia> R, (x, y, z) = PolynomialRing(ZZ, ["x", "y", "z"])

ERROR: UndefVarError: `PolynomialRing` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

This is Nemo version 0.45.3.

@greatpet it would be great with an updated example.

---

<div class="post-metadata">

**Author:** ![Andreas\_Poulsen](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andreas\_Poulsen](https://discourse.julialang.org/u/Andreas_Poulsen)\
**Post date:** [June 2, 2024, 3:38pm UTC](https://discourse.julialang.org/t/simple-and-quick-way-to-factor-a-polynomial/106403/11 "2024-06-02T15:38:49Z")

</div>

It seems like `PolynomialRing` has been renamed to `polynomial_ring`. The following works:

```julia
julia> using Nemo
julia> R, (x, y, z) = polynomial_ring(ZZ, ["x", "y", "z"]);
julia> factor(-3*x^2*y-6*x^2*z-3*x*y^2-5*x*y*z+2*x*z^2+y^2*z+2*y*z^2)
-1 * (x + y) * (y + 2*z) * (3*x - z)

```
