@@ -81,12 +81,12 @@ function integrate(
8181
8282 eq = expand (eq)
8383
84- if x == nothing
84+ if x === nothing
8585 vars = get_variables (eq)
8686 if length (vars) > 1
8787 error (" Multiple symbolic variables detect. Please pass the independent variable to `integrate`" )
8888 elseif length (vars) == 1
89- x = vars[ 1 ]
89+ x = first ( vars)
9090 else
9191 @syms 𝑥
9292 x = 𝑥
@@ -125,27 +125,66 @@ function integrate(
125125 end
126126end
127127
128+ # Helper to extract numeric value from symbolic expression
129+ function extract_numeric_value (expr)
130+ # Already a number
131+ if expr isa Number
132+ return expr
133+ end
134+
135+ # Unwrap Num type
136+ if expr isa Num
137+ expr = Symbolics. value (expr)
138+ if expr isa Number
139+ return expr
140+ end
141+ end
142+
143+ # Try Float64 conversion (works for BasicSymbolic numeric literals)
144+ try
145+ result = Float64 (expr)
146+ return result
147+ catch
148+ end
149+
150+ # Try converting to Julia expression and evaluating
151+ # This handles cases like sin(0) that need to be evaluated
152+ try
153+ julia_expr = Symbolics. toexpr (expr)
154+ result = Base. invokelatest (eval, julia_expr)
155+ if result isa Number
156+ return result
157+ end
158+ catch
159+ end
160+
161+ # Return as-is if conversion fails
162+ return expr
163+ end
164+
128165# Evaluate an expression at a bound, using limit for infinite bounds
129166function eval_at_bound (expr, x, bound)
167+ # Unwrap both expression and variable for consistent handling
168+ expr_unwrapped = value (expr)
169+ x_unwrapped = value (x)
170+
130171 if isinf (bound)
131172 # Use symbolic limits for infinite bounds
132- expr_unwrapped = value (expr)
133- x_unwrapped = value (x)
134173 try
135174 result = limit (expr_unwrapped, x_unwrapped, bound)
136175 # limit returns a tuple (value, assumptions), extract the value
137176 if result isa Tuple
138- return first (result)
139- else
140- return result
177+ result = first (result)
141178 end
179+ return extract_numeric_value (result)
142180 catch e
143181 # If limit computation fails, fall back to direct substitution
144182 # This may result in NaN for indeterminate forms
145- return substitute (expr , Dict (x => bound))
183+ return substitute (expr_unwrapped , Dict (x_unwrapped => bound))
146184 end
147185 else
148- return substitute (expr, Dict (x => bound))
186+ result = substitute (expr_unwrapped, Dict (x_unwrapped => bound))
187+ return extract_numeric_value (result)
149188 end
150189end
151190
@@ -155,10 +194,10 @@ function integrate(eq, xx::Tuple; kwargs...)
155194 sol = integrate (eq, x; kwargs... )
156195
157196 if sol isa Tuple
158- if ! isequal (first (sol), 0 ) && sol[2 ] == 0
197+ if ! isequal (first (sol), 0 ) && isequal ( sol[2 ], 0 )
159198 hi_val = eval_at_bound (first (sol), x, hi)
160199 lo_val = eval_at_bound (first (sol), x, lo)
161- result = hi_val - lo_val
200+ result = extract_numeric_value ( hi_val - lo_val)
162201 # Check if the result is valid (not NaN or undefined)
163202 if result isa Number && (isnan (result) || isinf (result))
164203 return nothing
@@ -167,10 +206,10 @@ function integrate(eq, xx::Tuple; kwargs...)
167206 else
168207 return nothing
169208 end
170- elseif sol != nothing
209+ elseif sol != = nothing
171210 hi_val = eval_at_bound (sol, x, hi)
172211 lo_val = eval_at_bound (sol, x, lo)
173- result = hi_val - lo_val
212+ result = extract_numeric_value ( hi_val - lo_val)
174213 # Check if the result is valid (not NaN or undefined)
175214 if result isa Number && (isnan (result) || isinf (result))
176215 return nothing
@@ -184,26 +223,26 @@ end
184223function get_solved (p, sol)
185224 if sol isa Tuple
186225 s = sol[1 ]
187- return s == nothing ? 0 : s
226+ return s === nothing ? 0 : s
188227 else
189- return sol == nothing ? 0 : sol
228+ return sol === nothing ? 0 : sol
190229 end
191230end
192231
193232function get_unsolved (p, sol)
194233 if sol isa Tuple
195234 u = sol[2 ]
196- return u == nothing ? 0 : u
235+ return u === nothing ? 0 : u
197236 else
198- return sol == 0 || sol == nothing ? p : 0
237+ return isequal ( sol, 0 ) || sol = == nothing ? p : 0
199238 end
200239end
201240
202241function get_err (p, sol)
203242 if sol isa Tuple
204243 return sol[3 ]
205244 else
206- return sol == 0 || sol == nothing ? Inf : 0
245+ return isequal ( sol, 0 ) || sol = == nothing ? Inf : 0
207246 end
208247end
209248
356395function try_symbolic (eq, x, has_sym_consts = false , params = []; plan = default_plan ())
357396 y = integrate_symbolic (eq, x; plan)
358397
359- if y == nothing
398+ if y === nothing
360399 if has_sym_consts && ! isempty (params)
361400 @info (" Symbolic integration failed. Try changing constant parameters ([$(join (params, " , " )) ]) to numerical values." )
362401 end
0 commit comments