public void ToTaylorExpansion_1()
        {
            var ln = new LogarithmicFunction {
                Aparam = 2, Bparam = 5
            };

            var expansion = ln.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[0].Coefficient.Should().BeApproximately(Math.Log(5), 1e-10);
            expansion[1].Coefficient.Should().BeApproximately(0.4, 1e-10);
            expansion[2].Coefficient.Should().BeApproximately(-0.16 / 2, 1e-10);
            expansion[3].Coefficient.Should().BeApproximately(0.064 / 3, 1e-10);
            expansion[4].Coefficient.Should().BeApproximately(-0.0256 / 4, 1e-10);
            expansion[5].Coefficient.Should().BeApproximately(0.01024 / 5, 1e-10);
            expansion[6].Coefficient.Should().BeApproximately(1.0, 1e-10);
        }
Example #2
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);
        }