Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions freeride/monopoly.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ def _solve(self):
self.q = best_q
self.p = self.demand.p(best_q)
self.profit = best_profit

def __repr__(self) -> str:
"""Return a concise text summary of the monopoly outcome."""
return f"Monopoly: Q = {self.q:g}, P = {self.p:g}, Profit = {self.profit:g}"

def _repr_latex_(self) -> str:
"""Return a LaTeX summary for notebook display."""
return f"$Q^* = {self.q:g},\\ P^* = {self.p:g},\\ \\Pi = {self.profit:g}$"
9 changes: 9 additions & 0 deletions tests/test_monopoly.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def test_basic_outcome(self):
self.assertAlmostEqual(m.p, 6.0)
self.assertAlmostEqual(m.profit, 16.0)

def test_repr_methods(self):
"""Monopoly representations should summarize the solved outcome."""
monopoly = Monopoly(Demand(10, -1), Cost(0, 2))
self.assertEqual(repr(monopoly), "Monopoly: Q = 4, P = 6, Profit = 16")
self.assertEqual(
monopoly._repr_latex_(),
"$Q^* = 4,\\ P^* = 6,\\ \\Pi = 16$",
)

def test_piecewise_demand_zero_cost(self):
"""Profit maximization with piecewise demand and zero cost."""
demand = Demand([10, 5], [-1, -1])
Expand Down
Loading