Exemple #1
0
        public void Base_ReturnsVariablesSortedByPowerDescendingAndNameAscending(
            string input, string expected)
        {
            var summand = Summand.Parse(input);

            summand.Base.Should().Be(expected);
        }
        public void SummandCreateTest()
        {
            ISummandBuilder builder = new SummandBuilder();

            string input = "5.811z^4y^3xa^0";

            var expectedSummand = new Summand
            {
                Multiplier = 5.811,
                Variables  = new List <Variable>
                {
                    new Variable {
                        Exponent = 1, Letter = 'x'
                    },
                    new Variable {
                        Exponent = 3, Letter = 'y'
                    },
                    new Variable {
                        Exponent = 4, Letter = 'z'
                    },
                }
            };


            var summand = builder.Create(input);

            Assert.IsNotNull(summand);
            Assert.IsTrue(expectedSummand.Equals(summand));
        }
        private static Summand ConvertToSummand(string stringToParse)
        {
            Summand summand = new Summand();

            summand.Coefficient      = FindCoefficient(stringToParse);
            summand.PolynomialDegree = FindPolynomialDegree(stringToParse);

            var elementaryFunctionList = FindElementaryFunction(stringToParse);

            foreach (var function in elementaryFunctionList)
            {
                summand.Multiplicands.Add(ConvertToElementaryFunction(function));
            }

            var sumsRaisedToPowerList = FindSumRaisedToPower(stringToParse);

            foreach (var sum in sumsRaisedToPowerList)
            {
                SumRaisedToPower sumRaisedToPower = new SumRaisedToPower();
                sumRaisedToPower.Degree = GetSumsRaisedToPowerDegree(sum);
                sumRaisedToPower.Sum    = Parse(EliminateDegree(sum));
                summand.SumsRaisedToPower.Add(sumRaisedToPower);
            }

            return(summand);
        }
Exemple #4
0
        public void EquationShouldBeTransformedToCanonicalForm()
        {
            // GIVEN
            IEquationFormer former = new EquationFormer();

            const string inputEquation = "x^2 + 3.5xy + y = y^2 - xy + y";

            var expectedLeftPart = new List <Summand>
            {
                new Summand(new[] { new Variable('x', degree: 2) }),
                new Summand(new[] { new Variable('y', degree: 2) }, coefficient: -1),
                new Summand(new[] { new Variable('x'), new Variable('y') }, coefficient: 4.5f)
            };
            var expectedRightPart = new List <Summand> {
                Summand.Empty()
            };

            // WHEN
            var resultEquation = former.GetCanonicalForm(inputEquation);

            // THEN
            Assert.IsNotNull(resultEquation);
            Assert.IsTrue(resultEquation.LeftPart.SequenceEqual(expectedLeftPart));
            Assert.IsTrue(resultEquation.RightPart.SequenceEqual(expectedRightPart));
        }
        public void EquationShouldBeGroupedToLeftPart()
        {
            // GIVEN
            IEquationGrouper grouper = new EquationGrouper();

            var inputEquation = CreateTestEquation();

            var expectedLeftPart = new List <Summand>(inputEquation.LeftPart)
            {
                new Summand(new[] { new Variable('y', degree: 2) }, coefficient: -1),
                new Summand(new[] { new Variable('x'), new Variable('y') }),
                new Summand(new[] { new Variable('y') }, coefficient: -1)
            };
            var expectedRightPart = new List <Summand> {
                Summand.Empty()
            };

            // WHEN
            var resultEquation = grouper.GroupToLeft(inputEquation);

            // THEN
            Assert.IsNotNull(resultEquation);
            Assert.IsTrue(resultEquation.LeftPart.SequenceEqual(expectedLeftPart));
            Assert.IsTrue(resultEquation.RightPart.SequenceEqual(expectedRightPart));
        }
Exemple #6
0
        public Summand Create(string input)
        {
            input = input.ToLower();

            var summand = new Summand();

            double multiplier     = 1;
            var    multiplierSign = FindSignMultiplier(ref input);

            var parts = Helper.SplitVariables(input);

            //смотрим, является ли первая часть коэффициентом
            var possibleMultiplier = parts.First();

            if (Helper.TryParseDouble(possibleMultiplier, out double result))
            {
                multiplier = result;
                parts.RemoveAt(0);
            }

            var variables = FindVariables(parts).ToList();

            summand.Multiplier = multiplier * multiplierSign;
            summand.Variables  = variables;

            return(summand);
        }
