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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
t | NumericArray | Yes | — | Sample times. |
xmax | NumericScalar | No | 1 | Peak position fraction in [0, 1]. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Sawtooth output sampled at t. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:sawtooth:InvalidInput | Primary input is not numeric-real tensor/scalar compatible. | sawtooth: expected numeric input |
RunMat:sawtooth:ComplexUnsupported | Primary input includes complex values. | sawtooth: input must be real; complex values are not supported |
RunMat:sawtooth:XmaxInvalid | xmax argument is not a real numeric scalar. | sawtooth: xmax must be a real numeric scalar in [0, 1] |
RunMat:sawtooth:XmaxOutOfRange | xmax argument lies outside [0, 1] or is non-finite. | sawtooth: xmax must be a finite scalar in [0, 1] |
RunMat:sawtooth:ArgCount | More than one optional argument is provided. | sawtooth: expected 1 or 2 arguments |
RunMat:sawtooth:InternalError | Internal tensor construction or GPU gather fails. | sawtooth: internal error |
How sawtooth works
- Operates element-wise on scalars, vectors, matrices, and N-D tensors; preserves input shape.
- Reduces input modulo
2*piso the waveform is periodic across negative and positive sample times. - Returns
-1at the start of each period and approaches+1just before the period boundary wraps whenxmax = 1. - Returns a piecewise-linear triangle wave when
xmax = 0.5, peaking at1at the centre of each period. - Returns a pure falling ramp when
xmax = 0and a pure rising ramp whenxmax = 1. - Integer and logical inputs are promoted to double precision before evaluation.
- Non-finite sample times (
NaN,+Inf,-Inf) propagate asNaNelement-wise, matching MATLAB. - Complex and text inputs are rejected with a builtin-scoped error.
- Rejects
xmaxvalues outside[0, 1]and non-scalarxmaxarguments.
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.
Related Math functions
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
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how sawtooth is executed, line by line, in Rust.
- View the source for sawtooth in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.