realsqrt — Compute real square roots elementwise and reject values that require complex results.
Y = realsqrt(X) computes sqrt(X) for real nonnegative input. Unlike sqrt, it rejects negative real values and complex input instead of producing a complex result.
Syntax
Y = realsqrt(X)Inputs
X- Type
Any- Required
- Yes
- Description
- Real single or double numeric input.
Returns
Y- Type
NumericArray- Description
- Real elementwise square-root result.
Errors
RunMat:realsqrt:InvalidInput- When
- Input is not real single or double numeric data.
- Message
realsqrt: invalid input
RunMat:realsqrt:ComplexResult- When
- At least one real input value is negative and would require a complex result.
- Message
realsqrt: input must be nonnegative
RunMat:realsqrt:Internal- When
- Internal tensor construction or provider interaction fails.
- Message
realsqrt: internal error
RunMat:realsqrt:TooManyOutputs- When
- More than one output is requested.
- Message
realsqrt: too many output arguments
Behavior
realsqrt(X) applies the real square root elementwise and preserves shape. Only real single and double input is accepted; logical, character, fixed-width integer, and every complex representation are rejected before coercion or provider dispatch.
Sparse input remains sparse: stored values are square-rooted and implicit zeros remain zero. A negative stored or dense value raises a domain error because its square root would require a complex result. NaNs and positive infinities follow IEEE real square-root arithmetic.
GPU execution
RunMat rejects complex provider buffers first, then uses provider reduction to prove that real input is nonnegative. When the proof and square-root operation are available, the result remains provider-resident. Otherwise RunMat gathers and applies the same real-domain validation on the host.
realsqrt is not fused. A negative value must raise the documented domain error instead of becoming NaN inside a fused real shader.
Examples
Preserve single precision and array pages
input = reshape(single([4 9]), [1 1 2]);
result = realsqrt(input);Take the real square root of a scalar
y = realsqrt(9)Expected output:
y = 3Take real square roots elementwise across a matrix
A = [1 4 9; 16 25 36];
R = realsqrt(A)Expected output:
R = [1 2 3; 4 5 6]Reject values whose square root is not real
realsqrt([-1 4])Expected output:
Error using realsqrt
Input must be nonnegative.Preserve sparse storage
S = sparse([1 3], [1 2], [4 9], 3, 2);
R = realsqrt(S)Expected output:
R = sparse([1 3], [1 2], [2 3], 3, 2)Take real square roots of provider-resident data
G = gpuArray([0 1; 4 9]);
out = realsqrt(G);
result = gather(out)Expected output:
result = [0 1; 2 3]Using realsqrt with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how realsqrt changes the result.
Run a small realsqrt example, explain the result, then change one input and compare the output.
FAQ
How does realsqrt differ from sqrt?⌄
sqrt promotes negative real values to complex results. realsqrt stays in the real domain and raises an error when any value is negative.
Does realsqrt accept complex input with zero imaginary parts?⌄
No. Every complex representation is rejected because realsqrt requires real storage as well as real values.
Can realsqrt run on GPU data?⌄
Yes. RunMat uses provider execution after proving that a real provider buffer is nonnegative; otherwise it gathers and validates the same contract on the host.
Can realsqrt be fused?⌄
No. It remains a fusion boundary so negative input raises the documented error instead of producing a shader NaN.
What happens to NaN values?⌄
NaN remains NaN. A provider may need to gather when a reduction cannot establish the nonnegative-domain condition.
Related Math functions
Elementwise
abs · angle · besselj · bsxfun · complex · conj · double · eps · erf · erfcinv · exp · expm1 · flintmax · gamma · gammaln · heaviside · hypot · imag · int16 · int32 · int64 · int8 · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · rescale · sign · single · sqrt · times · typecast · uint16 · uint32 · uint64 · uint8
Optim
acceptancesa · annealingboltz · annealingfast · coneprog · crossoverarithmetic · crossoverheuristic · crossoverintermediate · crossoverlaplace · crossoverscattered · crossoversinglepoint · crossovertwopoint · eqnproblem · EquationProblem.solve · EquationProblem.subsasgn · EquationProblem.subsref · evaluate · fgoalattain · fitscalingprop · fitscalingrank · fitscalingshiftlinear · fitscalingtop · fminbnd · fmincon · fminimax · fminsearch · fminunc · fseminf · fsolve · fzero · ga · gacreationlinearfeasible · gacreationnonlinearfeasible · gacreationsobol · gacreationuniform · gacreationuniformint · gaplotbestf · gaplotbestindiv · gaplotdistance · gaplotexpectation · gaplotgenealogy · gaplotmaxconstr · gaplotrange · gaplotscorediversity · gaplotscores · gaplotselection · gaplotstopping · integerConstraint · integral · intlinprog · linprog · lsqcurvefit · lsqlin · LsqlinWarmStart.subsasgn · LsqlinWarmStart.subsref · lsqnonlin · lsqnonneg · mutationadaptfeasible · mutationgaussian · mutationpositivebasis · mutationpower · mutationuniform · optimexpr · optimget · OptimizationConstraint.evaluate · OptimizationConstraint.subsasgn · OptimizationConstraint.subsref · OptimizationExitflag.double · OptimizationExitflag.eq · OptimizationExitflag.ne · OptimizationExitflag.string · OptimizationExpression.acos · OptimizationExpression.acosh · OptimizationExpression.acot · OptimizationExpression.acoth · OptimizationExpression.acsc · OptimizationExpression.acsch · OptimizationExpression.asec · OptimizationExpression.asech · OptimizationExpression.asin · OptimizationExpression.asinh · OptimizationExpression.atan · OptimizationExpression.atanh · OptimizationExpression.cat · OptimizationExpression.cos · OptimizationExpression.cosh · OptimizationExpression.cot · OptimizationExpression.coth · OptimizationExpression.csc · OptimizationExpression.csch · OptimizationExpression.ctranspose · OptimizationExpression.cumprod · OptimizationExpression.cumsum · OptimizationExpression.diag · OptimizationExpression.diff · OptimizationExpression.dot · OptimizationExpression.eq · OptimizationExpression.evaluate · OptimizationExpression.exp · OptimizationExpression.ge · OptimizationExpression.horzcat · OptimizationExpression.ldivide · OptimizationExpression.le · OptimizationExpression.log · OptimizationExpression.mean · OptimizationExpression.minus · OptimizationExpression.mldivide · OptimizationExpression.mpower · OptimizationExpression.mrdivide · OptimizationExpression.mtimes · OptimizationExpression.norm · OptimizationExpression.plus · OptimizationExpression.power · OptimizationExpression.prod · OptimizationExpression.rdivide · OptimizationExpression.reshape · OptimizationExpression.sec · OptimizationExpression.sech · OptimizationExpression.sin · OptimizationExpression.sinh · OptimizationExpression.sqrt · OptimizationExpression.subsasgn · OptimizationExpression.subsref · OptimizationExpression.sum · OptimizationExpression.tan · OptimizationExpression.tanh · OptimizationExpression.times · OptimizationExpression.trace · OptimizationExpression.transpose · OptimizationExpression.uminus · OptimizationExpression.uplus · OptimizationExpression.vertcat · OptimizationExpression.zeros · OptimizationExpression.zerosLike · OptimizationProblem.solve · OptimizationProblem.subsasgn · OptimizationProblem.subsref · OptimizationVariable.subsasgn · OptimizationVariable.subsref · optimoptions · optimplotconstrviolation · optimplotfirstorderopt · optimplotfunccount · optimplotfval · optimplotfvalconstr · optimplotmilp · optimplotstepsize · optimplotx · optimproblem · optimset · optimvar · optimwarmstart · particleswarm · patternsearch · psplotbestf · psplotbestx · psplotfuncount · psplotmaxconstr · psplotmeshsize · pswcreationuniform · pswplotbestf · quad · quadprog · QuadprogWarmStart.subsasgn · QuadprogWarmStart.subsref · saplotbestf · saplotbestx · saplotf · saplotstopping · saplottemperature · saplotx · savemilpsolutions · secondordercone · selectionremainder · selectionroulette · selectionstochunif · selectiontournament · selectionuniform · simulannealbnd · solve · surrogateopt · surrogateoptplot · temperatureboltz · temperatureexp · temperaturefast
Trigonometry
acos · acosd · acosh · acotd · acscd · asecd · asin · asind · asinh · atan · atan2 · atan2d · atand · 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
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 · square · tripuls · unwrap · upsample · zplane
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Interpolation
griddedInterpolant · griddedInterpolant.subsref · interp1 · interp1q · interp2 · pchip · ppval · spline
Integer Division
Source you can read
Every RunMat function's source is public. Read exactly how realsqrt is executed, line by line, in Rust.
- View the source for realsqrt in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.