矩阵相乘有点迷糊
帮忙做下,越简单越好,能实现就OK
设计和编写代表矩阵的Matrix类。该类包括矩阵行列数变量int rows和int cols,矩阵数据数组double data[][],构造方法Matrix()、Matrix(int rows,int cols)、Matrix(int rows,int cols,double data[][]),获取某元素值的方法getData(int row,int col),设置某元素值的方法setData(int row,int col,double value),计算两个矩阵的乘积的方法multiply(Matrix m)以及toString()等内容。
[code="java"]public class Matrix {
int rows;
int cols;
double data[][];
public Matrix() {
this(0, 0);
}
public Matrix(int rows, int cols) {
this(rows, cols, new double[rows][cols]);
}
public Matrix(int rows, int cols, double[][] data) {
super();
this.rows = rows;
this.cols = cols;
this.data = data;
}
public double getData(int row, int col) {
return data[row][col];
}
public void setData(int row, int col, double value) {
data[row][col] = value;
}
public double[][] multiply(double[][] m2) {
int m1rows = data.length;
int m1cols = data[0].length;
int m2rows = m2.length;
int m2cols = m2[0].length;
if (m1cols != m2rows)
throw new IllegalArgumentException("matrix doesn't match");
double[][] result = new double[m1rows][m2cols];
// multiply
for (int i = 0; i < m1rows; i++)
for (int j = 0; j < m2cols; j++)
for (int k = 0; k < m1cols; k++)
result[i][j] += data[i][k] * m2[k][j];
return result;
}
public String toString() {
StringBuffer sb = new StringBuffer();
int rows = data.length;
int cols = data[0].length;
sb.append("array[" + rows + "][" + cols + "] = " + "\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
sb.append(" " + data[i][j] + " ");
sb.append("\n");
}
sb.append("\n");
return sb.toString();
}
public static void main(String[] argv) {
double x[][] = { { 3, 2, 3 }, { 5, 9, 8 }, };
Matrix m = new Matrix(3, 2, x);
double y[][] = { { 4, 7 }, { 9, 3 }, { 8, 1 }, };
double z[][] = m.multiply(y);
Matrix zm = new Matrix(z.length, z[0].length, z);
// print result
System.out.println(zm);
}
}[/code]
你好,参考这个例子.
两 个 矩 阵 Am× n、 Bn× l相 乘 得 到 Cm× l,每 个 元 素 Cij = aik*bk j (i=1..m,n=1..n)
public class MatrixMultiply{
public static void main( String args[] ){
int i,j,k;
int a[][]=new int[2][3];
int b[][]={ {1,5,2,8},{5,9,10,-3},{2,7,-5,-18} };
int c[][]=new int[2][4];
for( i=0; i<2; i++ )
for( j=0; j<3; j++ )
a[i][j]=(i+1)*(j+2);
for( i=0; i<2; i++ ){
for( j=0; j<4; j++ ){
c[i][j]=0;
for( k=0; k<3; k++ )
c[i][j]+=a[i][k]*b[k][j];
}
}
System.out.println("\n*** Matrix A ***");
for( i=0; i<2; i++ ){
for( j=0; j<3; j++ )
System.out.print(a[i][j]+" ");
System.out.println();
}
System.out.println("\n*** Matrix B ***");
for( i=0; i<3; i++ ){
for( j=0; j<4; j++ )
System.out.print(b[i][j]+" ");
System.out.println();
}
System.out.println("\n*** Matrix C ***");
for( i=0; i<2; i++ ){
for( j=0; j<4; j++ )
System.out.print(c[i][j]+" ");
System.out.println();
}
}
}
其结果为:
C:\>java MatrixMultiply
*** Matrix A ***
2 3 4
4 6 8
*** Matrix B ***
1 5 2 8
5 9 10 -3
2 7 -5 -18
*** Matrix C ***
25 65 14 -65
50 130 28 -130
[code="java"]
/**
计算两个矩阵的乘积的方法multiply(Matrix m)以及toString()等内容。
*/
public class Matrix {
private int rows;
private int cols;
private double data[][];
public Matrix() {
}
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
}
public Matrix(int rows, int cols, double data[][]) {
this(rows, cols);
this.data = data;
}
public double getData(int row, int col) {
return data[row][col];
}
public void setData(int row, int col, double value) {
data[row][col] = value;
}
public double[][] multiply(Matrix m) {
double resultMatrix[][] = new double[data.length][m.data[0].length];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < m.data[0].length; j++) {
double result = 0;
for (int z = 0; z < m.data.length; z++) {
result = result + data[i][z] * m.data[z][j];
}
resultMatrix[i][j] = result;
}
}
return resultMatrix;
}
public String toString() {
StringBuffer result = new StringBuffer();
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
result.append(result).append(data[i][j]).append(",");
}
result.append("\n");
}
return result.toString();
}
}
[/code]
[code="java"]package com.jones;
/**
@author jones
*
*/
public class Matrix {
private int rows; // 行
private int cols; // 列
private double[][] data; // 数据
public Matrix() {
}
// 构造方法
public Matrix(int M, int N) {
this.rows = M;
this.cols = N;
data = new double[M][N];
}
public Matrix(int rows, int cols, double data[][]) {
this.rows = rows;
this.cols = cols;
this.data = data;
}
public double getData(int row, int col) {
return data[row][col];
}
public void setData(int row, int col, double value) {
data[row][col] = value;
}
// 两个矩阵相乘
public Matrix multiply(Matrix B) {
Matrix A = this;
if (A.cols != B.rows)
throw new RuntimeException("Illegal matrix dimensions.");
Matrix C = new Matrix(A.rows, B.cols);
for (int i = 0; i < C.rows; i++)
for (int j = 0; j < C.cols; j++)
for (int k = 0; k < A.cols; k++)
C.data[i][j] += (A.data[i][k] * B.data[k][j]);
return C;
}
// 打印矩阵到控制台
public void show() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
System.out.printf("%9.4f ", data[i][j]);
System.out.println();
}
}
@Override
public String toString() {
return "共有" + rows + "行," + cols + "列";
}
}
[/code]