1 /*
2  * Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
3  * Applied Mathematics, Norway.
4  *
5  * Contact information: E-mail: tor.dokken@sintef.no
6  * SINTEF ICT, Department of Applied Mathematics,
7  * P.O. Box 124 Blindern,
8  * 0314 Oslo, Norway.
9  *
10  * This file is part of SISL.
11  *
12  * SISL is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Affero General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * SISL is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Affero General Public License for more details.
21  *
22  * You should have received a copy of the GNU Affero General Public
23  * License along with SISL. If not, see
24  * <http://www.gnu.org/licenses/>.
25  *
26  * In accordance with Section 7(b) of the GNU Affero General Public
27  * License, a covered work must retain the producer line in every data
28  * file that is created or manipulated using SISL.
29  *
30  * Other Usage
31  * You can be released from the requirements of the license by purchasing
32  * a commercial license. Buying such a license is mandatory as soon as you
33  * develop commercial activities involving the SISL library without
34  * disclosing the source code of your own applications.
35  *
36  * This file may be used in accordance with the terms contained in a
37  * written agreement between you and SINTEF ICT.
38  */
39 
40 #include "sisl-copyright.h"
41 
42 /*
43  *
44  * $Id: s6lusolp.c,v 1.2 2001-03-19 15:59:02 afr Exp $
45  *
46  */
47 
48 
49 #define S6LUSOLP
50 
51 #include "sislP.h"
52 
53 #if defined(SISLNEEDPROTOTYPES)
54 void
s6lusolp(double ea[],double eb[],int nl[],int im,int * jstat)55 s6lusolp(double ea[],double eb[],int nl[],int im,int *jstat)
56 #else
57 void s6lusolp(ea,eb,nl,im,jstat)
58      double ea[];
59      double eb[];
60      int    nl[];
61      int    im;
62      int    *jstat;
63 #endif
64 /*
65 ************************************************************************
66 *
67 ***********************************************************************
68 *
69 *   PURPOSE : Solve the equationsystem LU=eb by forth- and back-
70 *             substitution. L and U are stored in ea.
71 *
72 *
73 *   INPUT   : ea   - The factorized coeffecient-matrix.
74 *             im   - The number of equations.
75 *             nl   - Ordering of lines in the matrix.
76 *
77 *
78 *   INPUT/OUTPUT : eb - the right side of the equationsystem and
79 *                       the found values for the unknowns.
80 *
81 *   OUTPUT  : jstat  - Status variable.
82 *                        < 0 : Error
83 *                        = 0 : ok
84 *                        > 0 : Warning
85 *
86 *
87 *   METHOD  : Solve on the equation-system LU=eb.
88 *
89 *
90 *   REFERENCES : Cheney & Kincaid : Numerical Mathematics and
91 *                                   Computing.
92 *
93 *-
94 *   CALLS      :
95 *
96 *   WRITTEN BY : Vibeke Skytt, SI, 86-10.
97 *
98 ************************************************************************
99 */
100 {
101   int kpos = 0;      /* Position of error.                             */
102   int ki,kj;         /* Counters.                                      */
103   double *sx = SISL_NULL; /* Array used to keep solution of equation system
104 			internally.                                    */
105   double tdiv;       /* Dividend in expression.                        */
106 
107   /* Allocate space for local array.  */
108 
109   if ((sx = newarray(im,double)) == SISL_NULL) goto err101;
110 
111   for (ki=0; ki<im-1; ki++)
112     {
113       /*  Gauss on right side of equation  */
114 
115       for (kj=ki+1; kj<im; kj++)
116 	eb[nl[kj]] -= eb[nl[ki]]*ea[ki+nl[kj]*im];
117     }
118 
119   tdiv = ea[im-1+nl[im-1]*im];
120   if (DEQUAL(tdiv,DZERO)) goto warn1;
121   sx[im-1] = eb[nl[im-1]]/tdiv;
122 
123   for (ki=im-2; ki>=0; ki--)
124     {
125       /*  Backwards substitution.   */
126 
127       for (kj=ki+1; kj<im; kj++)
128 	eb[nl[ki]] -= sx[kj]*ea[kj+nl[ki]*im];
129 
130       tdiv = ea[ki+nl[ki]*im];
131       if (DEQUAL(tdiv,DZERO)) goto warn1;
132       sx[ki] = eb[nl[ki]]/tdiv;
133     }
134   for (ki=0; ki<im; ki++) eb[ki] = sx[ki];
135 
136   /* Equation system solved.  */
137 
138   *jstat = 0;
139   goto out;
140 
141 /* Singular equation system.  */
142 
143 warn1 : *jstat = 1;
144         goto out;
145 
146 /* Error in space allocation.  */
147 
148 err101: *jstat = -101;
149         s6err("s6lusolp",*jstat,kpos);
150         goto out;
151 
152 out:
153 
154 /* Free space occupied by local array.  */
155 
156 if (sx != SISL_NULL) freearray(sx);
157 
158 return;
159 }
160