Skip to content

Instantly share code, notes, and snippets.

@tandat2209
Created May 9, 2016 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tandat2209/8d247c33a72c59950bd1b80d969a7107 to your computer and use it in GitHub Desktop.
Save tandat2209/8d247c33a72c59950bd1b80d969a7107 to your computer and use it in GitHub Desktop.
Matrix
package matrixTCP;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Matrix {
private int rows;
private int cols;
private double[][] data;
public Matrix(File file) {
try {
readMatrixFromFile(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Matrix(Matrix matrix) {
rows = matrix.rows;
cols = matrix.cols;
data = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = matrix.data[i][j];
}
}
}
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[rows][cols];
}
private void readMatrixFromFile(File file) throws FileNotFoundException {
Scanner s = new Scanner(file);
while(s.hasNextLine()){
rows = rows + 1;
// TODO need fix here
cols = s.nextLine().split(" ").length;
}
s.close();
data = new double[rows][cols];
s = new Scanner(file);
for (int i = 0; i < rows; i++) {
String[] row = s.nextLine().split(" ");
for (int j = 0; j < cols; j++) {
data[i][j] = Double.parseDouble(row[j]);
}
}
s.close();
}
public double get(int row, int col) {
return data[row][col];
}
public Matrix add(Matrix m2) {
Matrix re = new Matrix(this);
if (rows == m2.rows && cols == m2.cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
re.data[i][j] += m2.data[i][j];
}
}
}else{
System.err.println("Error: unable to execute summation");
}
return re;
}
public Matrix minus(Matrix m2) {
Matrix re = new Matrix(this);
if (rows == m2.rows && cols == m2.cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
re.data[i][j] -= m2.data[i][j];
}
}
} else{
System.err.println("Error: unable to execute substraction");
}
return re;
}
public Matrix multiply(Matrix m2) {
if(cols == m2.rows){
Matrix re = new Matrix(rows, m2.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < m2.cols; j++) {
double x = 0;
for (int k = 0; k < cols; k++) {
x += data[i][k] * m2.data[k][j];
}
re.data[i][j] = x;
}
}
return re;
}
else{
System.err.println("Error: unable to execute multiplication");
return this;
}
}
public Matrix multiply(double x) {
Matrix re = new Matrix(this);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
re.data[i][j] *= x;
}
}
return re;
}
// ma tran con sau khi xoa hang row va cot col
public Matrix subMatrix(int row, int col) {
Matrix matrix = new Matrix(rows - 1, cols - 1);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix.data[i][j] = data[i][j];
}
for (int j = col + 1; j < cols; j++) {
matrix.data[i][j - 1] = data[i][j];
}
}
for (int i = row + 1; i < rows; i++) {
for (int j = 0; j < col; j++) {
matrix.data[i - 1][j] = data[i][j];
}
for (int j = col + 1; j < cols; j++) {
matrix.data[i - 1][j - 1] = data[i][j];
}
}
return matrix;
}
private double minor(int row, int col){
return Math.pow(-1, row + col) * subMatrix(row, col).determinant();
}
// tinh dinh thuc
public double determinant(){
if(isSquareMatrix()){
if(rows == 1) return data[0][0];
if(rows == 2) return data[0][0] * data[1][1] - data[0][1] * data[1][0];
float dt = 0;
for (int i = 0; i < rows; i++){
dt += data[0][i] * minor(0, i); // tinh de quy theo hang 0
}
return dt;
} else{
System.err.println("Error: Not a square matrix");
return 0;
}
}
// Ma tran chuyen vi
public Matrix transpose(){
Matrix matrix = new Matrix(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix.data[j][i] = data[i][j];
}
}
return matrix;
}
// Ma tran lien hop
public Matrix adjugate(){
Matrix matrix = new Matrix(rows, cols);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
matrix.data[i][j] = minor(i, j);
}
return matrix;
}
// ma tran nghich dao
public Matrix invertible(){
if (determinant() != 0) // dinh thuc = 0 thi khong co ma tran nghich dao
{
return transpose().adjugate().multiply(1.0/determinant());
} else {
return new Matrix(this);
}
}
public boolean isSquareMatrix(){
return rows == cols;
}
public void set(int row, int col, double x) {
data[row][col] = x;
}
public String toString(){
StringBuilder s = new StringBuilder();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
long d = Math.round(data[i][j]);
s.append(d+ " | ");
}
s.append("\n");
}
return s.toString();
}
public static void main(String[] args) {
Matrix sm = new Matrix(new File(Matrix.class.getResource("square-matrix.txt").getPath()));
System.out.println(sm.determinant());
System.out.println(sm.transpose());
System.out.println(sm.transpose().adjugate());
System.out.println(sm.invertible());
System.out.println(sm.invertible().multiply(sm));
}
}
1 3 99 2
0 1 -1 -1
0 33 1 3
0 0 2 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment