Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Hàm tanh() cho số phức trong C++

std::tanh cho số phức (header <complex>) tính hyperbolic tangent của một số phức. Là phiên bản mở rộng của std::tanh cho số thực (<cmath>).

Cú pháp

template<class T> complex<T> tanh(const complex<T>& z);

Công thức toán học

tanh(z) = sinh(z) / cosh(z)
        = (e^z - e^{-z}) / (e^z + e^{-z})
        = -i * tan(i*z)

Ví dụ 1: Số thực (phần ảo = 0)

#include <iostream>
#include <complex>
#include <cmath>
using namespace std;

int main() {
    complex<double> z(1.0, 0.0); // Số thực 1.0
    complex<double> result = tanh(z);
    
    cout << "tanh(" << z << ") = " << result << '\n';
    // tanh(1) ≈ 0.761594
    return 0;
}

Đầu ra

tanh(1.000000,0.000000) = (0.761594, 0.000000)

Ví dụ 2: Số ảo thuần túy

#include <iostream>
#include <complex>
using namespace std;

int main() {
    complex<double> z(0.0, 1.0); // i
    complex<double> result = tanh(z);
    
    cout << "tanh(i) = " << result << '\n';
    // tanh(i) = i * tan(1) ≈ i * 1.55741
    return 0;
}

Đầu ra

tanh(0.000000,1.000000) = (0.000000, 1.55741)

Ví dụ 3: Số phức tổng quát

#include <iostream>
#include <complex>
using namespace std;

int main() {
    complex<double> z(1.0, 2.0); // 1 + 2i
    complex<double> result = tanh(z);
    
    cout << "tanh(1+2i) = " << result << '\n';
    return 0;
}

Đầu ra

tanh(1+2i) = (1.16674, -0.243458)

Các hàm hyperbolic phức khác

HàmHeaderMô tả
sinh(z)<complex>Hyperbolic sine
cosh(z)<complex>Hyperbolic cosine
tanh(z)<complex>Hyperbolic tangent
asinh(z)<complex>Inverse hyperbolic sine
acosh(z)<complex>Inverse hyperbolic cosine
atanh(z)<complex>Inverse hyperbolic tangent

Lưu ý

  • Kết quả cùng kiểu complex<T> với tham số.
  • Có singularities (điểm kỳ dị) tại z = i*(π/2 + kπ) (k ∈ ℤ) nơi cosh(z) = 0.
  • Dùng cho xử lý tín hiệu, điều khiển, vật lý tính toán.