After reading through the PR below, and some blog posts about generalized indexing in AbstractArrays
, I am wondering if the code below is the right way to iterate through the rows of a matrix:
"Check if argument is a probability vector."
isprobvec{T}(v::AbstractVector{T}) = all(x->x ≥ 0, v) && sum(v) ≈ one(T)
"Check if argument is a probability (stochastic) matrix."
isprobmat(m::AbstractMatrix) = all(i->isprobvec(view(m, i, :)), indices(m, 1))
JuliaLang:master
← JuliaLang:teh/inds
opened 12:34PM - 08 May 16 UTC
This is the one part of [ArrayIteration.jl](https://github.com/timholy/ArrayIter… ation.jl) that I think it's reasonable to move to Base for the julia-0.5 release. The core change is the addition of the ~~`inds`~~`indices` function, which should be thought of as a sister function to `size` but instead returns a tuple containing the in-bounds indexes of an array. The fallback is
``` jl
indices(A::AbstractArray, d) = 1:size(A,d)
indices{T,N}(A::AbstractArray{T,N}) = ntuple(d->inds(A, d), Val{N})
```
But this allows one to override `indices` for specific types:
``` jl
A = OffsetArray([1 3; 2 4], (-1,2))
@test A[0,3] == 1
@test A[1,3] == 2
@test A[0,4] == 3
@test A[1,4] == 4
julia> A[1,1]
ERROR: BoundsError: attempt to access OAs.OffsetArray{Int64,2,Array{Int64,2}} with inds (0:1,3:4):
#undef #undef
#undef #undef
at index [1,1]
in throw_boundserror(::OAs.OffsetArray{Int64,2,Array{Int64,2}}, ::Tuple{Int64,Int64}) at ./abstractarray.jl:134
[inlined code] from ./abstractarray.jl:103
in getindex(::OAs.OffsetArray{Int64,2,Array{Int64,2}}, ::Int64, ::Int64) at /home/tim/src/julia-0.5/test/abstractarray.jl:542
in eval(::Module, ::Any) at ./boot.jl:230
```
This adds the definition and uses it in `checkbounds`. The bigger job will be wiring it into the rest of Base. So I thought I'd post at this stage and see what folks think.