コード例 #1
0
        public void Simplify_AddingLeftNestedIdentifiersSwapped_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            environment.AddSymbol("y");
            SyntaxNode root = GetExpression("y + x + x", environment);

            root = LeftNestedAlgebraicSimplifier.Simplify((OperatorNode)root, environment);

            Assert.AreEqual("2 * x + y", root.ToString());
        }
コード例 #2
0
        public void AddSymbol_AlreadyDefinedSymbol_ThrowsException()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");

            DefinedSymbolException exception = Assert.Throws <DefinedSymbolException>(() => {
                environment.AddSymbol("x");
            });

            Assert.AreEqual("Symbol x has already been defined", exception.Message);
        }
        public void Simplify_AddingTwoDifferentVariables_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            environment.AddSymbol("y");
            SyntaxNode node = GetExpression("x + y", environment);

            node = AlgebraicIdentitySimplifier.Simplify((OperatorNode)node, environment);

            Assert.AreEqual("x + y", node.ToString());
        }
コード例 #4
0
        public void Analyze_RecognizeShorthandMultiplicationInsideIdentifer_ReturnsOperatorNode()
        {
            SyntaxNode  root        = new IdentifierNode("xy");
            Environment environment = new Environment();

            environment.AddSymbol("x");
            environment.AddSymbol("y");

            root = SemanticAnalyzer.Analyze(root, environment);

            Assert.AreEqual("x * y", root.ToString());
        }
コード例 #5
0
        public void ToString_DisplaysFunctionMultipleArguments_ReturnsString()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            environment.AddSymbol("y");
            Expression expression = new Expression(Parser.ParseExpression("1/2x+2y", environment), environment);
            Function   f          = new Function("f", new HashSet <string> {
                "x", "y"
            }, expression);

            Assert.AreEqual(f.ToString(), "f(x, y) = 1 * 2 ^ -1 * x + 2 * y");
        }
コード例 #6
0
        public void HasVariable_SymbolDoesNotExists_ReturnsFalse()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");

            Assert.True(environment.HasVariable("x"));
        }
コード例 #7
0
        public void Simplify_Subtract0_X()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            ConstantSimplifier simplifier = new ConstantSimplifier(environment);
            SyntaxNode         node       = Parser.ParseExpression("x - 0", environment);

            Assert.AreEqual("x", simplifier.Simplify(node).ToString());
        }
コード例 #8
0
        public void GetVariable_Symbol_ReturnsEnvironmentVariable()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");

            EnvironmentVariable variable = environment.Get("x");

            Assert.AreEqual(EnvironmentVariableType.Symbol, variable.Type);
        }
コード例 #9
0
        public void Analyze_AmbiguousVariable_ThrowsException()
        {
            IdentifierNode symbol      = new IdentifierNode("xyz");
            Environment    environment = new Environment();

            environment.AddSymbol("x");
            environment.AddSymbol("y");
            environment.AddSymbol("z");
            environment.AddSymbol("xy");
            environment.AddSymbol("yz");

            AmbiguousIdentifierException exception = Assert.Throws <AmbiguousIdentifierException>(() =>
            {
                SemanticAnalyzer.Analyze(symbol, environment);
            });

            Assert.NotNull(exception);
            Assert.AreEqual("Ambiguous identifier 'xyz' can be made from 'x, y, z', 'x, yz', 'xy, z'", exception.Message);
        }
コード例 #10
0
        public void Simplify_MultiplyBy1ManyTimes_X()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            ConstantSimplifier simplifier = new ConstantSimplifier(environment);
            SyntaxNode         node       = Parser.ParseExpression("x * 1 * 1 * 1", environment);

            Assert.AreEqual("x", simplifier.Simplify(node).ToString());
        }
コード例 #11
0
        public void Simplify_ComplexAlgebraExpression_SimplifiesDivision()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            ConstantSimplifier simplifier = new ConstantSimplifier(environment);
            SyntaxNode         node       = Parser.ParseExpression("3 * x / 3 + 12", environment);

            Assert.AreEqual("x + 12", simplifier.Simplify(node).ToString());
        }
コード例 #12
0
        public void Simplify_ComplexAlgebraicAddition3_ReturnsExpression()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode root = GetExpression("x + 3 * x", environment);

            root = RightNestedAlgebraicSimplifier.Simplify((OperatorNode)root, environment);

            Assert.AreEqual("4 * x", root.ToString());
        }
コード例 #13
0
        public void Simplify_ExponentiationTwoOfTheSameVariables_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode node = GetExpression("x ^ x", environment);

            node = AlgebraicIdentitySimplifier.Simplify((OperatorNode)node, environment);

            Assert.AreEqual("x ^ x", node.ToString());
        }
コード例 #14
0
        public void Simplify_NestedAddition_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("j");
            SyntaxNode root = GetExpression("6 * j + 9 * j", environment);

            root = NestedAlgebraicSimplifier.Simplify((OperatorNode)root, environment);

            Assert.AreEqual("15 * j", root.ToString());
        }
コード例 #15
0
        public void Simplify_AddingLeftNestedReverseTerm_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode root = GetExpression("x * 2 + x", environment);

            root = LeftNestedAlgebraicSimplifier.Simplify((OperatorNode)root, environment);

            Assert.AreEqual("3 * x", root.ToString());
        }
コード例 #16
0
        public void Analyze_VariableSymbol_ReturnsIdentifierNode()
        {
            SyntaxNode  node        = new IdentifierNode("x");
            Environment environment = new Environment();

            environment.AddSymbol("x");

            node = SemanticAnalyzer.Analyze(node, environment);

            Assert.AreEqual("x", node.ToString());
        }
コード例 #17
0
        public void Analyze_RecognizeShorthandMultiplicationInsideIdentifierWithPredefinedSymbols_ReturnsIdentifierNode()
        {
            SyntaxNode  root        = new IdentifierNode("xe");
            Environment environment = new Environment();

            environment.AddSymbol("x");

            root = SemanticAnalyzer.Analyze(root, environment);

            Assert.AreEqual("x * e", root.ToString());
        }
コード例 #18
0
        public void Analyze_Parentheses_ReturnsParenthesesNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode root = new ParenthesesNode(new IdentifierNode("x"));

            root = SemanticAnalyzer.Analyze(root, environment);

            Assert.AreEqual("(x)", root.ToString());
        }
コード例 #19
0
        public void Derive_x_Returns1()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode node       = Parser.ParseExpression("x", environment);
            Expression expression = new Expression(node, environment);
            Derivative derivative = new Derivative(expression, "x");

            Assert.AreEqual(derivative.Derive().ToString(), "1");
        }
コード例 #20
0
        public void Simplify_NestedAlgebraicOperator_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode node = GetExpression("3 * x + 2 * x", environment);

            node = OperatorSimplification.Simplify((OperatorNode)node, environment);

            Assert.NotNull(node);
            Assert.AreEqual("5 * x", node.ToString());
        }
コード例 #21
0
        public void ParseExpression_KeywordExpression_ReturnsString()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");

            TestExpression(
                "ln(x^2 + x - 3)",
                "ln(x ^ 2 + x + -3)",
                environment
                );
        }
コード例 #22
0
        public void Simplify_OperatorChildrenAreBothTheSameIdentifier_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            SyntaxNode node = GetExpression("x + x", environment);

            node = OperatorSimplification.Simplify((OperatorNode)node, environment);

            Assert.NotNull(node);
            Assert.AreEqual("2 * x", node.ToString());
        }
コード例 #23
0
        public void ToString_DisplaysFunction_ReturnsCachedString()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            Expression expression = new Expression(Parser.ParseExpression("1/2x+2", environment), environment);
            Function   f          = new Function("f", new HashSet <string> {
                "x"
            }, expression);

            Assert.AreEqual(f.ToString(), "f(x) = 1 * 2 ^ -1 * x + 2");
            Assert.AreEqual(f.ToString(), "f(x) = 1 * 2 ^ -1 * x + 2");
        }
コード例 #24
0
        public void Simplify_OperatorWithNumberAndFunction_ReturnsOperatorNode()
        {
            Environment environment = new Environment();

            environment.AddSymbol("x");
            environment.AddFunction("f", GetExpression("x", environment));
            SyntaxNode node = GetExpression("2 + f(x)", environment);

            node = OperatorSimplification.Simplify((OperatorNode)node, environment);

            Assert.NotNull(node);
            Assert.AreEqual("2 + f(x)", node.ToString());
        }
コード例 #25
0
        public void Analyze_AmbigiousSymbolDistribution_ReturnsOperatorNode()
        {
            IdentifierNode left  = new IdentifierNode("x");
            SyntaxNode     right = new NumberNode(1);
            SyntaxNode     root  = new FunctionOrDistributionNode(left, right);

            Environment environment = new Environment();

            environment.AddSymbol("x");

            root = SemanticAnalyzer.Analyze(root, environment);

            Assert.AreEqual("x * 1", root.ToString());
        }