__init__.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """
  2. A sub-package for efficiently dealing with polynomials.
  3. Within the documentation for this sub-package, a "finite power series,"
  4. i.e., a polynomial (also referred to simply as a "series") is represented
  5. by a 1-D numpy array of the polynomial's coefficients, ordered from lowest
  6. order term to highest. For example, array([1,2,3]) represents
  7. ``P_0 + 2*P_1 + 3*P_2``, where P_n is the n-th order basis polynomial
  8. applicable to the specific module in question, e.g., `polynomial` (which
  9. "wraps" the "standard" basis) or `chebyshev`. For optimal performance,
  10. all operations on polynomials, including evaluation at an argument, are
  11. implemented as operations on the coefficients. Additional (module-specific)
  12. information can be found in the docstring for the module of interest.
  13. This package provides *convenience classes* for each of six different kinds
  14. of polynomials:
  15. ============ ================
  16. **Name** **Provides**
  17. ============ ================
  18. Polynomial Power series
  19. Chebyshev Chebyshev series
  20. Legendre Legendre series
  21. Laguerre Laguerre series
  22. Hermite Hermite series
  23. HermiteE HermiteE series
  24. ============ ================
  25. These *convenience classes* provide a consistent interface for creating,
  26. manipulating, and fitting data with polynomials of different bases.
  27. The convenience classes are the preferred interface for the `~numpy.polynomial`
  28. package, and are available from the `numpy.polynomial` namespace.
  29. This eliminates the need to
  30. navigate to the corresponding submodules, e.g. ``np.polynomial.Polynomial``
  31. or ``np.polynomial.Chebyshev`` instead of
  32. ``np.polynomial.polynomial.Polynomial`` or
  33. ``np.polynomial.chebyshev.Chebyshev``, respectively.
  34. The classes provide a more consistent and concise interface than the
  35. type-specific functions defined in the submodules for each type of polynomial.
  36. For example, to fit a Chebyshev polynomial with degree ``1`` to data given
  37. by arrays ``xdata`` and ``ydata``, the
  38. `~chebyshev.Chebyshev.fit` class method::
  39. >>> from numpy.polynomial import Chebyshev
  40. >>> c = Chebyshev.fit(xdata, ydata, deg=1)
  41. is preferred over the `chebyshev.chebfit` function from the
  42. `numpy.polynomial.chebyshev` module::
  43. >>> from numpy.polynomial.chebyshev import chebfit
  44. >>> c = chebfit(xdata, ydata, deg=1)
  45. See :doc:`routines.polynomials.classes` for more details.
  46. Convenience Classes
  47. ===================
  48. The following lists the various constants and methods common to all of
  49. the classes representing the various kinds of polynomials. In the following,
  50. the term ``Poly`` represents any one of the convenience classes (e.g.
  51. ``Polynomial``, ``Chebyshev``, ``Hermite``, etc.) while the lowercase ``p``
  52. represents an **instance** of a polynomial class.
  53. Constants
  54. ---------
  55. - ``Poly.domain`` -- Default domain
  56. - ``Poly.window`` -- Default window
  57. - ``Poly.basis_name`` -- String used to represent the basis
  58. - ``Poly.maxpower`` -- Maximum value ``n`` such that ``p**n`` is allowed
  59. - ``Poly.nickname`` -- String used in printing
  60. Creation
  61. --------
  62. Methods for creating polynomial instances.
  63. - ``Poly.basis(degree)`` -- Basis polynomial of given degree
  64. - ``Poly.identity()`` -- ``p`` where ``p(x) = x`` for all ``x``
  65. - ``Poly.fit(x, y, deg)`` -- ``p`` of degree ``deg`` with coefficients
  66. determined by the least-squares fit to the data ``x``, ``y``
  67. - ``Poly.fromroots(roots)`` -- ``p`` with specified roots
  68. - ``p.copy()`` -- Create a copy of ``p``
  69. Conversion
  70. ----------
  71. Methods for converting a polynomial instance of one kind to another.
  72. - ``p.cast(Poly)`` -- Convert ``p`` to instance of kind ``Poly``
  73. - ``p.convert(Poly)`` -- Convert ``p`` to instance of kind ``Poly`` or map
  74. between ``domain`` and ``window``
  75. Calculus
  76. --------
  77. - ``p.deriv()`` -- Take the derivative of ``p``
  78. - ``p.integ()`` -- Integrate ``p``
  79. Validation
  80. ----------
  81. - ``Poly.has_samecoef(p1, p2)`` -- Check if coefficients match
  82. - ``Poly.has_samedomain(p1, p2)`` -- Check if domains match
  83. - ``Poly.has_sametype(p1, p2)`` -- Check if types match
  84. - ``Poly.has_samewindow(p1, p2)`` -- Check if windows match
  85. Misc
  86. ----
  87. - ``p.linspace()`` -- Return ``x, p(x)`` at equally-spaced points in ``domain``
  88. - ``p.mapparms()`` -- Return the parameters for the linear mapping between
  89. ``domain`` and ``window``.
  90. - ``p.roots()`` -- Return the roots of `p`.
  91. - ``p.trim()`` -- Remove trailing coefficients.
  92. - ``p.cutdeg(degree)`` -- Truncate p to given degree
  93. - ``p.truncate(size)`` -- Truncate p to given size
  94. """
  95. from .polynomial import Polynomial
  96. from .chebyshev import Chebyshev
  97. from .legendre import Legendre
  98. from .hermite import Hermite
  99. from .hermite_e import HermiteE
  100. from .laguerre import Laguerre
  101. def set_default_printstyle(style):
  102. """
  103. Set the default format for the string representation of polynomials.
  104. Values for ``style`` must be valid inputs to ``__format__``, i.e. 'ascii'
  105. or 'unicode'.
  106. Parameters
  107. ----------
  108. style : str
  109. Format string for default printing style. Must be either 'ascii' or
  110. 'unicode'.
  111. Notes
  112. -----
  113. The default format depends on the platform: 'unicode' is used on
  114. Unix-based systems and 'ascii' on Windows. This determination is based on
  115. default font support for the unicode superscript and subscript ranges.
  116. Examples
  117. --------
  118. >>> p = np.polynomial.Polynomial([1, 2, 3])
  119. >>> c = np.polynomial.Chebyshev([1, 2, 3])
  120. >>> np.polynomial.set_default_printstyle('unicode')
  121. >>> print(p)
  122. 1.0 + 2.0·x¹ + 3.0·x²
  123. >>> print(c)
  124. 1.0 + 2.0·T₁(x) + 3.0·T₂(x)
  125. >>> np.polynomial.set_default_printstyle('ascii')
  126. >>> print(p)
  127. 1.0 + 2.0 x**1 + 3.0 x**2
  128. >>> print(c)
  129. 1.0 + 2.0 T_1(x) + 3.0 T_2(x)
  130. >>> # Formatting supercedes all class/package-level defaults
  131. >>> print(f"{p:unicode}")
  132. 1.0 + 2.0·x¹ + 3.0·x²
  133. """
  134. if style not in ('unicode', 'ascii'):
  135. raise ValueError(
  136. f"Unsupported format string '{style}'. Valid options are 'ascii' "
  137. f"and 'unicode'"
  138. )
  139. _use_unicode = True
  140. if style == 'ascii':
  141. _use_unicode = False
  142. from ._polybase import ABCPolyBase
  143. ABCPolyBase._use_unicode = _use_unicode
  144. from numpy._pytesttester import PytestTester
  145. test = PytestTester(__name__)
  146. del PytestTester