Index: trunk/SophyaLib/NTools/tmatrix.h
===================================================================
--- trunk/SophyaLib/NTools/tmatrix.h	(revision 286)
+++ trunk/SophyaLib/NTools/tmatrix.h	(revision 288)
@@ -16,4 +16,5 @@
 class TMatrix : public AnyDataObj {
 public:
+
   // Creation / destruction 
   TMatrix();
@@ -26,19 +27,25 @@
   // Temporaire?
   inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
-  inline void SetTemp(bool temp=false) const
-                     {mNDBlock.SetTemp(temp);}
+  inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
 
   // Gestion taille/Remplissage
-  void Clone(const TMatrix<T>& a);
-  void Reset(T v=0);
-  void Realloc(uint_4 r,uint_4 c,bool force_alloc=true);
+  inline void Clone(const TMatrix<T>& a)  // Clone: copie des donnees de "a"
+              {mNDBlock.Clone(a.mNDBlock); mNr = a.mNr; mNc = a.mNc;}
+  inline void Reset(T v=0) {mNDBlock.Reset(v);}
+  inline void ReSize(uint_4 r,uint_4 c)  // Reallocation de place
+       {if(r==0||c==0) throw(SzMismatchError("TMatrix::ReSize r ou c==0\n"));
+        mNr = r; mNc = c; mNDBlock.ReSize(r*c);}
 
   // Informations pointeur/data
   inline int NRows() const {return mNr;}
-  inline int NCol()  const {return mNc;}
-  T const& operator()(uint_4 r,uint_4 c) const
-                     {return *(mNDBlock.Begin()+r*mNc+c);}
-  T&       operator()(uint_4 r,uint_4 c)
-                     {return *(mNDBlock.Begin()+r*mNc+c);}
+  inline int NCols() const {return mNc;}
+  inline T const& operator()(uint_4 r,uint_4 c) const
+                            {return *(mNDBlock.Begin()+r*mNc+c);}
+  inline T&       operator()(uint_4 r,uint_4 c)
+                            {return *(mNDBlock.Begin()+r*mNc+c);}
+  inline T const& operator[](uint_4 ip) const
+                            {return *(mNDBlock.Begin()+ip);}
+  inline T&       operator[](uint_4 ip)
+                            {return *(mNDBlock.Begin()+ip);}
   inline       T* Data()       {return mNDBlock.Begin();}
   inline const T* Data() const {return mNDBlock.Begin();}
@@ -46,7 +53,16 @@
   inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
 
+  // Operations matricielles
+  TMatrix<T> Transpose(void) const;
+
   // Operateur d'affectation
-  TMatrix<T>& operator = (T x);
-  TMatrix<T>& operator = (const TMatrix<T>& a);
+  // A = x (matrice diagonale x*Identite)
+  inline TMatrix<T>& operator = (T x)
+    {if(mNr!=mNc || mNr==0) throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0\n"));
+     for(uint_4 r=0;r<mNr;r++) for(uint_4 c=0;c<mNc;c++) (*this)(r,c)=(r==c)?x:0;
+     return *this;}
+  // A = B : partage les donnees si "a" est temporaire, clone sinon.
+  inline TMatrix<T>& operator = (const TMatrix<T>& a)
+                {if(this == &a) return *this; CloneOrShare(a); return *this;}
 
   // Impression
@@ -55,15 +71,22 @@
               {Print(cout,lp,i0,ni,j0,nj);}
 
-  // Surcharge d'operateurs INPLACE
-  TMatrix<T>& operator += (T b);
-  TMatrix<T>& operator -= (T b);
-  TMatrix<T>& operator *= (T b);
-  TMatrix<T>& operator /= (T b);
+  // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) (T) x
+  inline TMatrix<T>& operator += (T b) {mNDBlock += b; return *this;}
+  inline TMatrix<T>& operator -= (T b) {mNDBlock -= b; return *this;}
+  inline TMatrix<T>& operator *= (T b) {mNDBlock *= b; return *this;}
+  inline TMatrix<T>& operator /= (T b) {mNDBlock /= b; return *this;}
 
-  TMatrix<T>& operator += (const TMatrix<T>& a);
-  TMatrix<T>& operator -= (const TMatrix<T>& a);
+  // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) B
+  inline TMatrix<T>& operator += (const TMatrix<T>& a)
+              {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
+                  throw(SzMismatchError("TMatrix::operator+=A size mismatch"));
+               mNDBlock += a.mNDBlock; return *this;}
+  inline TMatrix<T>& operator -= (const TMatrix<T>& a)
+              {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
+                  throw(SzMismatchError("TMatrix::operator-=A size mismatch"));
+               mNDBlock -= a.mNDBlock; return *this;}
   TMatrix<T>& operator *= (const TMatrix<T>& a);
 
-  // Pour surcharge d'operateurs
+  // Pour surcharge d'operateurs C = A (+,-,*) B
   TMatrix<T> Add(const TMatrix<T>& b) const;
   TMatrix<T> Sub(const TMatrix<T>& b) const;
@@ -71,75 +94,62 @@
 
 protected:
+  // partage les donnees si "a" temporaire, clone sinon.
+  inline void CloneOrShare(const TMatrix<T>& a)
+              {mNDBlock.CloneOrShare(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
+  // Share: partage les donnees de "a"
+  inline void Share(const TMatrix<T>& a)
+              {mNDBlock.Share(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
+
   uint_4 mNr,mNc;
   NDataBlock<T> mNDBlock;
 };
 
-/////////////////////////////////////////////////////////////////////////
-template<class T>
+////////////////////////////////////////////////////////////////
+// Impression
+
+template <class T>
 inline ostream& operator << (ostream& os, const TMatrix<T>& a)
-                      {a.Print(os); return(os);}
+                            {a.Print(os); return(os);}
 
-/////////////////////////////////////////////////////////////////////////
-template<class T>
-inline TMatrix<T> operator + (const TMatrix<T>& a, double b) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result += b;
-  return result;
-}
+////////////////////////////////////////////////////////////////
+// Surcharge d'operateurs A (+=,-=,*=,/=) (T) x
 
-template<class T>
-inline TMatrix<T> operator + (double b,const TMatrix<T>& a) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result += b;
-  return result;
-}
+template <class T> inline TMatrix<T> operator + (const TMatrix<T>& a, T b)
+    {TMatrix<T> result(a); result.SetTemp(true); result += b; return result;}
 
-template<class T>
-inline TMatrix<T> operator - (const TMatrix<T>& a, double b) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result -= b;
-  return result;
-}
+template <class T> inline TMatrix<T> operator + (T b,const TMatrix<T>& a)
+    {TMatrix<T> result(a); result.SetTemp(true); result += b; return result;}
 
-template<class T>
-inline TMatrix<T> operator - (double b,const TMatrix<T>& a) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result.DataBlock() = b - result.DataBlock();
-  return result;
-}
+template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a, T b)
+    {TMatrix<T> result(a); result.SetTemp(true); result -= b; return result;}
 
