IT++ Logo
inv.cpp
Go to the documentation of this file.
1 
29 #ifndef _MSC_VER
30 # include <itpp/config.h>
31 #else
32 # include <itpp/config_msvc.h>
33 #endif
34 
35 #if defined(HAVE_LAPACK)
36 # include <itpp/base/algebra/lapack.h>
37 #endif
38 
39 #include <itpp/base/algebra/inv.h>
40 
41 
42 namespace itpp
43 {
44 
45 #if defined(HAVE_LAPACK)
46 
47 bool inv(const mat &X, mat &Y)
48 {
49  it_assert_debug(X.rows() == X.cols(), "inv: matrix is not square");
50 
51  int m = X.rows(), info, lwork;
52  lwork = m; // may be choosen better
53 
54  ivec p(m);
55  Y = X;
56  vec work(lwork);
57 
58  dgetrf_(&m, &m, Y._data(), &m, p._data(), &info); // LU-factorization
59  if (info != 0)
60  return false;
61 
62  dgetri_(&m, Y._data(), &m, p._data(), work._data(), &lwork, &info);
63  return (info == 0);
64 }
65 
66 bool inv(const cmat &X, cmat &Y)
67 {
68  it_assert_debug(X.rows() == X.cols(), "inv: matrix is not square");
69 
70  int m = X.rows(), info, lwork;
71  lwork = m; // may be choosen better
72 
73  ivec p(m);
74  Y = X;
75  cvec work(lwork);
76 
77  zgetrf_(&m, &m, Y._data(), &m, p._data(), &info); // LU-factorization
78  if (info != 0)
79  return false;
80 
81  zgetri_(&m, Y._data(), &m, p._data(), work._data(), &lwork, &info);
82  return (info == 0);
83 }
84 
85 #else
86 
87 bool inv(const mat &X, mat &Y)
88 {
89  it_error("LAPACK library is needed to use inv() function");
90  return false;
91 }
92 
93 bool inv(const cmat &X, cmat &Y)
94 {
95  it_error("LAPACK library is needed to use inv() function");
96  return false;
97 }
98 
99 #endif // HAVE_LAPACK
100 
101 cmat inv(const cmat &X)
102 {
103  cmat Y;
104  inv(X, Y);
105  return Y;
106 }
107 
108 
109 mat inv(const mat &X)
110 {
111  mat Y;
112  inv(X, Y);
113  return Y;
114 }
115 
116 } // namespace itpp
SourceForge Logo

Generated on Sat May 25 2013 16:32:18 for IT++ by Doxygen 1.8.2