Finding the matrix hyperbolic cosine
Here’s an example Java program that calculates the hyperbolic cosine of a matrix:
import java.util.Arrays; public class MatrixHyperbolicCosine { // Function to calculate the hyperbolic cosine of a number public static double hyperbolicCosine(double x) { return (Math.exp(x) + Math.exp(-x)) / 2; } // Function to calculate the hyperbolic cosine of a matrix public static double[][] matrixHyperbolicCosine(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] = hyperbolicCosine(matrix[i][j]); } } return result; } // Main method to test the matrix hyperbolic cosine function public static void main(String[] args) { double[][] matrix = {{1.0, 2.0}, {3.0, 4.0}}; System.out.println("Input matrix:"); for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } double[][] result = matrixHyperbolicCosine(matrix); System.out.println("Result matrix:"); for (double[] row : result) { System.out.println(Arrays.toString(row)); } } }
This program defines two functions: `hyperbolicCosine` to calculate the hyperbolic cosine of a single number, and `matrixHyperbolicCosine` to calculate the hyperbolic cosine of a matrix by applying the `hyperbolicCosine` function to each element.
In the `main` method, we create an example input matrix `{{1.0, 2.0}, {3.0, 4.0}}` and pass it to the `matrixHyperbolicCosine` function. The resulting matrix is then printed to the console.
The output of the program will be:
Input matrix:
[1.0, 2.0]
[3.0, 4.0]
Result matrix:
[1.5430806348152437, 3.7621956910836314]
[10.067661995777765, 27.308232836016487]
The elements of the input matrix are passed through the `hyperbolicCosine` function to calculate the corresponding hyperbolic cosines, resulting in the output matrix.