Mercurial > hg > octave-lyh
annotate liboctave/sparse-base-chol.h @ 10396:a0b51ac0f88a
optimize accumdim with summation
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 05 Mar 2010 12:31:30 +0100 |
parents | cbc402e64d83 |
children | 9a308e96194e |
rev | line source |
---|---|
5506 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2005, 2007, 2008 David Bateman |
7016 | 4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Andy Adler |
5 | |
6 This file is part of Octave. | |
5506 | 7 |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5506 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5506 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_sparse_base_chol_h) | |
25 #define octave_sparse_base_chol_h 1 | |
26 | |
27 #include "oct-sparse.h" | |
28 #include "dColVector.h" | |
29 | |
30 template <class chol_type, class chol_elt, class p_type> | |
31 class | |
32 sparse_base_chol | |
33 { | |
34 protected: | |
5512 | 35 #ifdef HAVE_CHOLMOD |
5506 | 36 class sparse_base_chol_rep |
37 { | |
38 public: | |
7520 | 39 sparse_base_chol_rep (void) : count (1), Lsparse (0), |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
40 is_pd (false), minor_p (0) { } |
5512 | 41 |
5506 | 42 sparse_base_chol_rep (const chol_type& a, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
43 const bool natural) : count (1) |
5506 | 44 { init (a, natural); } |
45 | |
46 sparse_base_chol_rep (const chol_type& a, octave_idx_type& info, | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 const bool natural) : count (1) |
5506 | 48 { info = init (a, natural); } |
49 | |
7637
2be056f03720
Fix fall back from sparse cholesky factorization to LU when matrix detected as not being positive definite
David Bateman <dbateman@free.fr>
parents:
7520
diff
changeset
|
50 ~sparse_base_chol_rep (void) |
2be056f03720
Fix fall back from sparse cholesky factorization to LU when matrix detected as not being positive definite
David Bateman <dbateman@free.fr>
parents:
7520
diff
changeset
|
51 { if (is_pd) CHOLMOD_NAME(free_sparse) (&Lsparse, &Common); } |
5506 | 52 |
53 cholmod_sparse * L (void) const { return Lsparse; } | |
5512 | 54 |
5506 | 55 octave_idx_type P (void) const |
56 { return (minor_p == static_cast<octave_idx_type>(Lsparse->ncol) ? | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
57 0 : minor_p + 1); } |
5506 | 58 |
59 ColumnVector perm (void) const { return perms + 1; } | |
60 | |
61 p_type Q (void) const; | |
62 | |
63 bool is_positive_definite (void) const { return is_pd; } | |
64 | |
65 double rcond (void) const { return cond; } | |
66 | |
67 int count; | |
68 | |
69 private: | |
70 cholmod_sparse *Lsparse; | |
71 | |
72 cholmod_common Common; | |
5512 | 73 |
5506 | 74 bool is_pd; |
75 | |
76 octave_idx_type minor_p; | |
77 | |
78 ColumnVector perms; | |
79 | |
80 double cond; | |
81 | |
82 octave_idx_type init (const chol_type& a, bool natural = true); | |
83 | |
84 void drop_zeros (const cholmod_sparse* S); | |
85 | |
86 // No assignment | |
87 sparse_base_chol_rep& operator = (const sparse_base_chol_rep& a); | |
88 }; | |
5512 | 89 #else |
90 class sparse_base_chol_rep | |
91 { | |
92 public: | |
93 sparse_base_chol_rep (void) : count (1), is_pd (false), minor_p (0) { } | |
94 | |
95 sparse_base_chol_rep (const chol_type& a, | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
96 const bool natural) : count (1) |
5512 | 97 { init (a, natural); } |
98 | |
99 sparse_base_chol_rep (const chol_type& a, octave_idx_type& info, | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
100 const bool natural) : count (1) |
5512 | 101 { info = init (a, natural); } |
102 | |
103 ~sparse_base_chol_rep (void) { } | |
104 | |
105 octave_idx_type P (void) const { return 0; } | |
106 | |
107 ColumnVector perm (void) const { return perms + 1; } | |
108 | |
109 p_type Q (void) const; | |
110 | |
111 bool is_positive_definite (void) const { return is_pd; } | |
112 | |
113 double rcond (void) const { return cond; } | |
114 | |
115 int count; | |
116 | |
117 private: | |
118 bool is_pd; | |
119 | |
120 octave_idx_type minor_p; | |
121 | |
122 ColumnVector perms; | |
123 | |
124 double cond; | |
125 | |
126 octave_idx_type init (const chol_type& a, bool natural = true); | |
127 | |
128 // No assignment | |
129 sparse_base_chol_rep& operator = (const sparse_base_chol_rep& a); | |
130 }; | |
131 #endif | |
5506 | 132 |
133 private: | |
134 sparse_base_chol_rep *rep; | |
135 | |
136 public: | |
137 | |
138 sparse_base_chol (void) : rep (new typename | |
139 sparse_base_chol<chol_type, chol_elt, p_type>::sparse_base_chol_rep ()) { } | |
140 | |
141 sparse_base_chol (const chol_type& a, const bool n) : rep (new typename | |
142 sparse_base_chol<chol_type, chol_elt, p_type>:: | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
143 sparse_base_chol_rep (a, n)) { } |
5506 | 144 |
145 sparse_base_chol (const chol_type& a, octave_idx_type& info, const bool n) : | |
146 rep (new typename sparse_base_chol<chol_type, chol_elt, p_type>:: | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
147 sparse_base_chol_rep (a, info, n)) { } |
5506 | 148 |
149 sparse_base_chol (const sparse_base_chol<chol_type, chol_elt, p_type>& a) : | |
150 rep (a.rep) { rep->count++; } | |
151 | |
152 ~sparse_base_chol (void) | |
153 { | |
154 if (--rep->count <= 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
155 delete rep; |
5506 | 156 } |
157 | |
158 sparse_base_chol& operator = (const sparse_base_chol& a) | |
159 { | |
160 if (this != &a) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
161 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
162 if (--rep->count <= 0) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
163 delete rep; |
5506 | 164 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
165 rep = a.rep; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
166 rep->count++; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
167 } |
5506 | 168 |
169 return *this; | |
170 } | |
171 | |
172 chol_type L (void) const; | |
173 | |
174 chol_type R (void) const { return L().hermitian (); } | |
175 | |
176 octave_idx_type P (void) const { return rep->P(); } | |
177 | |
178 ColumnVector perm (void) const { return rep->perm(); } | |
179 | |
180 p_type Q (void) const { return rep->Q(); } | |
181 | |
182 bool is_positive_definite (void) const | |
183 { return rep->is_positive_definite(); } | |
184 | |
185 double rcond (void) const { return rep->rcond(); } | |
186 | |
187 chol_type inverse (void) const; | |
188 }; | |
189 | |
190 #endif |