import java.util.Arrays;

public class Matrix {
	private int m;
	private int n;
	
	private double[][] nums;
	
	// Constructor, copies passed matrix to this.nums
	public Matrix(double[][] nums) {
		m = nums.length;
		n = nums[0].length;
		
		this.nums = new double[m][n];
		for (int i = 0; i < m; i++)
			this.nums[i] = Arrays.copyOfRange(nums[i],0,n);
	}
	
	
	/**
	 * @param i
	 * @param j
	 * @return
	 * @throws Exception 
	 */
	public double get(int i, int j) throws Exception {
		if (i < n && i >= 0 && j < m && j >= 0)
			return nums[i][j];
		else
			throw new Exception();
	}
	
	
	/**
	 * @param i
	 * @param j
	 * @param k
	 * @throws Exception
	 */
	public void set(int i, int j, double k) throws Exception {
		if (i < n && i >= 0 && j < m && j >= 0)
			nums[i][j] = k;
		else
			throw new Exception();
	}
	
	
	/**
	 * @return
	 */
	public Matrix copy() {
		return new Matrix(nums);
	}
	
	
	/**
	 * @param x
	 * @return
	 * @throws Exception 
	 */
	public Matrix addRet(Matrix x) throws Exception {
		return new Matrix(add(x, this));
	}
	
	
	/**
	 * @param x
	 * @return
	 * @throws Exception 
	 */
	public void add(Matrix x) throws Exception {
		this.nums = add(x, this);
	}
	
	
	private static double[][] add(Matrix x, Matrix y) throws Exception {
		if (x.m != y.m || x.n != y.n)
			throw new Exception();
		
		double[][] temp = new double[x.m][x.n];
		
		for (int i = 0; i < x.m; i++) {
			for (int j = 0; j < x.n; j++) {
				temp[i][j] = x.get(i,j) + y.get(i,j);
			}
		}
		
		return temp;
	}
	
	
	/**
	 * @param x
	 * @return
	 * @throws Exception 
	 */
	public Matrix multLRet(Matrix x) throws Exception {
		return multRet(this, x);
	}
	
	
	/**
	 * @param x
	 * @throws Exception 
	 */
	public void multL(Matrix x) throws Exception {
		double[][] temp = mult(this, x);
		
		this.n = temp.length;
		this.m = temp[0].length;
		this.nums = temp;
	}
	
	
	/**
	 * @param x
	 * @return
	 * @throws Exception 
	 */
	public Matrix multRRet(Matrix x) throws Exception {
		return multRet(x, this);
	}
	
	
	/**
	 * @param x
	 * @throws Exception 
	 */
	public void multR(Matrix x) throws Exception {
		double[][] temp = mult(x, this);
		
		this.n = temp.length;
		this.m = temp[0].length;
		this.nums = temp;
	}
	
	
	/**
	 * @param x
	 * @param y
	 * @return
	 * @throws Exception 
	 */
	private static Matrix multRet(Matrix x, Matrix y) throws Exception {
		double[][] temp = mult(x,y);
		return new Matrix(temp);
	}
	
	
	/**
	 * @param x
	 * @param y
	 * @throws Exception 
	 */
	private static double[][] mult(Matrix x, Matrix y) throws Exception {
		if (x.n != y.m)
			throw new Exception();
		
		double[][] temp = new double[x.n][y.m];
		
		for (int i = 0; i < x.n; i++) {
			for (int j = 0; j < y.m; j++) {
				for (int k = 0; k < x.n; k++)
					temp[i][j] += x.get(i,k) * y.get(k,j);
			}
		}
		
		return temp;
	}
}

Create Matrix.java