You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have found your code very useful for a project where i use simple Matrix, but i have found a little bug in the determinant calculation, i attach you the code solving this error:
double matDet(Matrix* M) {
Matrix* Copy;
Copy = matCopy(M);
double det, factor;
if (M->columns != M->rows) {
printf("Columns and rows must have same length");
return -1;
}
else if (M->columns ==2){
det = Copy->numbers[0][0]*Copy->numbers[1][1]-Copy->numbers[0][1]*Copy->numbers[1][0];
return det;
}
det = 0;
for (int i = 0; i < Copy->columns; i++) {
Matrix *Submat;
int ncol = Copy->columns-1;
double v = (double)malloc(sizeof(double)ncolncol);
double *cv = v;
unsigned int count;
for (int ii = 0; ii < Copy->columns; ii++){
for ( int jj = 0; jj < Copy->rows; jj++){
if (jj != 0 && ii != i){
*v = Copy->numbers[jj][ii];
v ++;
}
}
}
Submat = matCreator(ncol,ncol,cv);
det += matDet(Submat)*Copy->numbers[i][0]*pow(-1,i);
freee(Submat);
}
return det;
}
The text was updated successfully, but these errors were encountered:
thanks @miguemorales for catching an issue. Do you have an example matrix I can use to reproduce? I've slowly been adding tests as bugs arise. And are you interested in create a pr?
I have found your code very useful for a project where i use simple Matrix, but i have found a little bug in the determinant calculation, i attach you the code solving this error:
double matDet(Matrix* M) {
Matrix* Copy;
Copy = matCopy(M);
double det, factor;
if (M->columns != M->rows) {
printf("Columns and rows must have same length");
return -1;
}
else if (M->columns ==2){
det = Copy->numbers[0][0]*Copy->numbers[1][1]-Copy->numbers[0][1]*Copy->numbers[1][0];
return det;
}
det = 0;
for (int i = 0; i < Copy->columns; i++) {
Matrix *Submat;
int ncol = Copy->columns-1;
double v = (double)malloc(sizeof(double)ncolncol);
double *cv = v;
unsigned int count;
for (int ii = 0; ii < Copy->columns; ii++){
for ( int jj = 0; jj < Copy->rows; jj++){
if (jj != 0 && ii != i){
*v = Copy->numbers[jj][ii];
v ++;
}
}
}
Submat = matCreator(ncol,ncol,cv);
det += matDet(Submat)*Copy->numbers[i][0]*pow(-1,i);
freee(Submat);
}
}
The text was updated successfully, but these errors were encountered: