private void ChartAreaValuesView1_AddClicked(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_expressionTextProvider.Text))
            {
                _errorHandler.DisplayError(Strings.Expression_should_not_be_empty_,
                                           Strings.Warning_);
                return;
            }

            if (!AreChartValuesValid)
            {
                _errorHandler.DisplayError(
                    $"{Strings.Min_value_needs_to_be_less_than_Max_value}{Environment.NewLine}{Strings.CheckChartAreaValues}: x0, xN, y0, yN.",
                    Strings.Warning_);
                return;
            }
            try
            {
                customFunctionsEditor.ClearHighlightedErrors();
                CurrentChart.AddFunction(_expressionsEvaluator.Evaluate(_expressionTextProvider.Text,
                                                                        customFunctionsEditor.Text,
                                                                        _calculationsMode));
            }
            catch (Exception ex)
            {
                _exceptionsHandler.HandleException(ex);
            }
        }
Exemplo n.º 2
0
        public void ExpressionShouldEvaluateTheSameAsFunction(string tsl, double x)
        {
            //arrange
            var evaluatedFunction = _expressionsEvaluator.Evaluate(tsl, string.Empty, CalculationsMode.Real);

            //act
            var valueOfEvaluatedFunctionAtPointX = evaluatedFunction.Evaluate(x);
            var valueOfRealFunctionAtPointX      = Functions[tsl].Invoke(x);

            //assert
            Assert.AreEqual(valueOfRealFunctionAtPointX, valueOfEvaluatedFunctionAtPointX);
        }
 private void ChartAreaValuesView1_AddClicked(object sender, EventArgs e)
 {
     if (SharedViewState.Instance.ExpressionText != "")
     {
         try
         {
             SharedViewState.Instance.CustomFunctionsEditor.ClearHighlightedErrors();
             CurrentChart.AddFunction(_expressionsEvaluator.Evaluate(SharedViewState.Instance.ExpressionText,
                                                                     SharedViewState.Instance.CustomFunctionsText,
                                                                     _calculationsMode));
         }
         catch (Exception ex)
         {
             ExceptionsHandler.Instance.HandleException(ex, _errorHandler);
         }
     }
     else
     {
         _errorHandler.DispalyError(Strings.GUI_addToChartButton_Click_Expression_should_not_be_empty_,
                                    Strings.GUI_numericalOperationButton_Click_Warning_);
     }
 }
Exemplo n.º 4
0
        private void _view_CalculateClicked(object sender, EventArgs e)
        {
            if (SharedViewState.Instance.ExpressionText != "")
            {
                try
                {
                    SharedViewState.Instance.CustomFunctionsEditor.ClearHighlightedErrors();
                    var function = _expressionsEvaluator.Evaluate(SharedViewState.Instance.ExpressionText,
                                                                  SharedViewState.Instance.CustomFunctionsText, _calculationsMode);

                    var x = _view.X;
                    var y = _view.Y;
                    var z = new Complex(x, y);

                    dynamic result = function.EvaluateDynamic(x, y);

                    var resultStr = ScriptingExtensions.ToMathString(result);

                    _view.AddResult(SharedViewState.Instance.ExpressionText,
                                    _calculationsMode == CalculationsMode.Complex
                            ? z.ToMathString()
                            : (_calculationsMode == CalculationsMode.Fxy
                                ? $"{x.ToMathString()}, {y.ToMathString()}"
                                : x.ToMathString()), resultStr);
                }
                catch (Exception ex)
                {
                    ExceptionsHandler.Instance.HandleException(ex, _errorHandler);
                }
            }
            else
            {
                _errorHandler.DispalyError(Strings.GUI_addToChartButton_Click_Expression_should_not_be_empty_,
                                           Strings.GUI_numericalOperationButton_Click_Warning_);
            }
        }
Exemplo n.º 5
0
        private void _view_ComputeClicked(object sender, EventArgs e)
        {
            if (_calculationsMode == CalculationsMode.Real)
            {
                try
                {
                    SharedViewState.Instance.CustomFunctionsEditor.ClearHighlightedErrors();
                    var function = expressionsEvaluator.Evaluate(SharedViewState.Instance.ExpressionText,
                                                                 SharedViewState.Instance.CustomFunctionsText, _calculationsMode);

                    Func <double, double> fx = x => function.Evaluate(x);

                    var result = double.NaN;
                    var eps    = _view.Epsilon;

                    if (_view.SelectedOperation == Strings.Derivative ||
                        _view.SelectedOperation == Strings.Function_root)
                    {
                        if (double.IsNaN(eps))
                        {
                            _errorHandler.DispalyError(Strings.GivenΕIsNotValid, Strings.Error);
                            return;
                        }
                        if (!(eps > 0.0) || !(eps < 1))
                        {
                            _errorHandler.DispalyError(
                                Strings.GivenΕIsNotValidΕShouldBeSmallPositiveNumber, Strings.Error);
                            return;
                        }
                    }

                    var parametersStr = "";


                    if (_view.SelectedOperation == Strings.Integral)
                    {
                        result =
                            ((dynamic)
                             Convert.ChangeType(
                                 NumericalMethodsInfo.Instance._methods[_view.SelectedOperation][_view.SelectedMethod
                                 ],
                                 _integrationType))
                                (fx, _view.A, _view.B, _view.N);
                        parametersStr = $"a={_view.A.ToMathString()}; b={_view.B.ToMathString()}; N={_view.N}";
                    }
                    else if (_view.SelectedOperation == Strings.Derivative)
                    {
                        result =
                            ((dynamic)
                             Convert.ChangeType(
                                 NumericalMethodsInfo.Instance._methods[_view.SelectedOperation][_view.SelectedMethod
                                 ],
                                 _derivationType))
                                (fx, _view.X, _view.Order, eps);
                        parametersStr = $"n={_view.Order}; x={_view.X.ToMathString()}; ε={eps.ToMathString()}";
                    }
                    else if (_view.SelectedOperation == Strings.Function_root)
                    {
                        result =
                            ((dynamic)
                             Convert.ChangeType(
                                 NumericalMethodsInfo.Instance._methods[_view.SelectedOperation][_view.SelectedMethod
                                 ],
                                 _functionRootType))
                                (fx, _view.A, _view.B, eps, _view.N);
                        parametersStr =
                            $"a={_view.A.ToMathString()}; b={_view.B.ToMathString()}; ε={eps.ToMathString()}; N={_view.N}";
                    }

                    _view.AddResult(SharedViewState.Instance.ExpressionText,
                                    _view.SelectedOperation,
                                    _view.SelectedMethod,
                                    parametersStr,
                                    result.ToMathString());
                }
                catch (Exception ex)
                {
                    ExceptionsHandler.Instance.HandleException(ex, _errorHandler);
                }
            }
            else
            {
                _errorHandler.DispalyError(
                    Strings
                    .GUI_numericalOperationButton_Click_Only_Real_mode_is_supported_in_Numerical_calculations_right_now__more_to_come_in_next_versions_ +
                    Environment.NewLine +
                    Strings.GUI_numericalOperationButton_Click__Check__Real___f_x___mode,
                    Strings.GUI_numericalOperationButton_Click_Warning_);
            }
        }