Example #1
0
        public static Rational Harmonic(BigInteger n)
        {
            Rational sum = 0;

            for (BigInteger k = 1; k <= n; k++)
            {
                sum += Rational.Inv(k);
            }

            return(sum);
        }
Example #2
0
        public static Rational Harmonic(BigInteger n, int m)
        {
            Rational sum = 0;

            for (BigInteger k = 1; k <= n; k++)
            {
                sum += Rational.Inv(Rational.Pow(k, m));
            }

            return(sum);
        }
Example #3
0
        public Series Inv()
        {
            // We have the following recursive formula for the coefficients b[n] of
            // the multiplicative inverse f(z)^-1:
            //
            // b[0] = a[0]^-1
            // b[1] = -a[0]^-1(a[1]b[0])
            // b[2] = -a[0]^-1(a[1]b[1]+a[2]b[0])
            // b[3] = -a[0]^-1(a[1]b[2]+a[2]b[1]+a[3]b[0])
            // ...
            //
            // We also have an explicit formula for b[n]:
            //
            // b[n] = a[0]^-1(1-c[n,1]+c[n,2]-c[n,3]+...+c[n,n]),
            // c[n,k] = a[0]^-k sum(multinomial*a[i[1]]...a[i[k]])
            // where the sum is taken over all partitions (i[1],...,i[k]) of n
            // into k parts, and multinomial is the multinomial coefficient for the
            // partition.

            return(new Series((n, s) =>
            {
                Rational a0inv = Rational.Inv(this[0]);

                if (n == 0)
                {
                    return a0inv;
                }

                // Always prefer the recursive formula because it is much faster.
                if (s.ComputeTo(n - 1))
                {
                    int high = n;
                    Rational c = Rational.Zero;

                    if (high > this.Degree)
                    {
                        high = this.Degree;
                    }

                    for (int i = 1; i <= high; i++)
                    {
                        c += this[i] * s[n - i];
                    }

                    return -a0inv * c;
                }
                else
                {
                    Rational m = a0inv;
                    Rational c = Rational.Zero;

                    for (int i = 1; i <= n; i++)
                    {
                        m *= -a0inv;
                        c += m * PowCoefficient(this, i, n, true);
                    }

                    return c;
                }
            }, order: 0, degree: this.Degree != 0 ? InfinityDegree : 0, cache: true));
        }