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:
- Damenproblem: VL 5 Aufgabe 2
<br>Nicht die gewünschte rekursive Backtracking-Implementierung
- Erweiterung Matrix-Klasse: VL 8 Aufgabe 3
<br>Mathematisch falsche Lösung
- Keine
## Fragen

View File

@ -9,7 +9,7 @@ public class aufg3 {
// =====================
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;
boolean[] primes = new boolean[MAXIMUM];

View File

@ -16,7 +16,7 @@
## 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.
## Class String
## Class Array
### Usefull Methods
- 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.

View File

@ -9,5 +9,8 @@ public class Anwendung {
road_boat.move();
road_boat.bucketBilgeWater();
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");
}
public void bucketBilgeWater() {
System.out.println("We are dry again");
}

View File

@ -1,30 +1,30 @@
package part8.aufg3;
public class matrix {
private int rows;
private int cols;
private int numberOfRows;
private int numberOfCols;
int[][] field;
matrix(int rows, int cols){
this.rows = rows;
this.cols = cols;
matrix(int numberOfRows, int numberOfCols){
this.numberOfRows = numberOfRows;
this.numberOfCols = numberOfCols;
field = new int[rows][cols];
field = new int[numberOfRows][numberOfCols];
}
public int getCols() {
return cols;
public int getNumberOfCols() {
return numberOfCols;
}
public int getRows() {
return rows;
public int getNumberOfRows() {
return numberOfRows;
}
void print(){
for (int i=0; i<this.rows; i++){
for (int j=0; j<this.cols; j++){
for (int i = 0; i<this.numberOfRows; i++){
for (int j = 0; j<this.numberOfCols; j++){
System.out.print(field[i][j]);
if (j != this.cols-1) {
if (j != this.numberOfCols -1) {
System.out.print(" ; ");
}
}
@ -37,18 +37,32 @@ public class matrix {
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){
for (int i=0; i<this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
for (int i = 0; i<this.numberOfRows; i++) {
for (int j = 0; j < this.numberOfCols; j++) {
field[i][j] *= factor;
}
}
}
void addMatrix(matrix matrix2){
for (int i=0; i<this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
field[i][j] += matrix2.field[i][j];
for (int i = 0; i<this.numberOfRows; i++) {
for (int j = 0; j < this.numberOfCols; j++) {
this.field[i][j] += matrix2.field[i][j];
}
}
}
@ -56,10 +70,19 @@ public class matrix {
boolean multiplyMatrix(matrix matrix2){
boolean compatible = false;
if(this.getRows() == matrix2.getCols()){
if(this.getNumberOfRows() == matrix2.getNumberOfCols()){
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();
}
@ -67,24 +90,58 @@ public class matrix {
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){
matrix nr1 = new matrix(2,3);
matrix nr2 = new matrix(2,3);
// matrix nr1 = 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);
nr2.setValue(0,0,4);
matrix nr3 = new matrix(2,3);
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);
nr2.print();
matrix nr4 = new matrix(3,2);
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));
}
}