package math.matrix;

import exception.IllegalDimensionException;

/**
 * 2D diagonal matrix object d<sub>ij</sub>&isin;&#x211D;, i&ne;j=0
 * with 2 rows and 2 columns
 * @author Daniel Weschke
 */
public class Diagonal2D extends Matrix2D {

	/**
	 * UID
	 */
	private static final long serialVersionUID = 553077525134866149L;

	public Diagonal2D(){
		m = 2;
		n = 2;
		data = new double[2];
	}
	
	public Diagonal2D(double... i){
		m = 2;
		n = 2;
		data = i;
	}

	/**
	 * Generate an n-by-n identity matrix.
	 * An m-by-n matrix with ones on the diagonal and zeros elsewhere.
	 * @param n rows/columns
	 * @return n-by-n identity matrix
	 */
	public static Diagonal2D identity(int n){
		int i;
        Diagonal2D c = new Diagonal2D();
        for(i=0; i<n; i++)
        	c.set(i, 1);
        return c;
	}
	
	public void set(int i, double d){
		data[i] = d;
	}
    
    /**
     * Subtract two matrices of the same dimension (entrywise subtraction).
     * The subtraction of two m-by-n matrices <b>A</b> and <b>B</b>,
     * is again an m-by-n matrix computed by subtracting corresponding elements.
     * @param B matrix
     * @return <b>C</b> = <b>A</b> - <b>B</b>, entrywise subtracted matrix
     * @throws IllegalDimensionException
     */
    public Matrix minus(Matrix B) throws IllegalDimensionException{
    	checkDimensions(B);
    	int i,j;
        Matrix c = createAddition(B);
//		System.out.println(this instanceof Diagonal2D);
//		System.out.println(this);
//		System.out.println(B instanceof Diagonal2D);
//		System.out.println(B);
//		System.out.println(c instanceof Diagonal2D);
//		System.out.println(c);
        if(B instanceof Diagonal)
        	for(i=0; i<n; i++)
        		c.minus(i, i, ((Diagonal) B).get(i));
        else
	        for(i=0; i<m; i++)
	            for(j=0; j<n; j++)
	                c.minus(i,j, B.get(i,j));
        return c;
    }

	public String toString(){
		return Matrix2D.diag(data).toString();
	}

	public static void main(String[] args) {
	}

}
