Skip to content

Commit 89d8c96

Browse files
authored
Merge pull request #312 from mschauer/bilineardot
Fix 3x3 det and inv for complex entries
2 parents 0c21db3 + 16fafb3 commit 89d8c96

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

src/det.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
@inbounds x0 = SVector(A[1], A[2], A[3])
1616
@inbounds x1 = SVector(A[4], A[5], A[6])
1717
@inbounds x2 = SVector(A[7], A[8], A[9])
18-
return vecdot(x0, cross(x1, x2))
18+
return bilinear_vecdot(x0, cross(x1, x2))
1919
end
2020

2121
@inline function _det(::Size{(4,4)}, A::StaticMatrix)

src/inv.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121
@inbounds x2 = SVector{3}(A[7], A[8], A[9])
2222

2323
y0 = cross(x1,x2)
24-
d = vecdot(x0, y0)
24+
d = bilinear_vecdot(x0, y0)
2525
x0 = x0 / d
2626
y0 = y0 / d
2727
y1 = cross(x2,x0)

src/linalg.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,23 @@ end
236236
end
237237
end
238238

239+
@inline bilinear_vecdot(a::StaticArray, b::StaticArray) = _bilinear_vecdot(same_size(a, b), a, b)
240+
@generated function _bilinear_vecdot(::Size{S}, a::StaticArray, b::StaticArray) where {S}
241+
if prod(S) == 0
242+
return :(zero(promote_op(*, eltype(a), eltype(b))))
243+
end
244+
245+
expr = :(a[1] * b[1])
246+
for j = 2:prod(S)
247+
expr = :($expr + a[$j] * b[$j])
248+
end
249+
250+
return quote
251+
@_inline_meta
252+
@inbounds return $expr
253+
end
254+
end
255+
239256
@inline norm(v::StaticVector) = vecnorm(v)
240257
@inline norm(v::StaticVector, p::Real) = vecnorm(v, p)
241258

test/det.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
@test logdet(@SMatrix [1]) == 0.0
44
@test det(@SMatrix [0 1; 1 0]) == -1
55
@test logdet(@SMatrix Complex{Float64}[0 1; 1 0]) == log(det(@SMatrix Complex{Float64}[0 1; 1 0]))
6-
6+
@test det(eye(SMatrix{3,3})*im) == det(eye(3,3)*im)
7+
78
@test det(@SMatrix [0 1 0; 1 0 0; 0 0 1]) == -1
89
m = randn(Float64, 4,4)
910
@test det(SMatrix{4,4}(m)) det(m)

test/inv.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ end
2222
@test inv(@SMatrix [2])::SMatrix @SMatrix [0.5]
2323
@test inv(@SMatrix [1 2; 2 1])::SMatrix [-1/3 2/3; 2/3 -1/3]
2424
@test inv(@SMatrix [1 2 0; 2 1 0; 0 0 1])::SMatrix [-1/3 2/3 0; 2/3 -1/3 0; 0 0 1]
25-
25+
@test inv(@SMatrix [1+im 2 0; 1 2-im 1; 1 1 2+im])::SMatrix [2-2im -3+1im 1-1im
26+
-1+0im 2+1im -1+0im
27+
0+1im 0-1im 1-0.0im]/2
28+
2629
m = randn(Float64, 10,10) + eye(10) # well conditioned
2730
@test inv(SMatrix{10,10}(m))::StaticMatrix inv(m)
2831

test/linalg.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ using StaticArrays, Base.Test
8686
@test @inferred(cross(SVector(UInt(1),UInt(2),UInt(3)), SVector(UInt(4),UInt(5),UInt(6)))) === SVector(-3, 6, -3)
8787
end
8888

89+
@testset "inner products" begin
90+
v1 = @SVector [2,4,6,8]
91+
v2 = @SVector [4,3,2,1]
92+
93+
@test @inferred(dot(v1, v2)) === 40
94+
@test @inferred(dot(v1, -v2)) === -40
95+
@test @inferred(dot(v1*im, v2*im)) === 40*im*conj(im)
96+
@test @inferred(StaticArrays.bilinear_vecdot(v1*im, v2*im)) === 40*im*im
97+
end
98+
8999
@testset "transpose() and conj()" begin
90100
@test @inferred(conj(SVector(1+im, 2+im))) === SVector(1-im, 2-im)
91101

@@ -150,6 +160,7 @@ using StaticArrays, Base.Test
150160

151161
@testset "size zero" begin
152162
@test vecdot(SVector{0, Float64}(()), SVector{0, Float64}(())) === 0.
163+
@test StaticArrays.bilinear_vecdot(SVector{0, Float64}(()), SVector{0, Float64}(())) === 0.
153164
@test vecnorm(SVector{0, Float64}(())) === 0.
154165
@test vecnorm(SVector{0, Float64}(()), 1) === 0.
155166
@test trace(SMatrix{0,0,Float64}(())) === 0.

0 commit comments

Comments
 (0)