# [C++] Adding a Matrix with the multiplication of two other Matrix

• 05-13-2011
PyroBlast
[C++] Adding a Matrix with the multiplication of two other Matrix
Hello there guys.

I've a problem here.

I've a class with the operators =, + and * implemented.

Those operators are working well. The issue is related with a matrix equation.

What I need to do is:

[3x1](k+1) = [3x1] + [3x3] * [3x1](k) , Where those matrix are type Matrix.

A need that the matrix [3x1] holds the values from the result of that equation. This is a iterative method that I'm implementing.

Does anyone can help me out here?
• 05-13-2011
tabstop
If you've implemented + and * for matrices, then you're done. If you haven't, then what have you implemented + and * for?
• 05-13-2011
PyroBlast
Good point of view. I didn't have made myself clear.

What I mean is that, I've 3 equations:

For example:

X1(k+1)=(5-X2+X3)/4

X2(k+1)=(8-2*X1-X3)/-6

X3(k+1)=(4-X1-2*X2)/9

Then, I've a Matrix [3x1](k) = [ 0 0 0 ], which corresponds to [X1 X2 X3].

Then I've to do X1(k+1)=(5-(0)+(0))/4= 5/4=1.25

after that I use the 5/4 in the next equation, and the 0 from the matrix [3x1](k).

So, X2(k+1)=(8-2*(1.25)-0)/-6= ~0,91

and so on.

What I can't figure it out is how to do that. I think that I need to use for loops and the matrix [3x1](k) = [ 0 0 0 ] must save the current values, so that it can be used in the next computation, but I'm blocked. I'm seeing how to do that.
• 05-13-2011
tabstop
You need to pick a problem and stick with it: Either you are going to do this component-wise with three different equations or you are going to use operators * and +. If you intend to do component-wise manipulation, then you can throw away your class and make six variables (X, Y, Z, newX, newY, newZ) and write down your equations.
• 05-13-2011
PyroBlast
But the idea is to do the gauss-seidel method.

So I need the X updated to use right away in the next equation.

Do you understand what I mean?

Like, I ask the user to all that crap that will allow me to "build" the Matrix A, x and b (Ax=b).

Then I will transform those Matrix on a different order: X(updated=k+1) = Beta(=bi/aii) + (alfa Matrix *X(before=k) )

bi = right hand from the equation Ax=b;
aii = principal diagonal;
alfa Matrix=(aij)/aii;

But instead of doing right away the general purpose, I'm doing it for a particularly 3x3 matrix.

I make 3 Matrix with the costructor, X[3x1]=Beta[3x1] + Alfa[3x3]*X[3x1] ;

Here, both matrix X[3x1] are the same. So when I'm solving the first equation, the X Matrix will be updated and then the next equation will use the same X Matrix with the updated value, like in the example before.

Help meeeee lol
• 05-14-2011
tabstop
The statement that you've made three matrices is a bit worrisome, given that you have four in the problem. Unless by "matrix" you mean "vector", in which case you need to also make a "matrix" class to handle your Alfa, and then you can write operator*(matrix, vector) to finish it off.

(Or unless you aren't keeping old X around, in which case
Code:

`X = Beta + Alfa*X;`
and we're done.)
• 05-14-2011
VirtualAce
The answer is to overload the operators relative to your matrix class that you need in the equation. This has been stated more than once.