Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Exp rules #80

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# This file is machine-generated - editing it directly is not advised

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[CSTParser]]
deps = ["Tokenize"]
git-tree-sha1 = "c69698c3d4a7255bc1b4bc2afc09f59db910243b"
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
version = "0.6.2"

[[Combinatorics]]
deps = ["LinearAlgebra", "Polynomials", "Test"]
git-tree-sha1 = "50b3ae4d643dc27eaff69fb6be06ee094d5500c9"
Expand All @@ -9,9 +17,15 @@ version = "0.7.0"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "ec61a16eed883ad0cfa002d7489b3ce6d039bb9a"
git-tree-sha1 = "84aa74986c5b9b898b0d1acaf3258741ee64754f"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "1.4.0"
version = "2.1.0"

[[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "0809951a1774dc724da22d26e4289bbaab77809a"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.0"

[[Dates]]
deps = ["Printf"]
Expand All @@ -23,12 +37,12 @@ uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[DiffRules]]
deps = ["Random", "Test"]
git-tree-sha1 = "c49ec69428ffea0c1d1bbdc63d1a70f5df5860ad"
git-tree-sha1 = "dc0869fb2f5b23466b32ea799bd82c76480167f7"
uuid = "b552c78f-8df3-52c6-915a-8e097449b14b"
version = "0.0.7"
version = "0.0.10"

[[Distributed]]
deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[InteractiveUtils]]
Expand All @@ -49,10 +63,10 @@ uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[MacroTools]]
deps = ["Compat"]
git-tree-sha1 = "c443e1c8d58a4e9f61b708ad0a88286c7042145b"
deps = ["CSTParser", "Compat", "DataStructures", "Test", "Tokenize"]
git-tree-sha1 = "d6e9dedb8c92c3465575442da456aec15a89ff76"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.4.4"
version = "0.5.1"

[[Markdown]]
deps = ["Base64"]
Expand All @@ -61,15 +75,21 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[OrderedCollections]]
deps = ["Random", "Serialization", "Test"]
git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.1.0"

[[Pkg]]
deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Polynomials]]
deps = ["LinearAlgebra", "SparseArrays", "Test"]
git-tree-sha1 = "1a1eae52956658a6acae6fa1b6d6c3d488192895"
git-tree-sha1 = "62142bd65d3f8aeb2226ec64dd8493349147df94"
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
version = "0.5.1"
version = "0.5.2"

[[Printf]]
deps = ["Unicode"]
Expand Down Expand Up @@ -116,6 +136,11 @@ uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[Tokenize]]
git-tree-sha1 = "dfcdbbfb2d0370716c815cbd6f8a364efb6f42cf"
uuid = "0796e94c-ce3b-5d07-9a54-7f471281c624"
version = "0.5.6"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
Expand Down
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