MathPHP\SetTheory\Set::cartesianProduct PHP Method

cartesianProduct() public method

Example: A = (1, 2) B = (a, b) A×B = ((1, a), (1, b), (2, 1), (2, b))
public cartesianProduct ( Set $B ) : Set
$B Set
return Set
    public function cartesianProduct(Set $B)
    {
        $A×B = [];
        foreach ($this->A as $x) {
            foreach ($B->asArray() as $y) {
                $A×B[] = new Set([$x, $y]);
            }
        }
        return new Set($A×B);
    }

Usage Example

 /**
  * @dataProvider dataProviderForCartesianProduct
  */
 public function testCartesianProduct(array $A, array $B, array $A×B, Set $R)
 {
     $setA = new Set($A);
     $setB = new Set($B);
     $setA×B = $setA->cartesianProduct($setB);
     $A×B_array = $setA×B->asArray();
     $this->assertEquals($R, $setA×B);
     $this->assertEquals($A×B, $A×B_array);
     $this->assertEquals(count($setA×B), count($A×B));
     foreach ($setA×B as $key => $value) {
         $this->assertInstanceOf('MathPHP\\SetTheory\\Set', $value);
         $this->assertEquals(2, count($value));
     }
     foreach ($A×B_array as $key => $value) {
         $this->assertInstanceOf('MathPHP\\SetTheory\\Set', $value);
         $this->assertEquals(2, count($value));
     }
 }