1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | // C.Magneville 04/99
|
---|
3 | #ifndef TMATRIX_SEEN
|
---|
4 | #define TMATRIX_SEEN
|
---|
5 |
|
---|
6 | #include "machdefs.h"
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <iostream.h>
|
---|
9 | #include "ppersist.h"
|
---|
10 | #include "anydataobj.h"
|
---|
11 | #include "ndatablock.h"
|
---|
12 |
|
---|
13 | namespace PlanckDPC {
|
---|
14 |
|
---|
15 | template <class T>
|
---|
16 | class TMatrix : public AnyDataObj {
|
---|
17 | public:
|
---|
18 | // Creation / destruction
|
---|
19 | TMatrix();
|
---|
20 | TMatrix(uint_4 r,uint_4 c);
|
---|
21 | TMatrix(uint_4 r,uint_4 c,T* values,Bridge* br=NULL);
|
---|
22 | TMatrix(const TMatrix<T>& a);
|
---|
23 | TMatrix(const TMatrix<T>& a,bool share);
|
---|
24 | virtual ~TMatrix();
|
---|
25 |
|
---|
26 | // Temporaire?
|
---|
27 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
28 | inline void SetTemp(bool temp=false) const
|
---|
29 | {mNDBlock.SetTemp(temp);}
|
---|
30 |
|
---|
31 | // Gestion taille/Remplissage
|
---|
32 | void Clone(const TMatrix<T>& a);
|
---|
33 | void Reset(T v=0);
|
---|
34 | void Realloc(uint_4 r,uint_4 c,bool force_alloc=true);
|
---|
35 |
|
---|
36 | // Informations pointeur/data
|
---|
37 | inline int NRows() const {return mNr;}
|
---|
38 | inline int NCol() const {return mNc;}
|
---|
39 | T const& operator()(uint_4 r,uint_4 c) const
|
---|
40 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
41 | T& operator()(uint_4 r,uint_4 c)
|
---|
42 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
43 | inline T* Data() {return mNDBlock.Begin();}
|
---|
44 | inline const T* Data() const {return mNDBlock.Begin();}
|
---|
45 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
46 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
47 |
|
---|
48 | // Operateur d'affectation
|
---|
49 | TMatrix<T>& operator = (T x);
|
---|
50 | TMatrix<T>& operator = (const TMatrix<T>& a);
|
---|
51 |
|
---|
52 | // Impression
|
---|
53 | void Print(ostream& os,int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const;
|
---|
54 | inline void Print(int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const
|
---|
55 | {Print(cout,lp,i0,ni,j0,nj);}
|
---|
56 |
|
---|
57 | // Surcharge d'operateurs INPLACE
|
---|
58 | TMatrix<T>& operator += (T b);
|
---|
59 | TMatrix<T>& operator -= (T b);
|
---|
60 | TMatrix<T>& operator *= (T b);
|
---|
61 | TMatrix<T>& operator /= (T b);
|
---|
62 |
|
---|
63 | TMatrix<T>& operator += (const TMatrix<T>& a);
|
---|
64 | TMatrix<T>& operator -= (const TMatrix<T>& a);
|
---|
65 | TMatrix<T>& operator *= (const TMatrix<T>& a);
|
---|
66 |
|
---|
67 | // Pour surcharge d'operateurs
|
---|
68 | TMatrix<T> Add(const TMatrix<T>& b) const;
|
---|
69 | TMatrix<T> Sub(const TMatrix<T>& b) const;
|
---|
70 | TMatrix<T> Mul(const TMatrix<T>& b) const;
|
---|
71 |
|
---|
72 | protected:
|
---|
73 | uint_4 mNr,mNc;
|
---|
74 | NDataBlock<T> mNDBlock;
|
---|
75 | };
|
---|
76 |
|
---|
77 | /////////////////////////////////////////////////////////////////////////
|
---|
78 | template<class T>
|
---|
79 | inline ostream& operator << (ostream& os, const TMatrix<T>& a)
|
---|
80 | {a.Print(os); return(os);}
|
---|
81 |
|
---|
82 | /////////////////////////////////////////////////////////////////////////
|
---|
83 | template<class T>
|
---|
84 | inline TMatrix<T> operator + (const TMatrix<T>& a, double b) {
|
---|
85 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
86 | result += b;
|
---|
87 | return result;
|
---|
88 | }
|
---|
89 |
|
---|
90 | template<class T>
|
---|
91 | inline TMatrix<T> operator + (double b,const TMatrix<T>& a) {
|
---|
92 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
93 | result += b;
|
---|
94 | return result;
|
---|
95 | }
|
---|
96 |
|
---|
97 | template<class T>
|
---|
98 | inline TMatrix<T> operator - (const TMatrix<T>& a, double b) {
|
---|
99 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
100 | result -= b;
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 |
|
---|
104 | template<class T>
|
---|
105 | inline TMatrix<T> operator - (double b,const TMatrix<T>& a) {
|
---|
106 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
107 | result.DataBlock() = b - result.DataBlock();
|
---|
108 | return result;
|
---|
109 | }
|
---|
110 |
|
---|
111 | template<class T>
|
---|
112 | inline TMatrix<T> operator * (const TMatrix<T>& a, double b) {
|
---|
113 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
114 | result *= b;
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | template<class T>
|
---|
119 | inline TMatrix<T> operator * (double b,const TMatrix<T>& a) {
|
---|
120 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
121 | result *= b;
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 |
|
---|
125 | template<class T>
|
---|
126 | inline TMatrix<T> operator / (const TMatrix<T>& a, double b) {
|
---|
127 | TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
|
---|
128 | result /= b;
|
---|
129 | return result;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /////////////////////////////////////////////////////////////////////////
|
---|
133 | template<class T>
|
---|
134 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
135 | {return a.Add(b);}
|
---|
136 |
|
---|
137 | template<class T>
|
---|
138 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
139 | {return a.Sub(b);}
|
---|
140 |
|
---|
141 | template<class T>
|
---|
142 | inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
143 | {return a.Mul(b);}
|
---|
144 |
|
---|
145 | /////////////////////////////////////////////////////////////////////////
|
---|
146 | // Classe pour la gestion de persistance
|
---|
147 | template <class T>
|
---|
148 | class FIO_TMatrix : public PPersist {
|
---|
149 |
|
---|
150 | public:
|
---|
151 | FIO_TMatrix();
|
---|
152 | FIO_TMatrix(string const & filename);
|
---|
153 | FIO_TMatrix(const TMatrix<T> & obj);
|
---|
154 | FIO_TMatrix(TMatrix<T> * obj);
|
---|
155 | virtual ~FIO_TMatrix();
|
---|
156 |
|
---|
157 | virtual AnyDataObj* DataObj();
|
---|
158 | inline operator TMatrix<T>() { return(*dobj); }
|
---|
159 |
|
---|
160 | protected :
|
---|
161 | virtual void ReadSelf(PInPersist&);
|
---|
162 | virtual void WriteSelf(POutPersist&) const;
|
---|
163 | TMatrix<T> * dobj;
|
---|
164 | bool ownobj;
|
---|
165 | };
|
---|
166 |
|
---|
167 | } // Fin du namespace
|
---|
168 |
|
---|
169 | #endif
|
---|