0
0
NumPydata~10 mins

Polynomial operations with np.poly in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a polynomial from roots using np.poly.

NumPy
import numpy as np
roots = [1, 2, 3]
poly_coeffs = np.[1](roots)
print(poly_coeffs)
Drag options to blanks, or click blank then click option'
Apoly
Bpoly1d
Croots
Dpolyfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.poly1d instead of np.poly, which creates a polynomial object, not coefficients.
Using np.roots which finds roots from coefficients, not the other way around.
2fill in blank
medium

Complete the code to evaluate the polynomial at x=2 using np.polyval.

NumPy
import numpy as np
coeffs = [1, -6, 11, -6]  # coefficients for (x-1)(x-2)(x-3)
value = np.[1](coeffs, 2)
print(value)
Drag options to blanks, or click blank then click option'
Apolyval
Broots
Cpoly
Dpolyfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.poly which returns coefficients, not evaluation.
Using np.roots which finds roots, not evaluates.
3fill in blank
hard

Fix the error in the code to multiply two polynomials using np.polymul.

NumPy
import numpy as np
p1 = [1, -1]  # x - 1
p2 = [1, -2]  # x - 2
product = np.[1](p1, p2)
print(product)
Drag options to blanks, or click blank then click option'
Apolyval
Bpolyfit
Cpolyadd
Dpolymul
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.polyadd which adds polynomials instead of multiplying.
Using np.polyval which evaluates polynomials, not multiply.
4fill in blank
hard

Fill both blanks to create a polynomial from roots and then find its roots.

NumPy
import numpy as np
roots = [4, 5, 6]
coeffs = np.[1](roots)
found_roots = np.[2](coeffs)
print(found_roots)
Drag options to blanks, or click blank then click option'
Apoly
Bpolyval
Croots
Dpolyfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.polyval instead of np.roots to find roots.
Using np.polyfit which fits data, not related here.
5fill in blank
hard

Fill all three blanks to create a polynomial from roots, evaluate it at x=3, and multiply by another polynomial.

NumPy
import numpy as np
roots = [1, 2]
p1 = np.[1](roots)
val = np.[2](p1, 3)
p2 = [1, -3]
product = np.[3](p1, p2)
print(val)
print(product)
Drag options to blanks, or click blank then click option'
Apoly
Bpolyval
Cpolymul
Droots
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up np.roots with np.polyval.
Using np.polyfit which is for fitting data, not polynomial operations.