plus — Compute element-wise addition in MATLAB and RunMat.
plus(A, B) (or A + B) adds corresponding elements of A and B. Implicit expansion, complex handling, and output-shape behavior follow MATLAB semantics.
Syntax
C = plus(A, B)
C = plus(A, B, "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left numeric/logical operand. |
B | Any | Yes | — | Right numeric/logical operand. |
like | StringScalar | Yes | — | Literal string "like". |
prototype | LikePrototype | Yes | — | Output class/device prototype. |
Returns
| Name | Type | Description |
|---|---|---|
C | NumericArray | Elementwise sum result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:plus:InvalidArgument | Optional arguments are malformed or unsupported. | plus: invalid argument |
RunMat:plus:InvalidInput | Operands or prototypes cannot be converted into supported numeric/logical forms. | plus: invalid input |
RunMat:plus:SizeMismatch | Operands are not broadcast-compatible. | plus: array sizes are not compatible for broadcasting |
RunMat:plus:Internal | Provider interaction, gather/upload, or internal tensor construction failed. | plus: internal error |
How plus works
- Supports real, complex, logical, and character inputs; logical and character data are promoted to double precision before addition.
- Implicit expansion works across any dimension, provided the non-singleton extents match. Size mismatches raise the standard MATLAB-compatible error message.
- Complex operands follow MATLAB's analytic rule
(a + ib) + (c + id) = (a + c) + i(b + d). - Empty dimensions propagate naturally—if either operand has a zero-sized dimension after broadcasting, the result is empty with the broadcasted shape.
- Integer inputs promote to double precision before addition, mirroring MATLAB behaviour and keeping numeric tower rules consistent with other RunMat arithmetic builtins.
- The optional
'like'prototype makes the output adopt the residency (host or GPU) and complexity characteristics of the prototype, which is particularly useful for keeping implicit-expansion expressions on the device.
Does RunMat run plus on the GPU?
When a gpuArray provider is active:
1. If both operands are gpuArrays with identical shapes, RunMat dispatches to the provider's elem_add hook. 2. If one operand is a scalar (host or device) and the other is a gpuArray, the runtime calls scalar_add to keep the result on the device. 3. The fusion planner treats plus as a fusible elementwise node, so adjacent elementwise producers/consumers can execute inside a single WGSL kernel or provider-optimised pipeline, avoiding spurious host↔device transfers. 4. Implicit-expansion workloads (e.g., mixing row and column vectors) or unsupported operand kinds gather transparently to host memory, compute the result with full MATLAB semantics, and return a host tensor unless a 'like' GPU prototype is supplied—in which case the runtime re-uploads the output to honour the residency request.
GPU memory and residency
RunMat's auto-offload planner keeps tensors on the GPU whenever fused expressions benefit from device execution. Explicit gpuArray / gather calls remain 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.
Examples
Adding two matrices element-wise
A = [1 2 3; 4 5 6];
B = [7 8 9; 1 2 3];
S = plus(A, B)Expected output:
S =
8 10 12
5 7 9Adding a scalar to every element of a matrix
A = magic(3);
shifted = plus(A, 0.5)Expected output:
shifted =
8.5 1.5 6.5
3.5 5.5 9.5
10.5 7.5 2.5Using implicit expansion between a column and row vector
col = (1:3)';
row = [10 20 30];
m = plus(col, row)Expected output:
m =
11 21 31
12 22 32
13 23 33Adding complex inputs element-wise
z1 = [1+2i, 3-4i];
z2 = [2-1i, -1+1i];
sumz = plus(z1, z2)Expected output:
sumz =
3 + 1i 2 - 3iAdding character codes to produce numeric arrays
letters = 'ABC';
codes = plus(letters, 2)Expected output:
codes = [67 68 69]Keeping element-wise sums on the GPU with 'like'
proto = gpuArray.zeros(1, 1);
G1 = gpuArray([1 2 3]);
G2 = gpuArray([4 5 6]);
deviceSum = plus(G1, G2, 'like', proto);
result = gather(deviceSum)Expected output:
deviceSum =
1x3 gpuArray
5 7 9
result =
5 7 9Using plus with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how plus changes the result.
Run a small plus example, explain the result, then change one input and compare the output.
FAQ
Does plus support MATLAB implicit expansion?⌄
Yes. Any singleton dimensions expand automatically. If a dimension has incompatible non-singleton extents, plus raises the standard size-mismatch error.
What numeric type does plus 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 addition.
Can I add gpuArrays and 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 plus preserve gpuArray residency after a fallback?⌄
When a fallback occurs (for example, implicit expansion that the provider does not implement), the resulting array stays on the host by default. Provide a 'like', gpuArray(...) prototype if you need the runtime to re-upload the final result automatically.
How can I force the result to stay on the GPU?⌄
Provide a 'like' prototype: plus(A, B, 'like', gpuArray.zeros(1, 1)) keeps the result on the device even if one of the inputs originated on the host.
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 that broadcasted shape.
Are integer inputs supported?⌄
Yes. Integers promote to double precision during the addition, 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 plus.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how plus is executed, line by line, in Rust.
- View the source for plus 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.