public IEnumerable /*<IVariable>*//*!*/ SortedVariables(/*maybe null*/ IComparer variableComparer) { Contract.Ensures(Contract.Result <IEnumerable>() != null); if (variableComparer == null) { return(Variables); } else { ArrayList /*IVariable*/ vars = new ArrayList/*IVariable*/ (Count); foreach (IVariable variable in Variables) { vars.Add(variable); } vars.Sort(variableComparer); return(vars); } }
public IEnumerable/*<IVariable>*//*!*/ SortedVariables(/*maybe null*/ IComparer variableComparer) { Contract.Ensures(Contract.Result<IEnumerable>() != null); if (variableComparer == null) { return Variables; } else { ArrayList /*IVariable*/ vars = new ArrayList /*IVariable*/ (Count); foreach (IVariable variable in Variables) { vars.Add(variable); } vars.Sort(variableComparer); return vars; } }
/// <summary> /// Returns 0 if "this" and "c" are not equivalent constraints. If "this" and "c" /// are equivalent constraints, the non-0 return value "m" satisfies "this == m*c". /// </summary> /// <param name="c"></param> /// <returns></returns> public Rational IsEquivalent(LinearConstraint/*!*/ c) { Contract.Requires(c != null); // "m" is the scale factor. If it is 0, it hasn't been used yet. If it // is non-0, it will remain that value throughout, and it then says that // for every dimension "d", "this[d] == m * c[d]". Rational m = Rational.ZERO; ArrayList /*IVariable*/ dd = new ArrayList /*IVariable*/ (); foreach (IVariable/*!*/ d in this.GetDefinedDimensions()) { Contract.Assert(d != null); if (!dd.Contains(d)) { dd.Add(d); } } foreach (IVariable/*!*/ d in c.GetDefinedDimensions()) { Contract.Assert(d != null); if (!dd.Contains(d)) { dd.Add(d); } } foreach (IVariable/*!*/ d in dd) { Contract.Assert(d != null); Rational a = this[d]; Rational b = c[d]; if (a.IsZero || b.IsZero) { if (a.IsNonZero || b.IsNonZero) { return Rational.ZERO; // not equivalent } } else if (m.IsZero) { m = a / b; } else if (a != m * b) { return Rational.ZERO; // not equivalent } } // we expect there to have been some non-zero coefficient, so "m" should have been used by now System.Diagnostics.Debug.Assert(m.IsNonZero); // finally, check the rhs if (this.rhs == m * c.rhs) { return m; // equivalent } else { return Rational.ZERO; // not equivalent } }
/// <summary> /// Returns 0 if "this" and "c" are not equivalent constraints. If "this" and "c" /// are equivalent constraints, the non-0 return value "m" satisfies "this == m*c". /// </summary> /// <param name="c"></param> /// <returns></returns> public Rational IsEquivalent(LinearConstraint /*!*/ c) { Contract.Requires(c != null); // "m" is the scale factor. If it is 0, it hasn't been used yet. If it // is non-0, it will remain that value throughout, and it then says that // for every dimension "d", "this[d] == m * c[d]". Rational m = Rational.ZERO; ArrayList /*IVariable*/ dd = new ArrayList/*IVariable*/ (); foreach (IVariable /*!*/ d in this.GetDefinedDimensions()) { Contract.Assert(d != null); if (!dd.Contains(d)) { dd.Add(d); } } foreach (IVariable /*!*/ d in c.GetDefinedDimensions()) { Contract.Assert(d != null); if (!dd.Contains(d)) { dd.Add(d); } } foreach (IVariable /*!*/ d in dd) { Contract.Assert(d != null); Rational a = this[d]; Rational b = c[d]; if (a.IsZero || b.IsZero) { if (a.IsNonZero || b.IsNonZero) { return(Rational.ZERO); // not equivalent } } else if (m.IsZero) { m = a / b; } else if (a != m * b) { return(Rational.ZERO); // not equivalent } } // we expect there to have been some non-zero coefficient, so "m" should have been used by now System.Diagnostics.Debug.Assert(m.IsNonZero); // finally, check the rhs if (this.rhs == m * c.rhs) { return(m); // equivalent } else { return(Rational.ZERO); // not equivalent } }