一、介绍
1、介绍。
矩阵的运算,本质上就是二维数组的运算。
2、公共代码。
-
/**
-
* 打印二维数组
-
*
-
* @param a 二维数组a
-
*/
-
public static void print(double[][] a) {
-
print(a, 5, 0);
-
}
-
-
/**
-
* 打印二维数组
-
*
-
* @param a 二维数组a
-
* @param precision 如果是浮点型的话,保留几位小数
-
*/
-
public static void print(int[][] a, int width, int precision) {
-
String str = "%" + width + "d ";
-
for (int i = 0; i < a.length; i++) {
-
int[] aItem = a[i];
-
for (int j = 0; j < aItem.length; j++) {
-
System.out.print(String.format(str, aItem[j]));
-
}
-
System.out.println();
-
}
-
}
-
-
/**
-
* 打印二维数组
-
*
-
* @param a 二维数组a
-
* @param precision 如果是浮点型的话,保留几位小数
-
*/
-
public static void print(double[][] a, int width, int precision) {
-
String str = "%" + width + "." + precision + "f ";
-
for (int i = 0; i < a.length; i++) {
-
double[] aItem = a[i];
-
for (int j = 0; j < aItem.length; j++) {
-
System.out.print(String.format(str, aItem[j]));
-
}
-
System.out.println();
-
}
-
}
二、减法。
1、介绍。
2、代码。
-
/**
-
* 矩阵a和b对应位置的数相减
-
*
-
* @param a 矩阵a[m][n]
-
* @param b 矩阵b[n][p]
-
*/
-
public static double[][] sub(double[][] a, double[][] b) throws Exception {
-
int row = a.length;
-
int column = a[0].length;
-
int bRow = b.length;
-
int bColumn = b[0].length;
-
if (row != bRow || column != bColumn) {
-
throw new Exception("两个矩阵的大小不一致");
-
}
-
double[][] result = new double[row][column];
-
for (int i = 0; i < row; i++)
-
for (int j = 0; j < column; j++)
-
result[i][j] = a[i][j] - b[i][j];
-
return result;
-
}
-
public static void main(String[] args) {
-
try {
-
double[][] a = new double[][]{
-
{1, 2, 3, 4},
-
{5, 6, 7, 8},
-
{1, 1, 2, 2},
-
{3, 3, 4, 4},
-
};
-
System.out.println("矩阵a:");
-
ArrayUtils.print(a);
-
double[][] b = new double[][]{
-
{3, 2, 1, 4},
-
{8, 6, 5, 8},
-
{1, 2, 3, 4},
-
{8, 7, 6, 5},
-
};
-
System.out.println("矩阵b:");
-
ArrayUtils.print(b);
-
double[][] c = MatrixUtils.sub(a, b);
-
System.out.println("矩阵a-b=c:");
-
ArrayUtils.print(c);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
三、加法
1、介绍。
2、代码。
-
/**
-
* 矩阵a和b对应位置的数相加
-
*
-
* @param a 矩阵a[m][n]
-
* @param b 矩阵b[n][p]
-
*/
-
public static double[][] add(double[][] a, double[][] b) throws Exception {
-
int row = a.length;
-
int column = a[0].length;
-
int bRow = b.length;
-
int bColumn = b[0].length;
-
if (row != bRow || column != bColumn) {
-
throw new Exception("两个矩阵的大小不一致");
-
}
-
double[][] result = new double[row][column];
-
for (int i = 0; i < row; i++)
-
for (int j = 0; j < column; j++)
-
result[i][j] = a[i][j] + b[i][j];
-
return result;
-
}
-
public static void main(String[] args) {
-
try {
-
double[][] a = new double[][]{
-
{1, 2, 3, 4},
-
{5, 6, 7, 8},
-
{1, 1, 2, 2},
-
{3, 3, 4, 4},
-
};
-
System.out.println("矩阵a:");
-
ArrayUtils.print(a);
-
double[][] b = new double[][]{
-
{3, 2, 1, 4},
-
{8, 6, 5, 8},
-
{1, 2, 3, 4},
-
{8, 7, 6, 5},
-
};
-
System.out.println("矩阵b:");
-
ArrayUtils.print(b);
-
//加法
-
double[][] c = MatrixUtils.add(a, b);
-
System.out.println("矩阵a+b=c:");
-
ArrayUtils.print(c);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
四、数乘
1、介绍。
n和m是数,A和B是矩阵:n(mA)=(nm)A , n(AB)=(nA)B=A(nB) , (n+m)A=nA+mA , n(A+B)=nA+nB
2、代码。
-
/**
-
* 矩阵数乘
-
*
-
* @param a 矩阵a
-
* @param multiplier 乘数multiplier
-
*/
-
public static double[][] mul(double[][] a, double multiplier) {
-
int row = a.length;
-
int column = a[0].length;
-
double[][] result = new double[row][column];
-
for (int i = 0; i < row; i++)
-
for (int j = 0; j < column; j++)
-
result[i][j] = a[i][j] * multiplier;
-
return result;
-
}
-
public static void main(String[] args) {
-
try {
-
double[][] a = new double[][]{
-
{1, 2, 3, 4},
-
{5, 6, 7, 8},
-
{1, 1, 2, 2},
-
{3, 3, 4, 4},
-
};
-
System.out.println("矩阵a:");
-
ArrayUtils.print(a);
-
//数乘
-
double[][] c = MatrixUtils.mul(a, 3);
-
System.out.println("矩阵a*3=c:");
-
ArrayUtils.print(c);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
五、转置
1、介绍。
2、代码。
-
/**
-
* 矩阵转置
-
*
-
* @param a 矩阵a
-
*/
-
public static double[][] transform(double[][] a) {
-
int row = a.length;
-
int column = a[0].length;
-
double[][] result = new double[column][row];
-
for (int i = 0; i < column; i++)
-
for (int j = 0; j < row; j++)
-
result[i][j] = a[j][i];
-
return result;
-
}
-
public static void main(String[] args) {
-
try {
-
double[][] a = new double[][]{
-
{1, 2},
-
{5, 6},
-
{1, 1},
-
{3, 3},
-
};
-
System.out.println("矩阵a:");
-
ArrayUtils.print(a);
-
double[][] c = MatrixUtils.transform(a);
-
System.out.println("矩阵a转置c:");
-
ArrayUtils.print(c);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
六、乘法
1、介绍。
A、B和C都为矩阵: ABC=A(BC) , (A+B)C=AC+AB ,
, AB != BA。两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵
,它的一个元素表示为:。2、代码。
-
/**
-
* 矩阵a[m][n]和矩阵b[n][p]相乘,返回c[m][p]
-
*
-
* @param a 矩阵a[m][n]
-
* @param b 矩阵b[n][p]
-
*/
-
public static double[][] mul(double[][] a, double[][] b) throws Exception {
-
int aRow = a.length;
-
int aColumn = a[0].length;
-
int bRow = b.length;
-
int bColumn = b[0].length;
-
if (aColumn != bRow) {
-
throw new Exception("a[m][n]和b[n][p],其中两个n不相等");
-
}
-
double[][] result = new double[aRow][bColumn];
-
//计算
-
for (int i = 0; i < aRow; i++) {
-
for (int j = 0; j < bColumn; j++) {
-
double sum = 0;
-
for (int k = 0; k < aColumn; k++) {
-
sum += (a[i][k] * b[k][j]);
-
}
-
result[i][j] = sum;
-
}
-
}
-
return result;
-
}
-
public static void main(String[] args) {
-
try {
-
double[][] a = new double[][]{
-
{1, 2, 3, 4},
-
{5, 6, 7, 8},
-
{1, 1, 2, 2},
-
{3, 3, 4, 4},
-
};
-
System.out.println("矩阵a:");
-
ArrayUtils.print(a);
-
double[][] b = new double[][]{
-
{3, 2, 1, 4},
-
{8, 6, 5, 8},
-
{1, 2, 3, 4},
-
{8, 7, 6, 5},
-
};
-
System.out.println("矩阵b:");
-
ArrayUtils.print(b);
-
//乘法
-
double[][] c = MatrixUtils.mul(a, b);
-
System.out.println("矩阵a*b=c:");
-
ArrayUtils.print(c);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}