gesdd.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_LAPACK_GESDD_H_
36 #define _BLAZE_MATH_LAPACK_GESDD_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <memory>
44 #include <blaze/math/Aliases.h>
50 #include <blaze/math/Exception.h>
57 #include <blaze/util/Assert.h>
58 #include <blaze/util/DisableIf.h>
59 #include <blaze/util/EnableIf.h>
60 #include <blaze/util/NumericCast.h>
62 
63 
64 namespace blaze {
65 
66 //=================================================================================================
67 //
68 // LAPACK SVD FUNCTIONS (GESDD)
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
75 template< typename MT, bool SO, typename VT, bool TF >
76 inline void gesdd( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& s );
77 
78 template< typename MT1, bool SO, typename MT2, typename VT, bool TF >
79 inline void gesdd( DenseMatrix<MT1,SO>& A, DenseMatrix<MT2,SO>& U,
80  DenseVector<VT,TF>& s, char jobz );
81 
82 template< typename MT1, bool SO, typename MT2, typename VT, bool TF >
83 inline void gesdd( DenseMatrix<MT1,SO>& A, DenseVector<VT,TF>& s,
84  DenseMatrix<MT2,SO>& V, char jobz );
85 
86 template< typename MT1, bool SO, typename MT2, typename VT, bool TF, typename MT3 >
87 inline void gesdd( DenseMatrix<MT1,SO>& A, DenseMatrix<MT2,SO>& U,
88  DenseVector<VT,TF>& s, DenseMatrix<MT3,SO>& V, char jobz );
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
110 template< typename MT // Type of the matrix A
111  , bool SO // Storage order of the matrix A
112  , typename VT // Type of the vector s
113  , bool TF > // Transpose flag of the vector s
114 inline auto gesdd_backend( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& s )
115  -> DisableIf_t< IsComplex_v< ElementType_t<MT> > >
116 {
117  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
118 
119  using ET = ElementType_t<MT>;
120 
121  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
122  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
123  int lda ( numeric_cast<int>( (~A).spacing() ) );
124  int info( 0 );
125 
126  const int minimum( min( m, n ) );
127  const int maximum( max( m, n ) );
128 
129  int lwork( 3*minimum + max( maximum, 7*minimum ) + 2 );
130  const std::unique_ptr<ET[]> work( new ET[lwork] );
131  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
132 
133  gesdd( 'N', m, n, (~A).data(), lda, (~s).data(),
134  nullptr, 1, nullptr, 1, work.get(), lwork, iwork.get(), &info );
135 
136  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
137 
138  if( info > 0 ) {
139  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
140  }
141 }
143 //*************************************************************************************************
144 
145 
146 //*************************************************************************************************
163 template< typename MT // Type of the matrix A
164  , bool SO // Storage order of the matrix A
165  , typename VT // Type of the vector s
166  , bool TF > // Transpose flag of the vector s
167 inline auto gesdd_backend( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& s )
168  -> EnableIf_t< IsComplex_v< ElementType_t<MT> > >
169 {
170  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
171 
172  using CT = ElementType_t<MT>;
173  using BT = UnderlyingElement_t<CT>;
174 
175  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
176  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
177  int lda ( numeric_cast<int>( (~A).spacing() ) );
178  int info( 0 );
179 
180  const int minimum( min( m, n ) );
181  const int maximum( max( m, n ) );
182 
183  int lwork( 2*minimum + maximum + 2 );
184  const std::unique_ptr<CT[]> work( new CT[lwork] );
185  const std::unique_ptr<BT[]> rwork( new BT[7*minimum] );
186  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
187 
188  gesdd( 'N', m, n, (~A).data(), lda, (~s).data(),
189  nullptr, 1, nullptr, 1, work.get(), lwork, rwork.get(), iwork.get(), &info );
190 
191  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
192 
193  if( info > 0 ) {
194  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
195  }
196 }
198 //*************************************************************************************************
199 
200 
201 //*************************************************************************************************
263 template< typename MT // Type of the matrix A
264  , bool SO // Storage order of the matrix A
265  , typename VT // Type of the vector s
266  , bool TF > // Transpose flag of the vector s
268 {
274 
279 
280  const size_t M( (~A).rows() );
281  const size_t N( (~A).columns() );
282  const size_t mindim( min( M, N ) );
283 
284  resize( ~s, mindim, false );
285 
286  if( M == 0UL || N == 0 ) {
287  return;
288  }
289 
290  gesdd_backend( A, s );
291 }
292 //*************************************************************************************************
293 
294 
295 //*************************************************************************************************
314 template< typename MT1 // Type of the matrix A
315  , bool SO // Storage order of all matrices
316  , typename MT2 // Type of the matrix U
317  , typename VT // Type of the vector s
318  , bool TF > // Transpose flag of the vector s
319 inline auto gesdd_backend( DenseMatrix<MT1,SO>& A, DenseMatrix<MT2,SO>& U,
320  DenseVector<VT,TF>& s, char jobz )
321  -> DisableIf_t< IsComplex_v< ElementType_t<MT1> > >
322 {
323  BLAZE_INTERNAL_ASSERT( jobz == 'O' || jobz == 'N', "Invalid jobz flag detected" );
324  BLAZE_INTERNAL_ASSERT( jobz == 'N' || isSquare( ~U ), "Invalid matrix dimensions detected" );
325  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~U).rows() == (~A).rows(), "Invalid matrix dimensions detected" );
326  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
327 
328  using ET = ElementType_t<MT1>;
329 
330  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
331  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
332  int lda ( numeric_cast<int>( (~A).spacing() ) );
333  int ldu ( numeric_cast<int>( (~U).spacing() ) );
334  int info( 0 );
335 
336  const int minimum( min( m, n ) );
337  const int maximum( max( m, n ) );
338 
339  int lwork( ( jobz == 'O' )
340  ?( 3*minimum + max( maximum, 5*minimum*minimum + 4*maximum ) + 2 )
341  :( 3*minimum + max( maximum, 7*minimum ) + 2 ) );
342  const std::unique_ptr<ET[]> work( new ET[lwork] );
343  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
344 
345  gesdd( jobz, m, n, (~A).data(), lda, (~s).data(),
346  ( SO ? (~U).data() : nullptr ), ( SO ? ldu : 1 ),
347  ( SO ? nullptr : (~U).data() ), ( SO ? 1 : ldu ),
348  work.get(), lwork, iwork.get(), &info );
349 
350  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
351 
352  if( info > 0 ) {
353  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
354  }
355 }
357 //*************************************************************************************************
358 
359 
360 //*************************************************************************************************
379 template< typename MT1 // Type of the matrix A
380  , bool SO // Storage order of all matrices
381  , typename MT2 // Type of the matrix U
382  , typename VT // Type of the vector s
383  , bool TF > // Transpose flag of the vector s
384 inline auto gesdd_backend( DenseMatrix<MT1,SO>& A, DenseMatrix<MT2,SO>& U,
385  DenseVector<VT,TF>& s, char jobz )
386  -> EnableIf_t< IsComplex_v< ElementType_t<MT1> > >
387 {
388  BLAZE_INTERNAL_ASSERT( jobz == 'O' || jobz == 'N', "Invalid jobz flag detected" );
389  BLAZE_INTERNAL_ASSERT( jobz == 'N' || isSquare( ~U ), "Invalid matrix dimensions detected" );
390  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~U).rows() == (~A).rows(), "Invalid matrix dimensions detected" );
391  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
392 
393  using CT = ElementType_t<MT1>;
394  using BT = UnderlyingElement_t<CT>;
395 
396  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
397  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
398  int lda ( numeric_cast<int>( (~A).spacing() ) );
399  int ldu ( numeric_cast<int>( (~U).spacing() ) );
400  int info( 0 );
401 
402  const int minimum( min( m, n ) );
403  const int maximum( max( m, n ) );
404 
405  int lwork( ( jobz == 'O' )
406  ?( 2*minimum*minimum + 2*minimum + maximum + 2 )
407  :( 2*minimum + maximum + 2 ) );
408  const int lrwork( ( jobz == 'O' )
409  ?( max( 5*minimum*minimum + 5*minimum,
410  2*maximum*minimum + 2*minimum*minimum + minimum ) )
411  :( 7*minimum ) );
412  const std::unique_ptr<CT[]> work( new CT[lwork] );
413  const std::unique_ptr<BT[]> rwork( new BT[lrwork] );
414  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
415 
416  gesdd( jobz, m, n, (~A).data(), lda, (~s).data(),
417  ( SO ? (~U).data() : nullptr ), ( SO ? ldu : 1 ),
418  ( SO ? nullptr : (~U).data() ), ( SO ? 1 : ldu ),
419  work.get(), lwork, rwork.get(), iwork.get(), &info );
420 
421  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
422 
423  if( info > 0 ) {
424  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
425  }
426 }
428 //*************************************************************************************************
429 
430 
431 //*************************************************************************************************
520 template< typename MT1 // Type of the matrix A
521  , bool SO // Storage order of all matrices
522  , typename MT2 // Type of the matrix U
523  , typename VT // Type of the vector s
524  , bool TF > // Transpose flag of the vector s
526  DenseVector<VT,TF>& s, char jobz )
527 {
533 
539 
544 
545  const size_t M( (~A).rows() );
546  const size_t N( (~A).columns() );
547  const size_t mindim( min( M, N ) );
548 
549  if( jobz != 'O' && jobz != 'N' ) {
550  BLAZE_THROW_INVALID_ARGUMENT( "Invalid jobz argument provided" );
551  }
552 
553  if( jobz == 'O' && M >= N ) {
554  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix provided" );
555  }
556 
557  resize( ~s, mindim, false );
558 
559  if( jobz == 'O' ) {
560  resize( ~U, M, M, false );
561  }
562 
563  if( (~A).rows() == 0UL || (~A).columns() == 0UL ) {
564  return;
565  }
566 
567  gesdd_backend( ~A, ~U, ~s, jobz );
568 }
569 //*************************************************************************************************
570 
571 
572 //*************************************************************************************************
591 template< typename MT1 // Type of the matrix A
592  , bool SO // Storage order of all matrices
593  , typename VT // Type of the vector s
594  , bool TF // Transpose flag of the vector s
595  , typename MT2 > // Type of the matrix V
596 inline auto gesdd_backend( DenseMatrix<MT1,SO>& A, DenseVector<VT,TF>& s,
597  DenseMatrix<MT2,SO>& V, char jobz )
598  -> DisableIf_t< IsComplex_v< ElementType_t<MT1> > >
599 {
600  BLAZE_INTERNAL_ASSERT( jobz == 'O' || jobz == 'N', "Invalid jobz flag detected" );
601  BLAZE_INTERNAL_ASSERT( jobz == 'N' || isSquare( ~V ), "Invalid matrix dimensions detected" );
602  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~V).rows() == (~A).columns(), "Invalid matrix dimensions detected" );
603  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
604 
605  using ET = ElementType_t<MT1>;
606 
607  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
608  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
609  int lda ( numeric_cast<int>( (~A).spacing() ) );
610  int ldv ( numeric_cast<int>( (~V).spacing() ) );
611  int info( 0 );
612 
613  const int minimum( min( m, n ) );
614  const int maximum( max( m, n ) );
615 
616  int lwork( ( jobz == 'O' )
617  ?( 3*minimum + max( maximum, 5*minimum*minimum + 4*maximum + 2 ) )
618  :( 3*minimum + max( maximum, 7*minimum ) + 2 ) );
619  const std::unique_ptr<ET[]> work( new ET[lwork] );
620  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
621 
622  gesdd( jobz, m, n, (~A).data(), lda, (~s).data(),
623  ( SO ? nullptr : (~V).data() ), ( SO ? 1 : ldv ),
624  ( SO ? (~V).data() : nullptr ), ( SO ? ldv : 1 ),
625  work.get(), lwork, iwork.get(), &info );
626 
627  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
628 
629  if( info > 0 ) {
630  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
631  }
632 }
634 //*************************************************************************************************
635 
636 
637 //*************************************************************************************************
656 template< typename MT1 // Type of the matrix A
657  , bool SO // Storage order of all matrices
658  , typename VT // Type of the vector s
659  , bool TF // Transpose flag of the vector s
660  , typename MT2 > // Type of the matrix V
661 inline auto gesdd_backend( DenseMatrix<MT1,SO>& A, DenseVector<VT,TF>& s,
662  DenseMatrix<MT2,SO>& V, char jobz )
663  -> EnableIf_t< IsComplex_v< ElementType_t<MT1> > >
664 {
665  BLAZE_INTERNAL_ASSERT( jobz == 'O' || jobz == 'N', "Invalid jobz flag detected" );
666  BLAZE_INTERNAL_ASSERT( jobz == 'N' || isSquare( ~V ), "Invalid matrix dimensions detected" );
667  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~V).rows() == (~A).columns(), "Invalid matrix dimensions detected" );
668  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
669 
670  using CT = ElementType_t<MT1>;
671  using BT = UnderlyingElement_t<CT>;
672 
673  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
674  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
675  int lda ( numeric_cast<int>( (~A).spacing() ) );
676  int ldv ( numeric_cast<int>( (~V).spacing() ) );
677  int info( 0 );
678 
679  const int minimum( min( m, n ) );
680  const int maximum( max( m, n ) );
681 
682  int lwork( ( jobz == 'O' )
683  ?( 2*minimum*minimum + 2*minimum + maximum + 2 )
684  :( 2*minimum + maximum + 2 ) );
685  const int lrwork( ( jobz == 'O' )
686  ?( max( 5*minimum*minimum + 5*minimum,
687  2*maximum*minimum + 2*minimum*minimum + minimum ) )
688  :( 7*minimum ) );
689  const std::unique_ptr<CT[]> work( new CT[lwork] );
690  const std::unique_ptr<BT[]> rwork( new BT[lrwork] );
691  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
692 
693  gesdd( jobz, m, n, (~A).data(), lda, (~s).data(),
694  ( SO ? nullptr : (~V).data() ), ( SO ? 1 : ldv ),
695  ( SO ? (~V).data() : nullptr ), ( SO ? ldv : 1 ),
696  work.get(), lwork, rwork.get(), iwork.get(), &info );
697 
698  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
699 
700  if( info > 0 ) {
701  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
702  }
703 }
705 //*************************************************************************************************
706 
707 
708 //*************************************************************************************************
788 template< typename MT1 // Type of the matrix A
789  , bool SO // Storage order of all matrices
790  , typename MT2 // Type of the matrix U
791  , typename VT // Type of the vector s
792  , bool TF > // Transpose flag of the vector s
794  DenseMatrix<MT2,SO>& V, char jobz )
795 {
801 
807 
812 
813  const size_t M( (~A).rows() );
814  const size_t N( (~A).columns() );
815  const size_t mindim( min( M, N ) );
816 
817  if( jobz != 'O' && jobz != 'N' ) {
818  BLAZE_THROW_INVALID_ARGUMENT( "Invalid jobz argument provided" );
819  }
820 
821  if( jobz == 'O' && M < N ) {
822  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix provided" );
823  }
824 
825  resize( ~s, mindim, false );
826 
827  if( jobz == 'O' ) {
828  resize( ~V, N, N, false );
829  }
830 
831  if( (~A).rows() == 0UL || (~A).columns() == 0UL ) {
832  return;
833  }
834 
835  gesdd_backend( ~A, ~s, ~V, jobz );
836 }
837 //*************************************************************************************************
838 
839 
840 //*************************************************************************************************
860 template< typename MT1 // Type of the matrix A
861  , bool SO // Storage order of all matrices
862  , typename MT2 // Type of the matrix U
863  , typename VT // Type of the vector s
864  , bool TF // Transpose flag of the vector s
865  , typename MT3 > // Type of the matrix V
866 inline auto gesdd_backend( DenseMatrix<MT1,SO>& A, DenseMatrix<MT2,SO>& U,
867  DenseVector<VT,TF>& s, DenseMatrix<MT3,SO>& V, char jobz )
868  -> DisableIf_t< IsComplex_v< ElementType_t<MT1> > >
869 {
870  BLAZE_INTERNAL_ASSERT( jobz == 'A' || jobz == 'S' || jobz == 'N', "Invalid jobz flag detected" );
871  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~U).rows() == (~A).rows(), "Invalid matrix dimension detected" );
872  BLAZE_INTERNAL_ASSERT( jobz != 'A' || isSquare( ~U ), "Invalid non-square matrix detected" );
873  BLAZE_INTERNAL_ASSERT( jobz != 'S' || (~U).columns() == min( (~A).rows(), (~A).columns() ), "Invalid matrix dimension detected" );
874  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~V).columns() == (~A).columns(), "Invalid matrix dimension detected" );
875  BLAZE_INTERNAL_ASSERT( jobz != 'A' || isSquare( ~V ), "Invalid non-square matrix detected" );
876  BLAZE_INTERNAL_ASSERT( jobz != 'S' || (~V).rows() == min( (~A).rows(), (~A).columns() ), "Invalid matrix dimension detected" );
877  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
878 
879  using ET = ElementType_t<MT1>;
880 
881  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
882  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
883  int lda ( numeric_cast<int>( (~A).spacing() ) );
884  int ldu ( numeric_cast<int>( (~U).spacing() ) );
885  int ldv ( numeric_cast<int>( (~V).spacing() ) );
886  int info( 0 );
887 
888  const int minimum( min( m, n ) );
889  const int maximum( max( m, n ) );
890 
891  int lwork( 4*minimum*minimum + 6*minimum + maximum + 2 );
892  const std::unique_ptr<ET[]> work( new ET[lwork] );
893  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
894 
895  gesdd( jobz, m, n, (~A).data(), lda, (~s).data(),
896  ( SO ? (~U).data() : (~V).data() ), ( SO ? ldu : ldv ),
897  ( SO ? (~V).data() : (~U).data() ), ( SO ? ldv : ldu ),
898  work.get(), lwork, iwork.get(), &info );
899 
900  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
901 
902  if( info > 0 ) {
903  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
904  }
905 }
907 //*************************************************************************************************
908 
909 
910 //*************************************************************************************************
930 template< typename MT1 // Type of the matrix A
931  , bool SO // Storage order of all matrices
932  , typename MT2 // Type of the matrix U
933  , typename VT // Type of the vector s
934  , bool TF // Transpose flag of the vector s
935  , typename MT3 > // Type of the matrix V
936 inline auto gesdd_backend( DenseMatrix<MT1,SO>& A, DenseMatrix<MT2,SO>& U,
937  DenseVector<VT,TF>& s, DenseMatrix<MT3,SO>& V, char jobz )
938  -> EnableIf_t< IsComplex_v< ElementType_t<MT1> > >
939 {
940  BLAZE_INTERNAL_ASSERT( jobz == 'A' || jobz == 'S' || jobz == 'N', "Invalid jobz flag detected" );
941  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~U).rows() == (~A).rows(), "Invalid matrix dimension detected" );
942  BLAZE_INTERNAL_ASSERT( jobz != 'A' || isSquare( ~U ), "Invalid non-square matrix detected" );
943  BLAZE_INTERNAL_ASSERT( jobz != 'S' || (~U).columns() == min( (~A).rows(), (~A).columns() ), "Invalid matrix dimension detected" );
944  BLAZE_INTERNAL_ASSERT( jobz == 'N' || (~V).columns() == (~A).columns(), "Invalid matrix dimension detected" );
945  BLAZE_INTERNAL_ASSERT( jobz != 'A' || isSquare( ~V ), "Invalid non-square matrix detected" );
946  BLAZE_INTERNAL_ASSERT( jobz != 'S' || (~V).rows() == min( (~A).rows(), (~A).columns() ), "Invalid matrix dimension detected" );
947  BLAZE_INTERNAL_ASSERT( (~s).size() == min( (~A).rows(), (~A).columns() ), "Invalid vector dimension detected" );
948 
949  using CT = ElementType_t<MT1>;
950  using BT = UnderlyingElement_t<CT>;
951 
952  int m ( numeric_cast<int>( SO ? (~A).rows() : (~A).columns() ) );
953  int n ( numeric_cast<int>( SO ? (~A).columns() : (~A).rows() ) );
954  int lda ( numeric_cast<int>( (~A).spacing() ) );
955  int ldu ( numeric_cast<int>( (~U).spacing() ) );
956  int ldv ( numeric_cast<int>( (~V).spacing() ) );
957  int info( 0 );
958 
959  const int minimum( min( m, n ) );
960  const int maximum( max( m, n ) );
961 
962  int lwork( 4*minimum*minimum + 6*minimum + maximum + 2 );
963  const int lrwork( max( 5*minimum*minimum + 5*minimum,
964  2*maximum*minimum + 2*minimum*minimum + minimum ) );
965  const std::unique_ptr<CT[]> work( new CT[lwork] );
966  const std::unique_ptr<BT[]> rwork( new BT[lrwork] );
967  const std::unique_ptr<int[]> iwork( new int[8*minimum] );
968 
969  gesdd( jobz, m, n, (~A).data(), lda, (~s).data(),
970  ( SO ? (~U).data() : (~V).data() ), ( SO ? ldu : ldv ),
971  ( SO ? (~V).data() : (~U).data() ), ( SO ? ldv : ldu ),
972  work.get(), lwork, rwork.get(), iwork.get(), &info );
973 
974  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for singular value decomposition" );
975 
976  if( info > 0 ) {
977  BLAZE_THROW_LAPACK_ERROR( "Singular value decomposition failed" );
978  }
979 }
981 //*************************************************************************************************
982 
983 
984 //*************************************************************************************************
1086 template< typename MT1 // Type of the matrix A
1087  , bool SO // Storage order of all matrices
1088  , typename MT2 // Type of the matrix U
1089  , typename VT // Type of the vector s
1090  , bool TF // Transpose flag of the vector s
1091  , typename MT3 > // Type of the matrix V
1093  DenseVector<VT,TF>& s, DenseMatrix<MT3,SO>& V, char jobz )
1094 {
1100 
1106 
1111 
1117 
1118  const size_t M( (~A).rows() );
1119  const size_t N( (~A).columns() );
1120  const size_t mindim( min( M, N ) );
1121 
1122  if( jobz != 'A' && jobz != 'S' && jobz != 'N' ) {
1123  BLAZE_THROW_INVALID_ARGUMENT( "Invalid jobz argument provided" );
1124  }
1125 
1126  resize( ~s, mindim, false );
1127 
1128  if( jobz != 'N' ) {
1129  resize( ~U, M, ( jobz == 'A' ? M : mindim ), false );
1130  resize( ~V, ( jobz == 'A' ? N : mindim ), N, false );
1131  }
1132 
1133  if( (~A).rows() == 0UL || (~A).columns() == 0UL ) {
1134  return;
1135  }
1136 
1137  gesdd_backend( ~A, ~U, ~s, ~V, jobz );
1138 }
1139 //*************************************************************************************************
1140 
1141 } // namespace blaze
1142 
1143 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Constraint on the data type.
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
#define BLAZE_CONSTRAINT_MUST_HAVE_MUTABLE_DATA_ACCESS(T)
Constraint on the data type.In case the given data type T does not provide low-level data access to m...
Definition: MutableDataAccess.h:61
MT::ElementType * data(DenseMatrix< MT, SO > &dm) noexcept
Low-level data access to the dense matrix elements.
Definition: DenseMatrix.h:169
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPUTATION_TYPE(T)
Constraint on the data type.In case the given data type T is a computational expression (i...
Definition: Computation.h:81
Header file for the DenseVector base class.
Header file for the UnderlyingElement type trait.
Constraint on the data type.
Constraint on the data type.
Cast operators for numeric types.
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ADAPTOR_TYPE(T)
Constraint on the data type.In case the given data type T is an adaptor type (as for instance LowerMa...
Definition: Adaptor.h:81
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:80
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
size_t spacing(const DenseMatrix< MT, SO > &dm) noexcept
Returns the spacing between the beginning of two rows/columns.
Definition: DenseMatrix.h:252
Headerfile for the generic max algorithm.
Constraint on the data type.
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
#define BLAZE_CONSTRAINT_MUST_BE_CONTIGUOUS_TYPE(T)
Constraint on the data type.In case the given data type T is not an array-like data type with contigu...
Definition: Contiguous.h:61
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1147
Header file for the DenseMatrix base class.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
void gesdd(char jobz, int m, int n, float *A, int lda, float *s, float *U, int ldu, float *V, int ldv, float *work, int lwork, int *iwork, int *info)
LAPACK kernel for the singular value decomposition (SVD) of the given dense general single precision ...
Definition: gesdd.h:165
Header file for the exception macros of the math module.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1179
void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:738
Constraint on the data type.
Header file for the EnableIf class template.
Header file for run time assertion macros.
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
#define BLAZE_CONSTRAINT_MUST_BE_BLAS_COMPATIBLE_TYPE(T)
Constraint on the data type.In case the given data type T is not a BLAS compatible data type (i...
Definition: BLASCompatible.h:61
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
#define BLAZE_THROW_LAPACK_ERROR(MESSAGE)
Macro for the emission of an exception on detection of a LAPACK error.This macro encapsulates the def...
Definition: Exception.h:146
Header file for the IsComplex type trait.
Header file for the CLAPACK gesdd wrapper functions.
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:951
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101