Transpose & Inverse Matrix, Matrix Determinant

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 3
Reputation: marufsiddiqui is an unknown quantity at this point 
Solved Threads: 0
marufsiddiqui marufsiddiqui is offline Offline
Newbie Poster

Transpose & Inverse Matrix, Matrix Determinant

 
0
  #1
Jun 24th, 2007
i need some help/idea in coding a matrix class capable of giving its transpose matrix, inverse matrix & also the determinant of the matrix

i am totally a newbie in c++; & learing all the way

i just got the thought that to find the determinant it would be recursive .
is it true that the matrix must b square matrix (mxm)? and 3x3 is the least matrix size?

plz gimme some help/idea how to implement those functions
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #2
Jun 24th, 2007
first of all... you will have to explain to me what do these operations you want to do mean, cause i'm a little rusted in terms of determinants and that stuff... (it's been a long time since i work with matrixes)... i guess with a double for loop will e enough...

second... yes... until where i have understoos, matrixes are bidimensional arrays with the exact same dimensions, and the minimm is 3...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #3
Jun 24th, 2007
There is a snippet in the cplusplus section that I wrote to solve a matrix using Cramer's rule that might help you a bit.

http://www.daniweb.com/code/snippet643.html
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 3
Reputation: marufsiddiqui is an unknown quantity at this point 
Solved Threads: 0
marufsiddiqui marufsiddiqui is offline Offline
Newbie Poster

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #4
Jun 25th, 2007
Nichito
Here's the whole prob. see if u can help me

Class name: Matrix
Variables: row, column, a 2D array to store the values.
Methods:
1. Matrix( ): Constructs a null matrix with dimension 1 x 1.
2. void SetDimension(int row, int column): Sets the dimensions of the matrix.
3. void ReadMatrix( ): Takes the values from the user (in row major order).
4. void ShowMatirx( ): Prints the matrix in the screen.
5. Matrix Add(Matrix b): Returns a matrix that is the sum of current matrix and
matrix b, without affecting the current matrix.
6. Matrix Mult(Matrix b): Returns a matrix that is the product of current matrix
and matrix b, without affecting the current matrix. If multiplication is not possible
then returns a 1 x 1 null matrix.
7. Matrix Mult(double b): Returns a matrix that is produced by multiplying each
element of the current matrix with b, without affecting the current matrix.
8. double Determinant( ): Returns the determinant of the matrix. (think recursive)
9. Matrix Inverse( ): Returns the inverse matrix of the matrix if possible. Otherwise
returns a 1 x 1 null matrix.
10. Matrix Transpose( ): Returns the transpose matrix of the matrix.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #5
Jun 25th, 2007
So it is homework. You better get started then.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #6
Jun 25th, 2007
ok... until you show me your code, i can show you my code... until then i can only explain you how it works...

you can do that with (as i said before) double for loops... the first to manage rows and te second to manage columns... you can play with that so it goes from up to down or right to left, or the way you want it to go... that should help with the inverted matrix... the trasposed should work the same way...

post your code so we can help you better...

Note: don't forget to use [code][/code] tags and proper indentation...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #7
Jun 25th, 2007
ShawnCPlus's example will be of little use, since it doesn't even handle the general n x n case amongst other things...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #8
Jun 25th, 2007
Originally Posted by iamthwee View Post
ShawnCPlus's example will be of little use, since it doesn't even handle the general n x n case amongst other things...
Thanks, I appreciate that
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Transpose & Inverse Matrix, Matrix Determinant

 
1
  #9
Jun 25th, 2007
Originally Posted by marufsiddiqui View Post
i just got the thought that to find the determinant it would be recursive .
Maybe. It depends on how you code the determinant function. You could just go with the method that walks across rows with submatrices, whatever they're called, or you could use row operations.

is it true that the matrix must b square matrix (mxm)?
For there to exist a determinant? Yes.

and 3x3 is the least matrix size?
What? The smallest you can have is a 0x0 matrix, or a 1x1 matrix, depending on what you consider to be a matrix. You can have a 0x5 matrix, too. Seriously. But they're only useful if you're multiplying them by or expecting to get zero-dimensional vectors, which is not useful. Maybe you'll want to disallow 0-dimensional matrices.

