You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "common.h"
  10. #include <Eigen/LU>
  11. // computes an LU factorization of a general M-by-N matrix A using partial pivoting with row interchanges
  12. EIGEN_LAPACK_FUNC(getrf,(int *m, int *n, RealScalar *pa, int *lda, int *ipiv, int *info))
  13. {
  14. *info = 0;
  15. if(*m<0) *info = -1;
  16. else if(*n<0) *info = -2;
  17. else if(*lda<std::max(1,*m)) *info = -4;
  18. if(*info!=0)
  19. {
  20. int e = -*info;
  21. return xerbla_(SCALAR_SUFFIX_UP"GETRF", &e, 6);
  22. }
  23. if(*m==0 || *n==0)
  24. return 0;
  25. Scalar* a = reinterpret_cast<Scalar*>(pa);
  26. int nb_transpositions;
  27. int ret = Eigen::internal::partial_lu_impl<Scalar,ColMajor,int>
  28. ::blocked_lu(*m, *n, a, *lda, ipiv, nb_transpositions);
  29. for(int i=0; i<std::min(*m,*n); ++i)
  30. ipiv[i]++;
  31. if(ret>=0)
  32. *info = ret+1;
  33. return 0;
  34. }
  35. //GETRS solves a system of linear equations
  36. // A * X = B or A' * X = B
  37. // with a general N-by-N matrix A using the LU factorization computed by GETRF
  38. EIGEN_LAPACK_FUNC(getrs,(char *trans, int *n, int *nrhs, RealScalar *pa, int *lda, int *ipiv, RealScalar *pb, int *ldb, int *info))
  39. {
  40. *info = 0;
  41. if(OP(*trans)==INVALID) *info = -1;
  42. else if(*n<0) *info = -2;
  43. else if(*nrhs<0) *info = -3;
  44. else if(*lda<std::max(1,*n)) *info = -5;
  45. else if(*ldb<std::max(1,*n)) *info = -8;
  46. if(*info!=0)
  47. {
  48. int e = -*info;
  49. return xerbla_(SCALAR_SUFFIX_UP"GETRS", &e, 6);
  50. }
  51. Scalar* a = reinterpret_cast<Scalar*>(pa);
  52. Scalar* b = reinterpret_cast<Scalar*>(pb);
  53. MatrixType lu(a,*n,*n,*lda);
  54. MatrixType B(b,*n,*nrhs,*ldb);
  55. for(int i=0; i<*n; ++i)
  56. ipiv[i]--;
  57. if(OP(*trans)==NOTR)
  58. {
  59. B = PivotsType(ipiv,*n) * B;
  60. lu.triangularView<UnitLower>().solveInPlace(B);
  61. lu.triangularView<Upper>().solveInPlace(B);
  62. }
  63. else if(OP(*trans)==TR)
  64. {
  65. lu.triangularView<Upper>().transpose().solveInPlace(B);
  66. lu.triangularView<UnitLower>().transpose().solveInPlace(B);
  67. B = PivotsType(ipiv,*n).transpose() * B;
  68. }
  69. else if(OP(*trans)==ADJ)
  70. {
  71. lu.triangularView<Upper>().adjoint().solveInPlace(B);
  72. lu.triangularView<UnitLower>().adjoint().solveInPlace(B);
  73. B = PivotsType(ipiv,*n).transpose() * B;
  74. }
  75. for(int i=0; i<*n; ++i)
  76. ipiv[i]++;
  77. return 0;
  78. }