File tree 1 file changed +19
-0
lines changed 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ public static void main(String[] args) {
35
35
36
36
//test operations (valid)
37
37
System .out .println ("2 * m2:\n " + m2 .scale (2 ));
38
+ System .out .println ("m2 / 2:\n " + m2 .divide (2 ));
38
39
System .out .println ("m2 + m3:\n " + m2 .plus (m3 ));
39
40
System .out .println ("m2 - m3:\n " + m2 .minus (m3 ));
40
41
System .out .println ("m2 * m3: \n " +m2 .multiply (m3 ));
@@ -118,6 +119,24 @@ public Matrix scale(int scalar) {
118
119
119
120
return new Matrix (newData );
120
121
}
122
+
123
+ /**
124
+ * Returns this matrix divided by a factor. That is, computes sA where s is a
125
+ * constant and A is a matrix (this object).
126
+ *
127
+ * @param scalar : value to divide by
128
+ * @return A new matrix scaled by the scalar value
129
+ */
130
+ public Matrix divide (int scalar ) {
131
+
132
+ int [][] newData = new int [this .data .length ][this .data [0 ].length ];
133
+
134
+ for (int i = 0 ; i < this .getRows (); ++i )
135
+ for (int j = 0 ; j < this .getColumns (); ++j )
136
+ newData [i ][j ] = this .data [i ][j ] / scalar ;
137
+
138
+ return new Matrix (newData );
139
+ }
121
140
122
141
/**
123
142
* Adds this matrix to another matrix.
You can’t perform that action at this time.
0 commit comments