Matrix Multiply Math works

This commit is contained in:
Matti 2024-04-17 12:15:24 +02:00
parent 56cad4fb91
commit a35e70f869
6 changed files with 97 additions and 39 deletions

View File

@ -17,8 +17,7 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
## Angefangene unfertige Lösungen: ## Angefangene unfertige Lösungen:
- Damenproblem: VL 5 Aufgabe 2 - Damenproblem: VL 5 Aufgabe 2
<br>Nicht die gewünschte rekursive Backtracking-Implementierung <br>Nicht die gewünschte rekursive Backtracking-Implementierung
- Erweiterung Matrix-Klasse: VL 8 Aufgabe 3
<br>Mathematisch falsche Lösung
- Keine - Keine
## Fragen ## Fragen

View File

@ -9,7 +9,7 @@ public class aufg3 {
// ===================== // =====================
public static void main(String[] args){ public static void main(String[] args){
int MAXIMUM = 100_000_000; // takes about 13 seconds(!!) int MAXIMUM = 10_000_000; // takes about 13 seconds(!!)
int MAX_PER_ROW = 20; int MAX_PER_ROW = 20;
boolean[] primes = new boolean[MAXIMUM]; boolean[] primes = new boolean[MAXIMUM];

View File

@ -16,7 +16,7 @@
## Class Stringbuffer ## Class Stringbuffer
Ein String Buffer ist wie ein gewöhnlicher String eine Anreihung von Characters, jedoch können Länge und Inhalt eines String Buffers mithilfe von verschiedenen Methoden modifiziert werden. Ein String Buffer ist wie ein gewöhnlicher String eine Anreihung von Characters, jedoch können Länge und Inhalt eines String Buffers mithilfe von verschiedenen Methoden modifiziert werden.
## Class String ## Class Array
### Usefull Methods ### Usefull Methods
- equals() <br>Returns a Boolean value representing if two given Arrays are equal to one another. - equals() <br>Returns a Boolean value representing if two given Arrays are equal to one another.
- fill() <br>Assigns a value to each element of the array. - fill() <br>Assigns a value to each element of the array.

View File

@ -9,5 +9,8 @@ public class Anwendung {
road_boat.move(); road_boat.move();
road_boat.bucketBilgeWater(); road_boat.bucketBilgeWater();
road_boat.makeDust(); road_boat.makeDust();
System.out.println(road_boat.seats);
System.out.println(road_boat.driverPosition);
} }
} }

View File

@ -10,7 +10,6 @@ public class Amphibienfahrzeug implements Landfahrzeuge, Wasserfahrzeuge{
System.out.println("I am zooming around"); System.out.println("I am zooming around");
} }
public void bucketBilgeWater() { public void bucketBilgeWater() {
System.out.println("We are dry again"); System.out.println("We are dry again");
} }

View File

