__init__.py 6.6 KB

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