使用不同 类 - Java 的矩阵乘法
Matrix Multiplication using different classes - Java
我的 class 有这个作业,我必须在其中创建一个矩阵乘法程序。条件如下:
实现两种类型的算法以将两个 n × n 矩阵相乘。假设 n 是 2 的幂:
- 直接的 O(n^3) 矩阵乘法算法。
- Strassen 的矩阵乘法算法。
评估你的不同算法,并写一份简短的报告。为不同的 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);
}
}
这是我的另一个 class:
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;
}
}
我知道我必须为乘法方法传递一个 object/Matrix,但我该怎么做呢?我的代码中还有其他问题,但我现在想专注于传递对象。
让我们深入了解一下您的代码:
为什么矩阵里面有两个double[][]class?矩阵只是一个二维数组。你应该删除 arrayB
double[][] arrayA;
double[][] arrayB;
私有构造函数有什么用?对你来说,现在没用。
private Matrix(double[][] a, double[][] b)
{
arrayA = a;
arrayB = b;
}
在 public 构造函数中,您正在打印一个 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;
}
为什么你的 multiply 方法有 2 个参数?因为它在 class 矩阵中(有一个 double[][] 变量)。您只需要一个参数(我认为您的示例最好使用 Matrix 参数而不是 double[][] 参数和 return 也是一个 Matrix)。
我不喜欢在创建或乘法时打印。最好创建一个打印矩阵的方法,并在你想打印它们时调用它。
所以....最终代码应该是这样的:
主要
public class 矩阵乘法
{
public static void main(String[] args)
{
随机数 = new Random();
整数行 = rand.nextInt(7) + 2;
整数列 = 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();
}
}
矩阵Class
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();
}
}
}
我的 class 有这个作业,我必须在其中创建一个矩阵乘法程序。条件如下:
实现两种类型的算法以将两个 n × n 矩阵相乘。假设 n 是 2 的幂:
- 直接的 O(n^3) 矩阵乘法算法。
- Strassen 的矩阵乘法算法。
评估你的不同算法,并写一份简短的报告。为不同的 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);
}
}
这是我的另一个 class:
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;
}
}
我知道我必须为乘法方法传递一个 object/Matrix,但我该怎么做呢?我的代码中还有其他问题,但我现在想专注于传递对象。
让我们深入了解一下您的代码:
为什么矩阵里面有两个double[][]class?矩阵只是一个二维数组。你应该删除 arrayB
double[][] arrayA;
double[][] arrayB;
私有构造函数有什么用?对你来说,现在没用。
private Matrix(double[][] a, double[][] b) { arrayA = a; arrayB = b; }
在 public 构造函数中,您正在打印一个 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;
}
为什么你的 multiply 方法有 2 个参数?因为它在 class 矩阵中(有一个 double[][] 变量)。您只需要一个参数(我认为您的示例最好使用 Matrix 参数而不是 double[][] 参数和 return 也是一个 Matrix)。
我不喜欢在创建或乘法时打印。最好创建一个打印矩阵的方法,并在你想打印它们时调用它。
所以....最终代码应该是这样的:
主要 public class 矩阵乘法 { public static void main(String[] args) { 随机数 = new Random(); 整数行 = rand.nextInt(7) + 2; 整数列 = 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();
}
}
矩阵Class
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();
}
}
}