Blame view

fvn_sparse/UMFPACK/Source/umf_utsolve.c 9.07 KB
422234dc3   daniau   git-svn-id: https...
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
  /* ========================================================================== */
  /* === UMF_utsolve ========================================================== */
  /* ========================================================================== */
  
  /* -------------------------------------------------------------------------- */
  /* UMFPACK Copyright (c) Timothy A. Davis, CISE,                              */
  /* Univ. of Florida.  All Rights Reserved.  See ../Doc/License for License.   */
  /* web: http://www.cise.ufl.edu/research/sparse/umfpack                       */
  /* -------------------------------------------------------------------------- */
  
  /*  solves U'x = b or U.'x=b, where U is the upper triangular factor of a */
  /*  matrix.  B is overwritten with the solution X. */
  /*  Returns the floating point operation count */
  
  #include "umf_internal.h"
  
  GLOBAL double
  #ifdef CONJUGATE_SOLVE
  UMF_uhsolve			/* solve U'x=b  (complex conjugate transpose) */
  #else
  UMF_utsolve			/* solve U.'x=b (array transpose) */
  #endif
  (
      NumericType *Numeric,
      Entry X [ ],		/* b on input, solution x on output */
      Int Pattern [ ]		/* a work array of size n */
  )
  {
      /* ---------------------------------------------------------------------- */
      /* local variables */
      /* ---------------------------------------------------------------------- */
  
      Entry xk ;
      Entry *xp, *D, *Uval ;
      Int k, deg, j, *ip, col, *Upos, *Uilen, kstart, kend, up,
  	*Uip, n, uhead, ulen, pos, npiv, n1, *Ui ;
  
      /* ---------------------------------------------------------------------- */
      /* get parameters */
      /* ---------------------------------------------------------------------- */
  
      if (Numeric->n_row != Numeric->n_col) return (0.) ;
      n = Numeric->n_row ;
      npiv = Numeric->npiv ;
      Upos = Numeric->Upos ;
      Uilen = Numeric->Uilen ;
      Uip = Numeric->Uip ;
      D = Numeric->D ;
      kend = 0 ;
      n1 = Numeric->n1 ;
  
  #ifndef NDEBUG
      DEBUG4 (("Utsolve start: npiv "ID" n "ID"
  ", npiv, n)) ;
      for (j = 0 ; j < n ; j++)
      {
  	DEBUG4 (("Utsolve start "ID": ", j)) ;
  	EDEBUG4 (X [j]) ;
  	DEBUG4 (("
  ")) ;
      }
  #endif
  
      /* ---------------------------------------------------------------------- */
      /* singletons */
      /* ---------------------------------------------------------------------- */
  
      for (k = 0 ; k < n1 ; k++)
      {
  	DEBUG4 (("Singleton k "ID"
  ", k)) ;
  
  #ifndef NO_DIVIDE_BY_ZERO
  	/* Go ahead and divide by zero if D [k] is zero. */
  #ifdef CONJUGATE_SOLVE
  	/* xk = X [k] / conjugate (D [k]) ; */
  	DIV_CONJ (xk, X [k], D [k]) ;
  #else
  	/* xk = X [k] / D [k] ; */
  	DIV (xk, X [k], D [k]) ;
  #endif
  #else
  	/* Do not divide by zero */
  	if (IS_NONZERO (D [k]))
  	{
  #ifdef CONJUGATE_SOLVE
  	    /* xk = X [k] / conjugate (D [k]) ; */
  	    DIV_CONJ (xk, X [k], D [k]) ;
  #else
  	    /* xk = X [k] / D [k] ; */
  	    DIV (xk, X [k], D [k]) ;
  #endif
  	}
  #endif
  
  	X [k] = xk ;
  	deg = Uilen [k] ;
  	if (deg > 0 && IS_NONZERO (xk))
  	{
  	    up = Uip [k] ;
  	    Ui = (Int *) (Numeric->Memory + up) ;
  	    up += UNITS (Int, deg) ;
  	    Uval = (Entry *) (Numeric->Memory + up) ;
  	    for (j = 0 ; j < deg ; j++)
  	    {
  		DEBUG4 (("  k "ID" col "ID" value", k, Ui [j])) ;
  		EDEBUG4 (Uval [j]) ;
  		DEBUG4 (("
  ")) ;
  #ifdef CONJUGATE_SOLVE
  		/* X [Ui [j]] -= xk * conjugate (Uval [j]) ; */
  		MULT_SUB_CONJ (X [Ui [j]], xk, Uval [j]) ;
  #else
  		/* X [Ui [j]] -= xk * Uval [j] ; */
  		MULT_SUB (X [Ui [j]], xk, Uval [j]) ;
  #endif
  	    }
  	}
      }
  
      /* ---------------------------------------------------------------------- */
      /* nonsingletons */
      /* ---------------------------------------------------------------------- */
  
      for (kstart = n1 ; kstart < npiv ; kstart = kend + 1)
      {
  
  	/* ------------------------------------------------------------------ */
  	/* find the end of this Uchain */
  	/* ------------------------------------------------------------------ */
  
  	DEBUG4 (("kstart "ID" kend "ID"
  ", kstart, kend)) ;
  	/* for (kend = kstart ; kend < npiv && Uip [kend+1] > 0 ; kend++) ; */
  	kend = kstart ;
  	while (kend < npiv && Uip [kend+1] > 0)
  	{
  	    kend++ ;
  	}
  
  	/* ------------------------------------------------------------------ */
  	/* scan the whole Uchain to find the pattern of the first row of U */
  	/* ------------------------------------------------------------------ */
  
  	k = kend+1 ;
  	DEBUG4 (("
  Kend "ID" K "ID"
  ", kend, k)) ;
  
  	/* ------------------------------------------------------------------ */
  	/* start with last row in Uchain of U in Pattern [0..deg-1] */
  	/* ------------------------------------------------------------------ */
  
  	if (k == npiv)
  	{
  	    deg = Numeric->ulen ;
  	    if (deg > 0)
  	    {
  		/* :: make last pivot row of U (singular matrices only) :: */
  		for (j = 0 ; j < deg ; j++)
  		{
  		    Pattern [j] = Numeric->Upattern [j] ;
  		}
  	    }
  	}
  	else
  	{
  	    ASSERT (k >= 0 && k < npiv) ;
  	    up = -Uip [k] ;
  	    ASSERT (up > 0) ;
  	    deg = Uilen [k] ;
  	    DEBUG4 (("end of chain for row of U "ID" deg "ID"
  ", k-1, deg)) ;
  	    ip = (Int *) (Numeric->Memory + up) ;
  	    for (j = 0 ; j < deg ; j++)
  	    {
  		col = *ip++ ;
  		DEBUG4 (("  k "ID" col "ID"
  ", k-1, col)) ;
  		ASSERT (k <= col) ;
  		Pattern [j] = col ;
  	    }
  	}
  
  	/* empty the stack at the bottom of Pattern */
  	uhead = n ;
  
  	for (k = kend ; k > kstart ; k--)
  	{
  	    /* Pattern [0..deg-1] is the pattern of row k of U */
  
  	    /* -------------------------------------------------------------- */
  	    /* make row k-1 of U in Pattern [0..deg-1] */
  	    /* -------------------------------------------------------------- */
  
  	    ASSERT (k >= 0 && k < npiv) ;
  	    ulen = Uilen [k] ;
  	    /* delete, and push on the stack */
  	    for (j = 0 ; j < ulen ; j++)
  	    {
  		ASSERT (uhead >= deg) ;
  		Pattern [--uhead] = Pattern [--deg] ;
  	    }
  	    DEBUG4 (("middle of chain for row of U "ID" deg "ID"
  ", k, deg)) ;
  	    ASSERT (deg >= 0) ;
  
  	    pos = Upos [k] ;
  	    if (pos != EMPTY)
  	    {
  		/* add the pivot column */
  		DEBUG4 (("k "ID" add pivot entry at position "ID"
  ", k, pos)) ;
  		ASSERT (pos >= 0 && pos <= deg) ;
  		Pattern [deg++] = Pattern [pos] ;
  		Pattern [pos] = k ;
  	    }
  	}
  
  	/* Pattern [0..deg-1] is now the pattern of the first row in Uchain */
  
  	/* ------------------------------------------------------------------ */
  	/* solve using this Uchain, in reverse order */
  	/* ------------------------------------------------------------------ */
  
  	DEBUG4 (("Unwinding Uchain
  ")) ;
  	for (k = kstart ; k <= kend ; k++)
  	{
  
  	    /* -------------------------------------------------------------- */
  	    /* construct row k */
  	    /* -------------------------------------------------------------- */
  
  	    ASSERT (k >= 0 && k < npiv) ;
  	    pos = Upos [k] ;
  	    if (pos != EMPTY)
  	    {
  		/* remove the pivot column */
  		DEBUG4 (("k "ID" add pivot entry at position "ID"
  ", k, pos)) ;
  		ASSERT (k > kstart) ;
  		ASSERT (pos >= 0 && pos < deg) ;
  		ASSERT (Pattern [pos] == k) ;
  		Pattern [pos] = Pattern [--deg] ;
  	    }
  
  	    up = Uip [k] ;
  	    ulen = Uilen [k] ;
  	    if (k > kstart)
  	    {
  		/* concatenate the deleted pattern; pop from the stack */
  		for (j = 0 ; j < ulen ; j++)
  		{
  		    ASSERT (deg <= uhead && uhead < n) ;
  		    Pattern [deg++] = Pattern [uhead++] ;
  		}
  		DEBUG4 (("middle of chain, row of U "ID" deg "ID"
  ", k, deg)) ;
  		ASSERT (deg >= 0) ;
  	    }
  
  	    /* -------------------------------------------------------------- */
  	    /* use row k of U */
  	    /* -------------------------------------------------------------- */
  
  #ifndef NO_DIVIDE_BY_ZERO
  	    /* Go ahead and divide by zero if D [k] is zero. */
  #ifdef CONJUGATE_SOLVE
  	    /* xk = X [k] / conjugate (D [k]) ; */
  	    DIV_CONJ (xk, X [k], D [k]) ;
  #else
  	    /* xk = X [k] / D [k] ; */
  	    DIV (xk, X [k], D [k]) ;
  #endif
  #else
  	    /* Do not divide by zero */
  	    if (IS_NONZERO (D [k]))
  	    {
  #ifdef CONJUGATE_SOLVE
  		/* xk = X [k] / conjugate (D [k]) ; */
  		DIV_CONJ (xk, X [k], D [k]) ;
  #else
  		/* xk = X [k] / D [k] ; */
  		DIV (xk, X [k], D [k]) ;
  #endif
  	    }
  #endif
  
  	    X [k] = xk ;
  	    if (IS_NONZERO (xk))
  	    {
  		if (k == kstart)
  		{
  		    up = -up ;
  		    xp = (Entry *) (Numeric->Memory + up + UNITS (Int, ulen)) ;
  		}
  		else
  		{
  		    xp = (Entry *) (Numeric->Memory + up) ;
  		}
  		for (j = 0 ; j < deg ; j++)
  		{
  		    DEBUG4 (("  k "ID" col "ID" value", k, Pattern [j])) ;
  		    EDEBUG4 (*xp) ;
  		    DEBUG4 (("
  ")) ;
  #ifdef CONJUGATE_SOLVE
  		    /* X [Pattern [j]] -= xk * conjugate (*xp) ; */
  		    MULT_SUB_CONJ (X [Pattern [j]], xk, *xp) ;
  #else
  		    /* X [Pattern [j]] -= xk * (*xp) ; */
  		    MULT_SUB (X [Pattern [j]], xk, *xp) ;
  #endif
  		    xp++ ;
  		}
  	    }
  	}
  	ASSERT (uhead == n) ;
      }
  
  #ifndef NO_DIVIDE_BY_ZERO
      for (k = npiv ; k < n ; k++)
      {
  	/* This is an *** intentional *** divide-by-zero, to get Inf or Nan,
  	 * as appropriate.  It is not a bug. */
  	ASSERT (IS_ZERO (D [k])) ;
  	/* For conjugate solve, D [k] == conjugate (D [k]), in this case */
  	/* xk = X [k] / D [k] ; */
  	DIV (xk, X [k], D [k]) ;
  	X [k] = xk ;
      }
  #endif
  
  #ifndef NDEBUG
      for (j = 0 ; j < n ; j++)
      {
  	DEBUG4 (("Utsolve done "ID": ", j)) ;
  	EDEBUG4 (X [j]) ;
  	DEBUG4 (("
  ")) ;
      }
      DEBUG4 (("Utsolve done.
  ")) ;
  #endif
  
      return (DIV_FLOPS * ((double) n) + MULTSUB_FLOPS * ((double) Numeric->unz));
  }