Example #1
0
/*
*-----------------------------------------------------------------------------
*	funct:	mat_minor
*	desct:	find minor
*	given:	A = a square matrix,
*		i=row, j=col
*	retrn:	the minor of Aij
*-----------------------------------------------------------------------------
*/
double mat_minor(MATRIX A,int i,int j)
{
	MATRIX	S;
	double	result;

	if ( ( S = mat_creat(MatRow(A)-1, MatCol(A)-1, UNDEFINED) ) == NULL )
		return -1.0;
	mat_submat(A, i, j, S);
	result = mat_det( S );
	mat_free(S);

	return (result);

}
Example #2
0
//---------------------------------------------------------------------------
// Compute determinant of the matrix
NaReal      NaMatrix::det () const
{
#ifdef __matrix_h
    if(dim_rows() != dim_cols()){
        throw(na_size_mismatch);
    }

    NaReal  vDet = 0;
    MATRIX  mat = mat_creat(dim_rows(), dim_cols(), UNDEFINED);

    unsigned    i, j, n = dim_rows();
    for(i = 0; i < n; ++i){
        for(j = 0; j < n; ++j){
            mat[i][j] = get(i, j);
        }
    }

    vDet = mat_det(mat);
    mat_free(mat);
    return vDet;
#else
    throw(na_not_implemented);
#endif/* __matrix_h */

#if 0
    unsigned    i, j, k, n = dim_rows();


    // Predefined det()
    switch(n){
    case 1:
        vDet = get(0, 0);
        break;
    case 2:
        vDet = get(0, 0) * get (1, 1) - get(0, 1) * get(1, 0);
        break;
    default:{
        // Allocate space for minor matrix
        NaMatrix    mOfMinor(n - 1, n - 1);

        for(i = 0; i < n; ++i){
            // Compose minor
            for(j = 0; j < n - 1; ++j){
                for(k = 0; k < n - 1; ++k){
                    if(k < i){
                        mOfMinor[j][k] = get(1 + j, k);
                    }else if(k >= i){
                        mOfMinor[j][k] = get(1 + j, 1 + k);
                    }
                }
            }// minor composition
            //NaPrintLog("Minor(%d,%d):\n", i, n - 1);
            //mOfMinor.print_contents();

            // Summarize determinant
            if(i % 2){
                vDet -= get(0, i) * mOfMinor.det();
            }else{
                vDet += get(0, i) * mOfMinor.det();
            }
        }
        }break;
    }// end of switch for different cases
     
    //NaPrintLog("det=%g\n", vDet);
    return vDet;
#endif
}