跪距阵相乘的程序!

跪求一个距阵相乘的程序!!!
麻烦大家了!!!

------解决方案--------------------
我写的矩阵类,供参考:

public class Matrix {
private int[] data;
private int rows, cols;
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new int[rows * cols];
}
public Matrix(int rows, int cols, int[] src) {
this(rows, cols);
System.arraycopy(src, 0, data, 0, Math.min(src.length, data.length));
}
public Matrix multiply(Matrix multiplyer) {
if (cols != multiplyer.rows) throw new RuntimeException( "Cannot multiply these 2 matrixes. ");

Matrix product = new Matrix(rows, multiplyer.cols);
for (int i = 0; i < product.rows; i++) {
for (int j = 0; j < product.cols; j++)
for (int k = 0; k < cols; k++)
product.data[i*product.cols+j] += data[i*cols+k] * multiplyer.data[k*multiplyer.cols+j];
}
return product;
}
public String toString() {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
if (i % cols == 0) buf.append( "| ");
buf.append( " " + data[i]);
if ((i+1) % cols == 0) buf.append( " |\n ");
}
return buf.toString();
}
}

包含一个multiply方法,未使用二维数组,而是使用一维数组存储的,因为JAVA的二维数组是锯齿状的,不适合做矩阵乘法。元素默认初始化为0。

用法举例:

Matrix m1 = new Matrix(2, 2, new int[] { 1, 3, 4, 10 });
Matrix m2 = new Matrix(2, 3, new int[] { 4, 1, 5, 8 });
System.out.println(m1);
System.out.println(m2);
System.out.println(m1.multiply(m2));

输出:

| 1 3 |
| 4 10 |

| 4 1 5 |
| 8 0 0 |

| 28 1 5 |
| 96 4 20 |


有时间的话,把加法,减法,数乘补上,呵呵。

------解决方案--------------------
import java.text.DecimalFormat;
public class MatrixProduct implements Cloneable, java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private double[][] A;
private int m, n;
public MatrixProduct (int m, int n) {
this.m = m;
this.n = n;
A = new double[m][n];
}
public MatrixProduct (double[][] A) {
m = A.length;
n = A[0].length;
for (int i = 0; i < m; i++) {
if (A[i].length != n) {
throw new IllegalArgumentException( "All rows must have the same length. ");
}
}
this.A = A;
}

public MatrixProduct (double[][] A, int m, int n) {
this.A = A;
this.m = m;
this.n = n;
}

public MatrixProduct (double vals[], int m) {
this.m = m;
n = (m != 0 ? vals.length/m : 0);
if (m*n != vals.length) {
throw new IllegalArgumentException( "Array length must be a multiple of m. ");
}
A = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = vals[i+j*m];
}
}
}
public double[][] getArray () {
return A;
}
public MatrixProduct product (MatrixProduct matrix) {
if (matrix.m != n) {
throw new IllegalArgumentException( "Matrix inner dimensions must agree. ");
}
MatrixProduct M = new MatrixProduct(m,matrix.n);
double[][] C = M.getArray();
double[] B_kol = new double[n];
for (int j = 0; j < matrix.n; j++) {