Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน tanh() ใน C++ STL (Hyperbolic Tangent)

tanh(x) หรือ Hyperbolic Tangent คือ ฟังก์ชันไฮเปอร์โบลิก tangent คืนค่าในช่วง (-1, 1)

คุณสมบัติทางคณิตศาสตร์

  • Domain: ทุกจำนวนจริง ℝ
  • Range: (-1, 1) - ไม่ถึง -1 และ 1
  • Odd Function: tanh(-x) = -tanh(x)
  • สูตร: tanh(x) = sinh(x)/cosh(x) = (eˣ - e⁻ˣ)/(eˣ + e⁻ˣ)
  • Derivative: d/dx tanh(x) = 1 - tanh²(x) = sech²(x)
  • Limit: lim(x→±∞) tanh(x) = ±1

Syntax

#include <cmath>

double tanh(double x);
float tanhf(float x);
long double tanhl(long double x);

โค้ดตัวอย่างพื้นฐาน

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

int main() {
    double values[] = {-5, -2, -1, -0.5, 0, 0.5, 1, 2, 5};
    
    cout << "x\t\ttanh(x)" << endl;
    cout << "----------------------" << endl;
    
    for (double x : values) {
        cout << x << "\t\t" << tanh(x) << endl;
    }
    
    // Degree Input Example
    double degree = 60;
    double rad = degree * M_PI / 180;
    cout << "\ntanh(" << degree << "°) = tanh(" << rad << ") = " << tanh(rad) << endl;
    
    return 0;
}

ผลลัพธ์

x		tanh(x)
----------------------
-5		-0.999909
-2		-0.964028
-1		-0.761594
-0.5		-0.462117
0		0
0.5		0.462117
1		0.761594
2		0.964028
5		0.999909

tanh(60°) = tanh(1.0472) = 0.780714

กราฟของ tanh(x)

     1.0 ┤                    ╭──────────────
        │                  ╭─╯
        │                ╭─╯
     0.5 ┤              ╭╯
        │            ╭──╯
        │          ╭─╯
     0.0 ┼──────────╯──────────
        │        ╭─╯
        │      ╭─╯
    -0.5 ┤    ╭╯
        │  ╭─╯
        │╭─╯
    -1.0 ┤╯
        └─────────────────────
        -5   -2  -1  0  1  2  5

การประยุกต์ใช้สำคัญ

1. Neural Networks: Activation Function (ยอดนิยม)


// tanh Activation Function
double tanhActivation(double x) {
    return tanh(x);
}

// Derivative สำหรับ Backpropagation
double tanhDerivative(double x) {
    double t = tanh(x);
    return 1.0 - t * t;  // 1 - tanh²(x)
}

// ใช้ใน Hidden Layers (มักใช้แทน Sigmoid)
// ข้อดี: Zero-centered (-1 ถึง 1) → Converge เร็วกว่า Sigmoid (0 ถึง 1)

2. Signal Processing: Soft Clipping


// Soft Limiter - ป้องกัน Signal Clipping แบบ Hard
double softClip(double x, double threshold = 1.0) {
    return threshold * tanh(x / threshold);
}

// Input:  2.0  → Output: ~0.96 * threshold
// Input:  10.0 → Output: ~1.0 * threshold (ไม่เกิน threshold)

3. Physics: Velocity Addition (Relativity)


// Relativistic Velocity Addition
// u' = (u + v) / (1 + uv/c²)
// ใช้ tanh: rapidity η = atanh(v/c)
// u' = c * tanh(η₁ + η₂)

double relativisticAdd(double u, double v, double c = 1.0) {
    double eta1 = atanh(u / c);
    double eta2 = atanh(v / c);
    return c * tanh(eta1 + eta2);
}

4. Control Systems: Saturation


// Controller Output Saturation
double saturate(double controlSignal, double limit) {
    return limit * tanh(controlSignal / limit);
}
// Smooth Saturation แทน Hard Clamp

5. Data Normalization


// Normalize ข้อมูลให้อยู่ใน (-1, 1) แบบ Smooth
double normalizeTanh(double x, double scale) {
    return tanh(x / scale);
}
// ข้อมูล Outlier ไม่ส่งผลมาก (Squashing)

Inverse Function: atanh()


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

int main() {
    double x = 0.5;
    double y = tanh(x);
    double back = atanh(y);
    
    cout << "x = " << x << endl;
    cout << "tanh(x) = " << y << endl;
    cout << "atanh(tanh(x)) = " << back << endl;
    
    // Domain of atanh: (-1, 1)
    // atanh(1) = inf, atanh(-1) = -inf
    return 0;
}

ผลลัพธ์

x = 0.5
tanh(x) = 0.462117
atanh(tanh(x)) = 0.5

Taylor Series (Approximation)

tanh(x) = x - x³/3 + 2x⁵/15 - 17x⁷/315 + 62x⁹/2835 - ...
           │
           └─ ใช้สำหรับ |x| < 1 (收敛快)

Numerical Stability


// สำหรับ x ใหญ่มาก tanh(x) ≈ 1 - 2e⁻²ˣ
// Direct Computation: (eˣ - e⁻ˣ)/(eˣ + e⁻ˣ)
// อาจ Overflow เมื่อ x > 709 (double)

// Stable Version for Large x:
double stableTanh(double x) {
    if (x > 20) return 1.0 - 2.0 * exp(-2.0 * x);
    if (x < -20) return -1.0 + 2.0 * exp(2.0 * x);
    return tanh(x);  // ใช้ built-in สำหรับค่ากลาง
}

Complex Version (C++11)


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

int main() {
    complex<double> z(1, 1);  // 1 + i
    complex<double> result = tanh(z);
    cout << "tanh(1+i) = " << result << endl;  // (1.08392, 0.271753)
    return 0;
}

สรุป Hyperbolic Functions

Function Range Derivative Inverse Use Case
sinh(x)ℝcosh(x)asinh(x)Catenary, Physics
cosh(x)[1,∞)sinh(x)acosh(x)Catenary, Relativity
tanh(x)(-1,1)sech²(x)atanh(x)Neural Net Activation