Namespaces
Variants

std:: cosh (std::valarray)

From cppreference.net
定义于头文件 <valarray>
template < class T >
valarray < T > cosh ( const valarray < T > & va ) ;

对于 va 中的每个元素,计算该元素值的双曲余弦函数。

目录

参数

va - 要应用操作的值数组

返回值

值数组,包含 va 中各值的双曲余弦函数结果。

注释

使用非限定函数( cosh )执行计算。若该函数不可用,由于 实参依赖查找 机制,将使用 std:: cosh

该函数可以实现为返回类型不同于 std::valarray 的情况。此时,替换类型需具备以下特性:

可能的实现

template<class T>
valarray<T> cosh(const valarray<T>& va)
{
    valarray<T> other = va;
    for (T& i : other)
        i = cosh(i);
    return other; // 可能返回代理对象
}

示例

#include <cmath>
#include <iomanip>
#include <iostream>
#include <valarray>
void show(const char* title, const std::valarray<float>& data)
{
    const int w{9};
    std::cout << std::setw(w) << title << " | ";
    for (float x : data)
        std::cout << std::setw(w) << x << " | ";
    std::cout << '\n';
}
int main()
{
    const std::valarray<float> x{.1, .2, .3, .4};
    const auto sinh = std::sinh(x);
    const auto cosh = std::cosh(x);
    const auto z = (cosh * cosh) - (sinh * sinh);
    show("x", x);
    show("sinh(x)", sinh);
    show("cosh(x)", cosh);
    show("z", z);
}

输出:

        x |       0.1 |       0.2 |       0.3 |       0.4 | 
  sinh(x) |  0.100167 |  0.201336 |   0.30452 |  0.410752 | 
  cosh(x) |     1.005 |   1.02007 |   1.04534 |   1.08107 | 
        z |         1 |         1 |         1 |         1 |

参见

对 valarray 的每个元素应用函数 std::sinh
(函数模板)
对 valarray 的每个元素应用函数 std::tanh
(函数模板)
(C++11) (C++11)
计算双曲余弦 ( cosh(x) )
(函数)
计算复数的双曲余弦 ( cosh(z) )
(函数模板)