File tree Expand file tree Collapse file tree 4 files changed +111
-2
lines changed
Expand file tree Collapse file tree 4 files changed +111
-2
lines changed Original file line number Diff line number Diff line change 44
55namespace MCordingley \LinearAlgebra ;
66
7+ use ArrayAccess ;
78use MCordingley \LinearAlgebra \Decomposition \LUP ;
89
9- class Matrix
10+ class Matrix implements ArrayAccess
1011{
1112 /** @var array */
1213 protected $ internal ;
@@ -607,4 +608,42 @@ final public function transpose(): self
607608
608609 return new static ($ literal );
609610 }
611+
612+ /**
613+ * @param mixed $offset
614+ * @return bool
615+ */
616+ public function offsetExists ($ offset ): bool
617+ {
618+ return isset ($ this ->internal [$ offset ]);
619+ }
620+
621+ /**
622+ * @param mixed $offset
623+ * @return Vector|null
624+ * @throws MatrixException
625+ */
626+ public function offsetGet ($ offset )
627+ {
628+ return $ this ->offsetExists ($ offset ) ? new Vector ($ this ->internal [$ offset ]) : null ;
629+ }
630+
631+ /**
632+ * @param mixed $offset
633+ * @param mixed $value
634+ * @throws MatrixException
635+ */
636+ final public function offsetSet ($ offset , $ value )
637+ {
638+ throw new MatrixException ('Matrices are immutable. ' );
639+ }
640+
641+ /**
642+ * @param mixed $offset
643+ * @throws MatrixException
644+ */
645+ final public function offsetUnset ($ offset )
646+ {
647+ throw new MatrixException ('Matrices are immutable. ' );
648+ }
610649}
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ public function __construct(array $literal)
2121 */
2222 public static function fromMatrix (Matrix $ matrix , int $ row = 0 ): self
2323 {
24- return new self ( $ matrix-> toArray () [$ row ]) ;
24+ return $ matrix [$ row ];
2525 }
2626
2727 /**
@@ -223,4 +223,23 @@ public function maxNorm(): float
223223 return $ carry > $ value ? $ carry : $ value ;
224224 }, -INF );
225225 }
226+
227+ /**
228+ * @param mixed $offset
229+ * @return bool
230+ */
231+ public function offsetExists ($ offset ): bool
232+ {
233+ return isset ($ this ->internal [0 ][$ offset ]);
234+ }
235+
236+ /**
237+ * @param mixed $offset
238+ * @return float|null
239+ * @throws MatrixException
240+ */
241+ public function offsetGet ($ offset )
242+ {
243+ return $ this ->offsetExists ($ offset ) ? $ this ->internal [0 ][$ offset ] : null ;
244+ }
226245}
Original file line number Diff line number Diff line change 66
77use MCordingley \LinearAlgebra \Matrix ;
88use MCordingley \LinearAlgebra \MatrixException ;
9+ use MCordingley \LinearAlgebra \Vector ;
910use PHPUnit_Framework_TestCase ;
1011
1112final class MatrixTest extends PHPUnit_Framework_TestCase
@@ -713,4 +714,39 @@ public function testNonSquareDeterminant()
713714
714715 $ matrix ->determinant ();
715716 }
717+
718+ public function testOffsetExists ()
719+ {
720+ $ matrix = $ this ->buildMatrix ();
721+
722+ static ::assertTrue (isset ($ matrix [0 ]));
723+ static ::assertFalse (isset ($ matrix [100 ]));
724+ }
725+
726+ public function testOffsetGet ()
727+ {
728+ $ matrix = $ this ->buildMatrix ();
729+
730+ $ vector = $ matrix [1 ];
731+
732+ static ::assertInstanceOf (Vector::class, $ vector );
733+ }
734+
735+ public function testOffsetSet ()
736+ {
737+ $ matrix = $ this ->buildMatrix ();
738+
739+ static ::expectException (MatrixException::class);
740+
741+ $ matrix [0 ] = true ;
742+ }
743+
744+ public function testOffsetUnset ()
745+ {
746+ $ matrix = $ this ->buildMatrix ();
747+
748+ static ::expectException (MatrixException::class);
749+
750+ unset($ matrix [0 ]);
751+ }
716752}
Original file line number Diff line number Diff line change @@ -146,4 +146,19 @@ public function testProjection()
146146
147147 $ this ->assertEquals (new Vector ([12 / 10 , 4 / 10 ]), $ vector1 ->projection ($ vector2 ));
148148 }
149+
150+ public function testOffsetExists ()
151+ {
152+ $ vector = self ::buildVector ();
153+
154+ static ::assertTrue (isset ($ vector [0 ]));
155+ static ::assertFalse (isset ($ vector [100 ]));
156+ }
157+
158+ public function testOffsetGet ()
159+ {
160+ $ vector = self ::buildVector ();
161+
162+ static ::assertEquals (2 , $ vector [1 ]);
163+ }
149164}
You can’t perform that action at this time.
0 commit comments