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

◆ solve()

template<class RingType>
bool DMatLinAlg< RingType >::solve ( const Mat & B,
Mat & X )

Input: B, a matrix, the right hand side of AX=B Output: X, a matrix, solution to the above returns false iff inconsistent

printf("b:\n"); debug_out_list(b, LU.numRows());

printf("y:\n"); debug_out_list(y, rk);

printf("past test for consistency\n");

buffer o; printf("after i=%ld\n", i); displayMat(o, X); printf("%s\n", o.str());

printf("x:\n"); debug_out_list(x, LU.numColumns());

buffer o; printf("after col=%ld\n", col); displayMat(o, X); printf("%s\n", o.str());

Definition at line 340 of file dmat-lu.hpp.

341{
342 // printf("in dmat-lu: solve\n");
343 const Mat& LU = mLUObject.LUinPlace();
344 // printf("in dmat-lu: after LU solve\n");
345
346 // For each column of B, we solve it separately.
347
348 // Step 1: Take a column, and permute it via mPerm, into b.
349 // Step 2: Solve the lower triangular system Ly=b
350 // If there is no solution, then return false.
351 // Step 3: Solve the upper triangular system Ux = y.
352
353 // Sizes of objects here:
354 // A is m x n
355 // L is m x r
356 // U is r x n. Here, r <= m, and A has rank r.
357 // b is a vector 0..m-1
358 // y is a vector 0..r-1
359 // x is a vector 0..n-1
360
361 // printf("entering DMatLinAlg::solve\n");
362 const std::vector<size_t>& pivotColumns = mLUObject.pivotColumns();
363 const std::vector<size_t>& perm = mLUObject.permutation();
364 size_t rk = pivotColumns.size();
365
366 typename RingType::Element tmp(ring()), tmp2(ring());
367
368 typedef typename RingType::ElementArray ElementArray;
369 ElementArray b(ring(), LU.numRows());
370 ElementArray y(ring(), rk);
371 ElementArray x(ring(), LU.numColumns());
372
373 X.resize(LU.numColumns(), B.numColumns());
374
375 for (size_t col = 0; col < B.numColumns(); col++)
376 {
377 // Step 0: erase x (b will be set below, y is also set when needed).
378
379 for (size_t i = 0; i < LU.numColumns(); i++) ring().set_zero(x[i]);
380
381 // Step 1: set b to be the permuted i-th column of B.
382 for (size_t r = 0; r < B.numRows(); r++)
383 ring().set(b[r], B.entry(perm[r], col));
384
387
388 // Step 2: Solve Ly=b
389 for (size_t i = 0; i < rk; i++)
390 {
391 ring().set(y[i], b[i]);
392 for (size_t j = 0; j < i; j++)
393 {
394 ring().mult(tmp, LU.entry(i, j), y[j]);
395 ring().subtract(y[i], y[i], tmp);
396 }
397 }
398
401
402 // Step 2B: see if the solution is consistent
403 for (size_t i = rk; i < LU.numRows(); i++)
404 {
405 ring().set(tmp, b[i]);
406 for (size_t j = 0; j < rk; j++)
407 {
408 ring().mult(tmp2, LU.entry(i, j), y[j]);
409 ring().subtract(tmp, tmp, tmp2);
410 }
411 if (!ring().is_zero(tmp))
412 {
413 // printf("returning false\n");
414 return false;
415 }
416 }
417
419
420 // Step 3: Solve Ux=y
421 // and place x back into X as col-th column
422 for (long i = rk - 1; i >= 0; --i)
423 {
424 ring().set(x[i], y[i]);
425 for (size_t j = i + 1; j <= rk - 1; j++)
426 {
427 ring().mult(tmp, LU.entry(i, pivotColumns[j]), x[j]);
428 ring().subtract(x[i], x[i], tmp);
429 }
430 ring().divide(x[i], x[i], LU.entry(i, pivotColumns[i]));
431 ring().set(X.entry(pivotColumns[i], col), x[i]);
432
437 }
440
445 }
446
447 return true; // The system seems to have been consistent
448}
DMatLUinPlace< RingType > mLUObject
Definition dmat-lu.hpp:64
const RingType & ring() const
Definition dmat-lu.hpp:107
DMat< RingType > Mat
Definition dmat-lu.hpp:61
volatile int x

References DMat< ACoeffRing >::entry(), mLUObject, DMat< ACoeffRing >::numColumns(), DMat< ACoeffRing >::numRows(), DMat< ACoeffRing >::resize(), ring(), and x.

Referenced by inverse(), solveInvertible(), and MatrixOps::solveLinear().