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

Basic indexing #21

Merged
merged 1 commit into from
Jan 6, 2025
Merged

Basic indexing #21

merged 1 commit into from
Jan 6, 2025

Conversation

seanmcl
Copy link
Collaborator

@seanmcl seanmcl commented Jan 2, 2025

"Basic" indexing means indexing with a constant or slice on all the dimensions. (Advanced indexing is for things like indexing by other arrays).

https://numpy.org/doc/stable/user/basics.indexing.html

@seanmcl seanmcl force-pushed the sm/06-tensor branch 2 times, most recently from 5a52db1 to daff58a Compare January 4, 2025 00:23
Base automatically changed from sm/06-tensor to main January 4, 2025 00:25
-- (np.int64(1), array([1]))
--
-- x[0].shape, x[0:1:1].shape
-- ((), (1,))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean same "dimension" not "shape"?
I think the shape of x is (6,), which isn't the same as (1,)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fixed.

loop items (dim :: shape)
else -- there are fewer items than the shape
loop (.ellipsis :: items) shape
| _, _ => .error "impossible"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused here for a second, until I noticed the length comparison at the start.
Is this case checking the same thing as the line on 63? (maybe a comment would be helpful)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comment. I lifted the error case to the top so it's more obvious

    | [], shape => .ok (shapeToSliceIters shape, shape)
    | _, [] => .error "impossible" -- We checked above that there are at least as many dims as items

let basic := match basic with
| none => { iter with done := true } -- All slice iterators are maxxed out in this case. Should we check this?
| some basic => { iter with basic }
some (ns, basic)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The algorithm here has a lot of finicky details, but it is still pretty easy to read.
Nice job!

iter := iter'
match <- f ns res with
| .yield k
| .done k => res := k
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May have fixed already, the issue with break or return mentioned in prior reviews.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed and added similar tests.

dtype := arr.dtype,
shape := newShape,
data
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I wish I had read this function first. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha. Yes I kind of miss out-of-order definitions for this reason. Doing lots of gritty work so the top level stuff is pretty.

@seanmcl seanmcl merged commit 2f3e42c into main Jan 6, 2025
1 check passed
@seanmcl seanmcl deleted the sm/07-index branch January 6, 2025 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants