Generating special polynomials (Chebyshev, Hermite etc.)
Dear Numpy Users, I am trying to find out a way by which I can easily generate the n-th order "special" polynomial, where "special" could refer to Hermite, Chebyshev etc. Numpy 1.7 introduces several methods for such polynomials, but I couldn't find a convenience function that gives me a polynomial directly based on degree. For instance, I'd like: hermite(3) to result in array([ 0., -12., 0., 8.]) hermite(6) to result in array([-120., 0., 720., 0., -480., 0., 64.]) and so on. The quickest way I could come up with for this is: def hermite(n): if n <= 0: return numpy.array([1.0]) coeff_polynomial = [0.0] * n coeff_polynomial.extend([1]) return numpy.polynomial.hermite.herm2poly(coeff_polynomial) Now, if I am missing something, please let me know. If you think this is a useful feature, I volunteer to patch all the polynomial modules to generate such polynomials, if you could tell me appropriate function names for such convenience functions. Thanks! Kumar -- Kumar Appaiah
On Fri, Jun 14, 2013 at 08:59:03PM -0400, Kumar Appaiah wrote:
Dear Numpy Users,
I am trying to find out a way by which I can easily generate the n-th order "special" polynomial, where "special" could refer to Hermite, Chebyshev etc. Numpy 1.7 introduces several methods for such polynomials, but I couldn't find a convenience function that gives me a polynomial directly based on degree. For instance, I'd like:
hermite(3) to result in array([ 0., -12., 0., 8.]) hermite(6) to result in array([-120., 0., 720., 0., -480., 0., 64.]) and so on.
The quickest way I could come up with for this is:
def hermite(n): if n <= 0: return numpy.array([1.0])
I should technically raise a ValueError here if n is below 0, but I'll do the right thing in a patch if I am asked for one.
coeff_polynomial = [0.0] * n coeff_polynomial.extend([1]) return numpy.polynomial.hermite.herm2poly(coeff_polynomial)
Now, if I am missing something, please let me know. If you think this is a useful feature, I volunteer to patch all the polynomial modules to generate such polynomials, if you could tell me appropriate function names for such convenience functions.
Thanks! Kumar -- Kumar Appaiah
On Fri, Jun 14, 2013 at 6:59 PM, Kumar Appaiah
Dear Numpy Users,
I am trying to find out a way by which I can easily generate the n-th order "special" polynomial, where "special" could refer to Hermite, Chebyshev etc. Numpy 1.7 introduces several methods for such polynomials, but I couldn't find a convenience function that gives me a polynomial directly based on degree. For instance, I'd like:
hermite(3) to result in array([ 0., -12., 0., 8.]) hermite(6) to result in array([-120., 0., 720., 0., -480., 0., 64.]) and so on.
Generally that is a bad idea, polynomials tend to be numerically unstable and you lose all the virtue of the Hermite basis. However, you can do In [1]: from numpy.polynomial import Polynomial, Hermite In [2]: p = Hermite.basis(5) In [3]: p.convert(kind=Polynomial) Out[3]: Polynomial([ 0., 120., 0., -160., 0., 32.], [-1., 1.], [-1., 1.]) In [4]: Polynomial.cast(p) Out[4]: Polynomial([ 0., 120., 0., -160., 0., 32.], [-1., 1.], [-1., 1.]) In [5]: from numpy.polynomial import Chebyshev In [6]: Chebyshev.cast(p) Out[6]: Chebyshev([ 0., 20., 0., -30., 0., 2.], [-1., 1.], [-1., 1.]) Hmm, it should be possible to make the constructor take polynomials of different kinds since they all derive from PolyBase and can be detected. That could replace the cast method in a nice way.
The quickest way I could come up with for this is:
def hermite(n): if n <= 0: return numpy.array([1.0]) coeff_polynomial = [0.0] * n coeff_polynomial.extend([1]) return numpy.polynomial.hermite.herm2poly(coeff_polynomial)
Now, if I am missing something, please let me know. If you think this is a useful feature, I volunteer to patch all the polynomial modules to generate such polynomials, if you could tell me appropriate function names for such convenience functions.
Chuck
On Fri, Jun 14, 2013 at 08:07:57PM -0600, Charles R Harris wrote:
I am trying to find out a way by which I can easily generate the n-th order "special" polynomial, where "special" could refer to Hermite, Chebyshev etc. Numpy 1.7 introduces several methods for such polynomials, but I couldn't find a convenience function that gives me a polynomial directly based on degree. For instance, I'd like:
hermite(3) to result in array([ �0., -12., � 0., � 8.]) hermite(6) to result in array([-120., � �0., �720., � �0., -480., � �0., � 64.]) and so on.
Generally that is a bad idea, polynomials tend to be numerically unstable and you lose all the virtue of the Hermite basis. However, you can do
This may be true, but I am not concerned with the numerical instability in my current application. Nevertheless, I understand it if you don't want to introduce such a function.
In [1]: from numpy.polynomial import Polynomial, Hermite
In [2]: p = Hermite.basis(5)
In [3]: p.convert(kind=Polynomial) Out[3]: Polynomial([�� 0.,� 120.,��� 0., -160.,��� 0.,�� 32.], [-1.,� 1.], [-1.,� 1.])
In [4]: Polynomial.cast(p) Out[4]: Polynomial([�� 0.,� 120.,��� 0., -160.,��� 0.,�� 32.], [-1.,� 1.], [-1.,� 1.])
In [5]: from numpy.polynomial import Chebyshev
In [6]: Chebyshev.cast(p) Out[6]: Chebyshev([� 0.,� 20.,�� 0., -30.,�� 0.,�� 2.], [-1.,� 1.], [-1.,� 1.])
Hmm, it should be possible to make the constructor take polynomials of different kinds since they all derive from PolyBase and can be detected. That could replace the cast method in a nice way.
I now see that the polynomial structure is intended to be "rich", as opposed to the naïve function that I proposed. In the least, though, the documentation could reflect the example you gave me. I could send a patch that adds an example for each of the polynomial types in the documentation, much like yours, if that would be useful. Thanks for the clarification. Kumar -- Kumar Appaiah
On Sat, Jun 15, 2013 at 12:29:11AM -0400, Kumar Appaiah wrote:
I now see that the polynomial structure is intended to be "rich", as opposed to the naïve function that I proposed. In the least, though, the documentation could reflect the example you gave me. I could send a patch that adds an example for each of the polynomial types in the documentation, much like yours, if that would be useful.
Upon checking the latest numpy documentation, I found a hint for this here: http://docs.scipy.org/doc/numpy/reference/routines.polynomials.classes.html#... The documentation, when read as a whole, looks clear. I was complaining earlier because I was referring to pre-1.7 documentation, and hadn't familiarized myself with the new polynomial interface as well. Sorry for the noise. I'll bother you the list again if I run into additional trouble. Thanks! Kumar -- Kumar Appaiah
participants (2)
-
Charles R Harris
-
Kumar Appaiah