Matritzenmultiplikation

MiniMaus2

Grünschnabel
Hallo!:)
vielleicht kann mir ja einer mal das erklären:

Aufgabenstellung:
Implementieren Sie in der Klasse Matrixmult die Multiplikation von
n Matrizen in einer verketteten Liste.


das ist schon angegeben:

Java:
public class Matrixmult
{

    /**
     * Implementieren Sie diese Methode. Diese soll die uebergebene Liste
     * von Matrizen von links nach rechts multiplizieren und das Ergebnis
     * zurueckgeben.
     */
    public int[][] matrixMult(Liste Matrizen) {
    
        
        return null;
    }
   
    /**
     * Gibt eine Matrix auf der Console aus
     */
    public void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length;i++) {
            String line = "[ ";
            for ( int j = 0; j < matrix[i].length;j++) {
                line = line + matrix[i][j] + " ";
            }
            line = line + "]";
            System.out.println(line);
        }
    }
 
Wie wäre es wenn du deinen Code zwischen diese
Code:
[*JAVA][*/JAVA]
BB-Codes packst? Würde das lesen sehr erleichtern.
 
Nun, die grundsätzliche Frage ist doch: wie machst du das von Hand? Erste mit der zweiten mutiplizieren, das Ergebnis mit der dritten usw.

Überleg dir einfach wie du das Schritt für Schritt machen würdest und packs in Code ;). Rekursiv ginge das ganze übrigens auch sehr elegant.

REINHAUN!

PS: Code Tags spendiert!
 
Hallo,

schau mal hier:
Java:
/**
 * 
 */
package de.tutorials;

/**
 * @author Thomas.Darimont
 * 
 */
public class MultipleMatrixMultiplicationExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Matrix a = new Matrix(2, 3, 1, 2, 3, 4, 5, 6);
        Matrix b = new Matrix(3, 2, 6, -1, 3, 2, 0, -3);
        System.out.println(a.muliply(b));
    }

    static class Matrix {
        double[] elements;
        int rows;
        int columns;

        /**
         * @param columns
         * @param rows
         */
        public Matrix(int rows, int columns) {
            this(rows, columns, null);
        }

        public Matrix(int rows, int columns, double... values) {
            if (rows < 0 || columns < 0) {
                throw new IllegalArgumentException(String.format(
                        "Negative Row / Column count: Rows: %s Columns: %s",
                        rows, columns));
            }
            this.rows = rows;
            this.columns = columns;
            if (values == null) {
                this.elements = new double[rows * columns];
            } else {
                if (rows * columns < values.length) {
                    throw new IllegalArgumentException(String.format(
                            "Wrong number of elements! Should be %s but is %s",
                            rows * columns, values.length));
                }
                this.elements = values.clone();
            }
        }

        public double get(int row, int column) {
            return elements[columns * row + column];
        }

        public void set(int row, int column, double value) {
            elements[columns * row + column] = value;
        }

        public Matrix muliply(Matrix matrix) {
            Matrix result = new Matrix(this.rows, matrix.columns, null);
            for (int i = 0; i < result.rows; i++) {
                for (int j = 0; j < result.columns; j++) {
                    double value = 0.0D;
                    for (int n = 0; n < columns; n++) {
                        value += get(i, n) * matrix.get(n, j);
                    }
                    result.set(i, j, value);
                }
            }
            return result;
        }

        @Override
        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    stringBuilder.append(get(i, j));
                    if (j < columns - 1) {
                        stringBuilder.append(", ");
                    }
                }
                stringBuilder.append("\n");
            }
            return stringBuilder.toString();
        }
    }

}

Gruß Tom
 
Zurück