[英]Matrix Multiplication using different classes - Java
我在班上有这个作业,我必须创建一个矩阵乘法程序。 条件如下:
实现两种将两个n×n矩阵相乘的算法。 假设n是2的幂:
评估您的不同算法,并撰写一份简短报告。 为n的不同值(4、10、20,100)创建测试矩阵。 使用随机数生成矩阵。 计算算法的运行时间。 您的报告应包括运行时间和结论。
到目前为止,这是我的代码:
public class MatrixMultiplication
{
public static void main(String[] args)
{
Random rand = new Random();
int rows = rand.nextInt(7) + 2;
int columns = rand.nextInt(7) + 2;
System.out.println("The matrix has " + rows + " randomized rows");
System.out.println("The matrix has " + columns + " randomized column");
System.out.println();
double[][] a = new double[rows][columns];
double[][] b = new double[columns][rows];
System.out.println("The first matrix has the values: ");
Matrix m1 = new Matrix(a);
System.out.println("---------------------------------");
System.out.println("The second matrix has the values: ");
Matrix m2 = new Matrix(b);
System.out.println();
Matrix productRegular = m1.multiply(m2);
}
}
这是我的另一堂课:
import java.util.Random;
class Matrix
{
double[][] arrayA;
double[][] arrayB;
private Matrix(double[][] a, double[][] b)
{
arrayA = a;
arrayB = b;
}
public Matrix(double[][] array) //Create matrix values
{
Random rand = new Random();
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] = rand.nextInt(10);
System.out.print(array[i][j] + " | ");
}
System.out.println();
}
}
public double multiply(double[][] a, double[][] b)
{
double[][] c = new double[a.length][b[0].length];
System.out.println("Product of A and B is");
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < b[0].length; j++)
{
for(int k = 0; k < a[0].length; k++)
{
c[i][j] += a[i][k] * b[k][j];
System.out.println(c[i][j] + " | ");
}
}
System.out.println();
}
return c;
}
}
我知道我必须为乘法方法传递一个对象/矩阵,但是我该怎么做呢? 我的代码中还有其他问题,但是我现在想专注于传递对象。
让我们深入看一下您的代码:
为什么在Matrix类中有两个double [] []? 矩阵只是一个二维数组。 您应该删除arrayB
double[][] arrayA;
double[][] arrayB;
私有构造函数的意义是什么? 对您来说,现在没有用了。
private Matrix(double[][] a, double[][] b) { arrayA = a; arrayB = b; }
在公共构造函数中,您正在打印一个Matrix,但是您没有在任何地方保存。
public Matrix(double[][] array) //Create matrix values { Random rand = new Random(); for(int i = 0; i < array.length; i++) { for(int j = 0; j < array[i].length; j++) { array[i][j] = rand.nextInt(10); System.out.print(array[i][j] + " | "); } System.out.println(); }
arrayA = array;
}
无论如何,我认为最好让2个构造函数
public Matrix(double[][] array) //you just pass an array created outside the class
{
arrayA = array;
}
public Matrix(int rows, int columns) //Create matrix values
{
double[][] array = new double [rows][columns];
Random rand = new Random();
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] = rand.nextInt(10);
System.out.print(array[i][j] + " | ");
}
System.out.println();
}
arrayA = array;
}
为什么您的乘法方法有2个参数? 因为它位于Matrix类中(具有double [] []变量)。 您只需要一个参数(我认为对您的示例而言,使用Matrix参数代替double [] []参数并返回一个Matrix更好)。
创建或乘法时,我不喜欢打印。 最好创建一个打印矩阵的方法,并在需要打印矩阵时调用它。
所以....最终的代码将是这样的:
主要公共类MatrixMultiplication {public static void main(String [] args){Random rand = new Random(); int行= rand.nextInt(7)+ 2; int列= rand.nextInt(7)+ 2;
System.out.println("The matrix has " + rows + " randomized rows");
System.out.println("The matrix has " + columns + " randomized column");
System.out.println();
System.out.println("The first matrix has the values: ");
Matrix m1 = new Matrix(rows,columns);
m1.print();
System.out.println("---------------------------------");
System.out.println("The second matrix has the values: ");
Matrix m2 = new Matrix(columns, rows);
m2.print();
System.out.println();
System.out.println("Product of A and B is");
Matrix productRegular = m1.multiply(m2);
productRegular.print();
}
}
矩阵类
import java.util.Random;
class Matrix
{
double[][] arrayA;
public Matrix(double[][] array) //Create matrix values
{
arrayA=array;
}
public Matrix(int rows, int columns) //Create matrix values
{
double[][]array= new double[rows][columns];
Random rand = new Random();
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] = rand.nextInt(10);
}
}
arrayA=array;
}
public Matrix multiply(Matrix m)
{
double[][]b=m.arrayA;
double[][] c = new double[arrayA.length][b[0].length];
for(int i = 0; i < arrayA.length; i++)
{
for(int j = 0; j < b[0].length; j++)
{
for(int k = 0; k < arrayA[0].length; k++)
{
c[i][j] += arrayA[i][k] * b[k][j];
}
}
}
return new Matrix(c);
}
public void print(){
for(int i=0;i<arrayA.length;i++){
for(int j=0;j<arrayA[0].length;j++){
System.out.print(arrayA[i][j] + " | ");
}
System.out.println();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.