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

any example? #1

Open
3togo opened this issue Aug 24, 2022 · 1 comment
Open

any example? #1

3togo opened this issue Aug 24, 2022 · 1 comment

Comments

@3togo
Copy link

3togo commented Aug 24, 2022

Any simple example?

@HunterBelanger
Copy link
Owner

HunterBelanger commented Aug 26, 2022

Hello !

Yes, I really need to get around to putting a few brief examples into the readme. I only really use this class for storing multi-dimensional data, or for writing data to .npy files to then be used in Python after. For example, if you have N points each with an x, y, and z coordinate I would do something like:

#include<ndarray.hpp>
#include <vector>

struct Point {
    double x, y, z;
}

const std::vector<Point>& get_points();

int main() {

    const std::vector<Point>& points = get_points();
    // The {points.size(), 3} is initialized to an
    // std::vector<std::size_t> which is the shape of the array.
    NDarray<double> points_out({points.size(), 3});

    for (std::size_t i = 0; i < points.size(); i++) {
        points_out(i, 0) = points[i].x;
        points_out(i, 1) = points[i].y;
        points_out(i, 2) = points[i].z;
    }

    // You can then save your data
    points_out.save("points.npy");

    return 0;
}

You could store the Point objects directly in the NDarray if you only need them in C++. The problem is the Point objects can't be written to the Numpy file directly, as this wouldn't be understood in Python.

I hope this helps ! I will try to added a few more examples to the readme in a few days.

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

No branches or pull requests

2 participants