Finding the matrix sine.
Here’s a Java program that calculates the sine of each element in a matrix:
import java.util.Arrays; public class MatrixSine { public static void main(String[] args) { double[][] matrix = { {0.5, 1.2, 2.3}, {3.4, 4.5, 5.6}, {6.7, 7.8, 8.9} }; double[][] sineMatrix = calculateSine(matrix); System.out.println("Original Matrix:"); printMatrix(matrix); System.out.println("\nSine Matrix:"); printMatrix(sineMatrix); } public static double[][] calculateSine(double[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; double[][] sineMatrix = new double[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { sineMatrix[i][j] = Math.sin(matrix[i][j]); } } return sineMatrix; } public static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } } }
This program defines a `calculateSine` method that takes a 2D matrix as input and returns a new matrix where each element is the sine of the corresponding element in the original matrix. The `printMatrix` method is used to display the matrices on the console.
In the example above, the original matrix is defined as a 3×3 matrix with some arbitrary values. The program calculates the sine of each element and prints both the original and sine matrices to the console. You can modify the values in the `matrix` variable to test the program with different input matrices.
No comments yet! You be the first to comment.