java 线性得分_java 数学库 Commons Math 的学习,包含线性代数、分数、统计等

先创建maven项目,引入jar包

org.apache.commons

commons-math3

3.6.1

第一部分:线性代数

官方给出的例子

// Create a real matrix with two rows and three columns, using a factory

// method that selects the implementation class for us.

double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};

RealMatrix m = MatrixUtils.createRealMatrix(matrixData);

// One more with three rows, two columns, this time instantiating the

// RealMatrix implementation class directly.

double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};

RealMatrix n = new Array2DRowRealMatrix(matrixData2);

// Note: The constructor copies the input double[][] array in both cases.

// Now multiply m by n

RealMatrix p = m.multiply(n);

System.out.println(p.getRowDimension()); // 2

System.out.println(p.getColumnDimension()); // 2

// Invert p, using LU decomposition

RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();

For example, to solve the linear system

2x + 3y - 2z = 1

-x + 7y + 6x = -2

4x - 3y - 5z = 1

Start by decomposing the coefficient matrix A (in this case using LU decomposition) and build a solver

RealMatrix coefficients =

new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } },

false);

DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();

Next create aRealVectorarray to represent the constant vector B and usesolve(RealVector)to solve the system

RealVector constants = new ArrayRealVector(new double[] { 1, -2, 1 }, false);

RealVector solution = solver.solve(constants);

Thesolutionvector will contain values for x (solution.getEntry(0)), y (solution.getEntry(1)), and z (solution.getEntry(2)) that solve the system.

第二部分:分数

官方给出的例子

A fraction number, can be built from two integer arguments representing numerator and denominator or from a double which will be approximated:

Fraction f = new Fraction(1, 3); // 1 / 3

Fraction g = new Fraction(0.25); // 1 / 4

Of special note with fraction construction, when a fraction is created it is always reduced to lowest terms.

TheFractionclass provides many unary and binary fraction operations. These operations provide the means to add, subtract, multiple and, divide fractions along with other functions similar to the real number functions found injava.math.BigDecimal:

Fraction lhs = new Fraction(1, 3);

Fraction rhs = new Fraction(2, 5);

Fraction answer = lhs.add(rhs); // add two fractions

answer = lhs.subtract(rhs); // subtract two fractions

answer = lhs.abs(); // absolute value

answer = lhs.reciprocal(); // reciprocal of lhs

Like fraction construction, for each of the fraction functions, the resulting fraction is reduced to lowest terms.

完整代码如下

importorg.apache.commons.math3.fraction.Fraction;

importorg.apache.commons.math3.linear.*;

importorg.apache.commons.math3.util.FastMath;

public classTrilateration {

public static voidmain(String[] args) {

//1. 支持对数计算 log10^100 2.0System.out.println(FastMath.log(10,100));

System.out.println("=====================");

//2. 线性代数/*2.1 两个矩阵相乘1 2 3 1 32 5 3 * 2 5 => 1*1+2*2+3*1 1*2+2*5+3*7 => 8 331 7 2*1+5*2+3*1 2*2+5*5+3*7 15 50*/// 2行3列double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};

RealMatrix m = MatrixUtils.createRealMatrix(matrixData);

// 3行2列double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};

RealMatrix n = newArray2DRowRealMatrix(matrixData2);

// m*nRealMatrix p = m.multiply(n);

System.out.println(p.getRowDimension()); // 2 行System.out.println(p.getColumnDimension()); // 2 列

System.out.println("=====================");

double[][] data = p.getData();

/*8.0 33.015.0 50.0*/for(inti = 0; i < data.length; i++) { //遍历行for(intj = 0;j

}

System.out.println();

}

System.out.println("=====================");

/*2.2 求解线性方程组3x1 - 2x2 = 122x1 + x2 = 1 => x1=2 x2=-3*/RealMatrix coefficients =

newArray2DRowRealMatrix(new double[][] { { 3, -2}, { 2, 1} },

false);

DecompositionSolver solver = newLUDecomposition(coefficients).getSolver();

RealVector constants = newArrayRealVector(new double[] { 12, 1}, false);

RealVector solution = solver.solve(constants);

// 1.9999999999999998System.out.println(solution.getEntry(0));

//-3.0000000000000004System.out.println(solution.getEntry(1));

System.out.println("=====================");

// 转置double[] [] transpose_data = { {20d,50d,16d}, {35d,26d,18d}};

RealMatrix transpose_m = MatrixUtils.createRealMatrix(transpose_data);

double[][] data1 = transpose_m.transpose().getData();

/*

矩阵20 50 1635 26 18

得到的转置矩阵--行变列,列变行20.0 35.050.0 26.016.0 18.0*/for(inti = 0; i < data1.length; i++) { //遍历行for(intj = 0;j

}

System.out.println();

}

System.out.println("================");

/*矩阵1 02 4逆矩阵 ---- 任何一个矩阵乘以它的逆矩阵等于单位矩阵1.0 0.0-0.5 0.25*/double[][] matrixData3 = { {1d,0d}, {2d,4d}};

RealMatrix n1 = newArray2DRowRealMatrix(matrixData3);

RealMatrix pInverse = newLUDecomposition(n1).getSolver().getInverse();//转换成逆矩阵double[][] data2 = pInverse.getData();

for(inti = 0; i < data2.length; i++) { //遍历行for(intj = 0;j

}

System.out.println();

}

System.out.println("================");

// Fraction---分数Fraction f = newFraction(1, 3); // 1 / 3System.out.println(f.getNumerator()); //分子 1System.out.println(f.getDenominator()); //分母 3

Fraction lhs = newFraction(1, 3);

Fraction rhs = newFraction(2, 5);

Fraction answer = lhs.add(rhs); // add two fractions 相加 1/3+2/5= 5/15+6/15=11/15System.out.println(answer.toString()); // 11/15

answer = lhs.subtract(rhs); // subtract two fractions 减去 1/3-2/5= 5/15-6/15=-1/15System.out.println(answer.toString()); // -1/15

answer = lhs.divide(rhs); // divide two fractions 除法 (1/3)/(2/5)= 1/3 * 5/2 = 5/6System.out.println(answer.toString()); // 5 / 6

answer = lhs.multiply(rhs); // multiply two fractions 减去 1/3 * 2/5= 2 / 15System.out.println(answer.toString()); // 2 / 15

answer = lhs.abs(); // absolute value 绝对值System.out.println(answer.toString()); // 1/3

answer = lhs.reciprocal(); // reciprocal of lhs 倒数 1/3---> 3System.out.println(answer.toString()); //3

}

}

运行结果截图

191d60b218427235e73756df2497047b.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值