1 | /*************************************************************************
|
---|
2 | This file is a part of ALGLIB project.
|
---|
3 |
|
---|
4 | >>> SOURCE LICENSE >>>
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
8 | License, or (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | A copy of the GNU General Public License is available at
|
---|
16 | http://www.fsf.org/licensing/licenses
|
---|
17 |
|
---|
18 | >>> END OF LICENSE >>>
|
---|
19 | *************************************************************************/
|
---|
20 |
|
---|
21 | using System;
|
---|
22 |
|
---|
23 | namespace alglib
|
---|
24 | {
|
---|
25 | public class csolve
|
---|
26 | {
|
---|
27 | /*************************************************************************
|
---|
28 | Solving a system of linear equations with a system matrix given by its
|
---|
29 | LU decomposition.
|
---|
30 |
|
---|
31 | The algorithm solves a system of linear equations whose matrix is given by
|
---|
32 | its LU decomposition. In case of a singular matrix, the algorithm returns
|
---|
33 | False.
|
---|
34 |
|
---|
35 | The algorithm solves systems with a square matrix only.
|
---|
36 |
|
---|
37 | Input parameters:
|
---|
38 | A - LU decomposition of a system matrix in compact form (the
|
---|
39 | result of the RMatrixLU subroutine).
|
---|
40 | Pivots - row permutation table (the result of a
|
---|
41 | RMatrixLU subroutine).
|
---|
42 | B - right side of a system.
|
---|
43 | Array whose index ranges within [0..N-1].
|
---|
44 | N - size of matrix A.
|
---|
45 |
|
---|
46 | Output parameters:
|
---|
47 | X - solution of a system.
|
---|
48 | Array whose index ranges within [0..N-1].
|
---|
49 |
|
---|
50 | Result:
|
---|
51 | True, if the matrix is not singular.
|
---|
52 | False, if the matrux is singular. In this case, X doesn't contain a
|
---|
53 | solution.
|
---|
54 |
|
---|
55 | -- ALGLIB --
|
---|
56 | Copyright 2005-2008 by Bochkanov Sergey
|
---|
57 | *************************************************************************/
|
---|
58 | public static bool cmatrixlusolve(ref AP.Complex[,] a,
|
---|
59 | ref int[] pivots,
|
---|
60 | AP.Complex[] b,
|
---|
61 | int n,
|
---|
62 | ref AP.Complex[] x)
|
---|
63 | {
|
---|
64 | bool result = new bool();
|
---|
65 | AP.Complex[] y = new AP.Complex[0];
|
---|
66 | int i = 0;
|
---|
67 | int j = 0;
|
---|
68 | AP.Complex v = 0;
|
---|
69 | int i_ = 0;
|
---|
70 |
|
---|
71 | b = (AP.Complex[])b.Clone();
|
---|
72 |
|
---|
73 | y = new AP.Complex[n-1+1];
|
---|
74 | x = new AP.Complex[n-1+1];
|
---|
75 | result = true;
|
---|
76 | for(i=0; i<=n-1; i++)
|
---|
77 | {
|
---|
78 | if( a[i,i]==0 )
|
---|
79 | {
|
---|
80 | result = false;
|
---|
81 | return result;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | //
|
---|
86 | // pivots
|
---|
87 | //
|
---|
88 | for(i=0; i<=n-1; i++)
|
---|
89 | {
|
---|
90 | if( pivots[i]!=i )
|
---|
91 | {
|
---|
92 | v = b[i];
|
---|
93 | b[i] = b[pivots[i]];
|
---|
94 | b[pivots[i]] = v;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | //
|
---|
99 | // Ly = b
|
---|
100 | //
|
---|
101 | y[0] = b[0];
|
---|
102 | for(i=1; i<=n-1; i++)
|
---|
103 | {
|
---|
104 | v = 0.0;
|
---|
105 | for(i_=0; i_<=i-1;i_++)
|
---|
106 | {
|
---|
107 | v += a[i,i_]*y[i_];
|
---|
108 | }
|
---|
109 | y[i] = b[i]-v;
|
---|
110 | }
|
---|
111 |
|
---|
112 | //
|
---|
113 | // Ux = y
|
---|
114 | //
|
---|
115 | x[n-1] = y[n-1]/a[n-1,n-1];
|
---|
116 | for(i=n-2; i>=0; i--)
|
---|
117 | {
|
---|
118 | v = 0.0;
|
---|
119 | for(i_=i+1; i_<=n-1;i_++)
|
---|
120 | {
|
---|
121 | v += a[i,i_]*x[i_];
|
---|
122 | }
|
---|
123 | x[i] = (y[i]-v)/a[i,i];
|
---|
124 | }
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /*************************************************************************
|
---|
130 | Solving a system of linear equations.
|
---|
131 |
|
---|
132 | The algorithm solves a system of linear equations by using the
|
---|
133 | LU decomposition. The algorithm solves systems with a square matrix only.
|
---|
134 |
|
---|
135 | Input parameters:
|
---|
136 | A - system matrix.
|
---|
137 | Array whose indexes range within [0..N-1, 0..N-1].
|
---|
138 | B - right side of a system.
|
---|
139 | Array whose indexes range within [0..N-1].
|
---|
140 | N - size of matrix A.
|
---|
141 |
|
---|
142 | Output parameters:
|
---|
143 | X - solution of a system.
|
---|
144 | Array whose index ranges within [0..N-1].
|
---|
145 |
|
---|
146 | Result:
|
---|
147 | True, if the matrix is not singular.
|
---|
148 | False, if the matrix is singular. In this case, X doesn't contain a
|
---|
149 | solution.
|
---|
150 |
|
---|
151 | -- ALGLIB --
|
---|
152 | Copyright 2005-2008 by Bochkanov Sergey
|
---|
153 | *************************************************************************/
|
---|
154 | public static bool cmatrixsolve(AP.Complex[,] a,
|
---|
155 | AP.Complex[] b,
|
---|
156 | int n,
|
---|
157 | ref AP.Complex[] x)
|
---|
158 | {
|
---|
159 | bool result = new bool();
|
---|
160 | int[] pivots = new int[0];
|
---|
161 | int i = 0;
|
---|
162 |
|
---|
163 | a = (AP.Complex[,])a.Clone();
|
---|
164 | b = (AP.Complex[])b.Clone();
|
---|
165 |
|
---|
166 | clu.cmatrixlu(ref a, n, n, ref pivots);
|
---|
167 | result = cmatrixlusolve(ref a, ref pivots, b, n, ref x);
|
---|
168 | return result;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | public static bool complexsolvesystemlu(ref AP.Complex[,] a,
|
---|
173 | ref int[] pivots,
|
---|
174 | AP.Complex[] b,
|
---|
175 | int n,
|
---|
176 | ref AP.Complex[] x)
|
---|
177 | {
|
---|
178 | bool result = new bool();
|
---|
179 | AP.Complex[] y = new AP.Complex[0];
|
---|
180 | int i = 0;
|
---|
181 | AP.Complex v = 0;
|
---|
182 | int ip1 = 0;
|
---|
183 | int im1 = 0;
|
---|
184 | int i_ = 0;
|
---|
185 |
|
---|
186 | b = (AP.Complex[])b.Clone();
|
---|
187 |
|
---|
188 | y = new AP.Complex[n+1];
|
---|
189 | x = new AP.Complex[n+1];
|
---|
190 | result = true;
|
---|
191 | for(i=1; i<=n; i++)
|
---|
192 | {
|
---|
193 | if( a[i,i]==0 )
|
---|
194 | {
|
---|
195 | result = false;
|
---|
196 | return result;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | //
|
---|
201 | // pivots
|
---|
202 | //
|
---|
203 | for(i=1; i<=n; i++)
|
---|
204 | {
|
---|
205 | if( pivots[i]!=i )
|
---|
206 | {
|
---|
207 | v = b[i];
|
---|
208 | b[i] = b[pivots[i]];
|
---|
209 | b[pivots[i]] = v;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | //
|
---|
214 | // Ly = b
|
---|
215 | //
|
---|
216 | y[1] = b[1];
|
---|
217 | for(i=2; i<=n; i++)
|
---|
218 | {
|
---|
219 | im1 = i-1;
|
---|
220 | v = 0.0;
|
---|
221 | for(i_=1; i_<=im1;i_++)
|
---|
222 | {
|
---|
223 | v += a[i,i_]*y[i_];
|
---|
224 | }
|
---|
225 | y[i] = b[i]-v;
|
---|
226 | }
|
---|
227 |
|
---|
228 | //
|
---|
229 | // Ux = y
|
---|
230 | //
|
---|
231 | x[n] = y[n]/a[n,n];
|
---|
232 | for(i=n-1; i>=1; i--)
|
---|
233 | {
|
---|
234 | ip1 = i+1;
|
---|
235 | v = 0.0;
|
---|
236 | for(i_=ip1; i_<=n;i_++)
|
---|
237 | {
|
---|
238 | v += a[i,i_]*x[i_];
|
---|
239 | }
|
---|
240 | x[i] = (y[i]-v)/a[i,i];
|
---|
241 | }
|
---|
242 | return result;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | public static bool complexsolvesystem(AP.Complex[,] a,
|
---|
247 | AP.Complex[] b,
|
---|
248 | int n,
|
---|
249 | ref AP.Complex[] x)
|
---|
250 | {
|
---|
251 | bool result = new bool();
|
---|
252 | int[] pivots = new int[0];
|
---|
253 |
|
---|
254 | a = (AP.Complex[,])a.Clone();
|
---|
255 | b = (AP.Complex[])b.Clone();
|
---|
256 |
|
---|
257 | clu.complexludecomposition(ref a, n, n, ref pivots);
|
---|
258 | result = complexsolvesystemlu(ref a, ref pivots, b, n, ref x);
|
---|
259 | return result;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|