log2 — Base-2 logarithm of scalars, vectors, matrices, or N-D tensors.
Y = log2(X) computes the base-2 logarithm of every element in X, following MATLAB's semantics for real, logical, character, and complex inputs. Negative real values promote to complex results so that log2(-1) yields 0 + i·π/ln(2).
How does the log2 function behave in MATLAB / RunMat?
log2operates element-wise with MATLAB broadcasting rules.- Logical inputs convert to doubles (
true → 1.0,false → 0.0) before the logarithm is applied. - Character arrays are interpreted as their numeric code points and return dense double tensors of the same shape.
log2(0)returns-Inf; positive infinity staysInf;NaNpropagates unchanged.- Negative real values promote to complex results:
log2([-1 1])returns[0 + i·π/ln(2), 0]. - Complex inputs follow MATLAB's definition
log2(z) = log(z) / ln(2)and clamp subnormal imaginary parts to zero for readability.
GPU behavior
RunMat Accelerate keeps tensors on the GPU when the active provider implements unary_log2 and the data is known to remain in the real domain. If complex outputs are required (for example, negative inputs) or the provider lacks the hook, RunMat gathers the tensor to the host, computes the exact MATLAB-compatible result, updates residency metadata, and returns the host-resident value.
GPU residency
You typically do **not** need to call gpuArray yourself. The auto-offload planner keeps tensors on the GPU when profitable and the result stays real. When complex results are required, RunMat automatically gathers the data to the host to produce the precise MATLAB-compatible answer. Use gpuArray/gather only when you want to mirror MathWorks MATLAB workflows explicitly.
Examples of using log2 in MATLAB / RunMat
Computing base-2 logarithms of powers of two
values = [1 2 4 8];
powers = log2(values)Expected output:
powers = [0 1 2 3]Understanding how log2 handles zero
z = log2(0)Expected output:
z = -InfWorking with negative inputs using complex results
neg = [-1 -2];
out = log2(neg)Expected output:
out = [0.0000 + 4.5324i 1.0000 + 4.5324i]Checking power-of-two exponents for matrix sizes
A = [64 128; 256 512];
exponents = log2(A)Expected output:
exponents = [6 7; 8 9]Running log2 on GPU-resident data
G = gpuArray([1 4 16 64]);
result = log2(G);
host = gather(result)Expected output:
host = [0 2 4 6]Applying log2 to character codes
C = 'ABC';
values = log2(C)Expected output:
values = [6.0224 6.0444 6.0661]FAQ
When should I use log2 instead of log or log10?
Use log2 when you care about binary scalings—such as signal-processing bit widths, FFT sizes, or exponent analysis. Use log (natural logarithm) for exponential growth/decay and log10 for decimal magnitudes.
What happens if an element is zero?
log2(0) returns negative infinity (-Inf), matching MATLAB behavior.
How does log2 handle negative real numbers?
Negative values promote to complex numbers with an imaginary component of π/ln(2). This preserves phase information instead of producing NaN.
Can I pass complex inputs to log2?
Yes. Complex scalars and tensors are handled as log(z) / ln(2), matching MATLAB exactly.
Does the GPU implementation support complex outputs?
Current providers operate on real buffers. When complex outputs are required, RunMat gathers the tensor to the host while keeping fusion metadata consistent.
Is log2 numerically stable for very small or large values?
Yes. The implementation promotes to 64-bit doubles throughout and clamps tiny imaginary parts to zero, mirroring MATLAB's behavior for well-conditioned inputs.
See also
log, log10, log1p, exp, sqrt, gpuArray, gather
Source & Feedback
- Source code: `crates/runmat-runtime/src/builtins/math/elementwise/log2.rs`
- Found a bug? Open an issue with a minimal reproduction.