Skip to content
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

Fixes in power_rev #55

Open
wants to merge 4 commits into
base: IA_1.0-dev
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
205 changes: 205 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.7.2"
manifest_format = "2.0"

[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"

[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.CRlibm]]
deps = ["CRlibm_jll"]
git-tree-sha1 = "32abd86e3c2025db5172aa182b982debed519834"
uuid = "96374032-68de-5a5b-8d9e-752f78720389"
version = "1.0.1"

[[deps.CRlibm_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "e329286945d0cfc04456972ea732551869af1cfc"
uuid = "4e9b3aee-d8a1-5a3d-ad8b-7d824db253f0"
version = "1.0.1+0"

[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"

[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[deps.Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[deps.EnumX]]
git-tree-sha1 = "1d2621e1a6246c5cf1116be0055686f305210b80"
uuid = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
version = "1.0.2"

[[deps.ErrorfreeArithmetic]]
git-tree-sha1 = "d6863c556f1142a061532e79f611aa46be201686"
uuid = "90fa49ef-747e-5e6f-a989-263ba693cf1a"
version = "0.5.2"

[[deps.FastRounding]]
deps = ["ErrorfreeArithmetic", "LinearAlgebra"]
git-tree-sha1 = "6344aa18f654196be82e62816935225b3b9abe44"
uuid = "fa42c844-2597-5d31-933b-ebd51ab2693f"
version = "0.3.1"

[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.IntervalArithmetic]]
deps = ["CRlibm", "EnumX", "FastRounding", "LinearAlgebra", "Markdown", "Random", "RecipesBase", "RoundingEmulator", "SetRounding", "StaticArrays"]
repo-rev = "1.0-dev"
repo-url = "https://github.com/JuliaIntervals/IntervalArithmetic.jl.git"
uuid = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
version = "0.20.6"

[[deps.JLLWrappers]]
deps = ["Preferences"]
git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.4.1"

[[deps.LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[deps.LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[deps.LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[deps.LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"

[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[deps.LinearAlgebra]]
deps = ["Libdl", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[deps.MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[deps.MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[deps.NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[deps.OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"

[[deps.Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[deps.Preferences]]
deps = ["TOML"]
git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.3.0"

[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[deps.REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[deps.Random]]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[deps.RecipesBase]]
git-tree-sha1 = "6bf3f380ff52ce0832ddd3a2a7b9538ed1bcca7d"
uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
version = "1.2.1"

[[deps.RoundingEmulator]]
git-tree-sha1 = "40b9edad2e5287e05bd413a38f61a8ff55b9557b"
uuid = "5eaf0fd0-dfba-4ccb-bf02-d820a40db705"
version = "0.2.1"

[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[deps.SetRounding]]
git-tree-sha1 = "d7a25e439d07a17b7cdf97eecee504c50fedf5f6"
uuid = "3cc68bcd-71a2-5612-b932-767ffbe40ab0"
version = "0.2.1"

[[deps.Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[deps.SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "4f6ec5d99a28e1a749559ef7dd518663c5eca3d5"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.4.3"

[[deps.Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[deps.Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[deps.UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[deps.Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"

[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[deps.p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
32 changes: 20 additions & 12 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function plus_rev(a::Interval, b::Interval, c::Interval) # a = b + c
return a, b_new, c_new
end

plus_rev(a,b,c) = plus_rev(promote(a,b,c)...)
plus_rev(a::F, b::F, c) where {T, F<:Interval{T}} = plus_rev(a, b, F(c))
Copy link
Member

@lucaferranti lucaferranti Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this (and the others) be

function plus_rev(a::Interval{T}, b::Interval{S}, c::Interval(R})
  TSR = promote_type(T, S, R) # not sure if you can do it in one line, but you get the idea
  F = Interval{TSR}
  return plus_rev(F(a), F(b), F(c))
end

but it gets pretty tedious... I get the idea of not having promotion Float64 --> Interval{Float64}, but I think a promotion rule for Interval{T}, Interval{S} would be harmless.

In both cases something like plus_rev(1.0, 1..2, 3..4) wouldn't work. This can be a bug (if you think in the more ducktyping way) or a feature (if you come from a strong static typing programming language)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the second method, which previously had a promotion, I truly don't recall what was the motivation. I guess it was a way of avoiding using promote...

In any case, let me note that many operations in the 1.0-dev branch of IA impose the same Interval{T} for the operands: e.g., (1.0f0 .. 2.f0) - ( 1.5 .. 2.5) throws a MethodError due to ambiguities...

Copy link
Member

@lucaferranti lucaferranti Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's because of ambiguity, then it's a bug (or at least misleading error message). In general, it's not necessarily wrong to disallow implicit conversions, strongly typed languages do it and for good reasons (see this accident ), but it doesn't feel julian, also for every function with multiple arguments you would need to manually 1) either define methods for all possible missing cases 2) or have an own interval _promote_interval to have things like f(1.0, 1..2, 2.0) work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is probably tangential to this PR


"""
minus_rev(a::Interval, b::Interval[, c::Interval])
Expand All @@ -42,7 +42,7 @@ function minus_rev(a::Interval, b::Interval, c::Interval) # a = b - c
return a, b_new, c_new
end

minus_rev(a,b,c) = minus_rev(promote(a,b,c)...)
minus_rev(a::F, b::F, c) where {T, F<:Interval{T}} = minus_rev(a, b, F(c))

function minus_rev(a::Interval, b::Interval) # a = -b
b_new = b ∩ (-a)
Expand Down Expand Up @@ -79,7 +79,7 @@ function mul_rev(a::Interval, b::Interval, c::Interval) # a = b * c
return a, b′, c′
end

mul_rev(a,b,c) = mul_rev(promote(a,b,c)...)
mul_rev(a::F, b::F, c) where {T, F<:Interval{T}} = mul_rev(a, b, F(c))

"""
Reverse division
Expand All @@ -92,7 +92,7 @@ function div_rev(a::Interval, b::Interval, c::Interval) # a = b / c
return a, b, c
end

div_rev(a,b,c) = div_rev(promote(a,b,c)...)
div_rev(a::F, b::F, c) where {T, F<:Interval{T}} = div_rev(a, b, F(c))

"""
inv_rev(a::Interval, b::Interval)
Expand All @@ -113,7 +113,7 @@ function inv_rev(a::Interval, b::Interval) # a = inv(b)
return a, b_new
end

inv_rev(a,b) = inv_rev(promote(a,b)...)
inv_rev(a::F, b) where {T, F<:Interval{T}}= inv_rev(a, F(b))

"""
power_rev(a::Interval, b::Interval, n::Integer)
Expand All @@ -128,27 +128,35 @@ The triplet `(a, b_new, n)` where
- `a` and `n` are unchanged
- `b_new` is the interval hull of the set ``{x ∈ b : xⁿ ∈ a}``
"""
function power_rev(a::Interval{T}, b::Interval{T}, n::Integer) where {T} # a = b^n, log(a) = n.log(b), b = a^(1/n)
domain = zero(T) .. Inf
function power_rev(a::F, b::F, n::Integer) where {T, F<:Interval{T}} # a = b^n, log(a) = n.log(b), b = a^(1/n)
if iszero(n)
1 ∈ a && return (a, entireinterval(T) ∩ b, n)
return (a, emptyinterval(T), n)
end

# if n < 0
# aa, bb, _ = power_rev(inv(a), b, -n)
# return (a, bb, n)
# end

if n == 2 # a = b^2
root = √a
b1 = b ∩ root
b2 = b ∩ (-root)

elseif iseven(n)
root = a^(1//n)
# root = a^(1//n)
root = nthroot(a, n)

b1 = b ∩ root
b2 = b ∩ (-root)

elseif isodd(n)
pos_root = (a ∩ domain) ^ (1//n)
neg_root = -( ( (-a) ∩ domain ) ^ (1//n) )
domain = F(0, Inf)
# pos_root = (a ∩ domain) ^ (1//n)
# neg_root = -( ( (-a) ∩ domain) ^ (1//n) )
pos_root = nthroot(a ∩ domain, n)
neg_root = - nthroot( (-a) ∩ domain, n)

b1 = b ∩ pos_root
b2 = b ∩ neg_root
Expand All @@ -160,7 +168,7 @@ function power_rev(a::Interval{T}, b::Interval{T}, n::Integer) where {T} # a =
return (a, b, n)
end

power_rev(a::Interval{T}, n::Integer) where {T} = power_rev(a, entireinterval(T), n)
power_rev(a::F, n::Integer) where {T, F<:Interval{T}} = power_rev(a, entireinterval(F), n)

function power_rev(a::Interval, b::Interval, c::Interval) # a = b^c

Expand All @@ -175,7 +183,7 @@ function power_rev(a::Interval, b::Interval, c::Interval) # a = b^c
return a, b_new, c_new
end

power_rev(a, b, c) = power_rev(promote(a, b, c)...)
power_rev(a::F, b::F, c) where {T, F<:Interval{T}} = power_rev(a, b, F(c))


"""
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/extrema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/hyperbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/inv_hyperbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/inv_rev.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/inv_trig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
4 changes: 2 additions & 2 deletions test/Non1788tests/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using IntervalArithmetic
using IntervalContractors

#Preamble
setprecision(53)
setprecision(Interval, Float64)
# setprecision(53)
# setprecision(Interval, Float64)

# Using approximate checks for validaty update later?
import Base.isapprox
Expand Down
Loading