What does the cosh function do in MATLAB / RunMat?
Y = cosh(X) computes the hyperbolic cosine of every element in X, extending naturally to complex values.
How does the cosh function behave in MATLAB / RunMat?
- Works on scalars, vectors, matrices, and N-D tensors with MATLAB broadcasting semantics for scalar expansion.
- Logical inputs are converted to double precision (
true → 1.0,false → 0.0) before applyingcosh. - Complex inputs follow the analytic rule
cosh(a + bi) = cosh(a)cos(b) + i·sinh(a)sin(b), propagatingNaN/Infcomponents independently. - Character arrays are converted to their numeric code points prior to evaluation, with double-precision outputs that preserve the input shape.
- Empty arrays return empty results that respect MATLAB’s shape semantics.
cosh Function GPU Execution Behaviour
When RunMat Accelerate is active, tensors that already reside on the GPU stay there. Providers implementing the optional unary_cosh hook execute the operation entirely on the device (and fused elementwise kernels can inline cosh alongside other operations). If the active provider lacks this hook, RunMat gathers the data back to the host, computes the reference result, and only re-uploads when downstream operations demand GPU residency.
Examples of using the cosh function in MATLAB / RunMat
Hyperbolic cosine of a scalar
y = cosh(2);
Expected output:
y = 3.7622;
Applying cosh elementwise to a vector
x = linspace(-2, 2, 5);
y = cosh(x);
Expected output:
y = [3.7622 1.5431 1.0000 1.5431 3.7622];
Evaluating cosh on a matrix
A = [0 0.5; 1.0 1.5];
B = cosh(A);
Expected output:
B = [1.0000 1.1276; 1.5431 2.3524];
Executing cosh on a GPU tensor
G = gpuArray([0.25 0.75; 1.25 1.75]);
result_gpu = cosh(G);
result = gather(result_gpu);
Expected output:
result = [1.0310 1.2947; 1.8890 2.9510];
Working with complex inputs
z = 1 + 2i;
w = cosh(z);
Expected output:
w = -0.6421 + 1.0686i;
Hyperbolic cosine for character codes
chars = 'AZ';
codes = cosh(chars);
Expected output:
codes = [4.3771e18 9.1487e18];
GPU residency in RunMat (Do I need gpuArray?)
You usually do not need to call gpuArray explicitly. The fusion planner keeps tensors on the GPU whenever the active provider exposes the required kernels (such as unary_cosh). Manual gpuArray / gather calls remain available for MATLAB compatibility or when you must control residency before interoperating with external code.
FAQ
When should I use cosh?
Use cosh for hyperbolic modelling, solving differential equations, or transforming signals where the hyperbolic cosine naturally appears.
Does cosh work with complex inputs?
Yes. Real and imaginary components are evaluated using the analytic continuation, matching MATLAB’s cosh semantics.
What happens if the GPU provider lacks unary_cosh?
RunMat falls back to the host implementation. Tensors are gathered to the CPU, evaluated, and left on the host unless later operations request GPU residency ('like', planner decisions, etc.).
Can cosh participate in fusion?
Yes. The fusion planner can inline cosh inside elementwise groups, generating WGSL kernels that execute directly on the GPU when supported.
Are integers preserved?
Inputs are promoted to double precision before evaluation, matching MATLAB behaviour. Cast back explicitly if you need integer outputs.
How does cosh handle NaN or Inf values?
cosh propagates NaN and Inf in the same way as MATLAB: each component is treated independently, and results mirror IEEE-754 expectations.
Is there a warmup penalty on first GPU use?
Providers may compile elementwise pipelines during initialization. If unary_cosh is unavailable, the CPU fallback avoids the warmup altogether.
See Also
cos, sinh, tanh, gpuArray, gather
Source & Feedback
- The full source code for the implementation of the
coshfunction is available at:crates/runmat-runtime/src/builtins/math/trigonometry/cosh.rs - Found a bug or behavioral difference? Please open an issue with details and a minimal repro.