private void SubstituteVariablesValues() { foreach (var variable in _variablesList) { if (variable.Value.All(x => x.Variable.Equals(Variable.NULL))) continue; var equation = new Equation(new Expression(1, variable), variable.Value); variable.Value = new List<Expression>(); equation.Solve(variable); variable.SetValue(equation.RightPart); } }
/// <summary> /// Returns true if two Equations are the same. e.g. x+y=2 and 2x+2y=4 /// </summary> /// <param name="other"></param> /// <returns></returns> public bool IsEquivalent(Equation other) { Solve(Variable.NULL); other.Solve(Variable.NULL); if (IsEmpty(_rightPart) && IsEmpty(other.RightPart)) return true; if (_rightPart.Count != other.RightPart.Count) return false; _rightPart = _rightPart.OrderBy(x => x.Variable.Name).ToList(); var coef1 = _rightPart[0].Coefficient; var coef2 = other.RightPart[0].Coefficient; var a = coef1/coef2; for (int i = 0; i < _rightPart.Count; i++) { var var1Name = _rightPart[i].Variable.Name; bool hasSameVariable = false; foreach (var expression in other.RightPart) { if (expression.Variable.Name == var1Name) { hasSameVariable = true; coef2 = expression.Coefficient; break; } } if (!hasSameVariable) return false; coef1 = _rightPart[i].Coefficient; if ((Math.Abs(coef1 / coef2 - a) > TOLERANCE)) return false; } if (_leftPart.Count == 0 && other.LeftPart.Count == 0) return true; coef1 = _leftPart[0].Coefficient; coef2 = other._leftPart[0].Coefficient; if ((Math.Abs(coef1/coef2 - a) < TOLERANCE)) return true; return false; }