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

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

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.

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 tags and proper indentation...

Edited 3 Years Ago by pritaeas: Fixed formatting.

ShawnCPlus's example will be of little use, since it doesn't even handle the general n x n case amongst other things...

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

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.

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.

#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;
  }

If this is any help... this is a determinant algorithm i did up in C++. It is known (i think) as a Recursive method. Its not the most efficient, but it works pretty well. It is used to find the Determinant of the invoking matrix (written as a separate class). remember to add #include <math> at the top of your code - its required for the pow() functionality. Hope its of some help to you.

double CDblMatrix::Det()
{
  double detValue = 0;
  int newRows = this->m_nRows - 1;
  int newCols = this->m_nCols - 1;
  int incRows = 0;
  int incCols = 0;
  double incRowCopy;
  double sign;
  double tempDet;
  double tempDetSign;
  double origValue;

  try{

    // If the matrix is a 2 x 2 matrix - Simply Determinant calculation
    if((this->m_nRows == 2) && (this->m_nCols == 2)){
      detValue = (this->m_ppData[0][0] * this->m_ppData[1][1]) - (this->m_ppData[1][0] * this->m_ppData[0][1]);
    }
    // If not a square matrix
    if(this->m_nRows != this->m_nCols){
      throw 100;
    }

    else{
 for(int i = 0; i < this->m_nRows; i++){

        CDblMatrix temp(newRows, newCols);

        for(int i1 = 0; i1 < this->m_nRows; i1++){
          for(int j2 = 0; j2 < this->m_nCols; j2++){

            if((i1 != i) && (j2 != 0) && (incRows <= (newRows-1)) && (incCols <= (newCols-1))){
              temp.m_ppData[incRows][incCols] = this->m_ppData[i1][j2];

              if((incCols <= (newCols-1)) && (incRows <= (temp.m_nRows - 1))){
                // Last element of row, not last row
                if((incCols == (newCols - 1)) && (incRows < (temp.m_nRows-1))){
                  incCols = 0;
                  incRows++;
                }
                // If last element
                else if((incCols == (newCols - 1)) && (incRows == (newRows - 1))){
                  break;
                }
                // Normal increment of column
                else if((incRows <= (temp.m_nRows-1)) && (incCols < (temp.m_nCols - 1))){
                  incCols++;
                }
              }
            }
          }
        }

 // Reset values
        incRows = 0;
        incCols = 0;

        // Find determinant of the new reduced matrix
        tempDet = temp.Det();

        // Copy int value to double - required for pow(double, double)
        incRowCopy = i;

        // Determine whether 1 or -1 to multipy by the determinant
        sign = pow(-1, incRowCopy + 2);

        // The correctly signed determinant
        tempDetSign = sign * tempDet;

        // Orignal value of the coordinte to multipy the determinant by
        origValue = this->m_ppData[i][0];
 // Finally total the values of determinants
        detValue = detValue + (tempDetSign * origValue);

      }
    }
  }

  catch(...){
    cout << "The matrix is not a square matrix! Can't find determinant of a non-square matrix." << endl;
    return 0;
    //throw;
  }


  return detValue;

}

(1) Yes, I'm well aware the post is quite old. The reason I posted the piece of code is so it might be of some help to other people doing assignments with similar requirements - ie. through google searches.

(2) regarding the pow()... yeah you got a point there. The pow() was just kinda what came to mind at the time. The code is certainly not the best it could be. I've since found other things that could do with improving. But cheers for the suggestion...

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
int  i ,j  ,x ,y ;
ifstream ifile;

ifile.open("asd.txt");

float **c;
ifile>>x>>y;
cout<<x<<" "<<y<<endl; 
c = new float *[x];

			for (i=0; i<y; i++)	
			{
			c[i] = new float [y];
			}



for (i=0 ; i<x; i++)
{
	for (j=0; j<y; j++)
		{
		ifile>>c[i][j];
		cout<<c[i][j]<<" ";
		}
		cout<<endl;
}		

float temp= 1.0;
float temp1= 1.0;

//*********************************************
for( j=0;j<y;j++)
{ 
	int l=0;
	for( i=l; i<x; i++)
	{ 
		temp = c[i][j];
		for(int k=0 ; k<y ; k++)
		{
			c[i][k] = c[i][k] / temp;
		}
	}
	l++;

	for(int j1=0;j1<x ;j1++)
	{ 
		 for(int i1=0  ;i1<y ; i1++)
		 {
			temp1 = c[i1][j1];
			c[i1][j1] = c[i1][j1] - temp1;
		 }
	}
}

//********************************************























/*float temp= 1.0;
	for (  i=0 ; i<x; i++)
	{
			temp= temp * c[i][i];
			for ( j=0; j<y ; j++)
			{
			c[i][j]= (  c[i][j]/c[i][i]  );
			
			}
			
			for ( k=(i+1) ; k<x; k++)
			{
				for ( z=i; z<x; z++)
				{
				c[k][z] =  (   c[k][z] - ( c[i][i]*c[k][i]  )    );
				}
			}

		}

*/

cout<<endl<<endl;
cout<<"det ="<<temp<<endl<<endl;
for (i=0; i<x; i++)
{
	for (j=0; j<y; j++)
	{
	cout<<c[i][j]<<" ";
	
	}
cout<<endl; 

}


