Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

operator/matrix.hh
Go to the documentation of this file.
00001 /* interface for a matrix
00002  */
00003 
00004 #ifndef matrix_hh
00005 #define matrix_hh
00006 
00007 #include "compositions.hh"
00008 #include "matrixIterator.hh"
00009 #include "space/elementPairs.hh"
00010 
00011 #define ApplyMatrix_D 0
00012 
00013 namespace concepts {
00014 
00015   // forward declarations
00016   template<typename F, typename G>
00017   class BilinearForm;
00018 
00019   class InOutParameters;
00020 
00021   // **************************************************************** Matrix **
00022 
00032   template<class F>
00033   class Matrix : public Operator<F> {
00034   public:
00035     typedef F                                       value_type;
00037     typedef typename Realtype<F>::type              r_type;
00039     typedef typename Cmplxtype<F>::type             c_type;
00040 
00041     typedef _Matrix_iterator<F, F&, F*>             iterator;
00042     typedef _Matrix_iterator<F, const F&, const F*> const_iterator;
00043 
00044     Matrix(uint nofRows, uint nofCols) : Operator<F>(nofRows, nofCols) {}
00045 
00047     const uint nofRows() const { return this->dimX(); }
00049     const uint nofCols() const { return this->dimY(); }
00050 
00055     iterator begin(uint r = 0) { return iterator(*this, r); }
00057     iterator end() { return iterator(*this, nofRows(), 0); }
00062     const_iterator begin(uint r = 0) const { return const_iterator(*this, r); }
00064     const_iterator end() const { 
00065       return const_iterator(*this, nofRows(), 0);
00066     }
00067 
00072     template<class G>
00073     static void assembly(Matrix<F>& dest, const Space<G>& spc,
00074                          BilinearForm<F,G>& bf, const Real threshold = 0.0);
00079     template<class G>
00080     static void assembly(Matrix<F>& dest,
00081                          const Space<G>& spcX, const Space<G>& spcY,
00082                          BilinearForm<F,G>& bf, const Real threshold = 0.0);
00088     template<class G>
00089     static void assembly(Matrix<F>& dest, BilinearForm<F,G>& bf,
00090                          const ElementPairList<G>& pairs);
00091 
00093     virtual F operator()(const uint i, const uint j) const = 0;
00095     virtual F& operator()(const uint i, const uint j) = 0;
00096 
00098     virtual void operator()(const Function<r_type>& fncY,
00099                             Function<F>& fncX) = 0;
00100     virtual void operator()(const Function<c_type>& fncY,
00101                             Function<c_type>& fncX) = 0;
00102 
00103     virtual bool operator==(const Matrix<F>& otherMat) const {
00104       uint rows = nofRows();
00105       uint cols = nofCols();
00106 
00107       if( rows != otherMat.nofRows() || cols != otherMat.nofCols() )
00108         return false;
00109 
00110       for(uint i=0; i<rows; i++)
00111         for(uint j=0; j<cols; j++)
00112           if( (*this)(i,j)!=otherMat(i,j) )
00113             return false;
00114 
00115       return true;
00116     }
00117 
00119     virtual void transpMult(const Vector<r_type>& fncY,
00120                             Vector<F>& fncX) = 0;
00121     virtual void transpMult(const Vector<c_type>& fncY,
00122                             Vector<c_type>& fncX) = 0;
00123 
00150     static void setTimings(InOutParameters* timings);
00154     static bool timings();
00156   private:
00158     static InOutParameters* timings_;
00160     static uint timeCntr_;
00161   };
00162 
00163   // **************************************************************** apply **
00164 
00173   template<class F, class H, class I>
00174   void apply(Operator<F>& op, const Matrix<H>& mX, Matrix<I>& mY) {
00175     // number of rows
00176     const uint n = op.dimX();
00177 
00178     // number of rows should be the same as for the solver
00179     conceptsAssert(mX.dimX() == n, Assertion());
00180     conceptsAssert(mY.dimX() == n, Assertion());
00181     // the resulting matrix should have the same number of columns
00182     // then the applicated matrix
00183     conceptsAssert(mX.dimY() == mY.dimY(), Assertion());
00184 
00185     // loop over columns
00186     Vector<H> fncX(mX.dimX());
00187     Vector<I> fncY(mY.dimX());
00188     I& f = fncY(0);
00189     for (uint c = 0; c < mX.dimY(); ++c) {
00190       // Write column of applicated matrix to vector.
00191       // Optimal would be a replacement by a method of the matrix.
00192       for (uint r = 0; r < n; ++r)
00193         fncX(r) = mX(r, c);
00194 
00195       DEBUGL(ApplyMatrix_D, "fncX = " << fncX);
00196       op(fncX, fncY);
00197       DEBUGL(ApplyMatrix_D, "fncY = " << fncY);
00198 
00199       // Add vector to column of resulting matrix, could be
00200       // Optimal would be a replacement by a method of the matrix.
00201       for (uint r = 0; r < n; ++r)
00202         if ((f = fncY(r)) != 0.0)
00203           mY(r, c) += f;
00204       DEBUGL(ApplyMatrix_D, "finished column " << c);
00205     }
00206   }
00207 
00208 } // namespace concepts
00209 
00210 #endif // matrix_hh

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)