Skip to content

Commit

Permalink
fix range bugs for matrix multiply/solve
Browse files Browse the repository at this point in the history
  • Loading branch information
artkuo committed Oct 30, 2015
1 parent 197672f commit e4b8c38
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
*(A::AbstractArray, r::Range) = A*collect(r)
\(A::AbstractArray, 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
Expand Down
5 changes: 5 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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_approx_eq [1 2 3; 4 5 6; 7 8 9]\linspace(1,3,3) [-1/3, 2/3, 0]

# tricky floating-point ranges

@test [0.1:0.1:0.3;] == [linspace(0.1,0.3,3);] == [1:3;]./10
Expand Down

0 comments on commit e4b8c38

Please sign in to comment.