From 37d6463e63f7cd5af2677892805fd623ca95f31c Mon Sep 17 00:00:00 2001 From: yashrajgupta Date: Wed, 29 May 2019 07:04:39 +0530 Subject: [PATCH 1/5] Add reduction functions --- src/IntervalArithmetic.jl | 5 +-- src/intervals/intervals.jl | 1 + src/intervals/reduction.jl | 45 ++++++++++++++++++++++++ test/interval_tests/reduction.jl | 59 ++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/intervals/reduction.jl create mode 100644 test/interval_tests/reduction.jl diff --git a/src/IntervalArithmetic.jl b/src/IntervalArithmetic.jl index e35faeea0..4def4fc4c 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 , mpfr_vector_sum, mpfr_vector_dot, + mpfr_vector_sumAbs, mpfr_vector_sumSquare if VERSION >= v"1.5.0-DEV.124" import Base: isdisjoint @@ -81,7 +82,7 @@ export export showfull import Base: rounding, setrounding, setprecision - +import Base.MPFR: MPFRRoundUp, MPFRRoundDown, MPFRRoundNearest, MPFRRoundToZero, MPFRRoundFromZero, MPFRRoundingMode ## Multidimensional 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..f07732b5f --- /dev/null +++ b/src/intervals/reduction.jl @@ -0,0 +1,45 @@ +# 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 mpfr_vector_sum(v :: Vector{T} , r :: Float64) where T + n = length(v) + sum = BigFloat(0) + for i = 1 : n + sum = sum + BigFloat(v[i]) + end + r == 0 && return Float64(sum, MPFRRoundToZero) + r == 0.5 && return Float64(sum, MPFRRoundNearest) + r == Inf && return Float64(sum, MPFRRoundUp) + r == -Inf && return Float64(sum, MPFRRoundDown) +end + +function mpfr_vector_dot(v :: Vector{T}, u :: Vector{T}, r :: Float64) where T + m = length(u) + sum = BigFloat(0) + for i = 1 : m + sum = sum + BigFloat(v[i]) * BigFloat(u[i]) + end + r == 0 && return Float64(sum, MPFRRoundToZero) + r == 0.5 && return Float64(sum, MPFRRoundNearest) + r == Inf && return Float64(sum, MPFRRoundUp) + r == -Inf && return Float64(sum, MPFRRoundDown) +end + +function mpfr_vector_sumAbs(v :: Vector{T} , r :: Float64) where T + n = length(v) + sum = BigFloat(0) + for i = 1 : n + sum = sum + BigFloat(abs(v[i])) + end + r == 0 && return Float64(sum, MPFRRoundToZero) + r == 0.5 && return Float64(sum, MPFRRoundNearest) + r == Inf && return Float64(sum, MPFRRoundUp) + r == -Inf && return Float64(sum, MPFRRoundDown) +end + +function mpfr_vector_sumSquare(v :: Vector{T} , r :: Float64) where T + return mpfr_vector_dot(v, v, r) +end diff --git a/test/interval_tests/reduction.jl b/test/interval_tests/reduction.jl new file mode 100644 index 000000000..4b4f37232 --- /dev/null +++ b/test/interval_tests/reduction.jl @@ -0,0 +1,59 @@ +using IntervalArithmetic +using Test + +setprecision(Interval, Float64) + +@testset "Reduction Test" begin + @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), 0.5) == 6.0 + @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), 0.5)) + @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.5)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), 0.5) == 6.0 + @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), 0.5)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.5) == Inf + @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), 0.5) == 14.0 + @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), 0.5)) + @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.5) == Inf + @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), 0.5) == 14.0 + @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), 0.5) == -1.0 + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), 0.5)) + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), 0.5)) + @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), 0.0) == 6.0 + @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), 0.0)) + @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.0)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), 0.0) == 6.0 + @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), 0.0)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.0) == Inf + @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), 0.0) == 14.0 + @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), 0.0)) + @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.0) == Inf + @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), 0.0) == 14.0 + @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), 0.0) == -1.0 + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), 0.0)) + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), 0.0)) + @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), Inf) == 6.0 + @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), Inf)) + @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), Inf)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), Inf) == 6.0 + @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), Inf)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), Inf) == Inf + @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), Inf) == 14.0 + @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), Inf)) + @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), Inf) == Inf + @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), Inf) == 14.0 + @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), Inf) == -1.0 + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), Inf)) + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), Inf)) + @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), -Inf) == 6.0 + @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), -Inf)) + @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), -Inf)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), -Inf) == 6.0 + @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), -Inf)) + @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), -Inf) == Inf + @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), -Inf) == 14.0 + @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), -Inf)) + @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), -Inf) == Inf + @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), -Inf) == 14.0 + @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), -Inf) == -1.0 + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), -Inf)) + @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), -Inf)) +end From 20ed589e0e9bc700c9420aaa5d144f1032b30377 Mon Sep 17 00:00:00 2001 From: yashrajgupta Date: Mon, 10 Jun 2019 14:49:58 +0530 Subject: [PATCH 2/5] remove excess code --- src/IntervalArithmetic.jl | 4 +- src/intervals/reduction.jl | 43 +++++--------------- test/interval_tests/reduction.jl | 69 ++++++++------------------------ 3 files changed, 29 insertions(+), 87 deletions(-) diff --git a/src/IntervalArithmetic.jl b/src/IntervalArithmetic.jl index 4def4fc4c..4de0dfc2d 100644 --- a/src/IntervalArithmetic.jl +++ b/src/IntervalArithmetic.jl @@ -64,8 +64,8 @@ export cancelminus, cancelplus, isunbounded, .., @I_str, ±, pow, extended_div, - setformat, @format , mpfr_vector_sum, mpfr_vector_dot, - mpfr_vector_sumAbs, mpfr_vector_sumSquare + setformat, @format , vector_sum, vector_dot, + vector_sumAbs, vector_sumSquare if VERSION >= v"1.5.0-DEV.124" import Base: isdisjoint diff --git a/src/intervals/reduction.jl b/src/intervals/reduction.jl index f07732b5f..365307139 100644 --- a/src/intervals/reduction.jl +++ b/src/intervals/reduction.jl @@ -4,42 +4,21 @@ 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 mpfr_vector_sum(v :: Vector{T} , r :: Float64) where T - n = length(v) - sum = BigFloat(0) - for i = 1 : n - sum = sum + BigFloat(v[i]) - end - r == 0 && return Float64(sum, MPFRRoundToZero) - r == 0.5 && return Float64(sum, MPFRRoundNearest) - r == Inf && return Float64(sum, MPFRRoundUp) - r == -Inf && return Float64(sum, MPFRRoundDown) +function vector_sum(v :: Vector{T} , r :: RoundingMode) where T + sum1 = sum(BigFloat.(v)) + return Float64(sum1, r) end -function mpfr_vector_dot(v :: Vector{T}, u :: Vector{T}, r :: Float64) where T - m = length(u) - sum = BigFloat(0) - for i = 1 : m - sum = sum + BigFloat(v[i]) * BigFloat(u[i]) - end - r == 0 && return Float64(sum, MPFRRoundToZero) - r == 0.5 && return Float64(sum, MPFRRoundNearest) - r == Inf && return Float64(sum, MPFRRoundUp) - r == -Inf && return Float64(sum, MPFRRoundDown) +function vector_dot(v :: Vector{T}, u :: Vector{T}, r :: RoundingMode) where T + sum1 = sum(BigFloat.(v) .* BigFloat.(u)) + return Float64(sum1, r) end -function mpfr_vector_sumAbs(v :: Vector{T} , r :: Float64) where T - n = length(v) - sum = BigFloat(0) - for i = 1 : n - sum = sum + BigFloat(abs(v[i])) - end - r == 0 && return Float64(sum, MPFRRoundToZero) - r == 0.5 && return Float64(sum, MPFRRoundNearest) - r == Inf && return Float64(sum, MPFRRoundUp) - r == -Inf && return Float64(sum, MPFRRoundDown) +function vector_sumAbs(v :: Vector{T} , r :: RoundingMode) where T + sum1 = sum(BigFloat.(abs.(v))) + return Float64(sum1, r) end -function mpfr_vector_sumSquare(v :: Vector{T} , r :: Float64) where T - return mpfr_vector_dot(v, v, r) +function vector_sumSquare(v :: Vector{T} , r :: RoundingMode) where T + return vector_dot(v, v, r) end diff --git a/test/interval_tests/reduction.jl b/test/interval_tests/reduction.jl index 4b4f37232..bc69cb349 100644 --- a/test/interval_tests/reduction.jl +++ b/test/interval_tests/reduction.jl @@ -2,58 +2,21 @@ using IntervalArithmetic using Test setprecision(Interval, Float64) - +setprecision(256) @testset "Reduction Test" begin - @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), 0.5) == 6.0 - @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), 0.5)) - @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.5)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), 0.5) == 6.0 - @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), 0.5)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.5) == Inf - @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), 0.5) == 14.0 - @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), 0.5)) - @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.5) == Inf - @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), 0.5) == 14.0 - @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), 0.5) == -1.0 - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), 0.5)) - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), 0.5)) - @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), 0.0) == 6.0 - @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), 0.0)) - @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.0)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), 0.0) == 6.0 - @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), 0.0)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.0) == Inf - @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), 0.0) == 14.0 - @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), 0.0)) - @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), 0.0) == Inf - @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), 0.0) == 14.0 - @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), 0.0) == -1.0 - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), 0.0)) - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), 0.0)) - @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), Inf) == 6.0 - @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), Inf)) - @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), Inf)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), Inf) == 6.0 - @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), Inf)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), Inf) == Inf - @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), Inf) == 14.0 - @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), Inf)) - @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), Inf) == Inf - @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), Inf) == 14.0 - @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), Inf) == -1.0 - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), Inf)) - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), Inf)) - @test mpfr_vector_sum(Base.vect(1.0, 2.0, 3.0), -Inf) == 6.0 - @test isnan(mpfr_vector_sum(Base.vect(1.0, 2.0, NaN, 3.0), -Inf)) - @test isnan(mpfr_vector_sum(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), -Inf)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -2.0, 3.0), -Inf) == 6.0 - @test isnan(mpfr_vector_sumAbs(Base.vect(1.0, -2.0, NaN, 3.0), -Inf)) - @test mpfr_vector_sumAbs(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), -Inf) == Inf - @test mpfr_vector_sumSquare(Base.vect(1.0, 2.0, 3.0), -Inf) == 14.0 - @test isnan(mpfr_vector_sumSquare(Base.vect(1.0, 2.0, NaN, 3.0), -Inf)) - @test mpfr_vector_sumSquare(Base.vect(1.0, -Inf, 2.0, Inf, 3.0), -Inf) == Inf - @test mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0), Base.vect(1.0, 2.0, 3.0), -Inf) == 14.0 - @test mpfr_vector_dot(Base.vect(0x10000000000001p0, 0x1p104), Base.vect(0x0fffffffffffffp0, -1.0), -Inf) == -1.0 - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, NaN, 3.0), Base.vect(1.0, 2.0, 3.0, 4.0), -Inf)) - @test isnan(mpfr_vector_dot(Base.vect(1.0, 2.0, 3.0, 4.0), Base.vect(1.0, 2.0, NaN, 3.0), -Inf)) + @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)) + @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 + @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 + @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 vector_sum([sqrt(big(3)), 5^(1/3)], RoundDown) == 3.442026754245574 + @test vector_sum([sqrt(big(3)), 5^(1/3)], RoundUp) == 3.4420267542455742 end From 06dd3f7c16d82847ae585bafe18080057c5698d4 Mon Sep 17 00:00:00 2001 From: Krish Agarwal Date: Fri, 17 Jul 2020 11:33:59 +0530 Subject: [PATCH 3/5] Reduuctions functions return Float32, Float64 and BigFloat --- src/intervals/reduction.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/intervals/reduction.jl b/src/intervals/reduction.jl index 365307139..054dc463a 100644 --- a/src/intervals/reduction.jl +++ b/src/intervals/reduction.jl @@ -6,17 +6,17 @@ r is the rounding direction which takes 0.0 for RoundingToZero , 0.5 for Roundin """ function vector_sum(v :: Vector{T} , r :: RoundingMode) where T sum1 = sum(BigFloat.(v)) - return Float64(sum1, r) + 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 Float64(sum1, r) + return T(sum1, r) end function vector_sumAbs(v :: Vector{T} , r :: RoundingMode) where T sum1 = sum(BigFloat.(abs.(v))) - return Float64(sum1, r) + return T(sum1, r) end function vector_sumSquare(v :: Vector{T} , r :: RoundingMode) where T From c2b5259b46538e5ed0935f73c9012e89b43e21ba Mon Sep 17 00:00:00 2001 From: Krish Agarwal Date: Fri, 17 Jul 2020 11:34:18 +0530 Subject: [PATCH 4/5] Remove base functions which have been imported twice --- src/IntervalArithmetic.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/IntervalArithmetic.jl b/src/IntervalArithmetic.jl index 4de0dfc2d..e65310c12 100644 --- a/src/IntervalArithmetic.jl +++ b/src/IntervalArithmetic.jl @@ -82,7 +82,6 @@ export export showfull import Base: rounding, setrounding, setprecision -import Base.MPFR: MPFRRoundUp, MPFRRoundDown, MPFRRoundNearest, MPFRRoundToZero, MPFRRoundFromZero, MPFRRoundingMode ## Multidimensional From 5d75a6a9b49775fc4b0d9bbd3c4d43c23b70a656 Mon Sep 17 00:00:00 2001 From: Krish Agarwal Date: Fri, 17 Jul 2020 11:50:48 +0530 Subject: [PATCH 5/5] Add ITF1788 and non ITF1788 tests --- test/ITF1788_tests/ITF1788_tests.jl | 1 + test/ITF1788_tests/libieeep1788_reduction.jl | 64 ++++++++++++++++++++ test/interval_tests/intervals.jl | 1 + test/interval_tests/reduction.jl | 30 ++++----- 4 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 test/ITF1788_tests/libieeep1788_reduction.jl 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 index bc69cb349..c4c7e4d3f 100644 --- a/test/interval_tests/reduction.jl +++ b/test/interval_tests/reduction.jl @@ -4,19 +4,19 @@ using Test setprecision(Interval, Float64) setprecision(256) @testset "Reduction 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)) - @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 - @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 - @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 vector_sum([sqrt(big(3)), 5^(1/3)], RoundDown) == 3.442026754245574 - @test vector_sum([sqrt(big(3)), 5^(1/3)], RoundUp) == 3.4420267542455742 + @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