From 392b1a3a47b14b5ca35c83dddedf00e2cbfb16cb Mon Sep 17 00:00:00 2001 From: Art Kuo Date: Fri, 30 Oct 2015 11:17:06 -0400 Subject: [PATCH] fix range bugs for matrix multiply/solve, with AbstractMatrix --- base/linalg/bidiag.jl | 2 +- base/range.jl | 4 ++++ test/ranges.jl | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index dcf846d11f28d..88d8e789292f8 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -262,7 +262,7 @@ function naivesub!{T}(A::Bidiagonal{T}, b::AbstractVector, x::AbstractVector = b x end -function \{T,S}(A::Bidiagonal{T}, B::AbstractVecOrMat{S}) +function \{T,S}(A::Bidiagonal{T}, B::StridedVecOrMat{S}) TS = typeof(zero(T)*zero(S) + zero(T)*zero(S)) TS == S ? A_ldiv_B!(A, copy(B)) : A_ldiv_B!(A, convert(AbstractArray{TS}, B)) end diff --git a/base/range.jl b/base/range.jl index 33a8a2e2e712c..d6e43325c3e02 100644 --- a/base/range.jl +++ b/base/range.jl @@ -633,6 +633,10 @@ end ./(r::FloatRange, x::Real) = FloatRange(r.start/x, r.step/x, r.len, r.divisor) ./(r::LinSpace, x::Real) = LinSpace(r.start / x, r.stop / x, r.len, r.divisor) +# Matrix multiplication/solve on ranges, A*r should treat r as collected vector +*{T}(A::AbstractArray{T, 2}, r::Range) = A*collect(r) +\{T}(A::AbstractArray{T, 2}, r::Range) = A\collect(r) + promote_rule{T1,T2}(::Type{UnitRange{T1}},::Type{UnitRange{T2}}) = UnitRange{promote_type(T1,T2)} convert{T}(::Type{UnitRange{T}}, r::UnitRange{T}) = r diff --git a/test/ranges.jl b/test/ranges.jl index b3dcb785b8292..e12d1e44beb6a 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -244,6 +244,11 @@ end @test all(([1:5;] - (1:5)) .== 0) @test all(((1:5) - [1:5;]) .== 0) +# matrix multiplication between matrix and range, e.g. A*r +@test [1 2 3;]*(1:3) == [14] +# and a matrix solve A\r +@test [1 0; 0 1]\linspace(1,2,2) ≈ [1., 2.] + # tricky floating-point ranges @test [0.1:0.1:0.3;] == [linspace(0.1,0.3,3);] == [1:3;]./10