View all functions

CategoryMath: Poly

What does the roots function do in MATLAB / RunMat?

roots(p) returns the zeros of the polynomial whose coefficients are stored in p, with coefficients ordered from the highest power of x to the constant term. The result is always a column vector whose entries may be complex.

How does the roots function behave in MATLAB / RunMat?

  • Leading zeros in the coefficient vector are discarded before solving. If all coefficients are zero, the result is an empty column vector.
  • Constant polynomials (degree 0) produce an empty output because they have no finite roots.
  • Linear polynomials return the single solution -b/a. Higher-degree polynomials are solved via the eigenvalues of the companion matrix.
  • Real coefficients can generate complex conjugate root pairs. Small imaginary round-off terms are rounded to zero to match MATLAB formatting.
  • Input vectors can be row or column vectors. Higher-dimensional arrays are rejected.
  • Inputs may be real or complex. Logical and integer types are converted to double precision automatically.

roots Function GPU Execution Behaviour

RunMat gathers GPU-resident coefficient vectors to the host because the companion matrix eigenvalue computation presently runs only on the CPU. The output is produced on the host as well. When future providers supply a dedicated polynomial root solver, the builtin can be updated to keep residency on-device transparently.

Examples of using the roots function in MATLAB / RunMat

Finding roots of a quadratic polynomial

p = [1 -3 2];
r = roots(p);

Expected output:

r =
     2
     1

Computing roots that include repeated factors

p = [1 -2 1 0];   % (x - 1)^2 * x
r = roots(p);

Expected output:

r =
     1
     1
     0

Handling leading zeros in the coefficient vector

p = [0 0 1 -4];
r = roots(p);

Expected output:

r =
     4

Calculating complex roots from real coefficients

p = [1 0 1];
r = roots(p);

Expected output:

r =
   0.0000 + 1.0000i
   0.0000 - 1.0000i

Solving roots of a polynomial stored on the GPU

p = gpuArray([1 0 -9 0]);
r = roots(p);

Expected output:

r =
    3.0000
   -3.0000
         0

The coefficients are gathered automatically, so no manual gather call is required.

FAQ

What shape must the coefficient vector have?

roots accepts row vectors, column vectors, or 1-D arrays. Higher-dimensional tensors are rejected with an error.

How are leading zeros handled?

Leading zeros are removed before solving. If all coefficients are zero, roots returns an empty column vector.

Does roots preserve the data type of the coefficients?

Coefficients are promoted to double precision internally. The output is a double vector when all roots are real and a complex double vector otherwise.

Are the roots sorted?

Roots are returned in the order supplied by the eigenvalue computation (typically descending magnitude). MATLAB also does not sort the roots.

Can I run roots entirely on the GPU?

Not yet. RunMat gathers coefficients from the GPU, solves the companion matrix on the CPU, and returns a host-resident vector. When GPU providers add a polynomial root solver, this builtin will automatically route to it.

How does RunMat handle numerical round-off?

Small imaginary components (|imag| ≤ 1e-10·(1 + |real|)) are rounded to zero so that near-real roots are displayed as real numbers, matching MATLAB formatting.

See Also

polyval, polyfit, residue, roots documentation (MathWorks)

Source & Feedback