samedi 7 mars 2015

output incorrect matrix determinant


I have been writing a matrix class that calculates the determinant of any n x m input matrix using 1D dynamic array (single pointer), however I am getting incorrect outputs and I can not seem to see my problem, here is my determinant member function (the member function da_minorM calculates the minor matrices):



double dymatrix::da_det(dymatrix & arr , int o_O) //determinant
{
o_O = arr.columns;
double det(0);
dymatrix Minor (arr.rows-1,arr.columns-1);
for (int i = 1; i <=o_O; i++)
{
//cout<<"i "<<i<<endl;
if (o_O == 1)
{
det+=matrix[0];
}
Minor = da_minorM(arr,1,i);
cout<<"Minor "<<Minor<<endl;
cout<<"arr "<<arr.matrix[0]<<endl;
det += pow(-1,i-1)*matrix[i-1]*da_det(Minor, o_O-1);
cout<<"i "<<i<<endl;
}
if (rows !=columns)
{
cerr<<"SIZE DO NOT MATCH, YOU FAIL"<<endl; exit(1);
}
return det;
}


I have overloaded "( )" and "[ ]" operators here:



double & operator[](const int n)
{
if(n<0 || n>=rows*columns)
{
cout<<"Error: trying to access matrix element out of bounds"<<endl<<" n "<<n<<" size "<<rows*columns<<endl<<endl;
exit(1);
}
return matrix[n];

}

int index(int j, int i) const //This member function returns the position of each index.
{
if (j > 0 && j <=rows && i > 0 && i <=columns)
{
//cout<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl;
return (i-1)+(j-1)*columns;
}
else {cout<<"Error, out of range"<<endl<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl; exit (1);}
}
//Overloading "**()**" operator.
double & operator()(int j, int i) const {return matrix[index(j,i)];}


The "index" member function converts the location of elements in 2D matrix to 1D matrix location.


This is my da_minorM matrix member function which calculates the minor matrices not minors:



dymatrix dymatrix::da_minorM(dymatrix &arr, int c, int r) //This calculates the minor matrix of any given indices.
{
dymatrix new_matrix(arr.rows-1,arr.columns-1); //This creates a new matrix which has a dimension of the rows -1 and columns -1 of the input matrix.
int ic =0; //Initialise the counter;
for (int j = 1; j<=arr.rows; j++) //The loop starts from the first row.
{
if(j != r ) //The loop will continue only if the row number is not equal to the row number that the user wishes to delete.
{
for (int i = 1; i <=arr.columns; i++) //This goes through the column elements.
{
if (i != c) //Again the loop will continue only if the column number is not equal to the column number that the user wishes to delete.
{
new_matrix[ic] = arr(j,i); //After the conditions above, the residual matrix should be the minor matrix of any element based on users choice.
ic++; //This ic counter will go through the 1D array element by element, as "[]" operator was overloaded above.
}
}
}
}
return new_matrix; //This returns the matrix.
if(columns !=rows)
{
cerr<<"Row and Columns must have same dimention"<<endl;
}
}


I found my error, I misplaced rows and columns (silly me :D) at



Minor = da_minorM(arr,1,i);


where I defined



da_minorM(array, columns, rows)


so instead I should have



Minor = da_minorM(arr,i,1);


Thanks for all the help




Aucun commentaire:

Enregistrer un commentaire