return 0;
}

Edited 5 Years Ago by Nick Evan: Added CODE-tags

can you give me a code each of this equation using cramer's rule and having multidimensional array consist of rows and columns..
these are the equation:
1.3x+2y+z=1
2.x-2y+z=5
3.3x+2y-5z=6
can i get the code as early this night?i need it for our midterm exam
tomorrow.thank you so much...
god bless...

sunny - please use code tags.

loyd - no one is going to do your exam for you. Also, please don't "hijack" threads - that is, if your comment isn't directly useful for the original poster, you should start a new thread instead of replying to this one.

David

Assemvbly Deter for Matrix i think it will help for you (i upload this maybe is has in other post can't find )

a:      data8   16,19,54,28,0,0,44,76,61
        .body                       
        .explicit                   
        movl r16 = a;;             
       ld8 r17=[r16],8;; 
       ld8 r18=[r16],8;; 
   	 ld8 r19=[r16],8;;
	 ld8 r20=[r16],8;;
	 ld8 r21=[r16],8;; 
	 ld8 r22=[r16],8;; 
	 ld8 r23=[r16],8;; 
 	 ld8 r24=[r16],8;; 
       ld8 r25=[r16],8;; 
       

        mov r27=0;;
        mov r28=-1;;
        
       mul r30=r17,r21;;
        
       mov r4=0;;
       mov r30=0;;
       while1: add r4=1,r4;;
        add r30=r30,r21;;
        cmp.ne p1=r4,r17;;
        br.sptk while1;;
        mul r31=r30,r25;;/a
        mov r4=0;;
       mov r31=0;;
        while2: add r4=1,r4;;
        add r31=r31,r25;;
        cmp.ne p1=r4,r30;;
        br.sptk while2;;
        mul r32=r18,r22;;
        mov r4=0;;
        mov r32=0;;
        while3: add r4=1,r4;;
        add r32=r32,r22;;
        cmp.ne p1=r4,r18;;
        br.sptk while3;;
        mul r33=r32,r23;;/b
        mov r4=0;;
        mov r33=0;;
        while4: add r4=1,r4;;
        add r33=r33,r23;;
        cmp.ne p1=r4,r32;;
        br.sptk while4;;
        mul r34=r19,r20;;
        mov r4=0;;
        mov r34=0;;
        while5: add r4=1,r4;;
        add r34=r34,r20;;
        cmp.ne p1=r4,r19;;
        br.sptk while5;;
        mul r35=r34,r24;;/c
        mov r4=0;;
        mov r35=0;;
        while6: add r4=1,r4;;
        add r35=r35,r34;;
        cmp.ne p1=r4,r24;;
        br.sptk while6;;
        mul r36=r19,r21;;
        mov r4=0;;
        mov r36=0;;
        while7: add r4=1,r4;;
        add r36=r36,r19;;
        cmp.ne p1=r4,r21;;
        br.sptk while7;;
        mul r37=r36,r23;;/d 
        mov r4=0;;
        mov r37=0;;
        while8: add r4=1,r4;;
        add r37=r37,r36;;
        cmp.ne p1=r4,r23;;
        br.sptk while8;;
        mul r50=r37,-1;;  
        mov r4=0;;
        mov r50=0;;
        while9: add r4=1,r4;;
        add r50=r50,r28;;
        cmp.ne p1=r4,r37;;
        br.sptk while9;;
	mul r38=r17,r22;;
        mov r4=0;;
        mov r38=0;;
        while10: add r4=1,r4;;
        add r38=r38,r17;;
        cmp.ne p1=r4,r22;;
        br.sptk while10;;
        mul r39=r38,r24;;/e
        mov r4=0;;
        mov r39=0;;
        while11: add r4=1,r4;;
        add r39=r39,r38;;
        cmp.ne p1=r4,r24;;
        br.sptk while11;;
        mul r51=r39,-1;;
        mov r4=0;;
        mov r51=0;;
        while12: add r4=1,r4;;
        add r51=r51,r28;;
        cmp.ne p1=r4,r39;;
         br.sptk while12;;
        mul r40=r18,r20;;
        mov r4=0;;
        mov r40=0;;
        while13: add r4=1,r4;;
        add r40=r40,r18;;
        cmp.ne p1=r4,r20;;
        br.sptk while13;;
        mul r41=r40,r25;;/f
        mov r4=0;;
        mov r41=0;;
        while14: add r4=1,r4;;
        add r41=r41,r40;;
        cmp.ne p1=r4,r25;;
        br.sptk while14;;
        mul r52=r41,-1;;
        mov r4=0;;
        mov r52=0;;
        while15: add r4=1,r4;;
        add r52=r52,r28;;
        cmp.ne p1=r4,r41;;
        br.sptk while15;;

        add r42=r31,r33;;
        add r43=r42,r35;;
        add r44=r43,r50;;
	add r45=r44,r51;;
        add r46=r45,r52;;
        st8 [r27]=r46;;

       done:   mov r8 = 0;;        
        br.ret.sptk.many b0;;       
        .endp main

Edited 5 Years Ago by Nick Evan: Added CODE-tags, although I don't know why you post asm in the C++ forum

This article has been dead for over six months. Start a new discussion instead.