Finding the matrix tangent
Here’s an example of a Java program that calculates the tangent of each element in a given matrix:
import java.util.Arrays; public class MatrixTangent { public static void main(String[] args) { double[][] matrix = { {0.5, 1.0, 1.5}, {2.0, 2.5, 3.0}, {3.5, 4.0, 4.5} }; double[][] tangentMatrix = calculateMatrixTangent(matrix); // Print the original matrix System.out.println("Original Matrix:"); printMatrix(matrix); // Print the tangent matrix System.out.println("\nTangent Matrix:"); printMatrix(tangentMatrix); } public static double[][] calculateMatrixTangent(double[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; double[][] tangentMatrix = new double[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { tangentMatrix[i][j] = Math.tan(matrix[i][j]); } } return tangentMatrix; } public static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } } }
This program defines a `calculateMatrixTangent` function that takes a 2D matrix as input and returns a new matrix where each element is the tangent of the corresponding element in the original matrix. The `printMatrix` function is used to display the matrices on the console.
In the `main` method, a sample matrix is provided, and the original matrix as well as the tangent matrix are printed to the console.
Note: This example assumes that the input matrix is a square matrix (i.e., the number of rows is equal to the number of columns). If you have a non-square matrix, you’ll need to modify the code accordingly.
No comments yet! You be the first to comment.