public final class PessimalDualMatrix extends Matrix { private double[] rowMajor ; private double[] columnMajor ; public PessimalDualMatrix(int rows, int cols) { super(rows, cols) ; rowMajor = new double[rows * cols] ; columnMajor = new double[rows * cols] ; } private int rowMajorIndex(int i, int j) { return (i * cols) + j ; } private int columnMajorIndex(int i, int j) { return i + (j * rows) ; } public void set(int i, int j, double d) { rowMajor[rowMajorIndex(i, j)] = d ; columnMajor[columnMajorIndex(i, j)] = d ; } public double getRowMajor(int i, int j) { return rowMajor[rowMajorIndex(i, j)] ; } public double getColumnMajor(int i, int j) { return columnMajor[columnMajorIndex(i, j)] ; } public double get(int i, int j) { return getRowMajor(i, j) ; } public void multiplyLeft(double[] v, double[] out) { assert v.length == rows ; assert cols == out.length ; for (int j = 0 ; j < cols ; ++j) { double a = 0.0 ; for (int i = 0 ; i < rows ; ++i) { a = a + (v[i] * getRowMajor(i, j)) ; } out[j] = a ; } } public void multiplyRight(double[] v, double[] out) { assert cols == v.length ; assert rows == out.length ; for (int i = 0 ; i < rows ; ++i) { double a = 0.0 ; for (int j = 0 ; j < cols ; ++j) { a = a + (getColumnMajor(i, j) * v[j]) ; } out[i] = a ; } } }