package app.PREDICTION;

/**
 * Created by Petr Flégl on 23.1.2017.
 */

import java.util.Arrays;

public class Matrix {

	public enum matrixType{
		XY,
		YX
	}

	private int nrows;
	private int ncols;
	private double[][] data;

	public Matrix(double[][] dat, matrixType type) {
		if(type==matrixType.YX) {
			this.data = dat;
			this.nrows = dat.length;
			this.ncols = dat[0].length;
		}
		if(type==matrixType.XY){
			this.ncols = dat.length;
			this.nrows = dat[0].length;
			double[][] transpos = new double[nrows][ncols];
			for (int x=0;x<dat.length;x++){
				for(int y=0;y<dat[0].length;y++){
					transpos[y][x]=dat[x][y];
				}
			}
			this.data=transpos;
		}


	}

	public Matrix(int nrow, int ncol) {
		this.nrows = nrow;
		this.ncols = ncol;
		data = new double[nrow][ncol];
	}

	public int getNrows() {
		return nrows;
	}

	public void setNrows(int nrows) {
		this.nrows = nrows;
	}

	public int getNcols() {
		return ncols;
	}

	public void setNcols(int ncols) {
		this.ncols = ncols;
	}

	public double[][] getValues() {
		return data;
	}

	public void setValues(double[][] values) {
		this.data = values;
	}

	public void setValueAt(int row, int col, double value) {
		data[row][col] = value;
	}

	public double getValueAt(int row, int col) {
		return data[row][col];
	}

	public boolean isSquare() {
		return nrows == ncols;
	}

	public int size() {
		if (isSquare())
			return nrows;
		return -1;
	}

	public Matrix multiplyByConstant(double constant) {
		Matrix mat = new Matrix(nrows, ncols);
		for (int i = 0; i < nrows; i++) {
			for (int j = 0; j < ncols; j++) {
				mat.setValueAt(i, j, data[i][j] * constant);
			}
		}
		return mat;
	}
	public Matrix insertColumnWithValue1() {
		Matrix X_ = new Matrix(this.getNrows(), this.getNcols()+1);
		for (int i=0;i<X_.getNrows();i++) {
			for (int j=0;j<X_.getNcols();j++) {
				if (j==0)
					X_.setValueAt(i, j, 1.0);
				else 
					X_.setValueAt(i, j, this.getValueAt(i, j-1));
				
			}
		}
		return X_;
	}

	@Override
	public String toString() {
		return "Matrix{" +
				"data=" + Arrays.deepToString(data) +
				'}';
	}
}
