RunMat
GitHub

sawtooth — Generate a periodic sawtooth waveform with optional peak position.

y = sawtooth(t) generates a periodic sawtooth wave with period 2*pi at sample times t, ranging in [-1, 1]. y = sawtooth(t, xmax) controls the peak position using xmax in [0, 1], following MATLAB-compatible waveform conventions.

Syntax

Y = sawtooth(t)
Y = sawtooth(t, xmax)

Inputs

NameTypeRequiredDefaultDescription
tNumericArrayYesSample times.
xmaxNumericScalarNo1Peak position fraction in [0, 1].

Returns

NameTypeDescription
YNumericArraySawtooth output sampled at t.

Errors

IdentifierWhenMessage
RunMat:sawtooth:InvalidInputPrimary input is not numeric-real tensor/scalar compatible.sawtooth: expected numeric input
RunMat:sawtooth:ComplexUnsupportedPrimary input includes complex values.sawtooth: input must be real; complex values are not supported
RunMat:sawtooth:XmaxInvalidxmax argument is not a real numeric scalar.sawtooth: xmax must be a real numeric scalar in [0, 1]

How sawtooth works

  • Operates element-wise on scalars, vectors, matrices, and N-D tensors; preserves input shape.
  • Reduces input modulo 2*pi so the waveform is periodic across negative and positive sample times.
  • Returns -1 at the start of each period and approaches +1 just before the period boundary wraps when xmax = 1.
  • Returns a piecewise-linear triangle wave when xmax = 0.5, peaking at 1 at the centre of each period.
  • Returns a pure falling ramp when xmax = 0 and a pure rising ramp when xmax = 1.
  • Integer and logical inputs are promoted to double precision before evaluation.
  • Non-finite sample times (NaN, +Inf, -Inf) propagate as NaN element-wise, matching MATLAB.
  • Complex and text inputs are rejected with a builtin-scoped error.
  • Rejects xmax values outside [0, 1] and non-scalar xmax arguments.

Examples

Default rising sawtooth over two periods

t = 0:pi/2:4*pi;
y = sawtooth(t)

Expected output:

y = [-1 -0.5 0 0.5 -1 -0.5 0 0.5 -1]

Triangle wave via xmax = 0.5

t = linspace(0, 2*pi, 5);
y = sawtooth(t, 0.5)

Expected output:

y = [-1 0 1 0 -1]

Pure falling sawtooth via xmax = 0

y = sawtooth([0 pi 2*pi], 0)

Expected output:

y = [1 0 1]

Using sawtooth with coding agents

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

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

FAQ

Why does sawtooth(2*pi) return -1 instead of +1?

RunMat reduces the input modulo 2*pi, so 2*pi is folded back to the start of the next period. The default rising sawtooth therefore restarts at -1 exactly on each period boundary, matching MATLAB.

How do I produce a triangle wave?

Pass xmax = 0.5 so the peak sits at the centre of each period and the waveform ramps symmetrically up and back down.

Can sawtooth accept complex inputs?

No. sawtooth is defined on real samples and rejects complex inputs with a builtin-scoped error to match MATLAB.

Signal

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

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

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

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 sawtooth 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.