2using System.Collections.Generic;
4using System.Xml.Serialization;
13[XmlType(TypeName =
"linearExpression")]
16 private readonly List<LinearTerm> _terms =
47 [XmlArrayItem(
"term")]
48 public List<LinearTerm>
Terms =>
54 [XmlAttribute(
"constant")]
119 ArgumentNullException.ThrowIfNull(
125 _terms.FirstOrDefault(
127 currentTerm.VariableId ==
130 if (existingTerm is
null)
143 double combinedCoefficient =
144 existingTerm.Coefficient +
151 if (combinedCoefficient == 0.0)
159 existingTerm.Coefficient =
176 ArgumentNullException.ThrowIfNull(
188 expression.Constant);
204 double updatedConstant =
303 throw new InvalidOperationException(
304 "A linear expression cannot contain a null " +
310 if (!variableIds.Add(
313 throw new InvalidOperationException(
314 $
"Variable identifier '{term.VariableId}' " +
315 "appears more than once in the linear " +
321 private static void EnsureFiniteValue(
330 throw new InvalidOperationException(
331 $
"{valueName} must be a finite number.");
void MultiplyBy(double factor)
Multiplies the entire expression by a scalar.
LinearExpression Clone()
Creates an independent copy of this expression.
void AddTerm(int variableId, double coefficient)
Adds a linear term to the expression.
void AddConstant(double value)
Adds a constant value to the expression.
LinearExpression()
Initializes an empty linear expression.
List< LinearTerm > Terms
Gets the linear terms of the expression.
void Clear()
Removes all terms and resets the constant to zero.
bool IsConstant
Gets a value indicating whether the expression contains no variable term.
void AddTerm(LinearTerm term)
Adds a linear term to the expression.
void Add(LinearExpression expression)
Adds another linear expression to this expression.
LinearExpression(double constant)
Initializes a linear expression with a constant value.
double Constant
Gets or sets the constant value of the expression.
int TermCount
Gets the number of variable terms.
void EnsureValid()
Validates the linear expression.
Represents one coefficient-variable term in a linear expression.
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
double Coefficient
Gets or sets the coefficient multiplying the referenced variable.
void EnsureValid()
Validates the linear term.