Exemplo n.º 1
0
        public void Test3()
        {
            var x    = MathS.Var("x");
            var expr = MathS.Sin(x);

            Assert.IsTrue(expr.DefiniteIntegral(x, 0, 3).Real > 1.5);
        }
Exemplo n.º 2
0
        public void Test3()
        {
            var x    = MathS.Var("x");
            var expr = x;

            Assert.IsTrue(expr.Substitute(x, 0) == 0);
        }
Exemplo n.º 3
0
        public void Test1()
        {
            var x    = MathS.Var("x");
            var expr = x;

            Assert.IsTrue(Number.Abs(expr.DefiniteIntegral(x, 0, 1).Real - 1.0 / 2) < 0.1);
        }
Exemplo n.º 4
0
        public void Test2()
        {
            var x    = MathS.Var("x");
            var expr = MathS.Sin(x);

            Assert.IsTrue(expr.DefiniteIntegral(x, -1, 1) == 0);
        }
Exemplo n.º 5
0
        public void TestPatt2()
        {
            var y    = MathS.Var("y");
            var expr = (MathS.Sqr(MathS.Sin(x + 2 * y)) + MathS.Sqr(MathS.Cos(x + 2 * y))) / (2 * MathS.Sin(x - y) * MathS.Cos(x - y) + 1);

            Assert.IsTrue(expr.Simplify() == 1 / (MathS.Sin(2 * (x - y)) + 1));
        }
Exemplo n.º 6
0
        public void TestPatt3()
        {
            var y    = MathS.Var("y");
            var expr = (x - y) * (x + y);

            Assert.IsTrue(expr.Simplify() == MathS.Sqr(x) - MathS.Sqr(y));
        }
Exemplo n.º 7
0
        public void TestPatt4()
        {
            var y    = MathS.Var("y");
            var expr = (x - y) * (x + y) / (x * x - y * y);

            Assert.IsTrue(expr.Simplify() == 1);
        }
Exemplo n.º 8
0
        public void Test1()
        {
            var x    = MathS.Var("x");
            var expr = x * x + MathS.Sin(x) * 0;

            Assert.IsTrue(expr.Substitute(x, 0).Simplify() == 0);
        }
Exemplo n.º 9
0
        public CompiledFuncTest()
        {
            x = MathS.Var("x");
            multiFuncNotCompiled = (MathS.Log(3, x) + MathS.Sqr(x)) * MathS.Sin(x + MathS.Cosec(x));
            multiFunc            = multiFuncNotCompiled.Compile(x);
            Expression <Func <Complex, Complex> > expr = x => (Complex.Log(x, 3) + Complex.Pow(x, 2)) * Complex.Sin(x + 1 / Complex.Sin(x));

            linqFunc = expr.Compile();
        }
Exemplo n.º 10
0
        public void TestPatt12()
        {
            var y    = MathS.Var("y");
            var a    = MathS.Var("a");
            var b    = MathS.Var("b");
            var c    = MathS.Var("c");
            var expr = (x * y * a * b * c) / (c * b * a * x * x);

            Assert.IsTrue(expr.Simplify(4) == y / x);
        }
Exemplo n.º 11
0
        public void Test2()
        {
            var x        = MathS.Var("x");
            var y        = MathS.Var("y");
            var expr     = x.Pow(x) - MathS.Sqrt(x - 3) / x + MathS.Sin(x);
            var expected = (3 * y).Pow(3 * y) - MathS.Sqrt(3 * y - 3) / (3 * y) + MathS.Sin(3 * y);
            var actual   = expr.Substitute(x, 3 * y);

            Assert.IsTrue(expected == actual);
        }
Exemplo n.º 12
0
        private void EveryFrame(object sender, EventArgs e)
        {
            var B         = MathS.Var("B");
            var expr2     = B * MathS.Sin(t + B) * MathS.Pow(MathS.e, MathS.i * B * MathS.Cos(t));
            var niceFunc2 = expr2.Compile(B);

            plotter.Clear();
            plotter.PlotIterativeComplex(niceFunc2, 0, t);
            plotter.Render();
            t += 0.0005m;
        }
