ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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// Deprecation bridge: VNLSparseLUSolverTraits (vnl_sparse_lu engine) is being
22// replaced by SparseLUSolverTraits (Eigen::SparseLU), proven faster and
23// more accurate. Eigen remains always available; only this VNL spelling is
24// gated. The guard is silent by default, warns under ITK_LEGACY_REMOVE, and
25// errors under ITK_FUTURE_LEGACY_REMOVE; ITK_LEGACY_TEST/SILENT opt out.
26#if __has_include(<itkConfigure.h>)
27# include <itkConfigure.h>
28# if defined(ITK_FUTURE_LEGACY_REMOVE)
29# error "VNLSparseLUSolverTraits is removed; use SparseLUSolverTraits (SparseLUSolverTraits.h, Eigen::SparseLU)."
30# elif defined(ITK_LEGACY_REMOVE) && !defined(ITK_LEGACY_SILENT) && !defined(ITK_LEGACY_TEST)
31# if defined(_MSC_VER)
32# pragma message("VNLSparseLUSolverTraits is deprecated; migrate to SparseLUSolverTraits (Eigen::SparseLU).")
33# else
34# warning "VNLSparseLUSolverTraits is deprecated; migrate to SparseLUSolverTraits (Eigen::SparseLU)."
35# endif
36# endif
37#endif
38
39#include "vnl/vnl_vector.h"
40#include "vnl/vnl_sparse_matrix.h"
41#include "vnl/algo/vnl_sparse_lu.h"
42
59template <typename T = double>
61{
62public:
63 using ValueType = T;
64 using MatrixType = vnl_sparse_matrix<ValueType>;
65 using VectorType = vnl_vector<ValueType>;
66 using SolverType = vnl_sparse_lu;
67
69 static bool
71 {
72 return true;
73 }
74
76 static MatrixType
77 InitializeSparseMatrix(const unsigned int & iN)
78 {
79 return MatrixType(iN, iN);
80 }
81
83 static MatrixType
84 InitializeSparseMatrix(const unsigned int & iRow, const unsigned int & iCol)
85 {
86 return MatrixType(iRow, iCol);
87 }
88
90 static VectorType
91 InitializeVector(const unsigned int & iN)
92 {
93 return VectorType(iN);
94 }
95
97 static void
98 FillMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
99 {
100 iA(iR, iC) = iV;
101 }
102
104 static void
105 AddToMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
106 {
107 iA(iR, iC) += iV;
108 }
109
111 static void
112 MatVecMult(const MatrixType & iA, const VectorType & iB, VectorType & oX)
113 {
114 iA.mult(iB, oX);
115 }
116
118 static bool
119 Solve(const MatrixType & iA, const VectorType & iB, VectorType & oX)
120 {
121 SolverType solver(iA);
122 Solve(solver, iB, oX);
123
124 return true;
125 }
126
129 static bool
130 Solve(const MatrixType & iA,
131 const VectorType & iBx,
132 const VectorType & iBy,
133 const VectorType & iBz,
134 VectorType & oX,
135 VectorType & oY,
136 VectorType & oZ)
137 {
138 SolverType solver(iA);
139 Solve(solver, iBx, iBy, iBz, oX, oY, oZ);
140
141 return true;
142 }
143
145 static bool
146 Solve(const MatrixType & iA, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
147 {
148 SolverType solver(iA);
149 Solve(solver, iBx, iBy, oX, oY);
150
151 return true;
152 }
153
155 static void
156 Solve(SolverType & solver, const VectorType & iB, VectorType & oX)
157 {
158 oX = solver.solve(iB);
159 }
160
163 static void
165 const VectorType & iBx,
166 const VectorType & iBy,
167 const VectorType & iBz,
168 VectorType & oX,
169 VectorType & oY,
170 VectorType & oZ)
171 {
172 oX = solver.solve(iBx);
173 oY = solver.solve(iBy);
174 oZ = solver.solve(iBz);
175 }
176
179 static void
180 Solve(SolverType & solver, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
181 {
182 oX = solver.solve(iBx);
183 oY = solver.solve(iBy);
184 }
185};
186
187#endif
Generic interface for sparse LU solver.
static MatrixType InitializeSparseMatrix(const unsigned int &iRow, const unsigned int &iCol)
initialize a sparse matrix of size iRow x iCol
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.
static bool Solve(const MatrixType &iA, const VectorType &iB, VectorType &oX)
Solve the linear system .
vnl_vector< ValueType > VectorType
vnl_sparse_matrix< ValueType > MatrixType
static void Solve(SolverType &solver, const VectorType &iB, VectorType &oX)
Solve the linear system factoring the internal matrix if needed.
static MatrixType InitializeSparseMatrix(const unsigned int &iN)
initialize a square sparse matrix of size iN x iN
static void MatVecMult(const MatrixType &iA, const VectorType &iB, VectorType &oX)
oX = iA * iB
static void FillMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] = iV
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: , , .
static bool Solve(const MatrixType &iA, const VectorType &iBx, const VectorType &iBy, VectorType &oX, VectorType &oY)
Solve the linear systems: , .
static VectorType InitializeVector(const unsigned int &iN)
initialize a vector of size iN
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.
static void AddToMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] += iV