C++ 数学解析(Mathematical Analysis)

我回来了,大家想我了吗?最近我摔伤了腿所以没更新,现在我又回来了。

极限、导数、微分、积分、泰勒级数、数值优化、微分方程、复变函数、傅里叶变换、拉普拉斯变换、矩阵分析、泛函与最优化


目录

一、数学解析导论:什么是数学解析?

二、C++ 数学基础库与高精度计算

三、极限理论与 C++ 数值极限计算

四、单变量微积分:导数、微分、高阶导数

五、多变量微积分:偏导、梯度、雅可比、海森矩阵

六、数值积分:矩形法、梯形法、辛普森积分、自适应积分

七、泰勒级数与函数逼近

八、微分方程求解:欧拉法、龙格‑库塔法(RK4)

九、复变函数与复分析

十、傅里叶分析(FFT)

十一、拉普拉斯变换

十二、矩阵分析与线性解析

十三、泛函分析与变分法入门

十四、最优化与凸分析(梯度下降、牛顿法、拟牛顿法)

十五、C++ 数学解析完整工程库(可直接使用)

十六、数学解析在 AI / 物理 / 图形学中的应用


一、数学解析导论

数学解析 = 连续量的变化规律
核心工具:

  • 极限
  • 微分(变化率)
  • 积分(累积量)
  • 级数(函数逼近)
  • 微分方程(动态系统)
  • 泛函(函数的函数)

用途:

  • 物理引擎
  • 渲染引擎
  • 神经网络反向传播
  • 信号处理
  • 控制系统
  • 金融建模
  • 科学计算

二、C++ 数学基础与高精度计算

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <functional>
#include <complex>
#include <algorithm>
#include <stdexcept>

using namespace std;

const double EPS = 1e-12;
const double PI = acos(-1.0);
const double E = exp(1.0);

using Func = function<double(double)>;
using Vec = vector<double>;
using Mat = vector<Vec>;
using Complex = complex<double>;

三、极限理论(Limit)

定义

lim⁡x→af(x)=L\lim_{x \to a} f(x) = Llimxaf(x)=L

C++ 数值极限计算

double limit(Func f, double a, double h = 1e-6) {
    double left = f(a - h);
    double right = f(a + h);
    return (left + right) * 0.5;
}

重要极限

  1. lim⁡x→0sin⁡xx=1\lim_{x\to 0}\frac{\sin x}{x}=1limx0xsinx=1
  2. lim⁡x→∞(1+1x)x=e\lim_{x\to\infty}(1+\frac{1}{x})^x=elimx(1+x1)x=e
double important_limit_e(int n = 1000000) {
    return pow(1.0 + 1.0 / n, n);
}

四、单变量微积分

4.1 数值导数(中心差分)

f′(x)≈f(x+h)−f(x−h)2hf'(x) \approx \frac{f(x+h)-f(x-h)}{2h}f(x)2hf(x+h)f(xh)

double derivative(Func f, double x, double h = 1e-5) {
    return (f(x + h) - f(x - h)) / (2 * h);
}

4.2 高阶导数

double nth_derivative(Func f, double x, int n, double h = 1e-4) {
    if (n == 0) return f(x);
    if (n == 1) return derivative(f, x, h);
    return (nth_derivative(f, x+h, n-1, h) - nth_derivative(f, x-h, n-1, h)) / (2*h);
}

4.3 微分

dy=f′(x)dxdy = f'(x)dxdy=f(x)dx


五、多变量微积分

5.1 偏导数

double partial_derivative(function<double(Vec)> f, Vec x, int dim, double h = 1e-5) {
    Vec x1 = x, x2 = x;
    x1[dim] += h;
    x2[dim] -= h;
    return (f(x1) - f(x2)) / (2 * h);
}

5.2 梯度(Gradient)

Vec gradient(function<double(Vec)> f, Vec x, double h = 1e-5) {
    Vec grad(x.size());
    for (int i = 0; i < x.size(); i++) {
        grad[i] = partial_derivative(f, x, i, h);
    }
    return grad;
}

5.3 雅可比矩阵(Jacobian)

5.4 海森矩阵(Hessian)


六、数值积分

6.1 矩形积分

double integrate_rect(Func f, double a, double b, int n = 10000) {
    double h = (b - a) / n;
    double sum = 0;
    for (int i = 0; i < n; i++) sum += f(a + i*h);
    return sum * h;
}

6.2 梯形积分

double integrate_trap(Func f, double a, double b, int n = 10000) {
    double h = (b - a) / n;
    double sum = 0.5 * (f(a) + f(b));
    for (int i = 1; i < n; i++) sum += f(a + i*h);
    return sum * h;
}

6.3 辛普森积分(高精度)

double integrate_simpson(Func f, double a, double b, int n = 10000) {
    if (n % 2 == 1) n++;
    double h = (b - a) / n;
    double sum = f(a) + f(b);
    for (int i = 1; i < n; i++) {
        sum += f(a + i*h) * (i%2 ? 4 : 2);
    }
    return sum * h / 3.0;
}

6.4 自适应积分


七、泰勒级数(Taylor Series)

f(x)=∑n=0∞f(n)(a)n!(x−a)nf(x) = \sum_{n=0}^\infty \frac{f^{(n)}(a)}{n!}(x-a)^nf(x)=n=0n!f(n)(a)(xa)n

double taylor_series(Func f, double x, double a, int order = 5) {
    double res = 0;
    double fact = 1;
    for (int n = 0; n <= order; n++) {
        if (n > 0) fact *= n;
        double d = nth_derivative(f, a, n);
        res += d * pow(x - a, n) / fact;
    }
    return res;
}

八、常微分方程(ODE)

8.1 欧拉法

Vec ode_euler(Func f, double y0, double t0, double t1, int steps) {
    Vec res;
    double h = (t1 - t0) / steps;
    double y = y0, t = t0;
    res.push_back(y);
    for (int i = 0; i < steps; i++) {
        y += h * f(t);
        t += h;
        res.push_back(y);
    }
    return res;
}

8.2 龙格‑库塔 RK4(工程标准)

double rk4(function<double(double,double)> f, double t, double y, double h) {
    double k1 = f(t, y);
    double k2 = f(t + h/2, y + h*k1/2);
    double k3 = f(t + h/2, y + h*k2/2);
    double k4 = f(t + h, y + h*k3);
    return y + h/6*(k1 + 2*k2 + 2*k3 + k4);
}

九、复变函数与复分析

Complex complex_derivative(Complex(*f)(Complex), Complex z, double h = 1e-5) {
    return (f(z + h) - f(z - h)) / (2 * h);
}

柯西‑黎曼方程、解析函数、留数定理均可实现。


十、傅里叶分析(Fourier)

离散傅里叶变换 DFT

vector<Complex> dft(const vector<Complex>& a) {
    int n = a.size();
    vector<Complex> res(n);
    for (int k = 0; k < n; k++) {
        Complex sum = 0;
        for (int t = 0; t < n; t++) {
            double angle = -2 * PI * k * t / n;
            sum += a[t] * Complex(cos(angle), sin(angle));
        }
        res[k] = sum;
    }
    return res;
}

快速傅里叶变换 FFT


十一、拉普拉斯变换

double laplace(Func f, double s, double inf = 100, int steps = 10000) {
    Func integrand = [&](double t) {
        return f(t) * exp(-s * t);
    };
    return integrate_simpson(integrand, 0, inf, steps);
}

十二、矩阵分析

矩阵求导、迹、行列式、特征值、奇异值分解均可完整实现。


十三、泛函与变分法

欧拉‑拉格朗日方程数值求解。


十四、最优化(数学解析核心应用)

牛顿法(二阶优化)

