Operator overloading for multiplication of two different classes

Could you, please, help me with operator overloading for multiplication of two different class pointers? I'd like to program this operation like in MATLAB, without extra subroutine.

Class definition:

class Vector
{
public:
double* V;
unsigned int Size;

Vector(unsigned int Elems, bool ZeroV = true)
{
Size = Elems;

if (Size < 1) return;

V = new double[Size];

if (ZeroV)
{
for (int i = 0; i < Size; i++)
V[i] = 0.0;
}
}

~Vector()
{
delete[] V;
}
};

class Matrix
{
public:
double** M;
unsigned int Rows;
unsigned int Cols;

Matrix(unsigned int Row, unsigned int Col, bool ZeroM = true)
{
Rows = Row; Cols = Col;

if (Rows < 1 || Cols < 1) return;

M = new double*[Rows];

for (int i = 0; i < Rows; i++)
M[i] = new double[Cols];

if (ZeroM)
{
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
M[i][j] = 0.0;
}
}

~Matrix()
{
for (int i = 0; i < Rows; i++)
delete[] M[i];

delete[] M;
}
};

My attempts to code this:

Vector* operator* (const Matrix *M)
{
Vector* Result = new Vector(M->Cols);

if (this->Size != M->Rows)
return Result;

for (int i = 0; i < M->Cols; i++)
for (int j = 0; j < M->Rows; j++)
Result->V[i] += this->V[j] * M->M[j][i];

return Result;
}

main function:

void main()
{
Vector* V = new Vector(5, true);
Matrix* M = new Matrix(5, 2, true);

Vector* Vnew = V * M;
}
It's possible to call C/C++ in Matlab https://www.mathworks.com/help/simulink/slref/coder.ceval.html

I assume vector here means column vector. If so, multiplying a column vector of length 5 by a 5x2 matrix isn't possible (V * M, [5x1]*[5x2]). Can we see the Matlab code?

Anyway,
(1) use int main, not void main
(2) Don't dynamically allocate the Vector or Matrix objects themselves.

Prefer to do:
1
2
3
4
Matrix M = Matrix(2, 3);
Vector V = Vector(3, true);

Vector V_result = M * V;

and go from there. Of course you'll need to change the operator* to take in a const reference to a matrix instead of a const pointer.

Edit: Wait a sec what's your actual issue? What errors are you getting, runtime or compile time?
Last edited on
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Vector
{
  public:
    double* V;
    unsigned int Size;
    
    Vector(unsigned int Elems, bool ZeroV = true)
    {
        Size = Elems;
        
        if (Size < 1) return;
        
        V = new double[Size];
        
        if (ZeroV)
        {
            for (unsigned int i = 0; i < Size; i++)
                V[i] = 0.0;
        }
    }
    
    ~Vector()
    {
        delete[] V;
    }
};

class Matrix
{
  public:
    double** M;
    unsigned int Rows;
    unsigned int Cols;
    
    Matrix(unsigned int Row, unsigned int Col, bool ZeroM = true)
    {
        Rows = Row; Cols = Col;
    
        if (Rows < 1 || Cols < 1) return;
       
        M = new double*[Rows];
        
        for (unsigned int i = 0; i < Rows; i++)
            M[i] = new double[Cols];
        
        if (ZeroM)
        {
            for (unsigned int i = 0; i < Rows; i++)
            for (unsigned int j = 0; j < Cols; j++)
                M[i][j] = 0.0;
    
        }
    }
    
    ~Matrix()
    {
        for (unsigned int i = 0; i < Rows; i++)
            delete[] M[i];
        
        delete[] M;
    }
};

Vector operator*(const Matrix& mat, const Vector& vec)
{   
    Vector result(mat.Rows, true);
    
    for (unsigned int i = 0; i < mat.Rows; i++)
        for (unsigned int j = 0; j < mat.Cols; j++)
            result.V[i] += mat.M[i][j] * vec.V[j];
        
    return result;
}

#include <iostream>
int main()
{
    Matrix M = Matrix(2, 3);
    M.M[0][0] = 1; // row 0, column 0
    M.M[0][1] = 2; 
    M.M[0][2] = 3; 
    M.M[1][0] = 4; // row 1, column 0
    M.M[1][1] = 5; 
    M.M[1][2] = 6;

    Vector V = Vector(3);
    V.V[0] =  7;
    V.V[1] =  9;
    V.V[2] = 11;
    
    Vector Vnew = M * V;;
    std::cout << Vnew.V[0] << '\n'
              << Vnew.V[1] << '\n';
    
}

Sloppy but here's an example of what I'm talking about, based on your code.
This might no be what you were looking for, so feel free to say so.

PS: To simplify construction, look into std::initializer_list
https://en.cppreference.com/w/cpp/utility/initializer_list
So that you could do something like Matrix m = { {1, 2}, {3, 4 } };
Last edited on
Registered users can post here. Sign in or register to post.