std::tanh(std::valarray)
来自cppreference.com
<tbody>
</tbody>
| 在标头 <valarray> 定义
|
||
template< class T > valarray<T> tanh( const valarray<T>& va ); |
||
对 va 中每个元素计算元素值的双曲正切。
参数
| va | - | 要应用操作到的值数组 |
返回值
含有 va 中各值的双曲正切的值数组。
注解
用无限定函数 (tanh) 进行计算。若该函数不可用,则会由于实参依赖查找而使用 std::tanh。
函数可以实现为拥有不同于 std::valarray 的返回类型。此时替换它的类型拥有下列属性:
- 提供 std::valarray 的所有
const成员函数。 - 能从替换类型构造 std::valarray、std::slice_array、std::gslice_array、std::mask_array 和 std::indirect_array。
- 所有接受一个
const std::valarray&类型参数的函数 ,除了 begin() 和 end()(C++11 起)也应该接受替换类型。 - 所有接受两个
const std::valarray&类型参数的函数都应该接受const std::valarray&和替换类型的每种组合。 - 返回类型添加不多于两层嵌套在最深层嵌套的参数类型上的模板。
- 提供 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
参阅
应用函数 std::sinh 到 valarray 的每个元素 (函数模板) | |
应用函数 std::cosh 到 valarray 的每个元素 (函数模板) | |
(C++11)(C++11) |
计算双曲正切(tanh(x)) (函数) |
| 计算复数的双曲正切(tanh(z)) (函数模板) |