0
0
NumPydata~10 mins

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

Choose your learning style9 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.