# Matrix Class & more...

This is a discussion on Matrix Class & more... within the C++ Programming forums, part of the General Programming Boards category; I have a matrix class that came with a book on C++, and it mentions that they left out several ...

1. ## 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.

4. ## Example for matrix.h

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

5. 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.

6. 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. 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:
| 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).

8. 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. 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).

10. 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. 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.