Skip to content

Commit e8f57bb

Browse files
committed
minor fixes
1 parent d006c82 commit e8f57bb

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

src/LinearMaps.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,17 @@ Base.ndims(::LinearMap) = 2
4747
Base.size(A::LinearMap, n) = (n==1 || n==2 ? size(A)[n] : error("LinearMap objects have only 2 dimensions"))
4848
Base.length(A::LinearMap) = size(A)[1] * size(A)[2]
4949

50-
# check dimension consistency for right multiply: y = A*x and Y = A*X
51-
function check_dim_mul(Y::AbstractVecOrMat, A::LinearMap, X::AbstractVecOrMat)
50+
# check dimension consistency for multiplication C = A*B
51+
function check_dim_mul(C, A, B)
5252
# @info "checked vector dimensions" # uncomment for testing
53-
m, n = size(A)
54-
(m == size(Y, 1) && n == size(X, 1) && size(Y, 2) == size(X, 2)) ||
55-
throw(DimensionMismatch("mul! $(size(X)) $(size(Y)) $(size(A))"))
56-
return nothing
57-
end
58-
59-
# check dimension consistency for left multiply: X = Y*A (e.g., Y=y')
60-
function check_dim_mul(X::AbstractVecOrMat, Y::AbstractVecOrMat, A::LinearMap)
61-
m, n = size(A)
62-
(n == size(X, 2) && m == size(Y, 2) && size(Y, 1) == size(X, 1)) ||
63-
throw(DimensionMismatch("left mul!"))
53+
mA, nA = size(A) # A always has two dimensions
54+
mB, nB = size(B, 1), size(B, 2)
55+
if mB != nA
56+
throw(DimensionMismatch("left factor has dimensions ($mA,$nA), right factor has dimensions ($mB,$nB)"))
57+
end
58+
if size(C, 1) != mA || size(C, 2) != nB
59+
throw(DimensionMismatch("result has dimensions $(size(C)), needs ($mA,$nB)"))
60+
end
6461
return nothing
6562
end
6663

src/left.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ Base.:(*)(y::AdjointAbsVec, A::LinearMap) = adjoint(*(A', y'))
1515
Base.:(*)(y::TransposeAbsVec, A::LinearMap) = transpose(transpose(A) * transpose(y))
1616

1717
# mul!(x, y', A)
18-
LinearAlgebra.mul!(x::AdjointAbsVec, y::AdjointAbsVec, A::LinearMap) =
19-
mul!(x, y, A, true, false)
20-
21-
# todo: not sure if we need bounds checks and propagate inbounds stuff here
18+
Base.@propagate_inbounds function LinearAlgebra.mul!(x::AbstractMatrix, y::AdjointAbsVec, A::LinearMap)
19+
@boundscheck check_dim_mul(x, y, A)
20+
@inbounds mul!(adjoint(x), A', y')
21+
return adjoint(x)
22+
end
2223

23-
# mul!(x, y', A, α, β)
24-
# the key here is that "adjoint" and "transpose" are lazy "views"
25-
function LinearAlgebra.mul!(x::AdjointAbsVec, y::AdjointAbsVec,
24+
Base.@propagate_inbounds function LinearAlgebra.mul!(x::AbstractMatrix, y::AdjointAbsVec,
2625
A::LinearMap, α::Number, β::Number)
27-
check_dim_mul(x, y, A)
28-
mul!(adjoint(x), A', y', α, β)
26+
@boundscheck check_dim_mul(x, y, A)
27+
@inbounds mul!(adjoint(x), A', y', α, β)
2928
return adjoint(x)
3029
end
3130

32-
# mul!(x, transpose(y), A, α, β)
33-
function LinearAlgebra.mul!(x::TransposeAbsVec, y::TransposeAbsVec,
31+
Base.@propagate_inbounds function LinearAlgebra.mul!(x::AbstractMatrix, y::TransposeAbsVec, A::LinearMap)
32+
@boundscheck check_dim_mul(x, y, A)
33+
@inbounds mul!(transpose(x), transpose(A), transpose(y))
34+
return transpose(x)
35+
end
36+
37+
Base.@propagate_inbounds function LinearAlgebra.mul!(x::AbstractMatrix, y::TransposeAbsVec,
3438
A::LinearMap, α::Number, β::Number)
35-
check_dim_mul(x, y, A)
36-
mul!(transpose(x), transpose(A), transpose(y), α, β)
39+
@boundscheck check_dim_mul(x, y, A)
40+
@inbounds mul!(transpose(x), transpose(A), transpose(y), α, β)
3741
return transpose(x)
3842
end

test/left.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using LinearAlgebra: mul!
55

66

77
function left_tester(L::LinearMap{T}) where {T}
8-
M,N = size(L)
8+
M, N = size(L)
99
A = Matrix(L)
1010

1111
x = rand(T, N)
@@ -26,12 +26,10 @@ function left_tester(L::LinearMap{T}) where {T}
2626

2727
b1 = transpose(y) * A
2828
b2 = similar(b1)
29-
@static if VERSION v"1.4.0"
30-
mul!(b2, transpose(y), L) # 3-arg # todo: fails in Julia 1.0
29+
mul!(b2, transpose(y), L) # 3-arg
3130
@test b1 b2
3231
mul!(b2, transpose(y), L, true, false) # 5-arg
3332
@test b1 b2
34-
end # version
3533

3634
true
3735
end

0 commit comments

Comments
 (0)