SymbolicUtils rules behave differenty for some trigonometric functions

Hello. I’m trying to use SymbolicUtils to transform a product of simple trigonometric functions using some trigonometric identities. I have found some problems when applying the rules I define. I would like to understand what I’m doing wrong.

Problem 1
A minimal working example involving the sinus of the double angle is

using SymbolicUtils

@syms a
r = @acrule(sin(2*~x) => 2*sin(~x)*cos(~x))

# This works and returns 2cos(a)*sin(a)
simplify(sin(2a), r)

# This doesn't work and returns (1//4)*sin(2a)
simplify(sin(2a) / 4, r)

As you can see, the latest call should return cos(a)*sin(a)//2, but instead does nothing. I have to define now another rule to make this work

using SymbolicUtils

@syms a

rules = [
    @acrule(sin(2*~x) => 2 * sin(~x) * cos(~x))
    @acrule(~a * sin(2*~x) => ~a * 2 * sin(~x) * cos(~x))
]

r = RestartedChain(rules)

# Now it works and returns (1//2)*cos(a)*sin(a)
simplify(sin(2a) / 4, r)

Why do I have to define the rule like that? Why is it not understanding that we want to apply the rule to the trigonometric expression and then multiply by the number? For example, if I now want to compute sin(2a) / sin(2b), it returns sin(2a) / sin(2b) instead of the correct result. It does not apply the rule to the numerator and the denominator. I’ve tried adding something like

@acrule(sin(~~x) / sin(~~y) => sin((~~x)...) / sin((~~y)...))

but it doesn’t work either.

Problem 2
If I instead define the following rule for the multiplication of two cosines and do the same naïve computation like the sin(a) / 4 one, now it is working:

using SymbolicUtils

@syms λ a₁ a₂ a₃ a₄

r = @acrule cos(~x) * cos(~y) => (cos(~x - ~y) + cos(~x + ~y)) / 2

# Returns the correct result
# (1//32)*(cos(a₁*λ + a₂*λ) + cos(a₁*λ - a₂*λ))*(cos(a₃*λ + a₄*λ) + cos(-a₃*λ + a₄*λ))
simplify(cos(a₁ * λ) * cos(a₂ * λ) * cos(a₃ * λ) * cos(a₄ * λ) / 8, r)

Look how now it returns the correct result when I divide by 8 the expression. It even recognizes that there is a pair of two cosines being multiplied. Why is it working now, but not with the previous example?

If I now, divide by λ instead of by 8, it no longer works:

# Returns again (cos(a₂*λ)*cos(a₃*λ)*cos(a₄*λ)*cos(a₁*λ)) / (λ^2)
simplify(cos(a₁ * λ) * cos(a₂ * λ) * cos(a₃ * λ) * cos(a₄ * λ) / λ^2, r)

Could you tell me which kinds of rules am I missing?

Open an issue to SymbolicUtils.jl. I don’t think the answer will be quick here, one would need to dive into the rules SymbolicUtils.jl/src/simplify_rules.jl at v3.29.0 · JuliaSymbolics/SymbolicUtils.jl · GitHub and find out which ones are acting up.

1 Like