You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Lagrange多项式推导为标准多项式形式?

将拉格朗日因式形式多项式转换为标准形式

要把拉格朗日多项式从因式形式转换成a_{n-1}x^{n-1} + a_{n-2}x^{n-2} + ... + a_0的标准形式,核心是计算每个项的多项式系数并合并,而不是拼接字符串。以下是修改后的完整C++实现:

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

// 多项式乘以(x - c),更新系数
void multiplyByLinearTerm(vector<double>& coeff, double c) {
    // 扩展一位,因为乘以x后次数+1
    coeff.push_back(0.0);
    // 从高次到低次更新,避免覆盖未计算的值
    for (int i = coeff.size() - 1; i > 0; --i) {
        coeff[i] = coeff[i-1] - c * coeff[i];
    }
    coeff[0] = -c * coeff[0];
}

// 计算拉格朗日多项式的标准形式系数
vector<double> lagrangeCoefficients(vector<pair<double, double>>& points) {
    int n = points.size();
    // 总系数,初始为0次多项式(常数0)
    vector<double> totalCoeff(n, 0.0);

    for (int i = 0; i < n; ++i) {
        double xi = points[i].first;
        double yi = points[i].second;

        // 初始化L_i(x)的系数为1(常数项1)
        vector<double> liCoeff = {1.0};
        double denominator = 1.0;

        // 构造L_i(x)的因式部分:乘积(x - x_j)/(xi - x_j),j≠i
        for (int j = 0; j < n; ++j) {
            if (j != i) {
                double xj = points[j].first;
                multiplyByLinearTerm(liCoeff, xj);
                denominator *= (xi - xj);
            }
        }

        // 除以分母,再乘以yi
        double scale = yi / denominator;
        for (double& c : liCoeff) {
            c *= scale;
        }

        // 合并到总系数中
        for (int k = 0; k < liCoeff.size(); ++k) {
            totalCoeff[k] += liCoeff[k];
        }
    }

    return totalCoeff;
}

// 将系数转换为标准形式的字符串
string coeffToString(const vector<double>& coeff) {
    string result;
    int degree = coeff.size() - 1;

    for (int i = degree; i >= 0; --i) {
        double c = coeff[i];
        // 忽略浮点误差导致的极小系数
        if (abs(c) < 1e-9) continue;

        // 处理正负号
        if (!result.empty()) {
            if (c > 0) {
                result += " + ";
            } else {
                result += " - ";
                c = abs(c);
            }
        } else {
            if (c < 0) {
                result += "-";
                c = abs(c);
            }
        }

        // 处理系数值:系数为1且非常数项时省略1
        if (abs(c - 1.0) > 1e-9 || i == 0) {
            // 保留两位小数优化显示,可按需调整
            char buffer[20];
            sprintf(buffer, "%.2f", c);
            result += buffer;
        }

        // 处理x的次数
        if (i > 0) {
            result += "x";
            if (i > 1) {
                result += "^" + to_string(i);
            }
        }
    }

    // 全零多项式的情况
    return result.empty() ? "0" : result;
}

int main() {
    int n;
    cout << "Enter the number of points: ";
    cin >> n;

    vector<pair<double, double>> points;
    cout << "Enter the dots in the format x y:\n";
    for (int i = 0; i < n; ++i) {
        double x, y;
        cin >> x >> y;
        points.emplace_back(x, y);
    }

    vector<double> coeff = lagrangeCoefficients(points);
    string polyStr = coeffToString(coeff);
    cout << "The Lagrange polynomial in standard form:\n" << polyStr << endl;

    return 0;
}

关键实现说明

  • 多项式表示:用vector<double>存储系数,索引k对应x^k的系数,比如[1,2,3]代表1 + 2x + 3x²。
  • 线性项乘法:multiplyByLinearTerm函数处理多项式与(x - c)的乘法,从高次到低次更新系数,避免覆盖未计算的中间值。
  • 基多项式计算:对每个点构造对应的拉格朗日基多项式,先乘所有(x - x_j)项,再除以分母常数,最后乘以y_i。
  • 系数合并:将每个基多项式的系数累加,得到最终的标准多项式系数。
  • 字符串格式化:coeffToString函数自动处理正负号、系数为1的省略规则、次数显示,同时过滤浮点误差导致的极小系数。

测试示例

输入3个点:

3
0 1
1 3
2 5

输出结果:

The Lagrange polynomial in standard form:
2.00x + 1.00

内容的提问来源于stack exchange,提问作者Hj Hj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 16:30:54