示例#1
0
        private void EvaluateButton_Click(object sender, RoutedEventArgs e)
        {
            CurrentExpression     = SymbolicVariable.Parse(ExpressionTextBox.Text);
            ExpressionResult.Text = CurrentExpression.ToString();

            D3Chart.Title = CurrentExpression.ToString();

            ParametersPanel.Children.Clear();
            // create dynamic sliders for the expression.
            foreach (var p in CurrentExpression.InvolvedSymbols)
            {
                Slider s = new Slider()
                {
                    Name = p + "Slider", Tag = p
                };
                s.Minimum = double.Parse(MinimumTextBox.Text);
                s.Maximum = double.Parse(MaximumTextBox.Text);
                s.Value   = s.Minimum;

                s.ValueChanged += new RoutedPropertyChangedEventHandler <double>(Sliders_ValueChanged);

                ParametersPanel.Children.Add(new TextBlock {
                    Text = p
                });
                ParametersPanel.Children.Add(s);
            }


            lgraph.Points.Clear();
        }
        /// <summary>
        /// Returns the function body as symbolic quantity scalar.
        /// </summary>
        public QsScalar ToSymbolicScalar()
        {
            //return (QsScalar)QsEvaluator.CurrentEvaluator.SilentEvaluate(SymbolicBodyText);
            var fv = SymbolicVariable.Parse(this.FunctionBody);

            return(new QsScalar(ScalarTypes.SymbolicQuantity)
            {
                SymbolicQuantity = fv.ToQuantity()
            });
        }
示例#3
0
        public void SolveTest()
        {
            var ss = SymbolicVariable.Parse("3*x-4");

            Assert.AreEqual(4.0 / 3, ss.Solve());

            var r = SymbolicVariable.Parse("5-3*x");

            Assert.AreEqual(5.0 / 3.0, r.Solve());
        }
        /// <summary>
        /// Differentiate operation for function.
        /// </summary>
        /// <param name="value">object of <see cref="QsScalar"/> that hold <see cref="AnyQuantity&lt;SymbolicVariable&gt;"/></param>
        /// <returns></returns>
        public override QsValue DifferentiateOperation(QsValue value)
        {
            QsScalar sval = (QsScalar)value;

            if (sval.ScalarType == ScalarTypes.SymbolicQuantity)
            {
                var dsv = sval.SymbolicQuantity.Value;

                string fname         = "_";
                string WholeFunction = string.Empty;
                if (this.FunctionBodyToken[0].TokenClassType == typeof(CurlyBracketGroupToken))
                {
                    // vector differentiation
                    // take every term in the vector and differentiate it
                    var           vcs = QsVar.VectorComponents(this.FunctionBodyToken[0]);
                    StringBuilder sc  = new StringBuilder();
                    sc.Append(fname + "(" + RemoveRedundantParameters(this.ParametersNames) + ") = ");
                    sc.Append("{ ");
                    foreach (var c in vcs)
                    {
                        SymbolicVariable nsv = SymbolicVariable.Parse(c);
                        int times            = (int)dsv.SymbolPower;
                        while (times > 0)
                        {
                            nsv = nsv.Differentiate(dsv.Symbol);
                            times--;
                        }
                        sc.Append(nsv.ToString());
                        sc.Append(" ");
                    }
                    sc.Append("}");

                    WholeFunction = sc.ToString();
                }
                else
                {
                    SymbolicVariable nsv = ToSymbolicVariable();
                    int times            = (int)dsv.SymbolPower;
                    while (times > 0)
                    {
                        nsv = nsv.Differentiate(dsv.Symbol);
                        times--;
                    }

                    WholeFunction = fname + "(" + RemoveRedundantParameters(this.ParametersNames) + ") = " + nsv.ToString();
                }


                return(QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, WholeFunction));
            }
            else
            {
                return(base.DifferentiateOperation(value));
            }
        }
