valarray submatrix representation using gslice

How should a function, foo, be defined so that it will accept a valarray (that represents a MxN matrix), so that foo operates on a sub-matrix?

I've read enough to know that I need to use gslice to pick out the sub-matrix. (I don't want to use the mask or indirect because the original matrix and the sub-matrix will be huge.)

As an example of what foo will see..
Say, original valarray has a 35 elements representing a 5 row by 7 col matrix; and the sub-matrix starts in the 2nd row, 2nd column (i.e., offset 8), and whose dimensions are 3 rows by 4 cols.
foo should just print out the sub-matrix values.

Suppose the initial values of the valarray are just set to the index number (i.e., first element is 0). It looks like:
 0  1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34

Open in new window

Then with start offset, S = 8, nRow = 3, nCol = 4, foo should be able to operate on the following sub-matrix:
 8  9 10 11
15 16 17 18
22 23 24 25

Open in new window


I started with this, but not sure how to use gslice and set up gslice params correctly.
foo should use this gslice, I think.
#include <iostream>
#include <valarray>
using namespace std;

template<class T>
void print(const valarray<T> &v) {
   for(size_t i = 0; i < v.size(); ++i) 
      cout << '\t' << v[i];
   cout << endl;
}

template<class T>
void init(valarray<T>& v)
{
   for(size_t i=0; i < v.size(); ++i)
   {
      v[i] = i;
   }
}

void foo()
{
}

int main()
{
   const int M=7, N=5;
   valarray<int> va(M*N);
   init(va);
   size_t sizes[2]   = {3, 4};
   size_t strides[2] = {7, 1};
   gslice gs;
   foo();
   print(va);
}

Open in new window

I was told not to use boost or my own class to handle the sub-matrix due to optimization plans.
Thanks.
LVL 33
phoffric\Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Author Commented:
I made some progress. But foo() is taking two parameters - a valarray and a gslice, and print2d is taking 3 parameters. But that may be good enough. I thought I would be able to pick an element from the matrix with two brackets; for example, va[2][1].
#include <iostream>
#include <valarray>
using namespace std;

template<class T>
void print2d(valarray<T> &v, size_t nRow, size_t nCol) {
   for(size_t r = 0; r < nRow; ++r)
   {
      for(size_t c = 0; c < nCol; ++c)
      {
          cout << ' ' << v[r*nCol + c];
      }
      cout << endl;
   }
   cout << endl;
}

template<class T>
void print(const valarray<T> &v) {
   for(size_t i = 0; i < v.size(); ++i) 
      cout << '\t' << v[i];
   cout << endl;
}

template<class T>
void init(valarray<T>& v)
{
   for(size_t i=0; i < v.size(); ++i)
   {
      v[i] = i;
   }
}

void foo(valarray<size_t>& vala, gslice& mygslice)
{
   size_t nRows = mygslice.size()[0];
   size_t nCols = mygslice.size()[1];
   valarray<size_t> varesult = vala[mygslice];
   print(varesult);
   print2d(varesult, nRows, nCols);
}

int main()
{
   const int M=7, N=5;
   valarray<size_t> va(M*N);
   init(va);

   size_t start=8;
   size_t lengths[]= {3, 4};
   size_t strides[]= {7, 1};

   gslice mygslice (
      start,
      valarray<size_t>(lengths,2),
      valarray<size_t>(strides,2));

   foo(va, mygslice);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffric\Author Commented:
I suppose that I can make a class that includes a valarray type and defines an operator () taking two arguments corresponding to row and column indices.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.