重幂求导

· · 个人记录

注: 用 \frac{\mathrm{d}f(x)}{\mathrm{d}x}g(x) 表示 f(x) 对 x 的导函数与 g(x) 的乘积。

求 \mathrm{d}x^x/\mathrm{d}x。

这玩意很典啊。设 y=x^x。

所以,

\ln y=\ln x^x

所以,

\ln y=x\ln x

两边分别对 x 求导,

\frac{\mathrm{d}\ln y}{\mathrm{d} x}=\frac{\mathrm{d}(x\ln x)}{\mathrm{d} x}

运用求导法则,

\frac{1}{y}\frac{\mathrm{d}y}{\mathrm{d}x}=\frac{\mathrm{d}x}{\mathrm{d}x}\ln x+\frac{\mathrm{d}\ln x}{\mathrm{d}x}x=\ln x+1

所以,

\frac{\mathrm{d}x^x}{\mathrm{d}x}=\frac{\mathrm{d}y}{\mathrm{d}x}=y(\ln x+1)=x^x(\ln x+1)

以上为背景。

好,我们现在要求 \mathrm{d}x^{f(x)}/\mathrm{d}x。

类比前面可得,

\frac{\mathrm{d}x^{f(x)}}{\mathrm{d}x}=x^{f(x)}\left(f'(x)\ln x+\frac{f(x)}{x}\right)

所以,难免不会有人没有不求 \mathrm{d}x^{x^{x}}/\mathrm{d}x 的想法。

我们用 f(x)=x^x 代入上式,正好我们刚求了 f'(x) 的表达式。

所以说,套用结论可得,

\frac{\mathrm{d} x^{x^{x}}}{\mathrm{d} x}=x^{x^{x}}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)

这个式子不是很好看,但是还行吧。

由此,我们可以递推得到 \mathrm{d}(^nx)/\mathrm{d}x 的表达式。

这样,我们可以写代码了。

这段代码能帮我们计算 ^n x 的表达式的 \LaTeX。

string f(int n) {
    if(n == 1) return "x";
    if(n == 2) return "x^x";
    return "x^{" + f(n - 1) + "}";
}

这段代码能够帮我们计算 \mathrm{d}(^n x)/\mathrm{d}x 的表达式的 \LaTeX。

string f_(int n) {
    if(n == 1) return "1";
    if(n == 2) return "x^x(\\ln x+1)";
    return f(n) + "\\left(" + f_(n - 1) + "\\ln x+x^{" + f(n - 2) + "-1}\\right)";
}

由此,我们有了如下结论:

\frac{\mathrm{d}x^{x^{x^x}}}{\mathrm{d} x}=x^{x^{x^x}}\left(x^{x^x}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)\ln x+x^{x^x-1}\right) \frac{\mathrm{d}x^{x^{x^{x^x}}}}{\mathrm{d}x}=x^{x^{x^{x^x}}}\left(x^{x^{x^x}}\left(x^{x^x}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)\ln x+x^{x^x-1}\right)\ln x+x^{x^{x^x}-1}\right)

以及,重幂求导法则,

\frac{\mathrm{d}(^n x)}{\mathrm{d}x}={}^n x\left(^{n-1}x\left(^{n-2}x\left(\cdots\left(x^x\left(\ln x+1\right)\ln x+x^{x-1}\right)\cdots\right)\ln x+\frac{^{n-2}x}{x}\right)\ln x+\frac{^{n-1}x}{x}\right)

我们要推广这个东西,有三条路可以走。

  1. 求 \mathrm{d}(^nf(x))/\mathrm{d}x。
  2. 求 \mathrm{d}^k(^nx)/\mathrm{d}x^k。
  3. 求 \mathrm{d}(x\uparrow\uparrow\uparrow n)/\mathrm{d}x。

推广 1 只需要在重幂求导法则外面套一个链式求导法则就好了。也就是说,

