I have these problems about polynomials and I've spent about 4 hours on this, but I just can't get it. I'm new to Python and programming and I've tried working it out on paper, but I just don't know.
Write and test a Python function negate(p)
that negates the polynomial represented by the list of its coeffeicients p
and returns a new polynomial (represented as a list). In other words, write a function that makes the list of numbers negative.
Write a Python function 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.
Write and test a function multiply_by_one_term(p, a, k)
that multiplies a given polynomial p
, represented by a list of coefficients, by ax^k
and returns the product as a new list.
I would really appreciate it if someone could help me.
If a polynomial of lowest degree p has zeros at x=x1,x2,…,xn x = x 1 , x 2 , … , x n , then the polynomial can be written in the factored form: f(x)=a(x−x1)p1(x−x2)p2⋯(x−xn)pn f ( x ) = a ( x − x 1 ) p 1 ( x − x 2 ) p 2 ⋯ ( x − x n ) p n where the powers pi on each factor can be determined by the behavior of the graph ...
A polynomial function is a function that involves only non-negative integer powers or only positive integer exponents of a variable in an equation like the quadratic equation, cubic equation, etc. For example, 2x+5 is a polynomial that has exponent equal to 1.
To solve a polynomial equation, first write it in standard form. Once it is equal to zero, factor it and then set each variable factor equal to zero. The solutions to the resulting equations are the solutions to the original. Not all polynomial equations can be solved by factoring.
A polynomial function is a function such as a quadratic, a cubic, a quartic, and so on, involving only non-negative integer powers of x.
I'd recommend using numpy.poly1d
and numpy.polymul
, where the coefficients are a0*x2 + a1*x + a2
.
For example, to represent 3*x**2 + 2*x + 1
:
p1 = numpy.poly1d([3,2,1])
And with the resulting poly1d
object you can operate using *
, /
and so on...:
print(p1*p1)
# 4 3 2
#9 x + 12 x + 10 x + 4 x + 1
If you want to build your own functions, assuming that p contains the coefficients in order: a0 + a1*x + a2*x**2 + ...
:
def eval_polynomial(p,x):
return sum((a*x**i for i,a in enumerate(p)))
def multiply_by_one_term(p, a, k):
return [0]*k + [a*i for i in p]
My evaluate function uses exponentials, which can be avoided with Horner's rule, as posted in another answer, which is available in Numpy's polyval
function
For polynomials, you should consider Horner's Method. Its main feature is that computing a polynomial of order N requires only N multiplies and N additions -- no exponentials:
def eval_polynomial(P, x):
'''
Compute polynomial P(x) where P is a vector of coefficients, highest
order coefficient at P[0]. Uses Horner's Method.
'''
result = 0
for coeff in P:
result = x * result + coeff
return result
>>> eval_poly([1, 0, 3], 2)
7
You can work through it by hand, or follow the link to see how it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With