Thread: Matrix Class & more...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Matrix Class & more...

    I have a matrix class that came with a book on C++, and it mentions that they left out several commonly used features in matrix classes.

    There's a link to http://www.ProgramCPP.com/chapter15/15_2_1.html that has new source, but it doesn't work if I cut and paste, so somehow I have to merge it.

    The next 3 posts have the attatched three files.

    Can anyone please help me? I tried putting the new parts at the appropriate places, but this stumps me:

    matrix<itemType>:peration + (matrix<itemType> first, matrix<itemType> second)

    shouldn't that be
    matrix<itemType>:perator + (matrix<itemType> first, matrix<itemType> second)?

    Also, +, -, and * are what this update would add.

    If I try to compile the matrixex.cpp... then it doesn't work.

    Thanks for your time!
    Last edited by Trauts; 11-26-2002 at 09:42 AM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    MATRIX.H

    Here's the matrix header file

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    VECTOR.H

    here's the vector header file

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Example for matrix.h

    Here's an example usage for the matrix header...

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This probably won't help you a lot, but here's my matrix class.

    It didn't take me long when I wrote this, and I've left out a very important feature: inverse.

    You are correct that "operator" and not "operation" should be used.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Inverse? Would that make something like

    1 2 3
    4 5 6
    7 8 9

    into

    9 8 7
    6 5 4
    3 2 1

    ?

    If so, I could do it, but there's probably an easier way...

    Code:
    	for (x=0; x<10; x++)	//x is row, y is column
    	{
    		for (y=0; y<10; y++)
    		{
    			iMatrix1[x][y] = x*y;
    		}
    	}
    
    	for (x=9,a=0; x>=0; x--,a++)	//a is row, b is column
    	{
    		for (y=9,b=0; y>=0; y--,b++)		//this loop is first loop flipped diagonally
    		{
    			iMatrix2[a][b] = x*y;
    		}
    	}
    The first loop makes a matrix containing the multiplication table up to 9x9, the second reverses it like I mentioned before. You'd just have to change the x*y part

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Trauts
    Inverse? Would that make something like

    1 2 3
    4 5 6
    7 8 9

    into

    9 8 7
    6 5 4
    3 2 1

    ?
    No, it is another matrix and when multiplied with your original one gives a matrix full of 0:s except for a diagonal.

    Example:
    Code:
    Inv       Your matrix   Eye
    | A B C |   | 1 2 3 |   | 1 0 0 |
    | D E F | * | 4 5 6 | = | 0 1 0 |
    | G H I |   | 7 8 9 |   | 0 0 1 |
    You may have to differ between left and right inverse. The example above is a left inverse (the inverse stands on the left side).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Yea, finding the inverse of a matrix isn't a trivial matter. If you want to do it the easiest, most general way, an augmented matrix like this is created:

    a b c 1 0 0
    d e f 0 1 0
    g h i 0 0 1

    then row operations are performed until the left side is the identity matrix. Whatever is on the right side is the inverse of the original matrix.

    e.g.:

    1 0 0 A B C
    0 1 0 D E F
    0 0 1 G H I

    then
    A B C
    D E F
    G H I
    is the inverse of the original.

    As for that book... well, it REALLY sucks. Its the book used in my programming class at my school, but I never look at it because it sucks so much ass, IMHO. The author explains things in a poor order and a lot of stuff is not clearly explained at all.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I know how to invert matrices using pen and paper. I use someone else's code in C++ when I have to invert matrices in my programs.

    I don't know what the most efficient algorithm would look like (perhaps a contest).
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Sang-drax
    I know how to invert matrices using pen and paper. I use someone else's code in C++ when I have to invert matrices in my programs.

    I don't know what the most efficient algorithm would look like (perhaps a contest).
    Yes, I applied the same exact method in my matrix class to calculate inverses. I have a function which performs forward elimination and one that performs backwards elimination. It works well, I think, but it's only benefits are for when you are calculating the inverse of an nxn sized matrix. Seperate algorithms based on the size of the matrix could be made for faster calculations.

    I can post the code if anyone is interested, but its a bit clunky and uncommented...

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Thats why I don't rely on the book so much. I try help files and doing it on my own a lot. I'm getting a lot better now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM