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

◆ solve() [3/4]

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

Definition at line 240 of file lapack.cpp.

243{
244 int size = static_cast<int>(A->numRows());
245 int bsize = static_cast<int>(b->numColumns());
246 int info;
247
248 /* make sure matrix is square */
249 if (A->numRows() != A->numColumns())
250 {
251 ERROR("expected a square matrix");
252 return false;
253 }
254
255 /* make sure dimensions of b make sense for Ax=b */
256 if (b->numRows() != size)
257 {
258 ERROR("expected matrices to have same number of rows");
259 return false;
260 ;
261 }
262
263 if (size == 0)
264 {
265 x->resize(size, bsize);
266 return true;
267 }
268
269 int* perm = new int[size];
270 std::vector<double> copyA = make_lapack_array(*A);
271 std::vector<double> copyb = make_lapack_array(*b);
272
273 dgesv_(&size,
274 &bsize,
275 copyA.data(),
276 &size,
277 perm,
278 copyb.data(), // also the result
279 &size,
280 &info);
281
282 delete [] perm; // Do we need this for anything??
283
284 // Now set x
285 x->resize(size, bsize);
286 fill_from_lapack_array(copyb, *x);
287
288 if (info > 0)
289 {
290 ERROR("according to dgesv, matrix is singular");
291 return false;
292 }
293 else if (info < 0)
294 {
295 ERROR("argument passed to dgesv had an illegal value");
296 return false;
297 }
298
299 return true;
300}
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 dgesv_(int *n, int *nrhs, double *a, int *lda, int *ipiv, double *b, int *ldb, int *info)
const int ERROR
Definition m2-mem.cpp:55
volatile int x

References dgesv_(), ERROR, fill_from_lapack_array(), make_lapack_array(), DMat< ACoeffRing >::numColumns(), DMat< ACoeffRing >::numRows(), and x.