コード例 #1
0
ファイル: MultiplyTests.cs プロジェクト: QuantumCores/Mathema
        public void Multiply_Number_FlatAdd()
        {
            //Arrange
            var expected = new FlatAddExpression();

            expected.Add(new VariableExpression("u", 2));
            expected.Add(new NumberExpression(3));
            expected.Count = new Complex(12, 0);
            expected.Execute();

            var c = new NumberExpression(3);
            var n = new FlatAddExpression();

            n.Add(new VariableExpression("u", 2));
            n.Add(new NumberExpression(3));
            n.Count = new Complex(4, 0);
            var expr = new BinaryExpression(c, OperatorTypes.Multiply, n);

            //Act
            var actual = expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
            Assert.AreEqual(expected.Count.Im.ToNumber(), actual.Count.Im.ToNumber());
        }
コード例 #2
0
        public void Divide_x_by_y()
        {
            //Arrange
            var expr     = new BinaryExpression(new VariableExpression("x", 1), Mathema.Enums.Operators.OperatorTypes.Divide, new VariableExpression("y", 1));
            var expected = "x / y";

            //Act
            var res    = expr.Execute();
            var actual = res.DimensionKey.ToString();

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
ファイル: SubtractTests.cs プロジェクト: QuantumCores/Mathema
        public void Subtract_Complex_ResultAsNumber()
        {
            //Arrange
            var expected = new NumberExpression(1);
            var c        = new ComplexExpression(2, 3);
            var n        = new ComplexExpression(1, 3);
            var expr     = new BinaryExpression(c, OperatorTypes.Subtract, n);

            //Act
            var actual = (NumberExpression)expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
        }
コード例 #4
0
ファイル: PowerTests.cs プロジェクト: QuantumCores/Mathema
        public void Pow_Complex_0()
        {
            //Arrange
            var expected = new NumberExpression(1);
            var c        = new ComplexExpression(-2, 3);
            var n        = new NumberExpression(0);
            var expr     = new BinaryExpression(c, OperatorTypes.Power, n);

            //Act
            var actual = (NumberExpression)expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
        }
コード例 #5
0
ファイル: CloneTests.cs プロジェクト: QuantumCores/Mathema
        public void Clone_BinaryExpression()
        {
            //Arrange
            var expr1  = new NumberExpression(-2);
            var expr2  = new NumberExpression(3);
            var actual = new BinaryExpression(expr1, OperatorTypes.Power, expr2);

            //Act
            var expected = (BinaryExpression)actual.Clone();

            //Assert
            Assert.IsTrue(!Object.ReferenceEquals(actual, expected));
            Assert.AreEqual(actual.Execute().Count.Re.ToNumber(), expected.Execute().Count.Re.ToNumber());
        }
コード例 #6
0
ファイル: MultiplyTests.cs プロジェクト: QuantumCores/Mathema
        public void Multiply_Number_Function()
        {
            //Arrange
            var expected = new CotExpression(new VariableExpression("x", 1), 12);
            var c        = new NumberExpression(3);
            var n        = new CotExpression(new VariableExpression("x", 1), 4);
            var expr     = new BinaryExpression(c, OperatorTypes.Multiply, n);

            //Act
            var actual = expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
            Assert.AreEqual(expected.Count.Im.ToNumber(), actual.Count.Im.ToNumber());
        }
コード例 #7
0
ファイル: MultiplyTests.cs プロジェクト: QuantumCores/Mathema
        public void Multiply_Number_Complex()
        {
            //Arrange
            var expected = new ComplexExpression(2, -6);
            var c        = new NumberExpression(2);
            var n        = new ComplexExpression(1, -3);
            var expr     = new BinaryExpression(c, OperatorTypes.Multiply, n);

            //Act
            var actual = (ComplexExpression)expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
            Assert.AreEqual(expected.Count.Im.ToNumber(), actual.Count.Im.ToNumber());
        }
コード例 #8
0
ファイル: MultiplyTests.cs プロジェクト: QuantumCores/Mathema
        public void Multiply_Number_NegativeNumber()
        {
            //Arrange
            var expected = new NumberExpression(-4);
            var c        = new NumberExpression(-2);
            var n        = new NumberExpression(2);
            var expr     = new BinaryExpression(c, OperatorTypes.Multiply, n);

            //Act
            var actual = expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
            Assert.AreEqual(expected.Count.Im.ToNumber(), actual.Count.Im.ToNumber());
        }
コード例 #9
0
        public void Add_Complex_NegativeNumber()
        {
            //Arrange
            var expected = new ComplexExpression(1, 3);
            var c        = new ComplexExpression(2, 3);
            var n        = new NumberExpression(-1);
            var expr     = new BinaryExpression(c, OperatorTypes.Add, n);

            //Act
            var actual = (ComplexExpression)expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
            Assert.AreEqual(expected.Count.Im.ToNumber(), actual.Count.Im.ToNumber());
        }
コード例 #10
0
ファイル: DivideTests.cs プロジェクト: QuantumCores/Mathema
        public void Divide_Complex_Complex()
        {
            //Arrange
            var expected = new ComplexExpression(new Fraction(-7, 10), new Fraction(9, 10));
            var c        = new ComplexExpression(2, 3);
            var n        = new ComplexExpression(1, -3);
            var expr     = new BinaryExpression(c, OperatorTypes.Divide, n);

            //Act
            var actual = (ComplexExpression)expr.Execute();

            //Assert
            Assert.AreEqual(expected.Count.Re.ToNumber(), actual.Count.Re.ToNumber());
            Assert.AreEqual(expected.Count.Im.ToNumber(), actual.Count.Im.ToNumber());
        }
コード例 #11
0
        public IEquationSolutions Solve(IExpression expression, string variable, IClassificationResult classification)
        {
            var res = new EquationSolutions();

            if (expression is FlatAddExpression fa)
            {
                IExpression        a  = null;
                IExpression        b  = null;
                IExpression        c  = null;
                List <IExpression> ca = new List <IExpression>();

                for (int i = 0; i < fa.Expressions.Count; i++)
                {
                    var kv   = fa.Expressions.ElementAt(i);
                    var deg  = kv.Value[0].DimensionKey.Value;
                    var key  = kv.Value[0].DimensionKey.Key;
                    var expr = kv.Value[0];

                    if (deg.ToNumber() == 2 && key == variable)
                    {
                        if (expr is VariableExpression)
                        {
                            a = new ComplexExpression(expr.Count);
                        }
                        else if (expr is IFunctionExpression)
                        {
                            a = new ComplexExpression(expr.Count);
                        }
                        else
                        {
                            var tmp = (FlatExpression)expr.Clone();
                            tmp.Remove(variable, 2);
                            a = tmp;
                        }
                    }
                    else if (deg.ToNumber() == 1 && key == variable)
                    {
                        if (expr is VariableExpression)
                        {
                            b = new ComplexExpression(expr.Count);
                        }
                        else if (expr is IFunctionExpression)
                        {
                            b = new ComplexExpression(expr.Count);
                        }
                        else
                        {
                            var tmp = (FlatExpression)expr.Clone();
                            tmp.Remove(variable, 2);
                            b = tmp;
                        }
                    }
                    else
                    {
                        ca.Add(kv.Value[0]);
                    }
                }

                if (ca.Count > 1)
                {
                    c = new FlatAddExpression();
                    var ct = (FlatAddExpression)c;
                    foreach (var ci in ca)
                    {
                        ct.Add(ci);
                    }
                }
                else if (ca.Count == 1)
                {
                    c = ca[0];
                }

                if (a != null && c != null)
                {
                    var delta = this.Delta(a, b, c);
                    var ds    = new BinaryExpression(delta, Enums.Operators.OperatorTypes.Power, new NumberExpression(new Fraction(1, 2)));

                    var dsqrt = ds.Execute();

                    var r1 = X1X2(b, dsqrt, a, true);
                    var r2 = X1X2(b, dsqrt, a, false);

                    res.Solutions.Add(variable, new Tuple <IExpression, List <IExpression> >(classification.SearchResult[variable].Item1, new List <IExpression>()
                    {
                        r1.Execute()
                    }));
                    res.Solutions[variable].Item2.Add(r2.Execute());
                }
            }

            return(res);
        }