MCordingley\LinearAlgebra\Matrix::map PHP Метод

map() публичный Метод

Iterates over the current matrix with a callback function to return a new matrix with the mapped values. $callback takes four arguments: - The current matrix element - The current row - The current column - The matrix being iterated over
public map ( callable $callback ) : self
$callback callable
Результат self
    public function map(callable $callback) : self
    {
        $literal = [];
        for ($i = 0, $rows = $this->getRowCount(); $i < $rows; $i++) {
            $row = [];
            for ($j = 0, $columns = $this->getColumnCount(); $j < $columns; $j++) {
                $row[] = $callback($this->get($i, $j), $i, $j, $this);
            }
            $literal[] = $row;
        }
        return new static($literal);
    }

Usage Example

Пример #1
0
 public function testMap()
 {
     $matrix = new Matrix([[1, 2], [3, 4]]);
     $mapped = $matrix->map(function ($value, $row, $column) {
         return $value + $row + $column;
     });
     static::assertEquals(1, $mapped->get(0, 0));
     static::assertEquals(3, $mapped->get(0, 1));
     static::assertEquals(4, $mapped->get(1, 0));
     static::assertEquals(6, $mapped->get(1, 1));
 }