-template<class T>
-inline TMatrix<T> operator * (const TMatrix<T>& a, double b) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result *= b;
-  return result;
-}
+template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a)
+    {TMatrix<T> result(a); result.SetTemp(true);
+     result.DataBlock() = b-result.DataBlock(); return result;}
 
-template<class T>
-inline TMatrix<T> operator * (double b,const TMatrix<T>& a) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result *= b;
-  return result;
-}
+template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, T b)
+    {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
 
-template<class T>
-inline TMatrix<T> operator / (const TMatrix<T>& a, double b) {
-  TMatrix<T> result(a,false); result.DataBlock().SetTemp(true);
-  result /= b;
-  return result;
-}
+template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a)
+    {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
 
-/////////////////////////////////////////////////////////////////////////
-template<class T>
+template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b)
+    {TMatrix<T> result(a); result.SetTemp(true); result /= b; return result;}
+
+////////////////////////////////////////////////////////////////
+// Surcharge d'operateurs C = A (+,-,*,/) B
+
+template <class T>
 inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
-                             {return a.Add(b);}
+                  {return a.Add(b);}
 
-template<class T>
+template <class T>
 inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
-                             {return a.Sub(b);}
+                  {return a.Sub(b);}
 
-template<class T>
+template <class T>
 inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
-                             {return a.Mul(b);}
+                  {return a.Mul(b);}
+
 
 /////////////////////////////////////////////////////////////////////////
@@ -147,18 +157,15 @@
 template <class T>
 class FIO_TMatrix : public  PPersist  {
-
 public:
-	    FIO_TMatrix();
-	    FIO_TMatrix(string const & filename); 
-	    FIO_TMatrix(const TMatrix<T> & obj);
-	    FIO_TMatrix(TMatrix<T> * obj);
-  virtual   ~FIO_TMatrix();
-
-  virtual   AnyDataObj* DataObj();
+  FIO_TMatrix();
+  FIO_TMatrix(string const & filename); 
+  FIO_TMatrix(const TMatrix<T> & obj);
+  FIO_TMatrix(TMatrix<T> * obj);
+  virtual ~FIO_TMatrix();
+  virtual AnyDataObj* DataObj();
   inline operator TMatrix<T>() { return(*dobj); }
-
 protected :
-  virtual void       ReadSelf(PInPersist&);           
-  virtual void       WriteSelf(POutPersist&) const;  
+  virtual void ReadSelf(PInPersist&);           
+  virtual void WriteSelf(POutPersist&) const;  
   TMatrix<T> * dobj;
   bool ownobj;
