rauf
rauf

Reputation: 43

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?

Upvotes: 0

Views: 269

Answers (1)

Pierre D
Pierre D

Reputation: 26211

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

Upvotes: 1

Related Questions