@@ -94,6 +94,12 @@ function _robust_real(x)
9494 return x_un
9595 end
9696
97+ # If the expression tree does not contain any Complex numbers or complex/imag operations,
98+ # then it is Real by definition (assuming variables are Real).
99+ if _is_manifestly_real (x_un)
100+ return x_un
101+ end
102+
97103 # Try symbolic simplification to see if it's real
98104 try
99105 nx = _safe_Num (x_un)
@@ -137,6 +143,66 @@ function _robust_real(x)
137143 return x_un
138144end
139145
146+ function _is_manifestly_real (x)
147+ if x isa Real
148+ return true
149+ end
150+ if x isa AbstractArray
151+ # Check if all elements are manifestly real
152+ for elem in x
153+ u = Symbolics. unwrap (elem)
154+ # Prevent infinite recursion for symbolic arrays where elements are getindex(self, ...)
155+ if Symbolics. iscall (u) && Symbolics. operation (u) == getindex
156+ args = Symbolics. arguments (u)
157+ # If the first argument is specifically the array itself (or wrapped version), skip
158+ # We check equality because unwrap behavior might differ
159+ if args[1 ] === x || args[1 ] == x
160+ continue
161+ end
162+ end
163+
164+ if ! _is_manifestly_real (u)
165+ return false
166+ end
167+ end
168+ return true
169+ end
170+ if x isa Complex
171+ return iszero (imag (x))
172+ end
173+ if SymbolicUtils. issym (x)
174+ return true
175+ end
176+
177+ if SymbolicUtils. iscall (x)
178+ op = SymbolicUtils. operation (x)
179+ if op == complex || op == Base. complex || op == imag || op == Base. imag
180+ return false
181+ end
182+
183+ # Check arguments recursively
184+ args = SymbolicUtils. arguments (x)
185+ for arg in args
186+ if ! _is_manifestly_real (arg)
187+ return false
188+ end
189+ end
190+ return true
191+ end
192+
193+ # Try to unwrap value (handles BasicSymbolicImpl wrappers around numbers)
194+ try
195+ v = Symbolics. value (x)
196+ if v != = x
197+ return _is_manifestly_real (v)
198+ end
199+ catch
200+ end
201+
202+ # Fallback for other types
203+ return false
204+ end
205+
140206"""
141207 AbstractIndexMatcher
142208
@@ -308,6 +374,7 @@ function _integrate_core(
308374 r_hypot_pow,
309375 r_float_to_int_pow, # Add float fix
310376 ])
377+ # println("DEBUG: Rewriting")
311378 expr_rewritten =
312379 SymbolicUtils. Postwalk (
313380 SymbolicUtils. PassThrough (chain);
@@ -365,8 +432,7 @@ function _integrate_core(
365432 end
366433
367434 expr_subbed = if expr_num isa Complex
368- robust_substitute (Symbolics. unwrap (real (expr_num)), subs_dict) +
369- im * robust_substitute (Symbolics. unwrap (imag (expr_num)), subs_dict)
435+ robust_substitute (Symbolics. unwrap (real (expr_num)), subs_dict) + im * robust_substitute (Symbolics. unwrap (imag (expr_num)), subs_dict)
370436 else
371437 robust_substitute (Symbolics. unwrap (expr_num), subs_dict)
372438 end
0 commit comments