TestMatrixMultiplikation.java
import java.io.*;
public class TestMatrixMultiplikation {
// Testprogramm für Strassen-Matrixmultiplikation mit Cutoff
public static void main(String[] args) throws IOException {
// erzeugt zwei Matrizen mit beliebigen Zahlen
// multipliziert sie und gibt das Ergebnis ggf. aus
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
// Werte vom Benutzer abfragen
String s; // String für eingegebene Zeile
System.out.println("Matrixdimension eingeben:");
s = in.readLine();
int dim = Integer.parseInt(s);
System.out.println("Soll der Strassen-Algorithmus verwendet werden (j/n):");
s = in.readLine();
boolean mitStrassen = false;
if (s.compareTo("j") == 0) {
mitStrassen = true;
}
int cutoff = 0;
if (mitStrassen) {
System.out.println("Größte Dimension für normale Multiplikation:");
s = in.readLine();
cutoff = Integer.parseInt(s);
}
System.out.println("Soll die Rechnung ausgegeben werden (j/n):");
s = in.readLine();
boolean mitAusgabe = false;
if (s.compareTo("j") == 0) {
mitAusgabe = true;
}
// erzeuge die Arrays
Matrix a = new Matrix(dim, dim);
Matrix b = new Matrix(dim, dim);
// fülle die Arrays mit (beliebigen) Zahlen
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
a.set(i, j, i+j);
b.set(i, j, i-j);
}
}
// multipliziere die Matrizen und miss die benötigte Zeit
Matrix c;
long timeBefore = 0L;
long timeAfter = 0L;
if (mitStrassen) {
timeBefore = System.currentTimeMillis();
c = Matrix.matmultStrassenMitCutoff(a, b, cutoff);
timeAfter = System.currentTimeMillis();
} else {
timeBefore = System.currentTimeMillis();
c = Matrix.matmult(a, b);
timeAfter = System.currentTimeMillis();
}
double timeSpent = (timeAfter - timeBefore)/1000.0; // in s
System.out.println("Zeit zum Multiplizieren: " + timeSpent + "s");
// gib die Rechnung aus, falls gewünscht
if (mitAusgabe) {
System.out.println("Matrix A:\n" + a);
System.out.println("Matrix B:\n" + b);
System.out.println("Matrix C = A*B:\n" + c);
}
}
}