polyder — Differentiate polynomials in MATLAB and RunMat.

polyder differentiates polynomials represented by coefficient vectors. Single-input, product-rule, and rational-function forms follow MATLAB coefficient conventions and output semantics.

Syntax

d = polyder(p)
d = polyder(a, b)
[num, den] = polyder(u, v)

Inputs

NameTypeRequiredDefaultDescription
pAnyYesPolynomial coefficient vector.
aAnyYesFirst polynomial coefficient vector.
bAnyYesSecond polynomial coefficient vector.

Returns

NameTypeDescription
dAnyDerivative coefficient vector.
numAnyQuotient-rule numerator coefficients.
denAnyQuotient-rule denominator coefficients.

Returned values from polyder depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:polyder:InvalidArgumentInput arity/output mode combination is invalid.polyder: invalid argument
RunMat:polyder:InvalidInputInputs cannot be interpreted as numeric coefficient vectors.polyder: invalid input
RunMat:polyder:InternalRuntime fails while building derivative outputs or provider fallback paths.polyder: internal runtime failure

How polyder works

  • Accepts real or complex scalars, row vectors, column vectors, or empty vectors. Inputs with more than one non-singleton dimension raise MATLAB-compatible errors.
  • Logical and integer coefficients are promoted to double precision before differentiation.
  • Leading zeros are removed in outputs (unless the polynomial is identically zero, in which case a single zero coefficient is returned).
  • The orientation of the first input polynomial is preserved for the derivative of a single polynomial or a product; the denominator in the quotient rule preserves the orientation of v.
  • Calling polyder(p, a) with a single output applies the product rule. Capturing two outputs alongside two input polynomials returns the quotient-rule numerator and denominator (u' * v - u * v', v * v).
  • Empty inputs are treated as the zero polynomial.
  • When inputs live on the GPU (e.g., gpuArray) and the active provider exposes the polyder hooks, differentiation runs entirely on-device and returns trimmed GPU tensors. Providers that lack the hooks fall back to gathering coefficients to the host, executing the reference CPU implementation, and returning host-resident results. Explicit calls to gpuArray remain available if you need to force residency.

Does RunMat run polyder on the GPU?

When a provider advertises the polyder hooks (the in-process and WGPU backends both do), single polynomials and the product/quotient forms execute fully on the GPU. Outputs preserve the orientation of the leading input while trimming leading zeros exactly as the CPU path does. If the provider declines the request—because the coefficients are complex or the backend lacks support— RunMat automatically gathers the inputs and falls back to the CPU reference implementation to preserve MATLAB compatibility.

Examples

Differentiating a cubic polynomial

p = [3 -2 5 7];   % 3x^3 - 2x^2 + 5x + 7
dp = polyder(p)

Expected output:

dp = [9 -4 5]

Applying the product rule

p = [1 0 -2];    % x^2 - 2
a = [1 1];       % x + 1
dp = polyder(p, a);   % derivative of conv(p, a)

Expected output:

dp = [3 2 -2]

Differentiating a rational function

u = [1 0 -4];      % x^2 - 4
v = [1 -1];        % x - 1
[num, den] = polyder(u, v);    % derivative of (u / v)

Expected output:

num = [1 -2 4];
den = [1 -2 1]

Preserving column-vector orientation

p = [1; 0; -3];    % column vector coefficients
dp = polyder(p)

Expected output:

dp =
     2
     0

Differentiating complex-valued coefficients

p = [1+2i, -3, 4i];
dp = polyder(p)

Expected output:

dp = [2+4i, -3]

Working with gpuArray inputs

g = gpuArray([2 0 -5 4]);
dp = polyder(g);         % stays on the GPU when provider hooks are available
result = gather(dp)

Expected output:

result = [6 0 -5]

Using polyder with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how polyder changes the result.

Run a small polyder example, explain the result, then change one input and compare the output.

FAQ

What happens if I pass an empty coefficient vector?

The empty vector represents the zero polynomial. polyder([]) returns [0], and the product and quotient forms treat empty inputs as zeros.

Does polyder support column-vector coefficients?

Yes. The orientation of the first polynomial is preserved for single-polynomial and product derivatives. For the quotient rule, the numerator inherits the orientation of u and the denominator inherits the orientation of v.

How are leading zeros handled in the result?

Leading zeros are removed automatically to mirror MATLAB. If all coefficients cancel out, a single zero coefficient is returned.

Can I differentiate logical or integer coefficient vectors?

Yes. Logical and integer inputs are promoted to double precision before differentiation, matching MATLAB semantics.

How do I compute the derivative of a rational function?

Call [num, den] = polyder(u, v). The numerator and denominator are the coefficients of (u' * v - u * v') and v * v, respectively, with leading zeros removed.

Does the builtin launch GPU kernels?

Yes whenever the active acceleration provider implements the polyder hooks. The in-process and WGPU backends both execute the derivative on the device. Providers that lack the hooks—or inputs that require complex arithmetic—fall back to the CPU reference implementation, returning the result on the host. Wrap the output in gpuArray manually if you need to restore device residency in that case.

What if I request more than two outputs?

polyder only defines one or two outputs. Additional requested outputs are filled with 0, just as RunMat currently does for other MATLAB builtins whose extra outputs are ignored.

Are complex coefficients supported in the quotient rule?

Yes. Both the numerator and denominator are computed using full complex arithmetic, so mixed real/complex inputs work without additional steps.

Do I need to normalise or pad my coefficient vectors?

No. Provide coefficients exactly as MATLAB expects (highest power first). The builtin takes care of padding, trimming, and orientation.

How precise is the computation?

All arithmetic uses IEEE 754 double precision (f64), matching MATLAB’s default numeric type.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · 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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

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

Symbolic

digits · int · limit · sym · syms · vpa

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 polyder is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.