@@ -48,15 +48,20 @@ Base.ndims(::LinearMap) = 2
4848Base. size (A:: LinearMap , n) = (n== 1 || n== 2 ? size (A)[n] : error (" LinearMap objects have only 2 dimensions" ))
4949Base. length (A:: LinearMap ) = size (A)[1 ] * size (A)[2 ]
5050
51+ # check dimension consistency for multiplication A*B
52+ _iscompatible ((A, B)) = size (A, 2 ) == size (B, 1 )
53+ function check_dim_mul (A, B)
54+ _iscompatible ((A, B)) ||
55+ throw (DimensionMismatch (" second dimension of left factor, $(size (A, 2 )) , " *
56+ " does not match first dimension of right factor, $(size (B, 1 )) " ))
57+ return nothing
58+ end
5159# check dimension consistency for multiplication C = A*B
5260function check_dim_mul (C, A, B)
53- # @info "checked vector dimensions" # uncomment for testing
5461 mA, nA = size (A) # A always has two dimensions
5562 mB, nB = size (B, 1 ), size (B, 2 )
56- (mB == nA) ||
57- throw (DimensionMismatch (" left factor has dimensions ($mA ,$nA ), right factor has dimensions ($mB ,$nB )" ))
58- (size (C, 1 ) != mA || size (C, 2 ) != nB) &&
59- throw (DimensionMismatch (" result has dimensions $(size (C)) , needs ($mA ,$nB )" ))
63+ (mB == nA && size (C, 1 ) == mA && size (C, 2 ) == nB) ||
64+ throw (DimensionMismatch (" A has size ($mA ,$nA ), B has size ($mB ,$nB ), C has size $(size (C)) " ))
6065 return nothing
6166end
6267
@@ -93,10 +98,8 @@ julia> A*x
9398```
9499"""
95100function Base.:(* )(A:: LinearMap , x:: AbstractVector )
96- m, n = size (A)
97- n == length (x) || throw (DimensionMismatch (" linear map has dimensions ($m ,$n ), " *
98- " vector has length $(length (x)) " ))
99- return mul! (similar (x, promote_type (eltype (A), eltype (x)), m), A, x)
101+ check_dim_mul (A, x)
102+ return mul! (similar (x, promote_type (eltype (A), eltype (x)), size (A, 1 )), A, x)
100103end
101104
102105"""
0 commit comments