Macaulay2 Engine
Loading...
Searching...
No Matches

◆ SVD() [3/4]

bool Lapack::SVD ( const DMatRR * A,
DMatRR * Sigma,
DMatRR * U,
DMatRR * VT )
static

Definition at line 569 of file lapack.cpp.

573{
574 bool ret = true;
575 char doit = 'A'; // other options are 'S' and 'O' for singular vectors only
576 int rows = static_cast<int>(A->numRows());
577 int cols = static_cast<int>(A->numColumns());
578 int info;
579 int min = (rows <= cols) ? rows : cols;
580
581 if (min == 0)
582 {
583 ERROR("expected a matrix with positive dimensions");
584 return false;
585 }
586
587 int max = (rows >= cols) ? rows : cols;
588 int wsize = (3 * min + max >= 5 * min) ? 3 * min + max : 5 * min;
589 double *workspace = new double[wsize];
590
591 std::vector<double> copyA = make_lapack_array(*A);
592 std::vector<double> u(rows * rows);
593 std::vector<double> vt(cols * cols);
594 std::vector<double> sigma(min);
595
596 dgesvd_(&doit,
597 &doit,
598 &rows,
599 &cols,
600 copyA.data(),
601 &rows,
602 sigma.data(),
603 u.data(),
604 &rows,
605 vt.data(),
606 &cols,
607 workspace,
608 &wsize,
609 &info);
610
611 if (info < 0)
612 {
613 ERROR("argument passed to dgesvd had an illegal value");
614 ret = false;
615 }
616 else if (info > 0)
617 {
618 ERROR("dgesvd did not converge");
619 ret = false;
620 }
621 else
622 {
623 U->resize(rows, rows);
624 VT->resize(cols, cols);
625 Sigma->resize(min, 1);
627 fill_from_lapack_array(vt, *VT);
628 fill_from_lapack_array(sigma, *Sigma);
629 }
630
631 delete [] workspace;
632
633 return ret;
634}
size_t numRows() const
Definition dmat.hpp:144
void resize(size_t new_nrows, size_t new_ncols)
Definition dmat.hpp:157
size_t numColumns() const
Definition dmat.hpp:145
std::vector< double > make_lapack_array(const DMatRR &mat)
Definition lapack.cpp:20
void fill_from_lapack_array(const std::vector< double > &doubles, DMatRR &mat)
Definition lapack.cpp:45
int dgesvd_(char *jobU, char *jobV, int *rows, int *cols, double *A, int *ldA, double *Sigma, double *U, int *ldU, double *VT, int *ldVT, double *work, int *lwork, int *info)
const int ERROR
Definition m2-mem.cpp:55
const mpreal min(const mpreal &x, const mpreal &y)
Definition mpreal.h:2792
#define max(a, b)
Definition polyroots.cpp:52

References dgesvd_(), ERROR, fill_from_lapack_array(), make_lapack_array(), max, DMat< ACoeffRing >::numColumns(), DMat< ACoeffRing >::numRows(), and DMat< ACoeffRing >::resize().