ITK  4.1.0
Insight Segmentation and Registration Toolkit
VNLIterativeSparseSolverTraits.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __VNLIterativeSparseSolverTraits_h
00019 #define __VNLIterativeSparseSolverTraits_h
00020 
00021 #include "vnl/vnl_vector.h"
00022 #include "vnl/vnl_sparse_matrix.h"
00023 #include "vnl/vnl_sparse_matrix_linear_system.h"
00024 #include "vnl/algo/vnl_lsqr.h"
00025 
00026 template< typename T = double >
00027 class VNLIterativeSparseSolverTraits
00028 {
00029 public:
00030   typedef T                              ValueType;
00031   typedef vnl_sparse_matrix< ValueType > MatrixType;
00032   typedef vnl_vector< ValueType >        VectorType;
00033   typedef vnl_lsqr                       SolverType;
00034 
00035   VNLIterativeSparseSolverTraits() {}
00036   ~VNLIterativeSparseSolverTraits() {}
00037 
00038   bool IsDirectSolver() const
00039   {
00040     return false;
00041   }
00042 
00043   MatrixType InitializeSparseMatrix(const unsigned int & iN) const
00044   {
00045     return MatrixType(iN, iN);
00046   }
00047 
00048   VectorType InitializeVector(const unsigned int & iN) const
00049   {
00050     return VectorType(iN);
00051   }
00052 
00053   void FillMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV) const
00054   {
00055     iA(iR, iC) = iV;
00056   }
00057 
00058   void AddToMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV) const
00059   {
00060     iA(iR, iC) += iV;
00061   }
00062 
00063   bool Solve(const MatrixType & iA, const VectorType & iB, VectorType & oX) const
00064   {
00065     typedef vnl_sparse_matrix_linear_system< ValueType > SparseLinearSystemType;
00066     SparseLinearSystemType system(iA, iB);
00067 
00068     SolverType solver(system);
00069     return solver.minimize(oX);
00070   }
00071 
00072   // no interest to use this method...
00073   bool Solve(const MatrixType & iA,
00074              const VectorType & iBx, const VectorType & iBy,
00075              VectorType & oX, VectorType & oY) const
00076   {
00077     bool result1 = Solve(iA, iBx, oX);
00078     bool result2 = Solve(iA, iBy, oY);
00079 
00080     return ( result1 && result2 );
00081   }
00082 
00083   bool Solve(const MatrixType & iA,
00084              const VectorType & iB,
00085              const long & iNbIter,
00086              VectorType & oX) const
00087   {
00088     typedef vnl_sparse_matrix_linear_system< ValueType > SparseLinearSystemType;
00089     SparseLinearSystemType system(iA, iB);
00090 
00091     SolverType solver(system);
00092     solver.set_max_iterations(iNbIter);
00093     return solver.minimize(oX);
00094   }
00095 };
00096 
00097 #endif
00098