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, 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"
46 #if defined _M_IX86 && defined _MSC_VER && _MSC_VER < 1700
47 #pragma float_control(precise, on)
53 int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n)
55 return hal::LU32f(A, astep, m, b, bstep, n);
58 int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n)
60 return hal::LU64f(A, astep, m, b, bstep, n);
63 bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n)
65 return hal::Cholesky32f(A, astep, m, b, bstep, n);
68 bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n)
70 return hal::Cholesky64f(A, astep, m, b, bstep, n);
73 template<typename _Tp> static inline _Tp hypot(_Tp a, _Tp b)
80 return a*std::sqrt(1 + b*b);
85 return b*std::sqrt(1 + a*a);
91 template<typename _Tp> bool
92 JacobiImpl_( _Tp* A, size_t astep, _Tp* W, _Tp* V, size_t vstep, int n, uchar* buf )
94 const _Tp eps = std::numeric_limits<_Tp>::epsilon();
97 astep /= sizeof(A[0]);
100 vstep /= sizeof(V[0]);
101 for( i = 0; i < n; i++ )
103 for( j = 0; j < n; j++ )
104 V[i*vstep + j] = (_Tp)0;
105 V[i*vstep + i] = (_Tp)1;
109 int iters, maxIters = n*n*30;
111 int* indR = (int*)alignPtr(buf, sizeof(int));
112 int* indC = indR + n;
115 for( k = 0; k < n; k++ )
117 W[k] = A[(astep + 1)*k];
120 for( m = k+1, mv = std::abs(A[astep*k + m]), i = k+2; i < n; i++ )
122 _Tp val = std::abs(A[astep*k+i]);
130 for( m = 0, mv = std::abs(A[k]), i = 1; i < k; i++ )
132 _Tp val = std::abs(A[astep*i+k]);
140 if( n > 1 ) for( iters = 0; iters < maxIters; iters++ )
142 // find index (k,l) of pivot p
143 for( k = 0, mv = std::abs(A[indR[0]]), i = 1; i < n-1; i++ )
145 _Tp val = std::abs(A[astep*i + indR[i]]);
150 for( i = 1; i < n; i++ )
152 _Tp val = std::abs(A[astep*indC[i] + i]);
154 mv = val, k = indC[i], l = i;
157 _Tp p = A[astep*k + l];
158 if( std::abs(p) <= eps )
160 _Tp y = (_Tp)((W[l] - W[k])*0.5);
161 _Tp t = std::abs(y) + hypot(p, y);
164 s = p/s; t = (p/t)*p;
175 #define rotate(v0, v1) a0 = v0, b0 = v1, v0 = a0*c - b0*s, v1 = a0*s + b0*c
177 // rotate rows and columns k and l
178 for( i = 0; i < k; i++ )
179 rotate(A[astep*i+k], A[astep*i+l]);
180 for( i = k+1; i < l; i++ )
181 rotate(A[astep*k+i], A[astep*i+l]);
182 for( i = l+1; i < n; i++ )
183 rotate(A[astep*k+i], A[astep*l+i]);
185 // rotate eigenvectors
187 for( i = 0; i < n; i++ )
188 rotate(V[vstep*k+i], V[vstep*l+i]);
192 for( j = 0; j < 2; j++ )
194 int idx = j == 0 ? k : l;
197 for( m = idx+1, mv = std::abs(A[astep*idx + m]), i = idx+2; i < n; i++ )
199 _Tp val = std::abs(A[astep*idx+i]);
207 for( m = 0, mv = std::abs(A[idx]), i = 1; i < idx; i++ )
209 _Tp val = std::abs(A[astep*i+idx]);
218 // sort eigenvalues & eigenvectors
219 for( k = 0; k < n-1; k++ )
222 for( i = k+1; i < n; i++ )
229 std::swap(W[m], W[k]);
231 for( i = 0; i < n; i++ )
232 std::swap(V[vstep*m + i], V[vstep*k + i]);
239 static bool Jacobi( float* S, size_t sstep, float* e, float* E, size_t estep, int n, uchar* buf )
241 return JacobiImpl_(S, sstep, e, E, estep, n, buf);
244 static bool Jacobi( double* S, size_t sstep, double* e, double* E, size_t estep, int n, uchar* buf )
246 return JacobiImpl_(S, sstep, e, E, estep, n, buf);
250 template<typename T> struct VBLAS
252 int dot(const T*, const T*, int, T*) const { return 0; }
253 int givens(T*, T*, int, T, T) const { return 0; }
254 int givensx(T*, T*, int, T, T, T*, T*) const { return 0; }
258 template<> inline int VBLAS<float>::dot(const float* a, const float* b, int n, float* result) const
263 __m128 s0 = _mm_setzero_ps(), s1 = _mm_setzero_ps();
264 for( ; k <= n - 8; k += 8 )
266 __m128 a0 = _mm_load_ps(a + k), a1 = _mm_load_ps(a + k + 4);
267 __m128 b0 = _mm_load_ps(b + k), b1 = _mm_load_ps(b + k + 4);
269 s0 = _mm_add_ps(s0, _mm_mul_ps(a0, b0));
270 s1 = _mm_add_ps(s1, _mm_mul_ps(a1, b1));
272 s0 = _mm_add_ps(s0, s1);
274 _mm_storeu_ps(sbuf, s0);
275 *result = sbuf[0] + sbuf[1] + sbuf[2] + sbuf[3];
280 template<> inline int VBLAS<float>::givens(float* a, float* b, int n, float c, float s) const
285 __m128 c4 = _mm_set1_ps(c), s4 = _mm_set1_ps(s);
286 for( ; k <= n - 4; k += 4 )
288 __m128 a0 = _mm_load_ps(a + k);
289 __m128 b0 = _mm_load_ps(b + k);
290 __m128 t0 = _mm_add_ps(_mm_mul_ps(a0, c4), _mm_mul_ps(b0, s4));
291 __m128 t1 = _mm_sub_ps(_mm_mul_ps(b0, c4), _mm_mul_ps(a0, s4));
292 _mm_store_ps(a + k, t0);
293 _mm_store_ps(b + k, t1);
299 template<> inline int VBLAS<float>::givensx(float* a, float* b, int n, float c, float s,
300 float* anorm, float* bnorm) const
305 __m128 c4 = _mm_set1_ps(c), s4 = _mm_set1_ps(s);
306 __m128 sa = _mm_setzero_ps(), sb = _mm_setzero_ps();
307 for( ; k <= n - 4; k += 4 )
309 __m128 a0 = _mm_load_ps(a + k);
310 __m128 b0 = _mm_load_ps(b + k);
311 __m128 t0 = _mm_add_ps(_mm_mul_ps(a0, c4), _mm_mul_ps(b0, s4));
312 __m128 t1 = _mm_sub_ps(_mm_mul_ps(b0, c4), _mm_mul_ps(a0, s4));
313 _mm_store_ps(a + k, t0);
314 _mm_store_ps(b + k, t1);
315 sa = _mm_add_ps(sa, _mm_mul_ps(t0, t0));
316 sb = _mm_add_ps(sb, _mm_mul_ps(t1, t1));
318 float abuf[4], bbuf[4];
319 _mm_storeu_ps(abuf, sa);
320 _mm_storeu_ps(bbuf, sb);
321 *anorm = abuf[0] + abuf[1] + abuf[2] + abuf[3];
322 *bnorm = bbuf[0] + bbuf[1] + bbuf[2] + bbuf[3];
327 template<> inline int VBLAS<double>::dot(const double* a, const double* b, int n, double* result) const
332 __m128d s0 = _mm_setzero_pd(), s1 = _mm_setzero_pd();
333 for( ; k <= n - 4; k += 4 )
335 __m128d a0 = _mm_load_pd(a + k), a1 = _mm_load_pd(a + k + 2);
336 __m128d b0 = _mm_load_pd(b + k), b1 = _mm_load_pd(b + k + 2);
338 s0 = _mm_add_pd(s0, _mm_mul_pd(a0, b0));
339 s1 = _mm_add_pd(s1, _mm_mul_pd(a1, b1));
341 s0 = _mm_add_pd(s0, s1);
343 _mm_storeu_pd(sbuf, s0);
344 *result = sbuf[0] + sbuf[1];
349 template<> inline int VBLAS<double>::givens(double* a, double* b, int n, double c, double s) const
352 __m128d c2 = _mm_set1_pd(c), s2 = _mm_set1_pd(s);
353 for( ; k <= n - 2; k += 2 )
355 __m128d a0 = _mm_load_pd(a + k);
356 __m128d b0 = _mm_load_pd(b + k);
357 __m128d t0 = _mm_add_pd(_mm_mul_pd(a0, c2), _mm_mul_pd(b0, s2));
358 __m128d t1 = _mm_sub_pd(_mm_mul_pd(b0, c2), _mm_mul_pd(a0, s2));
359 _mm_store_pd(a + k, t0);
360 _mm_store_pd(b + k, t1);
366 template<> inline int VBLAS<double>::givensx(double* a, double* b, int n, double c, double s,
367 double* anorm, double* bnorm) const
370 __m128d c2 = _mm_set1_pd(c), s2 = _mm_set1_pd(s);
371 __m128d sa = _mm_setzero_pd(), sb = _mm_setzero_pd();
372 for( ; k <= n - 2; k += 2 )
374 __m128d a0 = _mm_load_pd(a + k);
375 __m128d b0 = _mm_load_pd(b + k);
376 __m128d t0 = _mm_add_pd(_mm_mul_pd(a0, c2), _mm_mul_pd(b0, s2));
377 __m128d t1 = _mm_sub_pd(_mm_mul_pd(b0, c2), _mm_mul_pd(a0, s2));
378 _mm_store_pd(a + k, t0);
379 _mm_store_pd(b + k, t1);
380 sa = _mm_add_pd(sa, _mm_mul_pd(t0, t0));
381 sb = _mm_add_pd(sb, _mm_mul_pd(t1, t1));
383 double abuf[2], bbuf[2];
384 _mm_storeu_pd(abuf, sa);
385 _mm_storeu_pd(bbuf, sb);
386 *anorm = abuf[0] + abuf[1];
387 *bnorm = bbuf[0] + bbuf[1];
392 template<typename _Tp> void
393 JacobiSVDImpl_(_Tp* At, size_t astep, _Tp* _W, _Tp* Vt, size_t vstep,
394 int m, int n, int n1, double minval, _Tp eps)
397 AutoBuffer<double> Wbuf(n);
399 int i, j, k, iter, max_iter = std::max(m, 30);
402 astep /= sizeof(At[0]);
403 vstep /= sizeof(Vt[0]);
405 for( i = 0; i < n; i++ )
407 for( k = 0, sd = 0; k < m; k++ )
409 _Tp t = At[i*astep + k];
416 for( k = 0; k < n; k++ )
422 for( iter = 0; iter < max_iter; iter++ )
424 bool changed = false;
426 for( i = 0; i < n-1; i++ )
427 for( j = i+1; j < n; j++ )
429 _Tp *Ai = At + i*astep, *Aj = At + j*astep;
430 double a = W[i], p = 0, b = W[j];
432 for( k = 0; k < m; k++ )
433 p += (double)Ai[k]*Aj[k];
435 if( std::abs(p) <= eps*std::sqrt((double)a*b) )
439 double beta = a - b, gamma = hypot((double)p, beta);
442 double delta = (gamma - beta)*0.5;
443 s = (_Tp)std::sqrt(delta/gamma);
444 c = (_Tp)(p/(gamma*s*2));
448 c = (_Tp)std::sqrt((gamma + beta)/(gamma*2));
449 s = (_Tp)(p/(gamma*c*2));
453 for( k = 0; k < m; k++ )
455 _Tp t0 = c*Ai[k] + s*Aj[k];
456 _Tp t1 = -s*Ai[k] + c*Aj[k];
457 Ai[k] = t0; Aj[k] = t1;
459 a += (double)t0*t0; b += (double)t1*t1;
467 _Tp *Vi = Vt + i*vstep, *Vj = Vt + j*vstep;
468 k = vblas.givens(Vi, Vj, n, c, s);
472 _Tp t0 = c*Vi[k] + s*Vj[k];
473 _Tp t1 = -s*Vi[k] + c*Vj[k];
474 Vi[k] = t0; Vj[k] = t1;
482 for( i = 0; i < n; i++ )
484 for( k = 0, sd = 0; k < m; k++ )
486 _Tp t = At[i*astep + k];
489 W[i] = std::sqrt(sd);
492 for( i = 0; i < n-1; i++ )
495 for( k = i+1; k < n; k++ )
502 std::swap(W[i], W[j]);
505 for( k = 0; k < m; k++ )
506 std::swap(At[i*astep + k], At[j*astep + k]);
508 for( k = 0; k < n; k++ )
509 std::swap(Vt[i*vstep + k], Vt[j*vstep + k]);
514 for( i = 0; i < n; i++ )
521 for( i = 0; i < n1; i++ )
523 sd = i < n ? W[i] : 0;
525 for( int ii = 0; ii < 100 && sd <= minval; ii++ )
527 // if we got a zero singular value, then in order to get the corresponding left singular vector
528 // we generate a random vector, project it to the previously computed left singular vectors,
529 // subtract the projection and normalize the difference.
530 const _Tp val0 = (_Tp)(1./m);
531 for( k = 0; k < m; k++ )
533 _Tp val = (rng.next() & 256) != 0 ? val0 : -val0;
534 At[i*astep + k] = val;
536 for( iter = 0; iter < 2; iter++ )
538 for( j = 0; j < i; j++ )
541 for( k = 0; k < m; k++ )
542 sd += At[i*astep + k]*At[j*astep + k];
544 for( k = 0; k < m; k++ )
546 _Tp t = (_Tp)(At[i*astep + k] - sd*At[j*astep + k]);
550 asum = asum > eps*100 ? 1/asum : 0;
551 for( k = 0; k < m; k++ )
552 At[i*astep + k] *= asum;
556 for( k = 0; k < m; k++ )
558 _Tp t = At[i*astep + k];
564 s = (_Tp)(sd > minval ? 1/sd : 0.);
565 for( k = 0; k < m; k++ )
566 At[i*astep + k] *= s;
571 static void JacobiSVD(float* At, size_t astep, float* W, float* Vt, size_t vstep, int m, int n, int n1=-1)
573 JacobiSVDImpl_(At, astep, W, Vt, vstep, m, n, !Vt ? 0 : n1 < 0 ? n : n1, FLT_MIN, FLT_EPSILON*2);
576 static void JacobiSVD(double* At, size_t astep, double* W, double* Vt, size_t vstep, int m, int n, int n1=-1)
578 JacobiSVDImpl_(At, astep, W, Vt, vstep, m, n, !Vt ? 0 : n1 < 0 ? n : n1, DBL_MIN, DBL_EPSILON*10);
581 /* y[0:m,0:n] += diag(a[0:1,0:m]) * x[0:m,0:n] */
582 template<typename T1, typename T2, typename T3> static void
583 MatrAXPY( int m, int n, const T1* x, int dx,
584 const T2* a, int inca, T3* y, int dy )
587 for( i = 0; i < m; i++, x += dx, y += dy )
591 #if CV_ENABLE_UNROLLED
592 for(; j <= n - 4; j += 4 )
594 T3 t0 = (T3)(y[j] + s*x[j]);
595 T3 t1 = (T3)(y[j+1] + s*x[j+1]);
598 t0 = (T3)(y[j+2] + s*x[j+2]);
599 t1 = (T3)(y[j+3] + s*x[j+3]);
605 y[j] = (T3)(y[j] + s*x[j]);
609 template<typename T> static void
610 SVBkSbImpl_( int m, int n, const T* w, int incw,
611 const T* u, int ldu, bool uT,
612 const T* v, int ldv, bool vT,
613 const T* b, int ldb, int nb,
614 T* x, int ldx, double* buffer, T eps )
616 double threshold = 0;
617 int udelta0 = uT ? ldu : 1, udelta1 = uT ? 1 : ldu;
618 int vdelta0 = vT ? ldv : 1, vdelta1 = vT ? 1 : ldv;
619 int i, j, nm = std::min(m, n);
624 for( i = 0; i < n; i++ )
625 for( j = 0; j < nb; j++ )
628 for( i = 0; i < nm; i++ )
629 threshold += w[i*incw];
632 // v * inv(w) * uT * b
633 for( i = 0; i < nm; i++, u += udelta0, v += vdelta0 )
635 double wi = w[i*incw];
636 if( (double)std::abs(wi) <= threshold )
644 for( j = 0; j < m; j++ )
645 s += u[j*udelta1]*b[j*ldb];
650 for( j = 0; j < n; j++ )
651 x[j*ldx] = (T)(x[j*ldx] + s*v[j*vdelta1]);
657 for( j = 0; j < nb; j++ )
659 MatrAXPY( m, nb, b, ldb, u, udelta1, buffer, 0 );
660 for( j = 0; j < nb; j++ )
665 for( j = 0; j < nb; j++ )
666 buffer[j] = u[j*udelta1]*wi;
668 MatrAXPY( n, nb, buffer, 0, v, vdelta1, x, ldx );
674 SVBkSb( int m, int n, const float* w, size_t wstep,
675 const float* u, size_t ustep, bool uT,
676 const float* v, size_t vstep, bool vT,
677 const float* b, size_t bstep, int nb,
678 float* x, size_t xstep, uchar* buffer )
680 SVBkSbImpl_(m, n, w, wstep ? (int)(wstep/sizeof(w[0])) : 1,
681 u, (int)(ustep/sizeof(u[0])), uT,
682 v, (int)(vstep/sizeof(v[0])), vT,
683 b, (int)(bstep/sizeof(b[0])), nb,
684 x, (int)(xstep/sizeof(x[0])),
685 (double*)alignPtr(buffer, sizeof(double)), (float)(DBL_EPSILON*2) );
689 SVBkSb( int m, int n, const double* w, size_t wstep,
690 const double* u, size_t ustep, bool uT,
691 const double* v, size_t vstep, bool vT,
692 const double* b, size_t bstep, int nb,
693 double* x, size_t xstep, uchar* buffer )
695 SVBkSbImpl_(m, n, w, wstep ? (int)(wstep/sizeof(w[0])) : 1,
696 u, (int)(ustep/sizeof(u[0])), uT,
697 v, (int)(vstep/sizeof(v[0])), vT,
698 b, (int)(bstep/sizeof(b[0])), nb,
699 x, (int)(xstep/sizeof(x[0])),
700 (double*)alignPtr(buffer, sizeof(double)), DBL_EPSILON*2 );
705 /****************************************************************************************\
706 * Determinant of the matrix *
707 \****************************************************************************************/
709 #define det2(m) ((double)m(0,0)*m(1,1) - (double)m(0,1)*m(1,0))
710 #define det3(m) (m(0,0)*((double)m(1,1)*m(2,2) - (double)m(1,2)*m(2,1)) - \
711 m(0,1)*((double)m(1,0)*m(2,2) - (double)m(1,2)*m(2,0)) + \
712 m(0,2)*((double)m(1,0)*m(2,1) - (double)m(1,1)*m(2,0)))
714 double cv::determinant( InputArray _mat )
716 Mat mat = _mat.getMat();
718 int type = mat.type(), rows = mat.rows;
719 size_t step = mat.step;
720 const uchar* m = mat.ptr();
722 CV_Assert( !mat.empty() );
723 CV_Assert( mat.rows == mat.cols && (type == CV_32F || type == CV_64F));
725 #define Mf(y, x) ((float*)(m + y*step))[x]
726 #define Md(y, x) ((double*)(m + y*step))[x]
738 size_t bufSize = rows*rows*sizeof(float);
739 AutoBuffer<uchar> buffer(bufSize);
740 Mat a(rows, rows, CV_32F, (uchar*)buffer);
743 result = hal::LU32f(a.ptr<float>(), a.step, rows, 0, 0, 0);
746 for( int i = 0; i < rows; i++ )
747 result *= a.at<float>(i,i);
762 size_t bufSize = rows*rows*sizeof(double);
763 AutoBuffer<uchar> buffer(bufSize);
764 Mat a(rows, rows, CV_64F, (uchar*)buffer);
767 result = hal::LU64f(a.ptr<double>(), a.step, rows, 0, 0, 0);
770 for( int i = 0; i < rows; i++ )
771 result *= a.at<double>(i,i);
783 /****************************************************************************************\
784 * Inverse (or pseudo-inverse) of a matrix *
785 \****************************************************************************************/
787 #define Sf( y, x ) ((float*)(srcdata + y*srcstep))[x]
788 #define Sd( y, x ) ((double*)(srcdata + y*srcstep))[x]
789 #define Df( y, x ) ((float*)(dstdata + y*dststep))[x]
790 #define Dd( y, x ) ((double*)(dstdata + y*dststep))[x]
792 double cv::invert( InputArray _src, OutputArray _dst, int method )
795 Mat src = _src.getMat();
796 int type = src.type();
798 CV_Assert(type == CV_32F || type == CV_64F);
800 size_t esz = CV_ELEM_SIZE(type);
801 int m = src.rows, n = src.cols;
803 if( method == DECOMP_SVD )
805 int nm = std::min(m, n);
807 AutoBuffer<uchar> _buf((m*nm + nm + nm*n)*esz + sizeof(double));
808 uchar* buf = alignPtr((uchar*)_buf, (int)esz);
809 Mat u(m, nm, type, buf);
810 Mat w(nm, 1, type, u.ptr() + m*nm*esz);
811 Mat vt(nm, n, type, w.ptr() + nm*esz);
813 SVD::compute(src, w, u, vt);
814 SVD::backSubst(w, u, vt, Mat(), _dst);
815 return type == CV_32F ?
816 (w.ptr<float>()[0] >= FLT_EPSILON ?
817 w.ptr<float>()[n-1]/w.ptr<float>()[0] : 0) :
818 (w.ptr<double>()[0] >= DBL_EPSILON ?
819 w.ptr<double>()[n-1]/w.ptr<double>()[0] : 0);
824 if( method == DECOMP_EIG )
826 AutoBuffer<uchar> _buf((n*n*2 + n)*esz + sizeof(double));
827 uchar* buf = alignPtr((uchar*)_buf, (int)esz);
828 Mat u(n, n, type, buf);
829 Mat w(n, 1, type, u.ptr() + n*n*esz);
830 Mat vt(n, n, type, w.ptr() + n*esz);
834 SVD::backSubst(w, u, vt, Mat(), _dst);
835 return type == CV_32F ?
836 (w.ptr<float>()[0] >= FLT_EPSILON ?
837 w.ptr<float>()[n-1]/w.ptr<float>()[0] : 0) :
838 (w.ptr<double>()[0] >= DBL_EPSILON ?
839 w.ptr<double>()[n-1]/w.ptr<double>()[0] : 0);
842 CV_Assert( method == DECOMP_LU || method == DECOMP_CHOLESKY );
844 _dst.create( n, n, type );
845 Mat dst = _dst.getMat();
849 const uchar* srcdata = src.ptr();
850 uchar* dstdata = dst.ptr();
851 size_t srcstep = src.step;
852 size_t dststep = dst.step;
856 if( type == CV_32FC1 )
867 __m128 zero = _mm_setzero_ps();
868 __m128 t0 = _mm_loadl_pi(zero, (const __m64*)srcdata); //t0 = sf(0,0) sf(0,1)
869 __m128 t1 = _mm_loadh_pi(zero, (const __m64*)(srcdata+srcstep)); //t1 = sf(1,0) sf(1,1)
870 __m128 s0 = _mm_or_ps(t0, t1);
871 __m128 det =_mm_set1_ps((float)d);
872 s0 = _mm_mul_ps(s0, det);
873 static const uchar CV_DECL_ALIGNED(16) inv[16] = {0,0,0,0,0,0,0,0x80,0,0,0,0x80,0,0,0,0};
874 __m128 pattern = _mm_load_ps((const float*)inv);
875 s0 = _mm_xor_ps(s0, pattern);//==-1*s0
876 s0 = _mm_shuffle_ps(s0, s0, _MM_SHUFFLE(0,2,1,3));
877 _mm_storel_pi((__m64*)dstdata, s0);
878 _mm_storeh_pi((__m64*)((float*)(dstdata+dststep)), s0);
906 __m128d s0 = _mm_loadu_pd((const double*)srcdata); //s0 = sf(0,0) sf(0,1)
907 __m128d s1 = _mm_loadu_pd ((const double*)(srcdata+srcstep));//s1 = sf(1,0) sf(1,1)
908 __m128d sm = _mm_unpacklo_pd(s0, _mm_load_sd((const double*)(srcdata+srcstep)+1)); //sm = sf(0,0) sf(1,1) - main diagonal
909 __m128d ss = _mm_shuffle_pd(s0, s1, _MM_SHUFFLE2(0,1)); //ss = sf(0,1) sf(1,0) - secondary diagonal
910 __m128d det = _mm_load1_pd((const double*)&d);
911 sm = _mm_mul_pd(sm, det);
913 static const uchar CV_DECL_ALIGNED(16) inv[8] = {0,0,0,0,0,0,0,0x80};
914 __m128d pattern = _mm_load1_pd((double*)inv);
915 ss = _mm_mul_pd(ss, det);
916 ss = _mm_xor_pd(ss, pattern);//==-1*ss
918 s0 = _mm_shuffle_pd(sm, ss, _MM_SHUFFLE2(0,1));
919 s1 = _mm_shuffle_pd(ss, sm, _MM_SHUFFLE2(0,1));
920 _mm_storeu_pd((double*)dstdata, s0);
921 _mm_storeu_pd((double*)(dstdata+dststep), s1);
941 if( type == CV_32FC1 )
951 t[0] = (((double)Sf(1,1) * Sf(2,2) - (double)Sf(1,2) * Sf(2,1)) * d);
952 t[1] = (((double)Sf(0,2) * Sf(2,1) - (double)Sf(0,1) * Sf(2,2)) * d);
953 t[2] = (((double)Sf(0,1) * Sf(1,2) - (double)Sf(0,2) * Sf(1,1)) * d);
955 t[3] = (((double)Sf(1,2) * Sf(2,0) - (double)Sf(1,0) * Sf(2,2)) * d);
956 t[4] = (((double)Sf(0,0) * Sf(2,2) - (double)Sf(0,2) * Sf(2,0)) * d);
957 t[5] = (((double)Sf(0,2) * Sf(1,0) - (double)Sf(0,0) * Sf(1,2)) * d);
959 t[6] = (((double)Sf(1,0) * Sf(2,1) - (double)Sf(1,1) * Sf(2,0)) * d);
960 t[7] = (((double)Sf(0,1) * Sf(2,0) - (double)Sf(0,0) * Sf(2,1)) * d);
961 t[8] = (((double)Sf(0,0) * Sf(1,1) - (double)Sf(0,1) * Sf(1,0)) * d);
963 Df(0,0) = (float)t[0]; Df(0,1) = (float)t[1]; Df(0,2) = (float)t[2];
964 Df(1,0) = (float)t[3]; Df(1,1) = (float)t[4]; Df(1,2) = (float)t[5];
965 Df(2,0) = (float)t[6]; Df(2,1) = (float)t[7]; Df(2,2) = (float)t[8];
977 t[0] = (Sd(1,1) * Sd(2,2) - Sd(1,2) * Sd(2,1)) * d;
978 t[1] = (Sd(0,2) * Sd(2,1) - Sd(0,1) * Sd(2,2)) * d;
979 t[2] = (Sd(0,1) * Sd(1,2) - Sd(0,2) * Sd(1,1)) * d;
981 t[3] = (Sd(1,2) * Sd(2,0) - Sd(1,0) * Sd(2,2)) * d;
982 t[4] = (Sd(0,0) * Sd(2,2) - Sd(0,2) * Sd(2,0)) * d;
983 t[5] = (Sd(0,2) * Sd(1,0) - Sd(0,0) * Sd(1,2)) * d;
985 t[6] = (Sd(1,0) * Sd(2,1) - Sd(1,1) * Sd(2,0)) * d;
986 t[7] = (Sd(0,1) * Sd(2,0) - Sd(0,0) * Sd(2,1)) * d;
987 t[8] = (Sd(0,0) * Sd(1,1) - Sd(0,1) * Sd(1,0)) * d;
989 Dd(0,0) = t[0]; Dd(0,1) = t[1]; Dd(0,2) = t[2];
990 Dd(1,0) = t[3]; Dd(1,1) = t[4]; Dd(1,2) = t[5];
991 Dd(2,0) = t[6]; Dd(2,1) = t[7]; Dd(2,2) = t[8];
999 if( type == CV_32FC1 )
1005 Df(0,0) = (float)(1./d);
1023 int elem_size = CV_ELEM_SIZE(type);
1024 AutoBuffer<uchar> buf(n*n*elem_size);
1025 Mat src1(n, n, type, (uchar*)buf);
1029 if( method == DECOMP_LU && type == CV_32F )
1030 result = hal::LU32f(src1.ptr<float>(), src1.step, n, dst.ptr<float>(), dst.step, n) != 0;
1031 else if( method == DECOMP_LU && type == CV_64F )
1032 result = hal::LU64f(src1.ptr<double>(), src1.step, n, dst.ptr<double>(), dst.step, n) != 0;
1033 else if( method == DECOMP_CHOLESKY && type == CV_32F )
1034 result = hal::Cholesky32f(src1.ptr<float>(), src1.step, n, dst.ptr<float>(), dst.step, n);
1036 result = hal::Cholesky64f(src1.ptr<double>(), src1.step, n, dst.ptr<double>(), dst.step, n);
1046 /****************************************************************************************\
1047 * Solving a linear system *
1048 \****************************************************************************************/
1050 bool cv::solve( InputArray _src, InputArray _src2arg, OutputArray _dst, int method )
1053 Mat src = _src.getMat(), _src2 = _src2arg.getMat();
1054 int type = src.type();
1055 bool is_normal = (method & DECOMP_NORMAL) != 0;
1057 CV_Assert( type == _src2.type() && (type == CV_32F || type == CV_64F) );
1059 method &= ~DECOMP_NORMAL;
1060 CV_Assert( (method != DECOMP_LU && method != DECOMP_CHOLESKY) ||
1061 is_normal || src.rows == src.cols );
1063 // check case of a single equation and small matrix
1064 if( (method == DECOMP_LU || method == DECOMP_CHOLESKY) && !is_normal &&
1065 src.rows <= 3 && src.rows == src.cols && _src2.cols == 1 )
1067 _dst.create( src.cols, _src2.cols, src.type() );
1068 Mat dst = _dst.getMat();
1070 #define bf(y) ((float*)(bdata + y*src2step))[0]
1071 #define bd(y) ((double*)(bdata + y*src2step))[0]
1073 const uchar* srcdata = src.ptr();
1074 const uchar* bdata = _src2.ptr();
1075 uchar* dstdata = dst.ptr();
1076 size_t srcstep = src.step;
1077 size_t src2step = _src2.step;
1078 size_t dststep = dst.step;
1082 if( type == CV_32FC1 )
1084 double d = det2(Sf);
1089 t = (float)(((double)bf(0)*Sf(1,1) - (double)bf(1)*Sf(0,1))*d);
1090 Df(1,0) = (float)(((double)bf(1)*Sf(0,0) - (double)bf(0)*Sf(1,0))*d);
1098 double d = det2(Sd);
1103 t = (bd(0)*Sd(1,1) - bd(1)*Sd(0,1))*d;
1104 Dd(1,0) = (bd(1)*Sd(0,0) - bd(0)*Sd(1,0))*d;
1111 else if( src.rows == 3 )
1113 if( type == CV_32FC1 )
1115 double d = det3(Sf);
1122 (bf(0)*((double)Sf(1,1)*Sf(2,2) - (double)Sf(1,2)*Sf(2,1)) -
1123 Sf(0,1)*((double)bf(1)*Sf(2,2) - (double)Sf(1,2)*bf(2)) +
1124 Sf(0,2)*((double)bf(1)*Sf(2,1) - (double)Sf(1,1)*bf(2))));
1127 (Sf(0,0)*(double)(bf(1)*Sf(2,2) - (double)Sf(1,2)*bf(2)) -
1128 bf(0)*((double)Sf(1,0)*Sf(2,2) - (double)Sf(1,2)*Sf(2,0)) +
1129 Sf(0,2)*((double)Sf(1,0)*bf(2) - (double)bf(1)*Sf(2,0))));
1132 (Sf(0,0)*((double)Sf(1,1)*bf(2) - (double)bf(1)*Sf(2,1)) -
1133 Sf(0,1)*((double)Sf(1,0)*bf(2) - (double)bf(1)*Sf(2,0)) +
1134 bf(0)*((double)Sf(1,0)*Sf(2,1) - (double)Sf(1,1)*Sf(2,0))));
1145 double d = det3(Sd);
1152 t[0] = ((Sd(1,1) * Sd(2,2) - Sd(1,2) * Sd(2,1))*bd(0) +
1153 (Sd(0,2) * Sd(2,1) - Sd(0,1) * Sd(2,2))*bd(1) +
1154 (Sd(0,1) * Sd(1,2) - Sd(0,2) * Sd(1,1))*bd(2))*d;
1156 t[1] = ((Sd(1,2) * Sd(2,0) - Sd(1,0) * Sd(2,2))*bd(0) +
1157 (Sd(0,0) * Sd(2,2) - Sd(0,2) * Sd(2,0))*bd(1) +
1158 (Sd(0,2) * Sd(1,0) - Sd(0,0) * Sd(1,2))*bd(2))*d;
1160 t[2] = ((Sd(1,0) * Sd(2,1) - Sd(1,1) * Sd(2,0))*bd(0) +
1161 (Sd(0,1) * Sd(2,0) - Sd(0,0) * Sd(2,1))*bd(1) +
1162 (Sd(0,0) * Sd(1,1) - Sd(0,1) * Sd(1,0))*bd(2))*d;
1174 assert( src.rows == 1 );
1176 if( type == CV_32FC1 )
1180 Df(0,0) = (float)(bf(0)/d);
1188 Dd(0,0) = (bd(0)/d);
1196 if( method == DECOMP_QR )
1197 method = DECOMP_SVD;
1199 int m = src.rows, m_ = m, n = src.cols, nb = _src2.cols;
1200 size_t esz = CV_ELEM_SIZE(type), bufsize = 0;
1201 size_t vstep = alignSize(n*esz, 16);
1202 size_t astep = method == DECOMP_SVD && !is_normal ? alignSize(m*esz, 16) : vstep;
1203 AutoBuffer<uchar> buffer;
1206 _dst.create( src.cols, src2.cols, src.type() );
1207 Mat dst = _dst.getMat();
1210 CV_Error(CV_StsBadArg, "The function can not solve under-determined linear systems" );
1214 else if( is_normal )
1217 if( method == DECOMP_SVD )
1218 method = DECOMP_EIG;
1221 size_t asize = astep*(method == DECOMP_SVD || is_normal ? n : m);
1222 bufsize += asize + 32;
1225 bufsize += n*nb*esz;
1227 if( method == DECOMP_SVD || method == DECOMP_EIG )
1228 bufsize += n*5*esz + n*vstep + nb*sizeof(double) + 32;
1230 buffer.allocate(bufsize);
1231 uchar* ptr = alignPtr((uchar*)buffer, 16);
1233 Mat a(m_, n, type, ptr, astep);
1236 mulTransposed(src, a, true);
1237 else if( method != DECOMP_SVD )
1241 a = Mat(n, m_, type, ptr, astep);
1248 if( method == DECOMP_LU || method == DECOMP_CHOLESKY )
1254 if( method == DECOMP_LU || method == DECOMP_CHOLESKY )
1255 gemm( src, src2, 1, Mat(), 0, dst, GEMM_1_T );
1258 Mat tmp(n, nb, type, ptr);
1260 gemm( src, src2, 1, Mat(), 0, tmp, GEMM_1_T );
1265 if( method == DECOMP_LU )
1267 if( type == CV_32F )
1268 result = hal::LU32f(a.ptr<float>(), a.step, n, dst.ptr<float>(), dst.step, nb) != 0;
1270 result = hal::LU64f(a.ptr<double>(), a.step, n, dst.ptr<double>(), dst.step, nb) != 0;
1272 else if( method == DECOMP_CHOLESKY )
1274 if( type == CV_32F )
1275 result = hal::Cholesky32f(a.ptr<float>(), a.step, n, dst.ptr<float>(), dst.step, nb);
1277 result = hal::Cholesky64f(a.ptr<double>(), a.step, n, dst.ptr<double>(), dst.step, nb);
1281 ptr = alignPtr(ptr, 16);
1282 Mat v(n, n, type, ptr, vstep), w(n, 1, type, ptr + vstep*n), u;
1283 ptr += n*(vstep + esz);
1285 if( method == DECOMP_EIG )
1287 if( type == CV_32F )
1288 Jacobi(a.ptr<float>(), a.step, w.ptr<float>(), v.ptr<float>(), v.step, n, ptr);
1290 Jacobi(a.ptr<double>(), a.step, w.ptr<double>(), v.ptr<double>(), v.step, n, ptr);
1295 if( type == CV_32F )
1296 JacobiSVD(a.ptr<float>(), a.step, w.ptr<float>(), v.ptr<float>(), v.step, m_, n);
1298 JacobiSVD(a.ptr<double>(), a.step, w.ptr<double>(), v.ptr<double>(), v.step, m_, n);
1302 if( type == CV_32F )
1304 SVBkSb(m_, n, w.ptr<float>(), 0, u.ptr<float>(), u.step, true,
1305 v.ptr<float>(), v.step, true, src2.ptr<float>(),
1306 src2.step, nb, dst.ptr<float>(), dst.step, ptr);
1310 SVBkSb(m_, n, w.ptr<double>(), 0, u.ptr<double>(), u.step, true,
1311 v.ptr<double>(), v.step, true, src2.ptr<double>(),
1312 src2.step, nb, dst.ptr<double>(), dst.step, ptr);
1324 /////////////////// finding eigenvalues and eigenvectors of a symmetric matrix ///////////////
1326 bool cv::eigen( InputArray _src, OutputArray _evals, OutputArray _evects )
1328 Mat src = _src.getMat();
1329 int type = src.type();
1332 CV_Assert( src.rows == src.cols );
1333 CV_Assert (type == CV_32F || type == CV_64F);
1336 if( _evects.needed() )
1338 _evects.create(n, n, type);
1339 v = _evects.getMat();
1342 size_t elemSize = src.elemSize(), astep = alignSize(n*elemSize, 16);
1343 AutoBuffer<uchar> buf(n*astep + n*5*elemSize + 32);
1344 uchar* ptr = alignPtr((uchar*)buf, 16);
1345 Mat a(n, n, type, ptr, astep), w(n, 1, type, ptr + astep*n);
1346 ptr += astep*n + elemSize*n;
1348 bool ok = type == CV_32F ?
1349 Jacobi(a.ptr<float>(), a.step, w.ptr<float>(), v.ptr<float>(), v.step, n, ptr) :
1350 Jacobi(a.ptr<double>(), a.step, w.ptr<double>(), v.ptr<double>(), v.step, n, ptr);
1359 static void _SVDcompute( InputArray _aarr, OutputArray _w,
1360 OutputArray _u, OutputArray _vt, int flags )
1362 Mat src = _aarr.getMat();
1363 int m = src.rows, n = src.cols;
1364 int type = src.type();
1365 bool compute_uv = _u.needed() || _vt.needed();
1366 bool full_uv = (flags & SVD::FULL_UV) != 0;
1368 CV_Assert( type == CV_32F || type == CV_64F );
1370 if( flags & SVD::NO_UV )
1374 compute_uv = full_uv = false;
1384 int urows = full_uv ? m : n;
1385 size_t esz = src.elemSize(), astep = alignSize(m*esz, 16), vstep = alignSize(n*esz, 16);
1386 AutoBuffer<uchar> _buf(urows*astep + n*vstep + n*esz + 32);
1387 uchar* buf = alignPtr((uchar*)_buf, 16);
1388 Mat temp_a(n, m, type, buf, astep);
1389 Mat temp_w(n, 1, type, buf + urows*astep);
1390 Mat temp_u(urows, m, type, buf, astep), temp_v;
1393 temp_v = Mat(n, n, type, alignPtr(buf + urows*astep + n*esz, 16), vstep);
1396 temp_u = Scalar::all(0);
1399 transpose(src, temp_a);
1403 if( type == CV_32F )
1405 JacobiSVD(temp_a.ptr<float>(), temp_u.step, temp_w.ptr<float>(),
1406 temp_v.ptr<float>(), temp_v.step, m, n, compute_uv ? urows : 0);
1410 JacobiSVD(temp_a.ptr<double>(), temp_u.step, temp_w.ptr<double>(),
1411 temp_v.ptr<double>(), temp_v.step, m, n, compute_uv ? urows : 0);
1419 transpose(temp_u, _u);
1426 transpose(temp_v, _u);
1434 void SVD::compute( InputArray a, OutputArray w, OutputArray u, OutputArray vt, int flags )
1436 _SVDcompute(a, w, u, vt, flags);
1439 void SVD::compute( InputArray a, OutputArray w, int flags )
1441 _SVDcompute(a, w, noArray(), noArray(), flags);
1444 void SVD::backSubst( InputArray _w, InputArray _u, InputArray _vt,
1445 InputArray _rhs, OutputArray _dst )
1447 Mat w = _w.getMat(), u = _u.getMat(), vt = _vt.getMat(), rhs = _rhs.getMat();
1448 int type = w.type(), esz = (int)w.elemSize();
1449 int m = u.rows, n = vt.cols, nb = rhs.data ? rhs.cols : m, nm = std::min(m, n);
1450 size_t wstep = w.rows == 1 ? (size_t)esz : w.cols == 1 ? (size_t)w.step : (size_t)w.step + esz;
1451 AutoBuffer<uchar> buffer(nb*sizeof(double) + 16);
1452 CV_Assert( w.type() == u.type() && u.type() == vt.type() && u.data && vt.data && w.data );
1453 CV_Assert( u.cols >= nm && vt.rows >= nm &&
1454 (w.size() == Size(nm, 1) || w.size() == Size(1, nm) || w.size() == Size(vt.rows, u.cols)) );
1455 CV_Assert( rhs.data == 0 || (rhs.type() == type && rhs.rows == m) );
1457 _dst.create( n, nb, type );
1458 Mat dst = _dst.getMat();
1459 if( type == CV_32F )
1460 SVBkSb(m, n, w.ptr<float>(), wstep, u.ptr<float>(), u.step, false,
1461 vt.ptr<float>(), vt.step, true, rhs.ptr<float>(), rhs.step, nb,
1462 dst.ptr<float>(), dst.step, buffer);
1463 else if( type == CV_64F )
1464 SVBkSb(m, n, w.ptr<double>(), wstep, u.ptr<double>(), u.step, false,
1465 vt.ptr<double>(), vt.step, true, rhs.ptr<double>(), rhs.step, nb,
1466 dst.ptr<double>(), dst.step, buffer);
1468 CV_Error( CV_StsUnsupportedFormat, "" );
1472 SVD& SVD::operator ()(InputArray a, int flags)
1474 _SVDcompute(a, w, u, vt, flags);
1479 void SVD::backSubst( InputArray rhs, OutputArray dst ) const
1481 backSubst( w, u, vt, rhs, dst );
1487 void cv::SVDecomp(InputArray src, OutputArray w, OutputArray u, OutputArray vt, int flags)
1489 SVD::compute(src, w, u, vt, flags);
1492 void cv::SVBackSubst(InputArray w, InputArray u, InputArray vt, InputArray rhs, OutputArray dst)
1494 SVD::backSubst(w, u, vt, rhs, dst);
1499 cvDet( const CvArr* arr )
1501 if( CV_IS_MAT(arr) && ((CvMat*)arr)->rows <= 3 )
1503 CvMat* mat = (CvMat*)arr;
1504 int type = CV_MAT_TYPE(mat->type);
1505 int rows = mat->rows;
1506 uchar* m = mat->data.ptr;
1507 int step = mat->step;
1508 CV_Assert( rows == mat->cols );
1510 #define Mf(y, x) ((float*)(m + y*step))[x]
1511 #define Md(y, x) ((double*)(m + y*step))[x]
1513 if( type == CV_32F )
1520 else if( type == CV_64F )
1528 return cv::determinant(cv::cvarrToMat(arr));
1533 cvInvert( const CvArr* srcarr, CvArr* dstarr, int method )
1535 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
1537 CV_Assert( src.type() == dst.type() && src.rows == dst.cols && src.cols == dst.rows );
1538 return cv::invert( src, dst, method == CV_CHOLESKY ? cv::DECOMP_CHOLESKY :
1539 method == CV_SVD ? cv::DECOMP_SVD :
1540 method == CV_SVD_SYM ? cv::DECOMP_EIG : cv::DECOMP_LU );
1545 cvSolve( const CvArr* Aarr, const CvArr* barr, CvArr* xarr, int method )
1547 cv::Mat A = cv::cvarrToMat(Aarr), b = cv::cvarrToMat(barr), x = cv::cvarrToMat(xarr);
1549 CV_Assert( A.type() == x.type() && A.cols == x.rows && x.cols == b.cols );
1550 bool is_normal = (method & CV_NORMAL) != 0;
1551 method &= ~CV_NORMAL;
1552 return cv::solve( A, b, x, (method == CV_CHOLESKY ? cv::DECOMP_CHOLESKY :
1553 method == CV_SVD ? cv::DECOMP_SVD :
1554 method == CV_SVD_SYM ? cv::DECOMP_EIG :
1555 A.rows > A.cols ? cv::DECOMP_QR : cv::DECOMP_LU) + (is_normal ? cv::DECOMP_NORMAL : 0) );
1560 cvEigenVV( CvArr* srcarr, CvArr* evectsarr, CvArr* evalsarr, double,
1563 cv::Mat src = cv::cvarrToMat(srcarr), evals0 = cv::cvarrToMat(evalsarr), evals = evals0;
1566 cv::Mat evects0 = cv::cvarrToMat(evectsarr), evects = evects0;
1567 eigen(src, evals, evects);
1568 if( evects0.data != evects.data )
1570 const uchar* p = evects0.ptr();
1571 evects.convertTo(evects0, evects0.type());
1572 CV_Assert( p == evects0.ptr() );
1577 if( evals0.data != evals.data )
1579 const uchar* p = evals0.ptr();
1580 if( evals0.size() == evals.size() )
1581 evals.convertTo(evals0, evals0.type());
1582 else if( evals0.type() == evals.type() )
1583 cv::transpose(evals, evals0);
1585 cv::Mat(evals.t()).convertTo(evals0, evals0.type());
1586 CV_Assert( p == evals0.ptr() );
1592 cvSVD( CvArr* aarr, CvArr* warr, CvArr* uarr, CvArr* varr, int flags )
1594 cv::Mat a = cv::cvarrToMat(aarr), w = cv::cvarrToMat(warr), u, v;
1595 int m = a.rows, n = a.cols, type = a.type(), mn = std::max(m, n), nm = std::min(m, n);
1597 CV_Assert( w.type() == type &&
1598 (w.size() == cv::Size(nm,1) || w.size() == cv::Size(1, nm) ||
1599 w.size() == cv::Size(nm, nm) || w.size() == cv::Size(n, m)) );
1603 if( w.size() == cv::Size(nm, 1) )
1604 svd.w = cv::Mat(nm, 1, type, w.ptr() );
1605 else if( w.isContinuous() )
1610 u = cv::cvarrToMat(uarr);
1611 CV_Assert( u.type() == type );
1617 v = cv::cvarrToMat(varr);
1618 CV_Assert( v.type() == type );
1622 svd(a, ((flags & CV_SVD_MODIFY_A) ? cv::SVD::MODIFY_A : 0) |
1623 ((!svd.u.data && !svd.vt.data) ? cv::SVD::NO_UV : 0) |
1624 ((m != n && (svd.u.size() == cv::Size(mn, mn) ||
1625 svd.vt.size() == cv::Size(mn, mn))) ? cv::SVD::FULL_UV : 0));
1629 if( flags & CV_SVD_U_T )
1630 cv::transpose( svd.u, u );
1631 else if( u.data != svd.u.data )
1633 CV_Assert( u.size() == svd.u.size() );
1640 if( !(flags & CV_SVD_V_T) )
1641 cv::transpose( svd.vt, v );
1642 else if( v.data != svd.vt.data )
1644 CV_Assert( v.size() == svd.vt.size() );
1649 if( w.data != svd.w.data )
1651 if( w.size() == svd.w.size() )
1656 cv::Mat wd = w.diag();
1664 cvSVBkSb( const CvArr* warr, const CvArr* uarr,
1665 const CvArr* varr, const CvArr* rhsarr,
1666 CvArr* dstarr, int flags )
1668 cv::Mat w = cv::cvarrToMat(warr), u = cv::cvarrToMat(uarr),
1669 v = cv::cvarrToMat(varr), rhs,
1670 dst = cv::cvarrToMat(dstarr), dst0 = dst;
1671 if( flags & CV_SVD_U_T )
1677 if( !(flags & CV_SVD_V_T) )
1684 rhs = cv::cvarrToMat(rhsarr);
1686 cv::SVD::backSubst(w, u, v, rhs, dst);
1687 CV_Assert( dst.data == dst0.data );