Skip to content

Commit fefe5f3

Browse files
committed
Consolidate _bilinear_vecdot and _vecdot.
1 parent 3c9a2bc commit fefe5f3

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

src/linalg.jl

+8-25
Original file line numberDiff line numberDiff line change
@@ -214,42 +214,25 @@ end
214214
@inbounds return similar_type(a, typeof(Signed(a[2]*b[3])-Signed(a[3]*b[2])))(((Signed(a[2]*b[3])-Signed(a[3]*b[2]), Signed(a[3]*b[1])-Signed(a[1]*b[3]), Signed(a[1]*b[2])-Signed(a[2]*b[1]))))
215215
end
216216

217-
@inline dot(a::StaticVector, b::StaticVector) = _vecdot(same_size(a, b), a, b)
218-
@inline function _vecdot(::Size{S}, a::StaticArray, b::StaticArray) where {S}
217+
@inline dot(a::StaticVector, b::StaticVector) = _vecdot(same_size(a, b), a, b, (x, y) -> adjoint(x) * y)
218+
@inline bilinear_vecdot(a::StaticArray, b::StaticArray) = _vecdot(same_size(a, b), a, b, *)
219+
220+
@inline function _vecdot(::Size{S}, a::StaticArray, b::StaticArray, product) where {S}
219221
if prod(S) == 0
220-
za = zero(eltype(a))
221-
zb = zero(eltype(b))
222+
za, zb = zero(eltype(a)), zero(eltype(b))
222223
else
223224
# Use an actual element if there is one, to support e.g. Vector{<:Number}
224225
# element types for which runtime size information is required to construct
225226
# a zero element.
226-
za = zero(a[1])
227-
zb = zero(b[1])
227+
za, zb = zero(a[1]), zero(b[1])
228228
end
229-
ret = adjoint(za) * zb + adjoint(za) * zb
229+
ret = product(za, zb) + product(za, zb)
230230
@inbounds @simd for j = 1 : prod(S)
231-
ret += adjoint(a[j]) * b[j]
231+
ret += product(a[j], b[j])
232232
end
233233
return ret
234234
end
235235

236-
@inline bilinear_vecdot(a::StaticArray, b::StaticArray) = _bilinear_vecdot(same_size(a, b), a, b)
237-
@generated function _bilinear_vecdot(::Size{S}, a::StaticArray, b::StaticArray) where {S}
238-
if prod(S) == 0
239-
return :(zero(promote_op(*, eltype(a), eltype(b))))
240-
end
241-
242-
expr = :(a[1] * b[1])
243-
for j = 2:prod(S)
244-
expr = :($expr + a[$j] * b[$j])
245-
end
246-
247-
return quote
248-
@_inline_meta
249-
@inbounds return $expr
250-
end
251-
end
252-
253236
@inline LinearAlgebra.norm_sqr(v::StaticVector) = mapreduce(abs2, +, v; init=zero(real(eltype(v))))
254237

255238
@inline norm(a::StaticArray) = _norm(Size(a), a)

0 commit comments

Comments
 (0)