diff --git a/src/IntervalArithmetic.jl b/src/IntervalArithmetic.jl index e35faeea0..e65310c12 100644 --- a/src/IntervalArithmetic.jl +++ b/src/IntervalArithmetic.jl @@ -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 @@ -83,7 +84,6 @@ export showfull import Base: rounding, setrounding, setprecision - ## Multidimensional export IntervalBox diff --git a/src/intervals/intervals.jl b/src/intervals/intervals.jl index 78149832a..8ef815a0a 100644 --- a/src/intervals/intervals.jl +++ b/src/intervals/intervals.jl @@ -137,6 +137,7 @@ include("functions.jl") include("trigonometric.jl") include("hyperbolic.jl") include("complex.jl") +include("reduction.jl") # Syntax for intervals diff --git a/src/intervals/reduction.jl b/src/intervals/reduction.jl new file mode 100644 index 000000000..054dc463a --- /dev/null +++ b/src/intervals/reduction.jl @@ -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 diff --git a/test/ITF1788_tests/ITF1788_tests.jl b/test/ITF1788_tests/ITF1788_tests.jl index 9dc6c64d4..0e66b8007 100644 --- a/test/ITF1788_tests/ITF1788_tests.jl +++ b/test/ITF1788_tests/ITF1788_tests.jl @@ -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") \ No newline at end of file diff --git a/test/ITF1788_tests/libieeep1788_reduction.jl b/test/ITF1788_tests/libieeep1788_reduction.jl new file mode 100644 index 000000000..b862ddd1f --- /dev/null +++ b/test/ITF1788_tests/libieeep1788_reduction.jl @@ -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 (nehmeier@informatik.uni-wuerzburg.de) + Copyright 2015-2017 Oliver Heimlich (oheim@posteo.de) + + 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() diff --git a/test/interval_tests/intervals.jl b/test/interval_tests/intervals.jl index 34b557122..b90191149 100644 --- a/test/interval_tests/intervals.jl +++ b/test/interval_tests/intervals.jl @@ -13,3 +13,4 @@ include("parsing.jl") include("rounding.jl") include("bisect.jl") include("complex.jl") +include("reduction.jl") \ No newline at end of file diff --git a/test/interval_tests/reduction.jl b/test/interval_tests/reduction.jl new file mode 100644 index 000000000..c4c7e4d3f --- /dev/null +++ b/test/interval_tests/reduction.jl @@ -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