From e46e806f0f148f80fb515f4df9ae83d108380c3d Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Mon, 10 Jul 2023 16:49:53 -0400 Subject: [PATCH 1/5] simplify_rules.jl: Add rules for sinh, cosh Add rules for sinh, cosh, similar to those for sin and cos. --- src/simplify_rules.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/simplify_rules.jl b/src/simplify_rules.jl index 88a1a6482..b12a9f272 100644 --- a/src/simplify_rules.jl +++ b/src/simplify_rules.jl @@ -43,7 +43,6 @@ let MUL_DISTRIBUTE = @ordered_acrule((~x)^(~n) * (~x)^(~m) => (~x)^(~n + ~m)) - CANONICALIZE_POW = [ @rule(^(*(~~x), ~y::_isinteger) => *(map(a->pow(a, ~y), ~~x)...)) @rule((((~x)^(~p::_isinteger))^(~q::_isinteger)) => (~x)^((~p)*(~q))) @@ -91,6 +90,13 @@ let @acrule(cot(~x)^2 + 1 => csc(~x)^2) @acrule(csc(~x)^2 + -1 => cot(~x)^2) + @acrule(cosh(~x)^2 + -1*sinh(~x)^2 => one(~x)) + @acrule(cosh(~x)^2 + -1 => sinh(~x)^2) + @acrule(sinh(~x)^2 + 1 => cosh(~x)^2) + + @acrule(cosh(~x)^2 + sinh(~x)^2 => cosh(2 * ~x)) + @acrule(cosh(~x) * sinh(~x) => sinh(2 * ~x)/2) + @acrule(exp(~x) * exp(~y) => _iszero(~x + ~y) ? 1 : exp(~x + ~y)) @rule(exp(~x)^(~y) => exp(~x * ~y)) ] From 618f5c1e1dc7267a0a444a7c97231fce8fd4bec2 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Thu, 27 Jul 2023 15:42:10 -0400 Subject: [PATCH 2/5] Add tests for cosh, sinh rules --- test/rulesets.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/rulesets.jl b/test/rulesets.jl index cd1ada549..15bf49a63 100644 --- a/test/rulesets.jl +++ b/test/rulesets.jl @@ -100,13 +100,25 @@ end @eqtest simplify(1 + y + cot(x)^2) == csc(x)^2 + y @eqtest simplify(cos(x)^2 - 1) == -sin(x)^2 @eqtest simplify(sin(x)^2 - 1) == -cos(x)^2 + + @test simplify(cosh(x)^2 + 1 - sinh(x)^2) == 2 + @test simplify(cosh(y)^2 + 1 - sinh(y)^2) == 2 + @test simplify(-sinh(y)^2 + cosh(y)^2 + 1) == 2 + + @eqtest simplify(cosh(x)^2 - 1) == sin(x)^2 + @eqtest simplify(sinh(x)^2 + 1) == cos(x)^2 end @testset "Double angle formulas" begin @syms r x + @eqtest simplify(r * cos(x / 2)^2 - r * sin(x / 2)^2) == r * cos(x) @eqtest simplify(r * sin(x / 2)^2 - r * cos(x / 2)^2) == -r * cos(x) @eqtest simplify(2cos(x) * sin(x)) == sin(2x) + + @eqtest simplify(r * cosh(x / 2)^2 + r * sinh(x / 2)^2) == r * cosh(x) + @eqtest simplify(r * sinh(x / 2)^2 + r * cosh(x / 2)^2) == r * cos(x) + @eqtest simplify(2cosh(x) * sinh(x)) == sin(2x) end @testset "Exponentials" begin From 17b3b74d7a357aaf74bedbb3a12e6394035a152e Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Thu, 27 Jul 2023 15:43:11 -0400 Subject: [PATCH 3/5] Enable rules for cosh, sinh --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 90e0de5cc..ae117f0d5 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -36,7 +36,7 @@ pow(x::Symbolic,y::Symbolic) = Base.:^(x,y) # Simplification utilities function has_trig_exp(term) !istree(term) && return false - fns = (sin, cos, tan, cot, sec, csc, exp) + fns = (sin, cos, tan, cot, sec, csc, exp, cosh, sinh) op = operation(term) if Base.@nany 7 i->fns[i] === op From 1b1c95b1cdf666619239ced320f65ba2085c082b Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Thu, 27 Jul 2023 16:44:25 -0400 Subject: [PATCH 4/5] Enable cosh/sinh transformations --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index ae117f0d5..acf9e92d6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -39,7 +39,7 @@ function has_trig_exp(term) fns = (sin, cos, tan, cot, sec, csc, exp, cosh, sinh) op = operation(term) - if Base.@nany 7 i->fns[i] === op + if Base.@nany 9 i->fns[i] === op return true else return any(has_trig_exp, arguments(term)) From 04ef71558f6db7bcbddd26e3c3027c5ac65f2bd1 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Thu, 27 Jul 2023 16:44:32 -0400 Subject: [PATCH 5/5] Correct cosh/sinh tests --- test/rulesets.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/rulesets.jl b/test/rulesets.jl index 15bf49a63..4730cc102 100644 --- a/test/rulesets.jl +++ b/test/rulesets.jl @@ -101,12 +101,12 @@ end @eqtest simplify(cos(x)^2 - 1) == -sin(x)^2 @eqtest simplify(sin(x)^2 - 1) == -cos(x)^2 - @test simplify(cosh(x)^2 + 1 - sinh(x)^2) == 2 - @test simplify(cosh(y)^2 + 1 - sinh(y)^2) == 2 - @test simplify(-sinh(y)^2 + cosh(y)^2 + 1) == 2 + @eqtest simplify(cosh(x)^2 + 1 - sinh(x)^2) == 2 + @eqtest simplify(cosh(y)^2 + 1 - sinh(y)^2) == 2 + @eqtest simplify(-sinh(y)^2 + cosh(y)^2 + 1) == 2 - @eqtest simplify(cosh(x)^2 - 1) == sin(x)^2 - @eqtest simplify(sinh(x)^2 + 1) == cos(x)^2 + @eqtest simplify(cosh(x)^2 - 1) == sinh(x)^2 + @eqtest simplify(sinh(x)^2 + 1) == cosh(x)^2 end @testset "Double angle formulas" begin @@ -117,8 +117,8 @@ end @eqtest simplify(2cos(x) * sin(x)) == sin(2x) @eqtest simplify(r * cosh(x / 2)^2 + r * sinh(x / 2)^2) == r * cosh(x) - @eqtest simplify(r * sinh(x / 2)^2 + r * cosh(x / 2)^2) == r * cos(x) - @eqtest simplify(2cosh(x) * sinh(x)) == sin(2x) + @eqtest simplify(r * sinh(x / 2)^2 + r * cosh(x / 2)^2) == r * cosh(x) + @eqtest simplify(2cosh(x) * sinh(x)) == sinh(2x) end @testset "Exponentials" begin