简体   繁体   中英

Polynomials with Numpy

I am trying to write Bernstein polynomial on python, but I have a problem with poly1d function. In Bernstein function the variable is t , but in poly1d function default variable is x . In the following codes I call variable as t :

1: print((nCr(2, 0)*((np.poly1d([1, 0], variable="t"))**0)*((np.poly1d([-1, 1], variable="t"))**(2-0))))

2: print(np.poly1d([-1, 1], variable="t"))

But I got outputs like that:

1: 1 x^2 - 2 x + 1
 
2: -1 t + 1

How can I solve my problem for the first one?

np.poly1d() provides a variable argument just for printing purposes. It doesn't actually use it in operations.

For example:

>>> vars(np.poly1d([1, 2], variable='t'))
{'coeffs': array([1, 2]), '_variable': 't'}

# but:
>>> vars(1 * np.poly1d([1, 2], variable='t'))
{'coeffs': array([1, 2]), '_variable': 'x'}

So, I would recommend adding the variable bit as a last step:

pol = np.poly1d((nCr(2, 0) * ((np.poly1d([1, 0]))**0)*((np.poly1d([-1, 1]))**(2-0))), variable='t')
print(pol)
# out:
   2
1 t - 2 t + 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM