Java 查找下三角矩阵和上三角矩阵之间的乘积

Java 查找下三角矩阵和上三角矩阵之间的乘积

在Java中,数组是一个对象。它是一种非基本数据类型,可以存储相同数据类型的值。在Java中,矩阵实际上就是多维数组,它代表了多行和多列。

  • 三角矩阵 − 如果矩阵的对角线上方或下方的所有元素都为零,则称其为三角矩阵。

  • 下三角矩阵 − 如果矩阵的对角线上方的所有元素都为零,则称其为下三角矩阵。

  • 上三角矩阵 − 如果矩阵的对角线下方的所有元素都为零,则称其为上三角矩阵。

根据问题陈述,我们需要找到给定的下三角矩阵和上三角矩阵之间的乘积。

公式

矩阵1:

A   B
C   D

矩阵-2:

E   F
G   H

产品:

Matrix-1 * Matrix-2:    
AE+BG         AF+BH
CE+DG         CF+DH

让我们深入研究这篇文章,了解如何使用Java编程语言来实现它。

为了向你展示一些实例

示例1

假设下三角矩阵为 =

{{12, 0, 0},
{43, 3, 0},
{3, 8, 9}}

假设上三角矩阵为 =

{{5, 2, 1},
{0, 7, 10},
{0, 0, 4}}

这两个矩阵的乘积是:

60    24    12    
215   107   73    
15    62    119

示例2

假设下三角矩阵为 =

{{1, 0, 0},
{2, 3, 0},
{4, 5, 6}}

假设上三角矩阵是 =

{{7, 8, 9},
{0, 10, 11},
{0, 0, 12}}

这两个矩阵的乘积是:

7    8    9    
14    46    51    
28    82    163

步骤

  • 步骤1 - 声明和初始化两个整型多维数组。

  • 步骤2 - 嵌套for循环来执行乘积计算过程。

  • 步骤3 - 每次计算后,将元素存储在相应的行和列中的乘积矩阵数组中。

  • 步骤4 - 将计算出的乘积矩阵打印出来作为输出。

语法

要获取数组的长度(即数组中的元素个数),有一个数组的内置属性,即 length

下面是它的语法:

array.length

其中,’array’表示数组引用。

多种方法

我们以不同的方法提供了解决方案。

  • 通过使用数组元素的静态初始化

  • 通过使用用户定义的方法

让我们逐个查看程序及其输出。

方法1:通过使用数组元素的静态初始化

在此方法中,程序将初始化多维数组元素。然后根据算法,将输出下三角矩阵和上三角矩阵的乘积。

示例

public class Main {
   public static void main(String[] args) {
      int r, c;

      // Declare and initializing two matrices
      int[][] LowerTriMat = {{12, 0, 0},
         {43, 3, 0},
         {3, 8, 9}};
      int[][] upperTriMat = {{5, 2, 1},
         {0, 7, 10},
         {0, 0, 4}};

      // Calculating the number of rows and columns present in matrices
      r = LowerTriMat.length;
      c = LowerTriMat[0].length;

      // Declare the product matrix
      int[][] prodMatrix = new int[r][c];

      //initiating the loop to find the product of two matrices
      for(int i = 0; i < r; i++) {
         for (int j = 0; j < c; j++) {
            for (int k = 0; k < c; k++) {
               prodMatrix[i][j] += LowerTriMat[i][k] * upperTriMat[k][j];
            }
         }
      }

      // output result
      System.out.println("The Product matrix of two matrices is: ");
      for(int[] row : prodMatrix) {
         for (int column : row) {
            System.out.print(column + "    ");
         }
         System.out.println();
      }
   }
}

输出

The Product matrix of two matrices is: 
60     24     12    
215    107    73    
15     62    119

方法2:使用用户定义的方法

在这种方法中,数组元素将在程序中进行初始化。然后通过将数组作为参数传递调用用户定义的方法,并根据算法,在方法内部打印出下三角矩阵和上三角矩阵的乘积作为输出。

程序

public class Main {
   public static void main(String[] args){

      // Declare and initializing two matrices
      int[][] LowerTriangularMatrix = { {1, 0, 0},
         {2, 3, 0},
         {4, 5, 6} };
      int[][] upperTriangularMatrix = { {7, 8, 9},
         {0, 10, 11},
         {0, 0, 12} };
      productMat(LowerTriangularMatrix, upperTriangularMatrix);
   }  
   public static void productMat(int[][] LowerTriMat,int[][] upperTriMat) {

      // declare the variables 
      //and store the number of rows and columns present in matrices
      int r = LowerTriMat.length;
      int c = LowerTriMat[0].length;

      // Declare and initialize the product matrix
      int[][] prodMatrix = new int[r][c];

      //initiating the loop to find the product of two matrices
      for(int i = 0; i < r; i++) {
         for (int j = 0; j < c; j++) {
            for (int k = 0; k < c; k++) {
               prodMatrix[i][j] += LowerTriMat[i][k] * upperTriMat[k][j];
            }
         }
      }

      // output result
      System.out.println("The Product matrix of two matrices is: ");
      for(int[] row : prodMatrix){
         for (int column : row) {
            System.out.print(column + "    ");
         }
         System.out.println();
      }
   }
}

输出

The Product matrix of two matrices is: 
7      8      9    
14    46     51    
28    82    163

在本文中,我们使用Java编程语言探索了使用不同方法找到下三角矩阵和上三角矩阵的乘积。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程