| 1 | #ifndef SMTX_H_SEEN 
 | 
|---|
| 2 | #define SMTX_H_SEEN 
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include "machdefs.h"
 | 
|---|
| 5 | #include <iostream>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "ndatablock.h"
 | 
|---|
| 8 | #include "sopnamsp.h"
 | 
|---|
| 9 | #include "ctimer.h"
 | 
|---|
| 10 | #include "timing.h"
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include "jet.h"
 | 
|---|
| 13 | 
 | 
|---|
| 14 | 
 | 
|---|
| 15 | //========================================================
 | 
|---|
| 16 | //   Test d'expression template sur tableaux 
 | 
|---|
| 17 | //       Reza - Avril 2003
 | 
|---|
| 18 | //========================================================
 | 
|---|
| 19 | 
 | 
|---|
| 20 | 
 | 
|---|
| 21 | // ---------------------------------------------------
 | 
|---|
| 22 | //------- Expression/iterateur sur matrice -----------
 | 
|---|
| 23 | // ---------------------------------------------------
 | 
|---|
| 24 | template <class T>
 | 
|---|
| 25 | class JETExpMtx {
 | 
|---|
| 26 | public:
 | 
|---|
| 27 |   JETExpMtx() : db_(NULL), msz_(NULL), pd_(NULL) { } 
 | 
|---|
| 28 |   //cout << " DBG-AA" << hex << db_ << " - " << msz_ << dec << endl;}
 | 
|---|
| 29 |   JETExpMtx(NDataBlock<T>   * db, MtxSize const * msz) : 
 | 
|---|
| 30 |     db_(db), msz_(msz), pd_(NULL) { } 
 | 
|---|
| 31 |   // cout << " DBG-BBB" << hex << db_ << " - " << msz_ << dec << endl; }
 | 
|---|
| 32 |   JETExpMtx(JETExpMtx const & a) : db_(a.db_), msz_(a.msz_), pd_(a.pd_) { } 
 | 
|---|
| 33 |   // cout << " DBG-CCC" << hex << db_ << " - " << msz_ << dec << endl;}
 | 
|---|
| 34 |   inline T EvaluateAndStep() const { return (*pd_++); }
 | 
|---|
| 35 |   inline void Reset() const { pd_ = db_->Begin(); } 
 | 
|---|
| 36 |   //{ if (db_ == NULL) cout << " AAA-BUG db_" << endl;
 | 
|---|
| 37 |   //pd_ = db_->Begin(); if (pd_ == NULL) cout << " BBB-BUG db_" << endl;} 
 | 
|---|
| 38 |   inline MtxSize const & RefSize()  const { return *msz_; } 
 | 
|---|
| 39 |   // if (msz_ == NULL) cout << " BUG msz_ " << endl; return *msz_; }
 | 
|---|
| 40 |   void JETExpMtxSet(NDataBlock<T> * db, MtxSize const * msz)
 | 
|---|
| 41 |   { db_ = db; msz_ = msz; pd_ = NULL; }
 | 
|---|
| 42 | protected:
 | 
|---|
| 43 |   NDataBlock<T> * db_;
 | 
|---|
| 44 |   MtxSize const * msz_;
 | 
|---|
| 45 |   mutable T * pd_;
 | 
|---|
| 46 | };
 | 
|---|
| 47 | 
 | 
|---|
| 48 | 
 | 
|---|
| 49 | // ---------------------------------------------------
 | 
|---|
| 50 | // ------------- Classe de matrice -------------------
 | 
|---|
| 51 | // ---------------------------------------------------
 | 
|---|
| 52 | 
 | 
|---|
| 53 | template <class T>
 | 
|---|
| 54 | class SimpleMatrix : public AnyDataObj, public MtxSize, 
 | 
