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

std::vector<std::vector<double>> to NdArray<double> #59

Closed
georgios-v opened this issue May 23, 2020 · 3 comments
Closed

std::vector<std::vector<double>> to NdArray<double> #59

georgios-v opened this issue May 23, 2020 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@georgios-v
Copy link

Is your feature request related to a problem? Please describe.
My existing algorithms produce a 2d matrix in the form of std::vector<std::vector> of equal size

Describe the solution you'd like
there should be a constructor to a 2D NdArray with std::vector<std::vector> as input. It parallels the existing initializer list constructor

Describe alternatives you've considered
I have not found any alternative as it seems impossible to construct what I want with the existing API. So if this functionality exists the documentation fails to explain how it is provided.

@georgios-v
Copy link
Author

georgios-v commented May 23, 2020

So after I posted this I found out that this seems to work:

std::vector<std::vector<double>> vv(n_samples, std::vector<double>(n_samples, 0.0));
auto s = nc::NdArray<double>(n_samples, n_samples);
for (int i = 0; i < n_samples; ++i) {
	s(i, s.cSlice()) = nc::fromiter<double>(vv[i].begin(), vv[i].end());
}

Is this the proper way to do it? Even so there should be a constructor still.

@dpilger26 dpilger26 self-assigned this May 23, 2020
@dpilger26 dpilger26 added the enhancement New feature or request label May 23, 2020
@dpilger26
Copy link
Owner

No, what you have above will not work. Slicing operations in NumCpp return copies. You could modify your example by using one the overloaded put() methods. However, the most efficient way to do what you are trying to do would be to directly loop over the data.

#include "NumCpp.hpp"

#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>

int main()
{
    // create a vector<vector> and fill with monotonically increasing values
    const nc::uint32 n_samples = 10;
    std::vector<std::vector<double>> vv(n_samples, std::vector<double>(n_samples, 0.0));

    double start = 0.0;
    for (auto& row : vv)
    {
        std::iota(row.begin(), row.end(), start);
        start += row.size();
    }

    // convert the vector<vector> to NdArray
    auto s = nc::NdArray<double>(n_samples, n_samples);
    for (nc::uint32 row = 0; row < n_samples; ++row) 
    {
        for (nc::uint32 col = 0; col < n_samples; ++col)
        {
            s(row, col) = vv[row][col];
        }
    }

    // display the array for sanity
    std::cout << s;

    return EXIT_SUCCESS;
}

I'm working on a fairly large update at the moment that should be ready to release in 2 weeks or so. I'll add additional constructors in this update to more directly accomplish what you are trying to do.

@dpilger26
Copy link
Owner

Added additional NdArray constructor overloads including std::vector<std::vector<T>> into the version 2.0.0 release. See the documentation here for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants