一般连分数¶

$$ \newcommand{\dplus}{\space\underset{+}{}\space} $$

形如

$$ a_0 + \cfrac{b_1}{a_1 + \cfrac{b_2}{a_2 + \cfrac{b_3}{a_3 + ...}}} \quad a_n, b_n为实数 $$

也可以表示为 $$ a_0 + \frac{b_1}{a_1} \dplus \frac{b_2}{a_2} \dplus \frac{b_3}{a_3} $$

当$b_k$全为1时是简单连分数

$$ c_n = a_0 + \frac{b_1}{a_1} \dplus \frac{b_2}{a_2} \dplus \frac{b_3}{a_3} \dplus \dots \dplus \frac{b_n}{a_n} $$

无限连分数¶

$$ [a_0;a_1;a_2;a_3...] = \lim_{n \to \infty} [a_0;a_1,\dots,a_n] $$

定理¶

Transformation rules¶

$\rho_k$不为$0$

$$ a_0 + \frac{b1}{a_1} \dplus \frac{b_2}{a_2} \dplus \dots \dplus \frac{b_n}{a_n} = a_0 + \frac{\rho_1b_1}{\rho_1a_1} \dplus \frac{\rho_1\rho_2b_2}{\rho_2a_2} \dplus \dots \dplus \frac{\rho_{n - 1}\rho_{n}b_n}{\rho_na_n} $$

展开1¶

当$\alpha_k$为实数列不为0且$\alpha_k \neq \alpha_k$,有

$$ \sum_{k = 1}^{n} \frac{(-1)^{k - 1}}{\alpha_k} = \cfrac{1}{\alpha_1 + \cfrac{\alpha_1^2}{\alpha_2 - \alpha_1 + \cfrac{\alpha_2^2}{\alpha_3 - \alpha_2 + \cfrac{\ddots}{\cfrac{\alpha_{n - 1}^2}{\alpha_n - \alpha_{n - 1}}}}}} $$

当$n \to \infty$时有

$$ \sum_{k = 1}^{\infty} \frac{(-1)^{k - 1}}{\alpha_k} = \frac{1}{\alpha_1} \dplus \frac{\alpha_1^2}{\alpha_2 - \alpha_1} \dplus \frac{\alpha_2^2}{\alpha_3 - \alpha_2} \dplus \dots $$

展开2¶

对于实数列$\alpha_k且\alpha_k\neq0,1$,有

$$ \sum_{k = 1}^{n} \cfrac{(-1)^{k - 1}}{\alpha_1\dots\alpha_k} = \cfrac{1}{\alpha_1 + \cfrac{\alpha_1}{\alpha_2 - 1 + \cfrac{\alpha_2}{\alpha_3 - 1 + \cfrac{\ddots}{\alpha_{n - 1} + \cfrac{\alpha_{n - 1}}{\alpha_n - 1}}}}} $$

当$n \to \infty$时有

$$ \sum_{k = 1}^{\infty} \frac{(-1)^{k - 1}}{\alpha_1\dots\alpha_k} = \frac{1}{\alpha_1} \dplus \frac{\alpha_1}{\alpha_2 - 1} \dplus \frac{\alpha_2}{\alpha_3 - 1} \dplus \dots \dplus \frac{\alpha_{n - 1}}{\alpha_n - 1} $$

展开3¶

$$ \sum_{k = 0}^{n} \alpha_kx^k = \alpha_0 + \frac{\alpha_1x}{1} \dplus \frac{-\frac{\alpha_2}{\alpha_1}x}{1 + \frac{\alpha_2}{\alpha_1}x} \dplus \frac{-\frac{\alpha_3}{\alpha_2}x}{1 + \frac{\alpha_3}{\alpha_2}x} \dplus \dots \dplus \frac{-\frac{\alpha_n}{\alpha_{n - 1}}x}{1 + \frac{\alpha_n}{\alpha_{n - 1}}x} $$

对于$n \to \infty$是也成立

常见函数展开¶

先将函数展开成泰勒级数在使用上面的展开定理可得到$arctan(x)$的展开为

$arctan(x)$¶

$$ arctanx = \frac{x}{1} \dplus \frac{x^2}{3 - x^2} \dplus \frac{3^2x^2}{5 - 3x^2} \dplus \frac{5^2x^2}{7 - 5x^2} \dplus \dots $$

$ln(1 + x)$¶

$ln(1 + x)$的展开为

$$ ln(1 + x) = \frac{x}{1} \dplus \frac{1^2x}{2 - 1x} \dplus \frac{2^2x}{3 - 2x} \dplus \frac{3^2x}{4 - 3x} \dplus \dots $$

$e^x$¶

在展开3中设$\alpha_k = \frac{1}{k!}$得到

$$ e^x = 1 + \frac{x}{1} \dplus \frac{-\frac{1}{2}x}{1 + \frac{1}{2}x} \dplus \frac{-\frac{1}{3}x}{1 + \frac{1}{3}x} \dplus \dots \dplus \frac{-\frac{1}{n}x}{1 + \frac{1}{n}x} $$

Wallis-Euler递推关系¶

第$n$个收敛的有理分数为

$$ c_n = \frac{p_n}{q_n} $$

有

$$ \begin{gather*} p_n = a_np_{n - 1} + b_np_{n - 2} \qquad q_n = a_nq_{n - 1} + b_nq_{n - 2} \\\\ p_{-1} = 1 \quad p_0 = a_0 \quad q_{-1} = 0 \quad q_0 = 1 \end{gather*} $$

可以看到这是简单连分数地推关系的推广式。英文原名Wallis-Euler recurrence relations

