Skip to content

Reduction function #5

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export
cancelminus, cancelplus, isunbounded,
.., @I_str, ±,
pow, extended_div,
setformat, @format
setformat, @format , vector_sum, vector_dot,
vector_sumAbs, vector_sumSquare

if VERSION >= v"1.5.0-DEV.124"
import Base: isdisjoint
Expand All @@ -83,7 +84,6 @@ export showfull
import Base: rounding, setrounding, setprecision



## Multidimensional
export
IntervalBox
Expand Down
1 change: 1 addition & 0 deletions src/intervals/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ include("functions.jl")
include("trigonometric.jl")
include("hyperbolic.jl")
include("complex.jl")
include("reduction.jl")

# Syntax for intervals

Expand Down
24 changes: 24 additions & 0 deletions src/intervals/reduction.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file is part of the IntervalArithmetic.jl package; MIT licensed

"""
The file contains the reduction functions stated in the section 12.12.12 with correctly rounded results.
r is the rounding direction which takes 0.0 for RoundingToZero , 0.5 for RoundingNearest , +Inf for RoundingUp and -Inf for RoundingDown.
"""
function vector_sum(v :: Vector{T} , r :: RoundingMode) where T
sum1 = sum(BigFloat.(v))
return T(sum1, r)
end

function vector_dot(v :: Vector{T}, u :: Vector{T}, r :: RoundingMode) where T
sum1 = sum(BigFloat.(v) .* BigFloat.(u))
return T(sum1, r)
end

function vector_sumAbs(v :: Vector{T} , r :: RoundingMode) where T
sum1 = sum(BigFloat.(abs.(v)))
return T(sum1, r)
end

function vector_sumSquare(v :: Vector{T} , r :: RoundingMode) where T
return vector_dot(v, v, r)
end
1 change: 1 addition & 0 deletions test/ITF1788_tests/ITF1788_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ include("libieeep1788_tests_rev.jl")
include("libieeep1788_tests_set.jl")
include("mpfi.jl")
include("fi_lib.jl")
include("libieeep1788_reduction.jl")
64 changes: 64 additions & 0 deletions test/ITF1788_tests/libieeep1788_reduction.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#=

Unit tests from libieeep1788 for reduction operations
(Original author: Marco Nehmeier)
converted into portable ITL format by Oliver Heimlich.

Copyright 2013-2015 Marco Nehmeier ([email protected])
Copyright 2015-2017 Oliver Heimlich ([email protected])

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=#
#Language imports

#Test library imports
using Test

#Arithmetic library imports
using IntervalArithmetic

#Preamble
setprecision(256)
setprecision(Interval, Float64)
setrounding(Interval, :tight)
# Set full format, and show decorations
@format full

@testset "minimal_sum_test" begin
@test vector_sum([1.0, 2.0, 3.0], RoundNearest) == 6.0
@test isnan(vector_sum([1.0, 2.0, NaN, 3.0], RoundNearest))
@test isnan(vector_sum([1.0, -Inf, 2.0, Inf, 3.0], RoundNearest))
end

@testset "minimal_sum_abs_test" begin
@test vector_sumAbs([1.0, -2.0, 3.0], RoundNearest) == 6.0
@test isnan(vector_sumAbs([1.0, -2.0, NaN, 3.0], RoundNearest))
@test vector_sumAbs([1.0, -Inf, 2.0, Inf, 3.0], RoundNearest) == Inf
end

@testset "minimal_sum_sqr_test" begin
@test vector_sumSquare([1.0, 2.0, 3.0], RoundNearest) == 14.0
@test isnan(vector_sumSquare([1.0, 2.0, NaN, 3.0], RoundNearest))
@test vector_sumSquare([1.0, -Inf, 2.0, Inf, 3.0], RoundNearest) == Inf
end

@testset "minimal_dot_test" begin
@test vector_dot([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], RoundNearest) == 14.0
@test vector_dot([0x10000000000001p0, 0x1p104], [0x0fffffffffffffp0, -1.0], RoundNearest) == -1.0
@test isnan(vector_dot([1.0, 2.0, NaN, 3.0], [1.0, 2.0, 3.0, 4.0], RoundNearest))
@test isnan(vector_dot([1.0, 2.0, 3.0, 4.0], [1.0, 2.0, NaN, 3.0], RoundNearest))
@test isnan(vector_dot([1.0, 2.0, 0.0, 4.0], [1.0, 2.0, Inf, 3.0], RoundNearest))
@test isnan(vector_dot([1.0, 2.0, -Inf, 4.0], [1.0, 2.0, 0.0, 3.0], RoundNearest))
end
# FactCheck.exitstatus()
1 change: 1 addition & 0 deletions test/interval_tests/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ include("parsing.jl")
include("rounding.jl")
include("bisect.jl")
include("complex.jl")
include("reduction.jl")
22 changes: 22 additions & 0 deletions test/interval_tests/reduction.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using IntervalArithmetic
using Test

setprecision(Interval, Float64)
setprecision(256)
@testset "Reduction Test" begin
@test vector_sum([1.0, 2.0, 3.0], RoundDown) == 6.0
@test isnan(vector_sum([1.0, 2.0, NaN, 3.0], RoundDown))
@test isnan(vector_sum([1.0, -Inf, 2.0, Inf, 3.0], RoundDown))
@test vector_sumAbs([1.0, -2.0, 3.0], RoundDown) == 6.0
@test isnan(vector_sumAbs([1.0, -2.0, NaN, 3.0], RoundDown))
@test vector_sumAbs([1.0, -Inf, 2.0, Inf, 3.0], RoundDown) == Inf
@test vector_sumSquare([1.0, 2.0, 3.0], RoundDown) == 14.0
@test isnan(vector_sumSquare([1.0, 2.0, NaN, 3.0], RoundDown))
@test vector_sumSquare([1.0, -Inf, 2.0, Inf, 3.0], RoundUp) == Inf
@test vector_dot([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], RoundUp) == 14.0
@test vector_dot([0x10000000000001p0, 0x1p104], [0x0fffffffffffffp0, -1.0], RoundUp) == -1.0
@test isnan(vector_dot([1.0, 2.0, NaN, 3.0], [1.0, 2.0, 3.0, 4.0], RoundUp))
@test isnan(vector_dot([1.0, 2.0, 3.0, 4.0], [1.0, 2.0, NaN, 3.0], RoundUp))
@test vector_sum([sqrt(3), 5^(1/3)], RoundUp) == 3.4420267542455742
@test vector_sum([sqrt(3), 5^(1/3)], RoundUp) == 3.4420267542455742
end