Skip to content

Commit

Permalink
Fix axpy! bug and add tests
Browse files Browse the repository at this point in the history
(cherry picked from commit ae775ea)
ref JuliaLang#15407
  • Loading branch information
mustafa authored and tkelman committed Mar 13, 2016
1 parent e5701a5 commit a5ba47a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 3 additions & 4 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -447,21 +447,20 @@ function axpy!(α, x::AbstractArray, y::AbstractArray)
end

function axpy!{Ti<:Integer,Tj<:Integer}(α, x::AbstractArray, rx::AbstractArray{Ti}, y::AbstractArray, ry::AbstractArray{Tj})
if length(x) != length(y)
throw(DimensionMismatch("x has length $(length(x)), but y has length $(length(y))"))
if length(rx) != length(ry)
throw(DimensionMismatch("rx has length $(length(rx)), but ry has length $(length(ry))"))
elseif minimum(rx) < 1 || maximum(rx) > length(x)
throw(BoundsError(x, rx))
elseif minimum(ry) < 1 || maximum(ry) > length(y)
throw(BoundsError(y, ry))
elseif length(rx) != length(ry)
throw(ArgumentError("rx has length $(length(rx)), but ry has length $(length(ry))"))
end
for i = 1:length(rx)
@inbounds y[ry[i]] += x[rx[i]]*α
end
y
end


# Elementary reflection similar to LAPACK. The reflector is not Hermitian but ensures that tridiagonalization of Hermitian matrices become real. See lawn72
@inline function reflector!(x::AbstractVector)
n = length(x)
Expand Down
13 changes: 11 additions & 2 deletions test/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ y = ['a','b','c','d','e']
@test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,['g'])
@test_throws BoundsError Base.LinAlg.axpy!(α,x,collect(-1:5),y,collect(1:7))
@test_throws BoundsError Base.LinAlg.axpy!(α,x,collect(1:7),y,collect(-1:5))
@test_throws ArgumentError Base.LinAlg.axpy!(α,x,collect(1:3),y,collect(1:5))
@test_throws BoundsError Base.LinAlg.axpy!(α,x,collect(1:7),y,collect(1:7))
@test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,collect(1:2),['a','b'],collect(1:2))
@test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,collect(1:3),y,collect(1:5))

@test_throws ArgumentError diag(rand(10))
@test !issym(ones(5,3))
Expand Down Expand Up @@ -152,3 +151,13 @@ let
@test LinAlg.axpy!(α, x, deepcopy(y)) == x .* Matrix{Int}[α]
@test LinAlg.axpy!(α, x, deepcopy(y)) != Matrix{Int}[α] .* x
end

# test that LinAlg.axpy! works for x and y of different dimensions
let
α = 5
x = 2:5
y = ones(Int, 2, 4)
rx = [1 4]
ry = [2 8]
@test LinAlg.axpy!(α, x, rx, y, ry) == [1 1 1 1; 11 1 1 26]
end

0 comments on commit a5ba47a

Please sign in to comment.