Beispiel #1
0
def _lex(term, others, operator, matrix):
    if len(others) == 0:
        assert is_matrix(term, Variable)
        lists = [flatten(l) for l in term]
    else:
        assert is_1d_list(term, Variable) and all(is_1d_list(l, Variable) for l in others)
        lists = [flatten(term)] + [flatten(l) for l in others]
    assert is_matrix(lists, Variable)  # new check because some null cells (variables) may have been discarded
    assert all(len(l) == len(lists[0]) for l in lists)
    checkType(lists, [Variable])
    checkType(operator, TypeOrderedOperator)
    return ECtr(ConstraintLexMatrix(lists, operator)) if matrix else ECtr(ConstraintLex(lists, operator))
Beispiel #2
0
 def __getitem__li(self, indexes):  # li for ListInt
     if isinstance(indexes, PartialConstraint):
         indexes = auxiliary().replace_partial_constraint(indexes)
     if isinstance(indexes, Variable):
         return PartialConstraint(ConstraintElement(self, indexes))
     if isinstance(indexes, tuple) and len(indexes) > 0:
         indexes = auxiliary().replace_partial_constraints(list(indexes))
         if any(isinstance(i, Variable) for i in indexes):  # this must be a constraint Element-Matrix
             assert is_matrix(self) and len(indexes) == 2, "A matrix is expected, with two indexes"
             if all(isinstance(i, Variable) for i in indexes):
                 return PartialConstraint(ConstraintElementMatrix(self, indexes[0], indexes[1]))
             else:
                 if isinstance(indexes[0], Variable) and isinstance(indexes[1], int):
                     return PartialConstraint(ConstraintElement(self[:, indexes[1]], indexes[0]))
                 elif isinstance(indexes[0], int) and isinstance(indexes[1], Variable):
                     return PartialConstraint(ConstraintElement(self[indexes[0]], indexes[1]))
                 else:
                     assert False
         result = OpOverrider.project_recursive(self, indexes, 0)
         try:
             return ListVar(result)  # TODO is it ListVar or ListInt ?
         except TypeError:
             return result
     result = list.__getitem__(self, indexes)
     try:
         return ListInt(result)
     except TypeError:
         return result
Beispiel #3
0
def columns(m):
    def column(j):
        assert is_2d_list(m), "column() can only be called on 2-dimensional lists"
        assert all(len(row) > j for row in m), "one row has not at least j+1 elements"
        return ListVar(row[j] for row in m)

    assert is_matrix(m), "columns() can only be called on matrices"
    return ListVar(column(j) for j in range(len(m[0])))
Beispiel #4
0
 def __getitem__(self, i):
     assert isinstance(self.constraint, ConstraintElement)
     lst = self.constraint.arguments[TypeCtrArg.LIST].content
     index = self.constraint.arguments[TypeCtrArg.INDEX].content
     if isinstance(i, Variable):
         assert is_matrix(
             lst
         ), "Variables in element constraint must be in the form of matrix"
         self.constraint = ConstraintElementMatrix(lst, index, i)
     elif isinstance(i, int):
         self.constraint = ConstraintElement(lst[:, i], index)
     return self