![]() |
|
#1
|
|||
|
|||
Multiplying a Matrix???CPP / C++ / C Code:
CPP / C++ / C Code:
Does this seem like the right logic to anyone else? im just currious. I think im on the right path My logic is this: i have a matrix: matrix (1) 01 02 03 04 05 06 X matrix(2) 01 02 03 04 05 06 07 08 09 10 11 12 = matrix(3) [(1x1)+(1x5)+(1x9)] [(1x2)+(1x6)+(1x10)] [(1x3)+(1x7)+(1x11)] [(1x4)+(1x8)+(1x12)] [(2x1)+(2x5)+(2x9)] [(2x2)+(2x6)+(2x10)] [(2x3)+(2x7)+(2x11)] [(2x4)+(2x8)+(2x12)] |
|||
|
#2
|
|||
|
|||
Re: Multiplying a Matrix???You may want to review your operation signature a bit. You are returning a Matrix by value, but you have no return statement in your code. Depending on the size of the Matrix, it may be an issue. We generally expect to return a reference to the Matrix.
Your prototype declares a constant Matrix reference argument, but your impl loses the const modifier. Your "new matrix" is not declared before it is used, however you are using the variable name of the argument Matrix, which supposedly is going to be (and should be!) const. "rows" and "cols" are undeclared as well. We can assume that you mean matrix.rows and matrix.cols in your conditional statements. Usually, instead of: answer=tempAns+answer; ...we'd use: answer += tempAns; Noting that it is okay to use whitespace between operators, too. :davis: |
|
#3
|
|||
|
|||
Re: Multiplying a Matrix???Quote:
Yeah i hadnt worked on the return, i was just working out the basic logic of mulitplying two matrices together. But i stopped working on it last night, cause after so long you start hurting code vs helping it. lol. |
|
#4
|
|||
|
|||
Re: Multiplying a Matrix???Quote:
In general if a is an m x k matrix and b is a k x n then the product is an m x n matrix, and I show the calculations below. (You got the dimensions correct, but not the calculations.) Example a is 2 x 3 and b is 3 x 4 Code:
In the usual C matrix notation: Code:
Code:
Code:
Let c = the matrix product of a x b Then c will be a 2 x 4 matrix: Code:
Calculations for the elements of c are like this: Code:
. and, in general for each i, j: Code:
For example: Let arows be equal to the number of rows of aNotes: 1. The number of columns of a must be equal to the number of rows of b. 2. The number of rows of c is equal to the number of rows of a 3. The number of columns in c is equal to the number of columns in That's the math; here's the logic: Code:
For my example, I think the product goes like this: Code:
I suggest that you make a standalone program that you can use to test the matrix calculations and throroughly test it. Then you can try putting it your real application (class function definition, or whatever). My sequence for program development alway puts functionality first: Make sure you can get the right answer! Regards, Dave Last edited by davekw7x : 26-Jan-2007 at 10:07.
|
|
#5
|
|||
|
|||
Re: Multiplying a Matrix???woops your right, it should have been:
Quote:
|
|
#6
|
|||
|
|||
Re: Multiplying a Matrix???Quote:
I gave what I think is a good logic sequence for multiplying two matrices. Does your logic agree? It seems to me that for every element in the product matrix we need a loop that adds some stuff. That makes a total of three loops: If you had defined a function that would, somehow, allow multiplication of vectors consisting of a row of the left-hand matrix and a column of the right-hand matrix, then the innermost loop could have been hidden in it, but I don't see any way, in general, to multiply two matrices with two loops. Other programming languages may multiply matrices with no loops in the user program, but internally, they have loops. That's what you are doing with your Matrix class multiplication member function: making it easy for an application program to multiply matrix objects so that the user program doesn't have those messy and error-prone loops. Furthermore, the loop for going through the columns needs to be re-initialized for each row, doesn't it? In general I can't see any advantage in using something like this, as you do in your program: CPP / C++ / C Code:
I would rather use the following: CPP / C++ / C Code:
Or, in some cases the following may be appropriate: CPP / C++ / C Code:
However, since this loop, in this program, can be traversed multiple times, it won't work your way. It will either crash as you try to go beyond the end of the array or it will give the wrong answer. Regards, Dave |
|
#7
|
|||
|
|||
Re: Multiplying a Matrix???My matrix.cpp
CPP / C++ / C Code:
My Matrix.h CPP / C++ / C Code:
this is the driver my teacher wrote soo i didnt write it: Quote:
okay so i have nooo idea what i did wrong, but i think it has something to do with my Constructor and/or copy constructor. But im getting an error after i go to copy a constructor i believe. Im not worried about the addition, subtraction or multiplication right now, just want to get it working. Can anyone see where i screwed up? |
|
#8
|
|||
|
|||
Re: Multiplying a Matrix???After reviewing i think my problem actully is in the overloading in the "=" sign
any suggestions? |
|
#9
|
|||
|
|||
Re: Multiplying a Matrix???Quote:
Make the program tell you how far it got: CPP / C++ / C Code:
If it seems to be crashing here, then go into the operator function and look at the code. If you still don't see it, put some cout statements to see how far it gets there. What is the assignment operator supposed to do? What is it doing? Regards, Last edited by davekw7x : 27-Jan-2007 at 00:27.
|
|
#10
|
|||
|
|||
Re: Multiplying a Matrix???Yeah this leads me to think the problem lays in:
CPP / C++ / C Code:
So im thinking it needs to be more like: CPP / C++ / C Code:
|
Recent GIDBlog
Compress Your Web Site by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help... A small problem with allocating memory | Cristovao | C++ Forum | 4 | 17-Jan-2007 15:17 |
| i need help in C++ PLZ | its_me | C++ Forum | 3 | 04-Dec-2006 21:51 |
| how to initialize a char matrix matrix with empty elements? | swedenguy | C Programming Language | 4 | 22-Aug-2006 11:43 |
| debugging multi-dimensional vector/ matrix class | counterflow | C++ Forum | 1 | 15-Mar-2006 12:14 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 06:17 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The