In [1]:
# 得到arctanx在x的n次连分数展开
def get_arctan_p(n: int):

    def f(x: float):
        a = list(0 for _ in range(n + 1))
        b = list(0 for _ in range(n + 1))

        xx = x * x

        for i in range(n + 1):
            # 此时 i==0连分数无效
            if i == 0:
                a[i] = 0
                b[i] = 1
            elif i == 1:
                a[i] = 1
                b[i] = x
            else:
                a[i] = 2 * i - 1 - (2 * i - 3) * xx
                b[i] = ((i - 2) * 2 + 1) * ((i - 2) * 2 + 1) * xx

        p = list(0 for _ in range(n + 1))
        q = list(0 for _ in range(n + 1))
        c = list(0 for _ in range(n + 1))

        for i in range(n + 1):

            if i == 0:
                p[i] = 0
                q[i] = 1
            elif i == 1:
                p[i] = a[i] * p[i - 1] + b[i]
                q[i] = a[i] * q[i - 1]
            else:
                p[i] = a[i] * p[i - 1] + b[i] * p[i - 2]
                q[i] = a[i] * q[i - 1] + b[i] * q[i - 2]

            c[i] = p[i] / q[i]

        return (c, p ,q)
    
    return f
In [2]:
import numpy as np
import matplotlib.pyplot as plt

def get_arctan_1(n: int, m: int):
    pf = get_arctan_p(n)
    def r_f(x: float):
        (c, _, _) = pf(x)
        return c[m]
    return r_f


x = np.linspace(-10, 10, 100)
y = np.arctan(x)

plt.plot(x, y, '-', label=r'$arctan(x)$')

p = get_arctan_1(10, 2)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=2$')

p = get_arctan_1(10, 3)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=3$')

p = get_arctan_1(10, 4)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=4$')

p = get_arctan_1(10, 9)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=9$')

plt.ylim(-2, 2)
plt.xlim(-10, 10)
plt.title('Countinued Fractions Expand')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid()
plt.show()
No description has been provided for this image
In [ ]:
# 得到ln(1 + x)在x的n次连分数展开
def get_ln_p(n: int):

    def f(x: float):
        a = list(0 for _ in range(n + 1))
        b = list(0 for _ in range(n + 1))

        xx = x * x

        for i in range(n + 1):
            # 此时 i==0连分数无效
            if i == 0:
                a[i] = 0
                b[i] = 1
            elif i == 1:
                a[i] = 1
                b[i] = x
            else:
                a[i] = i - (i - 1) * x
                b[i] = (i - 1) * (i - 1) * x

        p = list(0 for _ in range(n + 1))
        q = list(0 for _ in range(n + 1))
        c = list(0 for _ in range(n + 1))

        for i in range(n + 1):

            if i == 0:
                p[i] = 0
                q[i] = 1
            elif i == 1:
                p[i] = a[i] * p[i - 1] + b[i]
                q[i] = a[i] * q[i - 1]
            else:
                p[i] = a[i] * p[i - 1] + b[i] * p[i - 2]
                q[i] = a[i] * q[i - 1] + b[i] * q[i - 2]

            c[i] = p[i] / q[i]

        return (c, p ,q)
    
    return f
In [ ]:
import numpy as np
import matplotlib.pyplot as plt

def get_ln_1(n: int, m: int):
    pf = get_ln_p(n)
    def r_f(x: float):
        (c, _, _) = pf(x)
        return c[m]
    return r_f


x = np.linspace(-1, 10, 100)[1:]
y = np.log(1 + x)

plt.plot(x, y, '-', label=r'$ln(1 + x)$')

p = get_ln_1(10, 2)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=2$')

p = get_ln_1(10, 3)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=3$')

p = get_ln_1(10, 4)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=4$')

p = get_ln_1(10, 9)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=9$')

plt.ylim(-10, 10)
plt.xlim(-1, 10)
plt.title('Countinued Fractions Expand')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid()
plt.show()
No description has been provided for this image
In [ ]:
# 得到e^x在x的n次连分数展开
def get_ex_p(n: int):

    def f(x: float):
        a = list(0 for _ in range(n + 1))
        b = list(0 for _ in range(n + 1))

        for i in range(n + 1):
            # 此时 i==0连分数无效
            if i == 0:
                a[i] = 1
                b[i] = 1
            elif i == 1:
                a[i] = 1
                b[i] = x
            else:
                a[i] = 1 + x / i
                b[i] = -x / i 

        p = list(0 for _ in range(n + 1))
        q = list(0 for _ in range(n + 1))
        c = list(0 for _ in range(n + 1))

        for i in range(n + 1):

            if i == 0:
                p[i] = a[0]
                q[i] = 1
            elif i == 1:
                p[i] = a[i] * p[i - 1] + b[i]
                q[i] = a[i] * q[i - 1]
            else:
                p[i] = a[i] * p[i - 1] + b[i] * p[i - 2]
                q[i] = a[i] * q[i - 1] + b[i] * q[i - 2]

            c[i] = p[i] / q[i]

        return (c, p ,q)
    
    return f
In [ ]:
import numpy as np
import matplotlib.pyplot as plt

def get_ex_1(n: int, m: int):
    pf = get_ex_p(n)
    def r_f(x: float):
        (c, _, _) = pf(x)
        return c[m]
    return r_f


x = np.linspace(-6, 4, 100)
y = np.exp(x)

plt.plot(x, y, '-', label=r'$e^x$')

p = get_ex_1(10, 2)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=2$')

p = get_ex_1(10, 3)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=3$')

p = get_ex_1(10, 4)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=4$')

p = get_ex_1(10, 5)
p_f = np.vectorize(p)
y = p_f(x)
plt.plot(x, y, '-', label=r'$n=5$')

plt.ylim(-10, 10)
plt.xlim(-5, 5)
plt.title('Countinued Fractions Expand')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid()
plt.show()
No description has been provided for this image