日期:2014-05-20  浏览次数:20855 次

急!!!!!!!!!!!java矩阵相乘,各路大神都来看一看啊

(1) 设计和编写代表矩阵的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()等内容。
(2) 编写测试类MatrixTest,并在该类中创建两个矩阵对象,计算其乘积。
(1) 编写Matrix类
(2) 编写MatrixTest类。在该类中通过键盘输入方式确定所要创建的两个矩阵的行列数,根据行列数随机生成数据或键盘输入,并通过setData方法生成矩阵的内容。
(3) 计算矩阵的乘积,并把结果通过toString方法输出到屏幕上


//import java.util.Scanner;
class Matrix
{
int rows;
int cols;
double data[][];
void Matrix()
{
rows=0;
cols=0;
data=new double[rows][cols];

}
void Matrix(int rows,int cols)
{
this.rows=rows;
this.cols=cols;
data=new double[this.rows][this.cols];
}
void Matrix(int rows,int cols,double data[][])
{
this.rows=rows;
this.cols=cols;
data=new double[this.rows][this.cols];
//this.data=data[rows][cols];
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
{
this.data[i][j]=data[i][j];
}
}
double getData(int row,int col)
{
//for(int i=0;i<row;i++)
//for(int j=0;j<col;j++)
//{
//data[row][col]
//}
data=new double[row][col];
return data[row][col];
}
void setData(int row,int col,double value)
{
data=new double[row][col];
this.data[row][col]=value;
}
double[][] multiply(Matrix m)
{
Matrix n=new Matrix();
for(int i=0;i<m.rows;i++){
for(int j=0;j<this.cols;j++){
n.data[i][j]=0;
for(int k=0;k<m.cols;k++)
n.data[i][j]+=m.data[i][k]*this.data[k][j];
}
}
return n.data;
}
/*
String toString(Matrix m,Matrix n)
{
for(int i=0;i<m.rows;i++)
for(int j=0;j<this.cols;j++)
return n.data[i][j]+" ";
return " ";
}*/
void print(Matrix n)
{
for(int i=0;i<n.rows;i++)
for(int j=0;j<n.cols;j++){
System.out.print(n.data[i][j]+" ");
System.out.println();}
}

}
class MatrixTest

{
public static void main(String[] args) 
{
System.out.println("helloworld");
Matrix x=new Matrix();
Matrix k=new Matrix();
x.Matrix(2,3);
System.out.println("helloworld");
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
x.setData(i,j,(i+1)*(j+2));
}
System.out.println("helloworld");
double b[][]={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}}; 
Matrix y=new Matrix();
y.Matrix(3,4,b);
k.data=y.multiply(x);
y.print(k);

}
}


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
  at Matrix.setData(MatrixTest.java:41)
  at MatrixTest.main(MatrixTest.java:85)


for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
x.setData(i,j,(i+1)*(j+2));
}

这样赋值难道有错误码?
void setData(int row,int col,double value)
{
data=new double[row][col];
this.data[row][col]=value;
}
还是这个函数写错了啊?




------解决方案--------------------
multiply核心算法没有改你的,你自己改吧
------解决方案--------------------
Java code

class Matrix {
    int rows;
    int cols;
    double data[][];

    public Matrix() {

    }

    public Matrix(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        data = new double[rows][cols];
    }

    public Matrix(int rows, int cols, double data[][]) {
        this.rows = rows;
        this.cols = cols;
        this.data = data;
    }

    double getData(int row, int col) {

        return data[row][col];
    }

    void setData(int row, int col, double value) {
        this.data[row][col] = value;
    }

    public double[][] multiply(Matrix m) {
        Matrix n = new Matrix();
        n.data = new double[m.rows][this.cols];
        for (int i = 0; i < m.rows; i++) {
            for (int j = 0; j < this.cols; j++) {
                n.data[i][j] = 0;
                for (int k = 0; k < m.cols; k++)
                    n.data[i][j] += m.data[i][k] * this.data[k][j];
            }
        }
        return n.data;
    }

    void print(Matrix n) {
        for (int i = 0; i < n.rows; i++) {
            for (int j = 0; j < n.cols; j++) {
                System.out.print(n.data[i][j] + "\t");
            }
            System.out.println();
        }
    }

}

public class MatrixTest

{
    public static void main(String[] args) {
        Matrix x = new Matrix(3, 3);

        for (int i = 0; i < x.rows; i++)
            for (int j = 0; j < x.cols; j++) {
                x.setData(i, j, (i + 1) * (j + 2));
            }
        double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
        Matrix y = new Matrix(b.length, b[0].length, b);
        double d[][] = y.multiply(x);
        Matrix k = new Matrix(d.length, d[0].length, d);
        y.print(k);
    }
}