euler314 commited on
Commit
f159645
·
verified ·
1 Parent(s): 99f6a90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -12
app.py CHANGED
@@ -26,8 +26,8 @@ d_sym = 1
26
 
27
  # Symbolic expression for the standard cubic discriminant
28
  Delta_expr = (
29
- ( (b_sym*c_sym)/(6*a_sym**2) - (b_sym**3)/(27*a_sym**3) - d_sym/(2*a_sym) )**2
30
- + ( c_sym/(3*a_sym) - (b_sym**2)/(9*a_sym**2) )**3
31
  )
32
 
33
  # Turn that into a fast numeric function:
@@ -127,7 +127,6 @@ def compute_high_y_curve(betas, z_a, y):
127
  numerator = -4*a*(a-1)*y*betas - 2*a*y - 2*a*(2*a-1)
128
  return numerator/denominator
129
 
130
-
131
  def compute_custom_expression(betas, z_a, y, num_expr_str, denom_expr_str):
132
  """
133
  Compute a custom curve given numerator and denominator expressions
@@ -152,12 +151,11 @@ def compute_custom_expression(betas, z_a, y, num_expr_str, denom_expr_str):
152
  result = num_func(betas, z_a, y) / denom_func(betas, z_a, y)
153
  return result
154
 
155
-
156
  def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
157
  custom_num_expr=None, custom_denom_expr=None):
158
  if z_a <= 0 or y <= 0 or z_min >= z_max:
159
  st.error("Invalid input parameters.")
160
- return None
161
 
162
  betas = np.linspace(0, 1, beta_steps)
163
 
@@ -211,6 +209,7 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
211
  )
212
  )
213
 
 
214
  # Add custom expression if both numerator and denominator are provided
215
  if custom_num_expr and custom_denom_expr:
216
  custom_curve = compute_custom_expression(betas, z_a, y, custom_num_expr, custom_denom_expr)
@@ -231,7 +230,81 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
231
  yaxis_title="Value",
232
  hovermode="x unified",
233
  )
234
- return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
  def compute_cubic_roots(z, beta, z_a, y):
237
  """
@@ -419,7 +492,8 @@ def generate_curves_plot(z, y, beta, a, s_range, n_points, n_guesses, tolerance)
419
 
420
  return fig, intersections
421
 
422
- # Streamlit UI
 
423
  st.title("Cubic Root Analysis")
424
 
425
  tab1, tab2, tab3 = st.tabs(["z*(β) Curves", "Im{s} vs. z", "Curve Intersections"])
@@ -448,11 +522,13 @@ with tab1:
448
 
449
  if st.button("Compute z vs. β Curves"):
450
  with col2:
451
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1,
452
- beta_steps, z_steps,
453
- custom_num_expr, custom_denom_expr)
454
- if fig is not None:
455
- st.plotly_chart(fig, use_container_width=True)
 
 
456
 
457
  st.markdown("### Additional Expressions")
458
  st.markdown("""
 
26
 
27
  # Symbolic expression for the standard cubic discriminant
28
  Delta_expr = (
29
+ ((b_sym*c_sym)/(6*a_sym**2) - (b_sym**3)/(27*a_sym**3) - d_sym/(2*a_sym))**2
30
+ + (c_sym/(3*a_sym) - (b_sym**2)/(9*a_sym**2))**3
31
  )
32
 
33
  # Turn that into a fast numeric function:
 
127
  numerator = -4*a*(a-1)*y*betas - 2*a*y - 2*a*(2*a-1)
128
  return numerator/denominator
129
 
 
130
  def compute_custom_expression(betas, z_a, y, num_expr_str, denom_expr_str):
131
  """
132
  Compute a custom curve given numerator and denominator expressions
 
151
  result = num_func(betas, z_a, y) / denom_func(betas, z_a, y)
152
  return result
153
 
 
154
  def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
155
  custom_num_expr=None, custom_denom_expr=None):
156
  if z_a <= 0 or y <= 0 or z_min >= z_max:
157
  st.error("Invalid input parameters.")
158
+ return None, None
159
 
160
  betas = np.linspace(0, 1, beta_steps)