示例#5
0
        public void SolveVariableTest()
        {
            var h  = SymbolicVariable.Parse("2*x+4*y-4");
            var hs = h.Solve("x");

            Assert.AreEqual("-2*y+2", hs.ToString());

            var v = SymbolicVariable.Parse("5*u-4/r+3*h-2*x");

            Assert.AreEqual("2.5*u-2/r+1.5*h", v.Solve("x").ToString());


            Assert.AreEqual("-1.66666666666667*u+1.33333333333333/r+0.666666666666667*x", v.Solve("h").ToString());
        }
        public AnyQuantity <SymbolicVariable> ToSymbolicQuantity()
        {
            var fv = SymbolicVariable.Parse(this.FunctionBody);

            return(fv.ToQuantity());
        }
 public SymbolicVariable ToSymbolicVariable()
 {
     return(SymbolicVariable.Parse(this.FunctionBody));
 }
        /// <summary>
        /// Take the value and operation in text and return the current function operationed by value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        private QsFunction FOperation(QsValue value, string operation)
        {
            if (value is QsFunction)
            {
                QsFunction fn2 = (QsFunction)value;

                string fParameters = RemoveRedundantParameters(this.ParametersNames.Union(fn2.ParametersNames).ToArray());

                string thisFunctionBody = this.FunctionBody;
                foreach (string p in this.ParametersNames)
                {
                    thisFunctionBody = thisFunctionBody.Replace(p, "$" + p);
                }

                string targetFunctionBody = fn2.FunctionBody;
                foreach (string p in fn2.ParametersNames)
                {
                    targetFunctionBody = targetFunctionBody.Replace(p, "$" + p);
                }

                // form the expressions that will be parsed.
                string fpt = "(" + thisFunctionBody + ")" + operation + "(" + targetFunctionBody + ")";

                fpt = fpt.Replace("!", "__FAC__"); // replacing the ! sign with __FAC__ to include the '!' sign into the calculations {because '!' is operator in parsing so it doesn't enter the algebraic calculations}
                //evaulate fpt

                try
                {
                    QsScalar sc            = (QsScalar)QsEvaluator.CurrentEvaluator.SilentEvaluate(fpt);
                    string   FuncBody      = sc.SymbolicQuantity.Value.ToString().Replace("__FAC__", "!");
                    string   WholeFunction = "_(" + fParameters + ") = " + FuncBody;
                    return(QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, WholeFunction));
                }
                catch (QsIncompleteExpression)
                {
                    // something happened make the operation in old fashion
                    string WholeFunction;
                    Token  FunctionToken = JoinFunctionsArrayTokensWithOperation(operation, out WholeFunction, this, fn2);
                    return(QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, WholeFunction, FunctionToken));
                }
            }
            else if (value is QsScalar)
            {
                QsScalar svl = (QsScalar)value;

                var fname = "_";

                var fbody = this.FunctionBody;
                if (svl.ScalarType == ScalarTypes.SymbolicQuantity)
                {
                    fbody = "(" + fbody + ")" + operation + "(" + svl.SymbolicQuantity.Value.ToString() + ")";
                }
                else if (svl.ScalarType == ScalarTypes.NumericalQuantity)
                {
                    fbody = "(" + fbody + ")" + operation + svl.NumericalQuantity.Value.ToString();
                }
                else if (svl.ScalarType == ScalarTypes.RationalNumberQuantity)
                {
                    fbody = "(" + fbody + ")" + operation + svl.RationalQuantity.Value.Value.ToString();
                }
                else if (svl.ScalarType == ScalarTypes.FunctionQuantity)
                {
                    fbody = "(" + fbody + ")" + operation + "(" + svl.FunctionQuantity.Value.FunctionBody + ")";
                }
                else
                {
                    throw new QsException("Operation '" + operation + "' for the target scalar type (" + svl.ScalarType + ") is not supported");
                }


                QsScalar fb = SymbolicVariable.Parse(fbody).ToQuantity().ToScalar();

                string FuncBody = string.Empty;
                if (fb.ScalarType == ScalarTypes.SymbolicQuantity)
                {
                    FuncBody = fb.SymbolicQuantity.Value.ToString().Replace("__FAC__", "!");
                }
                else
                {
                    FuncBody = fb.ToExpressionParsableString();
                }

                string[] functionParametersArray = this.ParametersNames; // this is the available parameters for the original function.

                if (svl.ScalarType == ScalarTypes.SymbolicQuantity)
                {
                    List <string> newParametersList = new List <string>(functionParametersArray);

                    newParametersList.AddRange(svl.SymbolicQuantity.Value.InvolvedSymbols);

                    functionParametersArray = newParametersList.ToArray();
                }

                if (svl.ScalarType == ScalarTypes.FunctionQuantity)
                {
                    List <string> newParametersList = new List <string>(functionParametersArray);

                    newParametersList.AddRange(svl.FunctionQuantity.Value.ParametersNames);

                    functionParametersArray = newParametersList.ToArray();
                }

                var f = fname + "(" + RemoveRedundantParameters(functionParametersArray) + ") = " + FuncBody;
                return(QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, f));
            }

            else
            {
                throw new NotImplementedException();
            }
        }