Finding the matrix hyperbolic tangent
Here’s an example Java program that finds the hyperbolic tangent of each element in a matrix:
import java.util.Arrays; public class MatrixHyperbolicTangent { public static void main(String[] args) { double[][] matrix = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0} }; double[][] result = calculateMatrixHyperbolicTangent(matrix); System.out.println("Original Matrix:"); printMatrix(matrix); System.out.println("Hyperbolic Tangent Matrix:"); printMatrix(result); } public static double[][] calculateMatrixHyperbolicTangent(double[][] matrix) { int rows = matrix.length; int columns = matrix[0].length; double[][] result = new double[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[i][j] = Math.tanh(matrix[i][j]); } } return result; } public static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } System.out.println(); } }
In this program, the `calculateMatrixHyperbolicTangent` method takes a matrix as input and calculates the hyperbolic tangent for each element using the `Math.tanh()` function. The resulting matrix is then returned.
The program demonstrates the usage of this method by creating a sample matrix, calculating the hyperbolic tangent matrix, and printing both the original and resulting matrices to the console.
Note: This program assumes that the input matrix is a 2D array of doubles. You can modify the program as per your needs to handle different data types or matrix dimensions
No comments yet! You be the first to comment.