class Matrix{ //返回两个矩阵的乘积C=A*B public double[][] multiply(double[][] A,double[][] B){ ....... } //返回矩阵A的转置矩阵A' public double[][] transpose(double[][] A){ ....... } //返回两个矩阵的和C=A+B public double[][] add(double[][] A,double[][] B){ ....... } }
现在要调用Matrix类计算矩阵A = X'*X+Y,其中X,Y是两个矩阵。
方法(一): class Test{ public static void main(String[] args){ double[][] X = {.......};//输入X double[][] Y = {.......};//输入Y Matrix matrix = new Matrix(); double[][] xTrans = matrix.transpose(X); double[][] temp = matrix.multiply(xTrans,X); double[][] A = matrix.add(temp,Y); double[][] C = matrix.multiply(X,Y);//如果还想计算C = X*Y } }
方法(二): class Test{ public static void main(String[] args){ double[][] X = {.......};//输入X double[][] Y = {.......};//输入Y double[][] xTrans = new Matrix().transpose(X); double[][] temp = new Matrix().multiply(xTrans,X); double[][] A = new Matrix().add(temp,Y); } }