88
99from freeride .plotting import textbook_axes , update_axes_limits
1010
11+
1112class PolyBase (np .polynomial .Polynomial ):
12- """
13- A base class for polynomial functions with added methods.
14- The independent variable is q instead of x to align with typical price-quantity axes.
15- The dependent variable is is explicitly named as p instead of y.
13+ """Polynomial base class using ``q`` and ``p`` as variables.
1614
17- This class extends NumPy's polynomial class and provides additional methods for
18- working with polynomial functions .
15+ Extends : class:`numpy. polynomial.Polynomial` with economics-oriented
16+ helpers .
1917
2018 .. math::
2119
22- p = \\ sum_{k=0}^n c_k q^k
20+ p = \sum_{k=0}^n c_k q^k
2321
2422 Parameters
2523 ----------
2624 *coef : array-like or scalar
27- Coefficients of the polynomial. Can be specified as a list, tuple, or individual numerical arguments .
25+ Polynomial coefficients .
2826
2927 Attributes
3028 ----------
31- coef (ndarray): Coefficients of the polynomial.
32-
33- Example:
34- To create a polynomial and use its methods:
35-
36- >>> poly = PolyBase([1, -2, 1]) # Represents x^2 - 2x + 1
37- >>> poly.p(2.0) # Calculate the price at q=2.0
38- 1.0
29+ coef : ndarray
30+ Stored coefficients.
31+
32+ Examples
33+ --------
34+ >>> poly = PolyBase([1, -2, 1]) # x^2 - 2x + 1
35+ >>> poly.p(2.0)
36+ 1.0
3937 """
4038
4139 def __init__ (self , * coef , symbols = None , domain = None ):
42- """
43- Initialize a PolyBase object with the given coefficients.
44- The coefficients determine the polynomial represented by the object.
40+ """Initialize the polynomial.
4541
4642 Parameters
4743 ----------
4844 *coef : array-like or scalar
49- Coefficients of the polynomial. Can be specified as a list, tuple, or individual numerical arguments.
50-
51- Returns
52- ----------
53- None
54-
55- Examples
56- --------
57- >>> poly = PolyBase([1, -2, 3]) # Represents 1 - 2q + 3q^2
58- >>> poly = PolyBase(1, -2, 3) # Equivalent to the above
45+ Polynomial coefficients.
46+ symbols : tuple[str, str] | str | None, optional
47+ Variable names. Defaults to ``("q", "p")``.
48+ domain : tuple[float, float] | None, optional
49+ Closed interval for evaluation.
5950 """
6051 self .set_symbols (symbols )
6152 self .is_undefined = coef == ([],) # helpful in sum functions
@@ -107,42 +98,32 @@ def set_symbols(self, symbols):
10798 self ._symbol = self .x
10899
109100 def p (self , q : float ):
110- """
111- Calculate the price given a quantity value q.
101+ """Return price at quantity ``q``.
112102
113103 Parameters
114- --------
115- q (float): The quantity value.
104+ ----------
105+ q : float
106+ Quantity value.
116107
117108 Returns
118- --------
119- float: The corresponding price.
120-
121- Example
122- --------
123- >>> poly = PolyBase([1, -2, 3]) # Represents 1 - 2x + 3x^2
124- >>> poly.p(2.0)
125- 9.0
109+ -------
110+ float
111+ Price.
126112 """
127113 return self .__call__ (q )
128114
129115 def q (self , p ):
130- """
131- Calculate the quantity given a price value p.
116+ """Return the quantity at price ``p``.
132117
133118 Parameters
134- --------
135- p (float): The price value.
119+ ----------
120+ p : float
121+ Price value.
136122
137123 Returns
138- --------
139- float or ndarray: The corresponding quantity or array of quantities.
140-
141- Example
142- --------
143- >>> poly = PolyBase([1, -2, 1]) # Represents x^2 - 2x + 1
144- >>> poly.q(1.0)
145- 1.0
124+ -------
125+ float | ndarray
126+ Quantity or array of quantities.
146127 """
147128 # Perfectly Inelastic
148129 if self .slope == np .inf :
@@ -159,25 +140,20 @@ def q(self, p):
159140 def plot (
160141 self , ax = None , label = None , max_q = 100 , min_plotted_q = 0 , textbook_style = True
161142 ):
162- """
163- Plot the polynomial.
143+ """Plot the polynomial.
164144
165145 Parameters
166- --------
167- ax (matplotlib.axes._axes.Axes, optional): The matplotlib Axes to use for plotting.
168- If not provided, the current Axes will be used.
169- max_q (float, optional): The maximum x-value for the plot. Defaults to 100.
170- label (str, optional): The label for the plot. Defaults to None.
171- min_plotted_q (float, optional): The minimum quantity value to plot.
172-
173- Returns
174- --------
175- None
176-
177- Example
178- --------
179- >>> poly = PolyBase([1, -2, 1]) # Represents x^2 - 2x + 1
180- >>> poly.plot()
146+ ----------
147+ ax : matplotlib.axes.Axes, optional
148+ Target axes. Defaults to ``matplotlib.pyplot.gca()``.
149+ label : str, optional
150+ Legend label.
151+ max_q : float, optional
152+ Upper bound on quantity (default ``100``).
153+ min_plotted_q : float, optional
154+ Minimum quantity to plot.
155+ textbook_style : bool, optional
156+ Apply textbook-style axis formatting.
181157 """
182158 if ax is None :
183159 ax = plt .gca ()
@@ -195,19 +171,7 @@ def plot(
195171
196172 # Similar to numpy ABCPolyBase
197173 def _repr_latex_ (self ):
198- """
199- Generate LaTeX representation of the polynomial.
200-
201- Returns
202- --------
203- str: LaTeX representation of the polynomial.
204-
205- Example
206- --------
207- >>> poly = PolyBase([1, -2, 3]) # Represents 1 - 2x + 3x%2
208- >>> poly._repr_latex_()
209- '$p = 1 - 2q + 3q^2$'
210- """
174+ """Return a LaTeX representation of the polynomial."""
211175 # overwrite ABCPolyBase Method to use p/q instead of x\mapsto
212176 # get the scaled argument string to the basis functions
213177 if hasattr (self , "is_undefined" ) and self .is_undefined :
@@ -285,6 +249,3 @@ def vertical_shift(self, delta, inplace=True):
285249 new_coef = self .coef
286250 new_coef [0 ] += delta
287251 return self .__class__ (* new_coef , symbols = self .symbols )
288-
289-
290-
0 commit comments