📜  维埃塔的公式

📅  最后修改于: 2021-05-04 09:40:27             🧑  作者: Mango

Vieta的公式将多项式系数与其根的和与乘积以及成组的根的乘积相关联。 Vieta的公式描述了多项式的根与其系数的关系。考虑以下示例,查找具有给定根的多项式。 (仅讨论实值多项式,即多项式的系数为实数)。让我们采用二次多项式。有两个真实的根源r_{1}r_{2} ,然后找到一个多项式。

考虑多项式a_{2}x^{2} + a_{1}x + a_{0} 。给定根源,我们也可以将其写为

k(x - r_{1})(x - r_{2})

由于两个方程式代表相同的多项式,因此将两个多项式均等

a_{2}x^{2} + a_{1}x + a_{0} = k(x - r_{1})(x - r_{2})

简化上面的方程,我们得到

a_{2}x^{2} + a_{1}x + a_{0} = kx^{2}-k(r_{1} + r_{2})x + k(r_{1}r_{2})

比较双方的系数,我们得到

为了x^{2}a_{2} = k

为了xa_{1} = -k(r_{1} + r_{2})

从长期来看, a_{0} = kr_{1}r_{2}

这使,

a_{2} = k

\frac{a_{1}}{a_{2}} = -(r_{1} + r_{2})........(1)

\frac{a_{0}}{a_{2}} = r_{1}r_{2}..................(2)

方程(1)和(2)被称为二次多项式的维埃塔公式。
一般来说,对于nth多项式,有n个不同的维埃塔公式。它们可以简写为

为了0 \leq k \leq n

 \sum_{1 \leq i_{1} < i_{2} < ... < i_{k} \leq n} (r_{i_{1}}r_{i_{2}} ... r_{i_{k}}) = (-1)^{k}\frac{a_{n-k}}{a_{n}}.

以下示例说明了如何使用Vieta公式解决问题。

例子:

Input : n = 2
        roots = {-3, 2}
Output : Polynomial coefficients: 1, 1, -5

Input : n = 4
        roots = {-1, 2, -3, 7}
Output : Polynomial coefficients: 1, -5, -19, 29, 42
C++
// C++ program to implement vieta formula 
// to calculate polynomial coefficients.
#include 
using namespace std;
  
// Function to calculate polynomial 
// coefficients.
void vietaFormula(int roots[], int n)
{
    // Declare an array for
    // polynomial coefficient.
    int coeff[n + 1];
  
    // Set all coefficients as zero initially
    memset(coeff, 0, sizeof(coeff));
      
    // Set highest order coefficient as 1
    coeff[n] = 1;
  
    for (int i = 1; i <= n; i++) {
        for (int j = n - i - 1; j < n; j++) {
            coeff[j] = coeff[j] + (-1) * 
                roots[i - 1] * coeff[j + 1];
        }
    }
  
    cout << "Polynomial Coefficients: ";
    for (int i = n; i >= 0; i--) {
        cout << coeff[i] << " ";
    }
}
  
// Driver code
int main()
{
    // Degree of required polynomial
    int n = 4;
      
    // Initialise an array by
    // root of polynomial
    int roots[] = { -1, 2, -3, 7 };
      
    // Function call
    vietaFormula(roots, n);
      
    return 0;
}


Java
// Java program to implement vieta formula 
// to calculate polynomial coefficients.
import java.util.Arrays;
  
class GFG
{
  
// Function to calculate polynomial 
// coefficients.
static void vietaFormula(int roots[], int n)
{
    // Declare an array for
    // polynomial coefficient.
    int coeff[] = new int[++n + 1];
    Arrays.fill(coeff, 0);
      
    // Set highest order coefficient as 1
    coeff[n] = 1;
  
    for (int i = 1; i  0; i--) 
    {
        System.out.print(coeff[i] + " ");
    }
}
  
// Driver code
public static void main(String[] args)
{
    // Degree of required polynomial
    int n = 4;
      
    // Initialise an array by
    // root of polynomial
    int roots[] = { -1, 2, -3, 7 };
      
    // Function call
    vietaFormula(roots, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 program to implement 
# Vieta's formula to calculate
# polynomial coefficients.
def vietaFormula(roots, n):
      
    # Declare an array for
    # polynomial coefficient.
    coeff = [0] * (n + 1)
      
    # Set Highest Order 
    # Coefficient as 1
    coeff[n] = 1
    for i in range(1, n + 1):
        for j in range(n - i - 1, n):
            coeff[j] += ((-1) * roots[i - 1] *
                                coeff[j + 1])
      
    # Reverse Array 
    coeff = coeff[::-1] 
      
    print("Polynomial Coefficients : ", end = "")
      
    # Print Coefficients
    for i in coeff: 
        print(i, end = " ")
    print()
  
# Driver Code
if __name__ == "__main__":
      
    # Degree of Polynomial
    n = 4
      
    # Initialise an array by
    # root of polynomial
    roots = [-1, 2, -3, 7] 
      
    # Function call
    vietaFormula(roots, n)
      
# This code is contributed
# by Arihant Joshi


C#
// C# program to implement vieta formula 
// to calculate polynomial coefficients.
using System;
      
class GFG
{
  
// Function to calculate polynomial 
// coefficients.
static void vietaFormula(int []roots, int n)
{
    // Declare an array for
    // polynomial coefficient.
    int []coeff = new int[++n + 1];
      
    // Set highest order coefficient as 1
    coeff[n] = 1;
  
    for (int i = 1; i  0; i--) 
    {
        Console.Write(coeff[i] + " ");
    }
}
  
// Driver code
public static void Main(String[] args)
{
    // Degree of required polynomial
    int n = 4;
      
    // Initialise an array by
    // root of polynomial
    int []roots = { -1, 2, -3, 7 };
      
    // Function call
    vietaFormula(roots, n);
}
}
  
// This code has been contributed by 29AjayKumar


PHP
= 0; $i--) 
    {
        echo $coeff[$i]. " ";
    }
}
  
// Driver code
  
// Degree of required polynomial
$n = 4;
  
// Initialise an array by
// root of polynomial
$roots = array(-1, 2, -3, 7);
  
// Function call
vietaFormula($roots, $n);
  
// This code is contributed by mits
?>


输出:
Polynomial Coefficients: 1 -5 -19 29 42 

时间复杂度: \mathcal{O}(n^{2})