0

I am trying to implement Matrix Addition using expression templates. I am facing some trouble. Here is my matrix code:

#include<iostream>
#include<vector>
#include<cassert>

template <typename T>
class MatrixExpression {
  public:
    double operator[](size_t i) const { return static_cast<T const&>(*this)[i];}
    size_t size()const { return static_cast<T const&>(*this).size(); }
};
template<typename T>
class Matrix:public MatrixExpression<Matrix<T>>
{
    std::vector<std::vector<T>> mat;

    public:
    Matrix(std::size_t m, std::size_t n):mat(m,std::vector<T>(n)){}



    class Proxy
    {
        std::vector<T> vec;
        public:
        Proxy(std::vector<T> vec):vec(vec){ }

        T operator[](std::size_t i){ return vec[i];}
        //T &operator[](std::size_t i){ return vec[i];}
        std::size_t size() const{ return vec.size(); }
    };

    Proxy operator[](std::size_t i) const { return Proxy(mat[i]); }
    //Proxy &operator[](std::size_t i)      { return Proxy(mat[i]); }
    size_t size() const { return mat.size(); }

    Matrix(std::initializer_list<std::initializer_list<T>> lst)
    {
        int m=0,n=0;
        for(auto l:lst )
        {
            for(auto v:l)
            {
                n++;
            }
            m++;
        }
        int i=0,j=0;
        mat(m,std::vector<T>(n));
        for(auto l:lst )
        {
            for(auto v:l)
            {
                mat[i].push_back(v);
            }
            i++;
        }
    }


    Matrix(MatrixExpression<T> const& matx):mat(matx.size(),std::vector<T>(matx[0].size))
    {
        for(int i=0;i<matx.size();i++)
        {
            for(int j=0;j<matx[0].size();j++)
            {
                mat[i][j] = matx[i][j];
            }
        }
    }
};

template<typename T, typename X, typename Y>
class MatrixSum:public MatrixExpression<MatrixSum<T,X,Y>>
{
    X const& x;
    Y const& y;

    public:
    MatrixSum(X const& x1, Y const& y1):x(x1),y(y1){
        assert(x1.size()==y1.size());
        assert(x1[0].size()==y1[0].size());
    }

    class ProxySum
    {
        std::vector<T> vec1,vec2;
        public:
        ProxySum(std::vector<T> vec1,std::vector<T> vec2):vec1(vec1),vec2(vec2){ }

        T operator[](std::size_t i){ return vec1[i] + vec2[i];}
        //T &operator[](std::size_t i){ return vec1[i] + vec2[i];}
        std::size_t size() const{ return vec1[0].size(); }
    };
    ProxySum operator[](std::size_t i) const { return ProxySum(x[i],y[i]); }
    //ProxySum &operator[](std::size_t i){ return ProxySum(x[i],y[i]); }
    size_t size() const { return x.size(); } 

};

template<typename T,typename X,typename Y>
MatrixSum<T,X,Y>
operator+(X const& x, Y const& y)
{
    return MatrixSum<T,X,Y>(x,y);
}

I am getting two errors when using the Matrix class. First is the operator+ does not exist for Matrix (I used int from testing) even though I have implemented operator overloading for '+', and another error is in the second constructor for Matrix. It says that the call I have made for the constructor of mat variable is invalid.But vectors do have such constructor

Shantanu Shinde
  • 932
  • 3
  • 23
  • 48

1 Answers1

1

1) The following line is not a valid C++ syntax:

mat(m,std::vector<T>(n));

You should initialize mat member object in the constructor's initialization list, like this (assuming the outermost initializer_list is not empty):

Matrix(std::initializer_list<std::initializer_list<T>> lst) : mat(lst.size(), std::vector<T>(begin(lst)->size()))

2) As for the operator + you provided:

template<typename T,typename X,typename Y>
MatrixSum<T,X,Y>
operator+(X const& x, Y const& y)
{
    return MatrixSum<T,X,Y>(x,y);
}

Note that T template parameter is non-deducible, so the compiler cannot figure it out and thus cannot use this operator. The only way to call it would be like this:

matrix1.operator +<some_type>(matrix2);

...which is probably not what you want.

The right way would be to try and compute T at compile-time, based on X and Y types, using some metaprogramming.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • can you please give me some idea how to that. I am new to C++ templates. I don't have deep knowledge about it. – Shantanu Shinde Apr 02 '19 at 02:59
  • @Shantanu Shinde if T is the sum type, you could define `value_type` in the matrix, and then deduce the sum type as follows: decltype(std::decval() + std::decval()) – Igor R. Apr 02 '19 at 06:38