\frac{\mathrm{d}(^nf(x))}{\mathrm{d}x}=\frac{\mathrm{d}(^nf(x))}{\mathrm{d}f(x)}\cdot\frac{\mathrm{d}f(x)}{\mathrm{d}x}

主要研究推广 2 和推广 3。

推广 2,求 n 重幂的 k 阶导数。

首先我们看 x^x 的 k 阶导数。

\frac{\mathrm{d}x^x}{\mathrm{d}x}=x^x(\ln x+1)

用乘法法则得到,

\frac{\mathrm{d}^2x^x}{\mathrm{d}x^2}=\frac{\mathrm{d}x^x}{\mathrm{d}x}(\ln x+1)+\frac{\mathrm{d}(\ln x+1)}{\mathrm{d}x}x^x=x^x(\ln x+1)^2+x^{x-1}

继续求导,

\begin{aligned}\frac{\mathrm{d}^3x^x}{\mathrm{d}x^3}&=\frac{\mathrm{d}x^x}{\mathrm{d}x}(\ln x+1)^2+\frac{\mathrm{d}(\ln x+1)^2}{\mathrm{d}x}x^x+\frac{\mathrm{d}x^{x-1}}{\mathrm{d}x}\\&=x^x(\ln x+1)^3+2x^{x-1}(\ln x+1)+x^{x-1}\left(\ln x+\frac{x-1}{x}\right)\\&=x^x(\ln x+1)^3+3x^{x-1}(\ln x+1)-x^{x-2}\end{aligned}

继续求导,

\begin{aligned}\frac{\mathrm{d}^4x^x}{\mathrm{d}x^4}&=\frac{\mathrm{d}x^x}{\mathrm{d}x}\left(\ln x + 1\right)^{3} + \frac{\mathrm{d}\left(\ln x + 1\right)^{3}}{\mathrm{d}x}x^{x}+3\left(\frac{\mathrm{d}x^{x-1}}{\mathrm{d}x}(\ln x+1)+\frac{\mathrm{d}(\ln x+1)}{\mathrm{d}x}x^{x-1}\right)-\frac{\mathrm{d}x^{x-2}}{\mathrm{d}x}\\&= x^{x} \left(\ln x + 1\right)^{4} + 3x^{x - 1} \left(\ln x + 1\right)^{2}+3\left(x^{x-1}\left(\ln x+\frac{x-1}{x}\right)(\ln x+1)+x^{x-2}\right)-x^{x-2}\left(\ln x+\frac{x-2}{x}\right)\\&=x^x(\ln x+1)^4+6x^{x-1}(\ln x+1)^2-4x^{x-2}(\ln x+1)+3x^{x-2}+2x^{x-3}\end{aligned}

经过仔细观察之后发现没有什么明显的规律。我们观察这里的每一项,都可以写成 kx^{x-i}(\ln x+1)^j 的形式。

我们对这个式子求导,

\begin{aligned}\frac{\mathrm{d}x^{x-i}(\ln x+1)^j}{\mathrm{d}x}&=\frac{\mathrm{d}x^{x-i}}{\mathrm{d}x}(\ln x+1)^j+\frac{\mathrm{d}(\ln x+1)^j}{\mathrm{d}x}x^{x-i}\\&=x^{x-i}\left(\ln x+\frac{x-i}{x}\right)(\ln x+1)^j+j(\ln x+1)^{j-1}x^{x-i-1}\\&=x^{x-i}(\ln x+1)^{j+1}-ix^{x-i-1}(\ln x+1)^j+jx^{x-i-1}(\ln x+1)^{j-1}\end{aligned}

令 \mathcal{F}(i,j)=x^{x-i}(\ln x+1)^j,则可以简写成,

\frac{\mathrm{d}\mathcal{F}(i,j)}{\mathrm{d}x}=\mathcal{F}(i,j+1)-i\mathcal{F}(i+1,j)+j\mathcal{F}(i+1,j-1)\tag{*}

