Skip to content

Updated Matrix.java #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Data Structures/Matrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void main(String[] args) {
System.out.println("2 * m2:\n" + m2.scale(2));
System.out.println("m2 + m3:\n" + m2.plus(m3));
System.out.println("m2 - m3:\n" + m2.minus(m3));
System.out.println("m2 * m3: \n"+m2.multiply(m3));
}


Expand Down Expand Up @@ -157,6 +158,32 @@ public Matrix minus(Matrix other) throws RuntimeException {

return new Matrix(newData);
}

/**
* Multiplies this matrix with another matrix.
*
* @param other : Matrix to be multiplied with
* @return product
*/
public Matrix multiply(Matrix other) throws RuntimeException {

int[][] newData = new int[this.data.length][other.getColumns()];

if(this.getColumns() !=other.getRows())
throw new RuntimeException("The two matrices cannot be multiplied.");
int sum;
for (int i = 0; i < this.getRows(); ++i)
for(int j = 0; j < other.getColumns(); ++j){
sum = 0;
for(int k=0;k<this.getColumns();++k){
sum += this.data[i][k] * other.getElement(k, j);
}
newData[i][j] = sum;
}


return new Matrix(newData);
}

/**
* Checks if the matrix passed is equal to this matrix
Expand Down