Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -156,7 +156,7 @@ def compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr):
|
|
156 |
return result
|
157 |
|
158 |
def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
|
159 |
-
s_num_expr=None, s_denom_expr=None):
|
160 |
if z_a <= 0 or y <= 0 or z_min >= z_max:
|
161 |
st.error("Invalid input parameters.")
|
162 |
return None
|
@@ -166,28 +166,81 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
|
|
166 |
low_y_curve = compute_low_y_curve(betas, z_a, y)
|
167 |
high_y_curve = compute_high_y_curve(betas, z_a, y)
|
168 |
alt_low_expr = compute_alternate_low_expr(betas, z_a, y)
|
169 |
-
|
170 |
-
fig = go.Figure()
|
171 |
-
fig.add_trace(go.Scatter(x=betas, y=z_maxs, mode="markers+lines", name="Upper z*(β)",
|
172 |
-
marker=dict(size=5, color='blue'), line=dict(color='blue')))
|
173 |
-
fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines", name="Lower z*(β)",
|
174 |
-
marker=dict(size=5, color='lightblue'), line=dict(color='lightblue')))
|
175 |
-
fig.add_trace(go.Scatter(x=betas, y=low_y_curve, mode="markers+lines", name="Low y Expression",
|
176 |
-
marker=dict(size=5, color='red'), line=dict(color='red')))
|
177 |
-
fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines", name="High y Expression",
|
178 |
-
marker=dict(size=5, color='green'), line=dict(color='green')))
|
179 |
-
fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines", name="Alternate Low Expression",
|
180 |
-
marker=dict(size=5, color='orange'), line=dict(color='orange')))
|
181 |
|
|
|
182 |
if s_num_expr and s_denom_expr:
|
183 |
custom_curve = compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr)
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
return fig
|
190 |
|
|
|
191 |
def compute_cubic_roots(z, beta, z_a, y):
|
192 |
"""
|
193 |
Compute the roots of the cubic equation for given parameters.
|
@@ -274,7 +327,37 @@ def find_intersections(z, y, beta, a, s_range, n_guesses, tolerance):
|
|
274 |
continue
|
275 |
intersections = np.sort(np.append(intersections, refined_intersections))
|
276 |
return intersections
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
def generate_curves_plot(z, y, beta, a, s_range, n_points, n_guesses, tolerance):
|
279 |
s = np.linspace(s_range[0], s_range[1], n_points)
|
280 |
y1 = curve1(s, z, y)
|
|
|
156 |
return result
|
157 |
|
158 |
def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
|
159 |
+
s_num_expr=None, s_denom_expr=None, show_derivatives=False):
|
160 |
if z_a <= 0 or y <= 0 or z_min >= z_max:
|
161 |
st.error("Invalid input parameters.")
|
162 |
return None
|
|
|
166 |
low_y_curve = compute_low_y_curve(betas, z_a, y)
|
167 |
high_y_curve = compute_high_y_curve(betas, z_a, y)
|
168 |
alt_low_expr = compute_alternate_low_expr(betas, z_a, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
+
custom_curve = None
|
171 |
if s_num_expr and s_denom_expr:
|
172 |
custom_curve = compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr)
|
173 |
+
|
174 |
+
# Compute derivatives
|
175 |
+
derivatives = compute_all_derivatives(betas, z_mins, z_maxs, low_y_curve, high_y_curve,
|
176 |
+
alt_low_expr, custom_curve)
|
177 |
+
|
178 |
+
# Create subplots: one for curves, one for first derivatives, one for second derivatives
|
179 |
+
fig = go.Figure()
|
180 |
|
181 |
+
# Original curves
|
182 |
+
fig.add_trace(go.Scatter(x=betas, y=z_maxs, mode="markers+lines",
|
183 |
+
name="Upper z*(β)", line=dict(color='blue')))
|
184 |
+
fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines",
|
185 |
+
name="Lower z*(β)", line=dict(color='lightblue')))
|
186 |
+
fig.add_trace(go.Scatter(x=betas, y=low_y_curve, mode="markers+lines",
|
187 |
+
name="Low y Expression", line=dict(color='red')))
|
188 |
+
fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines",
|
189 |
+
name="High y Expression", line=dict(color='green')))
|
190 |
+
fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines",
|
191 |
+
name="Alternate Low Expression", line=dict(color='orange')))
|
192 |
+
|
193 |
+
if custom_curve is not None:
|
194 |
+
fig.add_trace(go.Scatter(x=betas, y=custom_curve, mode="markers+lines",
|
195 |
+
name="Custom Expression", line=dict(color='purple')))
|
196 |
+
|
197 |
+
if show_derivatives:
|
198 |
+
# First derivatives
|
199 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['upper'][0], mode="lines",
|
200 |
+
name="Upper z*(β) d/dβ", line=dict(color='blue', dash='dash')))
|
201 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['lower'][0], mode="lines",
|
202 |
+
name="Lower z*(β) d/dβ", line=dict(color='lightblue', dash='dash')))
|
203 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['low_y'][0], mode="lines",
|
204 |
+
name="Low y d/dβ", line=dict(color='red', dash='dash')))
|
205 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['high_y'][0], mode="lines",
|
206 |
+
name="High y d/dβ", line=dict(color='green', dash='dash')))
|
207 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['alt_low'][0], mode="lines",
|
208 |
+
name="Alt Low d/dβ", line=dict(color='orange', dash='dash')))
|
209 |
+
if custom_curve is not None:
|
210 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['custom'][0], mode="lines",
|
211 |
+
name="Custom d/dβ", line=dict(color='purple', dash='dash')))
|
212 |
+
|
213 |
+
# Second derivatives
|
214 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['upper'][1], mode="lines",
|
215 |
+
name="Upper z*(β) d²/dβ²", line=dict(color='blue', dash='dot')))
|
216 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['lower'][1], mode="lines",
|
217 |
+
name="Lower z*(β) d��/dβ²", line=dict(color='lightblue', dash='dot')))
|
218 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['low_y'][1], mode="lines",
|
219 |
+
name="Low y d²/dβ²", line=dict(color='red', dash='dot')))
|
220 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['high_y'][1], mode="lines",
|
221 |
+
name="High y d²/dβ²", line=dict(color='green', dash='dot')))
|
222 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['alt_low'][1], mode="lines",
|
223 |
+
name="Alt Low d²/dβ²", line=dict(color='orange', dash='dot')))
|
224 |
+
if custom_curve is not None:
|
225 |
+
fig.add_trace(go.Scatter(x=betas, y=derivatives['custom'][1], mode="lines",
|
226 |
+
name="Custom d²/dβ²", line=dict(color='purple', dash='dot')))
|
227 |
+
|
228 |
+
fig.update_layout(
|
229 |
+
title="Curves vs β: z*(β) Boundaries and Asymptotic Expressions",
|
230 |
+
xaxis_title="β",
|
231 |
+
yaxis_title="Value",
|
232 |
+
hovermode="x unified",
|
233 |
+
showlegend=True,
|
234 |
+
legend=dict(
|
235 |
+
yanchor="top",
|
236 |
+
y=0.99,
|
237 |
+
xanchor="left",
|
238 |
+
x=0.01
|
239 |
+
)
|
240 |
+
)
|
241 |
return fig
|
242 |
|
243 |
+
|
244 |
def compute_cubic_roots(z, beta, z_a, y):
|
245 |
"""
|
246 |
Compute the roots of the cubic equation for given parameters.
|
|
|
327 |
continue
|
328 |
intersections = np.sort(np.append(intersections, refined_intersections))
|
329 |
return intersections
|
330 |
+
@st.cache_data
|
331 |
+
def compute_derivatives(curve, betas):
|
332 |
+
"""Compute first and second derivatives of a curve"""
|
333 |
+
d1 = np.gradient(curve, betas)
|
334 |
+
d2 = np.gradient(d1, betas)
|
335 |
+
return d1, d2
|
336 |
|
337 |
+
def compute_all_derivatives(betas, z_mins, z_maxs, low_y_curve, high_y_curve, alt_low_expr, custom_curve=None):
|
338 |
+
"""Compute derivatives for all curves"""
|
339 |
+
derivatives = {}
|
340 |
+
|
341 |
+
# Upper z*(β)
|
342 |
+
derivatives['upper'] = compute_derivatives(z_maxs, betas)
|
343 |
+
|
344 |
+
# Lower z*(β)
|
345 |
+
derivatives['lower'] = compute_derivatives(z_mins, betas)
|
346 |
+
|
347 |
+
# Low y Expression
|
348 |
+
derivatives['low_y'] = compute_derivatives(low_y_curve, betas)
|
349 |
+
|
350 |
+
# High y Expression
|
351 |
+
derivatives['high_y'] = compute_derivatives(high_y_curve, betas)
|
352 |
+
|
353 |
+
# Alternate Low Expression
|
354 |
+
derivatives['alt_low'] = compute_derivatives(alt_low_expr, betas)
|
355 |
+
|
356 |
+
# Custom Expression (if provided)
|
357 |
+
if custom_curve is not None:
|
358 |
+
derivatives['custom'] = compute_derivatives(custom_curve, betas)
|
359 |
+
|
360 |
+
return derivatives
|
361 |
def generate_curves_plot(z, y, beta, a, s_range, n_points, n_guesses, tolerance):
|
362 |
s = np.linspace(s_range[0], s_range[1], n_points)
|
363 |
y1 = curve1(s, z, y)
|