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

◆ SVD() [1/4]

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

Definition at line 1491 of file lapack.cpp.

1495{
1496 bool ret = true;
1497 char doit = 'A'; // other options are 'S' and 'O' for singular vectors only
1498 int rows = static_cast<int>(A->numRows());
1499 int cols = static_cast<int>(A->numColumns());
1500 int info;
1501 int min = (rows <= cols) ? rows : cols;
1502
1503 if (min == 0)
1504 {
1505 ERROR("expected a matrix with positive dimensions");
1506 return false;
1507 }
1508
1509 int max = (rows >= cols) ? rows : cols;
1510 int wsize = 2 * min + max;
1511 double *workspace = new double[2 * wsize];
1512 double *rwork = new double[5 * min];
1513
1514 std::vector<double> copyA = make_lapack_array(*A);
1515 std::vector<double> u(2 * rows * rows);
1516 std::vector<double> vt(2 * cols * cols);
1517 std::vector<double> sigma(2 * min);
1518
1519 zgesvd_(&doit,
1520 &doit,
1521 &rows,
1522 &cols,
1523 copyA.data(),
1524 &rows,
1525 sigma.data(),
1526 u.data(),
1527 &rows,
1528 vt.data(),
1529 &cols,
1530 workspace,
1531 &wsize,
1532 rwork,
1533 &info);
1534
1535 if (info < 0)
1536 {
1537 ERROR("argument passed to zgesvd had an illegal value");
1538 ret = false;
1539 }
1540 else if (info > 0)
1541 {
1542 ERROR("zgesvd did not converge");
1543 ret = false;
1544 }
1545 else
1546 {
1547 U->resize(rows, rows);
1549 VT->resize(cols, cols);
1550 fill_from_lapack_array(vt, *VT);
1551 Sigma->resize(min, 1);
1552 fill_from_lapack_array(sigma, *Sigma);
1553 }
1554
1555 delete[] workspace;
1556 delete[] rwork;
1557
1558 return ret;
1559}
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 zgesvd_(char *jobU, char *jobV, int *rows, int *cols, double *A, int *ldA, double *Sigma, double *U, int *ldU, double *VT, int *ldVT, double *w, int *lwork, double *rwork, 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 ERROR, fill_from_lapack_array(), make_lapack_array(), max, DMat< ACoeffRing >::numColumns(), DMat< ACoeffRing >::numRows(), DMat< ACoeffRing >::resize(), and zgesvd_().