搜索了下java的矩阵运算工具,网上有很多。这篇博文总结的特别好,https://www.cnblogs.com/jpfss/p/11533902.html,大家可以拿去。
我试用了下ujmp包,觉得还是挺好用的,网上的资料不多。特分享在这里。
依赖pom
<dependency> <groupId>org.ujmp</groupId> <artifactId>ujmp-core</artifactId> <version>0.3.0</version> </dependency>
试用代码
public class MatrixTest { public static void main(String[] args) { DenseDoubleMatrix2D zeros = DenseMatrix.Factory.zeros(2, 2); DenseDoubleMatrix2D ones = DenseMatrix.Factory.zeros(2, 2); zeros.setAsDouble(2, 0, 0); zeros.setAsDouble(5, 0, 1); zeros.setAsDouble(-3, 1, 0); zeros.setAsDouble(-7, 1, 1); ones.setAsDouble(2, 0, 0); ones.setAsDouble(1, 0, 1); ones.setAsDouble(3, 1, 0); ones.setAsDouble(4, 1, 1); System.out.println("当前矩阵:"); System.out.println(zeros); Matrix transpose = zeros.transpose(); System.out.println("矩阵的转置:"); System.out.println(transpose); Matrix inv = zeros.inv(); System.out.println("矩阵的逆:"); System.out.println(inv); System.out.println("矩阵的维数:"); System.out.println(zeros.getDimensionCount()); System.out.println("矩阵的秩:"); System.out.println(zeros.rank()); Matrix plus = zeros.plus(ones); System.out.println("矩阵求和:"); System.out.println(plus); Matrix minus = zeros.minus(ones); System.out.println("矩阵求差:"); System.out.println(minus); Matrix mtimes = zeros.mtimes(ones); System.out.println("矩阵求积:"); System.out.println(mtimes); //矩阵和数值的四则运算 Matrix plus1 = zeros.plus(2); Matrix minus1 = zeros.minus(2); Matrix times = zeros.times(2); Matrix divide = zeros.divide(2); System.out.println("矩阵数值运算:"); System.out.println(times); double det = zeros.det(); System.out.println("矩阵行列式:"); System.out.println(det); Matrix[] eig = zeros.eig(); System.out.println("矩阵特征值:"); for (Matrix matrix : eig) { System.out.println(matrix); } double valueSum = zeros.getValueSum(); long rowCount = zeros.getRowCount(); long valueCount = zeros.getValueCount(); long valueCount1 = zeros.getValueCount(); System.out.println("所有元素求和:" + valueSum); ValueType valueType = zeros.getValueType(); System.out.println("所有元素类型:" + valueType); Matrix randMatrix = Matrix.Factory.rand(4, 4); System.out.println("随机矩阵:"); System.out.println(randMatrix); Matrix matrix = randMatrix.subMatrix(Ret.LINK, 1, 1, 2, 2); System.out.println("随机子矩阵:"); System.out.println(matrix); Matrix matrix1 = randMatrix.selectRows(Ret.LINK, 1, 2); System.out.println("选取行:"); System.out.println(matrix1); Matrix matrix2 = randMatrix.selectColumns(Ret.LINK, 2, 3); System.out.println("选取列:"); System.out.println(matrix2); Matrix eq = zeros.eq(Ret.LINK, ones); //相等的话在相应的位置设置为true否则为false System.out.println("矩阵是否相等:"); System.out.println(eq); System.out.println(ones); //对行排序? //有三种返回型,Ret.LINK Ret.ORIG, Ret.NEW 计算时间new > orig > link Matrix sortrows = ones.sortrows(Ret.LINK, 0, true); System.out.println("对行排序:"); System.out.println(sortrows); } /** * 常用构造方法 */ public static void test() { long m = 5; long n = 5; /** * 制造一个空矩阵 */ DefaultDenseMatrixFactory factory = Matrix.Factory; Matrix emptyMatrix = factory.emptyMatrix(); /** * 制造一个m*n随机矩阵 */ Matrix randMatrix = factory.rand(m, n); /** * 制造一个m*n零矩阵 */ Matrix zeroMatrix = factory.zeros(m, n); /** * 制造一个m*n对角线为1其余元素为0的矩阵 */ Matrix eyeMatrix = factory.eye(m, n); /** * 制造一个m*n全部元素为1的矩阵 */ Matrix oneMatrix = factory.ones(m, n); } }