cosh — Hyperbolic cosine of scalars, vectors, matrices, complex numbers, or character arrays with MATLAB broadcasting and GPU acceleration.
Y = cosh(X) computes the hyperbolic cosine of every element in X, extending naturally to complex values.
How cosh works in 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.
How cosh runs on the GPU
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.
GPU memory and residency
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.
Examples
Hyperbolic cosine of a scalar
y = cosh(2)Expected output:
y = 3.7622Applying 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.0314 1.2947; 1.8884 2.9642]Working with complex inputs
z = 1 + 2i;
w = cosh(z)Expected output:
w = -0.6421 + 1.0686iHyperbolic cosine for character codes
chars = 'AZ';
codes = cosh(chars)Expected output:
codes = [8.4744e27 6.1020e38]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.
Related functions to explore
These functions work well alongside cosh. Each page has runnable examples you can try in the browser.
cos, sinh, tanh, gpuArray, gather, acos, acosh, asin, asinh, atan, atan2, atanh, sin, tan
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cosh works, line by line, in Rust.
- View cosh.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.