Exemple #7
0
        private static IEnumerable <Summand> ReplaceSummandWithExpansion(Summand summand, int maxTaylorDegree)
        {
            if (MathHelper.IsZero(summand.Coefficient))
            {
                return(new List <Summand>());
            }

            if (summand.Multiplicands.Count == 0)
            {
                return(new List <Summand> {
                    summand
                });
            }

            var returnedList = summand.Multiplicands[0].ToTaylorExpansion(maxTaylorDegree);

            for (var i = 1; i < summand.Multiplicands.Count; i++)
            {
                var nextExpansion = summand.Multiplicands[i].ToTaylorExpansion(maxTaylorDegree);
                returnedList = Distribute(returnedList, nextExpansion);
            }

            var multiplied =
                returnedList.Select(s =>
            {
                s.Coefficient                 *= summand.Coefficient;
                s.PolynomialDegree            += summand.PolynomialDegree * s.PolynomialDegreeDenominator;
                var gcd                        = MathHelper.GreatestCommonDivisor(s.PolynomialDegree, s.PolynomialDegreeDenominator);
                s.PolynomialDegree            /= gcd;
                s.PolynomialDegreeDenominator /= gcd;
                return(s);
            });

            return(multiplied.OrderBy(s => s.LittleODegree).ThenBy(s => s.PolynomialDegree / (double)s.PolynomialDegreeDenominator));
        }
Exemple #8
0
        /// <summary>
        /// Builds a <code>Summand</code> object with the information read from the equation string
        /// </summary>
        /// <param name="coefficient">Coefficient of the summand</param>
        /// <param name="exponential">Exponent of the summant</param>
        /// <param name="variable">Variable of the summand</param>
        /// <param name="op">Operation. Can be a sum (+) or subtration (-)</param>
        /// <param name="passEqual">Informs if this summand is on the right or left side of the equality</param>
        /// <returns></returns>
        private Summand BuildSummand(string coefficient, string exponential, string variable, char op, bool passEqual)
        {
            if (String.IsNullOrEmpty(coefficient))
            {
                coefficient = "1";
            }
            if (String.IsNullOrEmpty(exponential))
            {
                exponential = "1";
            }

            Summand summand = new Summand {
                coefficient = Double.Parse(coefficient, CultureInfo.InvariantCulture), exponential = int.Parse(exponential), variable = variable
            };

            if (MINUS_OPERATOR.Equals(op))
            {
                summand.coefficient = summand.coefficient * -1;
            }

            if (passEqual)
            {
                summand.coefficient = summand.coefficient * -1;
            }

            return(summand);
        }
Exemple #9
0
        public void ParseSimpleOperands(string equations, Summand answer)
        {
            var parser = new Parser(equations);

            var summand = parser.GetSummand().First();

            CompareSummand(answer, summand);
        }
 public void SummandsToStringWithisInEquationTests(
     Summand summand,
     string stringifiedSummand,
     string stringifiedWithIsInEquationSummand)
 {
     Assert.Equal(stringifiedSummand, summand.ToString());
     Assert.Equal(stringifiedWithIsInEquationSummand, summand.ToString(isInEquation: true));
 }
Exemple #11
0
    public Summand ExtractContent()
    {
        Summand summand = content;

        content = null;
        onBecomeEmpty?.Invoke(Index);
        return(summand);
    }
Exemple #12
0
        public void Summand_ValidInput_ReturnsSummand(
            string input, float factor, int expectedVariablesCount)
        {
            var summand = Summand.Parse(input);

            summand.Factor.Should().Be(factor);
            summand.Variables.Count.Should().Be(expectedVariablesCount);
        }
