Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _set_linear_objective_term(self, variable, coefficient):
# TODO: the is extremely slow for objectives with many terms
if variable in self.objective.expression.atoms(sympy.Symbol):
a = sympy.Wild('a', exclude=[variable])
(new_expression, map) = self.objective.expression.replace(lambda expr: expr.match(a*variable), lambda expr: coefficient*variable, simultaneous=False, map=True)
self.objective.expression = new_expression
else:
self.objective.expression = sympy.Add._from_args((self.objective.expression, sympy.Mul._from_args((sympy.RealNumber(coefficient), variable))))