GSparseNumeric.cpp
14.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/***************************************************************************
* GSparseNumeric.cpp - sparse matrix numeric analysis class *
* ----------------------------------------------------------------------- *
* copyright : (C) 2006 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* 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. *
* *
***************************************************************************/
/* __ Includes ___________________________________________________________ */
#include "GSparseSymbolic.hpp"
#include "GSparseNumeric.hpp"
/* __ Namespaces _________________________________________________________ */
using namespace std;
/* __ Macros _____________________________________________________________ */
#define CS_FLIP(i) (-(i)-2)
#define CS_MARK(w,j) { w [j] = CS_FLIP (w [j]) ; }
#define CS_MARKED(w,j) (w [j] < 0)
/*==========================================================================
= =
= GSparseNumeric constructors/destructors =
= =
==========================================================================*/
/***************************************************************************
* GSparseNumeric constructor *
***************************************************************************/
GSparseNumeric::GSparseNumeric()
{
// Initialise private members for clean destruction
m_L = NULL;
m_U = NULL;
m_pinv = NULL;
m_B = NULL;
m_n_pinv = 0;
m_n_B = 0;
// Return
return;
}
/***************************************************************************
* GSparseNumeric destructor *
***************************************************************************/
GSparseNumeric::~GSparseNumeric()
{
// De-allocate only if memory has indeed been allocated
if (m_L != NULL) delete m_L;
if (m_U != NULL) delete m_U;
if (m_pinv != NULL) delete [] m_pinv;
if (m_B != NULL) delete [] m_B;
// Return
return;
}
/*==========================================================================
= =
= GSparseNumeric operators =
= =
==========================================================================*/
/***************************************************************************
* GSparseNumeric assignment operator *
***************************************************************************/
GSparseNumeric& GSparseNumeric::operator= (const GSparseNumeric& n)
{
// Execute only if object is not identical
if (this != &n) {
// De-allocate only if memory has indeed been allocated
if (m_L != NULL) delete m_L;
if (m_U != NULL) delete m_U;
if (m_pinv != NULL) delete [] m_pinv;
if (m_B != NULL) delete [] m_B;
// Initialise private members for clean destruction
m_L = NULL;
m_U = NULL;
m_pinv = NULL;
m_B = NULL;
m_n_pinv = 0;
m_n_B = 0;
// Copy m_L if it exists
if (n.m_L != NULL) {
m_L = new GSparseMatrix(*n.m_L);
if (m_L == NULL)
throw mem_alloc("GSparseNumeric::operator= (const GSparseNumeric&)",
1);
}
// Copy m_U if it exists
if (n.m_U != NULL) {
m_U = new GSparseMatrix(*n.m_U);
if (m_U == NULL)
throw mem_alloc("GSparseNumeric::operator= (const GSparseNumeric&)",
1);
}
// Copy m_pinv array if it exists
if (n.m_pinv != NULL && n.m_n_pinv > 0) {
m_pinv = new int[n.m_n_pinv];
if (m_pinv == NULL)
throw mem_alloc("GSparseNumeric::operator= (const GSparseNumeric&)",
n.m_n_pinv);
for (int i = 0; i < n.m_n_pinv; ++i)
m_pinv[i] = n.m_pinv[i];
m_n_pinv = n.m_n_pinv;
}
// Copy m_B array if it exists
if (n.m_B != NULL && n.m_n_B > 0) {
m_B = new double[n.m_n_B];
if (m_B == NULL)
throw mem_alloc("GSparseNumeric::operator= (const GSparseNumeric&)",
n.m_n_B);
for (int i = 0; i < n.m_n_B; ++i)
m_B[i] = n.m_B[i];
m_n_B = n.m_n_B;
}
} // endif: object was not identical
// Return
return *this;
}
/*==========================================================================
= =
= GSparseNumeric member functions =
= =
==========================================================================*/
/***************************************************************************
* cholesky_numeric_analysis *
* ----------------------------------------------------------------------- *
* L = chol (A, [pinv parent cp]), pinv is optional. This function *
* allocates the memory to hold the information. *
* ----------------------------------------------------------------------- *
* Input: A Sparse matrix *
* S Symbolic analysis of sparse matrix *
***************************************************************************/
void GSparseNumeric::cholesky_numeric_analysis(const GSparseMatrix& A,
const GSparseSymbolic& S)
{
// De-allocate memory that has indeed been previously allocated
if (m_L != NULL) delete m_L;
if (m_U != NULL) delete m_U;
if (m_pinv != NULL) delete [] m_pinv;
if (m_B != NULL) delete [] m_B;
// Initialise members
m_L = NULL;
m_U = NULL;
m_pinv = NULL;
m_B = NULL;
m_n_pinv = 0;
m_n_B = 0;
// Return if arrays in the symbolic analysis have not been allocated
if (!S.m_cp || !S.m_parent) return;
// Declare
double lki;
int top, i, p, k;
// Assign input matrix attributes
int n = A.m_cols;
// Allocate int workspace
int wrk_size = 2*n;
int* wrk_int = new int[wrk_size];
if (wrk_int == NULL)
throw mem_alloc(
"GSparseNumeric::cholesky_numeric_analysis(GSparseMatrix&, const GSparseSymbolic&)",
wrk_size);
// Allocate double workspace
wrk_size = n;
double* wrk_double = new double[wrk_size];
if (wrk_double == NULL)
throw mem_alloc(
"GSparseNumeric::cholesky_numeric_analysis(GSparseMatrix&, const GSparseSymbolic&)",
wrk_size);
// Assign pointers
int* cp = S.m_cp;
int* pinv = S.m_pinv;
int* parent = S.m_parent;
// Assign C = A(p,p) where A and C are symmetric and the upper part stored
GSparseMatrix C = (pinv) ? cs_symperm(A, pinv) : (A);
// Assign workspace pointer
int* c = wrk_int;
int* s = wrk_int + n;
double* x = wrk_double;
// Assign C matrix pointers
int* Cp = C.m_colstart;
int* Ci = C.m_rowinx;
double* Cx = C.m_data;
// Allocate L matrix
m_L = new GSparseMatrix(n, n, cp[n]);
if (m_L == NULL)
throw mem_alloc(
"GSparseNumeric::cholesky_numeric_analysis(GSparseMatrix&, const GSparseSymbolic&)",
1);
// Assign L matrix pointers
int* Lp = m_L->m_colstart;
int* Li = m_L->m_rowinx;
double* Lx = m_L->m_data;
// Initialise column pointers of L and c
for (k = 0; k < n; ++k)
Lp[k] = c[k] = cp[k];
// Compute L(:,k) for L*L' = C
for (k = 0; k < n; k++) {
// Nonzero pattern of L(k,:).
// Returns -1 if parent = NULL, s = NULL or c = NULL
top = cs_ereach(&C, k, parent, s, c); // find pattern of L(k,:)
x[k] = 0; // x (0:k) is now zero
// x = full(triu(C(:,k)))
for (p = Cp[k]; p < Cp[k+1]; p++) {
if (Ci[p] <= k)
x[Ci[p]] = Cx[p];
}
// d = C(k,k)
double d = x[k];
// Clear x for k+1st iteration
x[k] = 0;
// Triangular solve: Solve L(0:k-1,0:k-1) * x = C(:,k)
for ( ; top < n; top++) {
i = s[top]; // s [top..n-1] is pattern of L(k,:)
lki = x[i]/Lx[Lp[i]]; // L(k,i) = x (i) / L(i,i)
x[i] = 0; // clear x for k+1st iteration
for (p = Lp[i]+1; p < c[i]; p++)
x[Li[p]] -= Lx[p] * lki;
d -= lki * lki; // d = d - L(k,i)*L(k,i)
p = c[i]++;
Li[p] = k; // store L(k,i) in column i
Lx[p] = lki;
}
// Compute L(k,k)
if (d <= 0)
throw GSparseMatrix::matrix_not_pos_definite(
"GSparseNumeric::cholesky_numeric_analysis(GSparseMatrix&, const GSparseSymbolic&)",
k, d);
// Store L(k,k) = sqrt (d) in column k
p = c[k]++;
Li[p] = k;
Lx[p] = sqrt(d);
}
// Finalize L
Lp[n] = cp[n];
// Free workspace
delete [] wrk_int;
delete [] wrk_double;
// Return void
return;
}
/*==========================================================================
= =
= GSparseNumeric private functions =
= =
==========================================================================*/
/***************************************************************************
* cs_ereach *
* ----------------------------------------------------------------------- *
* Find nonzero pattern of Cholesky L(k,1:k-1) using etree and *
* triu(A(:,k)) *
* ----------------------------------------------------------------------- *
* Input: A Sparse matrix *
* k Node *
* parent ... *
* s ... *
* w ... *
* Input: top Index, where s[top..n-1] contains pattern *
* of L(k,:); -1 if arrays have not been *
* allocated *
***************************************************************************/
int GSparseNumeric::cs_ereach(const GSparseMatrix* A, int k,
const int* parent, int* s, int* w)
{
// Return -1 if arrays have not been allocated
if (!parent || !s || !w) return (-1);
// Assign A matrix attributes and pointers
int n = A->m_cols;
int top = n;
int* Ap = A->m_colstart;
int* Ai = A->m_rowinx;
// Mark node k as visited
CS_MARK(w, k);
// Loop over elements of node
int i;
for (int p = Ap[k]; p < Ap[k+1]; ++p) {
i = Ai[p]; // A(i,k) is nonzero
if (i > k) continue; // only use upper triangular part of A
// Traverse up etree
int len;
for (len = 0; !CS_MARKED(w,i); i = parent[i]) {
s[len++] = i; // L(k,i) is nonzero
CS_MARK(w, i); // mark i as visited
}
// Push path onto stack
while (len > 0)
s[--top] = s[--len];
}
// Unmark all nodes
for (int p = top; p < n; ++p)
CS_MARK(w, s[p]);
// Unmark node k
CS_MARK(w, k);
// Return index top, where s[top..n-1] contains pattern of L(k,:)
return top;
}
/*==========================================================================
= =
= GSparseNumeric static member functions =
= =
==========================================================================*/
/*==========================================================================
= =
= GSparseNumeric friends =
= =
==========================================================================*/
/***************************************************************************
* Output operator *
***************************************************************************/
ostream& operator<< (ostream& os, const GSparseNumeric& n)
{
// Put header is stream
os << "=== GSparseNumeric ===";
// Show L or V matrix
if (n.m_L != NULL) {
os << endl <<
" === L or V ";
os << *n.m_L << endl;
}
// Show U or R matrix
if (n.m_U != NULL) {
os << endl <<
" === U or R matrix ";
os << *n.m_U << endl;
}
// Show inverse permutation if it exists
if (n.m_pinv != NULL) {
os << endl << " Inverse permutation (of " << n.m_n_pinv << " elements)" << endl;
for (int i = 0; i < n.m_n_pinv; ++i)
os << " " << n.m_pinv[i];
}
// Show beta array if it exists
if (n.m_B != NULL) {
os << endl << " Beta[0.." << n.m_n_B << "]" << endl;
for (int i = 0; i < n.m_n_B; ++i)
os << " " << n.m_B[i];
}
// Return output stream
return os;
}
/*==========================================================================
= =
= GSparseNumeric exception classes =
= =
==========================================================================*/