Skip to content

Commit

Permalink
Completes mpfi.jl and fi.lib.jl (#390)
Browse files Browse the repository at this point in the history
* Add cot function

* Add coth function

* Improve rounding of cbrt

* add csc function

* Add csch function

* Add hypot function

* Add sec function

* Add sech function

* add tests

* Add rounding for cot
  • Loading branch information
krish8484 committed Jun 27, 2020
1 parent 095c6d4 commit b42eee9
Show file tree
Hide file tree
Showing 8 changed files with 2,983 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import Base:
+, -, *, /, //, fma,
<, >, ==, !=, , ^, <=,
in, zero, one, eps, typemin, typemax, abs, abs2, real, min, max,
sqrt, exp, log, sin, cos, tan, inv, cbrt,
sqrt, exp, log, sin, cos, tan, cot, inv, cbrt, csc, hypot, sec,
exp2, exp10, log2, log10,
asin, acos, atan,
sinh, cosh, tanh, asinh, acosh, atanh, sinpi, cospi,
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, sinpi, cospi,
union, intersect, isempty,
convert, promote_rule, eltype, size,
BigFloat, float, widen, big,
Expand Down
11 changes: 5 additions & 6 deletions src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,6 @@ function sqrt(a::Interval{T}) where T
end


function cbrt(a::Interval{T}) where T
isempty(a) && return a

@round(cbrt(a.lo), cbrt(a.hi))
end

"""
pow(x::Interval, n::Integer)
Expand Down Expand Up @@ -290,7 +285,7 @@ for f in (:exp, :expm1)
end
end

for f in (:exp2, :exp10)
for f in (:exp2, :exp10, :cbrt)

@eval function ($f)(x::BigFloat, r::RoundingMode) # add BigFloat functions with rounding:
setrounding(BigFloat, r) do
Expand Down Expand Up @@ -328,3 +323,7 @@ function log1p(a::Interval{T}) where T

@round( log1p(a.lo), log1p(a.hi) )
end

hypot(a::Interval{BigFloat}, b::Interval{BigFloat}) = sqrt(a^2 + b^2)

hypot(a::Interval{T}, b::Interval{T}) where T= atomic(Interval{T}, hypot(big53(a), big53(b)))
7 changes: 6 additions & 1 deletion src/intervals/hyperbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ function atanh(a::Interval{BigFloat})
return Interval(res_lo, res_hi)
end

coth(a::Interval{BigFloat}) = 1/tanh(a)

csch(a::Interval{BigFloat}) = 1/sinh(a)

sech(a::Interval{BigFloat}) = 1/cosh(a)
# Float64 versions of functions missing from CRlibm:
for f in (:tanh, :asinh, :acosh, :atanh)
for f in (:tanh, :asinh, :acosh, :atanh, :coth, :csch, :sech)

@eval function ($f)(a::Interval{Float64})
isempty(a) && return a
Expand Down
8 changes: 4 additions & 4 deletions src/intervals/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ end

# improved error-free arithmetic by RoundingEmulator.jl:
for T in (Float32, Float64)
for (op, f) in ( (:+, :add), (:-, :sub), (:*, :mul), (:/, :div), (:sqrt, :sqrt), (:cbrt, :cbrt) )
for (op, f) in ( (:+, :add), (:-, :sub), (:*, :mul), (:/, :div), (:sqrt, :sqrt))
for (mode, suffix) in zip((:Down, :Up), (:_down, :_up))
mode1 = Expr(:quote, mode)
mode1 = :(::RoundingMode{$mode1})
Expand Down Expand Up @@ -206,7 +206,7 @@ for mode in (:Down, :Up)


# functions not in CRlibm:
for f in (:sqrt, :inv, :tanh, :asinh, :acosh, :atanh, :cbrt)
for f in (:sqrt, :inv, :tanh, :asinh, :acosh, :atanh, :cot)


@eval function $f(::IntervalRounding{:slow},
Expand Down Expand Up @@ -274,7 +274,7 @@ function _setrounding(::Type{Interval}, rounding_type::Symbol)

# unary functions:

for f in (:sqrt, :inv, :cbrt)
for f in (:sqrt, :inv)
@eval $f(a::T, r::RoundingMode) where {T<:AbstractFloat} = $f($roundtype, a, r)
end

Expand All @@ -293,7 +293,7 @@ function _setrounding(::Type{Interval}, rounding_type::Symbol)

# unary functions:
for f in vcat(CRlibm.functions,
[:tanh, :asinh, :acosh, :atanh])
[:tanh, :asinh, :acosh, :atanh, :cot])

@eval $f(a::T, r::RoundingMode) where {T<:AbstractFloat} = $f($roundtype, a, r)

Expand Down
39 changes: 39 additions & 0 deletions src/intervals/trigonometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,42 @@ function atan(y::Interval{BigFloat}, x::Interval{BigFloat})

end
end

function cot(a::Interval{BigFloat})
isempty(a) && return a

diam(a) > Interval{BigFloat}(π).lo && return entireinterval(a)

lo_quadrant, lo = quadrant(Float64(a.lo))
hi_quadrant, hi = quadrant(Float64(a.hi))



lo_quadrant_mod = mod(lo_quadrant, 2)
hi_quadrant_mod = mod(hi_quadrant, 2)

if(lo_quadrant_mod == 1 && hi_quadrant_mod == 0)
if(lo_quadrant < hi_quadrant)
return entireinterval(a)
else
return @round(-Inf, cot(a.lo))
end
elseif(lo_quadrant_mod == 0 && hi_quadrant_mod == 0 && lo_quadrant > hi_quadrant)
return @round(-Inf, cot(a.lo))
end

return @round(cot(a.hi), cot(a.lo))
end

csc(a::Interval{BigFloat}) = 1/sin(a)

sec(a::Interval{BigFloat}) = 1/cos(a)

for f in (:cot, :csc, :sec)

@eval function ($f)(a::Interval{T}) where T
isempty(a) && return a

atomic(Interval{T}, ($f)(big53(a)) )
end
end
2 changes: 2 additions & 0 deletions test/ITF1788_tests/ITF1788_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ include("libieeep1788_tests_overlap.jl")
include("libieeep1788_tests_rec_bool.jl")
include("libieeep1788_tests_rev.jl")
include("libieeep1788_tests_set.jl")
include("mpfi.jl")
include("fi_lib.jl")
1,035 changes: 1,035 additions & 0 deletions test/ITF1788_tests/fi_lib.jl

Large diffs are not rendered by default.

1,890 changes: 1,890 additions & 0 deletions test/ITF1788_tests/mpfi.jl

Large diffs are not rendered by default.

0 comments on commit b42eee9

Please sign in to comment.