Bird
Raised Fist0
NumPydata~10 mins

Polynomial operations with np.poly in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Polynomial operations with np.poly
Define polynomial coefficients
↓
Create polynomial object with np.poly1d
↓
Perform operations: add, multiply, evaluate
↓
Get results: new polynomial coefficients or values
Start by defining polynomial coefficients, create polynomial objects, then perform operations like addition, multiplication, or evaluation to get new polynomials or values.
Execution Sample
NumPy
import numpy as np
p1 = np.poly1d([1, 2, 3])
p2 = np.poly1d([3, 4])
sum_poly = p1 + p2
prod_poly = p1 * p2
val = p1(2)
This code creates two polynomials, adds and multiplies them, and evaluates one at x=2.
Execution Table
StepActionPolynomial/ValueResult
1Create p1 with coefficients [1, 2, 3]p1p1 = 1*x^2 + 2*x + 3
2Create p2 with coefficients [3, 4]p2p2 = 3*x + 4
3Add p1 + p2sum_polysum_poly = 1*x^2 + 5*x + 7
4Multiply p1 * p2prod_polyprod_poly = 3*x^3 + 10*x^2 + 17*x + 12
5Evaluate p1 at x=2valval = 11
6End of operationsAll operations complete
💡 All polynomial operations done and evaluated.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
p1undefinedpoly1d([1, 2, 3])poly1d([1, 2, 3])poly1d([1, 2, 3])poly1d([1, 2, 3])poly1d([1, 2, 3])poly1d([1, 2, 3])
p2undefinedundefinedpoly1d([3, 4])poly1d([3, 4])poly1d([3, 4])poly1d([3, 4])poly1d([3, 4])
sum_polyundefinedundefinedundefinedpoly1d([1, 5, 7])poly1d([1, 5, 7])poly1d([1, 5, 7])poly1d([1, 5, 7])
prod_polyundefinedundefinedundefinedundefinedpoly1d([3, 10, 17, 12])poly1d([3, 10, 17, 12])poly1d([3, 10, 17, 12])
valundefinedundefinedundefinedundefinedundefined1111
Key Moments - 3 Insights
Why does adding p1 and p2 result in a polynomial of degree 2, not 3?
Because p1 is degree 2 and p2 is degree 1, adding them aligns terms by degree. The highest degree term comes from p1, so sum_poly remains degree 2 as shown in step 3 of the execution_table.
How does multiplication increase the polynomial degree?
Multiplying p1 (degree 2) by p2 (degree 1) adds their degrees, resulting in degree 3. This is shown in step 4 where prod_poly has degree 3.
What does p1(2) mean and how is it calculated?
p1(2) means evaluate polynomial p1 at x=2. It calculates 1*2^2 + 2*2 + 3 = 4 + 4 + 3 = 11, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the coefficient of x in sum_poly after step 3?
A2
B4
C5
D3
💡 Hint
Check the 'Polynomial/Value' column in step 3 of execution_table.
At which step does the polynomial degree increase to 3?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Result' column for degree info in execution_table rows.
If p1 was evaluated at x=3 instead of x=2, what would val be?
A12
B18
C15
D20
💡 Hint
Use the formula from key_moments step 3 with x=3.
Concept Snapshot
np.poly1d creates polynomial objects from coefficient lists.
You can add (+) and multiply (*) polynomials directly.
Evaluate polynomials by calling them like functions, e.g., p(2).
Addition aligns terms by degree; multiplication adds degrees.
Coefficients are ordered from highest degree to constant term.
Full Transcript
This visual execution shows how to use numpy's np.poly1d to work with polynomials. First, we define polynomials by their coefficients. Then, we create polynomial objects. We can add or multiply these polynomials, which changes their coefficients and degree accordingly. Finally, we can evaluate a polynomial at a specific value of x by calling it like a function. The execution table traces each step, showing how variables change and what results are produced. Key moments clarify common confusions about polynomial degree and evaluation. The quiz tests understanding by referencing the execution steps and variable states.

Practice

(1/5)
1. What does the np.poly function do in NumPy?
easy
A. It evaluates a polynomial at given points.
B. It calculates the derivative of a polynomial.
C. It finds the roots of a polynomial from coefficients.
D. It returns polynomial coefficients from given roots.

