Beispiel #1
0
        /// <summary>
        /// This function calculate the status value for an incomplete equation.
        /// This should be called if the IsCompleteEquation() method returns false.
        /// </summary>
        /// <returns>An enum value of type 'LinearEquationParserStatus'</returns>
        private LinearEquationParserStatus GetEquationStatus()
        {
            LinearEquationParserStatus status = LinearEquationParserStatus.Success;

            if ((!m_equalSignInEquationFlag) &&
                (!m_termBeforeEqualSignExistsFlag) &&
                (!m_termAfterEqualSignExistsFlag) &&
                (!m_atLeastOneVariableInEquationFlag))
            {
                status = LinearEquationParserStatus.SuccessNoEquation;
            }
            else if (!m_equalSignInEquationFlag)
            {
                status = LinearEquationParserStatus.ErrorNoEqualSign;
            }
            else if (!m_termBeforeEqualSignExistsFlag)
            {
                status = LinearEquationParserStatus.ErrorNoTermBeforeEqualSign;
            }
            else if (!m_termAfterEqualSignExistsFlag)
            {
                status = LinearEquationParserStatus.ErrorNoTermAfterEqualSign;
            }
            else if (!m_atLeastOneVariableInEquationFlag)
            {
                status = LinearEquationParserStatus.ErrorNoVariableInEquation;
            }
            else
            {
                status = LinearEquationParserStatus.Success;
            }

            return(status);
        }
        private void Solve()
        {
            mainToolStripStatusLabel.Text = Properties.Resources.IDS_SOLVING_EQUATIONS;

            Sparse2DMatrix <int, int, double> aMatrix = new Sparse2DMatrix <int, int, double>();
            SparseArray <int, double>         bVector = new SparseArray <int, double>();
            SparseArray <string, int>         variableNameIndexMap = new SparseArray <string, int>();
            int numberOfEquations = 0;

            LinearEquationParser       parser       = new LinearEquationParser();
            LinearEquationParserStatus parserStatus = LinearEquationParserStatus.Success;

            foreach (string inputLine in equationsRichTextBox.Lines)
            {
                parserStatus = parser.Parse(inputLine,
                                            aMatrix,
                                            bVector,
                                            variableNameIndexMap,
                                            ref numberOfEquations);

                if (parserStatus != LinearEquationParserStatus.Success)
                {
                    break;
                }
            }

            // Assume success.
            string mainStatusBarText = Properties.Resources.IDS_EQUATIONS_SOLVED;

            // Did an error occur?
            if (parserStatus == LinearEquationParserStatus.Success)
            {
                // Are there the same number of equations as variables?
                if (numberOfEquations == variableNameIndexMap.Count)
                {
                    // Create a solution vector.
                    SparseArray <int, double> xVector = new SparseArray <int, double>();

                    // Solve the simultaneous equations.
                    LinearEquationSolverStatus solverStatus =
                        LinearEquationSolver.Solve(numberOfEquations,
                                                   aMatrix,
                                                   bVector,
                                                   xVector);

                    if (solverStatus == LinearEquationSolverStatus.Success)
                    {
                        string solutionString = "";

                        foreach (KeyValuePair <string, int> pair in variableNameIndexMap)
                        {
                            solutionString += string.Format("{0} = {1}", pair.Key, xVector[pair.Value]);
                            solutionString += "\n";
                        }

                        equationsRichTextBox.Text += "\n" + solutionString;
                    }
                    else if (solverStatus == LinearEquationSolverStatus.IllConditioned)
                    {
                        mainStatusBarText = Properties.Resources.IDS_ILL_CONDITIONED_SYSTEM_OF_EQUATIONS;
                    }
                    else if (solverStatus == LinearEquationSolverStatus.Singular)
                    {
                        mainStatusBarText = Properties.Resources.IDS_SINGULAR_SYSTEM_OF_EQUATIONS;
                    }
                }
                else if (numberOfEquations < variableNameIndexMap.Count)
                {
                    mainStatusBarText = string.Format(Properties.Resources.IDS_TOO_FEW_EQUATIONS,
                                                      numberOfEquations, variableNameIndexMap.Count);
                }
                else if (numberOfEquations > variableNameIndexMap.Count)
                {
                    mainStatusBarText = string.Format(Properties.Resources.IDS_TOO_MANY_EQUATIONS,
                                                      numberOfEquations, variableNameIndexMap.Count);
                }
            }
            else
            {
                // An error occurred. Report the error in the status bar.
                mainStatusBarText = LinearEquationParserStatusInterpreter.GetStatusString(parserStatus);
            }

            mainToolStripStatusLabel.Text = mainStatusBarText;
        }