所以,不难写出求解 x^x 的 k 阶导数的 \LaTeX 的程序。

我们先定义一个结构体 Term,

struct Term {
    int k, i, j;
    string str() {
        return (k < 0 ? '-' : '+')
             + (abs(k) == 1 ? "" : to_string(abs(k)))
             + "x^{x" + (i == 0 ? "" : to_string(-i)) + "}"
             + (j == 0 ? "" : "(\\ln x+1)") + (j <= 1 ? "" : "^{" + to_string(j) + "}");
    }
}; 

其成员函数 str() 是格式化输出函数。

接着实现求导部分:

vector<Term> calcDeri(vector<Term> v) {
    map<pair<int, int>, int> mp;
    vector<Term> ret;
    for(auto iter : v) {
        mp[{iter.i, iter.j + 1}] += iter.k;
        mp[{iter.i + 1, iter.j}] -= iter.i * iter.k;
        mp[{iter.i + 1, iter.j - 1}] += iter.j * iter.k;
    }
    for(auto iter : mp) {
        if(!iter.second) continue;
        ret.push_back({(iter.second), (iter.first).first, (iter.first).second});
    }
    return ret;
}

用 map 实现各项系数的相加,用 vector<Term> 表示一个代数式。

调用,

terms = calcDeri(terms);

就可以完成一次求导操作。

不管如何,我们借此求出了 x^x 的 5,6 阶导数。

\frac{\mathrm{d}^5 x^x}{\mathrm{d}x^5}=x^{x}(\ln x+1)^{5}+10x^{x-1}(\ln x+1)^{3}+15x^{x-2}(\ln x+1)-10x^{x-2}(\ln x+1)^{2}-10x^{x-3}+10x^{x-3}(\ln x+1)-6x^{x-4} \frac{\mathrm{d}^6 x^x}{\mathrm{d}x^6}=x^{x}(\ln x+1)^{6}+15x^{x-1}(\ln x+1)^{4}+45x^{x-2}(\ln x+1)^{2}-20x^{x-2}(\ln x+1)^{3}+15x^{x-3}-60x^{x-3}(\ln x+1)+30x^{x-3}(\ln x+1)^{2}+40x^{x-4}-36x^{x-4}(\ln x+1)+24x^{x-5}

那么 \mathrm{d}^nx^x/\mathrm{d}x^n 有没有什么通项公式呢?

我们考察 \mathrm{d}^nx^x/\mathrm{d}x^n 中 \mathcal{F}(i,j) 的系数,记作 \mathcal{K}_n(i,j)。它是由什么递推而来的呢?

由 (*) 式可得,

\mathcal{K}_n(i,j)=\mathcal{K}_{n-1}(i,j-1)-(i-1)\mathcal{K}_{n-1}(i-1,j)+(j+1)\mathcal{K}_{n-1}(i-1,j+1)

初始值为 \mathcal{K}_0(0,0)=1,\mathcal{K}_0(i,j)=0,(i,j\neq0)。

这玩意当 dp 写也挺好的。

但这只是 x^x 的 k 阶导数的求法。我们还需要求出 \mathrm{d}^k({}^nx)/\mathrm{d}x^k。

如果要效仿之前的做法,我们必须找到 \mathrm{d}^k({}^nx)/\mathrm{d}x^k 每一项的通式。

但这似乎计算量非常的大。有没有别的方法呢?

我们回到这个式子,

