Matrix Upgrades

This commit is contained in:
Matti 2024-04-17 15:44:13 +02:00
parent a35e70f869
commit 80a27ea82e
2 changed files with 47 additions and 31 deletions

View File

@ -11,7 +11,7 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
- Vorlesung 5 Aufgabe 1 & 3 von 3
- Vorlesung 6 komplett
- Vorlesung 7 komplett
- Vorlesung 8 Aufgabe 1, 2, 4 von 4
- Vorlesung 8 komplett
- Vorlesung 9 Aufgabe 2
## Angefangene unfertige Lösungen:

View File

@ -24,7 +24,7 @@ public class matrix {
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.numberOfCols -1) {
if (j != this.numberOfCols - 1) {
System.out.print(" ; ");
}
}
@ -41,6 +41,18 @@ public class matrix {
return this.field[row];
}
public void setRow(int[] values, int row) {
if (values.length == this.getNumberOfCols()) {
for (int i = 0; i < values.length; i++){
this.setValue(row,i,values[i]);
}
}
else {
//
// Ammount of given Values is not compatible Exception
}
}
public int[] getCol(int col){
int[] result = new int[this.getNumberOfRows()];
@ -51,6 +63,18 @@ public class matrix {
return result;
}
public void setCol(int[] values, int col){
if (values.length == this.getNumberOfRows()){
for (int i=0; i<values.length; i++){
this.setValue(i, col, values[i]);
}
}
else {
//
// Ammount of given Values is not compatible Exception
}
}
void scalarMultiply(int factor){
for (int i = 0; i<this.numberOfRows; i++) {
for (int j = 0; j < this.numberOfCols; j++) {
@ -94,7 +118,6 @@ public class matrix {
int result = 0;
if (vector1.length == vector2.length){
// do stuff
for (int i=0; i<vector1.length; i++){
result += vector1[i] * vector2[i];
}
@ -107,41 +130,34 @@ public class matrix {
}
public static void main(String[] args){
// 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("========================");
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("========================");
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.setRow(new int[]{1,2,3},0);
nr3.setRow(new int[]{4,5,6},1);
nr3.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.setCol(new int[]{7,9,11},0);
nr4.setCol(new int[]{8,10,12},1);
nr4.print();
System.out.println(nr3.multiplyMatrix(nr4));
System.out.println(nr4.multiplyMatrix(nr3));
}
}