From dd167db661b125411770d2a1869f9b72e65f47f7 Mon Sep 17 00:00:00 2001 From: Matt McKay Date: Fri, 16 Jan 2026 10:09:31 +1100 Subject: [PATCH] Fix SymPy overflow error using Rational for exact computation The sympy code was using float values (A=2.0, alpha=0.3, delta=0.5) which caused 'OverflowError: mpz too large to convert to float' during symbolic computation. This fix: - Imports Rational from sympy - Defines A_sym, alpha_sym, delta_sym as Rational values for exact computation - Uses float() when printing s_star for display This matches the fix in the production lecture-python-intro repository. --- lectures/solow.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lectures/solow.md b/lectures/solow.md index ee02b8b..4e8180e 100644 --- a/lectures/solow.md +++ b/lectures/solow.md @@ -510,13 +510,18 @@ plt.show() One can also try to solve this mathematically by differentiating $c^*(s)$ and solve for $\frac{d}{ds}c^*(s)=0$ using [sympy](https://www.sympy.org/en/index.html). ```{code-cell} ipython3 -from sympy import solve, Symbol +from sympy import solve, Symbol, Rational ``` ```{code-cell} ipython3 +# Use Rational for exact symbolic computation to avoid overflow +A_sym = Rational(2) +alpha_sym = Rational(3, 10) +delta_sym = Rational(1, 2) + s_symbol = Symbol('s', real=True) -k = ((s_symbol * A) / delta)**(1/(1 - alpha)) -c = (1 - s_symbol) * A * k ** alpha +k = ((s_symbol * A_sym) / delta_sym)**(1/(1 - alpha_sym)) +c = (1 - s_symbol) * A_sym * k ** alpha_sym ``` Let's differentiate $c$ and solve using [sympy.solve](https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve) @@ -524,7 +529,7 @@ Let's differentiate $c$ and solve using [sympy.solve](https://docs.sympy.org/lat ```{code-cell} ipython3 # Solve using sympy s_star = solve(c.diff())[0] -print(f"s_star = {s_star}") +print(f"s_star = {float(s_star)}") ``` Incidentally, the rate of savings which maximizes steady state level of per capita consumption is called the [Golden Rule savings rate](https://en.wikipedia.org/wiki/Golden_Rule_savings_rate).