Exemplo n.º 13
0
        public void TestVars4mp()
        {
            var goose = MathS.Var("goose");
            var eq    = ((x - goose) * (x - 3) * (MathS.Sqr(x) - 4));
            var roots = eq.SolveEquation(x);

            foreach (var root in roots.FiniteSet())
            {
                AssertRoots(eq, x, root);
            }
        }
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         OutputTextbox.Text = MathS.FromString(InputTextbox.Text).Derive(MathS.Var("x")).Simplify().ToString();
     }
     catch (Exception ex)
     {
         OutputTextbox.Text = ex.Message;
     }
 }
Exemplo n.º 15
0
        public void InvertedFunctionTests(string func, int rootAmount)
        {
            Entity toRepl = func + "(x2 + 3)";
            Entity expr   = MathS.Sqr(toRepl) + 0.3 * toRepl - 0.1 * MathS.Var("a");
            var    roots  = expr.SolveEquation(x);

            AssertRootCount(roots, rootAmount);
            foreach (var root in roots.FiniteSet())
            {
                AssertRoots(expr.Substitute("a", 5), x, root.Substitute("n_1", 3).Substitute("a", 5));
            }
        }
Exemplo n.º 16
0
        public void TestVars2_0()
        {
            var goose = MathS.Var("goose");
            var momo  = MathS.Var("momo");
            var eq    = ((x - momo) * (x - goose)).Expand();
            var roots = eq.SolveEquation(x);

            foreach (var root in roots.FiniteSet())
            {
                AssertRoots(eq, x, root);
            }
        }
Exemplo n.º 17
0
        public void InvertedFunctions(string func, int rootAmount)
        {
            Entity toRepl = func + "(x2 + 3)";
            Entity expr   = MathS.Sqr(toRepl) + 0.3 * toRepl - 0.1 * MathS.Var("a");
            var    roots  = expr.SolveEquation(x);

            roots = (Set)roots.InnerSimplified;
            var finite = Assert.IsType <FiniteSet>(roots);

            AssertRootCount(finite, rootAmount);
            foreach (var root in finite)
            {
                AssertRoots(expr.Substitute("a", 5), x, root.Substitute("n_1", 3).Substitute("a", 5));
            }
        }
Exemplo n.º 18
0
        public void TestVars4()
        {
            var goose = MathS.Var("goose");
            var momo  = MathS.Var("momo");
            var quack = MathS.Var("quack");
            var eq    = ((x - goose) * (x - momo) * (x - quack) * (x - momo * goose * quack)).Expand();
            var roots = eq.SolveEquation(x);

            Assert.IsNotNull(roots, "roots is null");
            AssertRootCount(roots, 4);
            foreach (var root in roots.FiniteSet())
            {
                AssertRoots(eq, x, root);
            }
        }
 private void button5_Click(object sender, EventArgs e)
 {
     try
     {
         var roots = MathS.FromString(InputTextbox.Text).SolveNt(MathS.Var("x"));
         OutputTextbox.Text = "";
         foreach (var root in roots)
         {
             OutputTextbox.Text += root.ToString() + "  ";
         }
     }
     catch (Exception ex)
     {
         OutputTextbox.Text = ex.Message;
     }
 }
Exemplo n.º 20
0
 [TestMethod] public void SubscriptSingle() => Test(@"c_{e}", MathS.Var("c_e"));
