Minotaur 0.4.1
Docs for developers
Public Member Functions | List of all members
Minotaur::QuadraticFunction Class Reference

Public Member Functions

 QuadraticFunction ()
 Default constructor.
 
 QuadraticFunction (UInt nz, double *vals, UInt *irow, UInt *jcol, VariableConstIterator vbeg)
 Constructor using an array of values. More...
 
 QuadraticFunction (double *vals, VariableConstIterator vbeg, VariableConstIterator vend)
 
 ~QuadraticFunction ()
 Destroy.
 
QuadraticFunctionPtr clone () const
 
QuadraticFunctionPtr cloneWithVars (VariableConstIterator vbeg) const
 
void addTerm (VariablePair vp, const double weight)
 
void addTerm (ConstVariablePtr v1, ConstVariablePtr v2, double weight)
 
double getFixVarOffset (VariablePtr v, double val)
 
void incTerm (ConstVariablePair vp, const double weight)
 
void incTerm (ConstVariablePtr v1, ConstVariablePtr v2, const double weight)
 
void multiply (double c)
 Multiply by a constant. If constant is zero, all terms are removed.
 
void removeVar (VariablePtr v, double val, LinearFunctionPtr lf)
 
void subst (VariablePtr out, VariablePtr in, double rat)
 
QuadraticFunctionPtr copyAdd (ConstQuadraticFunctionPtr q2) const
 
QuadraticFunctionPtr copyMinus (ConstQuadraticFunctionPtr q2) const
 
QuadraticFunctionPtr copyMult (double c) const
 Multiply a quadratic function with a constant.
 
PolyFunPtr copyMult (LinearFunctionPtr l1) const
 Multiply a linear function and quadratic function.
 
PolyFunPtr copyMult (ConstQuadraticFunctionPtr q2) const
 Multiply two quadratics.
 
void add (ConstQuadraticFunctionPtr q2)
 
VarCountConstMap * getVarMap () const
 void operator*=(const double c); More...
 
VariablePairGroupConstIterator begin () const
 Constant Iterators to visit all the quadratic terms: begin.
 
VariablePairGroupConstIterator end () const
 Constant Iterators to visit all the quadratic terms: end.
 
double getWeight (ConstVariablePair &vp)
 
double getWeight (ConstVariablePtr v1, ConstVariablePtr v2)
 
int getFreq (ConstVariablePtr v1)
 Get the number of times variable v1 occurs in quadratic terms.
 
bool hasVar (ConstVariablePtr v) const
 Check if function contains a variable. More...
 
double eval (const std::vector< double > &x) const
 
double eval (const double *x) const
 
void computeBounds (double *l, double *u)
 Compute bounds for using in Presolver.
 
void bndsquadterms (double *l, double *u, VariablePtr v)
 Finds bound of quadratic term having v variable.
 
void bndsquadterms_2 (double *l, double *u, VariablePtr v, VariablePtr v2)
 Finds bound of quadratic term having v and excluding contribution of v2.
 
void evalGradient (const double *x, double *grad_f)
 
void evalGradient (const std::vector< double > &x, std::vector< double > &grad_f)
 
void evalHessian (const double mult, const double *x, const LTHessStor *stor, double *values, int *error)
 Evaluate the summation of multiplier and hessian of constraints.
 
Convexity isConvex ()
 
QfVector findSubgraphs ()
 
void prepJac (VarSetConstIter vbeg, VarSetConstIter vend)
 Stores Jacobian values, offset and indices in different arrays.
 
void prepHess ()
 Stores hessian matrix values, row and column indices in arrays.
 
void fillHessStor (LTHessStor *hess)
 
void fillJac (const double *x, double *values, int *error)
 Fills the value of jacobian of a function in an array.
 
void finalHessStor (const LTHessStor *hess)
 
UInt getNumTerms () const
 Get the number of terms in this expression.
 
UInt getNumBilTerms () const
 Get the number of bilinear terms in the expression.
 
UInt getNumSqTerms () const
 Get the number of square terms in the expression.
 
UInt getNumVars () const
 Get the number of variables in this expression.
 
void getVars (VariableSet *vars)
 Get the variables in the variable set from the variable frequency map.
 
bool isZero () const
 
NonlinearFunctionPtr getPersp (VariablePtr z, double eps, VariableGroup nNonzeroVar, double intTol)
 
VarIntMapConstIterator varsBegin () const
 Iterate over the variables in the quadratic function: begin.
 
VarIntMapConstIterator varsEnd () const
 Iterate over the variables in the quadratic function: end.
 
void write (std::ostream &s) const
 Display the quadratic function.
 

Constructor & Destructor Documentation

◆ QuadraticFunction() [1/2]

QuadraticFunction::QuadraticFunction ( UInt  nz,
double *  vals,
UInt irow,
UInt jcol,
VariableConstIterator  vbeg 
)

Constructor using an array of values.

Parameters
[in]nzNumber of non zero elements in hessian of quadratic function.
[in]valsThe pointer to the array of value of hessian.
[in]irowThe pointer to the array of index of the row.
[in]jcolThe pointer to the array of index of the column.

Example: x1^2 + 2x1x2 + x2^2 Hessian of above matrix is: [[2 2] [2 2]] nz = 3 vals = [2 2 2] irow = [0 1 1] jcol = [0 0 1]

◆ QuadraticFunction() [2/2]

QuadraticFunction::QuadraticFunction ( double *  vals,
VariableConstIterator  vbeg,
VariableConstIterator  vend 
)

Construct a quadratic function from a row major coefficient array representing symmetric matrix of size n x n and n variables.

Member Function Documentation

◆ add()

void QuadraticFunction::add ( ConstQuadraticFunctionPtr  q2)

