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

◆ least_squares_deficient() [3/4]

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

Definition at line 799 of file lapack.cpp.

802{
803 bool ret = true;
804 int rows = static_cast<int>(A->numRows());
805 int cols = static_cast<int>(A->numColumns());
806 int brows = static_cast<int>(b->numRows());
807 int bcols = static_cast<int>(b->numColumns());
808 double rcond = -1.0; // use machine precision
809 int rank, info;
810 int min = (rows < cols) ? rows : cols;
811 int max = (rows > cols) ? rows : cols;
812 int tempmax = ((2 * min > max) ? 2 * min : max);
813 int wsize = 3 * min + ((tempmax > bcols) ? tempmax : bcols);
814
815 if (brows != rows)
816 {
817 ERROR("expected compatible right hand side");
818 return false;
819 }
820
821 if (min == 0 || bcols == 0)
822 {
823 x->resize(cols, bcols);
824 return true;
825 }
826
827 std::vector<double> copyA = make_lapack_array(*A);
828 std::vector<double> copyb = make_lapack_array(*b);
829 double *workspace = new double[wsize];
830 double *sing = new double[min];
831
832 if (rows < cols)
833 {
834 // Make 'b' (copyb) into a cols x bcols matrix, by adding a zero block at
835 // the bottom
836 std::vector<double> copyb2(cols * bcols);
837 for (auto&a : copyb2) a = 0;
838 int bloc = 0;
839 int copyloc = 0;
840 for (int j = 0; j < bcols; j++)
841 {
842 copyloc = j * cols;
843 for (int i = 0; i < brows; i++)
844 {
845 copyb2[copyloc++] = copyb[bloc++];
846 }
847 }
848 std::swap(copyb2, copyb);
849 }
850
851 dgelss_(&rows,
852 &cols,
853 &bcols,
854 copyA.data(),
855 &rows,
856 copyb.data(),
857 &max,
858 sing,
859 &rcond,
860 &rank,
861 workspace,
862 &wsize,
863 &info);
864
865 if (info != 0)
866 {
867 ERROR("argument passed to dgelss had an illegal value");
868 ret = false;
869 }
870 else
871 {
872 x->resize(cols, bcols);
873 if (rows > cols)
874 {
875 // We only need the first 'cols' rows of copyb
876 int copyloc = 0;
877 for (int j = 0; j < bcols; j++)
878 {
879 copyloc = j * rows;
880 for (int i = 0; i < cols; i++)
881 {
882 x->entry(i,j) = copyb[copyloc++];
883 }
884 }
885 }
886 else
887 {
888 fill_from_lapack_array(copyb, *x);
889 }
890 }
891
892 delete [] workspace;
893 delete [] sing;
894
895 return ret;
896}
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 dgelss_(int *rows, int *cols, int *nhrs, double *A, int *ldA, double *b, int *ldb, double *Sigma, double *rcond, int *rank, double *work, int *lwork, 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 dgelss_(), ERROR, fill_from_lapack_array(), make_lapack_array(), max, DMat< ACoeffRing >::numColumns(), DMat< ACoeffRing >::numRows(), std::swap(), and x.