# SymbolicUtils rules behave differenty for some trigonometric functions

**URL:** <https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438>\
**Category:** General Usage\
**Tags:** question, symbolics\
**Created:** [May 29, 2025, 2:00pm UTC](https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438 "2025-05-29T14:00:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)\
**Post date:** [May 29, 2025, 2:00pm UTC](https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438/1 "2025-05-29T14:00:34Z")

</div>

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

```julia
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

```julia
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

```julia
@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:

```julia
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:

```julia
# 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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 30, 2025, 8:39pm UTC](https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438/2 "2025-05-30T20:39:59Z")

</div>

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](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/v3.29.0/src/simplify_rules.jl) and find out which ones are acting up.

---

<div class="post-metadata">

**Author:** ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)\
**Post date:** [June 2, 2025, 1:45pm UTC](https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438/3 "2025-06-02T13:45:53Z")

</div>

> [@ChrisRackauckas](#):
>
> [SymbolicUtils.jl/src/simplify\_rules.jl at v3.29.0 · JuliaSymbolics/SymbolicUtils.jl](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/v3.29.0/src/simplify_rules.jl)

Perefct, I’ve [opened it](https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/748).

Since I needed this to be done last week, I used Mathematica’s [`TrigReduce`](https://reference.wolfram.com/language/ref/TrigReduce.html) and [`TrigExpand`](https://reference.wolfram.com/language/ref/TrigExpand.html) functions to accomplish my goal. But I’d still be nice to be able to replicate those functions within Symbolics.jl.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 6, 2025, 1:13pm UTC](https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438/4 "2025-06-06T13:13:46Z")

</div>

There is also [GitHub - eswagel/SymbolicsMathLink.jl: Julia package to seamlessly integrate Mathematica's MathLink with Julia's native Symbolics package](https://github.com/eswagel/SymbolicsMathLink.jl)

---

<div class="post-metadata">

**Author:** ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)\
**Post date:** [June 16, 2025, 1:52pm UTC](https://discourse.julialang.org/t/symbolicutils-rules-behave-differenty-for-some-trigonometric-functions/129438/5 "2025-06-16T13:52:40Z")

</div>

Sorry for the delayed response. Thanks Chris! This package looks useful for certain use cases.
