cosh — Compute hyperbolic cosine element-wise in MATLAB and RunMat.
Y = cosh(X) computes hyperbolic cosine element-wise. MATLAB documents single and double real or complex arrays; RunMat mode additionally admits typed integers, logicals, and characters through explicit compatibility gates.
Syntax
Y = cosh(X)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Input scalar, array, char array, complex value, or gpuArray. |
Returns
| Name | Type | Description |
|---|---|---|
Y | Any | Element-wise hyperbolic cosine result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cosh:InvalidInput | Input cannot be interpreted as supported numeric/char/complex data. | cosh: invalid input |
RunMat:cosh:Internal | Internal 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.
- Single real and complex inputs preserve single output; double inputs preserve double output.
- Typed integers are a RunMat-only extension. All eight real integer classes require exact binary64 representability before evaluation and produce double output; aligned values above
flintmaxare accepted when their significant bits fit binary64. - Logical and character inputs are separately gated RunMat-only extensions and produce double output.
- 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.
- Table, timetable, tall, and distributed container inputs remain general RunMat container/runtime gaps rather than integer-storage gaps.
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 owning provider lacks this hook or returns an invalid output handle, RunMat gathers the data to the host, computes the reference result, and immediately uploads the result back to the input handle's owning provider.
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]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 gathers to the host implementation, evaluates with the original floating precision or the extension's double boundary, and restores the result to the owning provider.
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?⌄
Typed integers are a RunMat-only extension. They are accepted only when exactly representable as double, and the result is double.
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, RunMat evaluates on the host and uploads a new result handle to the input's owning provider.
Related Math functions
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · typecast · uint16 · uint32 · uint8
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Optim
coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimoptions · optimset · quad · secondordercone
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cosh is executed, line by line, in Rust.
- View the source for cosh in Rust on GitHub
- Learn how the RunMat 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 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.