File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -130,6 +130,50 @@ public function adjoint()
130130 return $ this ->inverse ()->multiply ($ this ->determinant ());
131131 }
132132
133+ /**
134+ * @param Matrix $other
135+ * @return Matrix
136+ * @throws MatrixException
137+ */
138+ public function contatenateBottom (Matrix $ other )
139+ {
140+ if ($ this ->columnCount !== $ other ->columnCount ) {
141+ throw new MatrixException (
142+ 'Cannot concatenate matrices of incompatible size: '
143+ . print_r ($ this ->internal , true )
144+ . ' and '
145+ . print_r ($ other ->internal , true )
146+ );
147+ }
148+
149+ return new Matrix (array_merge ($ this ->internal , $ other ->internal ));
150+ }
151+
152+ /**
153+ * @param Matrix $other
154+ * @return Matrix
155+ * @throws MatrixException
156+ */
157+ public function contatenateRight (Matrix $ other )
158+ {
159+ if ($ this ->rowCount !== $ other ->rowCount ) {
160+ throw new MatrixException (
161+ 'Cannot concatenate matrices of incompatible size: '
162+ . print_r ($ this ->internal , true )
163+ . ' and '
164+ . print_r ($ other ->internal , true )
165+ );
166+ }
167+
168+ $ concatenated = [];
169+
170+ for ($ i = 0 ; $ i < $ this ->rowCount ; $ i ++) {
171+ $ concatenated [] = array_merge ($ this ->internal [$ i ], $ other ->internal [$ i ]);
172+ }
173+
174+ return new Matrix ($ concatenated );
175+ }
176+
133177 /**
134178 * @return float The matrix's determinant
135179 * @throws MatrixException
Original file line number Diff line number Diff line change @@ -332,6 +332,31 @@ public function testAdjoint()
332332 $ this ->assertEquals (4 , $ adjoint ->get (2 , 2 ));
333333 }
334334
335+ public function testConcatenateBottom ()
336+ {
337+ $ matrixA = new Matrix ([[1 , 2 , 3 ]]);
338+ $ matrixB = new Matrix ([[4 , 5 , 6 ]]);
339+ $ concatenated = $ matrixA ->contatenateBottom ($ matrixB );
340+
341+ $ this ->assertEquals ([
342+ [1 , 2 , 3 ],
343+ [4 , 5 , 6 ],
344+ ], $ concatenated ->toArray ());
345+ }
346+
347+ public function testConcatenateRight ()
348+ {
349+ $ matrixA = new Matrix ([[1 ], [2 ], [3 ]]);
350+ $ matrixB = new Matrix ([[4 ], [5 ], [6 ]]);
351+ $ concatenated = $ matrixA ->contatenateRight ($ matrixB );
352+
353+ $ this ->assertEquals ([
354+ [1 , 4 ],
355+ [2 , 5 ],
356+ [3 , 6 ],
357+ ], $ concatenated ->toArray ());
358+ }
359+
335360 public function testDeterminant ()
336361 {
337362 $ matrix = new Matrix ([
You can’t perform that action at this time.
0 commit comments