RunMat
GitHub

All functions

CategoryMath: Poly
Auto GPU

RunMat automatically offloads this function to the GPU when it estimates a speedup, without requiring explicit gpuArray inputs.

Learn more about Auto GPU →

polyval — Evaluate a polynomial at given points and optionally compute prediction intervals using polyfit output.

polyval(p, x) evaluates the polynomial defined by the coefficient vector p at every element of x. Coefficients follow MATLAB’s convention: p(1) is the highest-order term, p(end) is the constant term. Both real and complex inputs are supported, and the output matches the shape of x.

Syntax

y = polyval(p, x)
y = polyval(p, x, [], mu)
[y, delta] = polyval(p, x, S)
[y, delta] = polyval(p, x, S, mu)
  • p is the coefficient vector with the highest-degree term first: p = [2 -3 5] represents 2*x^2 - 3*x + 5.
  • x is the scalar, vector, matrix, or N-D array of points at which to evaluate the polynomial. The output y has the same shape as x.
  • S is an optional error-estimation structure produced by polyfit. It must contain the fields R, normr, and df. When requested with two outputs, polyval returns delta, the standard error (1σ prediction interval) at each point.
  • mu is an optional two-element centring/scaling vector [mean(x), std(x)] returned by polyfit. When supplied, the polynomial is evaluated at (x - mu(1)) / mu(2). Pass [] for S if you want centring without prediction intervals.
  • Returns y, the evaluated polynomial values, and optionally delta, the element-wise standard error from the fit.

How polyval works

  • Accepts scalar, vector, or N-D coefficient inputs that have at most one non-singleton dimension.
  • Logical and integer coefficients are promoted to double precision; complex coefficients are kept exactly.
  • When p and x are real-valued and a provider is registered, RunMat issues a Horner-series GPU kernel via RunMat Accelerate. Mixed or complex inputs fall back to the reference CPU implementation, with purely real outputs re-uploaded to the device when residency makes sense.
  • polyval(p, x, [], mu) applies centering and scaling parameters from polyfit, evaluating the polynomial at (x - mu(1)) / mu(2).
  • [y, delta] = polyval(p, x, S, mu) computes prediction intervals using the structure S produced by polyfit. RunMat mirrors MATLAB rules: S must contain the fields R, normr, and df, and the interval collapses to zeros when df <= 0 or normr == 0.
  • Empty inputs yield empty outputs; an empty coefficient vector represents the zero polynomial.
  • MATLAB-compatible errors are raised for non-numeric inputs, invalid mu vectors, or malformed S structures.

How RunMat runs polyval on the GPU

When a GPU provider is active, RunMat first attempts to evaluate the polynomial in device memory using a dedicated Horner kernel. Coefficients and inputs are uploaded automatically when required. If the call requests complex arithmetic, prediction intervals, or otherwise falls outside the GPU kernel’s contract, RunMat gathers to the host, executes the CPU implementation, and (for real-valued results) pushes the output back to the GPU so downstream kernels retain residency.

Examples

Evaluating a polynomial at scalar points

p = [2 -3 5];   % 2x^2 - 3x + 5
y = polyval(p, 4)

Expected output:

y = 21

Evaluating across a vector of inputs

p = [1 0 -2 1];
x = linspace(-2, 2, 5);
y = polyval(p, x)

Expected output:

y = [ -3   2   1   0   5 ]

Evaluating a polynomial over a matrix grid

[X, Y] = meshgrid(-1:1);
Z = polyval([1 -3 2], X + Y)

Expected output:

Z =
    12     6     2
     6     2     0
     2     0     0

Using centering and scaling parameters from polyfit

x = -2:2;
noise = 0.05 * randn(size(x));
[p, S, mu] = polyfit(x, sin(x) + noise, 3);
y = polyval(p, x, [], mu)

Expected output:

% y closely matches sin(x) + noise with polynomial smoothing

Computing prediction intervals with polyfit output

[p, S, mu] = polyfit((0:10)', exp((0:10)'/10), 3);
[y, delta] = polyval(p, 5, S, mu)

Expected output:

% y is the fitted value at x = 5
% delta is the 1σ prediction interval (standard error)

Handling complex coefficients and inputs

p = [1+2i, -3, 4i];
z = [-1+1i, 0, 1-2i];
y = polyval(p, z)

Expected output:

% Complex results that agree with MATLAB's polyval

Evaluating on a gpuArray input

x = gpuArray.linspace(-1, 1, 2048);
p = [1 0 1];
y = polyval(p, x);    % Runs on the GPU for real-valued inputs

Expected output:

y is a gpuArray because the result is real-valued.

How RunMat validates polyval

polyval evaluates the polynomial using the standard Horner recurrence, the same scheme MATLAB and NumPy's polyval use. The CPU implementation covers real and complex coefficients plus the polyfit prediction-interval form. The GPU path has a dedicated Horner kernel for real inputs and falls back to CPU for complex or prediction-interval calls.

See Correctness & Trust for the full methodology and coverage table.

FAQ

Do coefficients need to be a row vector?

No. They can be row or column vectors (or any N-D shape with a single non-singleton dimension). The output always matches the shape of x.

What kinds of inputs are accepted?

Numeric scalars, vectors, matrices, N-D arrays, logical arrays, and complex data are all accepted. Logical and integer inputs are promoted to double precision automatically.

How do centering (mu) parameters work?

RunMat mirrors MATLAB: the polynomial is evaluated at (x - mu(1)) / mu(2). The mu vector must contain at least two finite values, and the scale mu(2) must be non-zero.

Why does [y, delta] = polyval(...) require the structure S?

The prediction interval comes from the QR factorization stored in S by polyfit. The structure must include the fields R, df, and normr; without them the interval cannot be computed.

What happens when df <= 0 or normr == 0?

The prediction interval collapses to zeros (RunMat matches MATLAB and Octave here). This typically occurs when the fit is exact or when there are insufficient degrees of freedom.

Can I keep results on the GPU?

Yes. RunMat re-uploads real-valued results to the active provider after the host evaluation. Complex outputs stay on the host until providers add complex buffer uploads.

Does polyval support sparse inputs?

Not yet. Dense inputs (including gpuArray tensors) are supported today. Sparse support will arrive once RunMat's sparse infrastructure stabilises.

What does polyval do?

polyval(p, x) evaluates the polynomial whose coefficients are stored in p at every point in x. Coefficients are listed highest degree first, so p = [2 -3 5] represents 2*x^2 - 3*x + 5. The output has the same shape as x.

How does polyval evaluate a polynomial?

polyval uses Horner's method (nested multiplication), which rewrites p(1)*x^n + p(2)*x^(n-1) + ... + p(end) as ((p(1)*x + p(2))*x + p(3))*x + .... Horner's method needs only n multiplications and n additions and is more numerically stable than computing each power of x explicitly.

How do I use polyval with polyfit?

— Fit a polynomial with polyfit, then evaluate it on a smooth grid with polyval. For example:

p = polyfit(x, y, 3);
xfine = linspace(min(x), max(x), 200);
yfit = polyval(p, xfine);
plot(x, y, 'o', xfine, yfit, '-');

When polyfit returns [p, S, mu], pass them through (polyval(p, xfine, S, mu)) to get centring-aware evaluation and prediction intervals.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Signal

blackman · conv · conv2 · deconv · filter · hamming · hann · sawtooth · sinc · square

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how polyval 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 for free

Write code or describe what you want to compute. The sandbox is free, no account required.