import java.text.DecimalFormat;
import java.util.concurrent.Semaphore;
public class Matrix
{
private boolean DEBUG = false;
private boolean INFO = false;
private int iDF = 0;
private float[][] matrix;
public Matrix(Matrix m){
this.matrix = m.getMatrix();
}
public Matrix(int size) {
matrix = new float[size][size];
}
public Matrix(int sizeX,int sizeY) {
matrix = new float[sizeY][sizeX];
}
public Matrix(float[][] m) {
matrix = new float[m.length][m[0].length];
matrix = m;
}
public void SetMatrix(float[][] m) {
if(m.length==matrix.length && m[0].length==matrix[0].length)
{
this.matrix = m;
}
}
public float[][] getMatrix() {
return this.matrix;
}
public void print()
{
for(int i = 0; i= tms) // check if switched all rows
{
iDF = 0;
break outahere;
} else {
for (int c = 0; c < tms; c++) {
temp = m[col][c];
m[col][c] = m[col + v][c]; // switch rows
m[col + v][c] = temp;
}
v++; // count row switchs
iDF = iDF * -1; // each switch changes determinant
// factor
}
}
if (m[col][col] != 0) {
if (DEBUG) {
System.out.println("tms = " + tms + " col = " + col
+ " row = " + row);
}
try {
f1 = (-1) * m[row][col] / m[col][col];
for (int i = col; i < tms; i++) {
m[row][i] = f1 * m[col][i] + m[row][i];
}
} catch (Exception e) {
System.out.println("Still Here!!!");
}
}
}
}
return m;
}
private float[][] UpperTriangle(float[][] m) {
if (INFO) {
System.out.println("Converting to Upper Triangle...");
}
float f1 = 0;
float temp = 0;
int tms = m.length; // get This Matrix Size (could be smaller than
// global)
int v = 1;
iDF = 1;
for (int col = 0; col < tms - 1; col++) {
for (int row = col + 1; row < tms; row++) {
v = 1;
outahere: while (m[col][col] == 0) // check if 0 in diagonal
{ // if so switch until not
if (col + v >= tms) // check if switched all rows
{
iDF = 0;
break outahere;
} else {
for (int c = 0; c < tms; c++) {
temp = m[col][c];
m[col][c] = m[col + v][c]; // switch rows
m[col + v][c] = temp;
}
v++; // count row switchs
iDF = iDF * -1; // each switch changes determinant
// factor
}
}
if (m[col][col] != 0) {
if (DEBUG) {
System.out.println("tms = " + tms + " col = " + col
+ " row = " + row);
}
try {
f1 = (-1) * m[row][col] / m[col][col];
for (int i = col; i < tms; i++) {
m[row][i] = f1 * m[col][i] + m[row][i];
}
} catch (Exception e) {
System.out.println("Still Here!!!");
}
}
}
}
return m;
}
// --------------------------------------------------------------
public float Determinant() {
float[][] matrix = this.matrix;
if (INFO) {
System.out.println("Getting Determinant...");
}
int tms = matrix.length;
float det = 1;
matrix = UpperTriangle(matrix);
for (int i = 0; i < tms; i++) {
det = det * matrix[i][i];
} // multiply down diagonal
det = det * iDF; // adjust w/ determinant factor
if (INFO) {
System.out.println("Determinant: " + det);
}
return det;
}
private float Determinant(float[][] matrix) {
if (INFO) {
System.out.println("Getting Determinant...");
}
int tms = matrix.length;
float det = 1;
matrix = UpperTriangle(matrix);
for (int i = 0; i < tms; i++) {
det = det * matrix[i][i];
} // multiply down diagonal
det = det * iDF; // adjust w/ determinant factor
if (INFO) {
System.out.println("Determinant: " + det);
}
return det;
}
public Matrix addMatrix(Matrix m) throws Exception{
if((m.matrix.length == this.matrix.length) && (m.matrix[0].length == this.matrix[0].length)){
Matrix retval = new Matrix(this.matrix.length);
for(int i = 0; i