麻烦大家了!!!

解决方案 »

  1.   

    我写的矩阵类,供参考: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 |
    有时间的话,把加法,减法,数乘补上,呵呵。
      

  2.   

    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++) {
             for (int k = 0; k < n; k++) {
                B_kol[k] = matrix.A[k][j];
             }
             for (int i = 0; i < m; i++) {
                double[] Arowi = A[i];
                double s = 0;
                for (int k = 0; k < n; k++) {
                   s += Arowi[k]*B_kol[k];
                }
                C[i][j] = s;
             }
          }
          return M;
    }  public void print (int w, int d) {
       DecimalFormat format = new DecimalFormat();
       int width=0;
       //System.out.print(false);  // start on new line.
          for (int i = 0; i < m; i++) {
             for (int j = 0; j < n; j++) {
                String s = format.format(A[i][j]); // format the number
                int padding = Math.max(1,width-s.length()); // At _least_ 1 space
                for (int k = 0; k < padding; k++)
                 System.out.print(' ');
                System.out.print(s);
             }
             System.out.print("\n");
          }
          System.out.print("\n");
          }}
    public class ProductMatrix { /**
     * @param args
     */
    public static void main(String[] args) {
     double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
     double[][] vals_1 = {{5,4,6,7},{3,2,4,3},{5,8,2,2}};
          MatrixProduct A = new MatrixProduct(vals);
          MatrixProduct B = new MatrixProduct(vals_1);
          MatrixProduct C=A.product(B);
          C.print(0,2); }}二维数组写的(顺便问一句,LZ为什么不用API,API里几乎所有的运算都包含了,你不用.)
    :)