@ -1,30 +1,30 @@
package part8.aufg3; package part8.aufg3;
public class matrix { public class matrix {
private int rows; private int numberOfRows;
private int cols; private int numberOfCols;
int[][] field; int[][] field;
matrix(int rows, int cols){ matrix(int numberOfRows, int numberOfCols){
this.rows = rows; this.numberOfRows = numberOfRows;
this.cols = cols; this.numberOfCols = numberOfCols;
field = new int[rows][cols]; field = new int[numberOfRows][numberOfCols];
} }
public int getCols() { public int getNumberOfCols() {
return cols; return numberOfCols;
} }
public int getRows() { public int getNumberOfRows() {
return rows; return numberOfRows;
} }
void print(){ void print(){
for (int i=0; i<this.rows; i++){ for (int i = 0; i<this.numberOfRows; i++){
for (int j=0; j<this.cols; j++){ for (int j = 0; j<this.numberOfCols; j++){
System.out.print(field[i][j]); System.out.print(field[i][j]);
if (j != this.cols-1) { if (j != this.numberOfCols -1) {
System.out.print(" ; "); System.out.print(" ; ");
} }
} }
@ -37,18 +37,32 @@ public class matrix {
int getValue(int row, int col){return field[row][col];} int getValue(int row, int col){return field[row][col];}
public int[] getRow(int row){
return this.field[row];
}
public int[] getCol(int col){
int[] result = new int[this.getNumberOfRows()];
for (int i=0; i<this.getNumberOfRows(); i++){
result[i] = this.getValue(i, col);
}
return result;
}
void scalarMultiply(int factor){ void scalarMultiply(int factor){
for (int i=0; i<this.rows; i++) { for (int i = 0; i<this.numberOfRows; i++) {
for (int j = 0; j < this.cols; j++) { for (int j = 0; j < this.numberOfCols; j++) {
field[i][j] *= factor; field[i][j] *= factor;
} }
} }
} }
void addMatrix(matrix matrix2){ void addMatrix(matrix matrix2){
for (int i=0; i<this.rows; i++) { for (int i = 0; i<this.numberOfRows; i++) {
for (int j = 0; j < this.cols; j++) { for (int j = 0; j < this.numberOfCols; j++) {
field[i][j] += matrix2.field[i][j]; this.field[i][j] += matrix2.field[i][j];
} }
} }
} }
@ -56,10 +70,19 @@ public class matrix {
boolean multiplyMatrix(matrix matrix2){ boolean multiplyMatrix(matrix matrix2){
boolean compatible = false; boolean compatible = false;
if(this.getRows() == matrix2.getCols()){ if(this.getNumberOfRows() == matrix2.getNumberOfCols()){
compatible = true; compatible = true;
matrix result = new matrix(this.getRows(), matrix2.getCols()); matrix result = new matrix(this.getNumberOfRows(), matrix2.getNumberOfCols());
for (int i=0; i<this.field.length; i++){
int[] currentRow = this.getRow(i);
for (int j=0; j<matrix2.field[0].length; j++){
int[] currentCol = matrix2.getCol(j);
int dotProduct = dotProduct(currentRow, currentCol);
result.setValue(i,j,dotProduct);
}
}
result.print(); result.print();
} }
@ -67,24 +90,58 @@ public class matrix {
return compatible; return compatible;
} }
private int dotProduct(int[] vector1, int[] vector2){
int result = 0;
if (vector1.length == vector2.length){
// do stuff
for (int i=0; i<vector1.length; i++){
result += vector1[i] * vector2[i];
}
}
else {
// Vectors not compatible Exception
// TODO
}
return result;
}
public static void main(String[] args){ public static void main(String[] args){
matrix nr1 = new matrix(2,3); // matrix nr1 = new matrix(2,3);
matrix nr2 = new matrix(2,3); // matrix nr2 = new matrix(2,3);
//
// nr1.setValue(1,1,1);
// nr2.setValue(0,0,4);
//
// nr2.addMatrix(nr1);
// nr2.print();
//
// nr2.scalarMultiply(8);
// nr2.print();
//
// nr2.scalarMultiply(7);
// nr2.print();
//
// System.out.println("========================");
nr1.setValue(1,1,1); matrix nr3 = new matrix(2,3);
nr2.setValue(0,0,4); nr3.setValue(0,0,1);
nr3.setValue(0,1,2);
nr3.setValue(0,2,3);
nr3.setValue(1,0,4);
nr3.setValue(1,1,5);
nr3.setValue(1,2,6);
nr3.print();
nr2.addMatrix(nr1); matrix nr4 = new matrix(3,2);
nr2.print(); nr4.setValue(0,0,7);
nr4.setValue(0,1,8);
nr4.setValue(1,0,9);
nr4.setValue(1,1,10);
nr4.setValue(2,0,11);
nr4.setValue(2,1,12);
nr4.print();
nr2.scalarMultiply(8);
nr2.print();
nr2.scalarMultiply(7);
nr2.print();
matrix nr3 = new matrix(7,4);
matrix nr4 = new matrix(6,7);
System.out.println(nr3.multiplyMatrix(nr4)); System.out.println(nr3.multiplyMatrix(nr4));
} }
} }