Matrix
Matrix is also called 2-dimensional array.
class NaiveMatrix {
private Integer[][] values;
public NaiveMatrix(int n, int m) {
this.values = new Integer[n][m];
for (int row = 0; row < values.length; row++) {
for (int column = 0; column < values[row].length; column++) {
values[row][column] = 0;
}
}
}
public void set(int row, int column, Integer value) {
values[row][column] = value;
}
public Integer get(int i, int j) {
return values[i][j];
}
public String toString() {
StringBuilder result = new StringBuilder();
for (Integer[] row : values) {
for (Integer value : row) {
result.append(value);
result.append(" ");
}
result.append("\n");
}
return result.toString();
}
}Lets try to use this matrix implementation.
The code above prints out the following.
Matrix operations
There are many operations that are described in linear algebra. We are going to implement couple of these. We are going to enhance NaiveMatrix class with more operations.
Addition
Adds to matrices together, summing each element on that position.
Then we can create two matrices and sum them together.
Here is how the output would look like.
Last updated
Was this helpful?