From 8f78e5955555883991143fdccdfd0a1f749d585d Mon Sep 17 00:00:00 2001 From: JeonSeongBae Date: Mon, 27 Nov 2017 13:43:47 +0900 Subject: [PATCH 1/2] insert scalar divide function --- Data Structures/Matrix/Matrix.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index edbdcdbf..275d3817 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -35,6 +35,7 @@ public class Matrix { //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)); @@ -118,6 +119,24 @@ public class Matrix { return new Matrix(newData); } + + /** + * Returns this matrix scaled by a factor. That is, computes sA where s is a + * constant and A is a matrix (this object). + * + * @param scalar : value to scale 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. From d82dec93f2880c6bd49761815539a98b79cade1a Mon Sep 17 00:00:00 2001 From: JeonSeongBae Date: Mon, 27 Nov 2017 13:46:35 +0900 Subject: [PATCH 2/2] comment --- Data Structures/Matrix/Matrix.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index 275d3817..67fd5147 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -121,10 +121,10 @@ public class Matrix { } /** - * Returns this matrix scaled by a factor. That is, computes sA where s is a + * 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 scale by + * @param scalar : value to divide by * @return A new matrix scaled by the scalar value */ public Matrix divide(int scalar) {