Inheritance: IElementaryFunction
示例#1
0
        public void ToTaylorExpansion_2()
        {
            var sine = new Sine { Aparam = 2, Bparam = 0 };

            var expansion = sine.ToTaylorExpansion(5).ToList();

            expansion.Should().HaveCount(4);
            expansion.Select(s => s.PolynomialDegree).Should().ContainInOrder(1, 3, 5, 0);
            expansion.Select(s => s.LittleODegree).Should().ContainInOrder(0, 0, 0, 5);
            expansion.Select(s => s.Coefficient).Should().ContainInOrder(2.0, -8.0 / 6, 32.0 / 120, 1.0);
        }
示例#2
0
        public void ToTaylorExpansion_3()
        {
            var sine = new Sine { Aparam = 2, Bparam = 5 };

            var expansion = sine.ToTaylorExpansion(5).ToList();

            expansion.Should().HaveCount(7);
            expansion.Select(s => s.PolynomialDegree).Should().ContainInOrder(0, 1, 2, 3, 4, 5, 0);
            expansion.Select(s => s.LittleODegree).Should().ContainInOrder(0, 0, 0, 0, 0, 0, 5);
            expansion.Select(s => s.Coefficient)
                .Should()
                .ContainInOrder(
                    Math.Sin(5),
                    2 * Math.Cos(5),
                    -2 * Math.Sin(5),
                    -8.0 / 6 * Math.Cos(5),
                    16.0 / 24 * Math.Sin(5),
                    32.0 / 120 * Math.Cos(5),
                    1.0
                );
        }
示例#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;
        }