Exemplo n.º 21
0
 [TestMethod] public void Greek() => Test(@"\alpha", MathS.Var("alpha"));
        /// <summary>
        /// Returns information about all monomials of an expression
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="replaceVars"></param>
        /// <returns></returns>
        internal static PolyInfo GatherAllPossiblePolynomials(Entity expr, bool replaceVars)
        {
            // TODO: refactor

            expr = expr.DeepCopy();

            // Init
            var res = new PolyInfo();
            var mentionedVarList = MathS.Utils.GetUniqueVariables(expr);
            var newList          = new List <string>();

            if (replaceVars)
            {
                // Replace all variables we can
                foreach (var varMentioned in mentionedVarList.FiniteSet())
                {
                    if (expr.FindSubtree(varMentioned) == null)
                    {
                        continue;
                    }
                    var replacement = TreeAnalyzer.GetMinimumSubtree(expr, varMentioned);
                    res.replacementInfo[varMentioned.Name] = replacement;
                    FindAndReplace(ref expr, replacement, new VariableEntity(PolyInfo.NewVarName(varMentioned.Name)));
                    newList.Add(PolyInfo.NewVarName(varMentioned.Name));
                }
            }
            else
            {
                foreach (var v in mentionedVarList.FiniteSet())
                {
                    newList.Add(v.Name);
                }
            }

            // Gather info about each var as if this var was the only argument of the polynomial P(x)
            foreach (var varMentioned in newList)
            {
                List <Entity> children;
                if (expr.entType == Entity.EntType.OPERATOR && expr.Name == "sumf" || expr.Name == "minusf")
                {
                    children = TreeAnalyzer.LinearChildren(expr, "sumf", "minusf", Const.FuncIfSum);
                }
                else
                {
                    children = new List <Entity> {
                        expr
                    }
                };
                res.monoInfo[varMentioned] = PolynomialSolver.GatherMonomialInformation <decimal>(children, MathS.Var(varMentioned));
            }

            return(res);
        }
    }
 [Fact] public void TestIneq() => Assert.NotEqual(MathS.Var("x"), MathS.Var("y"));
 [Fact] public void TestEq() => Assert.Equal(MathS.Var("x"), MathS.Var("x"));
Exemplo n.º 25
0
 [Fact] public void Greek() => Test(@"\alpha", MathS.Var("alpha"));
Exemplo n.º 26
0
 public void TestEq()
 {
     Assert.IsTrue(MathS.Var("x") == MathS.Var("x"));
 }
