// (C)2007 Attila Kovacs <attila@submm.caltech.edu>

//package crush.util;

public class Matrix {
    public double[][] value; 

    public Matrix() {}

    public Matrix(double[] v, int dir) {
	int n = v.length;

	switch(dir) {
	case COLUMN_VECTOR:
	    value = new double[n][1];
	    for(int i=0; i<n; i++) value[i][0] = v[i];
	    break;
	case ROW_VECTOR:
	    value = new double[1][n];
	    for(int i=0; i<n; i++) value[0][i] = v[i];
	    break;
	}
    }

    public Matrix(double[][] a) { value = a; }

    public Matrix(int n, int m) { value = new double[n][m]; }
    
    public Matrix dot(Matrix B) throws IllegalArgumentException {	
	if(value[0].length != B.value.length) 
	    throw new IllegalArgumentException("Incompatible Matrix Dimensions: " + getSizeString() + "," + B.getSizeString());

	int n = value.length;
	int m = B.value[0].length;
	int l = value[0].length;

	Matrix M = new Matrix(n, m);

	for(int i=0; i<n; i++) for(int j=0; j<m; j++)
	    for(int k=0; k<l; k++) M.value[i][j] += value[i][k] * B.value[k][j];
	
	return M;
    }

    public Matrix transpose() {
	int n = value.length;
	int m = value[0].length;

	Matrix M = new Matrix(m, n);

	for(int i=0; i<n; i++) for(int j=0; j<m; j++) M.value[j][i] = value[i][j];
	
	return M;
    }

    final String getSizeString() {
	return "[" + value.length + "x" + value[0].length + "]";
    }

    public final static int ROW_VECTOR = 0;
    public final static int COLUMN_VECTOR = 1;
}
