@@ -56,19 +56,23 @@ const _rw_trig_expand = SymbolicUtils.Rewriters.Fixpoint(
5656
5757function _trig_expand_products (x:: BasicSymbolic )
5858 # Expand trig products/powers into sums so `get_independent` can isolate constants.
59- y = Postwalk (ex -> begin
60- if ismul (ex)
61- # In SymbolicUtils v4, `arguments(ismul(...))` includes the numeric coefficient
62- # even though `ex.coeff` also stores it. Avoid double-counting it.
63- coeff = ex. coeff
64- factors = BasicSymbolic[
65- f for f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number)
66- ]
67- rest = isempty (factors) ? 1 : prod (factors; init= 1 )
68- return coeff * _rw_trig_expand (rest)
69- end
70- return _rw_trig_expand (ex)
71- end )(x)
59+ y = Postwalk (
60+ ex -> begin
61+ if ismul (ex)
62+ # In SymbolicUtils v4, `arguments(ismul(...))` includes the numeric coefficient
63+ # even though `ex.coeff` also stores it. Avoid double-counting it.
64+ coeff = ex. coeff
65+ factors = BasicSymbolic[
66+ f for f in arguments (ex) if ! (SymbolicUtils. unwrap_const (f) isa Number)
67+ ]
68+ rest = isempty (factors) ? 1 : prod (factors; init= 1 )
69+ return coeff * _rw_trig_expand (rest)
70+ end
71+ return _rw_trig_expand (ex)
72+ end ,
73+ )(
74+ x
75+ )
7276 return SymbolicUtils. expand (y)
7377end
7478_trig_expand_products (x:: Num ) = wrap (_trig_expand_products (unwrap (x)))
@@ -401,13 +405,21 @@ function _normalize_trig_signs(x::BasicSymbolic)
401405 arg = first (arguments (x))
402406 if SymbolicUtils. isnegative (arg)
403407 new_arg = - arg
404- return op === sin ? - sin (new_arg) : cos (new_arg)
408+ return if op === sin
409+ - SymbolicUtils. term (sin, new_arg)
410+ else
411+ SymbolicUtils. term (cos, new_arg)
412+ end
405413 elseif ismul (arg)
406414 coeff_val = SymbolicUtils. unwrap_const (arg. coeff)
407415 # Handle coefficients that are complex with zero imaginary part, e.g. (-2 + 0im)θ.
408416 if coeff_val isa Number && isreal (coeff_val) && real (coeff_val) < 0
409417 new_arg = - arg
410- return op === sin ? - sin (new_arg) : cos (new_arg)
418+ return if op === sin
419+ - SymbolicUtils. term (sin, new_arg)
420+ else
421+ SymbolicUtils. term (cos, new_arg)
422+ end
411423 end
412424 end
413425 end
0 commit comments