I am working on my own matrix class in Qt. I know there is a QGenericMatrix class template, but I need to set the size at runtime, which is not possible with this one. Apart from that I see this as a nice project to revive my linear algebra knowledge by implementing this.
However, I have already been able to define the * operator (multiplication) as follows:
MyMatrix.h
public:
MyMatrix(int rows, int cols, double initValues=0.0); // constructor to create NxM matrix with N=col, M=rows
MyMatrix& operator*(double value); // multiply matrix with a double
private:
int rows;
int cols;
double **mat;
void initMatrix(int rows=1, int cols=1); // initialise the matrix, if no rows,cols are given it creates a "1x1 matrix"
MyMatrix.cpp
// constructor
MyMatrix::MyMatrix(int rows, int cols, double initValues)
{
initMatrix(rows, cols);
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
this->mat[i][j] = initValues;
}
}
}
// multiply each element in matrix by value
MyMatrix& MyMatrix::operator*(double value) {
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
this->mat[i][j] = this->mat[i][j] * value;
}
}
return *this;
}
// initialise all matrix cells
void MyMatrix::initMatrix(int rows, int cols)
{
this->rows = rows; // assign argument to member variable rows
this->cols = cols; // assign argument to member variable cols
this->mat = new double*[this->rows]; // initialise mat with list of doubles ptrs with length rows
for (int i = 0; i < this->rows; ++i) { // iterate over each row-element
this->mat[i] = new double[this->cols]; // initialise each rows-element with list of doubles with length cols
}
}
main.cpp
int rows = 2;
int cols = 3;
MyMatrix mat1(rows, cols, 1.0); // creates matrix with all elements 1.0
mat1 = mat1 * 3.0;
Note I extracted only the relevant parts, the class has grown already, so I guess posting the all three files completely would be more confusing.
So far so good. The above seems to do what it should.
Now, I want to be able to directly access each element in the matrix. Similar to how one can access elements in a QVector, like so:
Read an element:
double temp = mat1[2][2] // read the element in row=2, column=2
Write to an element:
double temp = 17;
mat1[2][2] = temp // set value of element in row=2, column=2 to given double temp (here 17).
But I do not know how to define this [][] operator. I tried the following definition analog to the multiplication with a double, and because I need to give the row and column. I thought I try:
MyMatrix.h
MyMatrix& operator[int c][int r](double value); // write
MyMatrix& operator[int c][int r](); // read
The implementation to overwrite/read the element in row r and column c which I have in mind should look like this:
MyMatrix.cpp
// write to element
MyMatrix& MyMatrix::operator[int r][int c](double value) {
this->mat[r][c] = value;
return *this;
}
// read to element
double& MyMatrix::operator[int r][int c]() {
return this->mat[r][c];
}
But that does not do the Trick.
Btw: Even before compiling QtCreator says:
/path/MyMatrixClass/mymatrix.cpp:60: error: expected ']'
/path/MyMatrixClass/mymatrix.cpp:60: to match this '['
/path/MyMatrixClass/mymatrix.cpp:60: error: expected '(' for function-style cast or type construction
/path/MyMatrixClass/mymatrix.cpp:61: error: use of undeclared identifier 'r'
/path/MyMatrixClass/mymatrix.cpp:61: error: use of undeclared identifier 'c'
I tried already searching a for these errors, but so far I could not find anything giving me a clue to what I want to achieve.
So, perhaps someone can give me a link on where to look for an example or some advice on how I can achieve what I want.
PS: Later I also want to be able to extract a certain row, or certain column, but I guess (=hope) that should be straight forward once I know how to handle the [][] operator the right way.
This is the first time I am really defining my own operators for a class. And I think I got the general idea from the * operator. (I have also + and - operators already working). However, until now I used Qt mostly for GUI building, simple data handling with QVectors, plotting spectra and alike. So, I guess I am just missing some basic Qt/c++ syntax.