-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Stop preventing indexing with Numbers #12486
Conversation
I'm curious (and on Windows at the moment, lacking the ability to try it out myself); what happens after this change if I do using DualNumbers
A = rand(10,25)
d = Dual(5,0)
A[d, d+8] # note: both dual numbers actually represent integers
# and convert(Int, d) works just fine On current master, I get a nice error message explaining what I did wrong:
It would be nice to make sure that this change doesn't make arrays segfault or something because the indexing is weird... |
You still get the same error message. |
I'll have to look into your interpolations pull request a little more closely, but I'm afraid that this isn't going to be the way we should achieve this. I think we need to split For scalar indexing, we need to do three things to the indices:
If we allow all Multidimensional indexing is a little simpler:
Actually, I think all we need to do here is remove |
I'm not sure we have to work so hard. The basic idea is:
In other words, I shouldn't have any expectation that core julia is going to massage my indexes into something my type can support---core julia's job is simply to deliver the appropriate scalars to my task. |
In other words, I'm proposing that |
Agreed. There are two tricky things here:
|
I've thought before about how it'll be very nice in 0.5 to just define the scalar indexing fallbacks for |
Does this mean that any custom array type will have to redefine the methods to allow indexing with |
I doubt it. If your type is a wrapper around an As far as I can tell the focused issue is this: result = A[[:a, :b, :c], [1, 2, 3]] # should return a 3x3 result How should I'm fine with waiting on this until 0.5, however. This might be too tricky to tackle at this point. |
Thanks for checking for me :) |
Why would |
@ScottPJones that's #10458. TL/DR: It's up to the custom types to implement non-Integer indexing. We won't do that for you in base. |
OK, that all came up just before I found out about Julia. However, I don't understand why the direction is to make Julia more restrictive, and less user friendly? IMO, Julia is nice because it can generally infer types, you don't have to worry so much about them, but here, it seems you have to know that only one type is permitted (i.e. |
@ScottPJones, as background for some
get expanded (automagically!) to
I guess if we want to support |
What's weird about it? Maybe it's my background, where everything was a string, and you only interpreted strings as numbers, so "78", 78.0, 7.8e1, 78, were all considered equal, and when used as a subscript, all found the same node. |
Julia.js |
Not sure what you mean by that "Julia.js", I'm not saying that Julia should be like JS, but it would seem that if the compiler can see that the type isn't |
I'm in favor of allowing indexing with general numeric types (at least reals), but autoconverting strings to integers is less ok, largely because it's a pretty expensive operation that seems like it should be asked for explicitly. |
Ah, I wasn't intending to say that strings should be autoconverted in Julia, that doesn't make sense, as in Julia you need to use parse instead of convert to go from "48" to 48. |
Two of the things I love most about "the Julian way" of doing things, is that there is a strong culture of trying to do the thing that makes the most sense for the user, and that there is a similarly strong culture of not assuming anything about the user's intentions that cannot be more-or-less proven to be true. Sometimes - and I think this is such a time - those two are at odds with each-other... but I think we can still do both. It all boils down to our interpretation of what the Let's consider a 2D example for simplicity: julia> A = reshape(1:5*4,4,5)
4x5 Array{Int64,2}:
1 5 9 13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20
julia> ind2sub(size(A), 14)
(2,4) Generalizing for reals could mean (and I'm going out on a limb here...) that for a (custom, user-defined) julia> ind2sub((3.5, 5.5), 13.3)
(2.8000000000000007,4.0) (calculated using Note that this is not merely "allowing some weird, maybe-correct inputs to Since this effectively de-couples EDIT: I realize the most controversial thing about this suggestion is allowing |
It all comes down to what being an But for a scaled interpolation object (which works in terms of a given axis, instead of the indices - JuliaMath/Interpolations.jl#47), that's no longer true. It doesn't seem like that big of a departure from indexed interpolation, and it sure seems like this should work similarly. But we're now in the case where there's not a good answer for |
In my custom array I overloaded interpolation into indexing but it got too confusing. When something is an Int it gets the value at the index, when a Real it interpolates on another basis -- 2 and 2.0 will give different results. I would like to use |
This change permits
AbstractArray
types to perform vector indexing with any type ofNumber
. For Interpolations, this enablesB = A[linspace(1,10,1001), linspace(1,15,201)]
to work (well, it will after JuliaMath/Interpolations.jl#54 gets merged...).CC @mbauman, @jakebolewski (ref #10458)