What does the isinf function do in MATLAB / RunMat?
mask = isinf(x) returns a logical scalar or array indicating which elements of x are positive or negative infinity. The output matches MATLAB's semantics for scalars, matrices, higher-dimensional tensors, strings, logical arrays, and gpuArray values.
How does the isinf function behave in MATLAB / RunMat?
- Numeric scalars return a logical scalar (
true/false). - Numeric arrays return a logical array of the same size, with
truewherever the corresponding element is+Infor-Inf. - Complex inputs report
truewhen either the real or the imaginary component is infinite. - Logical inputs return
falsebecause logical values (0 or 1) are finite by construction. - Character arrays return logical arrays of zeros (characters map to finite code points).
- String arrays return logical arrays of zeros, mirroring MATLAB behavior.
stringscalars (string objects) return a logical scalarfalse.- When the input is a
gpuArray, RunMat keeps the computation on the device if the active acceleration provider implements thelogical_isinfhook; otherwise the runtime gathers the data back to the host automatically.
Examples of using the isinf function in MATLAB / RunMat
Checking if a scalar is infinite
result = isinf(1/0);
Expected output:
result =
1
Detecting infinities after division by zero
A = [1 0; 2 0];
quot = 1 ./ A;
mask = isinf(quot);
Expected output:
mask =
2×2 logical array
0 1
0 1
Flagging infinite components of a complex array
Z = [Inf+0i 1-Inf*1i 2+3i];
mask = isinf(Z);
Expected output:
mask =
1×3 logical array
1 1 0
Applying isinf to character data
chars = ['R' 'u' 'n'];
mask = isinf(chars);
Expected output:
mask =
1×3 logical array
0 0 0
Running isinf directly on the GPU
G = gpuArray([1 -Inf Inf]);
mask_gpu = isinf(G);
mask = gather(mask_gpu);
Expected output:
mask =
1×3 logical array
0 1 1
isinf Function GPU Execution Behaviour
When RunMat Accelerate is active, isinf looks for the provider hook logical_isinf. Providers that implement the hook execute the infinity test entirely on the GPU, producing a logical gpuArray result without any host transfers. If the hook is absent, RunMat gathers the input tensor back to the CPU, computes the mask on the host, and returns a regular logical array so the builtin always succeeds.
GPU residency in RunMat (Do I need gpuArray?)
You usually do not need to call gpuArray explicitly. RunMat's auto-offload planner keeps tensors on the GPU across fused expressions when that improves performance. You can still seed residency manually with gpuArray for compatibility with MATLAB scripts or when you want fine-grained control over data movement.
FAQ
Does isinf treat NaN values as infinite?
No. isinf only reports true for IEEE positive or negative infinity. NaN values return false, so you can combine isinf with isnan when you need to distinguish between the two.
What does isinf return for logical inputs?
Logical inputs always produce false because logical values are limited to 0 or 1, which are finite.
How does isinf handle complex numbers?
It returns true when either the real or the imaginary component is infinite, matching MATLAB semantics.
Does isinf move data between the CPU and GPU?
Only when necessary. If the selected provider implements logical_isinf, all work stays on the GPU. Otherwise, RunMat gathers the tensor to the host, computes the result, and delivers a logical array.
What happens with string or character inputs?
String scalars return false. Character arrays and string arrays return logical zeros with the same shape as the input.
Is there a performance difference between isinf, isnan, and isfinite?
Each predicate performs a single elementwise test. Performance is dominated by memory bandwidth, so they have comparable cost on both CPU and GPU.