ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
SparseLUSolverTraits.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 SparseLUSolverTraits_h
19#define SparseLUSolverTraits_h
20
21#include "itk_eigen.h"
22#include ITK_EIGEN(Sparse)
23#include ITK_EIGEN(SparseLU)
24
37template <typename T = double>
39{
40public:
41 using ValueType = T;
42 using MatrixType = Eigen::SparseMatrix<ValueType>;
43 using VectorType = Eigen::Matrix<ValueType, Eigen::Dynamic, 1>;
44 using SolverType = Eigen::SparseLU<MatrixType>;
45
46 static bool
48 {
49 return true;
50 }
51
53 static MatrixType
54 InitializeSparseMatrix(const unsigned int & iN)
55 {
56 return MatrixType(iN, iN);
57 }
58
60 static MatrixType
61 InitializeSparseMatrix(const unsigned int & iRow, const unsigned int & iCol)
62 {
63 return MatrixType(iRow, iCol);
64 }
65
67 static VectorType
68 InitializeVector(const unsigned int & iN)
69 {
70 return VectorType(iN);
71 }
72
74 static void
75 FillMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
76 {
77 iA.coeffRef(iR, iC) = iV;
78 }
79
81 static void
82 AddToMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV)
83 {
84 iA.coeffRef(iR, iC) += iV;
85 }
86
88 static void
89 MatVecMult(const MatrixType & iA, const VectorType & iB, VectorType & oX)
90 {
91 oX = iA * iB;
92 }
93
95 static bool
96 Solve(const MatrixType & iA, const VectorType & iB, VectorType & oX)
97 {
98 // Eigen::SparseLU copies and compresses the matrix internally
99 // (analyzePattern), so no manual copy/compress is needed here.
100 SolverType solver(iA);
101 oX = solver.solve(iB);
102 return solver.info() == Eigen::Success;
103 }
104
107 static bool
108 Solve(const MatrixType & iA,
109 const VectorType & iBx,
110 const VectorType & iBy,
111 const VectorType & iBz,
112 VectorType & oX,
113 VectorType & oY,
114 VectorType & oZ)
115 {
116 // Eigen::SparseLU copies and compresses the matrix internally.
117 SolverType solver(iA);
118 Solve(solver, iBx, iBy, iBz, oX, oY, oZ);
119 return solver.info() == Eigen::Success;
120 }
121
123 static bool
124 Solve(const MatrixType & iA, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
125 {
126 // Eigen::SparseLU copies and compresses the matrix internally.
127 SolverType solver(iA);
128 Solve(solver, iBx, iBy, oX, oY);
129 return solver.info() == Eigen::Success;
130 }
131
133 static void
134 Solve(SolverType & solver, const VectorType & iB, VectorType & oX)
135 {
136 oX = solver.solve(iB);
137 }
138
141 static void
143 const VectorType & iBx,
144 const VectorType & iBy,
145 const VectorType & iBz,
146 VectorType & oX,
147 VectorType & oY,
148 VectorType & oZ)
149 {
150 oX = solver.solve(iBx);
151 oY = solver.solve(iBy);
152 oZ = solver.solve(iBz);
153 }
154
157 static void
158 Solve(SolverType & solver, const VectorType & iBx, const VectorType & iBy, VectorType & oX, VectorType & oY)
159 {
160 oX = solver.solve(iBx);
161 oY = solver.solve(iBy);
162 }
163};
164
165#endif
Generic interface for sparse LU solver backed by Eigen::SparseLU.
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 &iB, VectorType &oX)
Solve the linear system reusing the factored matrix.
static void Solve(SolverType &solver, const VectorType &iBx, const VectorType &iBy, VectorType &oX, VectorType &oY)
Solve the linear systems: , reusing the factored matrix.
static bool Solve(const MatrixType &iA, const VectorType &iB, VectorType &oX)
Solve the linear system .
static void MatVecMult(const MatrixType &iA, const VectorType &iB, VectorType &oX)
oX = iA * iB
static void AddToMatrix(MatrixType &iA, const unsigned int &iR, const unsigned int &iC, const ValueType &iV)
iA[iR][iC] += iV
static VectorType InitializeVector(const unsigned int &iN)
initialize a vector of size iN
Eigen::SparseMatrix< ValueType > MatrixType
Eigen::SparseLU< MatrixType > SolverType
static MatrixType InitializeSparseMatrix(const unsigned int &iN)
initialize a square sparse matrix of size iN x iN
Eigen::Matrix< ValueType, Eigen::Dynamic, 1 > VectorType
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 void Solve(SolverType &solver, const VectorType &iBx, const VectorType &iBy, const VectorType &iBz, VectorType &oX, VectorType &oY, VectorType &oZ)
Solve the linear systems: , , reusing the factored matrix.
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, VectorType &oX, VectorType &oY)
Solve the linear systems: , .