RunMat
GitHub

roots — Compute the roots of a polynomial specified by its coefficients, matching MATLAB semantics including complex output.

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 roots works in 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.

How roots runs on the GPU

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

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

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.

These functions work well alongside roots. Each page has runnable examples you can try in the browser.

polyval, polyfit, roots, polyder, polyint

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how roots works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.