/*******************************************************************************
 * Copyright (c) 2010 Attila Kovacs <attila_kovacs@post.harvard.edu>.
 * All rights reserved. 
 * 
 * This file is part of crush.
 * 
 *     crush is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crush is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with crush.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila_kovacs@post.harvard.edu> - initial API and implementation
 ******************************************************************************/
// Copyright (c) 2007 Attila Kovacs 

package util;

//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;
}
