944,174 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 51095
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 24th, 2007
0

Transpose & Inverse Matrix, Matrix Determinant

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marufsiddiqui is offline Offline
3 posts
since Jun 2007
Jun 24th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Jun 24th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 25th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marufsiddiqui is offline Offline
3 posts
since Jun 2007
Jun 25th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

So it is homework. You better get started then.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 25th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Jun 25th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

ShawnCPlus's example will be of little use, since it doesn't even handle the general n x n case amongst other things...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 25th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 25th, 2007
1

Re: Transpose & Inverse Matrix, Matrix Determinant

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.

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

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

Quote ...
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.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is online now Online
2,480 posts
since Jun 2005
Jun 26th, 2007
0

Re: Transpose & Inverse Matrix, Matrix Determinant

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C1014 Fatal Error. Needs some help.
Next Thread in C++ Forum Timeline: Concerning a loop problem...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC