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

◆ least_squares_deficient() [1/4]

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

Definition at line 1728 of file lapack.cpp.

1731{
1732 bool ret = true;
1733 int rows = static_cast<int>(A->numRows());
1734 int cols = static_cast<int>(A->numColumns());
1735 int brows = static_cast<int>(b->numRows());
1736 int bcols = static_cast<int>(b->numColumns());
1737 double rcond = -1.0;
1738 int rank, info;
1739 int min = (rows < cols) ? rows : cols;
1740 int max = (rows > cols) ? rows : cols;
1741 int wsize = 2 * min + ((bcols > max) ? bcols : max);
1742
1743 if (brows != rows)
1744 {
1745 ERROR("expected compatible right hand side");
1746 return false;
1747 }
1748
1749 if (min == 0 || bcols == 0)
1750 {
1751 x->resize(cols, bcols);
1752 return true;
1753 }
1754
1755 std::vector<double> copyA = make_lapack_array(*A);
1756 std::vector<double> copyb = make_lapack_array(*b);
1757 double *workspace = new double[2 * wsize];
1758 double *sing = new double[min];
1759 double *rwork = new double[5 * min];
1760
1761 if (rows < cols)
1762 {
1763 // Make 'b' (copyb) into a cols x bcols matrix, by adding a zero block at
1764 // the bottom
1765 std::vector<double> copyb2(2 * cols * bcols);
1766 for (auto& a : copyb2) a = 0;
1767 int bloc = 0;
1768 int copyloc = 0;
1769 for (int j = 0; j < bcols; j++)
1770 {
1771 copyloc = 2 * j * cols;
1772 for (int i = 0; i < 2 * brows; i++)
1773 {
1774 copyb2[copyloc++] = copyb[bloc++];
1775 }
1776 }
1777 std::swap(copyb, copyb2);
1778 }
1779
1780 zgelss_(&rows,
1781 &cols,
1782 &bcols,
1783 copyA.data(),
1784 &rows,
1785 copyb.data(),
1786 &max,
1787 sing,
1788 &rcond,
1789 &rank,
1790 workspace,
1791 &wsize,
1792 rwork,
1793 &info);
1794
1795 if (info != 0)
1796 {
1797 ERROR("argument passed to zgelss had an illegal value");
1798 ret = false;
1799 }
1800 else
1801 {
1802 x->resize(cols, bcols);
1803 if (rows > cols)
1804 {
1805 // We only need the first 'cols' rows of copyb
1806 int copyloc = 0;
1807 for (int j = 0; j < bcols; j++)
1808 {
1809 copyloc = 2 * j * rows;
1810 for (int i = 0; i < cols; i++)
1811 {
1812 double re = copyb[copyloc++];
1813 double im = copyb[copyloc++];
1814 x->ring().set_from_doubles(x->entry(i,j), re, im);
1815 }
1816 }
1817 }
1818 else
1819 {
1820 fill_from_lapack_array(copyb, *x);
1821 }
1822 }
1823
1824 delete[] workspace;
1825 delete[] sing;
1826 delete[] rwork;
1827
1828 return ret;
1829}
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 zgelss_(int *rows, int *cols, int *nhrs, double *A, int *ldA, double *b, int *ldb, double *Sigma, double *rcond, int *rank, double *work, int *lwork, double *rwork, int *info)
const int ERROR
Definition m2-mem.cpp:55
size_t rank(const DMatZZpFFPACK &A)
Definition dmat.cpp:80
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 zgelss_().