- package lobos.never.die;
-
- import java.util.HashMap;
-
- public class Matrix {
- HashMap <Integer, Double> matrix;
- private int _rows, _cols, _default = 0;
- private double _det;
-
-
- public static void main(String[] args) {
- Matrix
- a = new Matrix(4, 5,
- 1, 1, 1, 1, 0,
- 6, 3, -3, -6, 3,
- 12, 3, 3, 12, 0,
- 8, 1, -1, -8, 0
- );
-
-
-
-
-
-
- System.out.println(a.rref().toString(10));
- }
-
- public Matrix(int rows, int cols, double ... vals) {
- int i = 0;
- _rows = rows;
- _cols = cols;
-
- matrix = new HashMap <Integer, Double> (rows*cols/2);
-
- for (double d : vals)
- matrix.put(i++, d);
- }
-
-
- public double get(int row, int col) {
- if (!(_rows > row && _cols > col && row > -1 && col > -1))
- throw new ArrayIndexOutOfBoundsException();
-
- if (!matrix.containsKey(_cols * row + col))
- return _default;
-
- return matrix.get(_cols * row + col);
- }
-
-
- public void set(int row, int col, double val) {
- if (!(_rows > row && _cols > col && row > -1 && col > -1))
- throw new ArrayIndexOutOfBoundsException();
-
- if (val != _default)
- matrix.put(_cols * row + col, val);
-
- if (val == _default)
- matrix.remove(_cols * row + col);
- }
-
- public void equals(Matrix M) {
- _rows = M._rows;
- _cols = M._cols;
- _default = M._default;
-
- for (int i : M.matrix.keySet())
- matrix.put(i, M.matrix.get(i));
- }
-
-
- public Matrix add(Matrix r) {
- if (!(_rows == r._rows && _cols == r._cols))
- return new Matrix(0,0);
-
- Matrix M = new Matrix(_rows, _cols);
-
- for (int i = 0; i < _rows; i++)
- for (int j = 0; j < _cols; j++)
- M.set(i, j, get(i, j) + r.get(i,j));
-
- return M;
- }
-
-
- public Matrix multiply(Matrix r) {
- double s = 0;
- Matrix M;
-
- if (_cols != r._rows)
- return new Matrix(0,0);
-
- M = new Matrix(_rows, r._cols);
-
- for (int i = 0; i < _rows; i++)
- for (int j = 0; j < r._cols; j++) {
- s = 0;
- for (int k = 0; k < _cols; k++)
- s += get(i, k)*r.get(k, j);
- M.set(i, j, s);
- }
- return M;
- }
-
-
- public Matrix rectangle(int x, int y, int x_len, int y_len) {
- Matrix M = new Matrix(x_len, y_len);
-
- for (int i = x; i < x+x_len; i++)
- for (int j = y; j < y+y_len; j++)
- try {
- M.set(i-x, j-y, get(i, j));
- }
- catch(java.lang.ArrayIndexOutOfBoundsException e) {
- M.set(i-x, j-y, _default);
- }
-
- return M;
- }
-
-
- public void swap_row(int r, int s) {
- double swap;
- for (int i=0; i < _cols; i++) {
- swap = get(r, i);
- set(r, i, get(s, i));
- set(s, i, swap);
- }
- }
-
-
- public Matrix ref() {
- double d, det = 1;
- Matrix M = rectangle(0, 0, _rows, _cols);
-
-
- for (int col=0; col < _rows; col++) {
- d = M.get(col, col);
-
-
- if (d != 0) {
- M.scale_row(1/d, col);
- det *= d;
- }
- else
- for (int i=col+1; i < _cols; i++) {
- d = M.get(i, col);
-
-
- if (d != 0) {
- M.scale_row(1/d, i);
- M.swap_row(i, col);
- det *= d;
- break;
- }
- }
-
-
-
- for (int i=col+1; i < _rows; i++) {
- d = M.get(i, col);
- if (d != 0) {
- M.scale_row(-1/d, i);
- M.add_rows(col, i);
- det *= -d;
- }
- }
- }
-
-
- for (int i=0; i < _rows; i++)
- det *= M.get(i, i);
-
- _det = det;
-
- return M;
- }
-
-
- public Matrix rref() {
-
- double d;
- Matrix M = ref();
-
- for (int i=_rows-1; i > -1; i--) {
- for (int k=0; k < i; k++) {
- d = M.get(k, i);
- if (d != 0)
- M.add_scale(i, k, -d);
- }
- }
- return M;
- }
-
-
- public Matrix inverse() {
- Matrix M = rectangle(0, 0, _rows, 2*_cols);
-
- for (int i=0; i < _rows; i++)
- M.set(i, i+_cols, 1);
-
- M = M.rref();
-
- return M.rectangle(0, _cols, _rows, _cols);
- }
-
-
- public void scale_row(double d, int row) {
- for (int i=0; i < _cols; i++)
- set(row, i, get(row, i)*d);
- }
-
-
- public void add_scale(int r, int s, double d) {
- for (int i=0; i < _cols; i++)
- set(s, i, d * get(r, i) + get(s, i));
- }
-
-
- public void add_rows(int r, int s) {
- add_scale(r, s, 1);
- }
-
-
- public double det() {
- ref();
- return _det;
- }
-
-
- public double inf_norm() {
- double m = 0, d = 0;
-
- for (int i=0; i < _rows; i++) {
- m = d > m ? d : m;
- d = 0;
- for (int j=0; j < _cols; j++)
- d = d > get(i, j) ? d : get(i, j);
- }
-
- return m;
- }
-
-
- public String toString(int padding) {
- int x, y;
- String d, s = "";
-
- for (int i = 0; i < _rows * _cols; i++) {
- y = i/_cols;
- x = i%_cols;
-
- d = Double.toString(get(y, x));
-
-
- for (int k=0; k < padding - d.length(); k++)
- s += " ";
-
- s += d;
-
- if (x == _cols - 1)
- s += "\n";
- }
-
- return s;
- }
- }