Solution

  1. Step 1: Understand the purpose of np.poly

    The function np.poly takes roots of a polynomial as input and returns the coefficients of the polynomial with those roots.
  2. Step 2: Differentiate from other polynomial functions

    Other functions like np.polyder find derivatives, np.polyval evaluates polynomials, and np.roots finds roots from coefficients, so they are different.
  3. Final Answer:

    It returns polynomial coefficients from given roots. -> Option D
  4. Quick Check:

    np.poly = coefficients from roots [OK]
Hint: Remember: np.poly converts roots to coefficients [OK]
Common Mistakes:
  • Confusing np.poly with np.roots
  • Thinking np.poly evaluates polynomials
  • Mixing up derivative functions with np.poly
2. Which of the following is the correct syntax to get polynomial coefficients from roots [-1, 2, 3] using NumPy?
easy
A. np.poly([-1, 2, 3])
B. np.polyroots([-1, 2, 3])
C. np.polyval([-1, 2, 3])
D. np.polycoef([-1, 2, 3])

Solution

  1. Step 1: Recall the correct function name

    The function to get polynomial coefficients from roots is np.poly.
  2. Step 2: Check the syntax

    The correct syntax is np.poly([-1, 2, 3]). Other options are not valid NumPy functions.
  3. Final Answer:

    np.poly([-1, 2, 3]) -> Option A
  4. Quick Check:

    Correct function is np.poly with roots list [OK]
Hint: Use np.poly with a list of roots inside parentheses [OK]
Common Mistakes:
  • Using non-existent functions like np.polyroots
  • Confusing np.polyval with np.poly
  • Misspelling function names
3. What is the output of the following code?
import numpy as np
coeffs = np.poly([1, 2])
print(coeffs)
medium
A. [1. 3. 2.]
B. [1. -3. 2.]
C. [1. -2. 1.]
D. [1. 2. -3.]

Solution

  1. Step 1: Understand np.poly output for roots [1, 2]

    np.poly returns coefficients of polynomial with roots 1 and 2, which is (x - 1)(x - 2) = x^2 - 3x + 2.
  2. Step 2: Write coefficients in descending order

    The coefficients are [1, -3, 2], representing 1*x^2 - 3*x + 2.
  3. Final Answer:

    [1. -3. 2.] -> Option B
  4. Quick Check:

    Roots [1,2] -> coeffs [1, -3, 2] [OK]
Hint: Multiply (x - root) terms and list coefficients [OK]
Common Mistakes:
  • Mixing signs of coefficients
  • Listing roots instead of coefficients
  • Confusing order of coefficients
4. The following code throws an error. What is the problem?
import numpy as np
coeffs = np.poly(3, 4, 5)
print(coeffs)
medium
A. roots should be a list or array, not a tuple without brackets
B. np.poly does not accept more than two roots
C. print statement syntax is incorrect
D. np.poly requires integer roots only

Solution

  1. Step 1: Check the input to np.poly

    The roots 3, 4, 5 are passed as separate positional arguments instead of a single sequence. np.poly expects one array-like input (list, array, or tuple).
  2. Step 2: Fix the roots input

    Wrap roots in brackets: coeffs = np.poly([3, 4, 5]) to pass a list to np.poly.
  3. Final Answer:

    roots should be a list or array, not a tuple without brackets -> Option A
  4. Quick Check:

    np.poly needs list/array input [OK]
Hint: Always pass roots as list or array to np.poly [OK]
Common Mistakes:
  • Passing tuple without brackets
  • Assuming np.poly limits root count
  • Misunderstanding print syntax errors
5. You have roots [-2, 0, 3] and want to create a polynomial with leading coefficient 2 instead of 1. How can you do this using np.poly?
hard
A. Add 2 to the coefficients returned by np.poly
B. Pass an extra argument 2 to np.poly as np.poly([-2, 0, 3], 2)
C. Multiply the output of np.poly([-2, 0, 3]) by 2
D. Use np.polyval with 2 as the first argument

Solution

  1. Step 1: Generate coefficients from roots

    Use np.poly([-2, 0, 3]) to get coefficients for polynomial with leading coefficient 1.
  2. Step 2: Scale coefficients to change leading coefficient

    Multiply all coefficients by 2 to make leading coefficient 2, since np.poly does not take leading coefficient as argument.
  3. Final Answer:

    Multiply the output of np.poly([-2, 0, 3]) by 2 -> Option C
  4. Quick Check:

    Scale coefficients to change leading term [OK]
Hint: Multiply coefficients array to adjust leading coefficient [OK]
Common Mistakes:
  • Trying to pass leading coefficient as np.poly argument
  • Adding instead of multiplying coefficients
  • Using np.polyval incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard