RunMat
GitHub

cosh — Compute hyperbolic cosine element-wise in MATLAB and RunMat.

Y = cosh(X) computes the hyperbolic cosine of each element in X. Real and complex inputs are supported with element-wise semantics matching MATLAB and RunMat.

Syntax

Y = cosh(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesInput scalar, array, char array, complex value, or gpuArray.

Returns

NameTypeDescription
YAnyElement-wise hyperbolic cosine result.

Errors

IdentifierWhenMessage
RunMat:cosh:InvalidInputInput cannot be interpreted as supported numeric/char/complex data.cosh: invalid input
RunMat:cosh:InternalInternal gather/conversion/allocation/provider flow failed.cosh: internal error

How cosh works

  • 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 applying cosh.
  • Complex inputs follow the analytic rule cosh(a + bi) = cosh(a)cos(b) + i·sinh(a)sin(b), propagating NaN/Inf components 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.

Does RunMat run cosh 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.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.0314 1.2947; 1.8884 2.9642]

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 = [8.4744e27 6.1020e38]

Using cosh with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how cosh changes the result.

Run a small cosh example, explain the result, then change one input and compare the output.

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.

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Signal

blackman · butter · conv · conv2 · deconv · filter · hamming · hann · sawtooth · sinc · square

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond · rref

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cosh is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.