SciPy - tanhm() Function



The scipy.linalg.tanhm() method calculates the hyperbolic tangent of a square matrix A. The matrix hyperbolic tangent is given by sinh(A)cosh^1(A) where sinh and cosh represent the matrix's hyperbolic sine and cosine, respectively. This is based on the decomposition of the matrices and then applies the hyperbolic tangent to each of the elements in the matrix.

The physicists and engineers mostly make use of this method in solving differential equations, checking stability, and describing hyperbolic events. Results turn out useful in dynamic systems that exhibit hyperbolic behavior over time.

Errors may occur when input matrix A isn't square: hyperbolic tangent functions can be defined for square matrices. Furthermore, matrices with complex eigenvalues can produce computational mistakes or undefined outcomes, making input validation critical.

The method can be combined with expm() (matrix exponential) or logm() (matrix logarithm) for dynamic simulations and stability studies, where hyperbolic transformations are required.

Syntax

The syntax for the SciPy tanhm() method is as follows −

.tanhm(A)

Parameters

This method accepts the following parameters −

  • A (N, N) array_like − Input square matrix (nn), real or complex.

Return Value

The hyperbolic tangent of the matrix 'A', of the same shape as 'A'.

Example 1

The below code, is the basic example of tanhm() method where we have created a 2x2 matrix to compute the hyperbolic tangent and the result will also be in the same shape of the input matrix.

import numpy as np
import scipy.linalg
from scipy.linalg import tanhm

A = np.array([[1, 2], 
              [3, 4]])

# Compute the hyperbolic tangent of the matrix
tanh_matrix = scipy.linalg.tanhm(A)
print("Matrix Hyperbolic Tangent:\n", tanh_matrix)

When we run above program, it produces following result

Matrix Hyperbolic Tangent:
 [[-0.03207326  0.47207856]
 [ 0.70811784  0.67604458]]

Example 2

For symmetric matrices, the tanhm() method retains the symmetry in the output.

In the below code we have created a symmetric 22 matrix as input into the tanhm() method. The function computes the hyperbolic tangent while preserving the symmetric structure in the result.

import numpy as np
import scipy.linalg
from scipy.linalg import tanhm

# Symmetric matrix
A = np.array([[1, 2], 
              [2, 1]])
tanh_matrix = tanhm(A)
print("Matrix Hyperbolic Tangent:\n", tanh_matrix)

Following is an output of the above code

Matrix Hyperbolic Tangent:
 [[0.1167303  0.87832445]
 [0.87832445 0.1167303 ]]

Example 3

Combining tanhm() and expm() simulates dynamic systems with hyperbolic and exponential transformations.

The 22 matrix A is transformed using tanhm() and then passed to expm() to compute its exponential. The output exhibits the interaction of hyperbolic and exponential dynamics.

import numpy as np
from scipy.linalg import expm, inv

# Input matrix
A = np.array([[1, 2], 
              [3, 4]])

# Compute sinh(A) and cosh(A)
sinh_A = (expm(A) - expm(-A)) / 2
cosh_A = (expm(A) + expm(-A)) / 2

# Compute tanh(A)
tanh_matrix = sinh_A @ inv(cosh_A)

# Compute the exponential of the hyperbolic tangent matrix
expm_matrix = expm(tanh_matrix)

print("Exponential of Hyperbolic Tangent Matrix:\n", expm_matrix)

Output of the above code is as follows

Exponential of Hyperbolic Tangent Matrix:
 [[1.18247368 0.70246666]
 [1.05369999 2.23617367]]
scipy_linalg.htm
Advertisements