Beispiel #3
0
        public string SolveEquation(string equation)
        {
            Sparse2DMatrix <int, int, double> aMatrix = new Sparse2DMatrix <int, int, double>();
            SparseArray <int, double>         bVector = new SparseArray <int, double>();
            SparseArray <string, int>         variableNameIndexMap = new SparseArray <string, int>();
            int numberOfEquations = 0;

            LinearEquationParser       parser       = new LinearEquationParser();
            LinearEquationParserStatus parserStatus = LinearEquationParserStatus.Success;

            string[] lines = equation.Split(new[] { "\r\n", "\r", "\n", "\\n", "," }, StringSplitOptions.None);

            foreach (string inputLine in lines)
            {
                parserStatus = parser.Parse(inputLine,
                                            aMatrix,
                                            bVector,
                                            variableNameIndexMap,
                                            ref numberOfEquations);

                if (parserStatus != LinearEquationParserStatus.Success)
                {
                    break;
                }
            }

            // Assume success.
            string mainStatusBarText = "Equations solved";

            // Did an error occur?
            if (parserStatus == LinearEquationParserStatus.Success)
            {
                // Are there the same number of equations as variables?
                if (numberOfEquations == variableNameIndexMap.Count)
                {
                    // Create a solution vector.
                    SparseArray <int, double> xVector = new SparseArray <int, double>();

                    // Solve the simultaneous equations.
                    LinearEquationSolverStatus solverStatus =
                        LinearEquationSolver.Solve(numberOfEquations,
                                                   aMatrix,
                                                   bVector,
                                                   xVector);

                    if (solverStatus == LinearEquationSolverStatus.Success)
                    {
                        string solutionString = "";
                        foreach (KeyValuePair <string, int> pair in variableNameIndexMap)
                        {
                            solutionString += string.Format("{0} = {1}" + " ", pair.Key, xVector[pair.Value]);
                        }

                        return(solutionString);
                    }
                    else if (solverStatus == LinearEquationSolverStatus.IllConditioned)
                    {
                        mainStatusBarText = "Error - the system of equations is ill conditioned.";
                    }
                    else if (solverStatus == LinearEquationSolverStatus.Singular)
                    {
                        mainStatusBarText = "Error - the system of equations is singular.";
                    }
                }
                else if (numberOfEquations < variableNameIndexMap.Count)
                {
                    mainStatusBarText = string.Format("Only {0} equations is too few equations for {1} variables", numberOfEquations, variableNameIndexMap.Count);
                }
                else if (numberOfEquations > variableNameIndexMap.Count)
                {
                    mainStatusBarText = string.Format("{0} equations is too many equations for only {1} variables", numberOfEquations, variableNameIndexMap.Count);
                }
            }
            else
            {
                // An error occurred. Report the error in the status bar.
                mainStatusBarText = LinearEquationParserStatusInterpreter.GetStatusString(parserStatus);
            }

            return(mainStatusBarText);
        }
