File: lu.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (171 lines) | stat: -rw-r--r-- 4,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*!
 * \file
 * \brief Implementation of LU factorisation functions.
 * \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/lu.h>
#include <itpp/base/specmat.h>


namespace itpp {

#if defined(HAVE_LAPACK)

  bool lu(const mat &X, mat &L, mat &U, ivec &p)
  {
    it_assert_debug(X.rows() == X.cols(), "lu: matrix is not quadratic");
    //int m, n, lda, info;
    //m = n = lda = X.rows();
    int m = X.rows(), info;

    mat A(X);
    L.set_size(m, m, false);
    U.set_size(m, m, false);
    p.set_size(m, false);

    dgetrf_(&m, &m, A._data(), &m, p._data(), &info);

    for (int i=0; i<m; i++) {
      for (int j=i; j<m; j++) {
	if (i == j) { // diagonal
	  L(i,j) = 1;
	  U(i,j) = A(i,j);
	} else { // upper and lower triangular parts
	  L(i,j) = U(j,i) = 0;
	  L(j,i) = A(j,i);
	  U(i,j) = A(i,j);
	}
      }
    }

    p = p - 1; // Fortran counts from 1

    return (info==0);
  }

  // Slower than not using LAPACK when matrix size smaller than approx 20.
  bool lu(const cmat &X, cmat &L, cmat &U, ivec &p)
  {
    it_assert_debug(X.rows() == X.cols(), "lu: matrix is not quadratic");
    //int m, n, lda, info;
    //m = n = lda = X.rows();
    int m = X.rows(), info;

    cmat A(X);
    L.set_size(m, m, false);
    U.set_size(m, m, false);
    p.set_size(m, false);

    zgetrf_(&m, &m, A._data(), &m, p._data(), &info);

    for (int i=0; i<m; i++) {
      for (int j=i; j<m; j++) {
	if (i == j) { // diagonal
	  L(i,j) = 1;
	  U(i,j) = A(i,j);
	} else { // upper and lower triangular parts
	  L(i,j) = U(j,i) = 0;
	  L(j,i) = A(j,i);
	  U(i,j) = A(i,j);
	}
      }
    }

    p = p - 1; // Fortran counts from 1

    return (info==0);
  }

#else

  bool lu(const mat &X, mat &L, mat &U, ivec &p)
  {
    it_error("LAPACK library is needed to use lu() function");
    return false;
  }

  bool lu(const cmat &X, cmat &L, cmat &U, ivec &p)
  {
    it_error("LAPACK library is needed to use lu() function");
    return false;
  }

#endif // HAVE_LAPACK


  void interchange_permutations(vec &b, const ivec &p)
  {
    it_assert(b.size() == p.size(),"interchange_permutations(): dimension mismatch");
    double temp;

    for (int k=0; k<b.size(); k++) {
      temp=b(k);
      b(k)=b(p(k));
      b(p(k))=temp;
    }
  }

  bmat permutation_matrix(const ivec &p)
  {
    it_assert (p.size() > 0, "permutation_matrix(): vector must have nonzero size");
    int n = p.size(), k;
    bmat P, identity;
    bvec row_k, row_pk;
    identity=eye_b(n);


    for (k=n-1; k>=0; k--) {
      // swap rows k and p(k) in identity
      row_k=identity.get_row(k);
      row_pk=identity.get_row(p(k));
      identity.set_row(k, row_pk);
      identity.set_row(p(k), row_k);

      if (k == n-1) {
	P=identity;
      } else {
	P*=identity;
      }

      // swap back
      identity.set_row(k, row_k);
      identity.set_row(p(k), row_pk);
    }
    return P;
  }

} // namespace itpp