__init__.py 6.6 KB

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