File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ public static void main(String[] args) {
37
37
System .out .println ("2 * m2:\n " + m2 .scale (2 ));
38
38
System .out .println ("m2 + m3:\n " + m2 .plus (m3 ));
39
39
System .out .println ("m2 - m3:\n " + m2 .minus (m3 ));
40
+ System .out .println ("m2 * m3: \n " +m2 .multiply (m3 ));
40
41
}
41
42
42
43
@@ -157,6 +158,32 @@ public Matrix minus(Matrix other) throws RuntimeException {
157
158
158
159
return new Matrix (newData );
159
160
}
161
+
162
+ /**
163
+ * Multiplies this matrix with another matrix.
164
+ *
165
+ * @param other : Matrix to be multiplied with
166
+ * @return product
167
+ */
168
+ public Matrix multiply (Matrix other ) throws RuntimeException {
169
+
170
+ int [][] newData = new int [this .data .length ][other .getColumns ()];
171
+
172
+ if (this .getColumns () !=other .getRows ())
173
+ throw new RuntimeException ("The two matrices cannot be multiplied." );
174
+ int sum ;
175
+ for (int i = 0 ; i < this .getRows (); ++i )
176
+ for (int j = 0 ; j < other .getColumns (); ++j ){
177
+ sum = 0 ;
178
+ for (int k =0 ;k <this .getColumns ();++k ){
179
+ sum += this .data [i ][k ] * other .getElement (k , j );
180
+ }
181
+ newData [i ][j ] = sum ;
182
+ }
183
+
184
+
185
+ return new Matrix (newData );
186
+ }
160
187
161
188
/**
162
189
* Checks if the matrix passed is equal to this matrix
You can’t perform that action at this time.
0 commit comments