Exemplo n.º 27
0
        public CalcResult Compute()
        {
            CalcResult res = new CalcResult();

            try
            {
                // INITIALIZATION OF STUFF
                Lexer         lexer         = new Lexer(rawExpression);
                TokenList     tokenList     = lexer.GenerateTokens();
                Parser        parser        = new Parser(tokenList);
                PostTokenList postTokenList = parser.Parse();
                postTokenList.Freeze();
                MathPreprocessor mathPreprocessor = new MathPreprocessor(postTokenList);
                mathPreprocessor.Process();
                MajorSegmentList         majorSegmentList   = mathPreprocessor.GetSegments();
                List <ExpressionSegment> expressionSegments = new List <ExpressionSegment>();
                expressionSegments.Add(
                    (ExpressionSegment)majorSegmentList.Select("expression").Item()
                    );

                // PROCESSING SOME `WHERE` AND `FOR` TOKENS
                for (int i = majorSegmentList.Count - 1; i >= 0; i--)
                {
                    if (majorSegmentList[i] is ExpressionSegment)
                    {
                        break;
                    }
                    if (majorSegmentList[i] is EqKeyword
                        &&
                        (((EqKeyword)majorSegmentList[i]).Type == Keyword.Type.WHERE ||
                         ((EqKeyword)majorSegmentList[i]).Type == Keyword.Type.FOR)
                        &&
                        FixedKeyword.IsVariable(((EqKeyword)majorSegmentList[i]).GetBeforeEq())
                        &&
                        (CustomData.GetType(((EqKeyword)majorSegmentList[i]).GetAfterEq()) == CustomData.Type.FIXED))
                    {
                        string v    = ((EqKeyword)majorSegmentList[i]).GetBeforeEq();
                        string expr = ((EqKeyword)majorSegmentList[i]).GetAfterEq();
                        expressionSegments[0].Substitute(v, expr);
                    }
                    else
                    {
                        majorSegmentList[i].Ignored = true;
                    }
                }

                MajorSegmentList newExpression = majorSegmentList.CutTill(expressionSegments[0]);
                res.InterpretedAs.Add(expressionSegments[0].Build());
                List <MajorSegmentList> list = DivideByField(newExpression);
                var simplOpt = new MajorSegmentList();
                simplOpt.Add(new FieldSegment("simplify", "", Field.Type.SIMPLIFICATION));
                list.Insert(0, simplOpt);

                // SEQUENTIAL PROCESSING
                list.Reverse();
                foreach (var fieldOpts in list)
                {
                    bool isLast = fieldOpts == list[list.Count - 1];

                    // PROCESSING SOME `WHERE` AND `FOR` TOKENS
                    foreach (var expressionSegment in expressionSegments)
                    {
                        for (int i = 1; i < fieldOpts.Count; i++)
                        {
                            if (fieldOpts[i] is EqKeyword &&
                                ((fieldOpts[i] as EqKeyword).Type == Keyword.Type.WHERE ||
                                 (fieldOpts[i] as EqKeyword).Type == Keyword.Type.FOR) &&
                                CustomData.GetType((fieldOpts[i] as EqKeyword).GetAfterEq()) == CustomData.Type.FIXED &&
                                CustomData.GetType((fieldOpts[i] as EqKeyword).GetBeforeEq()) == CustomData.Type.FIXED &&
                                FixedKeyword.IsVariable((fieldOpts[i] as EqKeyword).GetBeforeEq()))
                            {
                                expressionSegment.Substitute((fieldOpts[i] as EqKeyword).GetBeforeEq(), (fieldOpts[i] as EqKeyword).GetAfterEq());
                            }
                        }
                        res.InterpretedAs.Add(fieldOpts.Build() + " " + expressionSegment.Build());
                    }

                    var fieldType = FindField(fieldOpts[0].Keyname);

                    List <string> newExprs      = new List <string>();
                    List <string> newLatexExprs = new List <string>();
                    bool          quickExit     = false;
                    foreach (var expressionSegment in expressionSegments)
                    {
                        if (fieldType == FieldType.BOOL)
                        {
                            var vars = expressionSegment.tokens.ExtractVariables();
                            vars = Functions.MakeUnique(vars);
                            foreach (var v in vars)
                            {
                                if (v.Length > 1)
                                {
                                    throw new ParsingException("Multi-symbol vars are forbidden in boolean mode");
                                }
                            }
                            var    be = new BoolEng(expressionSegment.Build());
                            string newExpr;
                            newExpr = be.CompileTable();
                            string line = "{";
                            foreach (var onevar in vars)
                            {
                                line += ": " + onevar + " %";
                            }
                            line   += ": F %}\n";
                            newExpr = line + newExpr;
                            newExprs.Add(newExpr);
                            {
                                newExpr = newExpr.Replace(":", "<th>");
                                newExpr = newExpr.Replace("%", "</th>");
                                newExpr = newExpr.Replace("{", "<tr>");
                                newExpr = newExpr.Replace("}", "</tr>");
                                newExpr = newExpr.Replace("[", "<td class=\"cellbool-res\">");
                                newExpr = newExpr.Replace("]", "</td>");
                                newExpr = newExpr.Replace("\n", "");
                                newExpr = "<table id=\"bool-res\">" + newExpr + "</table>";
                            }
                            newLatexExprs.Add(newExpr);
                            quickExit = true;
                            break;
                        }
                        else if (AscCalc.MathAnalysis.Contains(fieldType))
                        {
                            expressionSegment.tokens.AddOmittedOps();
                            var    vars = expressionSegment.tokens.ExtractVariables();
                            string diffVar;
                            vars = Functions.MakeUnique(vars);
                            if (fieldOpts.Select(Names.FOR).Count == 0)
                            {
                                if (vars.Contains("x") || vars.Count == 0)
                                {
                                    diffVar = "x";
                                }
                                else if (vars.Contains("y"))
                                {
                                    diffVar = "y";
                                }
                                else
                                {
                                    diffVar = vars[0];
                                }
                                res.InterpretedAs.Add("Interpreted as `" + Names.FOR + " " + diffVar + "`");
                            }
                            else if (fieldOpts.Select(Names.FOR).Count == 1)
                            {
                                diffVar = (fieldOpts.Select(Names.FOR).Item() as FixedKeyword).GetVariable();
                            }
                            else
                            {
                                diffVar = (fieldOpts.Select(Names.FOR)[0] as FixedKeyword).GetVariable();
                            }
                            vars.Add(diffVar);
                            vars = Functions.MakeUnique(vars);
                            string req = expressionSegment.Build();
                            if (fieldType == FieldType.DERIVATIVE)
                            {
                                newExprs.Add(MathS.FromString(req).Derive(MathS.Var(diffVar)).ToString());
                                if (isLast && latex)
                                {
                                    newLatexExprs.Add("$$" + MathS.FromString(req).Derive(MathS.Var(diffVar)).Latexise().ToString() + "$$");
                                }
                            }
                            else if (fieldType == FieldType.SOLVE)
                            {
                                expressionSegment.tokens.AddOmittedOps();
                                if (vars.Count > 1)
                                {
                                    throw new InvalidRequestException();
                                }
                                var roots = MathS.FromString(req).SolveNt(MathS.Var(diffVar), precision: 400);
                                foreach (var root in roots)
                                {
                                    newExprs.Add(root.ToString());
                                }
                                if (isLast && latex)
                                {
                                    foreach (var root in roots)
                                    {
                                        newExprs.Add("$$" + root.ToString() + "$$");
                                    }
                                }
                            }
                        }

                        else if (fieldType == FieldType.SIMPLIFICATION)
                        {
                            expressionSegment.tokens.AddOmittedOps();
                            var vars = expressionSegment.tokens.ExtractVariables();
                            vars = Functions.MakeUnique(vars);
                            string req = expressionSegment.Build();
                            string newExpr;
                            newExpr = MathS.FromString(req).SimplifyIntelli().ToString();
                            newExprs.Add(newExpr);
                            if (isLast && latex)
                            {
                                newExpr = MathS.FromString(req).SimplifyIntelli().Latexise();
                                newLatexExprs.Add("$$" + newExpr + "$$");
                            }
                        }
                    }
                    if (newExprs.Count == 0)
                    {
                        res.Result = "No answer";
                        break;
                    }
                    if (!isLast && !quickExit)
                    {
                        expressionSegments = new List <ExpressionSegment>();
                        foreach (var nexp in newExprs)
                        {
                            expressionSegments.Add(new ExpressionSegment("expression", nexp));
                        }
                    }
                    else
                    {
                        res.Result = "";
                        foreach (var segm in newExprs)
                        {
                            res.Result += segm + " ";
                        }
                        res.LatexResult = "";
                        foreach (var segm in newLatexExprs)
                        {
                            res.LatexResult += segm + " ";
                        }
                        break; // For quickExit case
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(res);
        }
Exemplo n.º 28
0
 [TestMethod] public void SubscriptDuo() => Test(@"\Delta_{a2}", MathS.Var("Delta_a2"));
Exemplo n.º 29
0
 public static IEnumerable <double> FindVerticalAsymptotes(string funcString, string variName)
 {
     return(FindVerticalAsymptotes(funcString, MathS.Var(variName)));
 }
Exemplo n.º 30
0
 public void TestIneq()
 {
     Assert.IsTrue(MathS.Var("x") != MathS.Var("y"));
 }