Const Double Indexing with [][] in Matrix Class - c++

The following code contains a simple example of a Matrix class, with double indexing [][] enabled using a 'proxy' Row class.
#include <valarray>
#include <iostream>
template <typename T>
class Matrix {
private:
// Data members
int nRows_;
int nColumns_;
std::valarray<T> data_;
public:
// Constructor
Matrix(const int nRows,
const int nColumns)
: nRows_{nRows},
nColumns_{nColumns},
data_{std::valarray<T>(nRows*nColumns)} {}
// Row friend class to enable double indexing
class Row {
friend class Matrix;
private:
// Constructor
Row(Matrix& parent,
int row)
: parent_{parent},
row_{row} {}
// Data members
Matrix& parent_;
int row_;
public:
// Index columns
T& operator[](int column) {
int nColumns{parent_.nColumns_};
int element{row_*nColumns + column};
return parent_.data_[element];
}
};
// Index rows
Row operator[](int row) {
return Row(*this, row);
}
};
However, this doesn't allow double indexing of a const Matrix. For example, the below code fails to compile when the last line is included.
int main() {
Matrix<int> a{3,3};
const Matrix<int> b{3,3};
std::cout << a[1][2];
std::cout << b[1][2];
}
So the question is, how can I modify my Matrix class to allow for double indexing of const Matrix objects?

Since b is a const Matrix, you need to add const versions of your indexing operator.
Row operator[](int row) const { ... }
This will require additional changes to the Row class (or a second proxy class) to handle the const Matrix & and const overload of operator[].

I think I'm lacking a logical understanding of why things need/need not be const
It depends if you can modify argument or not. in general operator[] should not change object (unless it does, but then...ugh), it should return reference to object's resource. Tada! Your implementation doesn't allow that, because valarray is const then. You cant assign const matrix reference to
Matrix& parent_;
And as result
Row(const Matrix& parent, int row)
Why store Matrix itself... I'd stored reference of valarray.
Actually I would ditch container and create own transparent array in heap. that way Row would have a reference to part of array and length of row, nearly zero of overhead.

Related

C++ is there a way to overload operator [][]? [duplicate]

