Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

주어진 행렬의 자취와 법선을 찾는 자바 프로그램

<시간/>

이 기사에서는 주어진 행렬의 궤적과 법선을 찾는 방법을 이해할 것입니다. 행렬의 법선은 행렬의 모든 요소에 대한 제곱합의 제곱근입니다. 행렬의 자취는 주대각선에 있는 모든 요소의 합입니다(왼쪽 위에서 오른쪽 아래로).

아래는 동일한 데모입니다 -

입력이 다음과 같다고 가정 -

The matrix is defined as:
2 3 4
5 2 3
4 6 9

원하는 출력은 -

Trace value: 13.0
Normal value: 14.142135623730951

알고리즘

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix
Step 3 - Define the values.
Step 4 - To compute trace value, iterate over each element of the matrix using two for-loops, add the diagonal elements and store the value.
Step 5 - To compute the normal value, iterate over each element of the matrix using two for-loops, compute the sum of square of each element, them compute the square root of the value and store the value.
Step 5 - Display the result
Step 6 - Stop

예시 1

여기에서 모든 작업을 'main' 기능 아래에 묶습니다.

public class NormalAndTrace {
   public static void main(String args[]) {
      int[][] input_matrix = {
         {2, 3, 4},
         {5, 2, 3},
         {4, 6, 9}
      };
      int i, j, matrix_size = 3;
      double trace = 0, square = 0, normal = 0;
      System.out.println("The matrix is defined as: ");
      for(i = 0; i < matrix_size; i++) {
         for(j = 0; j < matrix_size; j++)
         System.out.print(input_matrix[i][j]+" ");
         System.out.println(" ");
      }
      System.out.println("\nThe Trace value of the matrix is ");
      for(i = 0; i < matrix_size; i++) {
         for(j = 0; j < matrix_size; j++) {
            if(i == j) {
               trace = trace + (input_matrix[i][j]);
            }
         }
      }
      System.out.println(trace);
      System.out.println("\nThe Normal value of the matrix is ");
      for(i = 0; i < matrix_size; i++) {
         for(j = 0; j < matrix_size; j++) {
            square = square + (input_matrix[i][j])*(input_matrix[i][j]);
         }
      }
      normal = Math.sqrt(square);
      System.out.println(normal);
   }
}

출력

The matrix is defined as:
2 3 4
5 2 3
4 6 9

The Trace value of the matrix is
13.0

The Normal value of the matrix is
14.142135623730951

예시 2

여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.

public class NormalAndTrace {
   static int matrix_size = 3;
   static void normal_trace(int input_matrix[][]){
      int i, j;
      double trace = 0, square = 0, normal = 0;
      System.out.println("\nThe Trace value of the matrix is ");
      for(i = 0; i < matrix_size; i++) {
         for(j = 0; j < matrix_size; j++) {
            if(i == j) {
               trace = trace + (input_matrix[i][j]);
            }
         }
      }
      System.out.println(trace);
      System.out.println("\nThe Normal value of the matrix is ");
      for(i = 0; i < matrix_size; i++) {
         for(j = 0; j < matrix_size; j++) {
            square = square + (input_matrix[i][j])*(input_matrix[i][j]);
         }
      }
      normal = Math.sqrt(square);
      System.out.println(normal);
   }
   public static void main(String args[]) {
      int i, j;
      int[][] input_matrix = { {2, 3, 4},
         {5, 2, 3},
         {4, 6, 9}
      };
      System.out.println("The matrix is defined as: ");
      for(i = 0; i < matrix_size; i++) {
         for(j = 0; j < matrix_size; j++)
         System.out.print(input_matrix[i][j]+" ");
         System.out.println(" ");
      }
      normal_trace(input_matrix);
   }
}

출력

The matrix is defined as:
2 3 4
5 2 3
4 6 9

The Trace value of the matrix is
13.0

The Normal value of the matrix is
14.142135623730951