sqrt — Compute principal square roots elementwise with compatible real and complex behavior.
Y = sqrt(X) computes the principal square root of each element in X, preserving single precision for documented single inputs and promoting negative real values to the corresponding complex floating class.
Syntax
Y = sqrt(X)Inputs
X- Type
Any- Required
- Yes
- Description
- Single or double real/complex, logical, or character input; typed integer inputs require RunMat mode.
Returns
Y- Type
NumericArray- Description
- Elementwise principal square root, promoted to complex for negative real input.
Errors
RunMat:sqrt:InvalidInput- When
- Input cannot be interpreted as supported numeric, logical, character, or complex data.
- Message
sqrt: invalid input
RunMat:sqrt:Internal- When
- Internal tensor construction or provider interaction fails.
- Message
sqrt: internal error
RunMat:sqrt:TooManyOutputs- When
- More than one output is requested.
- Message
sqrt: too many output arguments
Behavior
sqrt(X) applies the principal square root elementwise and preserves the input shape. Documented single inputs remain single. Negative real values promote the complete result to the corresponding complex floating class.
Typed integer input is a RunMat extension. Each authoritative integer value must be exactly representable at the double-precision square-root boundary; values that would round are rejected. Logical values convert to double zeros and ones, while character arrays use their numeric code points and produce dense double results.
Complex input uses the principal branch of the complex square root. Signed zeros, infinities, and NaNs follow IEEE arithmetic.
GPU execution
RunMat can keep real input on the active provider when the provider implements the required reduction and square-root operations and the values are known to be nonnegative. Otherwise it gathers the value and computes the same principal result on the host. Explicit gpuArray and gather calls remain available when placement is part of the program.
sqrt is not fused into a real-only shader expression. Negative real input must promote the complete result to complex storage instead of producing NaN. Complex provider input is gathered through the value-aware boundary when the provider cannot produce the required complex representation directly.
Examples
Preserve single precision and array pages
input = reshape(single([4 9]), [1 1 2]);
result = sqrt(input);Take the square root of a positive scalar
y = sqrt(9)Expected output:
y = 3Take square roots elementwise across a matrix
A = [1 4 9; 16 25 36];
R = sqrt(A)Expected output:
R = [1 2 3; 4 5 6]Promote negative real input to a complex result
values = [-1 -4 9];
roots = sqrt(values)Expected output:
roots = [0.0000 + 1.0000i, 0.0000 + 2.0000i, 3.0000 + 0.0000i]Take square roots of provider-resident data
G = gpuArray([0 1; 4 9]);
out = sqrt(G);
result = gather(out)Expected output:
result = [0 1; 2 3]Take principal square roots of complex values
z = [3 + 4i, -1 + 2i];
w = sqrt(z)Expected output:
w = [2 + 1i, 0.7862 + 1.2720i]Use the numeric code points of a character array
C = 'AB';
numericRoots = sqrt(C)Expected output:
numericRoots = [8.0623 8.2462]Using sqrt with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how sqrt changes the result.
Run a small sqrt example, explain the result, then change one input and compare the output.
FAQ
Does sqrt return complex results for negative input?⌄
Yes. Negative real values produce imaginary components, and a mixed-sign array promotes to a complex result.
How does sqrt handle logical input?⌄
Logical arrays convert to double before the square root is applied, so true becomes 1 and false becomes 0.
Can a GPU provider handle negative input?⌄
RunMat gathers when the active provider cannot produce the required complex representation. The host calculation uses the same principal-root contract.
Does sqrt preserve shape?⌄
Yes. The output has the same shape as the input.
How are NaN and infinity handled?⌄
They follow IEEE square-root behavior: NaN remains NaN, positive infinity remains positive infinity, and negative infinity produces a positive imaginary infinity.
How are complex values near an axis handled?⌄
RunMat uses the principal complex branch and retains small nonzero components. Exact zero components are normalized to positive zero.
Can providers add direct complex support?⌄
Yes. Provider execution is selected through typed capabilities, so an implementation that supports the required complex representation can keep the operation resident.
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 · realsqrt · rescale · sign · single · 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 sqrt is executed, line by line, in Rust.
- View the source for sqrt 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.