Giter Site home page Giter Site logo

row-major or col-major? about cnpy HOT 4 CLOSED

rogersce avatar rogersce commented on July 17, 2024
row-major or col-major?

from cnpy.

Comments (4)

RunOrVeith avatar RunOrVeith commented on July 17, 2024

For anyone else stumbling across this:
Numpy stores data in row-major by default, but Eigen in column-major.

So in order to load the data correctly, you have to do the following

    cnpy::npz_t loadedNpz = cnpy::npz_load(file.string());
    cnpy::NpyArray npPositions = loadedNpz["positions"];

    // YOU HAVE TO BE CAREFUL, NUMPY STORES DATA IN ROW-MAJOR, BUT EIGEN in COLUMN-MAJOR BY DEFAULT
    float *positionsArray = npPositions.data<float>();
    Eigen::MatrixX3f positions;
    positions = Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, 3, Eigen::RowMajor>>(positionsArray, npPositions.shape[0], npPositions.shape[1]);

If you load something that is square (e.g. a 4x4 matrix), it will appear to be transposed if you don't specify the Eigen::RowMajor flag. If you load something non-square (e.g 100x3), it will be completely different to the original data order if you don't specify the flag.

from cnpy.

firdota avatar firdota commented on July 17, 2024

if the var "fortran_order" in struct NpyArray is TRUE, then col-oder; else row-oder

from cnpy.

poor1017 avatar poor1017 commented on July 17, 2024

Thank you two.

from cnpy.

csyhping avatar csyhping commented on July 17, 2024

For anyone else stumbling across this: Numpy stores data in row-major by default, but Eigen in column-major.

So in order to load the data correctly, you have to do the following

    cnpy::npz_t loadedNpz = cnpy::npz_load(file.string());
    cnpy::NpyArray npPositions = loadedNpz["positions"];

    // YOU HAVE TO BE CAREFUL, NUMPY STORES DATA IN ROW-MAJOR, BUT EIGEN in COLUMN-MAJOR BY DEFAULT
    float *positionsArray = npPositions.data<float>();
    Eigen::MatrixX3f positions;
    positions = Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, 3, Eigen::RowMajor>>(positionsArray, npPositions.shape[0], npPositions.shape[1]);

If you load something that is square (e.g. a 4x4 matrix), it will appear to be transposed if you don't specify the Eigen::RowMajor flag. If you load something non-square (e.g 100x3), it will be completely different to the original data order if you don't specify the flag.

hi @RunOrVeith , i tried with both Eigne::RowMajor and Eigen::ColMajor, the results are same. I need to call .transpose() to get the same data as numpy. Could you help with this? Thanks.

from cnpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.