[英]Find the coefficients of a polynomial given its roots (zeros) in Prolog
How I can implement in Prolog program that find coefficients of the polynomial if I know its roots. 如果我知道多项式的根,如何在Prolog程序中实现找到多项式系数的方法。
for example: 例如:
input data (2;-1)
output (1;-1;2)
Multiplying together the first-degree factors for given roots will form an expanded polynomial. 将给定根的一阶因子相乘将形成一个扩展的多项式。 This is a natural fit with an "accumulator" design pattern in Prolog.
这与Prolog中的“累加器”设计模式自然契合。
That is, we'll introduce an auxiliary argument to remember the product of factors "so far" dealt with. 也就是说,我们将引入一个辅助参数来记住“到目前为止”所处理的因素的乘积。 Once the list of specified roots has been emptied, then we will have the desired polynomial expansion:
一旦指定根的列表被清空,我们将具有所需的多项式展开式:
/* polynomialFromListofRoots(ListofRoots,[1],Polynomial) */
polynomialFromListofRoots([ ],Poly,Poly).
polynomialFromListofRoots([R|Roots],Pnow,Poly) :-
polyMultiplyRootFactor(R,Pnow,Pnew),
polynomialFromListofRoots(Roots,Pnew,Poly).
/* polyMultiplyRootFactor(R,Poly,ProductXminusR) */
polyMultiplyRootFactor(R,Poly,Prod) :-
polyMultiplyRootFactorAux(R,0,Poly,Prod).
/* polyMultiplyRootFactorAux(R,Aux,Poly,Product) */
polyMultiplyRootFactorAux(R,A,[ ],[B]) :-
B is -R*A.
polyMultiplyRootFactorAux(R,A,[P|Ps],[RP|RPs]) :-
RP is P - R*A,
polyMultiplyRootFactorAux(R,P,Ps,RPs).
Using the example in the Question: 使用问题中的示例:
?- polynomialFromListofRoots([2,-1],[1],Poly).
Poly = [1, -1, -2]
yes
Note that this corrects the output
claimed in the Question. 请注意,这可以纠正问题中要求的
output
。
Sorry misread question. 抱歉误读了问题。
a^2x + bx +c = 0 a ^ 2x + bx + c = 0
Take the sum of the roots x1 + x2 this is equal to -b/a. 取根x1 + x2的总和等于-b / a。
Take the product of the roots x1*x2 this is equal to c/a. 取根x1 * x2的乘积等于c / a。
Now solve the resulting system of linear equations to find ab and c. 现在求解线性方程组的结果系统以找到ab和c。
Edit: 编辑:
The above solution works if you set the parameter of a = 1. You see when your given the roots you'll end up with two equations and three unknowns so you'll have to set a fixed value on one of the parameters and the above solutions fixes a = 1. 如果您设置a = 1的参数,上述解决方案将起作用。您会看到,当给定根时,您将得到两个方程和三个未知数,因此您必须在其中一个参数和上面的参数上设置一个固定值解决方案修复了a = 1。
So given 2 roots you can't get back a specific polynomial because theres no unique answer theres an infinte number of answers 因此,给定2个根,您将无法取回特定的多项式,因为没有唯一的答案,而答案的数量是无限的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.