object.extractLinear
object.extractLinear |
Purpose
Returns the variables and coefficients of the linear part of any expression.
Synopsis
vars, coef = a.extractLinear()
Arguments
a
|
An expression or variable.
|
vars
|
A list containing the variable objects composing the linear expression in
a.
|
coef
|
A list containing the corresponding coefficients in the linear expression.
|
Example
The following code snippets show what is the expected result of applying
extractLinear:
import xpress as xp x = xp.var() y = xp.var(name='myvar') a = x + 2*y b = 3*x c = y**2 + x**2 - 6*x d = x**5 - 7*x # nonlinear expression print (a.extractLinear()) # will print "([C1, myvar], [1, 2])" assert (a.extractLinear() == ([x, y], [1, 2])) print (b.extractLinear()) # will print "([C1], [3])" print (c.extractLinear()) # will print "([C1], [-6])" print (d.extractLinear()) # will print "([C1], [-7])"
Further information
1. Note that this operator returns variable objects, not indices, in the
vars portion of the output tuple. To obtain indices, use the
problem.getIndex function. Printing these lists will show the name of the associated variables, as determined by the user when creating the variable with the
name argument or, if
name was not provided, it will show the name as determined by the Optimizer's library (default variable names are "C"+index). See also the Modelling chapter.
2. This operator is most useful only for linear expressions with more than one element. For nonlinear expressions, the function attempts to extract as much linear information it can, but will not be able to infer linearity apart from the most obvious cases. For example, for the expression
x**4 + xp.log(xp.exp(y)), which contains the linear term
y, the function will return
([],[]).