This increment operator is dangerous to use because it only works on objects of type QuadraticFunction and does not work on type QuadraticFunctionPtr. So if you have:

QuadraticFunctionPtr qPtr, q2Ptr;

Then you cannot do:

qPtr += q2Ptr;

You will have to do:

(*qPtr) += q2Ptr;

The user must check if the left operand is not NULL.

◆ addTerm() [1/2]

void QuadraticFunction::addTerm ( ConstVariablePtr  v1,
ConstVariablePtr  v2,
double  weight 
)

Add a term of the form a*x_i*x_j to the expression. We do not check if a similar term does not exist.

◆ addTerm() [2/2]

void QuadraticFunction::addTerm ( VariablePair  vp,
const double  weight 
)

Add a term of the form a*x_i*x_j to the expression. We do not check if a similar term does not exist.

◆ clone()

QuadraticFunctionPtr QuadraticFunction::clone ( ) const

Copy the linear function. Variables and weights are copied. The weights in the clone and the original do not share the same space in memory.

◆ cloneWithVars()

QuadraticFunctionPtr QuadraticFunction::cloneWithVars ( VariableConstIterator  vbeg) const

Create the clone of the quadratic function while substituting the variables with the new one.

◆ copyAdd()

QuadraticFunctionPtr QuadraticFunction::copyAdd ( ConstQuadraticFunctionPtr  q2) const

Add a quadratic function to this quadratic function. Terms that become zero are not retained in the function.

◆ copyMinus()

QuadraticFunctionPtr QuadraticFunction::copyMinus ( ConstQuadraticFunctionPtr  q2) const

Subtract a linear function from this function. Terms that become zero are still retained in the function.

◆ eval() [1/2]

double QuadraticFunction::eval ( const double *  x) const

Evaluate the value of this quadratic expression for a given point x. It is assumed that x[i] is the value of the variable whose var->getIndex() returns i.

◆ eval() [2/2]

double QuadraticFunction::eval ( const std::vector< double > &  x) const

Evaluate the value of this quadratic expression for a given point x. It is assumed that x[i] is the value of the variable whose var->getIndex() returns i.

◆ evalGradient() [1/2]

void QuadraticFunction::evalGradient ( const double *  x,
double *  grad_f 
)

Evaluate the values of the gradient of the quadratic expression at a given point x. It is assumed that x[i] is the value of the variable whose var->getIndex() returns i.

◆ evalGradient() [2/2]

void QuadraticFunction::evalGradient ( const std::vector< double > &  x,
std::vector< double > &  grad_f 
)

Evaluate the values of the gradient of the quadratic expression at a given point x. It is assumed that x[i] is the value of the variable whose var->getIndex() returns i.

◆ findSubgraphs()

QfVector QuadraticFunction::findSubgraphs ( )

For the quadratic function finding the subgraphs (where each node represent a variable and edge means there is a bilinear term between those two variables) which are seprable i.e. they have no variable common between them.

◆ getFixVarOffset()

double QuadraticFunction::getFixVarOffset ( VariablePtr  v,
double  val 
)

Returns the value of square term of a variable in quadratic function, when it has val value and has some non zero coefficient.

◆ getVarMap()

VarCountConstMap * QuadraticFunction::getVarMap ( ) const

void operator*=(const double c);

Multiply the quadratic with a constant. Same precaution as for += operator above. If c is zero, then quadratic becomes empty. It is better for the calling routine to check if c is zero, if so, just delete the quadratic. Get the list of variables and how many times they occur

◆ getWeight() [1/2]

double QuadraticFunction::getWeight ( ConstVariablePair &  vp)

Get the coefficient of a term. Returns 0 if the term does not exist in the function.

◆ getWeight() [2/2]

double QuadraticFunction::getWeight ( ConstVariablePtr  v1,
ConstVariablePtr  v2 
)

Get the coefficient of a term. Returns 0 if the term does not exist in the function.

◆ hasVar()

bool QuadraticFunction::hasVar ( ConstVariablePtr  v) const

Check if function contains a variable.

Parameters
[in]vThe variable that we want to test.
Returns
True if this function is has v. False if it doesn't use it.

◆ incTerm() [1/2]

void QuadraticFunction::incTerm ( ConstVariablePair  vp,
const double  weight 
)

Add a term of the form a*x_i*x_j to the expression. If a similar term exists, it is incremented. If the new coefficient becomes zero, the term is dropped.

◆ incTerm() [2/2]

void QuadraticFunction::incTerm ( ConstVariablePtr  v1,
ConstVariablePtr  v2,
const double  weight 
)

Add a term of the form a*x_i*x_j to the expression. If a similar term exists, it is incremented. If the new coefficient becomes zero, the term is dropped.

◆ isConvex()

Convexity QuadraticFunction::isConvex ( )

Checks the convexity of the quadratic function by finding the eigen values of the hessian matrix. It will return whether function is convex (PSD hessian), concave (NSD hessian) or nonconvex (Indefinte hessian).

◆ isZero()

bool Minotaur::QuadraticFunction::isZero ( ) const
inline

Return true if the quadratic expression always returns zero on evaluation.

◆ removeVar()

void QuadraticFunction::removeVar ( VariablePtr  v,
double  val,
LinearFunctionPtr  lf 
)

Remove a variable v from the function. Add to lf any linear terms obtained by fixing the variable to the value 'val'.

◆ subst()

void QuadraticFunction::subst ( VariablePtr  out,
VariablePtr  in,
double  rat 
)

Substitutes some of the variables from the quadratic function with new variables and changing the coefficient value of those variable terms by the ration of 'rat'.


The documentation for this class was generated from the following files:

Minotaur source code documented by Doxygen 1.9.4 on Thu Apr 24 2025