Skip to content

Commit a6846a2

Browse files
authored
Define specialised abs2 and abs for complex intervals (#250)
* Define abs2 and abs for complex intervals * Add test * Add test for norm * Modify tests according to review * Update test/interval_tests/complex.jl Co-Authored-By: dpsanders <dpsanders@gmail.com>
1 parent 2bc78ea commit a6846a2

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/IntervalArithmetic.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using SetRounding
1313
using Markdown
1414

1515
using LinearAlgebra
16-
import LinearAlgebra: ×, dot
16+
import LinearAlgebra: ×, dot, norm
1717
export ×, dot
1818

1919

@@ -33,6 +33,7 @@ import Base:
3333
expm1, log1p,
3434
precision,
3535
isfinite, isnan, isinf, iszero,
36+
abs, abs2,
3637
show,
3738
isinteger, setdiff,
3839
parse, hash

src/intervals/complex.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,17 @@ function log(z::Complex{T}) where T<:Interval
103103

104104
return log(ρ) + im * θ
105105
end
106+
107+
108+
function abs2(z::Complex{T}) where T<:Interval
109+
return real(z)^2 + imag(z)^2
110+
end
111+
112+
function abs(z::Complex{T}) where T<:Interval
113+
return sqrt(abs2(z))
114+
end
115+
#
116+
# # \left( |x|^p \right)^{1/p}.
117+
# function norm(z::Complex{T}, p=2) where T<:Interval
118+
# return (abs(z)^(p))^(1 / p)
119+
# end

test/interval_tests/complex.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using IntervalArithmetic
22
using Test
33

4+
using LinearAlgebra
5+
46
@testset "Complex interval operations" begin
57
a = @interval 1im
68
b = @interval 4im + 3
@@ -44,3 +46,13 @@ end
4446
a = -3.1
4547
@test x (x^a)^(1/a)
4648
end
49+
50+
@testset "abs2 and abs" begin
51+
x = (0..3) + (0..4)*im
52+
@test abs2(x) == 0..25
53+
@test abs(x) == norm(x) == 0..5
54+
55+
y = (-1..1) + (-2..2)*im
56+
@test abs(y).lo == 0.0
57+
@test abs2(y).lo == 0.0
58+
end

0 commit comments

Comments
 (0)