1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
45 namespace cv { namespace hal {
47 /****************************************************************************************\
48 * LU & Cholesky implementation for small matrices *
49 \****************************************************************************************/
51 template<typename _Tp> static inline int
52 LUImpl(_Tp* A, size_t astep, int m, _Tp* b, size_t bstep, int n, _Tp eps)
55 astep /= sizeof(A[0]);
56 bstep /= sizeof(b[0]);
58 for( i = 0; i < m; i++ )
62 for( j = i+1; j < m; j++ )
63 if( std::abs(A[j*astep + i]) > std::abs(A[k*astep + i]) )
66 if( std::abs(A[k*astep + i]) < eps )
71 for( j = i; j < m; j++ )
72 std::swap(A[i*astep + j], A[k*astep + j]);
74 for( j = 0; j < n; j++ )
75 std::swap(b[i*bstep + j], b[k*bstep + j]);
79 _Tp d = -1/A[i*astep + i];
81 for( j = i+1; j < m; j++ )
83 _Tp alpha = A[j*astep + i]*d;
85 for( k = i+1; k < m; k++ )
86 A[j*astep + k] += alpha*A[i*astep + k];
89 for( k = 0; k < n; k++ )
90 b[j*bstep + k] += alpha*b[i*bstep + k];
98 for( i = m-1; i >= 0; i-- )
99 for( j = 0; j < n; j++ )
101 _Tp s = b[i*bstep + j];
102 for( k = i+1; k < m; k++ )
103 s -= A[i*astep + k]*b[k*bstep + j];
104 b[i*bstep + j] = s*A[i*astep + i];
112 int LU32f(float* A, size_t astep, int m, float* b, size_t bstep, int n)
114 return LUImpl(A, astep, m, b, bstep, n, FLT_EPSILON*10);
118 int LU64f(double* A, size_t astep, int m, double* b, size_t bstep, int n)
120 return LUImpl(A, astep, m, b, bstep, n, DBL_EPSILON*100);
123 template<typename _Tp> static inline bool
124 CholImpl(_Tp* A, size_t astep, int m, _Tp* b, size_t bstep, int n)
129 astep /= sizeof(A[0]);
130 bstep /= sizeof(b[0]);
132 for( i = 0; i < m; i++ )
134 for( j = 0; j < i; j++ )
137 for( k = 0; k < j; k++ )
138 s -= L[i*astep + k]*L[j*astep + k];
139 L[i*astep + j] = (_Tp)(s*L[j*astep + j]);
142 for( k = 0; k < j; k++ )
144 double t = L[i*astep + k];
147 if( s < std::numeric_limits<_Tp>::epsilon() )
149 L[i*astep + i] = (_Tp)(1./std::sqrt(s));
162 [ L20 L21 L22 ] y2 b2
163 [ L30 L31 L32 L33 ] y3 b3
165 [ L00 L10 L20 L30 ] x0 y0
166 [ L11 L21 L31 ] x1 = y1
171 for( i = 0; i < m; i++ )
173 for( j = 0; j < n; j++ )
176 for( k = 0; k < i; k++ )
177 s -= L[i*astep + k]*b[k*bstep + j];
178 b[i*bstep + j] = (_Tp)(s*L[i*astep + i]);
182 for( i = m-1; i >= 0; i-- )
184 for( j = 0; j < n; j++ )
187 for( k = m-1; k > i; k-- )
188 s -= L[k*astep + i]*b[k*bstep + j];
189 b[i*bstep + j] = (_Tp)(s*L[i*astep + i]);
197 bool Cholesky32f(float* A, size_t astep, int m, float* b, size_t bstep, int n)
199 return CholImpl(A, astep, m, b, bstep, n);
202 bool Cholesky64f(double* A, size_t astep, int m, double* b, size_t bstep, int n)
204 return CholImpl(A, astep, m, b, bstep, n);
207 //=============================================================================
208 // for compatibility with 3.0
210 int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n)
212 return LUImpl(A, astep, m, b, bstep, n, FLT_EPSILON*10);
215 int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n)
217 return LUImpl(A, astep, m, b, bstep, n, DBL_EPSILON*100);
220 bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n)
222 return CholImpl(A, astep, m, b, bstep, n);
225 bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n)
227 return CholImpl(A, astep, m, b, bstep, n);