Skip to content

Commit 3f74d22

Browse files
authored
Fix complex matrix multiplication (#133)
* Added methods to currectly dispatch complex mul * Added methods to dispatch correctly cpx matrix mul
1 parent 9a50c7d commit 3f74d22

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/multiplication.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ function set_multiplication_mode(multype)
3838
config[:multiplication] = multype
3939
end
4040

41+
function *(A::AbstractMatrix{Complex{Interval{T}}}, B::AbstractMatrix) where T
42+
return real(A)*B+im*imag(A)*B
43+
end
44+
45+
function *(A::AbstractMatrix, B::AbstractMatrix{Complex{Interval{T}}}) where T
46+
return A*real(B)+im*A*imag(B)
47+
end
48+
49+
function *(A::AbstractMatrix{Complex{Interval{T}}}, B::AbstractMatrix{Complex{Interval{T}}}) where T
50+
rA, iA = real(A), imag(A)
51+
rB, iB = real(B), imag(B)
52+
return rA*rB-iA*iB+im*(iA*rB+rA*iB)
53+
end
54+
55+
function *(A::AbstractMatrix{Complex{T}}, B::AbstractMatrix{Interval{T}}) where {T}
56+
return real(A)*B+im*imag(A)*B
57+
end
58+
59+
function *(A::AbstractMatrix{Interval{T}}, B::AbstractMatrix{Complex{T}}) where {T}
60+
return A*real(B)+im*A*imag(B)
61+
end
62+
63+
4164
function *(::MultiplicationType{:slow}, A, B)
4265
TS = promote_type(eltype(A), eltype(B))
4366
return mul!(similar(B, TS, (size(A,1), size(B,2))), A, B)

test/test_multiplication.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
@test get_multiplication_mode() == Dict(:multiplication => :fast)
55

66
A = [2..4 -2..1; -1..2 2..4]
7+
imA = im*A
8+
79
set_multiplication_mode(:slow)
810
@test A * A == [0..18 -16..8; -8..16 0..18]
11+
@test imA * imA == -1*[0..18 -16..8; -8..16 0..18]
912

1013
set_multiplication_mode(:rank1)
1114
@test A * A == [0..18 -16..8; -8..16 0..18]
15+
@test imA * imA == -1*[0..18 -16..8; -8..16 0..18]
1216

1317
set_multiplication_mode(:fast)
1418
@test A * A == [-2..19.5 -16..10; -10..16 -2..19.5]
1519
@test A * mid.(A) == [5..12.5 -8..2; -2..8 5..12.5]
1620
@test mid.(A) * A == [5..12.5 -8..2; -2..8 5..12.5]
21+
22+
@test imA * imA == -1*[-2..19.5 -16..10; -10..16 -2..19.5]
23+
@test mid.(A) * imA == im*[5..12.5 -8..2; -2..8 5..12.5]
24+
@test imA * mid.(A) == im*[5..12.5 -8..2; -2..8 5..12.5]
1725
end

0 commit comments

Comments
 (0)