Library of Assembled Shared Sources
matrix_solve.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2023 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_MATRIX_SOLVE_INL
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_MATRIX_SOLVE_INL
47
48#include "../num_common.h"
49#include "matrix_solve.h"
50
51namespace lass
52{
53namespace num
54{
55namespace impl
56{
57
58/** Given a complex matrix iA, this routine replaces it by the LU decomposition
59 * of a rowwise permutation of itself.
60 * @param ioMatrix [in,out]
61 * @arg Random iterator to first element of of a @e square matrix in row major order.
62 * @arg ioMatrix is replaced by its LU decomposition.
63 * @arg [ioMatrix, ioMatrix + iSize * iSize) must be a valid range
64 * @param oIndex [out]
65 * @arg records the row permutations effected by the partial pivoting.
66 * @arg [oIndex, oIndex + iSize) must be a valid range.
67 * @param iSize [in]
68 * @arg size of matrix
69 * @param oD [out]
70 * @arg indicates the number of row interchanges was even (+1) or odd (-1).
71 * @return - true: LU decomposition completed
72 * - false: matrix ioMatrix is singular
73 *
74 * This routine is used in combination with lusolve() to solve linear
75 * equations and lumprove() to improve the solution.
76 *
77 * Method: Crout's algorithm with partial pivoting.
78 */
79template
80<
81 typename T,
82 typename RandomIterator1,
83 typename RandomIterator2
84>
85bool ludecomp(RandomIterator1 ioMatrix,
86 RandomIterator2 oIndex,
87 std::ptrdiff_t iSize,
88 int& iD)
89{
90 typedef NumTraits<T> TNumTraits;
91 typedef typename TNumTraits::baseType TBase;
92 typedef NumTraits<TBase> TBaseTraits;
93
94 const TBase epsilon = static_cast<TBase>(1e-20);
95
96 std::vector<T> scaling(static_cast<size_t>(iSize));
97 typename std::vector<T>::iterator vv = scaling.begin();
98 iD = 1;
99
100 for (std::ptrdiff_t i = 0; i < iSize; ++i)
101 {
102 RandomIterator1 rowI = ioMatrix + (i * iSize);
103 TBase normMax = TBaseTraits::zero;
104 std::ptrdiff_t jMax = iSize;
105 for (std::ptrdiff_t j = 0; j < iSize; ++j)
106 {
107 const TBase temp = num::norm(rowI[j]);
108 if (temp > normMax)
109 {
110 normMax = temp;
111 jMax = j;
112 }
113 }
114 if (normMax == TBaseTraits::zero)
115 {
116 return false; // no decomposition
117 }
118 LASS_ASSERT(jMax != iSize);
119
120 vv[i] = num::conj(rowI[jMax]) / normMax;
121 }
122
123 for (std::ptrdiff_t j = 0; j < iSize; ++j)
124 {
125 std::ptrdiff_t i;
126 for (i = 0; i < j; ++i)
127 {
128 RandomIterator1 rowI = ioMatrix + (i * iSize);
129 T sum = rowI[j];
130 for (std::ptrdiff_t k = 0; k < i; ++k)
131 {
132 sum -= rowI[k] * ioMatrix[k * iSize + j];
133 }
134 rowI[j] = sum;
135 }
136
137 TBase normMax = TBaseTraits::zero;
138 std::ptrdiff_t iMax = iSize;
139 for (i = j; i < iSize; ++i)
140 {
141 RandomIterator1 rowI = ioMatrix + (i * iSize);
142 T sum = rowI[j];
143 for (std::ptrdiff_t k = 0; k < j; ++k)
144 {
145 sum -= rowI[k] * ioMatrix[k * iSize + j];
146 }
147 rowI[j] = sum;
148
149 const T dum = vv[i] * sum;
150 const TBase temp = num::norm(dum);
151 if (temp >= normMax)
152 {
153 normMax = temp;
154 iMax = i;
155 }
156 }
157 LASS_ASSERT(iMax != iSize);
158
159 if (j != iMax)
160 {
161 for (std::ptrdiff_t k = 0; k < iSize; ++k)
162 {
163 std::swap(ioMatrix[iMax * iSize + k], ioMatrix[j * iSize + k]);
164 }
165 iD = -iD;
166 vv[iMax] = vv[j];
167 }
168 oIndex[j] = iMax;
169
170 TBase temp = num::norm(ioMatrix[j * iSize + j]);
171 if (temp == TBaseTraits::zero)
172 {
173 ioMatrix[j * iSize + j] = TNumTraits::one;//CT(T(1), T(1));
174 temp = epsilon;
175 }
176
177 if (j != iSize - 1)
178 {
179 const T dum = num::conj(ioMatrix[j * iSize + j]) / temp;
180 for (i = j + 1; i < iSize; ++i)
181 {
182 ioMatrix[i * iSize + j] *= dum;
183 }
184 }
185 }
186
187 return true;
188}
189
190
191
192/** Solves the set of linear eqautions A X = B.
193 * @internal
194 * @param iMatrix [in]
195 * @arg Random iterator to first element of A in row major order
196 * @arg A as LU decomposition.
197 * @arg A must be square.
198 * @arg [iMatrix, iMatrx + iSize * iSize) must be a valid range
199 * @param iIndex [in]
200 * @arg permutation sequence filled by ludecomp().
201 * @arg [iIndex, iIndex + iSize) must be a valid range
202 * @param ioColumn [in,out]
203 * @arg as input, it is the righthand side vector B.
204 * @arg as output it is the solution vector X.
205 * @arg must be of same length as iA
206 * @arg [ioColumn, ioColumn + iSize) must be a valid range
207 * @param iSize [in]
208 * @arg size of matrix
209 *
210 * This routine can be used for successive calls with different right-hand
211 * sides B.
212 *
213 * Method: backward and forward substitution, taking into account the leading
214 * zero's in B.
215 */
216template
217<
218 typename T,
219 typename RandomIterator1,
220 typename RandomIterator2,
221 typename RandomIterator3
222>
223void lusolve(RandomIterator1 iMatrix,
224 RandomIterator2 iIndex,
225 RandomIterator3 ioColumn,
226 std::ptrdiff_t iSize)
227{
228 typedef NumTraits<T> TNumTraits;
229
230 std::ptrdiff_t ii = iSize;
231 for (std::ptrdiff_t i = 0; i < iSize; ++i)
232 {
233 RandomIterator1 rowI = iMatrix + (i * iSize);
234 const std::ptrdiff_t ip = iIndex[i];
235 T sum = ioColumn[ip];
236 ioColumn[ip] = ioColumn[i];
237
238 if (ii != iSize)
239 {
240 for (std::ptrdiff_t j = ii; j < i; ++j)
241 {
242 sum -= rowI[j] * ioColumn[j];
243 }
244 }
245 else
246 {
247 if (!(sum == TNumTraits::zero)) // BUG in STL???
248 {
249 ii = i;
250 }
251 }
252 ioColumn[i] = sum;
253 }
254
255 for (std::ptrdiff_t ic = iSize; ic > 0; --ic)
256 {
257 const std::ptrdiff_t i = ic - 1;
258 RandomIterator1 rowI = iMatrix + (i * iSize);
259 T sum = ioColumn[i];
260 for (std::ptrdiff_t j = ic; j < iSize; ++j)
261 {
262 sum -= rowI[j] * ioColumn[j];
263 }
264 ioColumn[i] = sum / rowI[i];
265 }
266}
267
268
269
270/** Improves a solution vector X of the linear set of equations A X = B.
271 * @internal
272 * @param iMatrix [in]
273 * @arg Random iterator to first element of matrix in row major order.
274 * @arg must be square.
275 * @arg [iMatrix, iMatrix + iSize * iSize) must be a valid range
276 * @param iMatrixLU [in]
277 * @arg LU decompostion of A
278 * @arg must be square.
279 * @arg [iMatrixLU, iMatrixLU + iSize * iSize) must be a valid range
280 * @param iIndex [in]
281 * @arg [iIndex, iIndex + iSize) must be a valid range
282 * @param iColumn [in]
283 * @arg [iColumn, iColumn + iSize) must be a valid range
284 * @param ioX [in,out]
285 * @arg [ioX, ioX + iSize) must be a valid range
286 * @param iSize [in]
287 * @arg size of matrix
288 */
289template
290<
291 typename T,
292 typename RandomIterator1,
293 typename RandomIterator2,
294 typename RandomIterator3,
295 typename RandomIterator4,
296 typename RandomIterator5
297>
298void lumprove(RandomIterator1 iMatrix,
299 RandomIterator2 iMatrixLU,
300 RandomIterator3 iIndex,
301 RandomIterator4 iColumn,
302 RandomIterator5 ioX,
303 std::ptrdiff_t iSize)
304{
305 std::vector<T> right(static_cast<size_t>(iSize));
306 typename std::vector<T>::iterator r = right.begin();
307 std::ptrdiff_t i;
308 for (i = 0; i < iSize; ++i)
309 {
310 RandomIterator1 rowI = iMatrix + (i * iSize);
311 r[i] = -iColumn[i];
312 for (std::ptrdiff_t j = 0; j < iSize; ++j)
313 {
314 r[i] += rowI[j] * ioX[j];
315 }
316 }
317
318 lusolve<T>(iMatrixLU, iIndex, r, iSize);
319
320 for (i = 0; i < iSize; ++i)
321 {
322 ioX[i] -= r[i];
323 }
324}
325
326
327
328/** Solve A X = B for 2x2 matrices with Cramer's rule.
329 * @param iMatrix [in]
330 * @arg Random iterator to first element of matrix A in row major order.
331 * @arg A must be of size 2x2.
332 * @arg [iMatrix, iMatrix + 4) must be a valid range
333 * @param ioColumn [in,out]
334 * @arg Random iterator to first element of B as input
335 * @arg Random iterator to first element of X as output
336 * @arg [iColumn, iColumn + 2) must be a valid range
337 */
338template
339<
340 typename T,
341 typename RandomIterator1,
342 typename RandomIterator2
343>
344bool cramer2(RandomIterator1 iMatrixRowMajor,
345 RandomIterator2 ioColumnFirst,
346 RandomIterator2 ioColumnLast)
347{
348 typedef NumTraits<T> TNumTraits;
349 RandomIterator1 m = iMatrixRowMajor; // shortcut ;)
350
351 const T determinant = m[0] * m[3] - m[1] * m[2];
352
353 if (determinant == TNumTraits::zero)
354 {
355 return false;
356 }
357
358 const T inverseDeterminant = num::inv(determinant);
359
360 for (RandomIterator2 column = ioColumnFirst; column != ioColumnLast; column += 2)
361 {
362 LASS_ASSERT(column + 1 != ioColumnLast);
363 const T x = column[0];
364 const T y = column[1];
365 column[0] = inverseDeterminant * (x * m[3] - y * m[1]);
366 column[1] = inverseDeterminant * (m[0] * y - m[2] * x);
367 }
368 return true;
369}
370
371
372
373/** Solve A X = B for 3x3 matrices with Cramer's rule.
374 * @internal
375 * @param iMatrix [in]
376 * @arg Random iterator to first element of matrix A in row major order.
377 * @arg A must be of size 3x3.
378 * @arg [iMatrix, iMatrix + 9) must be a valid range
379 * @param ioColumn [in,out]
380 * @arg Random iterator to first element of B as input
381 * @arg Random iterator to first element of X as output
382 * @arg [iColumn, iColumn + 3) must be a valid range
383 */
384template
385<
386 typename T,
387 typename RandomIterator1,
388 typename RandomIterator2
389>
390bool cramer3(RandomIterator1 iMatrixRowMajor,
391 RandomIterator2 ioColumnFirst,
392 RandomIterator2 ioColumnLast)
393{
394 typedef NumTraits<T> TNumTraits;
395 RandomIterator1 m = iMatrixRowMajor; // shortcut ;)
396
397 const T determinant =
398 m[0] * (m[4] * m[8] - m[7] * m[5]) +
399 m[3] * (m[7] * m[2] - m[1] * m[8]) +
400 m[6] * (m[1] * m[5] - m[4] * m[2]);
401
402 if (determinant == TNumTraits::zero)
403 {
404 return false;
405 }
406
407 const T inverseDeterminant = num::inv(determinant);
408
409 for (RandomIterator2 column = ioColumnFirst; column != ioColumnLast; column += 3)
410 {
411 LASS_ASSERT(column + 1 != ioColumnLast && column + 2 != ioColumnLast);
412 const T x = column[0];
413 const T y = column[1];
414 const T z = column[2];
415 column[0] = inverseDeterminant * (
416 x * (m[4] * m[8] - m[7] * m[5]) +
417 y * (m[7] * m[2] - m[1] * m[8]) +
418 z * (m[1] * m[5] - m[4] * m[2]));
419 column[1] = inverseDeterminant * (
420 m[0] * (y * m[8] - z * m[5]) +
421 m[3] * (z * m[2] - x * m[8]) +
422 m[6] * (x * m[5] - y * m[2]));
423 column[2] = inverseDeterminant * (
424 m[0] * (m[4] * z - m[7] * y) +
425 m[3] * (m[7] * x - m[1] * z) +
426 m[6] * (m[1] * y - m[4] * x));
427 }
428
429 return true;
430}
431
432
433
434/** Solve system of linear equations with a tridiagonal matrix.
435 * @internal
436 * @param iA [in]
437 * @arg Random iterator to first element of lower diagonal.
438 * @arg This is actually the matrix element in the "zeroth" column of the first row.
439 * So, iA[0] is being unused since it's not a valid matrix position. The first
440 * valid element is iA[1]
441 * @arg [iA, iA + N) must be a valid range
442 * @param iB [in]
443 * @arg Random iterator to first element of main diagonal.
444 * @arg This is actually the matrix element in the first column of the first row.
445 * @arg [iB, iB + N) must be a valid range
446 * @param iC [in]
447 * @arg Random iterator to first element of upper diagonal.
448 * @arg This is actually the matrix element in the second column of the first row.
449 * @arg [iC, iC + N) must be a valid range
450 * @arg The last element iC[N - 1] isn't really a valid element since it's a position
451 * outside the matrix.
452 */
453template
454<
455 typename T,
456 typename RandomIterator1,
457 typename RandomIterator2,
458 typename RandomIterator3
459>
460bool solveTridiagonal(RandomIterator1 iA, RandomIterator1 iB, RandomIterator1 iC,
461 RandomIterator2 ioSolution, RandomIterator3 ioTemp,
462 std::ptrdiff_t iSize)
463{
464 typedef NumTraits<T> TNumTraits;
465
466 T beta = iB[0];
467 if (beta == TNumTraits::zero)
468 {
469 return false;
470 }
471 ioSolution[0] /= beta;
472 for (std::ptrdiff_t i = 1; i < iSize; ++i)
473 {
474 ioTemp[i] = iC[i - 1] / beta;
475 beta = iB[i] - iA[i] * ioTemp[i];
476 if (beta == TNumTraits::zero)
477 {
478 return false;
479 }
480 ioSolution[i] = (ioSolution[i] - iA[i] * ioSolution[i - 1]) / beta;
481 }
482 for (std::ptrdiff_t i = iSize - 1; i > 0; --i)
483 {
484 ioSolution[i - 1] -= ioTemp[i] * ioSolution[i];
485 }
486 return true;
487}
488
489}
490
491}
492
493}
494
495#endif
496
497// EOF
T inv(const T &x)
return x ^ -1
Definition basic_ops.h:178
T norm(const T &x)
return norm of x as if x is real part of complex number: sqr(x)
Definition basic_ops.h:211
void lusolve(RandomIterator1 iMatrix, RandomIterator2 iIndex, RandomIterator3 ioColumn, std::ptrdiff_t iSize)
Solves the set of linear eqautions A X = B.
bool cramer2(RandomIterator1 iMatrixRowMajor, RandomIterator2 ioColumnFirst, RandomIterator2 ioColumnLast)
Solve A X = B for 2x2 matrices with Cramer's rule.
bool cramer3(RandomIterator1 iMatrixRowMajor, RandomIterator2 ioColumnFirst, RandomIterator2 ioColumnLast)
Solve A X = B for 3x3 matrices with Cramer's rule.
void lumprove(RandomIterator1 iMatrix, RandomIterator2 iMatrixLU, RandomIterator3 iIndex, RandomIterator4 iColumn, RandomIterator5 ioX, std::ptrdiff_t iSize)
Improves a solution vector X of the linear set of equations A X = B.
bool solveTridiagonal(RandomIterator1 iA_1, RandomIterator1 iB_0, RandomIterator1 iC_0, RandomIterator2 ioSolution, RandomIterator3 ioTemp, std::ptrdiff_t iSize)
Solve system of linear equations with a tridiagonal matrix.
bool ludecomp(RandomIterator1 ioMatrix, RandomIterator2 oIndex, std::ptrdiff_t iSize, int &iD)
Given a complex matrix iA, this routine replaces it by the LU decomposition of a rowwise permutation ...
numeric types and traits.
Definition basic_ops.h:70
T conj(const T &x)
return conjugate as if x is a complex number: x
Definition basic_ops.h:218
Library for Assembled Shared Sources.
Definition config.h:53