Skip to content

Instantly share code, notes, and snippets.

@blkbsstt
Created November 11, 2011 20:27
Show Gist options
  • Save blkbsstt/1359133 to your computer and use it in GitHub Desktop.
Save blkbsstt/1359133 to your computer and use it in GitHub Desktop.
package edu.ua.math420.rbb.simplex.revised;
import java.util.Arrays;
public class Matrix {
private Double[][] matrix;
private int height;
private int width;
private boolean transposed;
public Matrix(int height, int width) {
this(height, width, 0d);
}
public Matrix(int height, int width, Double fill) {
this(new Double[height][width]);
for(Double[] d : matrix) {
Arrays.fill(d, fill);
}
}
public Matrix(Double[][] matrix) {
this.matrix = matrix;
this.height = matrix.length;
this.width = (height > 0) ? matrix[0].length : 0;
}
public Matrix(Double[] vector) {
this.matrix = new Double[vector.length][1];
this.height = vector.length;
this.width = 1;
for(int i = 0; i < vector.length; i++)
this.matrix[i][0] = vector[i];
}
public static Matrix identity(int size) {
Matrix result = new Matrix(size,size);
for(int i = 0; i < result.height(); i++)
for(int j = 0; j < result.width(); j++)
if(i==j) result.set(i,j,1d);
return result;
}
public int height() {
return (!transposed) ? height : width;
}
public int width() {
return (!transposed) ? width : height;
}
public void transpose() {
transposed = !transposed;
}
public boolean isTransposed() {
return transposed;
}
public Matrix transposed() {
Matrix result = new Matrix(matrix);
if(!isTransposed())
result.transpose();
return result;
}
public Double get(int i, int j) {
return (!transposed) ? matrix[i][j] : matrix[j][i];
}
public void set(int i, int j, Double val) {
if (!transposed)
matrix[i][j] = val;
else
matrix[j][i] = val;
}
public Matrix times(Matrix other) {
Matrix result = new Matrix(height(), other.width());
for(int i = 0; i < height(); i++)
for(int j = 0; j < width(); j++)
for(int k = 0; k < other.width(); k++)
result.set(i, k,
result.get(i,k) + get(i,j) * other.get(j,k));
return result;
}
public Matrix plus(Matrix other) {
Matrix result = new Matrix(height(), width());
for(int i = 0; i < height(); i++)
for(int j = 0; j < width(); j++)
result.set(i, j, get(i,j) + other.get(i,j));
return result;
}
public Matrix minus(Matrix other) {
Matrix result = new Matrix(height(), width());
for(int i = 0; i < height(); i++)
for(int j = 0; j < width(); j++)
result.set(i, j, get(i,j) - other.get(i,j));
return result;
}
public Matrix rowMultiplication(int row, Double scalar) {
Matrix elem = identity(height());
elem.set(row, row, scalar);
return elem.times(this);
}
public Matrix rowAddition(int fromRow, int toRow, Double scalar) {
Matrix elem = identity(height());
elem.set(toRow, fromRow, scalar);
return elem.times(this);
}
public Matrix rowSwitching(int row1, int row2) {
Matrix elem = identity(height());
elem.set(row1, row2, 1d);
elem.set(row2, row1, 1d);
elem.set(row1, row1, 0d);
elem.set(row2, row2, 0d);
return elem.times(this);
}
public Matrix extend(int rows, int cols) {
return extend(rows, cols, 0d);
}
public Matrix extend(int rows, int cols, Double fill) {
Matrix result = new Matrix(height()+rows, width()+cols);
for(int i = 0; i < result.height(); i++)
for(int j = 0; j < result.width(); j++)
result.set(i, j,
(i < height() && j < width()) ? get(i,j) : fill);
return result;
}
public Matrix mergeRight(Matrix other) {
Matrix result = new Matrix(height(), width() + other.width());
for(int i = 0; i < height(); i++) {
for(int j = 0; j < width(); j++)
result.set(i,j,get(i,j));
for(int j = width(), k = 0; j < result.width(); j++, k++ )
result.set(i, j, other.get(i,k));
}
return result;
}
public Matrix mergeDown(Matrix other) {
Matrix result = new Matrix(height() + other.height(), width());
for(int j = 0; j < width(); j++) {
for(int i = 0; i < height(); i++)
result.set(i,j,get(i,j));
for(int i = height(), k = 0; i < result.height(); i++, k++)
result.set(i,j,other.get(k,j));
}
return result;
}
public Matrix col(int j) {
return keepCols(new int[] {j});
}
public Matrix firstNCols(int n) {
int[] cols = new int[n];
for(int i = 0; i < cols.length; i++)
cols[i] = i;
return keepCols(cols);
}
public Matrix keepCols(int[] cols) {
Matrix result = new Matrix(height(), cols.length);
for(int j = 0; j < cols.length; j++)
for(int i = 0; i < height(); i++)
result.set(i, j, get(i,cols[j]));
return result;
}
public Matrix row(int i) {
return keepRows(new int[] {i});
}
public Matrix firstNRows(int n) {
int[] rows = new int[n];
for(int i = 0; i < rows.length; i++)
rows[i] = i;
return keepRows(rows);
}
public Matrix keepRows(int[] rows) {
Matrix result = new Matrix(rows.length, width());
for(int i = 0; i < rows.length; i++)
for(int j = 0; j < width(); j++)
result.set(i, j, get(rows[i],j));
return result;
}
public Matrix subMatrix(int rows, int cols) {
return subMatrix(rows,cols,0,0);
}
public Matrix subMatrix(int rows, int cols, int i, int j) {
int[] rowsToKeep = new int[rows];
int[] colsToKeep = new int[cols];//TODO
for(int k = i, l = 0; k < i+rows; k++, l++)
rowsToKeep[l] = k;
for(int k = j, l = 0; k < j+cols; k++, l++)
colsToKeep[l] = k;
return keepRows(rowsToKeep).keepCols(colsToKeep);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < height(); i++) {
sb.append("[");
for(int j = 0; j < width(); j++) {
sb.append(get(i,j));
if(j != width()-1) sb.append(", ");
}
sb.append("]\n");
}
sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment