• Status: Solved
• Priority: Medium
• Security: Public
• Views: 173

# 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
``````
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
``````

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);
}
``````
I was told not to use boost or my own class to handle the sub-matrix due to optimization plans.
Thanks.
0
phoffric
• 2
1 Solution

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);
}
``````
0

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.