示例#1
0
        /// <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;
        }
示例#2
0
        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);
            }
        }
示例#3
0
        public static Equation operator *(Equation eq1, double value)
        {
            var leftPart = new List<Expression>();
            var rightPart = new List<Expression>();
            eq1.LeftPart.ForEach(x =>
            {
                var expression = new Expression(x.Coefficient*value, x.Variable);
                leftPart.Add(expression);
            });

            eq1.RightPart.ForEach(x =>
            {
                var expression = new Expression(x.Coefficient*value, x.Variable);
                rightPart.Add(expression);
            });

            var newEquation = new Equation(leftPart, rightPart);
            return newEquation;
        }