[英]Simplify algorithm with Horner Scheme
I have a problem to solve the following excercise: 我有一个问题要解决以下练习:
*Be given the special polynomial: *给出特殊多项式:
and the input: coefficients a[n], a[n-1], ..., a[0], argument x
输入:系数a [n],a [n-1],...,a [0],参数x
Create an algorithm in C# or Pseudocode which will use Horner's method to solve the special polynomial for x.* 在C#或Pseudocode中创建一个算法,它将使用Horner的方法来求解x的特殊多项式。*
I created an algorithm to solve default polynomial functions with Horner's method, but it doesn't work for the special function, because the exponents are squared. 我创建了一个用Horner方法解决默认多项式函数的算法,但它不适用于特殊函数,因为指数是平方的。 I don't know how to modify the algorithm to respect the squared exponents, because as far as I know, Horner's method doesn't use exponents.
我不知道如何修改算法以尊重平方指数,因为据我所知,Horner的方法不使用指数。 This is my code:
这是我的代码:
int[] a = new int[] { 0, 3, 2, 1};//a[0] - a[n]
int n = 3;
int x = 2;
double r = a[n];
for (int i = n - 1; i >= 0; i--)
{
r = r * x + a[i];
}
Console.WriteLine(r);
I'm thankful for any help! 我很感谢你的帮助!
4*4 = 1 + 3 + 5 + 7
4 * 4 = 1 + 3 + 5 + 7
x^(4*4) = x^1 * x^3 * x^5 * x^7
x ^(4 * 4)= x ^ 1 * x ^ 3 * x ^ 5 * x ^ 7
a(4)*x^(4*4) + a(3)*x^(3*3) + a(2)*x^(2*2) + a(1)*x + a(0) = (((a(4)*x^7 + a(3)) * x^5 + a(2) ) * x^3 + a(1) ) * x^1 + a(0)
a(4)* x ^(4 * 4)+ a(3)* x ^(3 * 3)+ a(2)* x ^(2 * 2)+ a(1)* x + a(0) =(((a(4)* x ^ 7 + a(3))* x ^ 5 + a(2))* x ^ 3 + a(1))* x ^ 1 + a(0)
You can keep track of the odd powers of x by multiplying the previous odd power by x^2 on each iteration
您可以通过在每次迭代时将先前的奇数幂乘以x ^ 2来跟踪x的奇数幂
Let me revisit the blunt way and Horner's method to evaluate a polynomial aₙxⁿ+…+a₂x²+a₁x+a₀ in a single variable x at a given value x₀ : 让我重新考虑一下钝方法和Horner的方法来评估给定值x 0的单个变量x中的多项式aₙxⁿ+ ... + a2x2 + a1x + a0:
Horner's method saves work because the powers x₀ has to be raised to are (much) lower and (, for this very reason,) fewer (down to only needing/using x₀ ). 霍纳的方法可以节省工作量,因为功率x 0必须提高到(更低)和(因为这个原因)更少(降低到只需要/使用x 0 )。
How do you save work evaluating a polynomial with square exponents ? 如何使用平方指数保存评估多项式的工作?
re-use lesser powers in the computation of greater ones. 在计算更大的权力时重复使用较小的权力。
just as neighbouring squares differ by and odd number two greater that the next lower pair, neighbouring square powers are an odd power apart
正如相邻正方形相差和奇数2大于下一个较低的一对,相邻的正方形权力的奇次幂开
the next odd power is the current one multiplied by the square
下一个奇数功率是当前的一个乘以平方
evaluate from "highest coefficient" to lowest 评估从“最高系数”到最低
have power evaluation memoized or keep powers (used) explicitly.
有权力评估备忘或明确保持权力(使用)。
It may be easiest to set them up upfront.最先设置它们可能是最简单的。
Is the combination of tricks (still) Horner's method ? 是技巧 (仍然) 霍纳的方法的组合 ?
You decide. 你决定。 (And your tutor/teacher/interviewer as applicable.)
(以及您的导师/老师/面试官。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.