MCordingley\LinearAlgebra\Matrix::spliceColumns PHP Method

spliceColumns() public method

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

Usage Example

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