|---|
| 55 |                      public JETExpression<T, JETExpMtx<T> > {
 | 
|---|
| 56 | public:
 | 
|---|
| 57 |                 SimpleMatrix();
 | 
|---|
| 58 |                 SimpleMatrix(sa_size_t nr, sa_size_t nc);
 | 
|---|
| 59 |                 SimpleMatrix(const SimpleMatrix<T> & m);
 | 
|---|
| 60 |                 SimpleMatrix(const SimpleMatrix<T> & m, bool share);
 | 
|---|
| 61 |   virtual       ~SimpleMatrix();
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   virtual void ReSize(const MtxSize & a);
 | 
|---|
| 64 |   
 | 
|---|
| 65 |   virtual SimpleMatrix<T>& Set(const SimpleMatrix<T> & a);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   inline  SimpleMatrix<T>& Set(JETEvaluator const &  exp)  
 | 
|---|
| 68 |   {  exp.EvaluateAll(*this);  return(*this); }
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   virtual SimpleMatrix<T>& Init(T c);
 | 
|---|
| 71 |   virtual SimpleMatrix<T>& Init(T c, T step);
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   inline SimpleMatrix<T>& operator = (const SimpleMatrix<T> & a)
 | 
|---|
| 74 |   { return Set(a); }
 | 
|---|
| 75 |   inline SimpleMatrix<T>& operator = (JETEvaluator const & a)
 | 
|---|
| 76 |   { return Set(a); }
 | 
|---|
| 77 |   inline SimpleMatrix<T>& operator = (T c)
 | 
|---|
| 78 |   { return Init(c); }
 | 
|---|
| 79 |  
 | 
|---|
| 80 |   inline T operator()(int r, int c) const 
 | 
|---|
| 81 |                 { return data_(r*ncol_+c); }
 | 
|---|
| 82 |   inline T& operator()(int r, int c) 
 | 
|---|
| 83 |                 { return data_(r*ncol_+c); }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   inline sa_size_t NRows() const {return nrow_; }
 | 
|---|
| 86 |   inline sa_size_t NCols() const {return ncol_; }
 | 
|---|
| 87 |   inline sa_size_t Size() const {return nrow_*ncol_; }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   virtual void Print(ostream& os) const;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   inline NDataBlock<T> const & DataBlock() const { return data_; }
 | 
|---|
| 92 |   inline NDataBlock<T>& DataBlock() { return data_; }
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   SimpleMatrix<T>& AddCst(T c);
 | 
|---|
| 95 |   SimpleMatrix<T>& MulCst(T c);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   SimpleMatrix<T>& AddElt(const SimpleMatrix<T> & b);
 | 
|---|
| 98 |   SimpleMatrix<T>& MulElt(const SimpleMatrix<T> & b);
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   inline SimpleMatrix<T>& operator += (T c) 
 | 
|---|
| 101 |   { return AddCst(c); }
 | 
|---|
| 102 |   inline SimpleMatrix<T>& operator *= (T c) 
 | 
|---|
| 103 |   { return MulCst(c); }
 | 
|---|
| 104 |   inline SimpleMatrix<T>& operator += (const SimpleMatrix<T> & b)
 | 
|---|
| 105 |   { return AddElt(b); }
 | 
|---|
| 106 |   inline SimpleMatrix<T>& operator *= (const SimpleMatrix<T> & b)
 | 
|---|
| 107 |   { return MulElt(b); }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | protected:
 | 
|---|
| 110 |   NDataBlock<T> data_;
 | 
|---|
| 111 |   JETExpMtx<T> dbv_;
 | 
|---|
| 112 | };
 | 
|---|
| 113 | 
 | 
|---|
| 114 | 
 | 
|---|
| 115 | template <class T>
 | 
|---|
| 116 | inline ostream& operator << (ostream& os, const SimpleMatrix<T> & m)
 | 
|---|
| 117 | { m.Print(os); return(os); }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | // -------------------------------------------------------------
 | 
|---|
| 120 | template <class T, class E>
 | 
|---|
| 121 | void JETExpression<T,E>::EvaluateAll(AnyDataObj& dest) const
 | 
|---|
| 122 |   {
 | 
|---|
| 123 |     //  cout << " JETExpression<T,E>::EvaluateAll(AnyDataObj& dest) " << endl;
 | 
|---|
| 124 |     SimpleMatrix<T>* dmx = dynamic_cast<SimpleMatrix<T> *>(&dest);
 | 
|---|
| 125 |     if (dmx == NULL) throw ParmError("SMExprMult<T>::EvaluateAll() - dest not a SimpleMatrix");
 | 
|---|
| 126 |     //  cout << " --- Doing Reset() " << endl;
 | 
|---|
| 127 |     e_.Reset();
 | 
|---|
| 128 |     //  cout << " --- Reset() OK " << endl;
 | 
|---|
| 129 |     dmx->ReSize(e_.RefSize());
 | 
|---|
| 130 |     //  cout << " --- Resize OK " << endl;
 | 
|---|
| 131 |     for(sa_size_t k=0; k<dmx->Size(); k++) dmx->DataBlock()(k) = e_.EvaluateAndStep();
 | 
|---|
| 132 |   }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | 
 | 
|---|
| 135 | #endif
 | 
|---|