square — Generate periodic square waves with MATLAB-compatible duty-cycle control.
y = square(t) generates a period-2*pi square wave sampled at t with outputs in {-1, +1}. y = square(t, duty) applies MATLAB-compatible percentage duty-cycle control over [0, 100].
Syntax
Y = square(t)
Y = square(t, duty)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
t | NumericArray | Yes | — | Sample times. |
duty | NumericScalar | No | 50 | Duty cycle percentage in [0, 100]. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Square-wave output sampled at t. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:square:InvalidInput | Primary input is not numeric-real tensor/scalar compatible. | square: expected numeric input |
RunMat:square:ComplexUnsupported | Primary input includes complex values. | square: input must be real; complex values are not supported |
RunMat:square:DutyInvalid | Duty argument is not a real numeric scalar. | square: duty must be a real numeric scalar in [0, 100] |
RunMat:square:DutyOutOfRange | Duty argument lies outside [0, 100] or is non-finite. | square: duty must be a finite scalar in [0, 100] |
RunMat:square:ArgCount | More than one optional argument is provided. | square: expected 1 or 2 arguments |
RunMat:square:InternalError | Internal tensor construction or GPU gather fails. | square: internal error |
How square 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. - Uses half-open interval semantics:
+1forphi/(2*pi) < duty/100,-1otherwise. duty = 0produces a constant-1waveform;duty = 100produces a constant+1waveform.- 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
dutyvalues outside[0, 100]and non-scalardutyarguments.
Examples
Default 50% duty cycle square wave
t = 0:pi/4:2*pi;
y = square(t)Expected output:
y = [1 1 1 1 -1 -1 -1 -1 1]25% duty cycle square wave
t = 0:pi/4:2*pi;
y = square(t, 25)Expected output:
y = [1 1 -1 -1 -1 -1 -1 -1 1]Constant outputs at the boundaries
y = square([0 pi 2*pi], 100)Expected output:
y = [1 1 1]Using square with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how square changes the result.
Run a small square example, explain the result, then change one input and compare the output.
FAQ
What happens exactly at the duty-cycle boundary?⌄
RunMat uses half-open semantics: the boundary phase belongs to the negative half. With the default 50% duty cycle, square(pi) returns -1. This matches MATLAB's documented behaviour.
How do I produce a constant +1 or -1?⌄
Pass duty = 100 for a constant +1 waveform and duty = 0 for a constant -1 waveform.
Can square accept complex inputs?⌄
No. square 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 square is executed, line by line, in Rust.
- View the source for square 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.