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

Simplifying acos(0/1), asin(0,1) and anything + 0 #1264

Merged
merged 14 commits into from
Sep 14, 2024
6 changes: 2 additions & 4 deletions src/solver/attract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,8 @@ function attract_trig(lhs, var)
r_trig = [@acrule(sin(~x::(contains_var))^2 + cos(~x::(contains_var))^2=>one(~x))
@acrule(sin(~x::(contains_var))^2 + -1=>-1 * cos(~x)^2)
@acrule(cos(~x::(contains_var))^2 + -1=>-1 * sin(~x)^2)
@acrule(cos(~x::(contains_var))^2 + -1 * sin(~x::(contains_var))^2=>cos(2 *
~x))
@acrule(sin(~x::(contains_var))^2 + -1 * cos(~x::(contains_var))^2=>-cos(2 *
~x))
@acrule(cos(~x::(contains_var))^2 + -1 * sin(~x::(contains_var))^2=>cos(2*~x))
@acrule(sin(~x::(contains_var))^2 + -1 * cos(~x::(contains_var))^2=>-cos(2*~x))
@acrule(cos(~x::(contains_var)) * sin(~x::(contains_var))=>sin(2 * ~x) / 2)
@acrule(tan(~x::(contains_var))^2 + -1 * sec(~x::(contains_var))^2=>one(~x))
@acrule(-1 * tan(~x::(contains_var))^2 + sec(~x::(contains_var))^2=>one(~x))
Expand Down
35 changes: 26 additions & 9 deletions src/solver/postprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,38 @@ function _postprocess_root(x::SymbolicUtils.BasicSymbolic)
end
end

arg = arguments(x)[1]
isnegone(x) = isequal(-1, expand(x))
ishalf(x) = isequal(1/2, expand(x))
isneghalf(x) = isequal(-1/2, expand(x))
symiszero(x) = isequal(0, expand(x))
symisone(x) = isequal(1, expand(x))
acos_rules = [(@rule acos(~x::symiszero) => Symbolics.term(/, pi, 2)),
(@rule acos(~x::symisone) => 0),
(@rule acos(~x::isnegone) => Symbolics.term(*, pi)),
(@rule acos(~x::ishalf) => Symbolics.term(/, pi, 3)),
(@rule acos(~x::isneghalf) => Symbolics.term(/, Symbolics.term(*,2,pi), 3))
]

asin_rules = [(@rule asin(~x::symiszero) => 0),
(@rule asin(~x::symisone) => Symbolics.term(/, pi, 2)),
(@rule asin(~x::isnegone) => -Symbolics.term(/, pi, 2)),
(@rule asin(~x::ishalf) => Symbolics.term(/, pi, 6)),
(@rule asin(~x::isneghalf) => Symbolics.term(/, Symbolics.term(*,-1,pi), 6))
]
n0rbed marked this conversation as resolved.
Show resolved Hide resolved

if oper === acos
if arg === 0
return Symbolics.term(/, pi, 2)
elseif arg === 1
return 0
for r in acos_rules
after_r = r(x)
!isnothing(after_r) && return after_r
end
elseif oper === asin
if arg === 0
return 0
elseif arg === 1
return Symbolics.term(/, pi, 2)
for r in asin_rules
after_r = r(x)
!isnothing(after_r) && return after_r
end
end


if oper === (+)
args = arguments(x)
for arg in args
Expand Down
Loading