Beispiel #4
0
        /// <summary>
        /// This method returns a status string corresponding to the passed
        /// status value returned from the LinearEquationParser.Parse method.
        /// </summary>
        /// <param name="status">An status value of type LinearEquationParserStatus</param>
        /// <returns>The string corresponding to the passed status code.</returns>
        public static string GetStatusString(LinearEquationParserStatus status)
        {
            string statusString = "";

            switch (status)
            {
            case LinearEquationParserStatus.Success:
            case LinearEquationParserStatus.SuccessNoEquation:
                statusString = Properties.Resources.IDS_SUCCESS;
                break;

            case LinearEquationParserStatus.ErrorIllegalEquation:
                statusString = Properties.Resources.IDS_ERROR_ILLEGAL_EQUATION;
                break;

            case LinearEquationParserStatus.ErrorNoEqualSign:
                statusString = Properties.Resources.IDS_ERROR_NO_EQUAL_SIGN;
                break;

            case LinearEquationParserStatus.ErrorMultipleEqualSigns:
                statusString = Properties.Resources.IDS_ERROR_MULTIPLE_EQUAL_SIGNS;
                break;

            case LinearEquationParserStatus.ErrorNoTermBeforeEqualSign:
                statusString = Properties.Resources.IDS_ERROR_NO_TERM_BEFORE_EQUAL_SIGN;
                break;

            case LinearEquationParserStatus.ErrorNoTermAfterEqualSign:
                statusString = Properties.Resources.IDS_ERROR_NO_TERM_AFTER_EQUAL_SIGN;
                break;

            case LinearEquationParserStatus.ErrorNoTermEncountered:
                statusString = Properties.Resources.IDS_ERROR_NO_TERM_ENCOUNTERED;
                break;

            case LinearEquationParserStatus.ErrorNoVariableInEquation:
                statusString = Properties.Resources.IDS_ERROR_NO_VARIABLE_IN_EQUATION;
                break;

            case LinearEquationParserStatus.ErrorMultipleDecimalPoints:
                statusString = Properties.Resources.IDS_ERROR_MULTIPLE_DECIMAL_POINTS;
                break;

            case LinearEquationParserStatus.ErrorTooManyDigits:
                statusString = Properties.Resources.IDS_ERROR_TOO_MANY_DIGITS;
                break;

            case LinearEquationParserStatus.ErrorMissingExponent:
                statusString = Properties.Resources.IDS_ERROR_MISSING_EXPONENT;
                break;

            case LinearEquationParserStatus.ErrorIllegalExponent:
                statusString = Properties.Resources.IDS_ERROR_ILLEGAL_EXPONENT;
                break;

            default:
                statusString = Properties.Resources.IDS_ERROR_ILLEGAL_EQUATION;
                break;
            }

            return(statusString);
        }
Beispiel #5
0
 private void SetLastStatusValue(LinearEquationParserStatus status,
                                 int positionIndex)
 {
     LastStatusValue = status;
     ErrorPosition   = positionIndex;
 }
        public static string GetStatusString(LinearEquationParserStatus status)
        {
            string statusString = "";

            switch (status)
            {
            case LinearEquationParserStatus.Success:
            case LinearEquationParserStatus.SuccessNoEquation:
                statusString = "The equation was parsed successfully.";
                break;

            case LinearEquationParserStatus.ErrorIllegalEquation:
                statusString = "The equation syntax is illegal.";
                break;

            case LinearEquationParserStatus.ErrorNoEqualSign:
                statusString = "There is no equal sign in the equation.";
                break;

            case LinearEquationParserStatus.ErrorMultipleEqualSigns:
                statusString = "There are multiple equal signs in the equation.";
                break;

            case LinearEquationParserStatus.ErrorNoTermBeforeEqualSign:
                statusString = "There is no term before the equal sign in the equation.";
                break;

            case LinearEquationParserStatus.ErrorNoTermAfterEqualSign:
                statusString = "There is no term after the equal sign in the equation.";
                break;

            case LinearEquationParserStatus.ErrorNoTermEncountered:
                statusString = "A number or a variable was expected.";
                break;

            case LinearEquationParserStatus.ErrorNoVariableInEquation:
                statusString = "There is no variable in the equation.";
                break;

            case LinearEquationParserStatus.ErrorMultipleDecimalPoints:
                statusString = "A number contains more than one decimal point.";
                break;

            case LinearEquationParserStatus.ErrorTooManyDigits:
                statusString = "A number contains more than 15 digits.";
                break;

            case LinearEquationParserStatus.ErrorMissingExponent:
                statusString = "A number contains the '^' character and is missing an exponent.";
                break;

            case LinearEquationParserStatus.ErrorIllegalExponent:
                statusString = "A number contains an illegal exponent.";
                break;

            default:
                statusString = "The equation syntax is illegal.";
                break;
            }

            return(statusString);
        }