⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ls_solve.cpp

📁 强大的C++库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*! * \file * \brief Implementation of functions for solving linear equation systems * \author Tony Ottosson * * ------------------------------------------------------------------------- * * IT++ - C++ library of mathematical, signal processing, speech processing, *        and communications classes and functions * * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * ------------------------------------------------------------------------- */#ifndef _MSC_VER#  include <itpp/config.h>#else#  include <itpp/config_msvc.h>#endif#if defined(HAVE_LAPACK)#  include <itpp/base/algebra/lapack.h>#endif#include <itpp/base/algebra/ls_solve.h>namespace itpp {  // ----------- ls_solve_chol -----------------------------------------------------------#if defined(HAVE_LAPACK)  bool ls_solve_chol(const mat &A, const vec &b, vec &x)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = 1;    char uplo='U';    it_assert_debug(A.cols() == n, "ls_solve_chol: System-matrix is not square");    it_assert_debug(n == b.size(), "The number of rows in A must equal the length of b!");    ivec ipiv(n);    x = b;    mat Chol = A;    dposv_(&uplo, &n, &nrhs, Chol._data(), &lda, x._data(), &ldb, &info);    return (info==0);  }  bool ls_solve_chol(const mat &A, const mat &B, mat &X)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = B.cols();    char uplo='U';    it_assert_debug(A.cols() == n, "ls_solve_chol: System-matrix is not square");    it_assert_debug(n == B.rows(), "The number of rows in A must equal the length of B!");    ivec ipiv(n);    X = B;    mat Chol = A;    dposv_(&uplo, &n, &nrhs, Chol._data(), &lda, X._data(), &ldb, &info);    return (info==0);  }  bool ls_solve_chol(const cmat &A, const cvec &b, cvec &x)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = 1;    char uplo='U';    it_assert_debug(A.cols() == n, "ls_solve_chol: System-matrix is not square");    it_assert_debug(n == b.size(), "The number of rows in A must equal the length of b!");    ivec ipiv(n);    x = b;    cmat Chol = A;    zposv_(&uplo, &n, &nrhs, Chol._data(), &lda, x._data(), &ldb, &info);    return (info==0);  }  bool ls_solve_chol(const cmat &A, const cmat &B, cmat &X)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = B.cols();    char uplo='U';    it_assert_debug(A.cols() == n, "ls_solve_chol: System-matrix is not square");    it_assert_debug(n == B.rows(), "The number of rows in A must equal the length of B!");    ivec ipiv(n);    X = B;    cmat Chol = A;    zposv_(&uplo, &n, &nrhs, Chol._data(), &lda, X._data(), &ldb, &info);    return (info==0);  }#else  bool ls_solve_chol(const mat &A, const vec &b, vec &x)  {    it_error("LAPACK library is needed to use ls_solve_chol() function");    return false;  }  bool ls_solve_chol(const mat &A, const mat &B, mat &X)  {    it_error("LAPACK library is needed to use ls_solve_chol() function");    return false;  }  bool ls_solve_chol(const cmat &A, const cvec &b, cvec &x)  {    it_error("LAPACK library is needed to use ls_solve_chol() function");    return false;  }  bool ls_solve_chol(const cmat &A, const cmat &B, cmat &X)  {    it_error("LAPACK library is needed to use ls_solve_chol() function");    return false;  }#endif // HAVE_LAPACK  vec ls_solve_chol(const mat &A, const vec &b)  {    vec x;    bool info;    info = ls_solve_chol(A, b, x);    it_assert_debug(info, "ls_solve_chol: Failed solving the system");    return x;  }  mat ls_solve_chol(const mat &A, const mat &B)  {    mat X;    bool info;    info = ls_solve_chol(A, B, X);    it_assert_debug(info, "ls_solve_chol: Failed solving the system");    return X;  }  cvec ls_solve_chol(const cmat &A, const cvec &b)  {    cvec x;    bool info;    info = ls_solve_chol(A, b, x);    it_assert_debug(info, "ls_solve_chol: Failed solving the system");    return x;  }  cmat ls_solve_chol(const cmat &A, const cmat &B)  {    cmat X;    bool info;    info = ls_solve_chol(A, B, X);    it_assert_debug(info, "ls_solve_chol: Failed solving the system");    return X;  }  // --------- ls_solve ---------------------------------------------------------------#if defined(HAVE_LAPACK)  bool ls_solve(const mat &A, const vec &b, vec &x)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = 1;    it_assert_debug(A.cols() == n, "ls_solve: System-matrix is not square");    it_assert_debug(n == b.size(), "The number of rows in A must equal the length of b!");    ivec ipiv(n);    x = b;    mat LU = A;    dgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), x._data(), &ldb, &info);    return (info==0);  }  bool ls_solve(const mat &A, const mat &B, mat &X)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = B.cols();    it_assert_debug(A.cols() == n, "ls_solve: System-matrix is not square");    it_assert_debug(n == B.rows(), "The number of rows in A must equal the length of B!");    ivec ipiv(n);    X = B;    mat LU = A;    dgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), X._data(), &ldb, &info);    return (info==0);  }  bool ls_solve(const cmat &A, const cvec &b, cvec &x)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = 1;    it_assert_debug(A.cols() == n, "ls_solve: System-matrix is not square");    it_assert_debug(n == b.size(), "The number of rows in A must equal the length of b!");    ivec ipiv(n);    x = b;    cmat LU = A;    zgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), x._data(), &ldb, &info);    return (info==0);  }  bool ls_solve(const cmat &A, const cmat &B, cmat &X)  {    int n, lda, ldb, nrhs, info;    n = lda = ldb = A.rows();    nrhs = B.cols();    it_assert_debug(A.cols() == n, "ls_solve: System-matrix is not square");    it_assert_debug(n == B.rows(), "The number of rows in A must equal the length of B!");    ivec ipiv(n);    X = B;    cmat LU = A;    zgesv_(&n, &nrhs, LU._data(), &lda, ipiv._data(), X._data(), &ldb, &info);    return (info==0);  }#else  bool ls_solve(const mat &A, const vec &b, vec &x)  {    it_error("LAPACK library is needed to use ls_solve() function");    return false;  }  bool ls_solve(const mat &A, const mat &B, mat &X)  {    it_error("LAPACK library is needed to use ls_solve() function");    return false;  }  bool ls_solve(const cmat &A, const cvec &b, cvec &x)  {    it_error("LAPACK library is needed to use ls_solve() function");    return false;  }  bool ls_solve(const cmat &A, const cmat &B, cmat &X)  {    it_error("LAPACK library is needed to use ls_solve() function");    return false;  }#endif // HAVE_LAPACK  vec ls_solve(const mat &A, const vec &b)  {    vec x;    bool info;    info = ls_solve(A, b, x);    it_assert_debug(info, "ls_solve: Failed solving the system");    return x;  }  mat ls_solve(const mat &A, const mat &B)  {    mat X;    bool info;    info = ls_solve(A, B, X);    it_assert_debug(info, "ls_solve: Failed solving the system");    return X;  }  cvec ls_solve(const cmat &A, const cvec &b)  {    cvec x;    bool info;    info = ls_solve(A, b, x);    it_assert_debug(info, "ls_solve: Failed solving the system");    return x;  }  cmat ls_solve(const cmat &A, const cmat &B)  {    cmat X;    bool info;    info = ls_solve(A, B, X);    it_assert_debug(info, "ls_solve: Failed solving the system");    return X;  }  // ----------------- ls_solve_od ------------------------------------------------------------------#if defined(HAVE_LAPACK)  bool ls_solve_od(const mat &A, const vec &b, vec &x)  {    int m, n, lda, ldb, nrhs, lwork, info;    char trans='N';    m = lda = ldb = A.rows();    n = A.cols();    nrhs = 1;    lwork = n + std::max(m,nrhs);    it_assert_debug(m >= n, "The system is under-determined!");    it_assert_debug(m == b.size(), "The number of rows in A must equal the length of b!");    vec work(lwork);    x = b;    mat QR = A;    dgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, x._data(), &ldb, work._data(), &lwork, &info);    x.set_size(n, true);    return (info==0);  }  bool ls_solve_od(const mat &A, const mat &B, mat &X)  {    int m, n, lda, ldb, nrhs, lwork, info;    char trans='N';    m = lda = ldb = A.rows();    n = A.cols();    nrhs = B.cols();    lwork = n + std::max(m,nrhs);    it_assert_debug(m >= n, "The system is under-determined!");    it_assert_debug(m == B.rows(), "The number of rows in A must equal the length of b!");    vec work(lwork);    X = B;    mat QR = A;    dgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, X._data(), &ldb, work._data(), &lwork, &info);    X.set_size(n, nrhs, true);    return (info==0);  }  bool ls_solve_od(const cmat &A, const cvec &b, cvec &x)  {    int m, n, lda, ldb, nrhs, lwork, info;    char trans='N';    m = lda = ldb = A.rows();    n = A.cols();    nrhs = 1;    lwork = n + std::max(m,nrhs);    it_assert_debug(m >= n, "The system is under-determined!");    it_assert_debug(m == b.size(), "The number of rows in A must equal the length of b!");    cvec work(lwork);    x = b;    cmat QR = A;    zgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, x._data(), &ldb, work._data(), &lwork, &info);    x.set_size(n, true);    return (info==0);  }  bool ls_solve_od(const cmat &A, const cmat &B, cmat &X)  {    int m, n, lda, ldb, nrhs, lwork, info;    char trans='N';    m = lda = ldb = A.rows();    n = A.cols();    nrhs = B.cols();    lwork = n + std::max(m,nrhs);    it_assert_debug(m >= n, "The system is under-determined!");    it_assert_debug(m == B.rows(), "The number of rows in A must equal the length of b!");    cvec work(lwork);    X = B;    cmat QR = A;    zgels_(&trans, &m, &n, &nrhs, QR._data(), &lda, X._data(), &ldb, work._data(), &lwork, &info);    X.set_size(n, nrhs, true);    return (info==0);  }#else  bool ls_solve_od(const mat &A, const vec &b, vec &x)  {    it_error("LAPACK library is needed to use ls_solve_od() function");    return false;  }  bool ls_solve_od(const mat &A, const mat &B, mat &X)  {    it_error("LAPACK library is needed to use ls_solve_od() function");    return false;  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -