Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions Manifest.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ rules() = [
rules(:BOOLEAN)
rules(:CALCULUS)
rules(:LOGARITHM)
rules(:EXP)
rules(:TRIGONOMETRY)
rules(:TYPES)
]
Expand Down Expand Up @@ -219,6 +220,21 @@ function rules(::Val{:LOGARITHM})
]
end

function rules(::Val{:EXP})
@vars α β n
@term RULES [
exp(*(2, π, im)) => 1
exp(π * im) => -1
exp(π * im * inv(2)) => im
exp(π * im * inv(3)) => 1 / 2 + √3 / 2 * im
exp(π * im * inv(4)) => √2 / 2 + √2 / 2 * im
exp(π * im * inv(6)) => √3 / 2 + 1 / 2 * im

exp(α) * exp(β) => exp(α + β)
exp(α)^n => exp(α * n)
]
end

function rules(::Val{:TRIGONOMETRY})
@vars α β θ

Expand Down
4 changes: 3 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export Variable, @vars
using MacroTools

macro term(ex)
:(convert(Term, $(esc(_term(ex)))))
:(convert(Term, $(esc(_term(ex)))))
end
function _term(ex::Expr)
ex.head === :$ && return ex.args[1]
Expand Down Expand Up @@ -69,6 +69,8 @@ function _show_term(f::Function)
return f
end
end

_show_term(::Irrational{sym}) where sym = sym
_show_term(x::Symbolic) = x.name
_show_term(x::Variable) = x.name
_show_term(x::Symbol) = Meta.quot(x)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
is_constant(t::Term) = is_constant(get(t))
is_constant(x) = typeof(x) [Expr, Symbolic, Variable]
is_constant(x) = !any(T->typeof(x) <: T, [Expr, Symbolic, Variable, Irrational])

is_ground(t::Term) = is_ground(get(t))
is_ground(ex::Expr) = all(is_ground, ex.args)
Expand Down
17 changes: 17 additions & 0 deletions test/rules.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Test
using Simplify
using Simplify: PatternRule, EvalRule, OrderRule
using Simplify: diff
using SpecialSets
Expand Down Expand Up @@ -251,6 +253,21 @@ end
end
end

@testset "EXP" begin
@vars α β n
@test normalize(@term(exp(2π * im))) == @term(1)
@test normalize(@term(exp(π * im))) == @term(-1)
@test normalize(@term(exp(π * im * inv(2)))) == @term(im)

# actually broken: the order of imaginary and real is not correct
@test normalize(@term(exp(π * im * inv(3)))) == @term(√3 * inv(2) * im + inv(2))
@test normalize(@term(exp(π * im * inv(4)))) == @term(√2 * inv(2) + √2 * inv(2) * im)
@test normalize(@term(exp(π * im * inv(6)))) == @term(inv(2) * im + √3 * inv(2))

@test normalize(@term(exp(α) * exp(β))) == @term(exp(α + β))
@test normalize(@term(exp(α)^n)) == @term(exp(α * n))
@test normalize(@term(exp(π * im))) == @term(-1)
end

@testset "custom" begin
@syms a
Expand Down