Java实现的矩阵类及矩阵的转置,加减乘和矩阵求逆

    xiaoxiao2021-03-25  99

    最近因为一些原因重新回到了java的怀抱,作为自己的第一个自学的纯面向对象的语言,对它的感情其实还是蛮深的,最近又在课上听到老师说过一个关于矩阵求逆的小程序,自己一时手热边用java完成了关于矩阵的功能。

    首先说矩阵,相信很多工科的同胞们都被其饱受折磨。可它的应用范围极广我们也不得不学,总之又爱又恨。关于矩阵类的实现,绝大多数C++程序员是用数组和指针实现的,大部分java工程师也是用数组实现的。而且我在网上搜过相关的博文,大部分人都只完成了int型一种矩阵,笔者这次在JAVA的Vector的前提下完成了int,float,double三种类型的矩阵,也算是对java的容器和数据类型及运算做了一个总结,说实话java确实不适合数据运算,它不支持运算符重载的缺陷型让一些数据计算变得很繁琐。有兴趣的笔友们可以在python或者C++的基础上完成矩阵类的编写。

    一,如何实现矩阵类

    public Vector data; public final static int MAT_INT = 1; public final static int MAT_FLOAT = 2; public final static int MAT_DOUBLE = 3; public int MAT_TYPE = 1; public int rows; public int cols; 从上面我们可以看到,笔者定义了一个存放矩阵数据的容器,三种矩阵类型参数,分别代表了int,float,double以及代表矩阵类型的MAT_TYPE,我们默认矩阵是int型的,毕竟这种类型最多。另外还有矩阵的大小参数rows,cols即矩阵的行数,列数。

    /** * @param rows 矩阵的行数,相当于y坐标 * @param cols 矩阵的列数,相当如x坐标 * @param type 矩阵的类型,int,float,double三种 */ public Mat(int rows, int cols, int type) { this.rows = rows; this.cols = cols; switch (type) { case MAT_INT: int zerosi = 0; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Integer>(cols); for (int j = 0; j < cols; j++) { v.add(j, zerosi); } data.add(i, v); } break; case MAT_FLOAT: float zerosf = 0; this.MAT_TYPE = 2; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Float>(cols); for (int j = 0; j < cols; j++) { v.add(j, zerosf); } data.add(i, v); } break; case MAT_DOUBLE: this.MAT_TYPE = 3; float zerosd = 0; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Double>(cols); for (int j = 0; j < cols; j++) { v.add(j, zerosd); } data.add(i, v); } break; default: break; } } public Mat(int rows, int cols) { this.rows = rows; this.cols = cols; int zeros = 0; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Integer>(cols); for (int j = 0; j < cols; j++) { v.add(j, zeros); } data.add(i, v); } } public Mat() { this(5, 5); } /** * @param num 以数组的方式来构造矩阵 */ public Mat(int num[][]) { this.rows = num.length; this.cols = num[0].length; this.MAT_TYPE = MAT_INT; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Integer>(cols); for (int j = 0; j < cols; j++) { v.add(j, num[i][j]); } data.add(i, v); } } public Mat(float num[][]) { this.rows = num.length; this.cols = num[0].length; this.MAT_TYPE = MAT_FLOAT; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Float>(cols); for (int j = 0; j < cols; j++) { v.add(j, num[i][j]); } data.add(i, v); } } public Mat(double num[][]) { this.rows = num.length; this.cols = num[0].length; this.MAT_TYPE = MAT_DOUBLE; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Double>(cols); for (int j = 0; j < cols; j++) { v.add(j, num[i][j]); } data.add(i, v); } }

    笔者定义了很多的Mat类的构造函数,包括默认的int型初始化和指定类型的初始化和相应的数组初始化。

    /** * 输出矩阵中的内容 */ public void print() { System.out.println("["); for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { System.out.print(v.get(j) + ","); } System.out.println(); } System.out.println("]"); } /** * 将矩阵中的元素全部置0 */ public void zeros() { if (MAT_TYPE == MAT_INT) { int zerosi = 0; for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, zerosi); } data.set(i, v); } } else if (MAT_TYPE == MAT_FLOAT) { float zerosf = 0; for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, zerosf); } data.set(i, v); } } else { for (int i = 0; i < rows; i++) { double zerosd = 0; Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, zerosd); } data.set(i, v); } } } /** * 将矩阵中的元素全部置1 */ public void ones() { if (MAT_TYPE == MAT_INT) { int onesi = 1; for (int x = 0; x < rows; x++) { Vector v = (Vector) data.get(x); for (int y = 0; y < cols; y++) { v.set(y, onesi); } data.set(x, v); } } else if (MAT_TYPE == MAT_FLOAT) { float onesf = 1; for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, onesf); } data.set(i, v); } } else { for (int i = 0; i < rows; i++) { double onesd = 1; Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, onesd); } data.set(i, v); } } }

    上面我们还可以看到笔者定义了三个很有用的成员函数,print()输出矩阵的内容,zeros()将矩阵数据全部置0,ones将矩阵的数据全部置1。

    接下来便是我们矩阵类的核心函数了:

    /** * 矩阵转置 */ public void T() { if (MAT_TYPE == MAT_INT) { Vector t = new Vector<Vector>(cols); for (int i = 0; i < cols; i++) { Vector v = new Vector<Integer>(rows); for (int j = 0; j < rows; j++) { Vector oldc = (Vector) data.get(j); v.add(j, oldc.get(i)); } t.add(i, v); } data = t; int tmp = this.rows; this.rows = this.cols; this.cols = tmp; } else if (MAT_TYPE == MAT_FLOAT) { Vector t = new Vector<Vector>(cols); for (int i = 0; i < cols; i++) { Vector v = new Vector<Float>(rows); for (int j = 0; j < rows; j++) { Vector oldc = (Vector) data.get(j); v.add(j, oldc.get(i)); } t.add(i, v); } data = t; int tmp = this.rows; this.rows = this.cols; this.cols = tmp; } else { Vector t = new Vector<Vector>(cols); for (int i = 0; i < cols; i++) { Vector v = new Vector<Double>(rows); for (int j = 0; j < rows; j++) { Vector oldc = (Vector) data.get(j); v.add(j, oldc.get(i)); } t.add(i, v); } data = t; int tmp = this.rows; this.rows = this.cols; this.cols = tmp; } } /** * 设置矩阵中某个坐标的元素内容 * * @param rowindex y坐标 * @param colindex x坐标 * @param num 要输入的内容 */ public void Set(int rowindex, int colindex, Object num) { Vector oldc = (Vector) data.get(rowindex); if (MAT_TYPE == MAT_INT) { int score = Integer.parseInt(num.toString()); oldc.set(colindex, score); } else if (MAT_TYPE == MAT_FLOAT) { float score = Float.parseFloat(num.toString()); oldc.set(colindex, score); } else { double score = Double.parseDouble(num.toString()); oldc.set(colindex, score); } } /** * 得到矩阵中某个坐标元素的内容 * * @param rowindex y坐标 * @param colindex x坐标 * @return 返回一个object类型 */ public Object Get(int rowindex, int colindex) { Vector oldc = (Vector) data.get(rowindex); return oldc.get(colindex); } /** * 矩阵行列式求值 * * @param num 矩阵的数据,以数组输入 * @param n 矩阵的行列数 * @return 返回行列式的值 */ public int getvalue(int num[][], int n) { int value = 0; if (n == 1) return num[0][0]; int temp[][] = new int[n - 1][n - 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { for (int k = 0; k < n - 1; k++) { int flag; if (j < i) flag = 0; else flag = 1; temp[j][k] = num[j + flag][k + 1]; } } int flag2 = -1; if (i % 2 == 0) flag2 = 1; value += flag2 * num[i][0] * getvalue(temp, n - 1); } return value; } /** * 矩阵行列式求值 * * @return */ public int dot() { if (MAT_TYPE != MAT_INT || rows != cols) { return -1; } int value[][] = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Object o = Get(i, j); value[i][j] = Integer.parseInt(o.toString()); } } int fianl = getvalue(value, rows); return fianl; } /** * 矩阵相加 * * @param m1 矩阵1 * @param m2 矩阵2 * @return 返回相加的矩阵 */ public static Mat add(Mat m1, Mat m2) { int outtype = 1; if (m1.MAT_TYPE >= m2.MAT_TYPE) { outtype = m1.MAT_TYPE; } else { outtype = m2.MAT_TYPE; } Mat out = new Mat(m1.rows, m1.cols, outtype); if (m1.rows != m2.rows || m1.cols != m1.rows) { return null; } else { for (int i = 0; i < m1.rows; i++) { for (int j = 0; j < m1.cols; j++) { Object o1 = m1.Get(i, j); Object o2 = m2.Get(i, j); float f1 = Float.parseFloat(o1.toString()); float f2 = Float.parseFloat(o2.toString()); float f3 = f1 + f2; out.Set(i, j, f3); } } } return out; } /** * 矩阵相加 * * @param m1 被减数矩阵 * @param m2 减数矩阵 * @return 返回相减矩阵 */ public static Mat sub(Mat m1, Mat m2) { int outtype = 1; if (m1.MAT_TYPE >= m2.MAT_TYPE) { outtype = m1.MAT_TYPE; } else { outtype = m2.MAT_TYPE; } Mat out = new Mat(m1.rows, m1.cols, outtype); if (m1.rows != m2.rows || m1.cols != m1.rows) { return null; } else { for (int i = 0; i < m1.rows; i++) { for (int j = 0; j < m1.cols; j++) { Object o1 = m1.Get(i, j); Object o2 = m2.Get(i, j); float f1 = Float.parseFloat(o1.toString()); float f2 = Float.parseFloat(o2.toString()); float f3 = f1 - f2; out.Set(i, j, f3); } } } return out; } /** * 矩阵相乘 * * @param m1 矩阵1 * @param m2 矩阵2 * @return 返回相乘矩阵 */ public static Mat mul(Mat m1, Mat m2) { //定义输出矩阵的类型,从m1,m2中选出MAT_TYPE较大的类型为输出矩阵的类型 int outtype = 1; if (m1.MAT_TYPE >= m2.MAT_TYPE) { outtype = m1.MAT_TYPE; } else { outtype = m2.MAT_TYPE; } //按照矩阵的乘法,m1.cols=m2.rows成立时才有意义,输出的矩阵的行数等于m1的行数,列数等于m2的列数 Mat out = new Mat(m1.rows, m2.cols, outtype); if (m1.cols != m2.rows) { return null; } else { //根据判断出的输出矩阵的类型来判断 switch (outtype) { case MAT_INT: int value1[] = new int[m1.cols];//m1每一行的数据存储的数组 int value2[] = new int[m2.rows];//m2每一列的数据存储的数组 for (int i = 0; i < m1.rows; i++) { for (int col = 0; col < m1.cols; col++) { //得到m1每一行上面的数据存储到数组中 Object o1 = m1.Get(i, col); value1[col] = Integer.parseInt(o1.toString()); } for (int j = 0; j < m2.cols; j++) { for (int row = 0; row < m2.rows; row++) { //得到m2每一列上的数据存储到数组中 Object o2 = m2.Get(row, j); value2[row] = Integer.parseInt(o2.toString()); } //输出矩阵上面将要装入的数据 int value = 0; //计算输出矩阵上的数据 for (int l = 0; l < value1.length; l++) { value += value1[l] * value2[l]; } //设置输出矩阵上的数据 out.Set(i, j, value); } } break; //同上,只是改了数据的类型 case MAT_FLOAT: float value11[] = new float[m1.cols]; float value22[] = new float[m2.rows]; for (int i = 0; i < m1.rows; i++) { for (int col = 0; col < m1.cols; col++) { Object o1 = m1.Get(i, col); value11[col] = Float.parseFloat(o1.toString()); } for (int j = 0; j < m2.cols; j++) { for (int row = 0; row < m2.rows; row++) { Object o2 = m2.Get(row, j); value22[row] = Float.parseFloat(o2.toString()); } float value = 0; for (int l = 0; l < value11.length; l++) { value += value11[l] * value22[l]; } out.Set(i, j, value); } } break; case MAT_DOUBLE: double value111[] = new double[m1.cols]; double value222[] = new double[m2.rows]; for (int i = 0; i < m1.rows; i++) { for (int col = 0; col < m1.cols; col++) { Object o1 = m1.Get(i, col); value111[col] = Double.parseDouble(o1.toString()); } for (int j = 0; j < m2.cols; j++) { for (int row = 0; row < m2.rows; row++) { Object o2 = m2.Get(row, j); value222[row] = Double.parseDouble(o2.toString()); } double value = 0; for (int l = 0; l < value111.length; l++) { value += value111[l] * value222[l]; } out.Set(i, j, value); } } break; default: break; } } return out; } /** * 矩阵求逆 * 相当如矩阵除法 * * @param m 输入矩阵 * @return 返回矩阵的逆矩阵 */ public static Mat inv(Mat m) { if (m.MAT_TYPE != MAT_INT || m.rows != m.cols) { return null; } int n = m.rows; int value[][] = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Object o = m.Get(i, j); value[i][j] = Integer.parseInt(o.toString()); } } int result[][] = value; int resultSum = m.getvalue(value, n); int temp[][] = new int[n - 1][n - 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n - 1; k++) { for (int g = 0; g < n - 1; g++) { int flag1 = 0; int flag2 = 0; if (k < i) flag1 = 0; else flag1 = 1; if (g < j) flag2 = 0; else flag2 = 1; temp[k][g] = value[k + flag1][g + flag2]; } } int flag3 = -1; if ((i + j) % 2 == 0) flag3 = 1; result[j][i] = (int) flag3 * m.getvalue(temp, n - 1) / resultSum; } } Mat out = new Mat(result); return out; }

    从上面我们看到矩阵一些所需要的计算和功能,当然除了矩阵求逆,其他一些功能都相对简单,矩阵求逆笔者也参考了很多前辈的算法,可是很难找到一个快速精准的算法。有关上面的程序,笔者上面有相对详细的注释,这里笔者便不累述了,好了一下分享完成的代码:

    package PMat; import java.util.Vector; public class Mat { /** * Java实现的矩阵类 * author:Pedro * Date:2017.4.1 */ public Vector data; public final static int MAT_INT = 1; public final static int MAT_FLOAT = 2; public final static int MAT_DOUBLE = 3; public int MAT_TYPE = 1; public int rows; public int cols; /** * @param rows 矩阵的行数,相当于y坐标 * @param cols 矩阵的列数,相当如x坐标 * @param type 矩阵的类型,int,float,double三种 */ public Mat(int rows, int cols, int type) { this.rows = rows; this.cols = cols; switch (type) { case MAT_INT: int zerosi = 0; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Integer>(cols); for (int j = 0; j < cols; j++) { v.add(j, zerosi); } data.add(i, v); } break; case MAT_FLOAT: float zerosf = 0; this.MAT_TYPE = 2; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Float>(cols); for (int j = 0; j < cols; j++) { v.add(j, zerosf); } data.add(i, v); } break; case MAT_DOUBLE: this.MAT_TYPE = 3; float zerosd = 0; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Double>(cols); for (int j = 0; j < cols; j++) { v.add(j, zerosd); } data.add(i, v); } break; default: break; } } public Mat(int rows, int cols) { this.rows = rows; this.cols = cols; int zeros = 0; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Integer>(cols); for (int j = 0; j < cols; j++) { v.add(j, zeros); } data.add(i, v); } } public Mat() { this(5, 5); } /** * @param num 以数组的方式来构造矩阵 */ public Mat(int num[][]) { this.rows = num.length; this.cols = num[0].length; this.MAT_TYPE = MAT_INT; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Integer>(cols); for (int j = 0; j < cols; j++) { v.add(j, num[i][j]); } data.add(i, v); } } public Mat(float num[][]) { this.rows = num.length; this.cols = num[0].length; this.MAT_TYPE = MAT_FLOAT; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Float>(cols); for (int j = 0; j < cols; j++) { v.add(j, num[i][j]); } data.add(i, v); } } public Mat(double num[][]) { this.rows = num.length; this.cols = num[0].length; this.MAT_TYPE = MAT_DOUBLE; data = new Vector<Vector>(rows); for (int i = 0; i < rows; i++) { Vector v = new Vector<Double>(cols); for (int j = 0; j < cols; j++) { v.add(j, num[i][j]); } data.add(i, v); } } /** * 输出矩阵中的内容 */ public void print() { System.out.println("["); for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { System.out.print(v.get(j) + ","); } System.out.println(); } System.out.println("]"); } /** * 将矩阵中的元素全部置0 */ public void zeros() { if (MAT_TYPE == MAT_INT) { int zerosi = 0; for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, zerosi); } data.set(i, v); } } else if (MAT_TYPE == MAT_FLOAT) { float zerosf = 0; for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, zerosf); } data.set(i, v); } } else { for (int i = 0; i < rows; i++) { double zerosd = 0; Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, zerosd); } data.set(i, v); } } } /** * 将矩阵中的元素全部置1 */ public void ones() { if (MAT_TYPE == MAT_INT) { int onesi = 1; for (int x = 0; x < rows; x++) { Vector v = (Vector) data.get(x); for (int y = 0; y < cols; y++) { v.set(y, onesi); } data.set(x, v); } } else if (MAT_TYPE == MAT_FLOAT) { float onesf = 1; for (int i = 0; i < rows; i++) { Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, onesf); } data.set(i, v); } } else { for (int i = 0; i < rows; i++) { double onesd = 1; Vector v = (Vector) data.get(i); for (int j = 0; j < cols; j++) { v.set(j, onesd); } data.set(i, v); } } } /** * 矩阵转置 */ public void T() { if (MAT_TYPE == MAT_INT) { Vector t = new Vector<Vector>(cols); for (int i = 0; i < cols; i++) { Vector v = new Vector<Integer>(rows); for (int j = 0; j < rows; j++) { Vector oldc = (Vector) data.get(j); v.add(j, oldc.get(i)); } t.add(i, v); } data = t; int tmp = this.rows; this.rows = this.cols; this.cols = tmp; } else if (MAT_TYPE == MAT_FLOAT) { Vector t = new Vector<Vector>(cols); for (int i = 0; i < cols; i++) { Vector v = new Vector<Float>(rows); for (int j = 0; j < rows; j++) { Vector oldc = (Vector) data.get(j); v.add(j, oldc.get(i)); } t.add(i, v); } data = t; int tmp = this.rows; this.rows = this.cols; this.cols = tmp; } else { Vector t = new Vector<Vector>(cols); for (int i = 0; i < cols; i++) { Vector v = new Vector<Double>(rows); for (int j = 0; j < rows; j++) { Vector oldc = (Vector) data.get(j); v.add(j, oldc.get(i)); } t.add(i, v); } data = t; int tmp = this.rows; this.rows = this.cols; this.cols = tmp; } } /** * 设置矩阵中某个坐标的元素内容 * * @param rowindex y坐标 * @param colindex x坐标 * @param num 要输入的内容 */ public void Set(int rowindex, int colindex, Object num) { Vector oldc = (Vector) data.get(rowindex); if (MAT_TYPE == MAT_INT) { int score = Integer.parseInt(num.toString()); oldc.set(colindex, score); } else if (MAT_TYPE == MAT_FLOAT) { float score = Float.parseFloat(num.toString()); oldc.set(colindex, score); } else { double score = Double.parseDouble(num.toString()); oldc.set(colindex, score); } } /** * 得到矩阵中某个坐标元素的内容 * * @param rowindex y坐标 * @param colindex x坐标 * @return 返回一个object类型 */ public Object Get(int rowindex, int colindex) { Vector oldc = (Vector) data.get(rowindex); return oldc.get(colindex); } /** * 矩阵行列式求值 * * @param num 矩阵的数据,以数组输入 * @param n 矩阵的行列数 * @return 返回行列式的值 */ public int getvalue(int num[][], int n) { int value = 0; if (n == 1) return num[0][0]; int temp[][] = new int[n - 1][n - 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { for (int k = 0; k < n - 1; k++) { int flag; if (j < i) flag = 0; else flag = 1; temp[j][k] = num[j + flag][k + 1]; } } int flag2 = -1; if (i % 2 == 0) flag2 = 1; value += flag2 * num[i][0] * getvalue(temp, n - 1); } return value; } /** * 矩阵行列式求值 * * @return */ public int dot() { if (MAT_TYPE != MAT_INT || rows != cols) { return -1; } int value[][] = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Object o = Get(i, j); value[i][j] = Integer.parseInt(o.toString()); } } int fianl = getvalue(value, rows); return fianl; } /** * 矩阵相加 * * @param m1 矩阵1 * @param m2 矩阵2 * @return 返回相加的矩阵 */ public static Mat add(Mat m1, Mat m2) { int outtype = 1; if (m1.MAT_TYPE >= m2.MAT_TYPE) { outtype = m1.MAT_TYPE; } else { outtype = m2.MAT_TYPE; } Mat out = new Mat(m1.rows, m1.cols, outtype); if (m1.rows != m2.rows || m1.cols != m1.rows) { return null; } else { for (int i = 0; i < m1.rows; i++) { for (int j = 0; j < m1.cols; j++) { Object o1 = m1.Get(i, j); Object o2 = m2.Get(i, j); float f1 = Float.parseFloat(o1.toString()); float f2 = Float.parseFloat(o2.toString()); float f3 = f1 + f2; out.Set(i, j, f3); } } } return out; } /** * 矩阵相加 * * @param m1 被减数矩阵 * @param m2 减数矩阵 * @return 返回相减矩阵 */ public static Mat sub(Mat m1, Mat m2) { int outtype = 1; if (m1.MAT_TYPE >= m2.MAT_TYPE) { outtype = m1.MAT_TYPE; } else { outtype = m2.MAT_TYPE; } Mat out = new Mat(m1.rows, m1.cols, outtype); if (m1.rows != m2.rows || m1.cols != m1.rows) { return null; } else { for (int i = 0; i < m1.rows; i++) { for (int j = 0; j < m1.cols; j++) { Object o1 = m1.Get(i, j); Object o2 = m2.Get(i, j); float f1 = Float.parseFloat(o1.toString()); float f2 = Float.parseFloat(o2.toString()); float f3 = f1 - f2; out.Set(i, j, f3); } } } return out; } /** * 矩阵相乘 * * @param m1 矩阵1 * @param m2 矩阵2 * @return 返回相乘矩阵 */ public static Mat mul(Mat m1, Mat m2) { //定义输出矩阵的类型,从m1,m2中选出MAT_TYPE较大的类型为输出矩阵的类型 int outtype = 1; if (m1.MAT_TYPE >= m2.MAT_TYPE) { outtype = m1.MAT_TYPE; } else { outtype = m2.MAT_TYPE; } //按照矩阵的乘法,m1.cols=m2.rows成立时才有意义,输出的矩阵的行数等于m1的行数,列数等于m2的列数 Mat out = new Mat(m1.rows, m2.cols, outtype); if (m1.cols != m2.rows) { return null; } else { //根据判断出的输出矩阵的类型来判断 switch (outtype) { case MAT_INT: int value1[] = new int[m1.cols];//m1每一行的数据存储的数组 int value2[] = new int[m2.rows];//m2每一列的数据存储的数组 for (int i = 0; i < m1.rows; i++) { for (int col = 0; col < m1.cols; col++) { //得到m1每一行上面的数据存储到数组中 Object o1 = m1.Get(i, col); value1[col] = Integer.parseInt(o1.toString()); } for (int j = 0; j < m2.cols; j++) { for (int row = 0; row < m2.rows; row++) { //得到m2每一列上的数据存储到数组中 Object o2 = m2.Get(row, j); value2[row] = Integer.parseInt(o2.toString()); } //输出矩阵上面将要装入的数据 int value = 0; //计算输出矩阵上的数据 for (int l = 0; l < value1.length; l++) { value += value1[l] * value2[l]; } //设置输出矩阵上的数据 out.Set(i, j, value); } } break; //同上,只是改了数据的类型 case MAT_FLOAT: float value11[] = new float[m1.cols]; float value22[] = new float[m2.rows]; for (int i = 0; i < m1.rows; i++) { for (int col = 0; col < m1.cols; col++) { Object o1 = m1.Get(i, col); value11[col] = Float.parseFloat(o1.toString()); } for (int j = 0; j < m2.cols; j++) { for (int row = 0; row < m2.rows; row++) { Object o2 = m2.Get(row, j); value22[row] = Float.parseFloat(o2.toString()); } float value = 0; for (int l = 0; l < value11.length; l++) { value += value11[l] * value22[l]; } out.Set(i, j, value); } } break; case MAT_DOUBLE: double value111[] = new double[m1.cols]; double value222[] = new double[m2.rows]; for (int i = 0; i < m1.rows; i++) { for (int col = 0; col < m1.cols; col++) { Object o1 = m1.Get(i, col); value111[col] = Double.parseDouble(o1.toString()); } for (int j = 0; j < m2.cols; j++) { for (int row = 0; row < m2.rows; row++) { Object o2 = m2.Get(row, j); value222[row] = Double.parseDouble(o2.toString()); } double value = 0; for (int l = 0; l < value111.length; l++) { value += value111[l] * value222[l]; } out.Set(i, j, value); } } break; default: break; } } return out; } /** * 矩阵求逆 * 相当如矩阵除法 * * @param m 输入矩阵 * @return 返回矩阵的逆矩阵 */ public static Mat inv(Mat m) { if (m.MAT_TYPE != MAT_INT || m.rows != m.cols) { return null; } int n = m.rows; int value[][] = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Object o = m.Get(i, j); value[i][j] = Integer.parseInt(o.toString()); } } int result[][] = value; int resultSum = m.getvalue(value, n); int temp[][] = new int[n - 1][n - 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n - 1; k++) { for (int g = 0; g < n - 1; g++) { int flag1 = 0; int flag2 = 0; if (k < i) flag1 = 0; else flag1 = 1; if (g < j) flag2 = 0; else flag2 = 1; temp[k][g] = value[k + flag1][g + flag2]; } } int flag3 = -1; if ((i + j) % 2 == 0) flag3 = 1; result[j][i] = (int) flag3 * m.getvalue(temp, n - 1) / resultSum; } } Mat out = new Mat(result); return out; } }

    转载请注明原文地址: https://ju.6miu.com/read-19601.html

    最新回复(0)