473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help in finding determinant of a matrix

3
I read the previous messages , but was unable to comprehend them in terms of programming. Kindly, guide me through proper and orgranize way.
Nov 21 '06 #1
5 5809
sivadhas2006
142 100+
I read the previous messages , but was unable to comprehend them in terms of programming. Kindly, guide me through proper and orgranize way.
Hi,

Check this thread...

determinant of a matrix


Regards,
M.Sivadhas.
Nov 21 '06 #2
macklin01
145 100+
Hi,

Check this thread...

determinant of a matrix


Regards,
M.Sivadhas.
A determinant is defined recursively. So, you need to expand along a row and recursively call your algorithm. Something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int SignOfTerm( int i, int j )
  3.  if( i+j % 2 == 0 )
  4.  { return 1; }
  5.  return -1;
  6. }
  7.  
  8. Matrix CreateSmallerMatrix( Matrix& Input, int i, int j)
  9. {
  10.  Matrix Output( Input.Rows-1, Input.Cols-1 );
  11.  
  12.  for( int ii=0 ; ii < i ; ii++ )
  13.  {
  14.   for( int jj=0 ; jj < j ; jj++ )
  15.   { Output(ii,jj) = Input(ii,jj);  }
  16.   for( int jj=j+1 ; jj < Input.Cols ; jj++ )
  17.   { Output(ii,jj-1) = Input(ii,jj);  }
  18.  }
  19.  
  20.  for( int ii=i+1 ; ii < Input.Rows ; ii++ )
  21.  {
  22.   for( int jj=0 ; jj < j ; jj++ )
  23.   { Output(ii-1,jj) = Input(ii,jj);  }
  24.   for( int jj=j+1 ; jj < Input.Cols ; jj++ )
  25.   { Output(ii-1,jj-1) = Input(ii,jj);  }
  26.  }
  27.  return Output;
  28. }
  29.  
  30. double Determinant( Matrix& Input )
  31. {
  32.  if( Input.rows != Input.cols )
  33.  { cout << "You can only take determinants of square matrices!\n"; return 0; }
  34.  
  35.  if( Input.rows == 1 && Input.cols == 1 )
  36.  { return Input(0,0); }
  37.  
  38.  double output = 0.0;
  39.  for( int j=0; j < cols ; j++ )
  40.  {
  41.   Matrix Temp = SmallerMatrix( Input, 0, j);
  42.   output += ( SignOfTerm(0,j) * Determinant(Temp) );
  43.  }
  44.  return output;
  45. }
  46.  
You'll have to do your own matrix handling (preferably some sort of object with a good constructor and destructor); I just used a matrix-like code to illustrate the point: the determinant of an n x n matrix is broken down into a sum of determinants of (n-1) x (n-1) matrices. The determinant of a 1 x 1 matrix is defined. So, the algorithm just keeps decreasing until it hits a combination of 1 x 1 determinants. -- Paul
Nov 22 '06 #3
Aanz
3
A determinant is defined recursively. So, you need to expand along a row and recursively call your algorithm. Something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int SignOfTerm( int i, int j )
  3.  if( i+j % 2 == 0 )
  4.  { return 1; }
  5.  return -1;
  6. }
  7.  
  8. Matrix CreateSmallerMatrix( Matrix& Input, int i, int j)
  9. {
  10.  Matrix Output( Input.Rows-1, Input.Cols-1 );
  11.  
  12.  for( int ii=0 ; ii < i ; ii++ )
  13.  {
  14.   for( int jj=0 ; jj < j ; jj++ )
  15.   { Output(ii,jj) = Input(ii,jj);  }
  16.   for( int jj=j+1 ; jj < Input.Cols ; jj++ )
  17.   { Output(ii,jj-1) = Input(ii,jj);  }
  18.  }
  19.  
  20.  for( int ii=i+1 ; ii < Input.Rows ; ii++ )
  21.  {
  22.   for( int jj=0 ; jj < j ; jj++ )
  23.   { Output(ii-1,jj) = Input(ii,jj);  }
  24.   for( int jj=j+1 ; jj < Input.Cols ; jj++ )
  25.   { Output(ii-1,jj-1) = Input(ii,jj);  }
  26.  }
  27.  return Output;
  28. }
  29.  
  30. double Determinant( Matrix& Input )
  31. {
  32.  if( Input.rows != Input.cols )
  33.  { cout << "You can only take determinants of square matrices!\n"; return 0; }
  34.  
  35.  if( Input.rows == 1 && Input.cols == 1 )
  36.  { return Input(0,0); }
  37.  
  38.  double output = 0.0;
  39.  for( int j=0; j < cols ; j++ )
  40.  {
  41.   Matrix Temp = SmallerMatrix( Input, 0, j);
  42.   output += ( SignOfTerm(0,j) * Determinant(Temp) );
  43.  }
  44.  return output;
  45. }
  46.  
