Skip to content

NdArray Initializer API #4

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

Closed
wants to merge 2 commits into from

Conversation

karllessard
Copy link
Collaborator

@karllessard karllessard commented Dec 5, 2021

This draft shows how we could hydrate a just-allocated array using a new API that focuses on a stateful builder approach rather than the positional endpoints setXXX(value, coords) exposed by the NdArray itself.

Not only it simplifies the task of initializing an array with explicit values but also it allows us to allocate an empty sparse array and then let the user initialize its data (right now, the user have to allocate and initialize the data of the indices and values subarrays first before being able to allocate a sparse array).

Example of usage:

NdArray<String> array = NdArrays.ofObjects(String.class, Shape.of(3, 2), initializer -> {
     initializer.byVectors()
         .put("Cat", "Dog")
         .put("House") // partial initialization
         .to(2)
         .put("Orange", "Apple");
});
// -> [["Cat", "Dog"], ["House", null], ["Orange", "Apple"]]

Copy link
Collaborator

@Craigacp Craigacp left a comment

Choose a reason for hiding this comment

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

Needs a bunch more documentation, I'm a bit confused by the type hierarchy, and it probably needs building out for the rest of the types before we make a release with it in.

Elements at(long... coordinates);

/**
* Set a n-dimensional array of doubles as the next element in the hydrated array.
Copy link
Collaborator

Choose a reason for hiding this comment

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

So maybe this should be "next elements" rather than "next element", but also it's not entirely clear to me what happens if I pass in a rectangular ndarray into a larger ndarray where the values are set. Are they read off in row-major order and placed into the next elements in row-major order of the target array? Or is it doing broadcasting/shape matching of some kind?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made the contract more strict when it comes to per-element initialization: an initialization pass must always be done with elements of the same rank. So basically you target a dimension and then provide the elements for it.

this.coordinates = Helpers.validateCoordinates(array, coordinates, 0);
}

protected long[] coordinates;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can these be final?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The lifecycle of these coordinates have changed in the last version

@karllessard
Copy link
Collaborator Author

Closing this draft, please take a look at #13 @Craigacp

@karllessard karllessard closed this Dec 5, 2022
@karllessard karllessard changed the title Skeleton of an NdArray hydration API NdArray Initializer API Dec 5, 2022
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