std::sinh(std::valarray)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

hey bestie πŸ’– welcome to std::sinh(std::valarray)(3) from the C++ Standard Libary manual β€” yes, that's the manual name, typo and all, and we're keeping the lore. This is the C++ Standard Library's valarray hyperbolic-sine page and it's lowkey a serve πŸ¦‹βœ¨

✨ NAME ✨

std::sinh(std::valarray) - std::sinh(std::valarray) β€” the name, twice, because why not πŸ’…


🌸 Synopsis 🌸

Defined in header <valarray>

template< class T >
valarray<T> sinh( const valarray<T>& va );

For each element in va computes hyperbolic sine of the value of the element. That's the whole assignment, no cap 🫢


πŸŽ€ Parameters πŸŽ€

  • va β€” value array to apply the operation to

πŸ’– Return value πŸ’–

Value array containing hyperbolic sine of the values in va. Slay, a whole new array of vibes ✨


🧚 Notes 🧚

okay so the computation uses the unqualified function (sinh) β€” no std:: prefix in the call, bestie. If such function is not available, std::sinh is used due to argument-dependent lookup. ADL said "I got you" 😌

The function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties:

  • All const member functions of std::valarray are provided. periodt.
  • std::valarray, std::slice_array, std::gslice_array, std::mask_array and std::indirect_array can be constructed from the replacement type.
  • For every function taking a const std::valarray<T>& except begin() and end() (since C++11), identical functions taking the replacement types shall be added;
  • For every function taking two const std::valarray<T>& arguments, identical functions taking every combination of const std::valarray<T>& and replacement types shall be added.
  • The return type does not add more than two levels of template nesting over the most deeply-nested argument type.
This is the whole replacement-type contract, and it is giving "we have valarray at home" energy but legally πŸ˜­πŸ’…

πŸ› οΈ Possible implementation πŸ› οΈ

template<class T>
valarray<T> sinh(const valarray<T>& va)
{
    valarray<T> other = va;
    for (T& i : other)
        i = sinh(i);

    return other; // proxy object may be returned
}

πŸ’» Example πŸ’»

Run this code, bestie:

// Run this code

#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <valarray>

template<typename T>
void show(char const* title, const std::valarray<T>& va)
{
    std::cout << title << " : " << std::right;
    for (T x : va)
        std::cout << std::fixed << x << ' ';
    std::cout << '\n';
}

template<typename T>
void sinh_for(std::valarray<T> const& z)
{
    // Hyperbolic sine is sinh(z) = (eᢻ - e⁻ᢻ) / 2.

    const std::valarray<T> sinh_z{std::sinh(z)};
    const std::valarray<T> e_z{std::exp(z)};
    const std::valarray<T> e_neg_z{std::exp(-z)};
    const std::valarray<T> sinh_def{(e_z - e_neg_z) / 2.0f};

    show("n         ", z);
    show("sinh(n)   ", sinh_z);
    show("(eⁿ-e⁻ⁿ)/2", sinh_def);

    std::cout.put('\n');
}

int main()
{
    sinh_for(std::valarray<float>{-.2f, -.1f, 0.f, .1f, .2f, INFINITY});
    sinh_for(std::valarray<std::complex<double>>{{-.2,-.1}, {.2,.1}});
}

πŸ“€ Output πŸ“€

n          : -0.200000 -0.100000 0.000000 0.100000 0.200000 inf
sinh(n)    : -0.201336 -0.100167 0.000000 0.100167 0.201336 inf
(eⁿ-e⁻ⁿ)/2 : -0.201336 -0.100167 0.000000 0.100167 0.201336 inf

n          : (-0.200000,-0.100000) (0.200000,0.100000)
sinh(n)    : (-0.200330,-0.101837) (0.200330,0.101837)
(eⁿ-e⁻ⁿ)/2 : (-0.200330,-0.101837) (0.200330,0.101837)

πŸ‘€ See also πŸ‘€

  • cosh(std::valarray) β€” applies the function std::cosh to each element of valarray (function template)
  • tanh(std::valarray) β€” applies the function std::tanh to each element of valarray (function template)
  • sinh, sinhf, sinhl β€” computes hyperbolic sine (\({\small\sinh{x}}\)sinh(x)) (function); the see-also row also carries (C++11) and (C++11) markers
  • sinh(std::complex) β€” computes hyperbolic sine of a complex number (\({\small\sinh{z}}\)sinh(z)) (function template)

πŸ™ˆ Hidden category πŸ™ˆ

  • Pages with unreviewed LWG DR marker

okay bye bestie 🫢 this page was sourced from http://cppreference.com, version/date 2024.06.10, and yes it's still std::sinh(std::valarray)(3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from