Finding the matrix inverse hyperbolic tangent function using Taylor series.
Here’s a Java program that calculates the inverse hyperbolic tangent (arctanh) of a matrix using the Taylor series expansion:
import java.util.Arrays; public class MatrixInverseHyperbolicTangent { // Number of terms in the Taylor series expansion private static final int TERMS = 10; // Calculate the power of x with the given exponent private static double power(double x, int exponent) { double result = 1.0; for (int i = 0; i < exponent; i++) { result *= x; } return result; } // Calculate the factorial of n private static int factorial(int n) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } // Calculate the inverse hyperbolic tangent of x using Taylor series private static double arctanh(double x) { double result = 0.0; for (int n = 0; n < TERMS; n++) { double numerator = power(x, 2 * n + 1); double denominator = 2 * n + 1; double term = numerator / denominator; if (n % 2 == 1) { term *= -1; } result += term; } return result; } // Calculate the inverse hyperbolic tangent of a matrix private static double[][] matrixArctanh(double[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; double[][] result = new double[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = arctanh(matrix[i][j]); } } return result; } // Utility method to print a matrix private static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } } // Test the program public static void main(String[] args) { double[][] matrix = { {0.5, 0.2}, {-0.4, 0.8} }; double[][] inverseArctanhMatrix = matrixArctanh(matrix); System.out.println("Matrix:"); printMatrix(matrix); System.out.println("Inverse Hyperbolic Tangent Matrix:"); printMatrix(inverseArctanhMatrix); } }
In this program, the `arctanh()` method calculates the inverse hyperbolic tangent of a single value using the Taylor series expansion. The `matrixArctanh()` method applies the `arctanh()` function to each element of the input matrix to obtain the inverse hyperbolic tangent matrix.
The program includes a `printMatrix()` utility method to display the matrices neatly.
You can run this program to test it with your desired matrix and observe the resulting inverse hyperbolic tangent matrix.
No comments yet! You be the first to comment.