std::cosh(std::valarray)

出自cppreference.com
< cpp‎ | numeric‎ | valarray
在2024年2月21日 (三) 10:22由Lynnboy對話 | 貢獻所做的修訂版本

(差異) ←上一修訂 | 最新修訂 (差異) | 下一修訂→ (差異)
 
 
 
 
在標頭 <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 |

[編輯] 參閱

應用函數 std::sinhvalarray 的每個元素
(函數模板) [編輯]
應用函數 std::tanh 到 valarray 的每個元素
(函數模板) [編輯]
(C++11)(C++11)
計算雙曲餘弦(cosh(x)
(函數) [編輯]
計算複數的雙曲餘弦(cosh(z)
(函數模板) [編輯]