Hướng dẫn how do you input a polynomial in python? - làm thế nào để bạn nhập một đa thức trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3
    Input: poly[] = {2, -6, 2, -1}, x = 3
    Output: 5
    
    # Evaluate value of 2x3 + 3x + 1 for x = 2
    Input: poly[] = {2, 0, 3, 1}, x = 2
    Output: 23
    
    # Evaluate value of 2x + 5 for x = 5
    Input: poly[] = {2, 5}, x = 5
    Output: 15

    Bàn luận

    Hướng dẫn how do you input a polynomial in python? - làm thế nào để bạn nhập một đa thức trong python?

    Bài viết sau đây chứa các chương trình để tính toán một phương trình đa thức cho rằng các hệ số của đa thức được lưu trữ trong một danh sách.

    Phương trình sẽ thuộc loại:

    • Chúng tôi sẽ được cung cấp giá trị của biến và chúng tôi phải tính toán giá trị của đa thức tại thời điểm đó. Để làm như vậy chúng tôi có hai cách tiếp cận.Using for loop to compute the value.
    • Cách tiếp cậnUsing Horner’s Method for computing the value.

    Phương pháp ngây thơ: Sử dụng cho vòng lặp để tính toán giá trị.

    Phương pháp tối ưu hóa: Sử dụng phương pháp Horner, để tính toán giá trị.

    • Phương pháp ngây thơ:cn will be multiplied with xn
    • Trong phương pháp này, phương pháp sau đây sẽ được tuân thủ. Đây là cách tiếp cận ngây thơ nhất để làm những câu hỏi như vậy.cn-1 will be multiplied with xn-1
    • Hệ số đầu tiên CN sẽ được nhân với XN
    • Sau đó, hệ số CN-1 sẽ được nhân với XN-1

    Example:

    Python3

    Các kết quả được tạo ra trong hai bước trên sẽ được thêm

    Điều này sẽ tiếp tục cho đến khi tất cả các hệ số được bảo hiểm.

    poly = [2,

    5
    0____11, 2, ____101016
    5
    7

    5
    8=
    Value of polynomial is: 5
    0

    Value of polynomial is: 5
    1=
    Value of polynomial is: 5
    3
    Value of polynomial is: 5
    4

    Value of polynomial is: 5
    5=
    Value of polynomial is: 5
    7

    Value of polynomial is: 5
    8
    Value of polynomial is: 5
    9
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    0
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    1
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    2

    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    3
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    4 =
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    6

    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    3
    Value of polynomial is: 5
    8
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    9
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    0
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    1__

    =0=1

    Output:

    5

     
    Case 1:
    Enter the coefficients of the form ax^3 + bx^2 + cx + d
    Enter coefficient:3
    Enter coefficient:4
    Enter coefficient:5
    Enter coefficient:6
    Enter the value of x:2
    The value of the polynomial is: 56.0
     
    Case 2:
    Enter the coefficients of the form ax^3 + bx^2 + cx + d
    Enter coefficient:2
    Enter coefficient:5
    Enter coefficient:6
    Enter coefficient:3
    Enter the value of x:1
    The value of the polynomial is: 16.0
    8
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    4 =
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    4 poly 2 poly 3
    O(n2)

    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    3
    Value of polynomial is: 5
    5=
    Value of polynomial is: 5
    5poly 8
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    4

    Độ phức tạp về thời gian: O (N2)

    Python3

    Phương pháp được tối ưu hóa:

    Phương pháp Horner có thể được sử dụng để đánh giá đa thức trong thời gian O (N). Để hiểu phương pháp, chúng ta hãy xem xét ví dụ về 2x3 - 6x2 + 2x - 1. đa thức có thể được đánh giá là ((2x - 6) x + 2) x - 1. Ý tưởng là khởi tạo kết quả là hệ số của XN Đó là 2 trong trường hợp này, nhiều lần nhân kết quả với X và thêm hệ số tiếp theo để kết quả. Cuối cùng, trả về kết quả. & Nbsp;

    =2 =3

    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    3
    Value of polynomial is: 5
    5= =7
    Value of polynomial is: 5
    7
    5
    7

    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    3
    Value of polynomial is: 5
    8
    Value of polynomial is: 5
    9
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    0
    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)
    1[5
    5
    6[7

    Các kết quả được tạo ra trong hai bước trên sẽ được thêm

    Điều này sẽ tiếp tục cho đến khi tất cả các hệ số được bảo hiểm.

    poly = [2,

    5
    0____11, 2, ____101016
    5
    7

    =0[5

    5
    11
    5
    12

    Output:

    Value of polynomial is: 5

    5
    8=
    Value of polynomial is: 5
    0
    O(n)


    Đây là một chương trình Python để tính toán một phương trình đa thức cho rằng các hệ số của đa thức được lưu trữ trong danh sách.

    Mô tả vấn đề

    Chương trình lấy các hệ số của phương trình đa thức và giá trị của X và cho giá trị của đa thức.

    Giải pháp vấn đề

    1. Nhập mô -đun toán học. 2. Lấy các hệ số của phương trình đa thức và lưu trữ nó trong danh sách. 3. Lấy giá trị của x. 4. Sử dụng một vòng lặp và trong khi vòng lặp để tính toán giá trị của biểu thức đa thức cho ba thuật ngữ đầu tiên và lưu trữ nó trong một biến tổng. 5. Thêm thuật ngữ thứ tư vào biến tổng. 6. In giá trị tính toán. 7. Thoát.
    2. Take in the coefficients of the polynomial equation and store it in a list.
    3. Take in the value of x.
    4. Use a for loop and while loop to compute the value of the polynomial expression for the first three terms and store it in a sum variable.
    5. Add the fourth term to the sum variable.
    6. Print the computed value.
    7. Exit.

    Chương trình/mã nguồn

    Dưới đây là mã nguồn của chương trình Python để tính toán một phương trình đa thức được đưa ra rằng các hệ số của đa thức được lưu trữ trong một danh sách. Đầu ra chương trình cũng được hiển thị dưới đây.

    import math
    print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
    lst=[]
    for i in range(0,4):
        a=int(input("Enter coefficient:"))
        lst.append(a)
    x=int(input("Enter the value of x:"))
    sum1=0
    j=3
    for i in range(0,3):
        while(j>0):
            sum1=sum1+(lst[i]*math.pow(x,j))
            break
        j=j-1
    sum1=sum1+lst[3]
    print("The value of the polynomial is:",sum1)

    Giải thích chương trình

    1. Mô -đun toán học được nhập. 2. Người dùng phải nhập các hệ số của đa thức được lưu trữ trong danh sách. 3. Người dùng cũng phải nhập giá trị của x. 4. Giá trị của I nằm trong khoảng từ 0 đến 2 bằng cách sử dụng vòng lặp For được sử dụng để truy cập các hệ số trong danh sách. 5. Giá trị của J nằm trong khoảng từ 3 đến 1, được sử dụng để xác định công suất cho giá trị của x. 6. Giá trị của ba thuật ngữ đầu tiên được tính toán theo cách này. 7. Thuật ngữ cuối cùng được thêm vào tổng cuối cùng. 8. Giá trị tính toán cuối cùng được in.
    2. User must enter the coefficients of the polynomial which is stored in a list.
    3. User must also enter the value of x.
    4. The value of i ranges from 0 to 2 using the for loop which is used to access the coefficients in the list.
    5. The value of j ranges from 3 to 1, which is used to determine the power for the value of x.
    6. The value of the first three terms is computed this way.
    7. The last term is added to the final sum.
    8. The final computed value is printed.

    Trường hợp kiểm tra thời gian chạy

     
    Case 1:
    Enter the coefficients of the form ax^3 + bx^2 + cx + d
    Enter coefficient:3
    Enter coefficient:4
    Enter coefficient:5
    Enter coefficient:6
    Enter the value of x:2
    The value of the polynomial is: 56.0
     
    Case 2:
    Enter the coefficients of the form ax^3 + bx^2 + cx + d
    Enter coefficient:2
    Enter coefficient:5
    Enter coefficient:6
    Enter coefficient:3
    Enter the value of x:1
    The value of the polynomial is: 16.0

    Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Chương trình Python.

    Để thực hành tất cả các chương trình Python, đây là bộ hoàn thành hơn 150 vấn đề và giải pháp Python.

    Bước tiếp theo:

    • Nhận Giấy chứng nhận miễn phí trong chương trình Python
    • Tham gia cuộc thi chứng nhận lập trình Python
    • Trở thành một người xếp hạng hàng đầu trong chương trình Python
    • Thực hiện các bài kiểm tra lập trình Python
    • Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
    • Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,

    Hướng dẫn how do you input a polynomial in python? - làm thế nào để bạn nhập một đa thức trong python?

    Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

    Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses.

    Làm thế nào để bạn viết một hàm đa thức trong Python?

    Viết hàm python eval_polynomial (p, x) trả về giá trị của p (x), trong đó p là đa thức được biểu thị bằng danh sách các hệ số của nó p.Ví dụ: eval_polynomial ([1, 0, 3], 2) sẽ trả về 1*2^2 + 0*2 + 3 = 7. Sử dụng một vòng trong khi vòng lặp.eval_polynomial(p, x) that returns the value of P(x) , where P is the polynomial represented by the list of its coefficients p . For example, eval_polynomial([1, 0, 3], 2) should return 1*2^2 + 0*2 + 3 = 7. Use a single while loop.

    Làm thế nào để bạn thêm đa thức trong numpy?

    Đa thức P (x) = C3 x2 + C2 x + C1 được biểu diễn trong Numpy là: (C1, C2, C3) {Các hệ số (hằng số)}.Hãy lấy hai đa thức p (x) và q (x) sau đó thêm chúng để nhận r (x) = p (x) + q (x) là kết quả của việc bổ sung hai đa thức đầu vào.3 x2 + C2 x + C1 is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}. Let take two polynomials p(x) and q(x) then add these to get r(x) = p(x) + q(x) as a result of addition of two input polynomials.