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
4 changes: 0 additions & 4 deletions ext/SymbolicsGroebnerExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,9 @@ end
# Helps with precompilation time
PrecompileTools.@setup_workload begin
@variables a b c x y z
equation1 = a*log(x)^b + c ~ 0
equation_actually_polynomial = sin(x^2 +1)^2 + sin(x^2 + 1) + 3
simple_linear_equations = [x - y, y + 2z]
equations_intersect_sphere_line = [x^2 + y^2 + z^2 - 9, x - 2y + 3, y - z]
PrecompileTools.@compile_workload begin
symbolic_solve(equation1, x)
symbolic_solve(equation_actually_polynomial)
symbolic_solve(simple_linear_equations, [x, y], warns=false)
symbolic_solve(equations_intersect_sphere_line, [x, y, z], warns=false)
end
Expand Down
6 changes: 6 additions & 0 deletions ext/SymbolicsNemoExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ end
PrecompileTools.@setup_workload begin
@variables a b c x y z
expr_with_params = expand((x + b)*(x^2 + 2x + 1)*(x^2 - a))
equation1 = a*log(x)^b + c ~ 0
equation_polynomial = 9^x + 3^x + 2
exp_eq = 5*2^(x+1) + 7^(x+3)
PrecompileTools.@compile_workload begin
symbolic_solve(equation1, x)
symbolic_solve(equation_polynomial, x)
symbolic_solve(exp_eq)
symbolic_solve(expr_with_params, x, dropmultiplicity=false)
symbolic_solve(x^10 - a^10, x, dropmultiplicity=false)
end
Expand Down
43 changes: 16 additions & 27 deletions src/solver/postprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,27 @@ 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))
opers = [acos, asin, atan]
exacts = [0, Symbolics.term(*, pi), Symbolics.term(/,pi,3),
Symbolics.term(/, pi, 2),
Symbolics.term(/, Symbolics.term(*, 2, pi), 3),
Symbolics.term(/, pi, 6),
Symbolics.term(/, Symbolics.term(*, 5, pi), 6),
Symbolics.term(/, pi, 4)
]

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
if any(isequal(oper, o) for o in opers) && isempty(Symbolics.get_variables(x))
val = eval(Symbolics.toexpr(x))
for i in eachindex(exacts)
exact_val = eval(Symbolics.toexpr(exacts[i]))
if isapprox(exact_val, val, atol=1e-6)
return exacts[i]
elseif isapprox(-exact_val, val, atol=1e-6)
return -exacts[i]
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is easier to extend id think, just add more exacts into exacts and the loop will check if the asin, acos, or atan evaluate into it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then make it a function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okok

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, and changed eval -> symbolic_to_float

end
end


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