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

这里,我们将所有操作绑定在一起,纳入“主体”函数。

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

更新于:2022 年 3 月 29 日

329 次浏览

启动你的 事业

通过完成课程获得认证

开始
广告