Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement S[I::AbstractVector{Bool}] and S[I::AbstractVector] for sparse X #7023

Closed
ViralBShah opened this issue May 29, 2014 · 13 comments
Closed
Labels
sparse Sparse arrays
Milestone

Comments

@ViralBShah
Copy link
Member

julia> s = speye(5); x =  [true,false,true];

julia> s[x]
ERROR: no method similar(SparseMatrixCSC{Float64,Int64}, Type{Float64}, (Int64,))
 in getindex at abstractarray.jl:363

julia> s = speye(5); y =  [1,2,3];

julia> s[y]
ERROR: no method similar(SparseMatrixCSC{Float64,Int64}, Type{Float64}, (Int64,))
 in getindex at abstractarray.jl:363

Cc: @tanmaykm

@ViralBShah
Copy link
Member Author

The mention of this issue in #7006 was done mistakenly and is unrelated.

tanmaykm added a commit to tanmaykm/julia that referenced this issue May 30, 2014
@tanmaykm
Copy link
Member

tanmaykm commented Jun 3, 2014

Indexing with boolean array fails unless it is of same size as the array. Is this what is desired? This is not the behavior in octave and matlab.

julia> a = rand(5, 5)
5x5 Array{Float64,2}:
 0.904719   0.659319  0.108763  0.439896  0.769862
 0.0666711  0.860941  0.830392  0.634226  0.423541
 0.940728   0.681775  0.345935  0.253252  0.673635
 0.147969   0.850448  0.57376   0.2739    0.97953 
 0.303279   0.970842  0.382316  0.954433  0.57161 

julia> a[[true,false,true]]
ERROR: BoundsError()
 in getindex_bool_1d at array.jl:285
 in getindex at array.jl:300

@nalimilan
Copy link
Member

I think this is intentional and related to the "no recycling" rule. It could be added to the Noteworthy differences section of the manual for Matlab. For now it's explained in the R section in PR #6221:

+- Like most languages, Julia does not, by default, allow operations on unequal
+  length vectors by cycling the shorter one. Julia will throw errors when the
+  lengths of inputs do not match, unlike R in which c(1,2,3,4) + c(1,2) will
+  evaluate to c(2,4,4,6).

@pao
Copy link
Member

pao commented Jun 3, 2014

...I'm not even sure what MATLAB's rule is here. Seems better to avoid this entirely.

>> a = magic(5)
a =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9
>> a(logical([0 1; 0 1]))
ans =
     4
    10

@tkelman
Copy link
Contributor

tkelman commented Jun 3, 2014

Matlab's rule appears to be a(b(:)) for a(b), and if b is shorter than a then b is effectively padded with false's. +1 for just erroring though (perhaps a nicer more specific error), that cycling behavior from R makes me throw up a little.

@pao
Copy link
Member

pao commented Jun 3, 2014

@tkelman ahh!

I think broadcast_getindex should probably have us covered.

@tanmaykm
Copy link
Member

tanmaykm commented Jun 4, 2014

Thanks. I think that's fair. Probably just needs a better mention in the array indexing section of the manual.

@jiahao
Copy link
Member

jiahao commented Jun 4, 2014

Matlab's behavior in this example is consistent with the general indexing rule that a smaller array behaves as if it is zero-padded beyond its actual dimensions, and is allowed to grow by zero-padding if necessary. This specific case of logical indexing is documented here in the subsection "Logical Indexing with a Smaller Array".

@pao
Copy link
Member

pao commented Jun 4, 2014

Matlab's behavior in this example is consistent with the general indexing rule that a smaller array behaves as if it is zero-padded beyond its actual dimensions, and is allowed to grow by zero-padding if necessary.

But I was not expecting MATLAB to columnize the matrix first.

tanmaykm added a commit to tanmaykm/julia that referenced this issue Jun 4, 2014
@jiahao
Copy link
Member

jiahao commented Jun 5, 2014

But I was not expecting MATLAB to columnize the matrix first.

Matlab calls this linear indexing. It's not documented, but I believe Matlab resorts to linear indexing whenever ordinary indexing doesn't make sense. (Implicitly flattening the matrix is part of the attempt to apply linear indexing semantics.) The interaction of all of Matlab's indexing rules can be rather intricate to figure out.

tanmaykm added a commit to tanmaykm/julia that referenced this issue Jun 5, 2014
tanmaykm added a commit to tanmaykm/julia that referenced this issue Jun 5, 2014
tanmaykm added a commit to tanmaykm/julia that referenced this issue Jun 6, 2014
@pao
Copy link
Member

pao commented Jun 6, 2014

I know it's linear indexing. I've just never seen it do that with a logical index! (Or if I have, I filed it into the "don't ever do that again" category).

At any rate, we seem to all agree it's not something we should emulate. Consider this horse thoroughly flogged.

@jiahao
Copy link
Member

jiahao commented Jun 6, 2014

I know you know. It was more a comment for posterity. ;)

@ViralBShah
Copy link
Member Author

#7047 closes this. The behaviour for logical indexing is the same as the dense case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

6 participants