1"""

2 A sub-package for efficiently dealing with polynomials.

3

4 Within the documentation for this sub-package, a "finite power series,"

5 i.e., a polynomial (also referred to simply as a "series") is represented

6 by a 1-D numpy array of the polynomial's coefficients, ordered from lowest

7 order term to highest. For example, array([1,2,3]) represents

8``P_0 + 2*P_1 + 3*P_2``, where P_n is the n-th order basis polynomial

9 applicable to the specific module in question, e.g., `polynomial` (which

10"wraps" the "standard" basis) or `chebyshev`. For optimal performance,

11 all operations on polynomials, including evaluation at an argument, are

12 implemented as operations on the coefficients. Additional (module-specific)

13 information can be found in the docstring for the module of interest.

14

15 This package provides *convenience classes* for each of six different kinds

16 of polynomials:

17

18 ======================== ================

19 **Name** **Provides**

20 ======================== ================

21 `~polynomial.Polynomial` Power series

22 `~chebyshev.Chebyshev` Chebyshev series

23 `~legendre.Legendre` Legendre series

24 `~laguerre.Laguerre` Laguerre series

25 `~hermite.Hermite` Hermite series

26 `~hermite_e.HermiteE` HermiteE series

27 ======================== ================

28

29 These *convenience classes* provide a consistent interface for creating,

30 manipulating, and fitting data with polynomials of different bases.

31 The convenience classes are the preferred interface for the `~numpy.polynomial`

32 package, and are available from the ``numpy.polynomial`` namespace.

33 This eliminates the need to navigate to the corresponding submodules, e.g.

34``np.polynomial.Polynomial`` or ``np.polynomial.Chebyshev`` instead of

35``np.polynomial.polynomial.Polynomial`` or

36``np.polynomial.chebyshev.Chebyshev``, respectively.

37 The classes provide a more consistent and concise interface than the

38 type-specific functions defined in the submodules for each type of polynomial.

39 For example, to fit a Chebyshev polynomial with degree ``1`` to data given

40 by arrays ``xdata`` and ``ydata``, the

41`~chebyshev.Chebyshev.fit` class method::

42

43 >>> from numpy.polynomial import Chebyshev

44 >>> c = Chebyshev.fit(xdata, ydata, deg=1)

45

46 is preferred over the `chebyshev.chebfit` function from the

47``np.polynomial.chebyshev`` module::

48

49 >>> from numpy.polynomial.chebyshev import chebfit

50 >>> c = chebfit(xdata, ydata, deg=1)

51

52 See :doc:`routines.polynomials.classes` for more details.

53

54 Convenience Classes

55===================

56

57 The following lists the various constants and methods common to all of

58 the classes representing the various kinds of polynomials. In the following,

59 the term ``Poly`` represents any one of the convenience classes (e.g.

60`~polynomial.Polynomial`, `~chebyshev.Chebyshev`, `~hermite.Hermite`, etc.)

61 while the lowercase ``p`` represents an **instance** of a polynomial class.

62

63 Constants

64---------

65

66- ``Poly.domain`` -- Default domain

67- ``Poly.window`` -- Default window

68- ``Poly.basis_name`` -- String used to represent the basis

69- ``Poly.maxpower`` -- Maximum value ``n`` such that ``p**n`` is allowed

70- ``Poly.nickname`` -- String used in printing

71

72 Creation

73--------

74

75 Methods for creating polynomial instances.

76

77- ``Poly.basis(degree)`` -- Basis polynomial of given degree

78- ``Poly.identity()`` -- ``p`` where ``p(x) = x`` for all ``x``

79- ``Poly.fit(x, y, deg)`` -- ``p`` of degree ``deg`` with coefficients

80 determined by the least-squares fit to the data ``x``, ``y``

81- ``Poly.fromroots(roots)`` -- ``p`` with specified roots

82- ``p.copy()`` -- Create a copy of ``p``

83

84 Conversion

85----------

86

87 Methods for converting a polynomial instance of one kind to another.

88

89- ``p.cast(Poly)`` -- Convert ``p`` to instance of kind ``Poly``

90- ``p.convert(Poly)`` -- Convert ``p`` to instance of kind ``Poly`` or map

91 between ``domain`` and ``window``

92

93 Calculus

94--------

95- ``p.deriv()`` -- Take the derivative of ``p``

96- ``p.integ()`` -- Integrate ``p``

97

98 Validation

99----------

100- ``Poly.has_samecoef(p1, p2)`` -- Check if coefficients match

101- ``Poly.has_samedomain(p1, p2)`` -- Check if domains match

102- ``Poly.has_sametype(p1, p2)`` -- Check if types match

103- ``Poly.has_samewindow(p1, p2)`` -- Check if windows match

104

105 Misc

106----

107- ``p.linspace()`` -- Return ``x, p(x)`` at equally-spaced points in ``domain``

108- ``p.mapparms()`` -- Return the parameters for the linear mapping between

109 ``domain`` and ``window``.

110- ``p.roots()`` -- Return the roots of `p`.

111- ``p.trim()`` -- Remove trailing coefficients.

112- ``p.cutdeg(degree)`` -- Truncate p to given degree

113- ``p.truncate(size)`` -- Truncate p to given size

114

115"""

116 from .polynomial import Polynomial

117 from .chebyshev import Chebyshev

118 from .legendre import Legendre

119 from .hermite import Hermite

120 from .hermite_e import HermiteE

121 from .laguerre import Laguerre

122

123__all__ = [

124 "set_default_printstyle",

125 "polynomial", "Polynomial",

126 "chebyshev", "Chebyshev",

127 "legendre", "Legendre",

128 "hermite", "Hermite",

129 "hermite_e", "HermiteE",

130 "laguerre", "Laguerre",

131]

132

133

134 def set_default_printstyle(style):

135 """

136 Set the default format for the string representation of polynomials.

137

138 Values for ``style`` must be valid inputs to ``__format__``, i.e. 'ascii'

139 or 'unicode'.

140

141 Parameters

142 ----------

143 style : str

144 Format string for default printing style. Must be either 'ascii' or

145 'unicode'.

146

147 Notes

148 -----

149 The default format depends on the platform: 'unicode' is used on

150 Unix-based systems and 'ascii' on Windows. This determination is based on

151 default font support for the unicode superscript and subscript ranges.

152

153 Examples

154 --------

155 >>> p = np.polynomial.Polynomial([1, 2, 3])

156 >>> c = np.polynomial.Chebyshev([1, 2, 3])

157 >>> np.polynomial.set_default_printstyle('unicode')

158 >>> print(p)

159 1.0 + 2.0·x + 3.0·x²

160 >>> print(c)

161 1.0 + 2.0·T₁(x) + 3.0·T₂(x)

162 >>> np.polynomial.set_default_printstyle('ascii')

163 >>> print(p)

164 1.0 + 2.0 x + 3.0 x**2

165 >>> print(c)

166 1.0 + 2.0 T_1(x) + 3.0 T_2(x)

167 >>> # Formatting supersedes all class/package-level defaults

168 >>> print(f"{p:unicode}")

169 1.0 + 2.0·x + 3.0·x²

170 """

171 if style not in ('unicode', 'ascii'):

172 raise ValueError(

173 f"Unsupported format string '{style}'. Valid options are 'ascii' "

174 f"and 'unicode'"

175 )

176 _use_unicode = True

177 if style == 'ascii':

178 _use_unicode = False

179 from ._polybase import ABCPolyBase

180 ABCPolyBase._use_unicode = _use_unicode

181

182

183 from numpy._pytesttester import PytestTester

184 test = PytestTester(__name__)

185 del PytestTester