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
2 changes: 1 addition & 1 deletion src/solver/ia_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function isolate(lhs, var; warns=true, conditions=[])
new_var = (@variables $new_var)[1]
rhs = map(
sol -> term(rev_oper[oper], sol) +
term(*, Base.MathConstants.pi, 2 * new_var),
term(*, Base.MathConstants.pi, new_var),
rhs)
@info string(new_var) * " ϵ" * " Ζ"

Expand Down
58 changes: 50 additions & 8 deletions src/solver/postprocess.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Alex: make sure `Num`s are not processed here as they'd break it.
_postprocess_root(x) = x

Expand Down Expand Up @@ -32,30 +31,30 @@ function _postprocess_root(x::SymbolicUtils.BasicSymbolic)
!iscall(x) && return x

x = Symbolics.term(operation(x), map(_postprocess_root, arguments(x))...)
oper = operation(x)

# sqrt(0), cbrt(0) => 0
# sqrt(1), cbrt(1) => 1
if iscall(x) &&
(operation(x) === sqrt || operation(x) === cbrt || operation(x) === ssqrt ||
operation(x) === scbrt)
if (oper === sqrt || oper === cbrt || oper === ssqrt ||
oper === scbrt)
arg = arguments(x)[1]
if isequal(arg, 0) || isequal(arg, 1)
return arg
end
end

# (X)^0 => 1
if iscall(x) && operation(x) === (^) && isequal(arguments(x)[2], 0)
if oper === (^) && isequal(arguments(x)[2], 0)
return 1
end

# (X)^1 => X
if iscall(x) && operation(x) === (^) && isequal(arguments(x)[2], 1)
if oper === (^) && isequal(arguments(x)[2], 1)
return arguments(x)[1]
end

# sqrt((N / D)^2 * M) => N / D * sqrt(M)
if iscall(x) && (operation(x) === sqrt || operation(x) === ssqrt)
if (oper === sqrt || oper === ssqrt)
function squarefree_decomp(x::Integer)
square, squarefree = big(1), big(1)
for (p, d) in collect(Primes.factor(abs(x)))
Expand Down Expand Up @@ -90,7 +89,7 @@ function _postprocess_root(x::SymbolicUtils.BasicSymbolic)
end

# (sqrt(N))^M => N^div(M, 2)*sqrt(N)^(mod(M, 2))
if iscall(x) && operation(x) === (^)
if oper === (^)
arg1, arg2 = arguments(x)
if iscall(arg1) && (operation(arg1) === sqrt || operation(arg1) === ssqrt)
if arg2 isa Integer
Expand All @@ -105,6 +104,49 @@ function _postprocess_root(x::SymbolicUtils.BasicSymbolic)
end
end

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))
]

if oper === acos
for r in acos_rules
after_r = r(x)
!isnothing(after_r) && return after_r
end
elseif oper === asin
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
if isequal(arg, 0)
after_removing = setdiff(args, arg)
isone(length(after_removing)) && return after_removing[1]
return Symbolics.term(+, after_removing)
end
end
end

return x
end

Expand Down
Loading