/// <summary> /// Replace var with a symbolic expression expr that is equal to it. /// If a variable has been added to this expression that wasn't there /// before, or if a variable has been dropped from this expression /// because it now has a coefficient of 0, inform the solver. /// PRECONDITIONS: /// var occurs with a non-zero coefficient in this expression. /// </summary> public /*sealed*/ void SubstituteOut(ClAbstractVariable var, ClLinearExpression expr, ClAbstractVariable subject, ClTableau solver) { if (Trace) { FnEnterPrint(string.Format("CLE:SubstituteOut: {0}, {1}, {2}, ...", var, expr, subject)); } if (Trace) { TracePrint("this = " + this); } double multiplier = ((ClDouble)_terms[var]).Value; _terms.Remove(var); IncrementConstant(multiplier * expr.Constant); foreach (ClAbstractVariable clv in expr.Terms.Keys) { double coeff = ((ClDouble)expr.Terms[clv]).Value; ClDouble d_old_coeff = (ClDouble)_terms[clv]; if (d_old_coeff != null) { double old_coeff = d_old_coeff.Value; double newCoeff = old_coeff + multiplier * coeff; if (Cl.Approx(newCoeff, 0.0)) { solver.NoteRemovedVariable(clv, subject); _terms.Remove(clv); } else { d_old_coeff.Value = newCoeff; } } else { // did not have that variable already _terms.Add(clv, new ClDouble(multiplier * coeff)); solver.NoteAddedVariable(clv, subject); } } if (Trace) { TracePrint("Now this is " + this); } }
/// <summary> /// Add a term c*v to this expression. If the expression already /// contains a term involving v, add c to the existing coefficient. /// If the new coefficient is approximately 0, delete v. Notify the /// solver if v appears or disappears from this expression. /// </summary> public /*sealed*/ ClLinearExpression AddVariable(ClAbstractVariable v, double c, ClAbstractVariable subject, ClTableau solver) { // body largely duplicated above if (Trace) { FnEnterPrint(string.Format("AddVariable: {0}, {1}, {2}, ...", v, c, subject)); } ClDouble coeff = (ClDouble)_terms[v]; if (coeff != null) { double new_coefficient = coeff.Value + c; if (Cl.Approx(new_coefficient, 0.0)) { solver.NoteRemovedVariable(v, subject); _terms.Remove(v); } else { coeff.Value = new_coefficient; } } else { if (!Cl.Approx(c, 0.0)) { _terms.Add(v, new ClDouble(c)); solver.NoteAddedVariable(v, subject); } } return(this); }
/// <summary> /// Replace var with a symbolic expression expr that is equal to it. /// If a variable has been added to this expression that wasn't there /// before, or if a variable has been dropped from this expression /// because it now has a coefficient of 0, inform the solver. /// PRECONDITIONS: /// var occurs with a non-zero coefficient in this expression. /// </summary> public void SubstituteOut(ClAbstractVariable var, ClLinearExpression expr, ClAbstractVariable subject, ClTableau solver) { double multiplier = (_terms[var]).Value; _terms.Remove(var); IncrementConstant(multiplier * expr.Constant); foreach (ClAbstractVariable clv in expr.Terms.Keys) { double coeff = (expr.Terms[clv]).Value; ClDouble dOldCoeff; if (_terms.TryGetValue(clv, out dOldCoeff)) { double oldCoeff = dOldCoeff.Value; double newCoeff = oldCoeff + multiplier * coeff; if (Approx(newCoeff, 0.0)) { solver.NoteRemovedVariable(clv, subject); _terms.Remove(clv); } else { dOldCoeff.Value = newCoeff; } } else { // did not have that variable already _terms.Add(clv, new ClDouble(multiplier * coeff)); solver.NoteAddedVariable(clv, subject); } } }
/// <summary> /// Add a term c*v to this expression. If the expression already /// contains a term involving v, add c to the existing coefficient. /// If the new coefficient is approximately 0, delete v. Notify the /// solver if v appears or disappears from this expression. /// </summary> public ClLinearExpression AddVariable(ClAbstractVariable v, double c, ClAbstractVariable subject, ClTableau solver) { ClDouble coeff = _terms[v]; if (coeff != null) { double newCoefficient = coeff.Value + c; if (Approx(newCoefficient, 0.0)) { solver.NoteRemovedVariable(v, subject); _terms.Remove(v); } else { coeff.Value = newCoefficient; } } else { if (!Approx(c, 0.0)) { _terms.Add(v, new ClDouble(c)); solver.NoteAddedVariable(v, subject); } } return(this); }
/// <summary> /// Add n*expr to this expression from another expression expr. /// Notify the solver if a variable is added or deleted from this /// expression. /// </summary> public ClLinearExpression AddExpression(ClLinearExpression expr, double n, ClAbstractVariable subject, ClTableau solver) { IncrementConstant(n * expr.Constant); foreach (ClAbstractVariable clv in expr.Terms.Keys) { double coeff = (expr.Terms[clv]).Value; AddVariable(clv, coeff * n, subject, solver); } return(this); }
/// <summary> /// Replace var with a symbolic expression expr that is equal to it. /// If a variable has been added to this expression that wasn't there /// before, or if a variable has been dropped from this expression /// because it now has a coefficient of 0, inform the solver. /// PRECONDITIONS: /// var occurs with a non-zero coefficient in this expression. /// </summary> public /*sealed*/ void SubstituteOut(ClAbstractVariable var, ClLinearExpression expr, ClAbstractVariable subject, ClTableau solver) { if (Trace) FnEnterPrint(string.Format("CLE:SubstituteOut: {0}, {1}, {2}, ...", var, expr, subject)); if (Trace) TracePrint("this = " + this); double multiplier = ((ClDouble) _terms[var]).Value; _terms.Remove(var); IncrementConstant(multiplier * expr.Constant); foreach (ClAbstractVariable clv in expr.Terms.Keys) { double coeff = ((ClDouble) expr.Terms[clv]).Value; ClDouble d_old_coeff = (ClDouble) _terms[clv]; if (d_old_coeff != null) { double old_coeff = d_old_coeff.Value; double newCoeff = old_coeff + multiplier * coeff; if (Cl.Approx(newCoeff, 0.0)) { solver.NoteRemovedVariable(clv, subject); _terms.Remove(clv); } else { d_old_coeff.Value = newCoeff; } } else { // did not have that variable already _terms.Add(clv, new ClDouble(multiplier * coeff)); solver.NoteAddedVariable(clv, subject); } } if (Trace) TracePrint("Now this is " + this); }
/// <summary> /// Add a term c*v to this expression. If the expression already /// contains a term involving v, add c to the existing coefficient. /// If the new coefficient is approximately 0, delete v. Notify the /// solver if v appears or disappears from this expression. /// </summary> public /*sealed*/ ClLinearExpression AddVariable(ClAbstractVariable v, double c, ClAbstractVariable subject, ClTableau solver) { // body largely duplicated above if (Trace) FnEnterPrint(string.Format("AddVariable: {0}, {1}, {2}, ...", v, c, subject)); ClDouble coeff = (ClDouble) _terms[v]; if (coeff != null) { double new_coefficient = coeff.Value + c; if (Cl.Approx(new_coefficient, 0.0)) { solver.NoteRemovedVariable(v, subject); _terms.Remove(v); } else { coeff.Value = new_coefficient; } } else { if (!Cl.Approx(c, 0.0)) { _terms.Add(v, new ClDouble(c)); solver.NoteAddedVariable(v, subject); } } return this; }
/// <summary> /// Add n*expr to this expression from another expression expr. /// Notify the solver if a variable is added or deleted from this /// expression. /// </summary> public /*sealed*/ ClLinearExpression AddExpression(ClLinearExpression expr, double n, ClAbstractVariable subject, ClTableau solver) { IncrementConstant(n * expr.Constant); foreach (ClAbstractVariable clv in expr.Terms.Keys) { double coeff = ((ClDouble) expr.Terms[clv]).Value; AddVariable(clv, coeff * n, subject, solver); } return this; }