ITK  6.0.0
Insight Toolkit
VNLIterativeSparseSolverTraits.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef VNLIterativeSparseSolverTraits_h
19 #define VNLIterativeSparseSolverTraits_h
20 
21 #include "vnl/vnl_vector.h"
22 #include "vnl/vnl_sparse_matrix.h"
23 #include "vnl/vnl_sparse_matrix_linear_system.h"
24 #include "vnl/algo/vnl_lsqr.h"
25 
42 template <typename T = double>
44 {
45 public:
46  using ValueType = T;
47  using MatrixType = vnl_sparse_matrix<ValueType>;
48  using VectorType = vnl_vector<ValueType>;
49  using SolverType = vnl_lsqr;
50 
52  static bool
54  {
55  return false;
56  }
57 
59  static MatrixType
60  InitializeSparseMatrix(const unsigned int & iN)
61  {
62  return MatrixType(iN, iN);
63  }
64 
66  static MatrixType
67  InitializeSparseMatrix(const unsigned int & iRow, const unsigned int & iCol)
68  {
69  return MatrixType(iRow, iCol);
70  }
71 
73  static VectorType
74  InitializeVector(const unsigned int & iN)
75  {
76  return VectorType(iN);
77  }
78 
80  static void
81  FillMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
82  {
83  iA(iR, iC) = iV;
84  }
85 
87  static void
88  AddToMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
89  {
90  iA(iR, iC) += iV;
91  }
92 
94  static bool
95  Solve(const MatrixType & iA, const VectorType & iB, VectorType & oX)
96  {
97  using SparseLinearSystemType = vnl_sparse_matrix_linear_system<ValueType>;
98  SparseLinearSystemType system(iA, iB);
99 
100  SolverType solver(system);
101  return solver.minimize(oX);
102  }
103 
106  static bool
107  Solve(const MatrixType & iA,
108  const VectorType & iBx,
109  const VectorType & iBy,
110  const VectorType & iBz,
111  VectorType & oX,
112  VectorType & oY,
113  VectorType & oZ)
114  {
115  bool result1 = Solve(iA, iBx, 100000, oX);
116  bool result2 = Solve(iA, iBy, 100000, oY);
117  bool result3 = Solve(iA, iBz, 100000, oZ);
118 
119  return (result1 && result2 && result3);
120  }
121 
123  static bool
124  Solve(const MatrixType & iA, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
125  {
126  bool result1 = Solve(iA, iBx, oX);
127  bool result2 = Solve(iA, iBy, oY);
128 
129  return (result1 && result2);
130  }
131 
133  static bool
134  Solve(const MatrixType & iA, const VectorType & iB, const long & iNbIter, VectorType & oX)
135  {
136  using SparseLinearSystemType = vnl_sparse_matrix_linear_system<ValueType>;
137  SparseLinearSystemType system(iA, iB);
138 
139  SolverType solver(system);
140  solver.set_max_iterations(iNbIter);
141  return solver.minimize(oX);
142  }
143 };
144 
145 #endif
VNLIterativeSparseSolverTraits::InitializeSparseMatrix
static MatrixType InitializeSparseMatrix(const unsigned int &iRow, const unsigned int &iCol)
initialize a sparse matrix of size iRow x iCol
Definition: VNLIterativeSparseSolverTraits.h:67
VNLIterativeSparseSolverTraits::InitializeVector
static VectorType InitializeVector(const unsigned int &iN)
initialize a vector of size iN
Definition: VNLIterativeSparseSolverTraits.h:74
VNLIterativeSparseSolverTraits::Solve
static bool Solve(const MatrixType &iA, const VectorType &iB, VectorType &oX)
Solve the linear system .
Definition: VNLIterativeSparseSolverTraits.h:95
VNLIterativeSparseSolverTraits::AddToMatrix
static void AddToMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] += iV
Definition: VNLIterativeSparseSolverTraits.h:88
VNLIterativeSparseSolverTraits
Generic interface for iterative sparse linear solver.
Definition: VNLIterativeSparseSolverTraits.h:43
VNLIterativeSparseSolverTraits::FillMatrix
static void FillMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] = iV
Definition: VNLIterativeSparseSolverTraits.h:81
VNLIterativeSparseSolverTraits::Solve
static bool Solve(const MatrixType &iA, const VectorType &iB, const long &iNbIter, VectorType &oX)
Solve the linear systems: in N iterations.
Definition: VNLIterativeSparseSolverTraits.h:134
VNLIterativeSparseSolverTraits::SolverType
vnl_lsqr SolverType
Definition: VNLIterativeSparseSolverTraits.h:49
VNLIterativeSparseSolverTraits::InitializeSparseMatrix
static MatrixType InitializeSparseMatrix(const unsigned int &iN)
initialize a square sparse matrix of size iN x iN
Definition: VNLIterativeSparseSolverTraits.h:60
VNLIterativeSparseSolverTraits::Solve
static bool Solve(const MatrixType &iA, const VectorType &iBx, const VectorType &iBy, const VectorType &iBz, VectorType &oX, VectorType &oY, VectorType &oZ)
Solve the linear systems: , , .
Definition: VNLIterativeSparseSolverTraits.h:107
VNLIterativeSparseSolverTraits::Solve
static bool Solve(const MatrixType &iA, const VectorType &iBx, const VectorType &iBy, VectorType &oX, VectorType &oY)
Solve the linear systems: , .
Definition: VNLIterativeSparseSolverTraits.h:124
VNLIterativeSparseSolverTraits::MatrixType
vnl_sparse_matrix< ValueType > MatrixType
Definition: VNLIterativeSparseSolverTraits.h:47
VNLIterativeSparseSolverTraits::IsDirectSolver
static bool IsDirectSolver()
Definition: VNLIterativeSparseSolverTraits.h:53
VNLIterativeSparseSolverTraits::ValueType
T ValueType
Definition: VNLIterativeSparseSolverTraits.h:46
VNLIterativeSparseSolverTraits::VectorType
vnl_vector< ValueType > VectorType
Definition: VNLIterativeSparseSolverTraits.h:48