File: lu.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (380 lines) | stat: -rw-r--r-- 7,327 bytes parent folder | download | duplicates (2)
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

/* README : 
 * The routines in this file use a pointer to a Matrix 
 * at the Fortran Level this pointer is stored as an integer 
 * so we cast  pointer to long int 
 * f_(fmat) 
 *  long *fmat 
 *  *fmat = (long)spCreate(*n,0,&error);
 * 
 * At fortran level the integer must be an integer *4 
 * in order to store a C long 
 * since we  are in scilab 
 * the pointer transmitted to f_ is an istk(il1) it can in fact contain 
 * something as long as a double 
 * Copyright ENPC (Chancelier)
 */


/*
 *  IMPORTS
 *
 *  >>> Import descriptions:
 *  spConfig.h
 *     Macros that customize the sparse matrix routines.
 *  spmatrix.h
 *     Macros and declarations to be imported by the user.
 *  spDefs.h
 *     Matrix type and macro definitions for the sparse matrix routines.
 */

#define spINSIDE_SPARSE
#include "spConfig.h"
#include "spmatrix.h"
#include "spDefs.h"


#include "../machine.h"

/*
 *
 * lufact1  >>> Creation and LU factorisation of a sparse matrix 
 * Entry << 
 *   val,rc 
 *       arrays of size k and kx2 a(rc(i),rc(*k+i))=val(i)
 *       for i=0,(*k-1)
 *   *n : size of the created square matrix 
 *   *k : number of given values 
 *   *eps : The machine precision number 
 *  Return >> 
 *   fmat : pointer to a long int which is a cast of an adress
 *   *nrank : The numerical rank 
 */


static void 
spFixThresold(eMatrix,eps,releps)
char *eMatrix;
double eps,releps;
{
  MatrixPtr  Matrix = (MatrixPtr)eMatrix;
  Matrix->AbsThreshold = eps;
  Matrix->RelThreshold = releps;
}


static void 
spGetNumRank(eMatrix,n)
char *eMatrix;
int *n;
{
  MatrixPtr  Matrix = (MatrixPtr)eMatrix;
  *n = Matrix->NumRank;
}

extern void cerro();

void 
C2F(lufact1)(val,lln,col,n,nel,fmat,eps,releps,nrank,ierr)
double *val,*eps,*releps;
long *fmat;
int *n,*nel,*nrank,*lln,*col,*ierr;
{
  int error,i,i0,i1,k,j;
  spREAL *pelement;
  *ierr = 0;
  *fmat = (long)spCreate(*n,0,&error);
    if (error != spOKAY) {
        *ierr = 1;
        return;
    }


  i0=0;
  i1=i0;
  i=1;
  for (k = 0 ;k < *nel; k++) {
      i0=i0+1;
      while (i0-i1 > lln[i-1]) {
	  i1=i0;
          i=i+1;
          i0=i0+1;
      }
      j=col[k];
      pelement = spGetElement((char*) *fmat,i,j);
      if (pelement == 0) {
          *ierr=2;
          return;
      }
      spADD_REAL_ELEMENT(pelement,(spREAL)(val[k]));

  }
  /* Fix the AbsThresold with scilex %eps */
  spFixThresold((char*) *fmat,*eps,*releps);
  /* spPrint((char *) *fmat,1,1,1); */
  error = spFactor((char*) *fmat);
  spGetNumRank((char *) *fmat,nrank);
  switch (error) {
  case spZERO_DIAG:
    cerro("zero_diag: A zero was encountered on the diagonal the matrix ");
    break;
  case spNO_MEMORY:
    *ierr=3;
    break;
  case spSINGULAR:
    *ierr=-1; /*Singular matrix" */
    break;
  case spSMALL_PIVOT:
    *ierr=-2; /* matrix is singular at precision level */
    break;
  }
}

/*
 * lusolve1  >>> Solves fmat*x=b 
 *   *fmat : a pointer to the sparse matrix factored by lufact 
 *   b,v 
 *      two arrays of size n the matrix size 
 */
extern void Cout(char *str);

static int 
spSolveCheck(eMatrix)
char *eMatrix;
{
  MatrixPtr Matrix = (MatrixPtr) eMatrix;
  if (Matrix->Error == spSINGULAR OR Matrix->Error == spSMALL_PIVOT )
    {      
	Cout("A matrix is singular or ill conditioned ");
	Cout("result will be fine only if b is in Im(A)");
    }
  return(1);
}

void C2F(lusolve1)(fmat,b,x)
double *b, *x;
long *fmat;
{
/*  if (spSolveCheck((char *) *fmat) != 0)*/
    spSolve((char*) *fmat,(spREAL*)b,(spREAL*)x);
}

/*
 * ludel1  >>> delete sparse matrix 
 *   *fmat : a pointer to the sparse matrix factored by lufact 
 */

void C2F(ludel1)(fmat)
long *fmat;
{
  spDestroy((char*) *fmat);
}

/*
 * lusize  >>> returns in n the size of the sparse matrix 
 *   *fmat : a pointer to the sparse matrix factored by lufact 
 */

static void 
spSize(eMatrix,n)
char *eMatrix;
int *n;
{
  MatrixPtr  Matrix = (MatrixPtr)eMatrix;
  *n=Matrix->Size;
}

void C2F(lusize)(fmat,n)
long *fmat;
int *n;
{
  spSize((char *) *fmat,n);
}

/*
 * luget1   >>> extract the LU coded matrix into a full array 
 *   sigg,sigd :
 *     two arrays of size n which code permutations 
 *   lu :  
 *     an array coded matrix of size nxn where lu will be stored 
 */


/* filling right permutation */
GetSigD(Matrix,indsigd,sigd)
MatrixPtr Matrix;
double sigd[];
int indsigd[];
{
  int I,J,mc=0,last=0;
  int Size=Matrix->Size;
for (I = 1; I <= Size; I++)
  {
    indsigd[I-1]=1;
    indsigd[Size+I-1]=  Matrix->IntToExtColMap[I];
    sigd[I-1]=1.0;
  }
/* counting missing colums */
for (I = 1; I <= Size; I++) 
  if (Matrix->ExtToIntColMap[I]== -1) mc++;
/* filling missing colums */
if (mc != 0)
  {
    for (I = Size -(mc)+1  ; I <= Size; I++)
      {
	for ( J=last+1; J <=Size; J++)
	  {
	    if (Matrix->ExtToIntColMap[J]==-1) 
	      { 
		last=J;break;
	      }
	  }
	indsigd[I-1]=1;
	indsigd[Size+I-1]= last;
      }
  }
}


/* filling left permutation */

GetSigG(Matrix,indsigg,sigg)
MatrixPtr Matrix;
double sigg[];
int indsigg[];
{
  int Size=Matrix->Size;
  int I,J,mc=0,last=0;
  /* counting missing Rows*/
  for (I = 1; I <= Size; I++) 
    if (Matrix->ExtToIntRowMap[I]== -1) mc++;

  for (I = 1; I <= Size-mc ; I++)
  {
	indsigg[I-1]= 1;
	indsigg[Size+Matrix->IntToExtRowMap[I]-1]=I;
	sigg[I-1]=1;
  }
/* filling missing Rows */
if (mc != 0)
  {
    for (I = Size -(mc)+1  ; I <= Size; I++)
      {
	for ( J=last+1; J <=Size; J++)
	  {
	    if (Matrix->ExtToIntRowMap[J]==-1) 
	      { 

		last=J;break;
	      }
	  }
	indsigg[I-1]= 1;
	indsigg[Size+last-1]=I;
	sigg[I-1]=1;
      }
  }
}


static void 
spLuget(eMatrix,indP,P,indl,l,indu,u,indQ,Q)
char *eMatrix;
int *indP,*indl,*indu,*indQ;
double *P,*Q,*l,*u;
{
int I,J;
int lsize,usize;

MatrixPtr Matrix = (MatrixPtr) eMatrix;
ElementPtr  pElement;
int Size;
Size = Matrix->Size;
GetSigD(Matrix,indQ,Q);
GetSigG(Matrix,indP,P);
for (J = 1; J <= Size ; J++)
  {
    indl[J-1] = 0;
    indu[J-1] = 0;
}
lsize=0;
usize=0;
for (I = 1; I <= Size ; I++)
  {
    indu[I-1]=indu[I-1]+1;
    indu[Size+usize]=I;
    u[usize]=1.0;
    usize=usize+1;

    pElement = Matrix->FirstInRow[I];
    while ( pElement != NULL )
      {
	  J = pElement->Col;
	  if (I >= J) {
	      indl[I-1] = indl[I-1]+1;
	      indl[Size+lsize]=J;
	      l[lsize]=(double) pElement->Real ;
	      lsize=lsize+1;

	  }
	  else {
	      indu[I-1] = indu[I-1]+1;
	      indu[Size+usize]=J;
	      u[usize]=(double) pElement->Real ;
	      usize=usize+1;

	  }
	pElement = pElement->NextInRow;
      };
  };
}





void C2F(luget1)(fmat,indP,P,indl,l,indu,u,indQ,Q)
long *fmat;
double *P,*Q,*l,*u;
int *indP,*indl,*indu,*indQ;
{
  spLuget((char *) *fmat,indP,P,indl,l,indu,u,indQ,Q);
}


/*
 * lusiz1   >>> extract the L and U  number of non zero elements
 * lsize and usize
 */


static void 
spLusiz(eMatrix,lsize,usize)
char *eMatrix;
int *lsize,*usize;
{
int J;
MatrixPtr Matrix = (MatrixPtr) eMatrix;
ElementPtr  pElement;
int Size;
Size = Matrix->Size;
*lsize=0;
*usize=Size;
for (J = 1; J <= Size ; J++)
  {
    pElement = Matrix->FirstInCol[J];
    while ( pElement != NULL )
      {
	if (pElement->Row >= J) 
	    *lsize=*lsize+1;
	else
	    *usize=*usize+1;
	pElement = pElement->NextInCol;
    };
  };
}  
void C2F(lusiz1)(fmat,lsize,usize)
long *fmat;
int *lsize,*usize;
{
  spLusiz((char *) *fmat,lsize,usize);
}