// Übung am 14.6.2004 bei Ralph Matthes, Informatik II, LMU München

// hier wurde nicht mit javadoc kommentiert!

public class Matrix{

    private int dimension;
    private double[][] a;  // Deklaration ohne Abmessungen

    public Matrix(double[][] b){

	dimension = b.length;
	a = new double[dimension][dimension];  // Erzeugung mit Abmessungen


	// wir nehmen hier an, daß b bereits quadratischer Array ist
	for (int i = 0; i<dimension; i++){
	    for (int j=0; j<dimension; j++) a[i][j] = b[i][j];
	}
    }

    public double eintrag(int i, int j){
	// kann schief gehen bei unpassenden i, j
	return a[i][j];
    }

    /* Matrizenmultipliaktion
     * @param die zweite Matrix
     * @return das Produkt
     */
    public Matrix mal (Matrix b){
	// wir nehmen an, daß b dieselben Abmessungen hat wie this
	double summe;
	double[][] c = new double[dimension][dimension];
	for (int i=0; i<dimension; i++){
	    for (int j=0; j<dimension; j++){
		summe = 0;
		for (int k=0; k<dimension; k++) summe += a[i][k]*b.eintrag(k,j);
		c[i][j] = summe;
	    }
	}
	return new Matrix(c); // man muß erst ein Matrix-Objekt erzeugen
    }


    /* Nun natürlich eine statische Methode: Man will ja keine Nullmatrix
       zu einer gegebenen Matrix bestimmen.*/
    public static Matrix nullmatrix(int dimension){

	double[][] doppelarray = new double[dimension][dimension];
	for (int i=0; i<dimension; i++){
	    for (int j=0; j<dimension; j++) doppelarray[i][j]=0.0;
	}
	     return new Matrix(doppelarray);

    }

    /* System.out.println(Matrix a) würde nur ausgeben, daß a ein
       Matrix-Objekt ist, dazu eine kaum nützliche Speicheradresse.
       Daher überschreibe man toString() von Object.*/
    public String toString(){
	/* folgende Zeile führt nur zur Ausgabe, daß es sich um einen
	   doppelt indizierten Array von double-Werten handelt, + Adresse */
	//return a.toString();
	
	// eine etwas rohe, aber doch nützliche Ausgabe:
	String ausgabe ="";
	for (int i=0; i<dimension; i++){
	    for (int j=0; j<dimension; j++){
		ausgabe += a[i][j] + ",";
	    }
	    ausgabe += "\n";
	}
	return ausgabe;
	
	
    }
    
    
}