If you want to be able to compute a determinant, you need at least a 1x1 matrix. Maybe the determinant of a 0x0 matrix is defined or definable as 1; I don't know. It's up to you whether you want to allow 0xN or Nx0 matrices; that's a bit of an abstract notion.

plz gimme some help/idea how to implement those functions
Use the algorithms you already know. The process of converting algorithms you can do on paper into computer code is called programming. If you don't know how to do some of these things, look on Wikipedia.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Transpose & Inverse Matrix, Matrix Determinant

 
0
  #10
Jun 26th, 2007
Here's some code to help you get started with the C++ syntax for writing a matrix class. I've put the matrix sum function in for you - you'll have to write the other matrix functions.

This code compiles and runs in Dev-c++4.9.9.2. As Rashakil Fol said, you may want to disable Nx0 or 0xN matrices.

  1. #include <vector>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. using namespace std;
  5. class Matrix{
  6. public:
  7. int rows, cols;
  8. double *cellData; // this is our 2 dimensional array
  9.  
  10. double &cells(int r, int c){ // use to access our 2-d array
  11. return cellData[r*cols+c];
  12. }
  13.  
  14. Matrix(){
  15. rows = 1;
  16. cols = 1;
  17. cellData = new double[rows*cols];
  18. }
  19.  
  20. ~Matrix(){ // clean up
  21. delete[] cellData;
  22. }
  23. Matrix operator=(Matrix a){
  24. rows = a.rows;
  25. cols = a.cols;
  26.  
  27. delete[] cellData;
  28. cellData = new double[rows*cols];
  29.  
  30. for(int i = 0; i < rows; i++)
  31. for(int j = 0; j < cols; j++)
  32. cells(i, j) = a.cells(i, j); // copy matrix
  33.  
  34. return *this;
  35. }
  36.  
  37. void SetDimensions(int rows, int cols){
  38. Matrix::rows = rows;
  39. Matrix::cols = cols;
  40.  
  41. delete[] cellData; // delete old matrix cells
  42. cellData = new double[rows*cols];
  43. cells(0, 0) = 0.;
  44. }
  45.  
  46. Matrix sum(Matrix a){
  47. Matrix ret;
  48.  
  49. if(a.cols != cols || a.rows != rows) // must be same size matrices
  50. return ret; // return blank 1x1 matrix
  51.  
  52. ret.SetDimensions(cols, rows);
  53.  
  54. for(int i = 0; i < rows; i++)
  55. for(int j = 0; j < cols; j++)
  56. ret.cells(i, j) = cells(i, j) + a.cells(i, j); // add 2 matrices together
  57.  
  58. return ret; // return summation result
  59. }
  60. };
  61.  
  62. int main(){
  63. Matrix x;
  64. x.SetDimensions(2, 2);
  65.  
  66. printf("Blank matrix:\n");
  67. printf("%f %f\n%f %f\n\n", x.cells(0, 0), x.cells(0, 1), x.cells(1, 0), x.cells(1, 1));
  68.  
  69. // use your readmatrix() function here...
  70. x.cells(0, 0) = 1.;
  71. x.cells(0, 1) = 2.;
  72. x.cells(1, 0) = 3.;
  73. x.cells(1, 1) = 4.;
  74.  
  75. printf("Populated matrix:\n");
  76. printf("%f %f\n%f %f\n\n", x.cells(0, 0), x.cells(0, 1), x.cells(1, 0), x.cells(1, 1));
  77.  
  78. x = x.sum(x); // add to self
  79. printf("Matrix after summation:\n");
  80. printf("%f %f\n%f %f\n\n", x.cells(0, 0), x.cells(0, 1), x.cells(1, 0), x.cells(1, 1));
  81.  
  82. getch();
  83.  
  84. return 0;
  85. }
Last edited by ~s.o.s~; Jun 26th, 2007 at 2:02 pm. Reason: Fixed code tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC