从根开始计算多项式系数的最佳方法是什么?

时间:2015-10-04 10:01:48

标签: matlab polynomials

poly函数以多项式的根作为输入,并返回多项式的系数。什么是基础算法?

有没有更好的方法从其根计算多项式的系数?

2 个答案:

答案 0 :(得分:2)

请注意:

  • 要获得多项式 p x )(具有领先系数1),您需要乘以所有条款表格 x - r ,其中 r 是根。 (根据其多重性,应多次考虑多个根。)
  • 多项式的乘法等价于卷积。事实上,documentation of conv

      

    w = conv(u,v)会返回向量uv卷积。如果uv是多项式系数的向量,则对它们进行卷积相当于将两个多项式相乘。

所以:

roots = [1 -2 3]; %// input (roots)
result = 1;  %// initiallize
for r = roots
    result = conv(result, [1 -r]);
end

在此示例中,结果为

result =
     1    -2    -5     6

poly返回的矢量相同。

或者,您可以手动进行卷积

roots = [1 -2 3]; %// input (roots)
result = 1;  %// initiallize
for r = roots
    result = [result 0] + [0 -r*result];
end

这相当于以下带有显式循环的代码,它应该更接近C ++实现:

roots = [1 -2 3]; %// input (roots)
result = nan(1,numel(roots)+1);  %// preallocate. Values will be overwritten
result(1) = 1; %// initiallize
for n = 1:numel(roots)
    result(n+1) = -roots(n)*result(n); %// update from right to left, due to overwriting
    for k = n:-1:2
        result(k) = result(k) - roots(n)*result(k-1);
    end
end

答案 1 :(得分:2)

因为你要求c ++答案。 典型的实现看起来像。

std::vector<int> poly( std::vector<int> roots )
{
    std::vector<int> result{1};
    for( size_t i = 0 ; i < roots.size() ; ++i)
    {
        std::vector<int> temp{result.begin(),result.end()};

        for(auto & item : temp )
            item *= ( -roots[i] );

        result.push_back(0);
        for( size_t j = 1 ; j < result.size() ; ++j)
        {
            result[j] += temp[j-1];
        }
    }
    return result;
}

希望有所帮助