Skip to content

Commit 1c0bbb2

Browse files
committed
Merge pull request #15407 from musmo/axpy!_fix
Fix axpy! and add tests.
2 parents 597dc7b + ae775ea commit 1c0bbb2

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

base/linalg/generic.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,21 +482,20 @@ function axpy!(α, x::AbstractArray, y::AbstractArray)
482482
end
483483

484484
function axpy!{Ti<:Integer,Tj<:Integer}(α, x::AbstractArray, rx::AbstractArray{Ti}, y::AbstractArray, ry::AbstractArray{Tj})
485-
if length(x) != length(y)
486-
throw(DimensionMismatch("x has length $(length(x)), but y has length $(length(y))"))
485+
if length(rx) != length(ry)
486+
throw(DimensionMismatch("rx has length $(length(rx)), but ry has length $(length(ry))"))
487487
elseif minimum(rx) < 1 || maximum(rx) > length(x)
488488
throw(BoundsError(x, rx))
489489
elseif minimum(ry) < 1 || maximum(ry) > length(y)
490490
throw(BoundsError(y, ry))
491-
elseif length(rx) != length(ry)
492-
throw(ArgumentError("rx has length $(length(rx)), but ry has length $(length(ry))"))
493491
end
494492
for i = 1:length(rx)
495493
@inbounds y[ry[i]] += x[rx[i]]*α
496494
end
497495
y
498496
end
499497

498+
500499
# Elementary reflection similar to LAPACK. The reflector is not Hermitian but ensures that tridiagonalization of Hermitian matrices become real. See lawn72
501500
@inline function reflector!(x::AbstractVector)
502501
n = length(x)

test/linalg/generic.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ y = ['a','b','c','d','e']
8484
@test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,['g'])
8585
@test_throws BoundsError Base.LinAlg.axpy!(α,x,collect(-1:5),y,collect(1:7))
8686
@test_throws BoundsError Base.LinAlg.axpy!(α,x,collect(1:7),y,collect(-1:5))
87-
@test_throws ArgumentError Base.LinAlg.axpy!(α,x,collect(1:3),y,collect(1:5))
8887
@test_throws BoundsError Base.LinAlg.axpy!(α,x,collect(1:7),y,collect(1:7))
89-
@test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,collect(1:2),['a','b'],collect(1:2))
88+
@test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,collect(1:3),y,collect(1:5))
9089

9190
@test_throws ArgumentError diag(rand(10))
9291
@test !issymmetric(ones(5,3))
@@ -200,6 +199,16 @@ let
200199
@test LinAlg.axpy!(α, x, deepcopy(y)) != Matrix{Int}[α] .* x
201200
end
202201

202+
# test that LinAlg.axpy! works for x and y of different dimensions
203+
let
204+
α = 5
205+
x = 2:5
206+
y = ones(Int, 2, 4)
207+
rx = [1 4]
208+
ry = [2 8]
209+
@test LinAlg.axpy!(α, x, rx, y, ry) == [1 1 1 1; 11 1 1 26]
210+
end
211+
203212
let
204213
vr = [3.0, 4.0]
205214
for Tr in (Float32, Float64)

0 commit comments

Comments
 (0)