\frac{\mathrm{d}x^{f(x)}}{\mathrm{d}x}=x^{f(x)}\left(f'(x)\ln x+\frac{f(x)}{x}\right)

我们直接对这个式子求导,再把 f(x)={}^{n-1}x 代入,不就能求出 ^nx 的二阶导了吗?

说的道理。

\begin{aligned}\frac{\mathrm{d}^2x^{f(x)}}{\mathrm{d}x^2}&=\deri{x^{f(x)}}\left(f'(x)\ln x+\frac{f(x)}{x}\right)+\deri{\left(f'(x)\ln x+\frac{f(x)}{x}\right)}x^{f(x)}\\&=x^{f(x)}\left(f'(x)\ln x+\frac{f(x)}{x}\right)^2+x^{f(x)}\left(\deri{f'(x)\ln x}+\deri{\frac{f(x)}x}\right)\\&=x^{f(x)}\left(f'(x)\ln x+\frac{f(x)}{x}\right)^2+x^{f(x)}\left(f''(x)\ln x+\frac{2f'(x)}{x}-\frac{f(x)}{x^2}\right)\end{aligned}

然后,你就会惊奇的发现,^nx 的二阶导是可以由 ^{n-1}x 的一阶导和二阶导推导而来的。

也就是说,这是一个递归的过程。我们可以写代码来硬算!

string f(int n, int k) {
    if(k == 0) {
        if(n == 2) return "x^x";
        return "x^{" + f(n - 1, 0) + "}";
    }
    else {
        if(n == 2) return "x^{x-" + to_string(k) + "}";
        return "x^{" + f(n - 1, 0) + "-" + to_string(k) + "}";
    }
}

string fD(int n, int k) {
    if(k == 0) {
        if(n == 2) return "x^x(\\ln x+1)";
    }
    else {
        if(n == 2) return "x^{x-" + to_string(k) + "}(\\ln x+1)";
    }
    return f(n, k) + "\\left(" + fD(n - 1, 0) + "\\ln x+" + f(n - 1, 1) + "\\right)";
}

string fDD(int n) {
    if(n == 2)
        return "x^x(\\ln x+1)^2+x^{x-1}";
    return f(n, 0) + "\\left(" + fD(n - 1, 0) + "\\ln x+" + f(n - 1, 1) + "\\right)^2+"
         + f(n, 0) + "\\left(\\left(" + fDD(n - 1) + "\\right)\\ln x+2" + fD(n - 1, 1) + "-" + f(n - 1, 2) + "\\right)";
}

我们用 {\rm f}(n, k),{\rm fD}(n, k),{\rm fDD}(n) 三个函数分别表示 ^nx/x^k,\mathrm{d}(^nx)/\mathrm{d}x\cdot1/x^k 和 \mathrm{d}^2(^nx)/\mathrm{d}x^2。

调用 {\rm fDD}(n) 即可得到想要的结果。由此可得,

\frac{\mathrm{d}^2x^{x^x}}{\mathrm{d}x^2}=x^{x^x}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)^2+x^{x^x}\left(\left(x^x(\ln x+1)^2+x^{x-1}\right)\ln x+2x^{x-1}(\ln x+1)-x^{x-2}\right) \frac{\mathrm{d}^2x^{x^{x^x}}}{\mathrm{d}x^2}=x^{x^{x^x}}\left(x^{x^x}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)\ln x+x^{x^x-1}\right)^2+x^{x^{x^x}}\left(\left(x^{x^x}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)^2+x^{x^x}\left(\left(x^x(\ln x+1)^2+x^{x-1}\right)\ln x+2x^{x-1}(\ln x+1)-x^{x-2}\right)\right)\ln x+2x^{x^x-1}\left(x^x(\ln x+1)\ln x+x^{x-1}\right)-x^{x^x-2}\right)

而 x^{x^{x^{x^x}}} 的二阶导的 \LaTeX 已经有 604 个字符长了。

归纳可得通项公式。但是我懒得写。

然而我们只解决了 \mathrm{d}^k(^nx)/\mathrm{d}x^k 在 n=2 和 k=2 时的情况。

也就是说,我们仍然需要求一个算法来解决这类问题。一种简单的想法是写出一个可以用于求解任意问题的导数计算器。

另一种想法是求出 \mathrm{d}^k(x^{f(x)})/\mathrm{d}x^k 的通项或递推。

但用现成的导数计算器求出来就是一坨答辩一样的式子。

目前没有想到较好的解法,遂咕之。