Skip to content

Commit fe7bb29

Browse files
committed
Fix generic axpy! for vector element types without commutative multiplication,
e.g. quaternions.
1 parent 4f9e506 commit fe7bb29

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

base/linalg/generic.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,20 +439,20 @@ function peakflops(n::Integer=2000; parallel::Bool=false)
439439
parallel ? sum(pmap(peakflops, [ n for i in 1:nworkers()])) : (2*Float64(n)^3/t)
440440
end
441441

442-
# BLAS-like in-place y=alpha*x+y function (see also the version in blas.jl
442+
# BLAS-like in-place y = x*α+y function (see also the version in blas.jl
443443
# for BlasFloat Arrays)
444-
function axpy!(alpha, x::AbstractArray, y::AbstractArray)
444+
function axpy!(α, x::AbstractArray, y::AbstractArray)
445445
n = length(x)
446446
if n != length(y)
447447
throw(DimensionMismatch("x has length $n, but y has length $(length(y))"))
448448
end
449449
for i = 1:n
450-
@inbounds y[i] += alpha * x[i]
450+
@inbounds y[i] += x[i]*α
451451
end
452452
y
453453
end
454454

455-
function axpy!{Ti<:Integer,Tj<:Integer}(alpha, x::AbstractArray, rx::AbstractArray{Ti}, y::AbstractArray, ry::AbstractArray{Tj})
455+
function axpy!{Ti<:Integer,Tj<:Integer}(α, x::AbstractArray, rx::AbstractArray{Ti}, y::AbstractArray, ry::AbstractArray{Tj})
456456
if length(x) != length(y)
457457
throw(DimensionMismatch("x has length $(length(x)), but y has length $(length(y))"))
458458
elseif minimum(rx) < 1 || maximum(rx) > length(x)
@@ -463,7 +463,7 @@ function axpy!{Ti<:Integer,Tj<:Integer}(alpha, x::AbstractArray, rx::AbstractArr
463463
throw(ArgumentError("rx has length $(length(rx)), but ry has length $(length(ry))"))
464464
end
465465
for i = 1:length(rx)
466-
@inbounds y[ry[i]] += alpha * x[rx[i]]
466+
@inbounds y[ry[i]] += x[rx[i]]*α
467467
end
468468
y
469469
end

test/linalg/generic.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ let x = Vector{Int}[[1,2], [3,4]]
141141
@test norm(x, 1) sqrt(5) + 5
142142
@test norm(x, 3) cbrt(sqrt(125)+125)
143143
end
144+
145+
# test that LinAlg.axpy! works for element type without commutative multiplication
146+
let
147+
α = ones(Int, 2, 2)
148+
x = fill([1 0; 1 1], 3)
149+
y = fill(zeros(Int, 2, 2), 3)
150+
@test LinAlg.axpy!(α, x, deepcopy(y)) == x .* Matrix{Int}[α]
151+
@test LinAlg.axpy!(α, x, deepcopy(y)) != Matrix{Int}[α] .* x
152+
end

0 commit comments

Comments
 (0)