Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trisolve.h
1 /*
2 This file is distributed as part of the BIAS library (Basic ImageAlgorithmS)
3 but it has not been developed by the authors of BIAS.
4 
5 For copyright, author and license information see below.
6 */
7 
8 
9 /*
10 *
11 * Template Numerical Toolkit (TNT): Linear Algebra Module
12 *
13 * Mathematical and Computational Sciences Division
14 * National Institute of Technology,
15 * Gaithersburg, MD USA
16 *
17 *
18 * This software was developed at the National Institute of Standards and
19 * Technology (NIST) by employees of the Federal Government in the course
20 * of their official duties. Pursuant to title 17 Section 105 of the
21 * United States Code, this software is not subject to copyright protection
22 * and is in the public domain. The Template Numerical Toolkit (TNT) is
23 * an experimental system. NIST assumes no responsibility whatsoever for
24 * its use by other parties, and makes no guarantees, expressed or implied,
25 * about its quality, reliability, or any other characteristic.
26 *
27 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
28 * see http://math.nist.gov/tnt for latest updates.
29 *
30 */
31 
32 
33 
34 // Triangular Solves
35 
36 #ifndef TRISLV_H
37 #define TRISLV_H
38 
39 
40 #include "triang.h"
41 
42 namespace TNT
43 {
44 
45  template <class MaTriX, class VecToR>
46  VecToR Lower_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b)
47  {
48  Subscript N = A.num_rows();
49 
50  // make sure matrix sizes agree; A must be square
51 
52  BIASASSERT(A.num_cols() == N);
53  BIASASSERT(b.dim() == N);
54 
55  VecToR x(N);
56 
57  Subscript i;
58  for (i=1; i<=N; i++)
59  {
60  typename MaTriX::element_type tmp=0;
61 
62  for (Subscript j=1; j<i; j++)
63  tmp = tmp + A(i,j)*x(j);
64 
65  x(i) = (b(i) - tmp)/ A(i,i);
66  }
67 
68  return x;
69  }
70 
71 
72  template <class MaTriX, class VecToR>
73  VecToR Unit_lower_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b)
74  {
75  Subscript N = A.num_rows();
76 
77  // make sure matrix sizes agree; A must be square
78 
79  BIASASSERT(A.num_cols() == N);
80  BIASASSERT(b.dim() == N);
81 
82  VecToR x(N);
83 
84  Subscript i;
85  for (i=1; i<=N; i++)
86  {
87 
88  typename MaTriX::element_type tmp=0;
89 
90  for (Subscript j=1; j<i; j++)
91  tmp = tmp + A(i,j)*x(j);
92 
93  x(i) = b(i) - tmp;
94  }
95 
96  return x;
97  }
98 
99 
100  template <class MaTriX, class VecToR>
102  /*const*/ VecToR &b)
103  {
104  return Lower_triangular_solve(A, b);
105  }
106 
107  template <class MaTriX, class VecToR>
109  /*const*/ VecToR &b)
110  {
111  return Unit_lower_triangular_solve(A, b);
112  }
113 
114 
115 
116  //********************** Upper triangular section ****************
117 
118  template <class MaTriX, class VecToR>
119  VecToR Upper_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b)
120  {
121  Subscript N = A.num_rows();
122 
123  // make sure matrix sizes agree; A must be square
124 
125  BIASASSERT(A.num_cols() == N);
126  BIASASSERT(b.dim() == N);
127 
128  VecToR x(N);
129 
130  Subscript i;
131  for (i=N; i>=1; i--)
132  {
133 
134  typename MaTriX::element_type tmp=0;
135 
136  for (Subscript j=i+1; j<=N; j++)
137  tmp = tmp + A(i,j)*x(j);
138 
139  x(i) = (b(i) - tmp)/ A(i,i);
140  }
141 
142  return x;
143  }
144 
145 
146  template <class MaTriX, class VecToR>
147  VecToR Unit_upper_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b)
148  {
149  Subscript N = A.num_rows();
150 
151  // make sure matrix sizes agree; A must be square
152 
153  BIASASSERT(A.num_cols() == N);
154  BIASASSERT(b.dim() == N);
155 
156  VecToR x(N);
157 
158  Subscript i;
159  for (i=N; i>=1; i--)
160  {
161 
162  typename MaTriX::element_type tmp=0;
163 
164  for (Subscript j=i+1; j<i; j++)
165  tmp = tmp + A(i,j)*x(j);
166 
167  x(i) = b(i) - tmp;
168  }
169 
170  return x;
171  }
172 
173 
174  template <class MaTriX, class VecToR>
176  /*const*/ VecToR &b)
177  {
178  return Upper_triangular_solve(A, b);
179  }
180 
181  template <class MaTriX, class VecToR>
183  /*const*/ VecToR &b)
184  {
185  return Unit_upper_triangular_solve(A, b);
186  }
187 
188 
189 } // namespace TNT
190 
191 #endif
192 // TRISLV_H
VecToR Lower_triangular_solve(MaTriX &A, VecToR &b)
Definition: trisolve.h:46
VecToR Upper_triangular_solve(MaTriX &A, VecToR &b)
Definition: trisolve.h:119
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
VecToR Unit_lower_triangular_solve(MaTriX &A, VecToR &b)
Definition: trisolve.h:73
VecToR Unit_upper_triangular_solve(MaTriX &A, VecToR &b)
Definition: trisolve.h:147
VecToR linear_solve(LowerTriangularView< MaTriX > &A, VecToR &b)
Definition: trisolve.h:101