improved accuracy of the matrix determinant and matrix inversion functions (trac #431)

pull/13383/head
Vadim Pisarevsky 15 years ago
parent fa91788222
commit da02688429
  1. 148
      modules/core/src/lapack.cpp
  2. 8
      tests/cxcore/src/amath.cpp

@ -229,11 +229,13 @@ double determinant( const Mat& mat )
size_t step = mat.step; size_t step = mat.step;
const uchar* m = mat.data; const uchar* m = mat.data;
CV_Assert( mat.rows == mat.cols ); CV_Assert( mat.rows == mat.cols && (type == CV_32F || type == CV_64F));
#define Mf(y, x) ((float*)(m + y*step))[x] #define Mf(y, x) ((float*)(m + y*step))[x]
#define Md(y, x) ((double*)(m + y*step))[x] #define Md(y, x) ((double*)(m + y*step))[x]
if( rows <= 10 )
{
if( type == CV_32F ) if( type == CV_32F )
{ {
if( rows == 2 ) if( rows == 2 )
@ -244,30 +246,21 @@ double determinant( const Mat& mat )
result = Mf(0,0); result = Mf(0,0);
else else
{ {
integer i, n = rows, *ipiv, info=0; size_t bufSize = rows*rows*sizeof(float);
int bufSize = n*n*sizeof(float) + (n+1)*sizeof(ipiv[0]), sign=0;
AutoBuffer<uchar> buffer(bufSize); AutoBuffer<uchar> buffer(bufSize);
Mat a(rows, rows, CV_32F, (uchar*)buffer);
Mat a(n, n, CV_32F, (uchar*)buffer);
mat.copyTo(a); mat.copyTo(a);
ipiv = (integer*)cvAlignPtr(a.data + a.step*a.rows, sizeof(integer)); result = LU((float*)a.data, rows, 0, 0);
sgetrf_(&n, &n, (float*)a.data, &n, ipiv, &info); if( result )
assert(info >= 0);
if( info == 0 )
{ {
result = 1; for( int i = 0; i < rows; i++ )
for( i = 0; i < n; i++ ) result *= ((const float*)(a.data + a.step*i))[i];
{ result = 1./result;
result *= ((float*)a.data)[i*(n+1)];
sign ^= ipiv[i] != i+1;
}
result *= sign ? -1 : 1;
} }
} }
} }
else if( type == CV_64F ) else
{ {
if( rows == 2 ) if( rows == 2 )
result = det2(Md); result = det2(Md);
@ -277,14 +270,31 @@ double determinant( const Mat& mat )
result = Md(0,0); result = Md(0,0);
else else
{ {
integer i, n = rows, *ipiv, info=0; size_t bufSize = rows*rows*sizeof(double);
int bufSize = n*n*sizeof(double) + (n+1)*sizeof(ipiv[0]), sign=0; AutoBuffer<uchar> buffer(bufSize);
Mat a(rows, rows, CV_64F, (uchar*)buffer);
mat.copyTo(a);
result = LU((double*)a.data, rows, 0, 0);
if( result )
{
for( int i = 0; i < rows; i++ )
result *= ((const double*)(a.data + a.step*i))[i];
result = 1./result;
}
}
}
}
else
{
integer i, n = rows, *ipiv, info=0, sign = 0;
size_t bufSize = n*n*sizeof(double) + (n+1)*sizeof(ipiv[0]);
AutoBuffer<uchar> buffer(bufSize); AutoBuffer<uchar> buffer(bufSize);
Mat a(n, n, CV_64F, (uchar*)buffer); Mat a(n, n, CV_64F, (uchar*)buffer);
mat.copyTo(a); mat.convertTo(a, CV_64F);
ipiv = (integer*)cvAlignPtr(a.data + a.step*a.rows, sizeof(integer));
ipiv = (integer*)cvAlignPtr(a.data + a.step*a.rows, sizeof(integer));
dgetrf_(&n, &n, (double*)a.data, &n, ipiv, &info); dgetrf_(&n, &n, (double*)a.data, &n, ipiv, &info);
assert(info >= 0); assert(info >= 0);
@ -299,9 +309,6 @@ double determinant( const Mat& mat )
result *= sign ? -1 : 1; result *= sign ? -1 : 1;
} }
} }
}
else
CV_Error( CV_StsUnsupportedFormat, "" );
#undef Mf #undef Mf
#undef Md #undef Md
@ -341,8 +348,6 @@ double invert( const Mat& src, Mat& dst, int method )
CV_Assert( src.rows == src.cols && (type == CV_32F || type == CV_64F)); CV_Assert( src.rows == src.cols && (type == CV_32F || type == CV_64F));
dst.create( src.rows, src.cols, type ); dst.create( src.rows, src.cols, type );
if( method == DECOMP_LU || method == DECOMP_CHOLESKY )
{
if( src.rows <= 3 ) if( src.rows <= 3 )
{ {
uchar* srcdata = src.data; uchar* srcdata = src.data;
@ -470,64 +475,89 @@ double invert( const Mat& src, Mat& dst, int method )
return result; return result;
} }
src.copyTo(dst); if( dst.cols <= 10 )
integer n = dst.cols, lwork=-1, elem_size = CV_ELEM_SIZE(type),
lda = (int)(dst.step/elem_size), piv1=0, info=0;
if( method == DECOMP_LU )
{ {
int buf_size = (int)(n*sizeof(integer)); int n = dst.cols, elem_size = CV_ELEM_SIZE(type);
AutoBuffer<uchar> buf; AutoBuffer<uchar> buf(n*n*2*elem_size);
uchar* buffer; Mat src1(n, n, type, (uchar*)buf);
Mat dst1(n, n, type, dst.isContinuous() ? dst.data : src1.data + n*n*elem_size);
src.copyTo(src1);
setIdentity(dst1);
if( type == CV_32F ) if( method == DECOMP_LU && type == CV_32F )
{ result = LU((float*)src1.data, n, (float*)dst1.data, n);
real work1 = 0; else if( method == DECOMP_LU && type == CV_64F )
sgetri_(&n, (float*)dst.data, &lda, &piv1, &work1, &lwork, &info); result = LU((double*)src1.data, n, (double*)dst1.data, n);
lwork = cvRound(work1); else if( method == DECOMP_LU && type == CV_32F )
result = Cholesky((float*)src1.data, n, (float*)dst1.data, n);
else
result = Cholesky((double*)src1.data, n, (double*)dst1.data, n);
dst1.copyTo(dst);
result = std::abs(result);
} }
else else
{
integer n = dst.cols, lwork=-1, lda = n, piv1=0, info=0;
int t_size = type == CV_32F ? n*n*sizeof(double) : 0;
int buf_size = t_size;
AutoBuffer<uchar> buf;
if( method == DECOMP_LU )
{ {
double work1 = 0; double work1 = 0;
dgetri_(&n, (double*)dst.data, &lda, &piv1, &work1, &lwork, &info); dgetri_(&n, (double*)dst.data, &lda, &piv1, &work1, &lwork, &info);
lwork = cvRound(work1); lwork = cvRound(work1);
}
buf_size += (int)((lwork + 1)*elem_size); buf_size += (int)(n*sizeof(integer) + (lwork + 1)*sizeof(double));
buf.allocate(buf_size); buf.allocate(buf_size);
buffer = (uchar*)buf; uchar* buffer = (uchar*)buf;
Mat arr = dst;
if( type == CV_32F ) if( type == CV_32F )
{ {
sgetrf_(&n, &n, (float*)dst.data, &lda, (integer*)buffer, &info); arr = Mat(n, n, CV_64F, buffer);
if(info==0) src.convertTo(arr, CV_64F);
sgetri_(&n, (float*)dst.data, &lda, (integer*)buffer, buffer += t_size;
(float*)(buffer + n*sizeof(integer)), &lwork, &info);
} }
else else
{ {
dgetrf_(&n, &n, (double*)dst.data, &lda, (integer*)buffer, &info); src.copyTo(arr);
if(info==0) lda = arr.step/sizeof(double);
dgetri_(&n, (double*)dst.data, &lda, (integer*)buffer,
(double*)cvAlignPtr(buffer + n*sizeof(integer), elem_size), &lwork, &info);
} }
dgetrf_(&n, &n, (double*)arr.data, &lda, (integer*)buffer, &info);
if(info==0)
dgetri_(&n, (double*)arr.data, &lda, (integer*)buffer,
(double*)cvAlignPtr(buffer + n*sizeof(integer), sizeof(double)),
&lwork, &info);
if(info==0 && arr.data != dst.data)
arr.convertTo(dst, dst.type());
} }
else if( method == CV_CHOLESKY ) else if( method == DECOMP_CHOLESKY )
{ {
char L[] = {'L', '\0'}; Mat arr = dst;
if( type == CV_32F ) if( type == CV_32F )
{ {
spotrf_(L, &n, (float*)dst.data, &lda, &info); buf.allocate(buf_size);
if(info==0) arr = Mat(n, n, CV_64F, (uchar*)buf);
spotri_(L, &n, (float*)dst.data, &lda, &info); src.convertTo(arr, CV_64F);
} }
else else
{ {
dpotrf_(L, &n, (double*)dst.data, &lda, &info); src.copyTo(arr);
lda = arr.step/sizeof(double);
}
char L[] = {'L', '\0'};
dpotrf_(L, &n, (double*)arr.data, &lda, &info);
if(info==0)
dpotri_(L, &n, (double*)arr.data, &lda, &info);
if(info==0) if(info==0)
dpotri_(L, &n, (double*)dst.data, &lda, &info); {
completeSymm(arr);
if( arr.data != dst.data )
arr.convertTo(dst, dst.type());
} }
completeSymm(dst);
} }
result = info == 0; result = info == 0;
} }

