Symbolics.jl simplify exp(0) to 1

I’m trying to check a system of linear differential equations using symbolic variables:
X'(t)=R(\alpha)X(t)
This is my code

using Symbolics
using SymbolicUtils
using LinearAlgebra

@variables α t

R = [cos(α) -sin(α); sin(α) cos(α)]

X = [im*exp(exp(α*im)*t) -im*exp(exp(-α*im)*t);
     exp(exp(α*im)*t) exp(exp(-α*im)*t)]


# Check the fundamental matrix
Dt = Differential(t)
dX = Dt.(X)

dX = expand_derivatives.(dX)

dX = expand.(dX)

H = simplify.(dX.-R*X)

display(H)

H = substitute.(H, (Dict(exp(0) => 1),))

display(H)

However the terms in the final output always contain things like “exp(0)”, eventhough the solution should be correct:

2×2 Matrix{Complex{Num}}:
 0  cos(-α)*sin(t*exp(0)*sin(-α))*exp(t*exp(0)*cos(-α)) - cos(α)*sin(t*exp(0)*sin(-α))*exp(t*exp(0)*cos(-α)) + (sin(-α) + sin(α))*exp(t*exp(0)*cos(-α))*cos(t*exp(0)*sin(-α)) + im*(-cos(-α)*exp(t*exp(0)*cos(-α))*cos(t*exp(0)*sin(-α)) + cos(α)*exp(t*exp(0)*cos(-α))*cos(t*exp(0)*sin(-α)) + sin(t*exp(0)*sin(-α))*(sin(-α) + sin(α))*exp(t*exp(0)*cos(-α)))
 0   cos(-α)*exp(t*exp(0)*cos(-α))*cos(t*exp(0)*sin(-α)) - cos(α)*exp(t*exp(0)*cos(-α))*cos(t*exp(0)*sin(-α)) - sin(t*exp(0)*sin(-α))*(sin(-α) + sin(α))*exp(t*exp(0)*cos(-α)) + im*(cos(-α)*sin(t*exp(0)*sin(-α))*exp(t*exp(0)*cos(-α)) - cos(α)*sin(t*exp(0)*sin(-α))*exp(t*exp(0)*cos(-α)) + (sin(-α) + sin(α))*exp(t*exp(0)*cos(-α))*cos(t*exp(0)*sin(-α)))

For the past hour I’ve tried every combination of “substitute.”, “expand.”, “simplify.” , but it doesn’t even get rid of the exp(0).

It would also be nice to have things like \cos(-\alpha) = \cos(\alpha) and \sin(-\alpha)=-\sin(\alpha).

Thanks for any help!

It would be nice to have such simple rules available by default. You could define the rules yourself.

For example:

rules = RuleSet([
    @rule(exp(0) => 1),
    @rule(sin(-(~x)) => -(sin(~x))),
    @rule(cos(-(~x)) => cos(~x))
])

H2 = map(H) do h
    re = simplify(real(h), rewriter=rules)
    im_part = simplify(imag(h), rewriter=rules)
    re + im * im_part
end

Will yield

2×2 Matrix{Complex{Num}}:
 0  cos(-α)*sin(t*sin(-α))*exp(t*cos(-α)) - cos(α)*sin(t*sin(-α))*exp(t*cos(-α)) + (sin(-α) + sin(α))*cos(t*sin(-α))*exp(t*cos(-α)) + im*(-cos(-α)*cos(t*sin(-α))*exp(t*cos(-α)) + cos(α)*cos(t*sin(-α))*exp(t*cos(-α)) + (sin(-α) + sin(α))*sin(t*sin(-α))*exp(t*cos(-α)))
 0   cos(-α)*cos(t*sin(-α))*exp(t*cos(-α)) - cos(α)*cos(t*sin(-α))*exp(t*cos(-α)) - (sin(-α) + sin(α))*sin(t*sin(-α))*exp(t*cos(-α)) + im*(cos(-α)*sin(t*sin(-α))*exp(t*cos(-α)) - cos(α)*sin(t*sin(-α))*exp(t*cos(-α)) + (sin(-α) + sin(α))*cos(t*sin(-α))*exp(t*cos(-α)))

I’m not sure why the cos(-α) is not taking…

edit:

This works:

rules = RuleSet([
    @rule(exp(0) => 1),
    @rule(sin((-1) * (~x)) => -(sin(~x))),
    @rule(cos((-1) * (~x)) => cos(~x)),
])

that yields

2×2 Matrix{Complex{Num}}:
 0  0
 0  0
8 Likes