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

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    [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?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you've implemented + and * for matrices, then you're done. If you haven't, then what have you implemented + and * for?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix multiplication
    By prico in forum C Programming
    Replies: 12
    Last Post: 02-11-2011, 07:47 PM
  2. matrix multiplication
    By hotdone in forum C Programming
    Replies: 4
    Last Post: 10-23-2010, 12:31 PM
  3. Matrix Multiplication
    By anirban in forum C Programming
    Replies: 13
    Last Post: 07-30-2009, 06:24 PM
  4. matrix multiplication using MPI C
    By cromologic in forum C Programming
    Replies: 0
    Last Post: 04-29-2008, 09:35 AM
  5. Matrix Multiplication again....
    By davidvoyage200 in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2002, 05:35 PM