首页  >  文章  >  Java  >  Java 中的矩阵乘法

Java 中的矩阵乘法

WBOY
WBOY原创
2024-08-30 16:27:311054浏览

Java中的矩阵存储在数组中。存在一维数组和二维数组,它们以矩阵形式存储值,这些维度称为数组。一维数组中只存储一维的数字,而二维数组中,数字以行和列的形式存储。在 Java 编程语言中,矩阵可用于对数字进行加法、减法和乘法。矩阵乘法是 Java 编程方法中最复杂的任务之一。在本文中,我们必须在 Java 中执行矩阵乘法,并展示如何将两个矩阵相乘并提供合理的输出。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

一般方法

Java 编程语言中的矩阵乘法以非常简单的方式执行。首先,我们输入第一个二维数组中的数字,然后输入第二个二维数组中元素的数字。数字按行添加,这意味着创建第一行,然后创建第二行中的数字,依此类推。然后以类似的方式创建第二个矩阵,然后我们开始将矩阵中的数字相乘。

Java 中矩阵乘法的示例

下面是矩阵乘法的例子

示例#1

在编码示例中,我们看到如何按行输入两个矩阵,然后进行矩阵乘法。两个矩阵相乘的代码如下所示。声明了三个数组。第一个和第二个矩阵的乘积显示在第三个矩阵内。然后矩阵显示为输出,它是数组中两个矩阵的乘积。

代码:

import java.util.Scanner;
public class MatixMultiplication
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of the matrices. They must be equal.");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the numbers of the first matrix. Numbers will be added row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the numbers of the 2nd matrix. Numbers will be added row wise.  \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = input.nextInt();
}
}
System.out.println("Generating the multiplication of matrices.....");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product of the matrices is shown as below");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
input.close();
}
}

显示了 2*2 矩阵的输出。第一个矩阵由 { 1,2

元素组成

3,4}

第二个矩阵也包含相同的元素。在样本输出中,我们注意到矩阵和样本输出的乘法。矩阵的元素以非常好的方式产生。产生的输出

{1,2                          { 1,2                                      { 7, 10

3,4}        *                3,4}              =                       15, 22}

输出:

Java 中的矩阵乘法

示例#2

在编码示例 2 中,我们有相同的程序,但现在我们使用 3 维数组进行乘法。我们现在使用 3 * 3 矩阵乘法并在另一个 3 维数组中显示输出。

代码:

import java.util.Scanner;
public class Matix
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of the matrices. They must be equal.");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the numbers of the first matrix. Numbers will be added row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the numbers of the 2nd matrix. Numbers will be added row wise.  \n");
for (int z = 0; z < n; z++)
{
for (int k = 0; k < n; k++)
{
b[z][k] = input.nextInt();
}
}
System.out.println("Generating the multiplication of matrices.....");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product of the matrices is shown as below");
for (int k = 0; k < n; k++)
{
for (int l = 0; l < n; l++)
{
System.out.print(c[k][l] + " ");
}
System.out.println();
}
input.close();
}
}

从第二个示例代码中,我们打印两个 3 * 3 矩阵。第一个矩阵是 {1,1,1

1,1,1

1,1,1}

第二个矩阵也是一样的。矩阵乘法通过以下方式生成

{1,1,1                          {1,1,1                                                { 3,3,3

1,1,1                *         1,1,1                                  =              3,3,3

1,1,1}                          1,1,1}                                                3,3,3}

输出:

Java 中的矩阵乘法

结论

在本文中,我们看到了 2 * 2 矩阵和 3 * 3 矩阵的乘法,并且输出以非常漂亮的方式显示。输出已明确给出。使用矩阵乘法,我们还可以创建 4*4 矩阵乘法。在程序的第一步中会询问基础。我们也可以创建 5 * 5、6 * 6 矩阵。越是基础越是程序的复杂度。

但是,简单的矩阵乘法在计算以 X 轴、Y 轴或 Z 轴为反射轴的点的反射时非常有用。这些简单的概念用于坐标几何,并用于几何应用的数学建模。

以上是Java 中的矩阵乘法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn