def scal_prod_triple(self, left_terms, right_terms, coefs): used_coefs = None checker = self._model._checker if is_iterable(coefs, accept_string=False): used_coefs = coefs elif is_number(coefs): if coefs: used_coefs = generate_constant(coefs, count_max=None) else: return self.new_zero_expr() else: self._model.fatal( "scal_prod_triple expects iterable or number as coefficients, got: {0!r}", coefs) if is_iterable(left_terms): used_left = checker.typecheck_var_seq(left_terms) else: checker.typecheck_var(left_terms) used_left = generate_constant(left_terms, count_max=None) if is_iterable(right_terms): used_right = checker.typecheck_var_seq(right_terms) else: checker.typecheck_var(right_terms) used_right = generate_constant(right_terms, count_max=None) if used_coefs is not coefs and used_left is not left_terms and used_right is not right_terms: # LOOK return left_terms * right_terms * coefs return self._scal_prod_triple(coefs=used_coefs, left_terms=used_left, right_terms=used_right)
def scal_prod_triple_vars(self, left_terms, right_terms, coefs): """ Creates a quadratic expression from two lists of variables and a sequence of coefficients. This method sums all quadratic terms built by multiplying the i_th coefficient by the product of the i_th expression in `left_terms` and the i_th expression in `right_terms` This method is faster than the standard generic scalar quadratic product method due to the fact that it takes only variables and does not take expressions as arguments. Example: `Model.scal_prod_vars_triple([x, y], [z, t], [2, 3])` returns the expression `2xz + 3yt`. :param left_terms: A list or an iterator on variables. :param right_terms: A list or an iterator on variables. :param coefs: A list or an iterator on numbers or a number. :returns: An instance of :class:`docplex.mp.quad.QuadExpr` or 0. Note: If either list or iterator is empty, this method returns zero. """ used_coefs = None checker = self._checker nb_non_iterables = 0 if is_iterable(coefs, accept_string=False): used_coefs = coefs elif is_number(coefs): if coefs: used_coefs = generate_constant(coefs, count_max=None) nb_non_iterables += 1 else: return self._aggregator.new_zero_expr() else: self.fatal( "scal_prod_triple expects iterable or number as coefficients, got: {0!r}", coefs) if is_iterable(left_terms): used_left = checker.typecheck_var_seq(left_terms) else: nb_non_iterables += 1 checker.typecheck_var(left_terms) used_left = generate_constant(left_terms, count_max=None) if is_iterable(right_terms): used_right = checker.typecheck_var_seq(right_terms) else: nb_non_iterables += 1 checker.typecheck_var(right_terms) used_right = generate_constant(right_terms, count_max=None) if nb_non_iterables >= 3: return left_terms * right_terms * coefs else: return self._aggregator._scal_prod_triple_vars( left_terms=used_left, right_terms=used_right, coefs=used_coefs)
def check_vars_domain(self, lbs, ubs, names): l_ubs = len(ubs) l_lbs = len(lbs) if l_lbs and l_ubs: names = names or generate_constant(None, max(l_lbs, l_ubs)) for lb, ub, varname in izip(lbs, ubs, names): self.check_var_domain(lb, ub, varname)
def check_vars_domain(self, lbs, ubs, names): l_ubs = len(ubs) l_lbs = len(lbs) if l_lbs and l_ubs: names = names or generate_constant(None, max(l_lbs, l_ubs)) # noinspection PyArgumentList,PyArgumentList for lb, ub, varname in izip(lbs, ubs, names): self.check_var_domain(lb, ub, varname)
def new_range_block(self, lbs, exprs, ubs, names): try: n_exprs = len(exprs) if n_exprs != len(lbs): # pragma: no cover self.fatal( 'incorrect number of expressions: expecting {0}, got: {1}'. format(len(lbs), n_exprs)) except TypeError: pass # no len available. if not names: names = generate_constant(None, None) ranges = [ self.new_range_constraint(lb, exp, ub, name) for lb, exp, ub, name in izip(lbs, exprs, ubs, names) ] # else: # ranges = [self.new_range_constraint(lb, exp, ub) for lb, exp, ub in izip(lbs, exprs, ubs)] self._post_constraint_block(ranges) return ranges