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

◆ least_squares() [1/4]

bool Lapack::least_squares ( const DMatCC * A,
const DMatCC * b,
DMatCC * x )
static

Definition at line 1634 of file lapack.cpp.

1635{
1636 bool ret = true;
1637 char job = 'N';
1638 int info;
1639 int rows = static_cast<int>(A->numRows());
1640 int cols = static_cast<int>(A->numColumns());
1641 int brows = static_cast<int>(b->numRows());
1642 int bcols = static_cast<int>(b->numColumns());
1643 int min = (rows <= cols) ? rows : cols;
1644 int max = (rows >= cols) ? rows : cols;
1645 int wsize = min + ((bcols >= max) ? bcols : max);
1646
1647 if (brows != rows)
1648 {
1649 ERROR("expected compatible right hand side");
1650 return false;
1651 }
1652
1653 if (min == 0 || bcols == 0)
1654 {
1655 x->resize(cols, bcols);
1656 return true;
1657 }
1658
1659 std::vector<double> copyA = make_lapack_array(*A);
1660 std::vector<double> copyb = make_lapack_array(*b);
1661 double *workspace = new double[2 * wsize];
1662
1663 if (rows < cols)
1664 {
1665 // Make 'b' (copyb) into a cols x bcols matrix, by adding a zero block at
1666 // the bottom
1667 std::vector<double> copyb2(2 * cols * bcols);
1668 for (auto& a : copyb2) a = 0;
1669 int bloc = 0;
1670 int copyloc = 0;
1671 for (int j = 0; j < bcols; j++)
1672 {
1673 copyloc = 2 * j * cols;
1674 for (int i = 0; i < 2 * brows; i++)
1675 {
1676 copyb2[copyloc++] = copyb[bloc++];
1677 }
1678 }
1679 std::swap(copyb, copyb2);
1680 }
1681
1682 zgels_(&job,
1683 &rows,
1684 &cols,
1685 &bcols,
1686 copyA.data(),
1687 &rows,
1688 copyb.data(),
1689 &max,
1690 workspace,
1691 &wsize,
1692 &info);
1693
1694 if (info != 0)
1695 {
1696 ERROR("argument passed to zgels had an illegal value");
1697 ret = false;
1698 }
1699 else
1700 {
1701 x->resize(cols, bcols);
1702 if (rows > cols)
1703 {
1704 // We only need the first 'cols' rows of copyb
1705 int copyloc = 0;
1706 for (int j = 0; j < bcols; j++)
1707 {
1708 copyloc = 2 * j * rows;
1709 for (int i = 0; i < cols; i++)
1710 {
1711 double re = copyb[copyloc++];
1712 double im = copyb[copyloc++];
1713 x->ring().set_from_doubles(x->entry(i,j), re, im);
1714 }
1715 }
1716 }
1717 else
1718 {
1719 fill_from_lapack_array(copyb, *x);
1720 }
1721 }
1722
1723 delete[] workspace;
1724
1725 return ret;
1726}
size_t numRows() const
Definition dmat.hpp:144
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 zgels_(char *job, int *rows, int *cols, int *nhrs, double *A, int *ldA, double *b, int *ldb, 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
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition mpreal.h:3244
volatile int x
#define max(a, b)
Definition polyroots.cpp:52

References ERROR, fill_from_lapack_array(), make_lapack_array(), max, DMat< ACoeffRing >::numColumns(), DMat< ACoeffRing >::numRows(), std::swap(), x, and zgels_().