Skip to content

Nested arrays for simple slicing - #1505

Open
d7919 wants to merge 9 commits into
nextfrom
nested_arrays_for_simple_slicing
Open

Nested arrays for simple slicing#1505
d7919 wants to merge 9 commits into
nextfrom
nested_arrays_for_simple_slicing

Conversation

@d7919

@d7919 d7919 commented Jan 16, 2019

Copy link
Copy Markdown
Member

This PR is mostly just for discussion -- it provides a sample implementation of a multi-dimensional container that supports simple slicing. It's ultimately backed by Array.

It's probably not very efficient currently.

It's missing unit tests.

Here's an ugly example of how it could be (ab)used

 ArrayND<BoutReal, 1> myArr(10);
  ArrayND<BoutReal, 2> myArr2(10, 20);
  ArrayND<BoutReal, 2> myArrPack(2, 5);

  // Fill a 1D array
  for(int i = 0 ; i<10; i++) {
    myArr[i] = i*i;
  }

 // Squash the 1D array into a 2D array.
  myArrPack.pack(myArr);

// Get a flattened (1D) version of a 2D array
  auto myArrTmp = myArrPack.flatten();

// Print all the values
  int count = 0;
  for (int i = 0 ; i<std::get<0>(myArrPack.shape()); i++) {
    for (int j = 0 ; j<std::get<1>(myArrPack.shape()); j++) {
      output<<myArr[count]<<" "<<myArrPack[i][j]<<" "<<myArrPack(i,j)<<" "<<myArrTmp(count)<<endl;
      count++;
    }
  }

// Get a slice of the 2D array -- this is a 1D array
auto mySlice = myArrPack[0];
ASSERT1(mySlice.size() == 5);
for (int i = 0; i<mySlice.size(); i++) {
   output<<i<<" (i+5)^2 = "<<mySlice[i]<<endl;
}

This could be useful for the inversion code where we deal with matrices/tensors and want to get the contiguous z-slices out of this data -- with ArrayND we can get these as Arrays without any copying.

@d7919 d7919 added discussion work in progress Not ready for merging labels Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discussion work in progress Not ready for merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant