ITK  5.4.0
Insight Toolkit
VNLSparseLUSolverTraits.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 VNLSparseLUSolverTraits_h
19 #define VNLSparseLUSolverTraits_h
20 
21 #include "vnl/vnl_vector.h"
22 #include "vnl/vnl_sparse_matrix.h"
23 #include "vnl/algo/vnl_sparse_lu.h"
24 
41 template <typename T = double>
43 {
44 public:
45  using ValueType = T;
46  using MatrixType = vnl_sparse_matrix<ValueType>;
47  using VectorType = vnl_vector<ValueType>;
48  using SolverType = vnl_sparse_lu;
49 
51  static bool
53  {
54  return true;
55  }
56 
58  static MatrixType
59  InitializeSparseMatrix(const unsigned int & iN)
60  {
61  return MatrixType(iN, iN);
62  }
63 
65  static MatrixType
66  InitializeSparseMatrix(const unsigned int & iRow, const unsigned int & iCol)
67  {
68  return MatrixType(iRow, iCol);
69  }
70 
72  static VectorType
73  InitializeVector(const unsigned int & iN)
74  {
75  return VectorType(iN);
76  }
77 
79  static void
80  FillMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
81  {
82  iA(iR, iC) = iV;
83  }
84 
86  static void
87  AddToMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
88  {
89  iA(iR, iC) += iV;
90  }
91 
93  static bool
94  Solve(const MatrixType & iA, const VectorType & iB, VectorType & oX)
95  {
96  SolverType solver(iA);
97  Solve(solver, iB, oX);
98 
99  return true;
100  }
101 
104  static bool
105  Solve(const MatrixType & iA,
106  const VectorType & iBx,
107  const VectorType & iBy,
108  const VectorType & iBz,
109  VectorType & oX,
110  VectorType & oY,
111  VectorType & oZ)
112  {
113  SolverType solver(iA);
114  Solve(solver, iBx, iBy, iBz, oX, oY, oZ);
115 
116  return true;
117  }
118 
120  static bool
121  Solve(const MatrixType & iA, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
122  {
123  SolverType solver(iA);
124  Solve(solver, iBx, iBy, oX, oY);
125 
126  return true;
127  }
128 
130  static void
131  Solve(SolverType & solver, const VectorType & iB, VectorType & oX)
132  {
133  oX = solver.solve(iB);
134  }
135 
138  static void
139  Solve(SolverType & solver,
140  const VectorType & iBx,
141  const VectorType & iBy,
142  const VectorType & iBz,
143  VectorType & oX,
144  VectorType & oY,
145  VectorType & oZ)
146  {
147  oX = solver.solve(iBx);
148  oY = solver.solve(iBy);
149  oZ = solver.solve(iBz);
150  }
151 
154  static void
155  Solve(SolverType & solver, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
156  {
157  oX = solver.solve(iBx);
158  oY = solver.solve(iBy);
159  }
160 };
161 
162 #endif
VNLSparseLUSolverTraits::MatrixType
vnl_sparse_matrix< ValueType > MatrixType
Definition: VNLSparseLUSolverTraits.h:46
VNLSparseLUSolverTraits::VectorType
vnl_vector< ValueType > VectorType
Definition: VNLSparseLUSolverTraits.h:47
VNLSparseLUSolverTraits::Solve
static void Solve(SolverType &solver, const VectorType &iBx, const VectorType &iBy, const VectorType &iBz, VectorType &oX, VectorType &oY, VectorType &oZ)
Solve the linear systems: , , factoring the internal matrix if needed.
Definition: VNLSparseLUSolverTraits.h:139
VNLSparseLUSolverTraits::FillMatrix
static void FillMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] = iV
Definition: VNLSparseLUSolverTraits.h:80
VNLSparseLUSolverTraits::ValueType
T ValueType
Definition: VNLSparseLUSolverTraits.h:45
VNLSparseLUSolverTraits::Solve
static bool Solve(const MatrixType &iA, const VectorType &iBx, const VectorType &iBy, VectorType &oX, VectorType &oY)
Solve the linear systems: , .
Definition: VNLSparseLUSolverTraits.h:121
VNLSparseLUSolverTraits::IsDirectSolver
static bool IsDirectSolver()
Definition: VNLSparseLUSolverTraits.h:52
VNLSparseLUSolverTraits::InitializeVector
static VectorType InitializeVector(const unsigned int &iN)
initialize a vector of size iN
Definition: VNLSparseLUSolverTraits.h:73
VNLSparseLUSolverTraits::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: VNLSparseLUSolverTraits.h:105
VNLSparseLUSolverTraits::SolverType
vnl_sparse_lu SolverType
Definition: VNLSparseLUSolverTraits.h:48
VNLSparseLUSolverTraits::InitializeSparseMatrix
static MatrixType InitializeSparseMatrix(const unsigned int &iN)
initialize a square sparse matrix of size iN x iN
Definition: VNLSparseLUSolverTraits.h:59
VNLSparseLUSolverTraits::InitializeSparseMatrix
static MatrixType InitializeSparseMatrix(const unsigned int &iRow, const unsigned int &iCol)
initialize a sparse matrix of size iRow x iCol
Definition: VNLSparseLUSolverTraits.h:66
VNLSparseLUSolverTraits::AddToMatrix
static void AddToMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] += iV
Definition: VNLSparseLUSolverTraits.h:87
VNLSparseLUSolverTraits::Solve
static void Solve(SolverType &solver, const VectorType &iBx, const VectorType &iBy, VectorType &oX, VectorType &oY)
Solve the linear systems: , factoring the internal matrix if needed.
Definition: VNLSparseLUSolverTraits.h:155
VNLSparseLUSolverTraits::Solve
static void Solve(SolverType &solver, const VectorType &iB, VectorType &oX)
Solve the linear system factoring the internal matrix if needed.
Definition: VNLSparseLUSolverTraits.h:131
VNLSparseLUSolverTraits::Solve
static bool Solve(const MatrixType &iA, const VectorType &iB, VectorType &oX)
Solve the linear system .
Definition: VNLSparseLUSolverTraits.h:94
VNLSparseLUSolverTraits
Generic interface for sparse LU solver.
Definition: VNLSparseLUSolverTraits.h:42