double newton_method(Func f, double x0, int max_iter = 100) {
    double x = x0;
    for (int i = 0; i < max_iter; i++) {
        double d = derivative(f, x);
        if (fabs(d) < EPS) break;
        double d2 = nth_derivative(f, x, 2);
        x -= d / d2;
    }
    return x;
}

十五、完整 C++ 数学解析库(可直接运行)

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <functional>
#include <complex>
#include <algorithm>
#include <stdexcept>

using namespace std;

const double EPS = 1e-12;
const double PI = acos(-1.0);
const double E = exp(1.0);

using Func = function<double(double)>;
using Vec = vector<double>;
using Mat = vector<Vec>;
using Complex = complex<double>;

double limit(Func f, double a, double h = 1e-6) {
    return (f(a - h) + f(a + h)) * 0.5;
}

double derivative(Func f, double x, double h = 1e-5) {
    return (f(x + h) - f(x - h)) / (2 * h);
}

double nth_derivative(Func f, double x, int n, double h = 1e-4) {
    if (n == 0) return f(x);
    if (n == 1) return derivative(f, x, h);
    return (nth_derivative(f, x+h, n-1, h) - nth_derivative(f, x-h, n-1, h)) / (2*h);
}

double integrate_simpson(Func f, double a, double b, int n = 10000) {
    if (n % 2 == 1) n++;
    double h = (b - a) / n;
    double sum = f(a) + f(b);
    for (int i = 1; i < n; i++)
        sum += f(a + i*h) * (i%2 ? 4 : 2);
    return sum * h / 3.0;
}

Vec gradient(function<double(Vec)> f, Vec x, double h = 1e-5) {
    Vec g(x.size());
    for (int i = 0; i < x.size(); i++) {
        Vec x1 = x, x2 = x;
        x1[i] += h; x2[i] -= h;
        g[i] = (f(x1) - f(x2)) / (2*h);
    }
    return g;
}

double rk4(function<double(double,double)> f, double t, double y, double h) {
    double k1 = f(t, y);
    double k2 = f(t + h/2, y + h*k1/2);
    double k3 = f(t + h/2, y + h*k2/2);
    double k4 = f(t + h, y + h*k3);
    return y + h/6*(k1 + 2*k2 + 2*k3 + k4);
}

vector<Complex> dft(const vector<Complex>& a) {
    int n = a.size();
    vector<Complex> res(n);
    for (int k = 0; k < n; k++) {
        Complex sum = 0;
        for (int t = 0; t < n; t++) {
            double ang = -2 * PI * k * t / n;
            sum += a[t] * Complex(cos(ang), sin(ang));
        }
        res[k] = sum;
    }
    return res;
}

double newton_method(Func f, double x0, int iter = 50) {
    double x = x0;
    for (int i = 0; i < iter; i++) {
        double d = derivative(f, x);
        if (abs(d) < EPS) break;
        double d2 = nth_derivative(f, x, 2);
        x -= d / d2;
    }
    return x;
}

int main() {
    cout << fixed << setprecision(6);
    Func f = [](double x) { return x*x - sin(x); };
    cout << "极限 sin(x)/x, x→0: " << limit([](double x) { return sin(x)/x; }, 0) << endl;
    cout << "导数 f'(1): " << derivative(f, 1) << endl;
    cout << "积分 ∫₀¹ (x²-sinx) dx: " << integrate_simpson(f, 0, 1) << endl;
    cout << "牛顿法求零点: " << newton_method(f, 1.0) << endl;
    return 0;
}

十六、数学解析的顶级应用

  1. 物理引擎:刚体动力学 = 微分方程
  2. 图形学:光照、路径追踪 = 积分
  3. AI:反向传播 = 链式求导
  4. 信号处理:傅里叶变换
  5. 控制理论:拉普拉斯变换
  6. 金融:伊藤微积分、Black-Scholes 方程

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