Phpml\Math\Set::cartesian PHP Method

cartesian() public static method

Creates the Cartesian product of A and B.
public static cartesian ( Set $a, Set $b ) : array
$a Set
$b Set
return array
    public static function cartesian(Set $a, Set $b) : array
    {
        $cartesian = [];
        foreach ($a as $multiplier) {
            foreach ($b as $multiplicand) {
                $cartesian[] = new self(array_merge([$multiplicand], [$multiplier]));
            }
        }
        return $cartesian;
    }

Usage Example

Example #1
0
 public function testCartesian()
 {
     $cartesian = Set::cartesian(new Set(['A']), new Set([1, 2]));
     $this->assertInternalType('array', $cartesian);
     $this->assertEquals([new Set(['A', 1]), new Set(['A', 2])], $cartesian);
     $this->assertEquals(2, count($cartesian));
 }