コード例 #1
0
        public void SolveFor(VariableIdentifierCollection resultVariable)
        {
            SimpleSolveForZero();
            bool foundResultingVariable   = false;
            VariableCollection _rightSide = new VariableCollection();

            foreach (Variable variable in LeftSide)
            {
                int matchingVariableCount = 0;
                foreach (VariableIdentifier varIdentifier in variable.Identifiers)
                {
                    if (resultVariable.Contains(varIdentifier))
                    {
                        matchingVariableCount++;
                    }
                }
                if (matchingVariableCount == 0)
                {
                    continue;
                }

                if (matchingVariableCount == resultVariable.Count)
                {
                    _rightSide.Add(variable);
                    foundResultingVariable = true;
                    continue;
                }

                throw new InvalidOperationException("Cannot solve this equation, beacuse ResultingVariable isn't a single variable in this context!");
            }

            if (LeftSide.Count == 0 || (LeftSide.Count == 1 && LeftSide[0].Multiplier == 0))
            {
                throw new InfinitlyManySolutionsException();
            }

            if (!foundResultingVariable)
            {
                throw new NoSolutionException("The equation has no solution for the following variable: " + resultVariable);
            }

            LeftSide -= _rightSide;
            RightSide = -(_rightSide);
            SimplifyBothSides();

            Variable finalVariable = new Variable(resultVariable, 1);

            int rightTermsCount = RightSide.Count;

            foreach (Variable variable in RightSide)
            {
                Variable divider = variable / finalVariable;
                LeftSide /= divider;
            }
            LeftSide /= rightTermsCount;
            RightSide = finalVariable;

            LeftSide.Simplify();

            VariableCollection leftSideTmp = LeftSide.Clone();

            LeftSide  = RightSide;
            RightSide = leftSideTmp;
        }