/// <summary> /// Compares two Constraints /// </summary> public static bool AreConstraintsEquals(Constraint first, Constraint second) { if (first.Sign != second.Sign || first.RightSide != second.RightSide) return false; for (var i = 0; i < 2; i++) { foreach (var var in first.LeftSide) { var sCoeff = GetCoefficientByLabel(second, var.Label); if (sCoeff == null) { if (var.Coefficient == 0) continue; return false; } if (var.Coefficient != sCoeff) return false; } var tmp = first; first = second; second = tmp; } return true; }
private static Fraction GetCoefficientByLabel(Constraint lim, string label) { foreach (var var in lim.LeftSide) if (var.Label == label) return var.Coefficient; return null; }
/// <summary> /// Represents math Constraint(copy of input Constraint) /// </summary> public Constraint(Constraint constraint) { _leftSide = new List<Variable>(constraint._leftSide); Sign = constraint.Sign; _rightSide = new Fraction(constraint._rightSide); }
/// <summary> /// Adds input constraint to current /// </summary> /// <param name="constraint">Constraint that must be equal</param> /// <exception cref="FormatException">If one of the constraints is not equal</exception> public void Add(Constraint constraint) { if (constraint.Sign != "=" || Sign != "=") throw new FormatException("Constraints must be equal constraints"); var current = new EqualityConstraint(LeftSide, RightSide); var input = new EqualityConstraint(constraint.LeftSide, constraint.RightSide); current.Add(input); _leftSide = new List<Variable>(current.LeftSide); _rightSide = current.RightSide; }