30 # include <itpp/config.h>
32 # include <itpp/config_msvc.h>
35 #if defined(HAVE_LAPACK)
36 # include <itpp/base/algebra/lapack.h>
47 #if defined(HAVE_LAPACK)
51 int n, lda, ldb, nrhs, info;
52 n = lda = ldb = A.rows();
56 it_assert_debug(A.cols() == n,
"ls_solve_chol: System-matrix is not square");
57 it_assert_debug(n == b.size(),
"The number of rows in A must equal the length of b!");
63 dposv_(&uplo, &n, &nrhs, Chol._data(), &lda, x._data(), &ldb, &info);
71 int n, lda, ldb, nrhs, info;
72 n = lda = ldb = A.rows();
76 it_assert_debug(A.cols() == n,
"ls_solve_chol: System-matrix is not square");
77 it_assert_debug(n == B.rows(),
"The number of rows in A must equal the length of B!");
83 dposv_(&uplo, &n, &nrhs, Chol._data(), &lda, X._data(), &ldb, &info);
90 int n, lda, ldb, nrhs, info;
91 n = lda = ldb = A.rows();
95 it_assert_debug(A.cols() == n,
"ls_solve_chol: System-matrix is not square");
96 it_assert_debug(n == b.size(),
"The number of rows in A must equal the length of b!");
102 zposv_(&uplo, &n, &nrhs, Chol._data(), &lda, x._data(), &ldb, &info);
109 int n, lda, ldb, nrhs, info;
110 n = lda = ldb = A.rows();
114 it_assert_debug(A.cols() == n,
"ls_solve_chol: System-matrix is not square");
115 it_assert_debug(n == B.rows(),
"The number of rows in A must equal the length of B!");
121 zposv_(&uplo, &n, &nrhs, Chol._data(), &lda, X._data(), &ldb, &info);
130 it_error(
"LAPACK library is needed to use ls_solve_chol() function");
136 it_error(
"LAPACK library is needed to use ls_solve_chol() function");
142 it_error(
"LAPACK library is needed to use ls_solve_chol() function");
148 it_error(
"LAPACK library is needed to use ls_solve_chol() function");
152 #endif // HAVE_LAPACK
192 #if defined(HAVE_LAPACK)
194 bool ls_solve(
const mat &A,
const vec &b, vec &x)
196 int n, lda, ldb, nrhs, info;
197 n = lda = ldb = A.rows();
200 it_assert_debug(A.cols() == n,
"ls_solve: System-matrix is not square");
201 it_assert_debug(n == b.size(),
"The number of rows in A must equal the length of b!");
207 dgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), x._data(), &ldb, &info);
212 bool ls_solve(
const mat &A,
const mat &B, mat &X)
214 int n, lda, ldb, nrhs, info;
215 n = lda = ldb = A.rows();
218 it_assert_debug(A.cols() == n,
"ls_solve: System-matrix is not square");
219 it_assert_debug(n == B.rows(),
"The number of rows in A must equal the length of B!");
225 dgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), X._data(), &ldb, &info);
230 bool ls_solve(
const cmat &A,
const cvec &b, cvec &x)
232 int n, lda, ldb, nrhs, info;
233 n = lda = ldb = A.rows();
236 it_assert_debug(A.cols() == n,
"ls_solve: System-matrix is not square");
237 it_assert_debug(n == b.size(),
"The number of rows in A must equal the length of b!");
243 zgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), x._data(), &ldb, &info);
248 bool ls_solve(
const cmat &A,
const cmat &B, cmat &X)
250 int n, lda, ldb, nrhs, info;
251 n = lda = ldb = A.rows();
254 it_assert_debug(A.cols() == n,
"ls_solve: System-matrix is not square");
255 it_assert_debug(n == B.rows(),
"The number of rows in A must equal the length of B!");
261 zgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), X._data(), &ldb, &info);
270 it_error(
"LAPACK library is needed to use ls_solve() function");
276 it_error(
"LAPACK library is needed to use ls_solve() function");
280 bool ls_solve(
const cmat &A,
const cvec &b, cvec &x)
282 it_error(
"LAPACK library is needed to use ls_solve() function");
286 bool ls_solve(
const cmat &A,
const cmat &B, cmat &X)
288 it_error(
"LAPACK library is needed to use ls_solve() function");
292 #endif // HAVE_LAPACK
332 #if defined(HAVE_LAPACK)
334 bool ls_solve_od(
const mat &A,
const vec &b, vec &x)
336 int m, n, lda, ldb, nrhs, lwork, info;
338 m = lda = ldb = A.rows();
344 it_assert_debug(m == b.size(),
"The number of rows in A must equal the length of b!");
350 dgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, x._data(), &ldb, work._data(), &lwork, &info);
356 bool ls_solve_od(
const mat &A,
const mat &B, mat &X)
358 int m, n, lda, ldb, nrhs, lwork, info;
360 m = lda = ldb = A.rows();
366 it_assert_debug(m == B.rows(),
"The number of rows in A must equal the length of b!");
372 dgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, X._data(), &ldb, work._data(), &lwork, &info);
373 X.set_size(n, nrhs,
true);
378 bool ls_solve_od(
const cmat &A,
const cvec &b, cvec &x)
380 int m, n, lda, ldb, nrhs, lwork, info;
382 m = lda = ldb = A.rows();
388 it_assert_debug(m == b.size(),
"The number of rows in A must equal the length of b!");
394 zgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, x._data(), &ldb, work._data(), &lwork, &info);
400 bool ls_solve_od(
const cmat &A,
const cmat &B, cmat &X)
402 int m, n, lda, ldb, nrhs, lwork, info;
404 m = lda = ldb = A.rows();
410 it_assert_debug(m == B.rows(),
"The number of rows in A must equal the length of b!");
416 zgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, X._data(), &ldb, work._data(), &lwork, &info);
417 X.set_size(n, nrhs,
true);
426 it_error(
"LAPACK library is needed to use ls_solve_od() function");
432 it_error(
"LAPACK library is needed to use ls_solve_od() function");
438 it_error(
"LAPACK library is needed to use ls_solve_od() function");
444 it_error(
"LAPACK library is needed to use ls_solve_od() function");
448 #endif // HAVE_LAPACK
487 #if defined(HAVE_LAPACK)
489 bool ls_solve_ud(
const mat &A,
const vec &b, vec &x)
491 int m, n, lda, ldb, nrhs, lwork, info;
500 it_assert_debug(m == b.size(),
"The number of rows in A must equal the length of b!");
507 dgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, x._data(), &ldb, work._data(), &lwork, &info);
512 bool ls_solve_ud(
const mat &A,
const mat &B, mat &X)
514 int m, n, lda, ldb, nrhs, lwork, info;
523 it_assert_debug(m == B.rows(),
"The number of rows in A must equal the length of b!");
527 X.set_size(n,
std::max(m, nrhs),
true);
530 dgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, X._data(), &ldb, work._data(), &lwork, &info);
531 X.set_size(n, nrhs,
true);
536 bool ls_solve_ud(
const cmat &A,
const cvec &b, cvec &x)
538 int m, n, lda, ldb, nrhs, lwork, info;
547 it_assert_debug(m == b.size(),
"The number of rows in A must equal the length of b!");
554 zgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, x._data(), &ldb, work._data(), &lwork, &info);
559 bool ls_solve_ud(
const cmat &A,
const cmat &B, cmat &X)
561 int m, n, lda, ldb, nrhs, lwork, info;
570 it_assert_debug(m == B.rows(),
"The number of rows in A must equal the length of b!");
574 X.set_size(n,
std::max(m, nrhs),
true);
577 zgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, X._data(), &ldb, work._data(), &lwork, &info);
578 X.set_size(n, nrhs,
true);
587 it_error(
"LAPACK library is needed to use ls_solve_ud() function");
593 it_error(
"LAPACK library is needed to use ls_solve_ud() function");
599 it_error(
"LAPACK library is needed to use ls_solve_ud() function");
605 it_error(
"LAPACK library is needed to use ls_solve_ud() function");
609 #endif // HAVE_LAPACK
653 int m = A.rows(), n = A.cols();
679 int m = A.rows(), n = A.cols();
705 int m = A.rows(), n = A.cols();
731 int m = A.rows(), n = A.cols();
768 it_assert(L.rows() == L.cols() && L.cols() == b.size() && b.size() == x.size(),
769 "forward_substitution: dimension mismatch");
770 int n = L.rows(), i, j;
773 x(0) = b(0) / L(0, 0);
774 for (i = 1;i < n;i++) {
778 for (j = 0; j < i; j++) {
779 temp += L._elem(i, j) * x(j);
782 x(i) = (b(i) - temp) / L._elem(i, i);
799 it_assert(L.rows() == L.cols() && L.cols() == b.size() && b.size() == x.size() && p <= L.rows() / 2,
800 "forward_substitution: dimension mismatch");
801 int n = L.rows(), i, j;
805 for (j = 0;j < n;j++) {
807 for (i = j + 1;i <
std::min(j + p + 1, n);i++) {
808 x(i) -= L(i, j) * x(j);
823 it_assert(U.rows() == U.cols() && U.cols() == b.size() && b.size() == x.size(),
824 "backward_substitution: dimension mismatch");
825 int n = U.rows(), i, j;
828 x(n - 1) = b(n - 1) / U(n - 1, n - 1);
829 for (i = n - 2; i >= 0; i--) {
833 for (j = i + 1; j < n; j++) {
834 temp += U._elem(i, j) * x(j);
837 x(i) = (b(i) - temp) / U._elem(i, i);
852 it_assert(U.rows() == U.cols() && U.cols() == b.size() && b.size() == x.size() && q <= U.rows() / 2,
853 "backward_substitution: dimension mismatch");
854 int n = U.rows(), i, j;
858 for (j = n - 1; j >= 0; j--) {
860 for (i =
std::max(0, j - q); i < j; i++) {
861 x(i) -= U(i, j) * x(j);