Exemple #13
0
        public void TestMultiplyExpressions()
        {
            Summand s1 = new Summand(1, new Dictionary <char, int>()
            {
                { 'x', 2 }, { 'y', 1 }
            });
            Summand s2 = new Summand(3, new Dictionary <char, int>()
            {
                { 'x', 1 }, { 'z', 2 }
            });

            Summand s3 = new Summand(2, new Dictionary <char, int>()
            {
                { 'x', 2 }, { 'y', 1 }
            });
            Summand s4 = new Summand(1, new Dictionary <char, int>()
            {
                { 'x', 1 }, { 'z', 2 }
            });

            Expression e1 = new Expression(new List <Summand>()
            {
                s1, s2
            });
            Expression e2 = new Expression(new List <Summand>()
            {
                s3, s4
            });

            Multiplier m = new Multiplier();

            m.expressions = new List <Expression>()
            {
                e1, e2
            };
            Expression res = m.Multiply();

            Assert.AreEqual(res.summands.Count, 4);
            // check coefficients first
            Assert.AreEqual(res.summands[0].coeff, 2);
            Assert.AreEqual(res.summands[1].coeff, 1);
            Assert.AreEqual(res.summands[2].coeff, 6);
            Assert.AreEqual(res.summands[3].coeff, 3);

            Assert.AreEqual(res.summands[0].variables['x'], 4);
            Assert.AreEqual(res.summands[0].variables['y'], 2);

            Assert.AreEqual(res.summands[1].variables['x'], 3);
            Assert.AreEqual(res.summands[1].variables['y'], 1);
            Assert.AreEqual(res.summands[1].variables['z'], 2);

            Assert.AreEqual(res.summands[2].variables['x'], 3);
            Assert.AreEqual(res.summands[2].variables['y'], 1);
            Assert.AreEqual(res.summands[2].variables['z'], 2);

            Assert.AreEqual(res.summands[3].variables['x'], 2);
            Assert.AreEqual(res.summands[3].variables['z'], 4);
        }
        public void Summand_Ctor_11()
        {
            var summand = new Summand("-2");

            Assert.AreEqual(2, summand.Multiplier);
            Assert.AreEqual(false, summand.IsPositive);
            Assert.AreEqual(string.Empty, summand.Variable);
            Assert.AreEqual(1, summand.Power);
        }
        public void Summand_Ctor_8()
        {
            var summand = new Summand("y");

            Assert.AreEqual(1, summand.Multiplier);
            Assert.AreEqual(true, summand.IsPositive);
            Assert.AreEqual("y", summand.Variable);
            Assert.AreEqual(1, summand.Power);
        }
        public void Summand_Ctor_4()
        {
            var summand = new Summand("3x^2");

            Assert.AreEqual(3, summand.Multiplier);
            Assert.AreEqual(true, summand.IsPositive);
            Assert.AreEqual("x", summand.Variable);
            Assert.AreEqual(2, summand.Power);
        }
        public void Summand_Ctor_1()
        {
            var summand = new Summand("3.5(xy)^2");

            Assert.AreEqual(3.5m, summand.Multiplier);
            Assert.AreEqual(true, summand.IsPositive);
            Assert.AreEqual("xy", summand.Variable);
            Assert.AreEqual(2, summand.Power);
        }
Exemple #18
0
        public void Normalize_ReturnsSummandWithDistinctVariableNamesAndCorrectPowers(
            string input, string expected)
        {
            var summand = Summand.Parse(input);

            var normalized = summand.Normalize();

            normalized.ToString().Should().Be(expected);
        }
        public void Summand_Ctor_9()
        {
            var summand = new Summand("-3.5xy");

            Assert.AreEqual(3.5m, summand.Multiplier);
            Assert.AreEqual(false, summand.IsPositive);
            Assert.AreEqual("xy", summand.Variable);
            Assert.AreEqual(1, summand.Power);
        }
        public void Summand_Ctor_13()
        {
            var summand = new Summand("1.6123x");

            Assert.AreEqual(1.6123m, summand.Multiplier);
            Assert.AreEqual(true, summand.IsPositive);
            Assert.AreEqual("x", summand.Variable);
            Assert.AreEqual(1, summand.Power);
        }
Exemple #21
0
        public void ctor_ZeroFactor_CreatesSummandWithoutVariables()
        {
            var var1 = new Variable('x', 2);
            var var2 = new Variable('y', 3);

            var summand = new Summand(0, new[] { var1, var2 });

            summand.Factor.Should().Be(0);
            summand.Variables.Should().BeEmpty();
        }
Exemple #22
0
        private static IEnumerable <Summand> RaiseSumsToPower(Summand summand)
        {
            var returned = new List <Summand> {
                summand
            };

            summand.SumsRaisedToPower.ForEach(
                sum => returned = DistributeIncludingElementaryFunctions(returned, RaiseSumToPower(sum)).ToList());

            return(returned);
        }
Exemple #23
0
 public Numerator_Form(Interface_Limit_calculator limit_calc, bool index, string formName)
 {
     InitializeComponent();
     Limits             = limit_calc;
     SumTextBox.Enabled = false;
     All_Functions      = new Summand();
     SumTextBox.Text    = "";
     Index                 = index;
     this.Text             = formName;
     Coefficient_Text.Text = "1.0";
 }