@ -2420,7 +2420,7 @@ void CxCore_DetTest::prepare_to_validation( int )
*((CvScalar*)(test_mat[REF_OUTPUT][0].data.db)) = cvRealScalar(cvTsLU(&test_mat[TEMP][0], 0, 0)); *((CvScalar*)(test_mat[REF_OUTPUT][0].data.db)) = cvRealScalar(cvTsLU(&test_mat[TEMP][0], 0, 0));
} }
//CxCore_DetTest det_test; CxCore_DetTest det_test;
@ -2475,8 +2475,8 @@ void CxCore_InvertTest::get_test_array_types_and_sizes( int test_case_idx, CvSiz
if( bits & 4 ) if( bits & 4 )
{ {
sizes[INPUT][0] = cvSize(min_size, min_size); sizes[INPUT][0] = cvSize(min_size, min_size);
if( bits & 8 ) if( bits & 16 )
method = CV_SVD_SYM; method = CV_CHOLESKY;
} }
} }
else else
@ -2536,7 +2536,7 @@ int CxCore_InvertTest::prepare_test_case( int test_case_idx )
{ {
cvTsFloodWithZeros( &test_mat[INPUT][0], ts->get_rng() ); cvTsFloodWithZeros( &test_mat[INPUT][0], ts->get_rng() );
if( method == CV_SVD_SYM ) if( method == CV_CHOLESKY )
{ {
cvTsGEMM( &test_mat[INPUT][0], &test_mat[INPUT][0], 1., cvTsGEMM( &test_mat[INPUT][0], &test_mat[INPUT][0], 1.,
0, 0., &test_mat[TEMP][0], CV_GEMM_B_T ); 0, 0., &test_mat[TEMP][0], CV_GEMM_B_T );

Loading…
Cancel
Save