| | |
Transpose & Inverse Matrix, Matrix Determinant
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 3
Reputation:
Solved Threads: 0
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
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
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...
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<-- 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
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.
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.
•
•
Join Date: Jun 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
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...
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<-- 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.
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.
•
•
•
•
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?
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
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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.
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)
#include <vector> #include <stdio.h> #include <conio.h> using namespace std; class Matrix{ public: int rows, cols; double *cellData; // this is our 2 dimensional array double &cells(int r, int c){ // use to access our 2-d array return cellData[r*cols+c]; } Matrix(){ rows = 1; cols = 1; cellData = new double[rows*cols]; } ~Matrix(){ // clean up delete[] cellData; } Matrix operator=(Matrix a){ rows = a.rows; cols = a.cols; delete[] cellData; cellData = new double[rows*cols]; for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) cells(i, j) = a.cells(i, j); // copy matrix return *this; } void SetDimensions(int rows, int cols){ Matrix::rows = rows; Matrix::cols = cols; delete[] cellData; // delete old matrix cells cellData = new double[rows*cols]; cells(0, 0) = 0.; } Matrix sum(Matrix a){ Matrix ret; if(a.cols != cols || a.rows != rows) // must be same size matrices return ret; // return blank 1x1 matrix ret.SetDimensions(cols, rows); for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) ret.cells(i, j) = cells(i, j) + a.cells(i, j); // add 2 matrices together return ret; // return summation result } }; int main(){ Matrix x; x.SetDimensions(2, 2); printf("Blank matrix:\n"); printf("%f %f\n%f %f\n\n", x.cells(0, 0), x.cells(0, 1), x.cells(1, 0), x.cells(1, 1)); // use your readmatrix() function here... x.cells(0, 0) = 1.; x.cells(0, 1) = 2.; x.cells(1, 0) = 3.; x.cells(1, 1) = 4.; printf("Populated matrix:\n"); printf("%f %f\n%f %f\n\n", x.cells(0, 0), x.cells(0, 1), x.cells(1, 0), x.cells(1, 1)); x = x.sum(x); // add to self printf("Matrix after summation:\n"); printf("%f %f\n%f %f\n\n", x.cells(0, 0), x.cells(0, 1), x.cells(1, 0), x.cells(1, 1)); getch(); return 0; }
Last edited by ~s.o.s~; Jun 26th, 2007 at 2:02 pm. Reason: Fixed code tags.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Stack based Calculator
- Next Thread: Problem with switch statement
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