161
 
 
209
  )
210
  )
211
 
212
+ custom_curve = None
213
  # Add custom expression if both numerator and denominator are provided
214
  if custom_num_expr and custom_denom_expr:
215
  custom_curve = compute_custom_expression(betas, z_a, y, custom_num_expr, custom_denom_expr)
 
230
  yaxis_title="Value",
231
  hovermode="x unified",
232
  )
233
+
234
+ # ----- NEW GRID: Compute Derivatives with Respect to β -----
235
+ # Use numpy.gradient assuming betas is evenly spaced.
236
+ dzmax_dbeta = np.gradient(z_maxs, betas)
237
+ dzmin_dbeta = np.gradient(z_mins, betas)
238
+ dlowy_dbeta = np.gradient(low_y_curve, betas)
239
+ dhighy_dbeta = np.gradient(high_y_curve, betas)
240
+ dcustom_dbeta = np.gradient(custom_curve, betas) if custom_curve is not None else None
241
+
242
+ fig_deriv = go.Figure()
243
+
244
+ fig_deriv.add_trace(
245
+ go.Scatter(
246
+ x=betas,
247
+ y=dzmax_dbeta,
248
+ mode="markers+lines",
249
+ name="d/dβ Upper z*(β)",
250
+ marker=dict(size=5, color='blue'),
251
+ line=dict(color='blue'),
252
+ )
253
+ )
254
+
255
+ fig_deriv.add_trace(
256
+ go.Scatter(
257
+ x=betas,
258
+ y=dzmin_dbeta,
259
+ mode="markers+lines",
260
+ name="d/dβ Lower z*(β)",
261
+ marker=dict(size=5, color='lightblue'),
262
+ line=dict(color='lightblue'),
263
+ )
264
+ )
265
+
266
+ fig_deriv.add_trace(
267
+ go.Scatter(
268
+ x=betas,
269
+ y=dlowy_dbeta,
270
+ mode="markers+lines",
271
+ name="d/dβ Low y Expression",
272
+ marker=dict(size=5, color='red'),
273
+ line=dict(color='red'),
274
+ )
275
+ )
276
+
277
+ fig_deriv.add_trace(
278
+ go.Scatter(
279
+ x=betas,
280
+ y=dhighy_dbeta,
281
+ mode="markers+lines",
282
+ name="d/dβ High y Expression",
283
+ marker=dict(size=5, color='green'),
284
+ line=dict(color='green'),
285
+ )
286
+ )
287
+
288
+ if dcustom_dbeta is not None:
289
+ fig_deriv.add_trace(
290
+ go.Scatter(
291
+ x=betas,
292
+ y=dcustom_dbeta,
293
+ mode="markers+lines",
294
+ name="d/dβ Custom Expression",
295
+ marker=dict(size=5, color='purple'),
296
+ line=dict(color='purple'),
297
+ )
298
+ )
299
+
300
+ fig_deriv.update_layout(
301
+ title="Derivatives vs β of Each Curve",
302
+ xaxis_title="β",
303
+ yaxis_title="d(Value)/dβ",
304
+ hovermode="x unified",
305
+ )
306
+
307
+ return fig, fig_deriv
308
 
309
  def compute_cubic_roots(z, beta, z_a, y):
310
  """
 
492
 
493
  return fig, intersections
494
 
495
+ # ------------------- Streamlit UI -------------------
496
+
497
  st.title("Cubic Root Analysis")
498
 
499
  tab1, tab2, tab3 = st.tabs(["z*(β) Curves", "Im{s} vs. z", "Curve Intersections"])
 
522
 
523
  if st.button("Compute z vs. β Curves"):
524
  with col2:
525
+ fig_main, fig_deriv = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1,
526
+ beta_steps, z_steps,
527
+ custom_num_expr, custom_denom_expr)
528
+ if fig_main is not None and fig_deriv is not None:
529
+ st.plotly_chart(fig_main, use_container_width=True)
530
+ st.markdown("### Derivative of Each Curve vs. β")
531
+ st.plotly_chart(fig_deriv, use_container_width=True)
532
 
533
  st.markdown("### Additional Expressions")
534
  st.markdown("""