Namespaces
Variants

std:: tanh (std::valarray)

From cppreference.net
ヘッダーで定義 <valarray>
template < class T >
valarray < T > tanh ( const valarray < T > & va ) ;

va 内の各要素について、その値の双曲線正接を計算します。

目次

パラメータ

va - 操作を適用する値配列

戻り値

va 内の値の双曲線正接を含む値配列。

注記

修飾されていない関数 ( tanh ) が計算の実行に使用されます。該当する関数が利用できない場合、 std:: tanh 実引数依存の名前探索 によって使用されます。

この関数は、戻り値の型が std::valarray と異なる型で実装される場合があります。この場合、置換型は以下の特性を持ちます:

実装例

template<class T>
valarray<T> tanh(const valarray<T>& va)
{
    valarray<T> other = va;
    for (T& i : other)
        i = tanh(i);
    return other; // プロキシオブジェクトが返される可能性があります
}

#include <cmath>
#include <iostream>
#include <valarray>
auto show = [](char const* title, const std::valarray<double>& va)
{
    std::cout << title << " :";
    for (auto x : va)
        std::cout << "  " << std::fixed << x;
    std::cout << '\n';
};
int main()
{
    const std::valarray<double> x = {.0, .1, .2, .3};
    const std::valarray<double> sinh = std::sinh(x);
    const std::valarray<double> cosh = std::cosh(x);
    const std::valarray<double> tanh = std::tanh(x);
    const std::valarray<double> tanh_by_def = sinh / cosh;
    const std::valarray<double> tanh_2x = std::tanh(2.0 * x);
    const std::valarray<double> tanh_2x_by_def = 
        (2.0 * tanh) / (1.0 + std::pow(tanh, 2.0));
    show("x              ", x);
    show("tanh(x)        ", tanh);
    show("tanh(x) (def)  ", tanh_by_def);
    show("tanh(2*x)      ", tanh_2x);
    show("tanh(2*x) (def)", tanh_2x_by_def);
}

出力:

x               :  0.000000  0.100000  0.200000  0.300000
tanh(x)         :  0.000000  0.099668  0.197375  0.291313
tanh(x) (def)   :  0.000000  0.099668  0.197375  0.291313
tanh(2*x)       :  0.000000  0.197375  0.379949  0.537050
tanh(2*x) (def) :  0.000000  0.197375  0.379949  0.537050

関連項目

valarrayの各要素に関数 std::sinh を適用する
(関数テンプレート)
valarrayの各要素に関数 std::cosh を適用する
(関数テンプレート)
(C++11) (C++11)
双曲線正接を計算する ( tanh(x) )
(関数)
複素数の双曲線正接を計算する ( tanh(z) )
(関数テンプレート)