What does the rdivide function do in MATLAB / RunMat?
rdivide(A, B) (or the operator form A ./ B) divides corresponding elements of A and B, honouring MATLAB's implicit expansion rules so that scalars and singleton dimensions broadcast automatically.
How does the rdivide function behave in MATLAB / RunMat?
- Supports real, complex, logical, and character inputs; logical and character data are promoted to double precision before division.
- Implicit expansion works across any dimension, provided the non-singleton extents match. Size mismatches raise the standard MATLAB-compatible error.
- Complex operands follow the analytic rule
(a + ib) ./ (c + id) = ((ac + bd) + i(bc - ad)) / (c^2 + d^2), matching MATLAB's behaviour for infinities and NaNs. - Empty dimensions propagate naturally—if the broadcasted shape contains a zero extent, the result is empty with that shape.
- Integer inputs promote to double precision, mirroring the behaviour of other RunMat arithmetic builtins.
- The optional
'like'prototype makes the output adopt the residency (host or GPU) and complexity characteristics of the prototype, letting you keep implicit-expansion expressions on the device without manualgpuArraycalls. When the prototype is complex, the result remains a complex host array (complex gpuArray prototypes are not yet supported).
rdivide Function GPU Execution Behaviour
When a gpuArray provider is active:
- If both operands are gpuArrays with identical shapes, RunMat dispatches to the provider's
elem_divhook so the entire computation stays on device memory. - If one operand is a scalar (host or device) and the other is a gpuArray, the runtime calls
scalar_div(tensor ./ scalar) orscalar_rdiv(scalar ./ tensor) accordingly. - The fusion planner treats
rdivideas a fusible elementwise node, so adjacent elementwise producers or consumers can execute inside a single WGSL kernel or provider-optimised pipeline, reducing host↔device transfers. - Implicit-expansion workloads (for example, combining a row vector with a column vector) or unsupported operand kinds gather transparently to host memory, compute the result with full MATLAB semantics, and return a host tensor. When you request
'like'with a complex prototype, RunMat gathers to the host, performs the conversion, and returns a complex host value so downstream code still sees MATLAB-compatible types.
Examples of using the rdivide function in MATLAB / RunMat
Divide two matrices element-wise
A = [8 12 18; 2 10 18];
B = [2 3 6; 2 5 9];
Q = rdivide(A, B);
Expected output:
Q =
4 4 3
1 2 2
Divide a matrix by a scalar
A = magic(3);
scaled = rdivide(A, 2);
Expected output:
scaled =
4.5 0.5 3.5
1.5 5.0 9.0
8.0 6.5 2.0
Use implicit expansion between a column and row vector
col = (1:3)';
row = [10 20 30];
ratio = rdivide(col, row);
Expected output:
ratio =
0.1 0.05 0.0333
0.2 0.10 0.0667
0.3 0.15 0.1000
Divide complex inputs element-wise
z1 = [1+2i, 3-4i];
z2 = [2-1i, -1+1i];
quot = rdivide(z1, z2);
Expected output:
quot =
0.0 + 1.0i -3.5 + 0.5i
Divide character codes by a numeric scalar
letters = 'ABC';
codes = rdivide(letters, 2);
Expected output:
codes = [32.5 33 33.5]
Execute rdivide directly on gpuArray inputs
G1 = gpuArray([10 20 30]);
G2 = gpuArray([2 5 10]);
deviceQuot = rdivide(G1, G2);
result = gather(deviceQuot);
Expected output:
deviceQuot =
1x3 gpuArray
5 4 3
result =
5 4 3
Keep the result on the GPU with a 'like' prototype
proto = gpuArray.zeros(1, 1);
A = [1 2 3];
B = [2 4 6];
C = rdivide(A, B, 'like', proto); % stays on the GPU for downstream work
Expected output:
C =
1x3 gpuArray
0.5 0.5 0.5
GPU residency in RunMat (Do I need gpuArray?)
RunMat's auto-offload planner keeps tensors on the GPU whenever fused expressions benefit from device execution. Explicit gpuArray / gather calls are still supported for MATLAB code that manages residency manually. When the active provider lacks the kernels needed for a particular call (for example, implicit expansion between gpuArrays of different shapes), RunMat gathers back to the host, computes the MATLAB-accurate result, and resumes execution seamlessly.
FAQ
Does rdivide support MATLAB implicit expansion?
Yes. Any singleton dimensions expand automatically. If a dimension has incompatible non-singleton extents, rdivide raises the standard size-mismatch error.
What numeric type does rdivide return?
Results are double precision for real inputs and complex double when either operand is complex. Logical and character inputs are promoted to double before division.
How does rdivide handle division by zero?
RunMat follows IEEE rules: finite ./ 0 produces signed infinity, while 0 ./ 0 yields NaN. Complex results follow MATLAB's analytic continuation rules.
Can I divide gpuArrays by host scalars?
Yes. RunMat keeps the computation on the GPU when the scalar is numeric. For other host operand types, the runtime gathers the gpuArray and computes on the CPU.
Does rdivide preserve gpuArray residency after a fallback?
When a fallback occurs (for example, implicit expansion that the provider does not implement), the current result remains on the host. Subsequent operations may move it back to the GPU when auto-offload decides it is profitable.
How can I force the result to stay on the GPU?
Provide a 'like' prototype: rdivide(A, B, 'like', gpuArray.zeros(1, 1)) keeps the result on the device even if one of the inputs originated on the host. Complex prototypes are honoured on the host today; supply a real gpuArray prototype when you need the result to remain device-resident.
How are empty arrays handled?
Empty dimensions propagate. If either operand has an extent of zero in the broadcasted shape, the result is empty with the broadcasted dimensions.
Are integer inputs supported?
Yes. Integers promote to double precision during the division, matching other RunMat arithmetic builtins.
Can I mix complex and real operands?
Absolutely. The result is complex, with broadcasting rules identical to MATLAB.
What about string arrays?
String arrays are not numeric and therefore raise an error when passed to rdivide.
See Also
times, ldivide, mrdivide, gpuArray, gather
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/math/elementwise/rdivide.rs - Found a bug or behavioural difference? Please open an issue with details and a minimal repro.