Inheritance: IElementaryFunction
        public void ToTaylorExpansion_2()
        {
            var power = new PowerFunction { Aparam = 2, Bparam = 3, PowerNumerator = 1, PowerDenominator = 2 };
            var expansion = power.ToTaylorExpansion(2).ToList();

            expansion.Should().HaveCount(4);

            expansion.Select(s => s.PolynomialDegree).Should().ContainInOrder(0, 1, 2, 0);
            expansion.Select(s => s.LittleODegree).Should().ContainInOrder(0, 0, 0, 2);
            expansion.Select(s => s.Coefficient).Should().ContainInOrder(Math.Pow(3, 0.5), Math.Pow(3, 0.5) * 1.0 / 3.0, Math.Pow(3, 0.5) * -1.0 / 18.0, 1.0);
        }
        public void ToTaylorExpansion_1()
        {
            var power = new PowerFunction {Aparam = 2, Bparam = 3, PowerNumerator = 3, PowerDenominator = 1};
            var expansion = power.ToTaylorExpansion(7).ToList();

            expansion.Should().HaveCount(4);

            expansion.Select(s => s.PolynomialDegree).Should().ContainInOrder(0, 1, 2, 3);
            expansion.Select(s => s.LittleODegree).Should().OnlyContain(x => x == 0);
            expansion.Select(s => s.Coefficient).Should().ContainInOrder(27.0, 54.0, 36.0, 8.0);
        }
Esempio n. 3
0
        public static IElementaryFunction ConvertToElementaryFunction(string str)
        {
            IElementaryFunction function = null;
            double[] arr = null;

            if (str.Contains("sin"))
            {
                function = new Sine();
                arr = FindParameters(str.Substring(4, str.Count() - 4));
                function.Aparam = arr[0];
                function.Bparam = arr[1];
            }
            else if (str.Contains("cos"))
            {
                function = new Cosine();
                arr = FindParameters(str.Substring(4, str.Count() - 4));
                function.Aparam = arr[0];
                function.Bparam = arr[1];
            }
            else if (str.Contains("ln"))
            {
                function = new LogarithmicFunction();
                arr = FindParameters(str.Substring(3, str.Count() - 3));
                function.Aparam = arr[0];
                function.Bparam = arr[1];
            }
            else if (str.Contains("e^"))
            {
                function = new ExponentialFunction();
                arr = FindParameters(str.Substring(3, str.Count() - 3));
                function.Aparam = arr[0];
                function.Bparam = arr[1];
            }
            else if (str.Contains(")^(") || str.Contains("x^(") || str.Contains(")^"))
            {
                function = new PowerFunction();

                int powerPosition = 0;

                for (int i = 0; i < str.Count(); i++)
                {
                    if (str[i] == '^')
                    {
                        powerPosition = i;
                        StringBuilder num = new StringBuilder();
                        StringBuilder den = new StringBuilder();
                        bool slash = false;
                        for (int j = i + 1; j < str.Count(); j++)
                        {
                            if (str[j] != '/' && str[j] != '(' && str[j] != ')')
                            {
                                if (slash == false)
                                {
                                    num.Append(str[j]);
                                }
                                else den.Append(str[j]);

                            }
                            else if (str[j] == '/')
                            {
                                slash = true;
                            }
                        }

                        if (slash == false)
                        {
                            ((PowerFunction)function).PowerDenominator = 1;
                        }
                        else
                        {
                            ((PowerFunction)function).PowerDenominator = int.Parse(den.ToString());
                        }

                        ((PowerFunction)function).PowerNumerator = int.Parse(num.ToString());

                    }
                }

                int beginPosition = 0;

                if (str[0] == '(')
                {
                    beginPosition = 1;
                }

                arr = FindParameters(str.Substring(beginPosition, powerPosition - beginPosition));
                function.Aparam = arr[0];
                function.Bparam = arr[1];
            }

            return function;
        }