예제 #1
0
 def __iter__(self):
     """
      TESTS::
      
          sage: IntegerVectors(0, []).list()
          [[]]
          sage: IntegerVectors(5, []).list()
          []
          sage: IntegerVectors(0, [1]).list()
          [[0]]
          sage: IntegerVectors(4, [1]).list()
          [[4]]
          sage: IntegerVectors(4, [2]).list()
          [[4, 0], [3, 1], [2, 2]]
          sage: IntegerVectors(4, [2,2]).list()
           [[4, 0, 0, 0],
           [3, 0, 1, 0],
           [2, 0, 2, 0],
           [2, 0, 1, 1],
           [1, 0, 3, 0],
           [1, 0, 2, 1],
           [0, 0, 4, 0],
           [0, 0, 3, 1],
           [0, 0, 2, 2]]
          sage: IntegerVectors(5, [1,1,1]).list()
          [[5, 0, 0],
           [4, 1, 0],
           [4, 0, 1],
           [3, 2, 0],
           [3, 1, 1],
           [3, 0, 2],
           [2, 3, 0],
           [2, 2, 1],
           [2, 1, 2],
           [2, 0, 3],
           [1, 4, 0],
           [1, 3, 1],
           [1, 2, 2],
           [1, 1, 3],
           [1, 0, 4],
           [0, 5, 0],
           [0, 4, 1],
           [0, 3, 2],
           [0, 2, 3],
           [0, 1, 4],
           [0, 0, 5]]
          sage: IntegerVectors(0, [2,3]).list()
          [[0, 0, 0, 0, 0]]
      """
     for iv in IntegerVectors(self.n, len(self.comp)):
         blocks = [
             IntegerVectors(iv[i], self.comp[i], max_slope=0).__iter__()
             for i in range(len(self.comp))
         ]
         for parts in cartesian_product.CartesianProduct(*blocks):
             res = []
             for part in parts:
                 res += part
             yield res
예제 #2
0
    def __iter__(self):
        """
        TESTS::

            sage: SignedCompositions(0).list()   #indirect doctest
            [[]]
            sage: SignedCompositions(1).list()   #indirect doctest
            [[1], [-1]]
            sage: SignedCompositions(2).list()   #indirect doctest
            [[1, 1], [1, -1], [-1, 1], [-1, -1], [2], [-2]]
        """
        for comp in Compositions_n.__iter__(self):
            l = len(comp)
            a = [[1, -1] for i in range(l)]
            for sign in cartesian_product.CartesianProduct(*a):
                yield [sign[i] * comp[i] for i in range(l)]
import cartesian_product

if __name__ == '__main__':
    set1 = (1, 2, 3)
    set2 = ('a', 'b', 'c')
    set3 = (1.1, 2.2, 3.3)

    # check no arguments
    c1 = cartesian_product.CartesianProduct()
    for i in c1:
        print(i)

    # check cartesian product of two sets
    c2 = cartesian_product.CartesianProduct(set1, set2)
    for i in c2:
        print(i)

    # check cartesian product of three sets
    c3 = cartesian_product.CartesianProduct(set1, set2, set3)
    for i in c3:
        print(i)

    # check cartesian product of two sets but both of sets repeated twice
    c4 = cartesian_product.CartesianProduct(set1, set1, set2, set2)
    for i in c4:
        print(i)