You'll have to do your own matrix handling (preferably some sort of object with a good constructor and destructor); I just used a matrix-like code to illustrate the point: the determinant of an n x n matrix is broken down into a sum of determinants of (n-1) x (n-1) matrices. The determinant of a 1 x 1 matrix is defined. So, the algorithm just keeps decreasing until it hits a combination of 1 x 1 determinants. -- Paul
k, since i have to use an array to find the determinant of a matrix, so then how should i start?
Nov 22 '06 #4
macklin01
145 100+
k, since i have to use an array to find the determinant of a matrix, so then how should i start?
Sorry to miss your response. my instant email notifications don't seem to be working. Are you still having difficulty?

You may not need to make any array class. (Although I'm surprised you didn't already have something implemented, since you're asking about functions that act on matrices/arrays.) Do something like this:

Expand|Select|Wrap|Line Numbers
  1. int ArraySize = 15; // (or whatever the size is, equal to rows * columns)
  2.  
  3. // allocate memory for a new temporary array. 
  4.  
  5. double* TemporaryArray;
  6. TemporaryArray = new double [ArraySize];
  7.  
  8. // Copy the necessary values into the temprary array here. 
  9. // Use a row-ordering form of some sort: ( a11, a12, ... a1n, a21, a22, ... , amn)
  10.  
  11. // run the determinant on this smaller array
  12.  
  13. double temp = Determinant( TemporaryArray , ArraySize );
  14.  
  15. // free the memory allocated to that temporary array
  16.  
  17. delete [] TemporaryArray; 
  18.  
So, you would temporarily allocate memory for the sub-forum, pass the memory address recursively, and then clean up after the temporary array is no longer needed. -- Paul
Dec 14 '06 #5
macklin01
145 100+
*edit*
Sorry for the double-post, but I only thought of this after the 5-minute edit window.

It had slipped my mind, but I have an open source SimpleArray class that could probably work for you. (Although you may need to fill it out with necessary copy constructors, etc.)

Check out SimpleArray add-on for EasyBMP. You'll need to download it as a part of the extensions package, but it's fully independent of EasyBMP. That should do well enough to get you started. Make sure to pass by reference, and not pass data copies with that class, though!!! -- Paul
*/edit*
Dec 14 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: greenflame | last post by:
I am working on an algorithm to put a 'matrix' in row echelon form, and eventually reduced row echelon form. My script only seems to work if there arent to many ones, zeros, or row(s) of zeros. I...
2
by: aruna | last post by:
i want a c program to find the determinant of matrix and inverse of matrix as two separate c programs . please help me
1
by: aruna | last post by:
iam not getting the proper output.so please help me /*to find determinant of matrix */ # include <stdio.h> # include <conio.h> # include <math.h> double det(double fArray,int nNum); void...
1
by: naren | last post by:
can anyone help with the code for determinant,cofactor and inverse of a matrix in c++ lang? i am not getting the logic.....
1
by: avdhoot | last post by:
Hi Everybody, I am doing some project in VB6 where I ahve to calculate the Determinant of the matrix. I have got a function MDETERM but not able to pass on the parameters of the matrix and get the...
14
by: James Stroud | last post by:
Hello All, I'm using numpy to calculate determinants of matrices that look like this (13x13):
2
by: pvsgangadhar | last post by:
I need the program in c-langauge to find the determinant of a matrix for specified no.or rows and columns.
29
by: curiously enough | last post by:
I am having trouble making this recursive C function work for square matrices of size greater than 3 rows and columns. #include<stdio.h> #include<conio.h> #include<math.h> int M; float...
1
by: haderika | last post by:
Hey, I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.