public function convolve(Matrix $b, int $stride = 1) : self
    {
        [$m, $n] = $b->shape();

        if ($m > $this->m or $n > $this->n) {
            throw new InvalidArgumentException('Matrix B cannot be'
                . ' larger than Matrix A.');
        }

        if ($stride < 1) {
            throw new InvalidArgumentException('Stride cannot be'
                . " less than 1, $stride given.");
        }

        $p = intdiv($m, 2);
        $q = intdiv($n, 2);

        $c = [];

        for ($i = 0; $i < $this->m; $i += $stride) {
            $temp = [];

            for ($j = 0; $j < $this->n; $j += $stride) {
                $sigma = 0;

                foreach ($b as $k => $rowB) {
                    foreach ($rowB as $l => $valueB) {
                        $x = $i + $p - (int) $k;
                        $y = $j + $q - (int) $l;

                        if ($x < 0 or $x >= $this->n or $y < 0 or $y >= $this->m) {
                            continue 1;
                        }

                        $sigma += $this->a[$x][$y] * $valueB;
                    }
                }

                $temp[] = $sigma;
            }

            $c[] = $temp;
        }

        return self::quick($c);
    }
public function convolve(Matrix $b, int $stride = 1) : self
    {
        [$m, $n] = $b->shape();

        if ($m > $this->m or $n > $this->n) {
            throw new InvalidArgumentException('Matrix B cannot be'
                . ' larger than Matrix A.');
        }

        if ($stride < 1) {
            throw new InvalidArgumentException('Stride cannot be'
                . " less than 1, $stride given.");
        }

        $p = intdiv($m, 2);
        $q = intdiv($n, 2);

        $c = [];

        for ($i = 0; $i < $this->m; $i += $stride) {
            $temp = [];

            for ($j = 0; $j < $this->n; $j += $stride) {
                $sigma = 0;

                foreach ($b as $k => $rowB) {
                    foreach ($rowB as $l => $valueB) {
                        $x = $i + $p - (int) $k;
                        $y = $j + $q - (int) $l;

                        if ($x < 0 or $x >= $this->n or $y < 0 or $y >= $this->m) {
                            continue 1;
                        }

                        $sigma += $this->a[$x][$y] * $valueB;
                    }
                }

                $temp[] = $sigma;
            }

            $c[] = $temp;
        }

        return self::quick($c);
    }
public function convolve ( Matrix $ b , int $ stride = 1 ) : self { [ $ m , $ n ] = $ b -> shape ( ) ; if ( $ m > $ this -> m or $ n > $ this -> n ) { throw new InvalidArgumentException ( 'Matrix B cannot be' . ' larger than Matrix A.' ) ; } if ( $ stride < 1 ) { throw new InvalidArgumentException ( 'Stride cannot be' . " less than 1, $stride given." ) ; } $ p = intdiv ( $ m , 2 ) ; $ q = intdiv ( $ n , 2 ) ; $ c = [ ] ; for ( $ i = 0 ; $ i < $ this -> m ; $ i += $ stride ) { $ temp = [ ] ; for ( $ j = 0 ; $ j < $ this -> n ; $ j += $ stride ) { $ sigma = 0 ; foreach ( $ b as $ k => $ rowB ) { foreach ( $ rowB as $ l => $ valueB ) { $ x = $ i + $ p - ( int ) $ k ; $ y = $ j + $ q - ( int ) $ l ; if ( $ x < 0 or $ x >= $ this -> n or $ y < 0 or $ y >= $ this -> m ) { continue 1 ; } $ sigma += $ this -> a [ $ x ] [ $ y ] * $ valueB ; } } $ temp [ ] = $ sigma ; } $ c [ ] = $ temp ; } return self :: quick ( $ c ) ; }
Convolve this matrix with another matrix.

@param \Rubix\Tensor\Matrix $b
@param int $stride
@throws \InvalidArgumentException
@return self
Convolve this matrix with another matrix .