Exemple #24
0
 public Equation(
     IReadOnlyCollection <Summand> leftPart  = null,
     IReadOnlyCollection <Summand> rightPart = null)
 {
     _leftPart = leftPart ?? new List <Summand> {
         Summand.Empty()
     };
     _rightPart = rightPart ?? new List <Summand> {
         Summand.Empty()
     };
 }
Exemple #25
0
 public void AddToNumeratorDe(Summand summ, bool index)
 {
     if (index)
     {
         Stack_Numerator.Push(summ);
     }
     else
     {
         Stack_Denominator.Push(summ);
     }
 }
Exemple #26
0
        public void SummandNormalizeTest()
        {
            var summand1   = new Summand(1, new Variable('x'), new Variable('x'));
            var normalized = summand1.Normalize();

            Assert.That(normalized.ToString(), Is.EqualTo("x^2"));

            var summand2    = new Summand(1, new Variable('x'), new Variable('x'), new Variable('y'), new Variable('y'), new Variable('x'));
            var normalized2 = summand2.Normalize();

            Assert.That(normalized2.ToString(), Is.EqualTo("x^3y^2"));
        }
Exemple #27
0
        public void TestCalculateIndex()
        {
            Summand s = new Summand(10, new Dictionary <char, int>()
            {
                { 'x', 2 }, { 'y', 1 }
            });
            List <long> magicNumberLookup = new List <long>()
            {
                2, 3, 5
            };
            List <char> variables = new List <char>()
            {
                'y', 'x'
            };

            Assert.AreEqual(s.CalculateIndex(variables, magicNumberLookup), 35);

            magicNumberLookup = new List <long>()
            {
                3, 6, 14
            };
            variables = new List <char>()
            {
                'z', 'y', 'x'
            };
            Assert.AreEqual(s.CalculateIndex(variables, magicNumberLookup), 96);

            s = new Summand(10, new Dictionary <char, int>());
            magicNumberLookup = new List <long>()
            {
                3, 6, 14
            };
            variables = new List <char>()
            {
                'z', 'y', 'x'
            };
            Assert.AreEqual(s.CalculateIndex(variables, magicNumberLookup), 3);

            s = new Summand(10, new Dictionary <char, int>()
            {
                { 'x', 3 }
            });
            magicNumberLookup = new List <long>()
            {
                3, 6, 14, 36
            };
            variables = new List <char>()
            {
                'z', 'y', 'x'
            };
            Assert.AreEqual(s.CalculateIndex(variables, magicNumberLookup), 353);
        }
Exemple #28
0
        /// <summary>
        /// Adds a <code>Summand</code> object to the SummandsList, verifying if there is another variable already in list to the sum or subtracted.
        /// </summary>
        /// <param name="summand"><code>Summand</code> object to be added to the list</param>
        private void AddSummandToList(Summand summand)
        {
            var found = SummandsList.Find(x => x.variable.Equals(summand.variable) && x.exponential == summand.exponential);

            if (found != null)
            {
                found.coefficient += summand.coefficient;
            }
            else
            {
                SummandsList.Add(summand);
            }
        }
Exemple #29
0
        public void TestClone()
        {
            Summand s = new Summand(10, new Dictionary <char, int>()
            {
                { 'x', 2 }, { 'y', 1 }
            });
            Summand cloned = s.Clone();

            Assert.AreNotSame(s, cloned);
            Assert.AreEqual(cloned.coeff, 10);
            Assert.AreEqual(cloned.variables['x'], 2);
            Assert.AreEqual(cloned.variables['y'], 1);
        }
Exemple #30
0
        private static Summand SimpleSummandProduct(Summand s1, Summand s2)
        {
            var denominator = s1.PolynomialDegreeDenominator * s2.PolynomialDegreeDenominator;
            var numerator   = s1.PolynomialDegree * s2.PolynomialDegreeDenominator +
                              s2.PolynomialDegree * s1.PolynomialDegreeDenominator;
            var gcd = MathHelper.GreatestCommonDivisor(numerator, denominator);

            return(new Summand
            {
                Coefficient = s1.Coefficient * s2.Coefficient,
                PolynomialDegree = numerator / gcd,
                PolynomialDegreeDenominator = denominator / gcd,
                LittleODegree = s1.LittleODegree + s2.LittleODegree
            });
        }