Skip to content

Commit

Permalink
Merge pull request #534 from eschnett/patch-1
Browse files Browse the repository at this point in the history
simplify_rules.jl: Add rules for sinh, cosh
  • Loading branch information
shashi authored Aug 2, 2023
2 parents 9f5873e + 04ef715 commit be2ee4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/simplify_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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))
]
Expand Down
4 changes: 2 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ 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
if Base.@nany 9 i->fns[i] === op
return true
else
return any(has_trig_exp, arguments(term))
Expand Down
12 changes: 12 additions & 0 deletions test/rulesets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

@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) == sinh(x)^2
@eqtest simplify(sinh(x)^2 + 1) == cosh(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 * cosh(x)
@eqtest simplify(2cosh(x) * sinh(x)) == sinh(2x)
end

@testset "Exponentials" begin
Expand Down

0 comments on commit be2ee4a

Please sign in to comment.