it.ssc.vector_spaces.MatrixFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsr331-ssc Show documentation
Show all versions of jsr331-ssc Show documentation
This is a JSR331 interface for SSC (Software for the Calculation of the Simplex) is a java library for solving linear programming problems v. 3.0.1.
SSC was designed and developed by Stefano Scarioli.
The newest version!
package it.ssc.vector_spaces;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class MatrixFunction {
static final BigDecimal ZERO_BIG=new BigDecimal("0.0",MathContext.DECIMAL128);
static final BigDecimal NEG_ONE_BIG=new BigDecimal("-1.0",MathContext.DECIMAL128);
public static double determinant(BigMatrix matrix) throws MatrixException {
return detBigMatrix(matrix).setScale(30, RoundingMode.HALF_DOWN).doubleValue();
}
static BigDecimal detBigMatrix(BigMatrix matrix) throws MatrixException {
if(matrix.n_column!= matrix.n_row) throw new MatrixException("Per il calcolo del determinante la matrice deve essere quadrata");
BigDecimal det_a=ZERO_BIG;
BigDecimal appo_big;
if(matrix.n_column==1) {
return matrix.big_matrix[0][0];
}
else if(matrix.n_column==2) {
BigDecimal diag1=matrix.big_matrix[0][0].multiply(matrix.big_matrix[1][1], MathContext.DECIMAL128);
BigDecimal diag2=matrix.big_matrix[0][1].multiply(matrix.big_matrix[1][0], MathContext.DECIMAL128);
return diag1.subtract(diag2, MathContext.DECIMAL128);
}
else if(matrix.n_column>2) {
BigMatrix new_matrix;
int i_optimal=buildRowOptimal(matrix);
//navigo per le colonne sulla prima riga
for (int _j=0;_jrow_index) _ni=_i-1;
else _ni=_i;
if(_j >col_index) _nj=_j-1;
else _nj=_j;
new_matrix.big_matrix[_ni][_nj]= matrix.big_matrix[_i][_j];
}
}
return new_matrix;
}
public static BigMatrix inverse(BigMatrix matrix) throws MatrixException {
if(matrix.n_column!= matrix.n_row) throw new MatrixException("Per il calcolo dell'inversa la matrice deve essere quadrata");
int dim_n=matrix.n_column;
BigDecimal det_A=detBigMatrix(matrix);
if(det_A.compareTo(ZERO_BIG)==0) throw new MatrixException("Per il calcolo dell'inversa la matrice deve avere determinante diverso da zero");
BigMatrix matrix_inverse=new BigMatrix(dim_n,dim_n);
BigDecimal det_Aij;
BigMatrix Aij;
for(int _i=0;_i