Skip to content

Commit

Permalink
Added complex sqrt and some complex set functions (#195)
Browse files Browse the repository at this point in the history
* Added complex sqrt and some complex set functions

Added complex sqrt and some complex set functions

Added complex sqrt and some complex set functions

Fixed test

Fixed complex sqrt test

Added more tests

Fixed complex in

added tests back in

* Updated LICENSE.md to include sqrt code attribution
  • Loading branch information
wormell authored and dpsanders committed Aug 25, 2018
1 parent cab2433 commit a9d3ef0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
27 changes: 27 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,30 @@ 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.

The code for ssqs and sqrt{::Complex{<:Interval}} is modified from Base
module in Julia 0.6.4, licensed also under the MIT license:

> Copyright (c) 2009-2016: Jeff Bezanson, Stefan Karpinski, Viral B. Shah,
> and other contributors:
>
> https://github.com/JuliaLang/julia/contributors
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77 changes: 77 additions & 0 deletions src/intervals/complex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function ssqs(x::T, y::T,RND::RoundingMode) where T<:AbstractFloat
k::Int = 0
ρ = +(*(x,x,RND),*(y,y,RND),RND)
if !isfinite(ρ) && (isinf(x) || isinf(y))
ρ = convert(T, Inf)
elseif isinf(ρ) ||==0 && (x!=0 || y!=0)) || ρ<nextfloat(zero(T))/(2*eps(T)^2)
m::T = max(abs(x), abs(y))
k = m==0 ? m : exponent(m)
xk, yk = ldexp(x,-k), ldexp(y,-k)
ρ = +(*(xk,xk,RND),*(yk,yk,RND),RND)
end
ρ, k
end

# function ssqs(x::Interval{T}, y::Interval{T}) where T<:AbstractFloat
# x = abs(x); y = abs(y)
# ρl,kl = ssqs(inf(x),inf(y),RoundDown)
# ρu,ku = ssqs(sup(x),sup(y),RoundUp)
# ρl, kl, ρu, ku
# end

function sqrt_realpart(x::T, y::T,RND::RoundingMode) where T<:AbstractFloat
# @assert x ≥ 0
ρ, k = ssqs(x,y,RND)
if isfinite(x) ρ = +(ldexp(x,-k),sqrt(ρ,RND),RND) end

if isodd(k)
k = div(k-1,2)
else
k = div(k,2)-1
ρ *= 2
end
ρ = ldexp(sqrt(ρ,RND),k) #sqrt((abs(z)+abs(x))/2) without over/underflow
end

function sqrt(z::Complex{Interval{T}}) where T<:AbstractFloat
x, y = reim(z)

(inf(x) < 0 && inf(y) < 0 && sup(y) >= 0) && error("Interval lies across branch cut")
x == y == 0 && return Complex(zero(x),y)

(inf(x) < 0 && sup(x) > 0) && return sqrt((inf(x)..0) + im*y) sqrt((0..sup(x)) + im*y)

absx = abs(x)

absy= abs(y)
ρl = sqrt_realpart(inf(absx),inf(absy),RoundDown)
ρu = sqrt_realpart(sup(absx),sup(absy),RoundUp)

ηu = sup(y)
if isfinite(ηu)
if ηu >= 0
ηu_re = sqrt_realpart(inf(absx),ηu,RoundDown)
else
ηu_re = sqrt_realpart(sup(absx),ηu,RoundUp)
end
ηu = ηu_re 0 ? /(ηu,2ηu_re,RoundUp) : zero(T)
end

ηl = inf(y)
if ηl >= 0
ηl_re = sqrt_realpart(sup(absx),ηl,RoundUp)
else
ηl_re = sqrt_realpart(inf(absx),ηl,RoundDown)
end
ηl = ηl_re 0 ? /(ηl,2ηl_re,RoundDown) : zero(T)

ξ = ρl..ρu
η = ηl..ηu

if mid(x)<0
ξ = flipsign(η,mid(η))
η = flipsign((ρl..ρu),mid(η))
end

Complex(ξ,η)
end
2 changes: 1 addition & 1 deletion src/intervals/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ include("arithmetic.jl")
include("functions.jl")
include("trigonometric.jl")
include("hyperbolic.jl")

