RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • blackman
    • butter
    • buttord
    • cheb2ord
    • conv
    • conv2
    • deconv
    • downsample
    • envelope
    • filter
    • filtfilt
    • fir1
    • freqz
    • gauspuls
    • hamming
    • hann
    • hilbert
    • periodogram
    • pulstran
    • pwelch
    • rectpuls
    • resample
    • sawtooth
    • sinc
    • spectrogram
    • square
    • tripuls
    • unwrap
    • upsample
    • zplane

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

NameTypeRequiredDefaultDescription
tNumericArrayYes—Sample times.
dutyNumericScalarNo50Duty cycle percentage in [0, 100].

Returns

NameTypeDescription
YNumericArraySquare-wave output sampled at t.

Errors

IdentifierWhenMessage
RunMat:square:InvalidInputPrimary input is not numeric-real tensor/scalar compatible.square: expected numeric input
RunMat:square:ComplexUnsupportedPrimary input includes complex values.square: input must be real; complex values are not supported
RunMat:square:DutyInvalidDuty argument is not a real numeric scalar.square: duty must be a real numeric scalar in [0, 100]
RunMat:square:DutyOutOfRangeDuty argument lies outside [0, 100] or is non-finite.square: duty must be a finite scalar in [0, 100]
RunMat:square:ArgCountMore than one optional argument is provided.square: expected 1 or 2 arguments
RunMat:square:InternalErrorInternal 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*pi so the waveform is periodic across negative and positive sample times.
  • Uses half-open interval semantics: +1 for phi/(2*pi) < duty/100, -1 otherwise.
  • duty = 0 produces a constant -1 waveform; duty = 100 produces a constant +1 waveform.
  • MATLAB-compatible calls use single or double inputs for t and duty. In runmat compatibility mode, supported integer and logical arguments are accepted as a RunMat extension and 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 duty values outside [0, 100] and non-scalar duty arguments.

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

Signal

blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · tripuls · unwrap · upsample · zplane

Elementwise

abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · typecast · uint16 · uint32 · uint8

Trigonometry

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

Reduction

all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var

Structure

bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

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

Optim

coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimoptions · optimset · quad · secondordercone

Ops

cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftn · fftshift · ifft · ifft2 · ifftn · ifftshift

Interpolation

griddedInterpolant · interp1 · interp1q · interp2 · pchip · ppval · spline

Discrete

lcm · primes

Ode

ode15s · ode23 · ode45

Poly

polyder · polyfit · polyint · polyval · roots

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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How square works
  • Examples
  • Default 50% duty cycle square wave
  • 25% duty cycle square wave
  • Constant outputs at the boundaries
  • Using square with coding agents
  • FAQ
  • Related Math functions
  • Signal
  • Elementwise
  • Trigonometry
  • Reduction
  • Structure
  • Rounding
  • Factor
  • Solve
  • Optim
  • Ops
  • Symbolic
  • Fft
  • Interpolation
  • Discrete
  • Ode
  • Poly
  • Open-source implementation
  • About RunMat