Skip to content

Commit 884b21c

Browse files
committed
Implement ArrayAccess interface.
1 parent 8659476 commit 884b21c

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

src/Matrix.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace MCordingley\LinearAlgebra;
66

7+
use ArrayAccess;
78
use 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
}

src/Vector.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

tests/MatrixTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use MCordingley\LinearAlgebra\Matrix;
88
use MCordingley\LinearAlgebra\MatrixException;
9+
use MCordingley\LinearAlgebra\Vector;
910
use PHPUnit_Framework_TestCase;
1011

1112
final 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
}

tests/VectorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)