ldivide — Compute element-wise left division in MATLAB and RunMat.
ldivide(A, B) (or A .\ B) divides each element of B by the corresponding element of A. Broadcasting, complex handling, and output-shape behavior follow MATLAB semantics.
Syntax
C = ldivide(A, B)
C = ldivide(A, B, "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left divisor operand. |
B | Any | Yes | — | Right dividend operand. |
like | StringScalar | Yes | — | Literal string "like". |
prototype | LikePrototype | Yes | — | Output class/device prototype. |
Returns
| Name | Type | Description |
|---|---|---|
C | NumericArray | Elementwise left quotient result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:ldivide:InvalidArgument | Optional arguments are malformed or unsupported. | ldivide: invalid argument |
RunMat:ldivide:InvalidInput | Operands or prototypes cannot be converted into supported numeric/logical forms. | ldivide: invalid input |
RunMat:ldivide:SizeMismatch | Operands are not broadcast-compatible. | ldivide: array sizes are not compatible for broadcasting |
RunMat:ldivide:Internal | Provider interaction, gather/upload, or internal tensor construction failed. | ldivide: internal error |
How ldivide works
- Supports real, complex, logical, and character inputs; logical and character data are promoted to double precision before division.
- Implicit expansion follows MATLAB rules: singleton dimensions expand automatically, while mismatched non-singleton extents raise MATLAB-compatible size errors.
- Complex operands use the analytic continuation
B ./ A, propagatingNaNandInfexactly as MATLAB does. - Empty shapes propagate cleanly—if the broadcasted output has a zero dimension, the result is empty with the expected shape.
- All eight integer classes are supported. Integer operands must share a class, or the other operand must be scalar double; the result preserves the integer class, rounds to nearest with half ties away from zero, and saturates on overflow. Complex integer arithmetic is rejected.
- The optional
'like'prototype makes the result adopt the residency (host or GPU) and numeric flavour of the prototype. This form is a RunMat extension and is rejected while strict compatibility mode disables extensions.
Does RunMat run ldivide on the GPU?
RunMat first validates integer admission and operand metadata, then uses the exact common owner’s elem_div(B, A) path for compatible resident floating-point or same-class integer pairs, including provider-supported implicit expansion. A resident tensor with a host double scalar uses scalar_div or scalar_rdiv for floating data; an exactly representable integral scalar paired with resident integer data is uploaded in that integer class and evaluated through elem_div. Every provider result is checked for shape, owner, device, storage, class, precision, and non-aliasing. Unsupported or mixed cases gather automatically, execute on the host, and restore the class-preserving result to the original provider; explicit residency must be preserved. The fusion planner can still fuse supported floating elementwise expressions.
GPU memory and residency
You usually do not need to call gpuArray manually. RunMat’s auto-offload planner keeps tensors on the GPU whenever provider kernels cover the operation. When a provider fallback happens, the runtime gathers automatically, computes the class-preserving answer, and restores it to the original provider. Explicit gpuArray inputs must remain resident; the optional 'like' form is a separately gated RunMat extension.
Examples
Left-dividing a vector by a scalar
A = 2;
B = [4 6 8];
Q = ldivide(A, B)Expected output:
Q = [2 3 4]Broadcasting between column divisors and row numerators
A = (1:3)'; % column of divisors
B = [10 20 40]; % row of numerators
M = ldivide(A, B); % implicit expansionExpected output:
M =
10.0000 20.0000 40.0000
5.0000 10.0000 20.0000
3.3333 6.6667 13.3333Element-wise left division of complex values
A = [1+2i, 3-4i];
B = [2-1i, -1+1i];
Z = ldivide(A, B)Expected output:
Z =
0.0000 - 1.0000i -0.2800 - 0.0400iDividing character codes by a scalar
A = 'ABC';
B = 2;
codes = ldivide(A, B)Expected output:
codes = [0.0308 0.0303 0.0301]Computing reciprocals with ldivide
A = [1 2 4 8];
B = 1;
R = ldivide(A, B); % equivalent to 1 ./ AExpected output:
R = [1 0.5 0.25 0.125]Keeping results on the GPU with 'like'
proto = gpuArray.zeros(1, 1);
A = gpuArray([2 4 8 16]);
B = gpuArray([4 8 16 32]);
deviceResult = ldivide(A, B, 'like', proto);
hostCheck = gather(deviceResult)Expected output:
deviceResult =
1x4 gpuArray
2 2 2 2
hostCheck = [2 2 2 2]Using ldivide with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how ldivide changes the result.
Run a small ldivide example, explain the result, then change one input and compare the output.
FAQ
Does ldivide support MATLAB implicit expansion?⌄
Yes. Singleton dimensions expand automatically; otherwise incompatible shapes raise MATLAB-style errors.
What numeric type does ldivide return?⌄
Floating inputs preserve their supported floating flavour, mixed or complex inputs return the corresponding complex result, and logical and character inputs promote before division. Typed-integer division preserves the integer class under the documented same-class/scalar-double rules.
How does ldivide handle division by zero?⌄
finite ./ 0 yields signed infinities, and 0 ./ 0 becomes NaN, matching MATLAB and IEEE-754 behaviour.
Can I divide gpuArrays by host scalars?⌄
Yes. Numeric scalars stay on device through scalar_div/scalar_rdiv. Non-numeric host scalars trigger a gather-then-divide fallback.
Does ldivide preserve gpuArray residency after a fallback?⌄
Yes. Unsupported provider cases gather automatically, compute with MATLAB-compatible semantics, and restore the class-preserving result to the original provider. Explicit gpuArray inputs must produce resident output or the call errors; automatic residency may return to host only when safe class preservation on the provider is unavailable.
How do I keep the result on the GPU?⌄
Provide a real gpuArray prototype: ldivide(A, B, 'like', gpuArray.zeros(1,1)). The runtime re-uploads the host result when necessary.
How are empty arrays handled?⌄
Empty operands propagate cleanly—the output shape is the broadcasted shape, and the data vector is empty.
Are integers and logicals supported?⌄
Yes. Logical inputs promote before division. Typed integers preserve their integer class, use nearest rounding with half ties away from zero, and saturate; incompatible integer classes and complex integer arithmetic are rejected.
Can I mix real and complex operands?⌄
Absolutely. Mixed cases return complex doubles with full MATLAB semantics.
Related Math functions
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · 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
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
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
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ldivide is executed, line by line, in Rust.
- View the source for ldivide 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.