vnl_sparse_lu.cxx
Go to the documentation of this file.
1 // This is core/vnl/algo/vnl_sparse_lu.cxx
2 //:
3 // \file
4 #include <iostream>
5 #include <cassert>
6 #include "vnl_sparse_lu.h"
7 
8 #include <sparse/spMatrix.h>
9 
10 // destructor - undo the spCreate() from the constructor(s)
11 // (memory leak fix of 7 Feb. 2008 by Toon Huysmans)
13 {
14  spDestroy( pmatrix_ );
15 }
16 
17 //: constructor - controls if condition information is computed
19  A_(M), factored_(false),condition_computed_(false), mode_(mode),norm_(0), rcond_(0), largest_(0), pivot_thresh_(0),absolute_thresh_(0),diag_pivoting_(1),pmatrix_(nullptr)
20 {
21  int n = (int)M.columns();
22  assert(n == (int)(M.rows()));
23  int error = 0;
24  pmatrix_ = spCreate(n, 0, &error);
25  if (error!=spOKAY)
26  {
27  std::cout << "In vnl_sparse_lu::vnl_sparse_lu - error in creating matrix\n";
28  return;
29  }
30  // fill the internal sparse matrix from A_
31  spElement* pelement = nullptr;
32  for (A_.reset(); A_.next();)
33  {
34  int r = A_.getrow();
35  int c = A_.getcolumn();
36  double v = A_.value();
37  pelement = spGetElement(pmatrix_, r+1, c+1);
38  if (pelement == nullptr)
39  {
40  std::cout<< "In vnl_sparse_lu::vnl_sparse_lu - error in getting element\n";
41  return;
42  }
43  *pelement = v;
44  }
46  {
47  largest_ = spLargestElement(pmatrix_);
49  std::cout << " Largest element in matrix = " << largest_ << '\n';
50  norm_ = spNorm(pmatrix_);
51  }
52 }
53 
54 //: estimate the condition number.
56 {
57  int error = spOKAY;
58  rcond_ = spCondition(pmatrix_, norm_, &error);
59  if (error!=spOKAY)
60  {
61  std::cout << "In vnl_sparse_lu::est_condition(..) - error in condition number calculation\n";
62  return false;
63  }
64  condition_computed_ = true;
65  return true;
66 }
67 
68 //: Solve least squares problem M x = b.
70 {
71  if (!pmatrix_)
72  {
73  std::cout << "In vnl_sparse_lu::solve(..) - matrix not defined\n";
74  return;
75  }
76  unsigned n = b.size();
77  assert(n == A_.columns());
78  auto* rhs = new spREAL[n+1];
79  for (unsigned i = 0; i<n; ++i)
80  rhs[i+1]=b[i];
82  {
83  std::cout << "Matrix before ordering\n";
84  spPrint(pmatrix_,1,1,1);
85  }
86 
87  int error = 0;
88  //check if the matrix needs ordering
89  //if not, run the decomposition
90  if (!factored_)
91  {
92  error = spOrderAndFactor(pmatrix_, rhs, pivot_thresh_,
94  if (error != spOKAY)
95  {
96  std::cout << "In vnl_sparse_lu::solve(..) - error in factoring\n";
97  return;
98  }
100  if (!est_condition())
101  return;
102  factored_ = true;
103  }
104 
106  {
107  std::cout << "Matrix after ordering\n";
108  spPrint(pmatrix_,1,1,1);
109  }
110 
111  spSolve(pmatrix_, rhs, rhs);
112 
113  for (unsigned i = 0; i<n; ++i)
114  (*x)[i]=rhs[i+1];
115 
116  delete [] rhs;
117 }
118 
119 //: Solve least squares problem M x = b.
121 {
122  vnl_vector<double> ret(b.size());
123  this->solve(b, &ret);
124  return ret;
125 }
126 
127 //: Solve problem M^t x = b
129 {
130  if (!pmatrix_)
131  {
132  std::cout << "In vnl_sparse_lu::solve(..) - matrix not defined\n";
133  return;
134  }
135  unsigned n = b.size();
136  assert(n == A_.columns());
137  auto* rhs = new spREAL[n+1];
138  for (unsigned i = 0; i<n; ++i)
139  rhs[i+1]=b[i];
140  int error = 0;
142  {
143  std::cout << "Matrix before ordering\n";
144  spPrint(pmatrix_,1,1,1);
145  }
146 
147  //check if the matrix needs ordering
148  //if not, run the decomposition
149  if (!factored_)
150  {
151  error = spOrderAndFactor(pmatrix_, rhs, pivot_thresh_,
153  if (error != spOKAY)
154  {
155  std::cout << "In vnl_sparse_lu::solve(..) - error in factoring\n";
156  return;
157  }
159  if (!est_condition())
160  return;
161  factored_ = true;
162  }
163 
165  {
166  std::cout << "Matrix after ordering\n";
167  spPrint(pmatrix_,1,1,1);
168  }
169 
170  spSolveTransposed(pmatrix_, rhs, rhs);
171 
172  for (unsigned i = 0; i<n; ++i)
173  (*x)[i]=rhs[i+1];
174 
175  delete [] rhs;
176 }
177 
178 //: Solve problem M^t x = b
180 {
181  vnl_vector<double> ret(b.size());
182  this->solve_transpose(b, &ret);
183  return ret;
184 }
185 
186 //: Compute determinant.
188 {
189  int exponent;
190  double determ;
191  if (!factored_)
192  {
193  spFactor(pmatrix_);
195  if (!est_condition())
196  return 0;
197  factored_ = true;
198  }
199  spDeterminant(pmatrix_, &exponent, &determ);
200  if (exponent<0)
201  {
202  while (exponent<0)
203  {
204  determ *= 0.1;
205  exponent++;
206  }
207  return determ;
208  }
209  else if (exponent>0)
210  while (exponent>0)
211  {
212  determ *= 10.0;
213  exponent--;
214  }
215 
216  return determ;
217 }
218 
219 //: the reciprocal of the condition number
221 {
222  if (!factored_)
223  {
224  spFactor(pmatrix_);
226  if (!est_condition())
227  return 0;
228  factored_ = true;
229  }
230  return rcond_;
231 }
232 
233 //:Estimated upper bound of error in solution
235 {
237  return 0;
238 
239  if (!factored_)
240  {
241  spFactor(pmatrix_);
242  if (!est_condition())
243  return 0;
244  factored_ = true;
245  }
246  double roundoff = spRoundoff(pmatrix_, largest_);
247  if (rcond_>0)
248  return roundoff/rcond_;
249  return 0;
250 }
unsigned int columns() const
Get the number of columns in the matrix.
Solution of the linear system Mx = b for sparse matrices.
void * pmatrix_
The internal matrix representation.
double absolute_thresh_
Definition: vnl_sparse_lu.h:94
T value() const
Returns the value pointed to by the internal iterator.
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
vnl_sparse_matrix< double > A_
Definition: vnl_sparse_lu.h:86
void reset() const
Resets the internal iterator.
bool est_condition()
estimate the condition number.
double max_error_bound()
An estimate of maximum error in solution.
vnl_sparse_lu(vnl_sparse_matrix< double > const &M, operation mode=quiet)
Make sparse_lu decomposition of M optionally computing the reciprocal condition number.
#define v
Definition: vnl_vector.h:42
double determinant()
Compute determinant.
operation
Modes of computation. See constructor for details.
Definition: vnl_sparse_lu.h:32
double pivot_thresh_
Definition: vnl_sparse_lu.h:93
double rcond()
Return reciprocal condition number (smallest/largest singular values).
unsigned int rows() const
Get the number of rows in the matrix.
bool condition_computed_
Definition: vnl_sparse_lu.h:88
operation mode_
Definition: vnl_sparse_lu.h:89
vnl_vector< double > solve_transpose(vnl_vector< double > const &b)
Solve problem M^t x = b.
vnl_vector< double > solve(vnl_vector< double > const &b)
Solve problem M x = b.
int getrow() const
Returns the row of the entry pointed to by internal iterator.
bool next() const
Moves the internal iterator to next non-zero entry in matrix.
int getcolumn() const
Returns the column of the entry pointed to by internal iterator.