Skip to content

Commit

Permalink
function
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rbed committed Sep 8, 2024
1 parent e4474cc commit b9cd1f5
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/solver/postprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,8 @@ function _postprocess_root(x::SymbolicUtils.BasicSymbolic)
end
end

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

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
end
end
trig_simplified = check_trig_consts(x)
!isequal(trig_simplified, x) && return trig_simplified

if oper === (+)
args = arguments(x)
Expand Down Expand Up @@ -153,3 +135,33 @@ function postprocess_root(x)
end
x # unreachable
end

This comment has been minimized.

Copy link
@ChrisRackauckas

ChrisRackauckas Sep 8, 2024

Member

docstring?

function check_trig_consts(x)

This comment has been minimized.

Copy link
@ChrisRackauckas

ChrisRackauckas Sep 8, 2024

Member

does it only have to be trig?

!iscall(x) && return x

oper = operation(x)
inv_opers = [asin, acos, atan]
inv_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)
]

if any(isequal(oper, o) for o in inv_opers) && isempty(Symbolics.get_variables(x))
val = eval(Symbolics.toexpr(x))
for i in eachindex(inv_exacts)
exact_val = eval(Symbolics.toexpr(inv_exacts[i]))
if isapprox(exact_val, val, atol=1e-6)

This comment has been minimized.

Copy link
@ChrisRackauckas

ChrisRackauckas Sep 8, 2024

Member

why 1e-6? The default is probably better.

return inv_exacts[i]
elseif isapprox(-exact_val, val, atol=1e-6)
return -inv_exacts[i]
end
end
end

# add [sin, cos, tan] simplifications in the future?
return x
end

0 comments on commit b9cd1f5

Please sign in to comment.