Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -124,23 +124,33 @@ def compute_alternate_low_expr(betas, z_a, y):
|
|
124 |
|
125 |
def compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr):
|
126 |
"""
|
127 |
-
Compute
|
128 |
-
|
|
|
|
|
129 |
"""
|
130 |
beta_sym, z_a_sym, y_sym = sp.symbols("beta z_a y", positive=True)
|
131 |
local_dict = {"beta": beta_sym, "z_a": z_a_sym, "y": y_sym}
|
|
|
132 |
try:
|
|
|
133 |
num_expr = sp.sympify(s_num_expr, locals=local_dict)
|
134 |
denom_expr = sp.sympify(s_denom_expr, locals=local_dict)
|
135 |
s_expr = num_expr / denom_expr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
except sp.SympifyError as e:
|
137 |
st.error(f"Error parsing expressions: {e}")
|
138 |
return np.full_like(betas, np.nan)
|
139 |
|
140 |
-
|
141 |
with np.errstate(divide='ignore', invalid='ignore'):
|
142 |
-
result =
|
143 |
-
# Ensure result is a numpy array
|
144 |
if np.isscalar(result):
|
145 |
result = np.full_like(betas, result)
|
146 |
return result
|
@@ -302,16 +312,41 @@ with tab1:
|
|
302 |
z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000, step=1000, key="z_steps")
|
303 |
|
304 |
st.subheader("Custom s Expression")
|
305 |
-
st.markdown("Enter expressions
|
|
|
|
|
|
|
306 |
s_num = st.text_input("s numerator", value="y*beta*(z_a-1)", key="s_num")
|
307 |
s_denom = st.text_input("s denominator", value="z_a", key="s_denom")
|
308 |
|
309 |
if st.button("Compute z vs. β Curves", key="tab1_button"):
|
310 |
with col2:
|
|
|
311 |
fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
|
312 |
s_num, s_denom)
|
313 |
if fig is not None:
|
314 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
|
316 |
# ----- Tab 2: Im{s} vs. z -----
|
317 |
with tab2:
|
|
|
124 |
|
125 |
def compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr):
|
126 |
"""
|
127 |
+
Compute custom curve by:
|
128 |
+
1. Computing s = s_num/s_denom
|
129 |
+
2. Inserting s into the final expression:
|
130 |
+
(y*beta*(z_a-1)*s + (a*s+1)*((y-1)*s-1))/((a*s+1)*(s^2 + s))
|
131 |
"""
|
132 |
beta_sym, z_a_sym, y_sym = sp.symbols("beta z_a y", positive=True)
|
133 |
local_dict = {"beta": beta_sym, "z_a": z_a_sym, "y": y_sym}
|
134 |
+
|
135 |
try:
|
136 |
+
# First calculate s = num/denom
|
137 |
num_expr = sp.sympify(s_num_expr, locals=local_dict)
|
138 |
denom_expr = sp.sympify(s_denom_expr, locals=local_dict)
|
139 |
s_expr = num_expr / denom_expr
|
140 |
+
|
141 |
+
# Now substitute this s into the main expression
|
142 |
+
a = z_a_sym # a is alias for z_a
|
143 |
+
numerator = y_sym*beta_sym*(z_a_sym-1)*s_expr + (a*s_expr+1)*((y_sym-1)*s_expr-1)
|
144 |
+
denominator = (a*s_expr+1)*(s_expr**2 + s_expr)
|
145 |
+
final_expr = numerator/denominator
|
146 |
+
|
147 |
except sp.SympifyError as e:
|
148 |
st.error(f"Error parsing expressions: {e}")
|
149 |
return np.full_like(betas, np.nan)
|
150 |
|
151 |
+
final_func = sp.lambdify((beta_sym, z_a_sym, y_sym), final_expr, modules=["numpy"])
|
152 |
with np.errstate(divide='ignore', invalid='ignore'):
|
153 |
+
result = final_func(betas, z_a, y)
|
|
|
154 |
if np.isscalar(result):
|
155 |
result = np.full_like(betas, result)
|
156 |
return result
|
|
|
312 |
z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000, step=1000, key="z_steps")
|
313 |
|
314 |
st.subheader("Custom s Expression")
|
315 |
+
st.markdown("""Enter expressions for s = numerator/denominator
|
316 |
+
(using variables `y`, `beta`, `z_a`)""")
|
317 |
+
st.latex(r"\text{This s will be inserted into:}")
|
318 |
+
st.latex(r"\frac{y\beta(z_a-1)\underline{s}+(a\underline{s}+1)((y-1)\underline{s}-1)}{(a\underline{s}+1)(\underline{s}^2 + \underline{s})}")
|
319 |
s_num = st.text_input("s numerator", value="y*beta*(z_a-1)", key="s_num")
|
320 |
s_denom = st.text_input("s denominator", value="z_a", key="s_denom")
|
321 |
|
322 |
if st.button("Compute z vs. β Curves", key="tab1_button"):
|
323 |
with col2:
|
324 |
+
# Compute and plot the z vs. β curves
|
325 |
fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
|
326 |
s_num, s_denom)
|
327 |
if fig is not None:
|
328 |
st.plotly_chart(fig, use_container_width=True)
|
329 |
+
|
330 |
+
# Add explanation of the curves
|
331 |
+
st.markdown("### Curve Explanations")
|
332 |
+
st.markdown("""
|
333 |
+
- **Upper z*(β)** (Blue): Maximum z value where discriminant is zero
|
334 |
+
- **Lower z*(β)** (Light Blue): Minimum z value where discriminant is zero
|
335 |
+
- **Low y Expression** (Red): Asymptotic approximation for low y values
|
336 |
+
- **High y Expression** (Green): Asymptotic approximation for high y values
|
337 |
+
- **Alternate Low Expression** (Orange): Alternative asymptotic expression
|
338 |
+
- **Custom s Expression** (Purple): Result from user-defined s substituted into:
|
339 |
+
""")
|
340 |
+
st.latex(r"\frac{y\beta(z_a-1)\underline{s}+(a\underline{s}+1)((y-1)\underline{s}-1)}{(a\underline{s}+1)(\underline{s}^2 + \underline{s})}")
|
341 |
+
|
342 |
+
# Display the current parameter values
|
343 |
+
st.markdown("### Current Parameters")
|
344 |
+
st.markdown(f"""
|
345 |
+
- z_a = {z_a_1}
|
346 |
+
- y = {y_1}
|
347 |
+
- z range: [{z_min_1}, {z_max_1}]
|
348 |
+
- s = ({s_num})/({s_denom})
|
349 |
+
""")
|
350 |
|
351 |
# ----- Tab 2: Im{s} vs. z -----
|
352 |
with tab2:
|