I have class CMatrix, where is "double pointer" to array of values.
class CMatrix {
public:
int rows, cols;
int **arr;
};
I simply need to access the values of matrix by typing:
CMatrix x;
x[0][0] = 23;
I know how to do that using:
x(0,0) = 23;
But I really need to do that the other way. Can anyone help me with that?
At the end I did it this way...
class CMatrix {
public:
int rows, cols;
int **arr;
public:
int const* operator[]( int const y ) const
{
return &arr[0][y];
}
int* operator[]( int const y )
{
return &arr[0][y];
}
....
You cannot overload operator [][], but the common idiom here is to use a proxy class, i.e. overload operator [] on your Matrix class to return an instance of a different class which then has operator [] overloaded on it.
For example:
class CMatrix {
public:
class CRow {
friend class CMatrix;
public:
int& operator[](int col)
{
return parent.arr[row][col];
}
private:
CRow(CMatrix &parent_, int row_) :
parent(parent_),
row(row_)
{}
CMatrix& parent;
int row;
};
CRow operator[](int row)
{
return CRow(*this, row);
}
private:
int rows, cols;
int **arr;
};
There is no operator[][] in C++. However, you can overload operator[] to return another structure, and in that overload operator[] too to get the effect you want.
You can do it by overloading operator[] to return an int*, which is then indexed by the second application of []. Instead of int* you could also return another class representing a row, whose operator[] gives access to individual elements of the row.
Essentially, subsequent applications of operator[] work on the result of the previous application.
If you create a matrix using Standard Library containers, it's trivial:
class Matrix {
vector<vector<int>> data;
public:
vector<int>& operator[] (size_t i) { return data[i]; }
};
Some support for this has been added for containers in C++23: Multidimensional subscript operator
Although the syntax used is v[x, y, z] rather than v[x][y][z].
You could operator[] and make it return a pointer to the respective row or column of the matrix.
Because pointers support subscripting by [ ], access by the 'double-square' notation [][] is possible then.
Other answers tell you how you can do it, but...
Don’t do that.
Instead, overload operator ().
int & operator () ( size_t row, size_t col )
{
// maybe validate `row` and `col` first?
return arr[row * cols + col];
}
Why not?
The C++ FAQ explains it all. Straight from the horses’ mouths.
How do I create a subscript operator for a Matrix class?
Why shouldn’t my Matrix class’s interface look like an array-of-array?
I still don’t get it. Why shouldn’t my Matrix class’s interface look like an array-of-array?
tl;dr : encapsulation and safety

How to access to subsequence of a valarray considering it as a 2D matrix in C++

I'm learning C++, so please be patient with me.
I have a std::valarray in which there are double elements and I consider it as a 2D matrix.
class Matrix {
valarray<double> elems;
int r, c;
public:
/* type? operator[](int r) { return ? } */
//...
}
I want to overload the operator[], so that I can get a row of the matrix, and after that, I want have the m[r][c] access operator.
Is there any way to get a row, as a sequence of double using std::slice in the valarray, so that if I change a value, it is changed also in the matrix?
I've read this definition in valarray:
std::slice_array<T> operator[]( std::slice slicearr );
My operator[] must have std::slice_array<double>& as returned type?
Thanks.
I don't think std::slice and std::slice_array is what you're looking for, especially the latter is nothing but a helper type with a very limited public interface. You can instead return an proxy object. Here's a possible example of how to implement that.
class Matrix {
/* ... */
class RowProxy {
public:
RowProxy(std::valarray<double>& elems, int c, int row) :
elems(elems), c(c), row(row) {}
double& operator[](int j)
{
return elems[row*c + j];
}
private:
std::valarray<double>& elems;
int row;
int c;
};
RowProxy operator[](int i)
{
return RowProxy(elems, c, i);
}
};
This way, you can access the data with two operator[].
Matrix m(2, 4); // Assuming the ctor initializes elemens with row*column
m[0][0] = 1.234;
m[1][0] = 2.234;
m[1][3] = -52.023;
Note that both Matrix and RowProxy are missing overloads and proper handling for const-ness, and variable names are poor. Also, you might want to think about an out-of-bounds error handling strategy. But it may serve as a starting point for your implementation.

Efficient way to access submatrices with single index

I'm trying to perform a strided access to a submatrix with a single index. I need this for a library I'm working on which uses expression templates. I have worked out the following class, where the access is performed by the overloaded operator[], see below:
template <class A, class Type>
class SubMatrixExpr
{
private:
int Rows_; // Rows of the SubMatrix
int Columns_; // Columns of the SubMatrix
int Rows_up_; // Rows of the original Matrix
int Columns_up_; // Columns of the original Matrix
int a_, c_; // Starting indices of the SubMatrix as evaluated in the original Matrix
int rowstep_, columnstep_; // Stride along rows and columns for the original matrix
A M_;
public:
SubMatrixExpr(A &M, int Rows_up, int Columns_up, int Rows, int Columns, int a, int rowstep, int c, int columnstep) :
a_(a), c_(c), M_(M),
Rows_(Rows),
Columns_(Columns),
Rows_up_(Rows_up), Columns_up_(Columns_up),
rowstep_(rowstep), columnstep_(columnstep) { }
inline const Type& operator[](const int i) const
{
const int LocalRow = i/Columns_;
const int LocalColumn = i%Columns_;
const int GlobalRow = a_+rowstep_*LocalRow;
const int GlobalColumn = c_+columnstep_*LocalColumn;
return M_[IDX2R(GlobalRow,GlobalColumn,Columns_up_)];
}
inline Type& operator[](const int i)
{
// Similar to above
}
};
where
#define IDX2R(i,j,N) (((i)*(N))+(j))
The overloaded operator[] works correctly, but is computationally very expensive.
Is there any way to better implement the overloaded operator[]?
Thanks a lot in advance.
The only way you may got speedup is if you now the size of the matrices and sub-matrices at compile time. Then using template / constexpr may speedup things. For example if the size is known to be a power of 2 at compile time, the compiler will be able to replace division by shift.

How to recognize the assignment operator?

I've got class with overloaded [] and I need to make it to recognize, when I try to set the values to the array. I assume, that I'll have to overload the operator =, nevertheless I don't know, how shall that whole stuff look like. Part of my code:
class Matrix {
public:
Matrix(int x, int y);
~Matrix(void);
Matrix& operator =(const Matrix &matrix); //ok...this is probably wrong...
class Proxy {
public:
Proxy(double* _array) : _array(_array) {
}
double &operator[](int index) const {
return _array[index];
}
private:
double* _array;
};
Proxy operator[](int index) const {
return Proxy(_arrayofarrays[index]);
}
Proxy operator[](int index) {
return Proxy(_arrayofarrays[index]);
}
int x, y;
double** _arrayofarrays;
};
So I just need to be able to recognize when I try to set Matrix matrix(3,3); matrix[0][0]=1;
Everything else works allright, so I assumed that it's not needed to paste the whole code
It looks like you want something which is not directly possible: operator[][].
You can emulate the behavior of this by using an intermediate class:
Since a Matrix class is typically indexed as [rows][columns], you can
have the first operator method return the corresponding Row object.
The row class can than overload the operator[] and return the corresponding
element.
Row& Matrix::operator[](int r);
double& Row::operator[](int c);
Now when you create your matrix object, you can index it as expected:
Matrix matrix(3,3);
matrix[0][0] = 1;
The last line is then equivalent to calling:
matrix.operator[](0).operator[](0) = 1;
To check for out-of-bounds indices, store the matrix sizes:
Proxy operator[](int index) {
assert(index < num_rows);
return Proxy(_arrayofarrays[index]);
}
double &operator[](int index) const {
assert(index < num_cols);
return _array[index];
}
As Aldo suggeested, Proxy can be passed the array length value in it's constructor:
Proxy(double* _array, int _length) : _array(_array), num_cols(_length){
}
As a general rule of thumb, if you're passing a raw array to a function, you will almost always want to pass the length of that array as well.
Your Proxy::operator[] overload is returning a double&. This double& is the object that will eventually be assigned to by client code, and you can't intercept that (at least not easily). Probably what you need to do is to check the index parameter in your operator[] overloads, and throw your own exception there rather than passing that index along to your internal arrays.

C++ overload operator [ ][ ]

I have class CMatrix, where is "double pointer" to array of values.
class CMatrix {
public:
int rows, cols;
int **arr;
};
I simply need to access the values of matrix by typing:
CMatrix x;
x[0][0] = 23;
I know how to do that using:
x(0,0) = 23;
But I really need to do that the other way. Can anyone help me with that?
At the end I did it this way...
class CMatrix {
public:
int rows, cols;
int **arr;
public:
int const* operator[]( int const y ) const
{
return &arr[0][y];
}
int* operator[]( int const y )
{
return &arr[0][y];
}
....
You cannot overload operator [][], but the common idiom here is to use a proxy class, i.e. overload operator [] on your Matrix class to return an instance of a different class which then has operator [] overloaded on it.
For example:
class CMatrix {
public:
class CRow {
friend class CMatrix;
public:
int& operator[](int col)
{
return parent.arr[row][col];
}
private:
CRow(CMatrix &parent_, int row_) :
parent(parent_),
row(row_)
{}
CMatrix& parent;
int row;
};
CRow operator[](int row)
{
return CRow(*this, row);
}
private:
int rows, cols;
int **arr;
};
There is no operator[][] in C++. However, you can overload operator[] to return another structure, and in that overload operator[] too to get the effect you want.
You can do it by overloading operator[] to return an int*, which is then indexed by the second application of []. Instead of int* you could also return another class representing a row, whose operator[] gives access to individual elements of the row.
Essentially, subsequent applications of operator[] work on the result of the previous application.
If you create a matrix using Standard Library containers, it's trivial:
class Matrix {
vector<vector<int>> data;
public:
vector<int>& operator[] (size_t i) { return data[i]; }
};
Some support for this has been added for containers in C++23: Multidimensional subscript operator
Although the syntax used is v[x, y, z] rather than v[x][y][z].
You could operator[] and make it return a pointer to the respective row or column of the matrix.
Because pointers support subscripting by [ ], access by the 'double-square' notation [][] is possible then.
Other answers tell you how you can do it, but...
Don’t do that.
Instead, overload operator ().
int & operator () ( size_t row, size_t col )
{
// maybe validate `row` and `col` first?
return arr[row * cols + col];
}
Why not?
The C++ FAQ explains it all. Straight from the horses’ mouths.
How do I create a subscript operator for a Matrix class?
Why shouldn’t my Matrix class’s interface look like an array-of-array?
I still don’t get it. Why shouldn’t my Matrix class’s interface look like an array-of-array?
tl;dr : encapsulation and safety