MathTL
|
00001 // -*- c++ -*- 00002 00003 // +--------------------------------------------------------------------+ 00004 // | This file is part of MathTL - the Mathematical Template Library | 00005 // | | 00006 // | Copyright (c) 2002-2009 | 00007 // | Thorsten Raasch, Manuel Werner | 00008 // +--------------------------------------------------------------------+ 00009 00010 #ifndef _MATHTL_SPARSE_MATRIX_H 00011 #define _MATHTL_SPARSE_MATRIX_H 00012 00013 #include <iostream> 00014 #include <list> 00015 00016 #include <utils/array1d.h> 00017 #include <algebra/vector.h> 00018 #include <algebra/matrix_block.h> 00019 #include <algebra/symmetric_matrix.h> 00020 #include <algebra/triangular_matrix.h> 00021 #include <algebra/infinite_vector.h> 00022 00023 // matrix norms, for convenience 00024 #include <algebra/matrix_norms.h> 00025 00026 namespace MathTL 00027 { 00038 template <class C> 00039 class SparseMatrix 00040 : public MatrixBlock<C> 00041 { 00042 public: 00046 typedef typename Vector<C>::size_type size_type; 00047 00051 explicit SparseMatrix(const size_type n = 1); 00052 00056 SparseMatrix(const SparseMatrix<C>& M); 00057 00061 SparseMatrix(const size_type row_dimension, const size_type column_dimension); 00062 00066 ~SparseMatrix(); 00067 00069 MatrixBlock<C>* clone() const; 00070 00072 MatrixBlock<C>* clone_transposed() const; 00073 00077 const size_type row_dimension() const; 00078 00082 const size_type column_dimension() const; 00083 00088 const size_type size() const; 00089 00093 bool empty() const; 00094 00098 void resize(const size_type rows, const size_type columns); 00099 00103 const size_type entries_in_row(const size_type row) const; 00104 00108 const size_type get_nth_index(const size_type row, const size_type n) const; 00109 00113 const C get_nth_entry(const size_type row, const size_type n) const; 00114 00118 const C get_entry(const size_type row, const size_type column) const; 00119 00123 template <class MATRIX> 00124 void get_block(const size_type firstrow, const size_type firstcolumn, 00125 const size_type rows, const size_type columns, 00126 MATRIX& M) const; 00127 00132 void get_row(const size_type row, 00133 InfiniteVector<C, size_type>& v, 00134 const size_type offset = 0) const; 00135 00139 void set_entry(const size_type row, const size_type column, const C value); 00140 00146 void set_row(const size_type row, 00147 const std::list<size_type>& indices, 00148 const std::list<C>& entries); 00149 00154 template <class MATRIX> 00155 void set_block(const size_type firstrow, const size_type firstcolumn, 00156 const MATRIX& M, 00157 const bool reflect = false, 00158 const double factor = 1.0); 00159 00163 SparseMatrix<C>& operator = (const SparseMatrix<C>& M); 00164 00168 void scale(const C s); 00169 00173 void add(const C s, const SparseMatrix<C>& M); 00174 00178 void diagonal(const size_type n, const C diag); 00179 00185 template <class VECTOR> 00186 void apply(const VECTOR& x, VECTOR& Mx) const; 00187 00189 void apply(const Vector<C>& x, Vector<C>& Mx) const; 00190 00196 template <class VECTOR> 00197 void apply_transposed(const VECTOR& x, VECTOR& Mtx) const; 00198 00200 void apply_transposed(const Vector<C>& x, Vector<C>& Mtx) const; 00201 00206 void compress(const double eta = 1e-16); 00207 00212 void print(std::ostream& os, 00213 const unsigned int tabwidth = 8, 00214 const unsigned int precision = 3) const; 00215 00226 void matlab_output(const char *file, const char *Matrixname, const int binary, const int rowend = -1, const int columnend = -1) const; 00227 void matlab_output(const char *file, const char *Matrixname, const int binary, const int rowstart, const int rowend, const int columnstart, const int columnend) const; 00228 00232 void matlab_input(const char *file); 00233 00234 protected: 00239 C** entries_; 00240 00247 size_type** indices_; 00248 00252 size_type rowdim_; 00253 00257 size_type coldim_; 00258 00262 void kill(); 00263 }; 00264 00268 template <class C> 00269 SparseMatrix<C> operator - (const SparseMatrix<C>& M, const SparseMatrix<C>& N); 00270 00274 template <class C> 00275 SparseMatrix<C> operator * (const SparseMatrix<C>& M, const SparseMatrix<C>& N); 00276 00280 template <class C> 00281 SparseMatrix<C> transpose(const SparseMatrix<C>& M); 00282 00286 template <class C> 00287 std::ostream& operator << (std::ostream& os, const SparseMatrix<C>& M); 00288 } 00289 00290 // include implementation of inline functions 00291 #include <algebra/sparse_matrix.cpp> 00292 00293 #endif