determinant of matrix in c N*N dimension return process kill .I took from net can anyone help me out where exactly its making this issue and how to overcome this issue.As per my program i pass @dimensional matrix of order x=100;
-
Quote:
float determinant1(float **a,int n)
{
int i,j,j1,j2 ; // general loop and matrix subscripts
float det = 0 ; // init determinant
float **m = NULL ; // pointer to pointers to implement 2d
// square arrayif (n < 1) { } // error condition, should never get here else if (n == 1) { // should not get here det = a\[0\]\[0\] ; } else if (n == 2) { // basic 2X2 sub-matrix determinate // definition. When n==2, this ends the det = a\[0\]\[0\] \* a\[1\]\[1\] - a\[1\]\[0\] \* a\[0\]\[1\] ;// the recursion series } // recursion continues, solve next sub-matrix else { // solve the next minor by building a // sub matrix det = 0 ; // initialize determinant of sub-matrix // for each column in sub-matrix for (j1 = 0 ; j1 < n ; j1++) { // get space for the pointer list m = (float \*\*) malloc((n-1)\* sizeof(float \*)) ; for (i = 0 ; i < n-1 ; i++) m\[i\] = (float \*) malloc((n-1)\* sizeof(float)) ; // i\[0\]\[1\]\[2\]\[3\] first malloc // m -> + + + + space for 4 pointers // | | | | j second malloc // | | | +-> \_ \_ \_ \[0\] pointers to // | | +----> \_ \_ \_ \[1\] and memory for // | +-------> \_ a \_ \[2\] 4 doubles // +----------> \_ \_ \_ \[3\] // // a\[1\]\[2\] // build sub-matrix with minor elements excluded for (i = 1 ; i < n ; i++) { j2 = 0 ; // start at first sum-matrix column position // loop to copy source matrix less one column for (j = 0 ; j < n ; j++) { if (j == j1) continue ; // don't copy the minor column element m\[i-1\]\[j2\] = a\[i\]\[j\] ; // copy source element into new sub-matrix // i-1
-
Quote:
float determinant1(float **a,int n)
{
int i,j,j1,j2 ; // general loop and matrix subscripts
float det = 0 ; // init determinant
float **m = NULL ; // pointer to pointers to implement 2d
// square arrayif (n < 1) { } // error condition, should never get here else if (n == 1) { // should not get here det = a\[0\]\[0\] ; } else if (n == 2) { // basic 2X2 sub-matrix determinate // definition. When n==2, this ends the det = a\[0\]\[0\] \* a\[1\]\[1\] - a\[1\]\[0\] \* a\[0\]\[1\] ;// the recursion series } // recursion continues, solve next sub-matrix else { // solve the next minor by building a // sub matrix det = 0 ; // initialize determinant of sub-matrix // for each column in sub-matrix for (j1 = 0 ; j1 < n ; j1++) { // get space for the pointer list m = (float \*\*) malloc((n-1)\* sizeof(float \*)) ; for (i = 0 ; i < n-1 ; i++) m\[i\] = (float \*) malloc((n-1)\* sizeof(float)) ; // i\[0\]\[1\]\[2\]\[3\] first malloc // m -> + + + + space for 4 pointers // | | | | j second malloc // | | | +-> \_ \_ \_ \[0\] pointers to // | | +----> \_ \_ \_ \[1\] and memory for // | +-------> \_ a \_ \[2\] 4 doubles // +----------> \_ \_ \_ \[3\] // // a\[1\]\[2\] // build sub-matrix with minor elements excluded for (i = 1 ; i < n ; i++) { j2 = 0 ; // start at first sum-matrix column position // loop to copy source matrix less one column for (j = 0 ; j < n ; j++) { if (j == j1) continue ; // don't copy the minor column element m\[i-1\]\[j2\] = a\[i\]\[j\] ; // copy source element into new sub-matrix // i-1
Check your compiler documentation on how to increase the stacksize. Very likely your program dies due to stack overflow. This tends to happen when you have very deep levels of function calls, and 100 levels of recursion qualifies as very deep! Alternately, test your program with smaller matrices, say 10x10 rather than 100x100. If it works, it's a stack overflow issue. To prevent stack overflow, the only safe method is to avoid recursion, i. e. you should transform your program into one that doesn't require recursion. Otherwise you need to set a maximum recursion level and a set a stacksize that is sufficient to run the program with the maximum defined recursion level; you should exit the program with an error message, if the input would lead to a recursion level exceeding the predefined maximum.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Check your compiler documentation on how to increase the stacksize. Very likely your program dies due to stack overflow. This tends to happen when you have very deep levels of function calls, and 100 levels of recursion qualifies as very deep! Alternately, test your program with smaller matrices, say 10x10 rather than 100x100. If it works, it's a stack overflow issue. To prevent stack overflow, the only safe method is to avoid recursion, i. e. you should transform your program into one that doesn't require recursion. Otherwise you need to set a maximum recursion level and a set a stacksize that is sufficient to run the program with the maximum defined recursion level; you should exit the program with an error message, if the input would lead to a recursion level exceeding the predefined maximum.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
thank you for ur suggestion but can u kindly tell me how to set the maximum recrusion size or else the without recrusion process..
What I meant is, check programatically the depth of your recursion. E. g. define a static counter within your recursive function, and always compare it to the maximum allowed value, like this:
const int max_recursion_level = 20;
int foo(int n) {
int result = 0;
// define static recursion counter
static int recursion_level = 0;
// increment at the start of the function, before the first recursive call
++recursion_level;
// safety check
if (recursion_level > max_recursion_level)
throw("recursion level exceeded!");if (n<=0)
result = 0;
else if (n==1)
result = 1;
else
result = foo(n-1) + foo(n-2);// decrement counter at the end of the function, after the last recursive call
--recursion_level;return result;
}As for calculating the determinant without recursion, I am sure there are plenty of algorithms on the web if you search for it. I?ve found one in the first response to this question: http://cboard.cprogramming.com/cplusplus-programming/30001-determinant-calculation.html[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
What I meant is, check programatically the depth of your recursion. E. g. define a static counter within your recursive function, and always compare it to the maximum allowed value, like this:
const int max_recursion_level = 20;
int foo(int n) {
int result = 0;
// define static recursion counter
static int recursion_level = 0;
// increment at the start of the function, before the first recursive call
++recursion_level;
// safety check
if (recursion_level > max_recursion_level)
throw("recursion level exceeded!");if (n<=0)
result = 0;
else if (n==1)
result = 1;
else
result = foo(n-1) + foo(n-2);// decrement counter at the end of the function, after the last recursive call
--recursion_level;return result;
}As for calculating the determinant without recursion, I am sure there are plenty of algorithms on the web if you search for it. I?ve found one in the first response to this question: http://cboard.cprogramming.com/cplusplus-programming/30001-determinant-calculation.html[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Actually i am confused now do i need to call int foo() inside the determinant()? if at all i have to set counter can u please help me to implement in my code.
No, to simplify matters I programmed a recursive function, foo(). It is an implementation of the Fibonacci sequence. The only actual code of that function is the if/else/else-if block. In your case, the determinant function is the recursive function. To check the recursion level, you should do the same as I did for foo(): add a static counter variable that is initialized to 0, define a maximum recursion level somewhere accessible, and maintain that counter when you enter and leave your function. Note that all of my advice is based on the assumption that your problem is caused by a stack overflow. It is an educated guess of mine, but I cannot test it, because I don't know what compiler and what settings you are using. Have you verified this is the cause of your problem? If not, then following my advice may not solve it!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
No, to simplify matters I programmed a recursive function, foo(). It is an implementation of the Fibonacci sequence. The only actual code of that function is the if/else/else-if block. In your case, the determinant function is the recursive function. To check the recursion level, you should do the same as I did for foo(): add a static counter variable that is initialized to 0, define a maximum recursion level somewhere accessible, and maintain that counter when you enter and leave your function. Note that all of my advice is based on the assumption that your problem is caused by a stack overflow. It is an educated guess of mine, but I cannot test it, because I don't know what compiler and what settings you are using. Have you verified this is the cause of your problem? If not, then following my advice may not solve it!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
So nice of fren i set the recrusion_level in my program and while running its displaying exceed recrusion level as per printf i wrote and in infinte.any suggestion i really need to overcome this.
I think you do not understand: maybe you should just forget the recursion counter - it is only a safeguard, and will not help you calculate the determinant! Please check out the link to code I posted above. It does not use recursion and therefore does not need a counter. And it might even be slightly faster.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)