Skip to content

insert scalar divide function #330

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 2 commits into from
Mar 29, 2018
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
19 changes: 19 additions & 0 deletions Data Structures/Matrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void main(String[] args) {

//test operations (valid)
System.out.println("2 * m2:\n" + m2.scale(2));
System.out.println("m2 / 2:\n" + m2.divide(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 @@ -118,6 +119,24 @@ public Matrix scale(int scalar) {

return new Matrix(newData);
}

/**
* Returns this matrix divided by a factor. That is, computes sA where s is a
* constant and A is a matrix (this object).
*
* @param scalar : value to divide by
* @return A new matrix scaled by the scalar value
*/
public Matrix divide(int scalar) {

int[][] newData = new int[this.data.length][this.data[0].length];

for (int i = 0; i < this.getRows(); ++i)
for(int j = 0; j < this.getColumns(); ++j)
newData[i][j] = this.data[i][j] / scalar;

return new Matrix(newData);
}

/**
* Adds this matrix to another matrix.
Expand Down