include("complex.jl")

# Syntax for intervals

Expand Down
18 changes: 18 additions & 0 deletions src/intervals/set_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function in(x::T, a::Interval) where T<:Real
a.lo <= x <= a.hi
end

in(x::T, a::Complex{<:Interval}) where T<:Real = x real(a) && 0 imag(a)
in(x::Complex{T}, a::Complex{<:Interval}) where T<:Real = real(x) real(a) && imag(x) imag(a)



"""
Expand Down Expand Up @@ -52,6 +55,10 @@ function isdisjoint(a::Interval, b::Interval)
islessprime(b.hi, a.lo) || islessprime(a.hi, b.lo)
end

function isdisjoint(a::Complex{<:Interval}, b::Complex{<:Interval})
isdisjoint(real(a),real(b)) || isdisjoint(imag(a),imag(b))
end


# Intersection
"""
Expand All @@ -71,6 +78,14 @@ end
intersect(a::Interval{T}, b::Interval{S}) where {T,S} =
intersect(promote(a, b)...)

function intersect(a::Complex{Interval{T}},b::Complex{Interval{T}}) where T
isdisjoint(a,b) && return complex(emptyinterval(T),emptyinterval(T)) # for type stability

complex(intersect(real(a),real(b)),intersect(imag(a),imag(b)))
end
intersect(a::Interval{Complex{T}}, b::Interval{Complex{S}}) where {T,S} =
intersect(promote(a, b)...)


## Hull
"""
Expand All @@ -83,6 +98,8 @@ all of `a` and `b`.
hull(a::Interval, b::Interval) = Interval(min(a.lo, b.lo), max(a.hi, b.hi))
#
# hull{T,S}(a::Interval{T}, b::Interval{S}) = hull(promote(a, b)...)
hull(a::Complex{<:Interval},b::Complex{<:Interval}) =
complex(hull(real(a),real(b)),hull(imag(a),imag(b)))

"""
union(a, b)
Expand All @@ -94,6 +111,7 @@ to `hull(a,b)`.
union(a::Interval, b::Interval) = hull(a, b)
#
# union(a::Interval, b::Interval) = union(promote(a, b)...)
union(a::Complex{<:Interval},b::Complex{<:Interval}) = hull(a, b)


"""
Expand Down
19 changes: 18 additions & 1 deletion test/interval_tests/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@ end

@testset "Complex interval operations" begin
a = @interval 1im
@test typeof(a)== Complex{IntervalArithmetic.Interval{Float64}}
b = @interval 4im + 3
c = (@interval -1 4) + (@interval 0 2)*im

@test typeof(a) == Complex{IntervalArithmetic.Interval{Float64}}
@test a == Interval(0) + Interval(1)*im
@test a * a == Interval(-1)
@test a + a == Interval(2)*im
@test a - a == 0
@test a / a == 1
@test a^2 == -1

@test 3+2im c
@test a b == (@interval 0 3) + (@interval 1 4)*im
@test c (a b) == (@interval 0 3) + (@interval 1 2)*im
@test a b ==+*im
@test isdisjoint(a,b) == true
end

@testset "Complex functions" begin
Z = (3 ± 1e-7) + (4 ± 1e-7)*im
@test sin(Z) == complex(sin(real(Z))*cosh(imag(Z)),sinh(imag(Z))*cos(real(Z)))
@test exp(-im * Interval(π)) == Interval(-1.0, -0.9999999999999999) - Interval(1.224646799147353e-16, 1.2246467991473532e-16)*im

sZ = sqrt(Z)
@test sZ == Interval(1.99999996999999951619,2.00000003000000070585) + Interval(0.99999996999999984926,1.00000003000000048381)*im
@test sqrt(-Z) == imag(sZ) - real(sZ)*im

@test sqrt((@interval -1 0) + (@interval 0)*im) .== (@interval 0 1)*im
@test sqrt((@interval -1 1) + (@interval 0)*im) .== (@interval 0 1) + (@interval 0 1)*im
@test sqrt((@interval -9//32 Inf)*im + (@interval 0)) .== (@interval 0 Inf) + (@interval -3//8 Inf)*im
end

0 comments on commit a9d3ef0

Please sign in to comment.