MCordingley\LinearAlgebra\Matrix::spliceRows PHP Method

spliceRows() public method

public spliceRows ( integer $offset, integer $length = null, array $replacement = null ) : self
$offset integer
$length integer
$replacement array
return self
    public function spliceRows(int $offset, int $length = null, array $replacement = null) : self
    {
        if ($replacement) {
            if ($this->getColumnCount() !== count($replacement[0])) {
                throw new MatrixException('Cannot splice [' . count($replacement) . '] columns into matrix of [' . $this->getRowCount() . '] column.');
            }
            if (!static::subArraysAreEqualSize($replacement)) {
                throw new MatrixException('Cannot splice in new rows of unequal size.');
            }
        }
        $spliced = $this->toArray();
        array_splice($spliced, $offset, $length, $replacement);
        return new static($spliced);
    }

Usage Example

Example #1
0
 public function testSpliceUnevenRows()
 {
     $matrix = new Matrix([[1, -1, 2], [4, 0, 6], [0, 1, -1]]);
     static::expectException(MatrixException::class);
     $matrix->spliceRows(1, 1, [[8, 5, 1], [2]]);
 }