0

Hi I'm implementing a matrix class in c++
I know that there are great libraries that do that like opencv but I need to do that myself.

For example if I implement the sum I can do like this

class Mat{
public:
    double* data;
    int rows,cols;

    Mat(int r,int c):rows(r),cols(c){
        data = new double[r*c];
    }
};

void Sum(Mat& A,Mat& B,Mat& C){
    for (int i = 0; i < A.rows*A.cols; ++i){
        C.data[i] = A.data[i]+B.data[i];
    }   
}

int main(){    
    //Allocate Matrices
    Mat A(300,300);
    Mat B(300,300);
    Mat C(300,300);

    //do the sum
    sum(A,B,C);
}

I would like to get something more readable like this but without losing efficiency

C = A + B

This way C is reallocated every time and I don't want that

Thank you for your time

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
Rosh
  • 53
  • 1
  • 5

1 Answers1

1

Delay the calculation.

class MatAccess {
    friend class Mat;
    friend class MatOpAdd;
    virtual double operator[](int index) const = 0;
};

class MatOpAdd: public MatAccess {
friend class Mat;
private:
    const MatAccess& left;
    const MatAccess& right;
    MatOpAdd(const MatAccess& left, const MatAccess& right):
        left(left), right(right) {}
    double operator[](int index) const {
        return left[index] + right[index];
    }
};

class Mat: public MatAccess{
public:
    double* data;
    int rows,cols;

    Mat(int r,int c):rows(r),cols(c){
        data = new double[r*c];
    }

    MatOpAdd operator +(const MatAccess& other) {
        return MatOpAdd(*this, other);
    }

    const Mat& operator = (const MatAccess& other) {
        for(int i = 0; i < rows*cols; ++i) {
            data[i] = other[i];
        }
        return *this;
    }
private:
    double operator[](int index) const {
        return data[index];
    }
    double& operator[](int index) {
        return data[index];
    }
};


int main(){    
    //Allocate Matrices
    Mat A(300,300);
    Mat B(300,300);
    Mat C(300,300);

    //do the sum
    C = A + B;
}

Now the '+' calculation will be done in the "operator="

Things I would change:

  • MatAccess should include the dimensions (rows,cols).
  • Mat adding constructors and operator= or make it not copyable
  • Mat::operator+ and Mat::operator= check for equal rows,col
  • delete memory when not used anymore or
  • use std::vector for simpler memory managment.

Created a bigger example here: https://gist.github.com/KoKuToru/1d23af4bbf0b2bc89893

KoKuToru
  • 4,055
  • 2
  • 20
  • 21
  • wow we came up with really similar answers at almost the same time :) I'm new to S0 what should I do in this kind of situation? – Rosh Jan 02 '15 at 12:25
  • 1
    There are two possible solutions, accept my answer or post your own answer and accept your own. Don't post your answert in your question .. – KoKuToru Jan 02 '15 at 12:26
  • It's important to think about more difficult expressions such as A + B + C or (A + B) * C if the goal is to be 100% flexible and efficient. For just this use case it suffices of course – stefan Jan 02 '15 at 14:23