5555
5656function _trig_expand_products (x:: BasicSymbolic )
5757 # Expand trig products/powers into sums so `get_independent` can isolate constants.
58- y = Postwalk (ex -> begin
59- if ispow (ex)
60- base, exponent = arguments (ex)
61- exp_val = SymbolicUtils. unwrap_const (exponent)
62- if exp_val isa Integer && exp_val == 2 && _is_sin_cos (base)
63- arg = first (arguments (base))
64- if operation (base) === cos
65- return (1 + cos (2 * arg)) / 2
66- else
67- return (1 - cos (2 * arg)) / 2
58+ y = Postwalk (
59+ ex -> begin
60+ if ispow (ex)
61+ base, exponent = arguments (ex)
62+ exp_val = SymbolicUtils. unwrap_const (exponent)
63+ if exp_val isa Integer && exp_val == 2 && _is_sin_cos (base)
64+ arg = first (arguments (base))
65+ if operation (base) === cos
66+ return (1 + cos (2 * arg)) / 2
67+ else
68+ return (1 - cos (2 * arg)) / 2
69+ end
6870 end
69- end
70- elseif ismul (ex)
71- # In SymbolicUtils v4, `arguments(ismul(...))` includes the numeric coefficient
72- # even though `ex.coeff` also stores it. Avoid double-counting it.
73- factors = BasicSymbolic[
74- f for f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number)
75- ]
76- trig_idx = findall (_is_sin_cos, factors)
77- if length (trig_idx) >= 2
78- i, j = trig_idx[1 ], trig_idx[2 ]
79- repl = _trig_mul_to_sum (factors[i], factors[j])
80- if repl != = nothing
81- others = BasicSymbolic[]
82- for (k, f) in pairs (factors)
83- (k == i || k == j) && continue
84- push! (others, f)
71+ elseif ismul (ex)
72+ # In SymbolicUtils v4, `arguments(ismul(...))` includes the numeric coefficient
73+ # even though `ex.coeff` also stores it. Avoid double-counting it.
74+ factors = BasicSymbolic[
75+ f for f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number)
76+ ]
77+ trig_idx = findall (_is_sin_cos, factors)
78+ if length (trig_idx) >= 2
79+ i, j = trig_idx[1 ], trig_idx[2 ]
80+ repl = _trig_mul_to_sum (factors[i], factors[j])
81+ if repl != = nothing
82+ others = BasicSymbolic[]
83+ for (k, f) in pairs (factors)
84+ (k == i || k == j) && continue
85+ push! (others, f)
86+ end
87+ coeff = ex. coeff
88+ return coeff * prod (others; init= 1 ) * repl
8589 end
86- coeff = ex. coeff
87- return coeff * prod (others; init= 1 ) * repl
8890 end
8991 end
90- end
91- return ex
92- end )(x)
92+ return ex
93+ end ,
94+ )(
95+ x
96+ )
9397 return SymbolicUtils. expand (y)
9498end
9599_trig_expand_products (x:: Num ) = wrap (_trig_expand_products (unwrap (x)))
@@ -227,7 +231,10 @@ function _strip_real_imag(x::BasicSymbolic)
227231 if coeff_val isa Number
228232 r = real (coeff_val)
229233 rest = prod (
230- (f for f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number));
234+ (
235+ f for
236+ f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number)
237+ );
231238 init= 1 ,
232239 )
233240 return iszero (r) ? 0 : r * rest
@@ -249,7 +256,10 @@ function _strip_real_imag(x::BasicSymbolic)
249256 if coeff_val isa Number
250257 i = imag (coeff_val)
251258 rest = prod (
252- (f for f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number));
259+ (
260+ f for
261+ f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number)
262+ );
253263 init= 1 ,
254264 )
255265 return iszero (i) ? 0 : i * rest
0 commit comments