objective c - determinant algorithm of a 4x4 matrix -



objective c - determinant algorithm of a 4x4 matrix -

i pick first row , multiply each element cofactor, in cases method returning nan. example,

1 0 0 1 0 2 0 0 0 0 3 0 0 0 0 4

in case method returns nan.

does know did wrong?

getdet3 returns determinant of 3x3 matrix , works fine.

-(double) getdet4:(double[4][4])mat { double det = 0; double small[3][3]; int i, j, k; int i_ = 1, j_; ( i=0; i<4; i++ ){ if (mat[0][i] == 0) continue; // little matrix here ( j=0; j<3; j++ ){ j_ = 0; ( k=0; k<3; k++ ){ if ( == j_ ) j_++; small[j][k] = mat[i_][j_]; j_++; } i_++; } det += mat[0][i] * [self getdet3:small] * pow(-1, i+j); } homecoming det; }

well, there few mistakes in code.

1) initialization of i_ = 1 should done before j loop, otherwise maintain old value.

2) computation of pow(-1, i+j) should depend on i, since j has same value every time in look (namely, 3).

so, assuming getdet3 correct, error introduced i_ going out of bounds. whole, code should like:

-(double) getdet4:(double[4][4])mat { double det = 0; double small[3][3]; int i, j, k; int i_, j_; ( i=0; i<4; i++ ){ if (mat[0][i] == 0) continue; // little matrix here i_ = 1; ( j=0; j<3; j++ ){ j_ = 0; ( k=0; k<3; k++ ){ if ( == j_ ) j_++; small[j][k] = mat[i_][j_]; j_++; } i_++; } det += mat[0][i] * [self getdet3:small] * pow(-1, i); } homecoming det; }

objective-c algorithm matrix determinants

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -