TRIQS / nda

C++ library for multi-dimensional arrays
https://triqs.github.io/nda
Other
13 stars 11 forks source link

Unexpected behavior when creating a permuted_indices_view #54

Closed Thoemi09 closed 1 month ago

Thoemi09 commented 8 months ago

The behavior of permuted_indices_view (and the idx_map::transpose function) works differently than I would expect. Instead of applying a given permutation directly to the indices, the inverse permutation is applied.

Steps to reproduce

#include <iostream>
#include <nda/nda.hpp>

int main() {
    nda::array<double, 3> a(2, 4, 6);
    std::cout << "Original: " << a.shape() << std::endl;

    auto b = nda::permuted_indices_view<nda::encode(std::array{1, 2, 0})>(a);
    std::cout << "Permuted: " << b.shape() << std::endl;
}

Output:

Original: (2 4 6)
Permuted: (6 2 4)

Expected behavior

I would have expected a behavior like numpy's transpose function:

import numpy as np

a = np.zeros((2, 4, 6))
print("Original: {}".format(a.shape))

b = np.transpose(a, (1, 2, 0))
print("Permuted: {}".format(b.shape))

Output:

Original: (2, 4, 6)
Permuted: (4, 6, 2)
Wentzell commented 1 month ago

As you have also clarified in your documentation improvements, c.f. https://github.com/TRIQS/nda/blob/b5c39bb80ec6ec398c2c55bd6b7d61fef348067c/c%2B%2B/nda/layout/idx_map.hpp#L594-L596 the convention in NDA is for the permutation to describe how to obtain the new length / stride value from the old one, i.e. Snew[p(i)] = S[i] and Lnew[p(i)] = L[i]