Index: /trunk/SophyaLib/TArray/tarray.cc
===================================================================
--- /trunk/SophyaLib/TArray/tarray.cc	(revision 969)
+++ /trunk/SophyaLib/TArray/tarray.cc	(revision 970)
@@ -140,9 +140,9 @@
 TArray<T>& TArray<T>::Set(const TArray<T>& a)
 {
-  if (this != &a) { 
-    CloneOrShare(a);
-    if (mInfo) {delete mInfo; mInfo = NULL;}
-    if (a.mInfo) mInfo = new DVList(*(a.mInfo));
-  }
+  if (this == &a) return(*this);
+  if (a.NbDimensions() < 1) 
+    throw RangeCheckError("TArray<T>::Set(a ) - Array a not allocated ! ");
+  if (NbDimensions() < 1) CloneOrShare(a);
+  else CopyElt(a);
   return(*this);
 }
@@ -158,4 +158,23 @@
   if (a.mInfo) mInfo = new DVList(*(a.mInfo));
 }
+
+//! Clone if \b a is not temporary, share if temporary
+template <class T>
+void TArray<T>::CloneOrShare(const TArray<T>& a)
+{
+  string exmsg = "TArray<T>::CloneOrShare()";
+  if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
+  mNDBlock.CloneOrShare(a.mNDBlock);
+}
+
+//! Share data with a
+template <class T>
+void TArray<T>::Share(const TArray<T>& a)
+{
+  string exmsg = "TArray<T>::Share()";
+  if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
+  mNDBlock.Share(a.mNDBlock);
+}
+
 
 //! Resize array
@@ -261,5 +280,6 @@
     throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! ");
   if ( !force && (AvgStep() == 1) ) {
-    TArray<T> ra(*this, true);
+    TArray<T> ra;
+    ra.Share(*this);
     ra.SetTemp(true);
     return(ra);
@@ -408,6 +428,10 @@
 
 //! Substract a constant value \b x to an array
-template <class T>
-TArray<T>& TArray<T>::Sub(T x)
+/*! 
+Substract a constant from the *this = *this-x
+\param fginv == true : Perfoms the inverse subtraction (*this = x-(*this)) 
+*/
+template <class T>
+TArray<T>& TArray<T>::Sub(T x, bool fginv)
 {
   if (NbDimensions() < 1) 
@@ -419,5 +443,8 @@
     uint_8 maxx = totsize_*step;
     pe = Data();
-    for(k=0; k<maxx; k+=step )  pe[k] -= x;
+    if (fginv)
+      for(k=0; k<maxx; k+=step )  pe[k] = x-pe[k];
+    else
+      for(k=0; k<maxx; k+=step )  pe[k] -= x;
   }
   else {    // Non regular data spacing ...
@@ -428,5 +455,8 @@
     for(j=0; j<naxa; j++)  {
       pe = mNDBlock.Begin()+Offset(ka,j);
-      for(k=0; k<gpas; k+=step)  pe[k] -= x;
+      if (fginv)
+	for(k=0; k<gpas; k+=step)  pe[k] = x-pe[k];
+      else
+	for(k=0; k<gpas; k+=step)  pe[k] -= x;
     }
   }
@@ -462,10 +492,14 @@
 
 //! Divide an array by a constant value \b x
-template <class T>
-TArray<T>& TArray<T>::Div(T x)
+/*! 
+Divide the array by a constant *this = *this/x
+\param fginv == true : Perfoms the inverse division (*this = x/(*this)) 
+*/
+template <class T>
+TArray<T>& TArray<T>::Div(T x, bool fginv)
 {
   if (NbDimensions() < 1) 
     throw RangeCheckError("TArray<T>::Div(T )  - Not Allocated Array ! ");
-  if (x == (T) 0 ) 
+  if (!fginv && (x == (T) 0) ) 
     throw MathExc("TArray<T>::Div(T )  - Divide by zero ! ");
   T * pe;
@@ -475,5 +509,8 @@
     uint_8 maxx = totsize_*step;
     pe = Data();
-    for(k=0; k<maxx; k+=step )  pe[k] /= x;
+    if (fginv)
+      for(k=0; k<maxx; k+=step )  pe[k] = x/pe[k];
+    else 
+      for(k=0; k<maxx; k+=step )  pe[k] /= x;
   }
   else {    // Non regular data spacing ...
@@ -484,64 +521,14 @@
     for(j=0; j<naxa; j++)  {
       pe = mNDBlock.Begin()+Offset(ka,j);
-      for(k=0; k<gpas; k+=step)  pe[k] /= x;
-    }
-  }
-  return(*this);
-}
-
-
-//! Inverse substract : A = \b x - A
-template <class T>
-TArray<T>& TArray<T>::SubInv(T x)
-{
-  if (NbDimensions() < 1) 
-    throw RangeCheckError("TArray<T>::SubInv(T )  - Not Allocated Array ! ");
-  T * pe;
-  uint_8 j,k;
-  if (AvgStep() > 0)   {  // regularly spaced elements
-    uint_8 step = AvgStep();
-    uint_8 maxx = totsize_*step;
-    pe = Data();
-    for(k=0; k<maxx; k+=step )  pe[k] = x-pe[k];
-  }
-  else {    // Non regular data spacing ...
-    uint_4 ka = MaxSizeKA();
-    uint_8 step = Step(ka);
-    uint_8 gpas = Size(ka)*step;
-    uint_8 naxa = Size()/Size(ka);
-    for(j=0; j<naxa; j++)  {
-      pe = mNDBlock.Begin()+Offset(ka,j);
-      for(k=0; k<gpas; k+=step)  pe[k] = x-pe[k];
-    }
-  }
-  return(*this);
-}
-
-//! Inverse Divide : A(i,j,...) = x / A(i,j,...)
-template <class T>
-TArray<T>& TArray<T>::DivInv(T x)
-{
-  if (NbDimensions() < 1) 
-    throw RangeCheckError("TArray<T>::DivInv(T )  - Not Allocated Array ! ");
-  T * pe;
-  uint_8 j,k;
-  if (AvgStep() > 0)   {  // regularly spaced elements
-    uint_8 step = AvgStep();
-    uint_8 maxx = totsize_*step;
-    pe = Data();
-    for(k=0; k<maxx; k+=step )  pe[k] = x/pe[k];
-  }
-  else {    // Non regular data spacing ...
-    uint_4 ka = MaxSizeKA();
-    uint_8 step = Step(ka);
-    uint_8 gpas = Size(ka)*step;
-    uint_8 naxa = Size()/Size(ka);
-    for(j=0; j<naxa; j++)  {
-      pe = mNDBlock.Begin()+Offset(ka,j);
-      for(k=0; k<gpas; k+=step)  pe[k] = x/pe[k];
-    }
-  }
-  return(*this);
-}
+      if (fginv)
+	for(k=0; k<gpas; k+=step)  pe[k] = x/pe[k];
+      else 
+	for(k=0; k<gpas; k+=step)  pe[k] /= x;
+    }
+  }
+  return(*this);
+}
+
+
 
 
@@ -583,6 +570,10 @@
 
 //! Substract two TArrays
-template <class T>
-TArray<T>& TArray<T>::SubElt(const TArray<T>& a)
+/*! 
+Substract two TArrays *this = *this-a
+\param fginv == true : Perfoms the inverse subtraction (*this = a-(*this)) 
+*/
+template <class T>
+TArray<T>& TArray<T>::SubElt(const TArray<T>& a, bool fginv)
 {
   if (NbDimensions() < 1) 
@@ -600,5 +591,8 @@
     pe = Data();
     pea = a.Data();
-    for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] -= pea[ka] ;
+    if (fginv) 
+      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] = pea[ka]-pe[k] ;
+    else 
+      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] -= pea[ka] ;
   }
   else {    // Non regular data spacing ...
@@ -611,9 +605,13 @@
       pe = mNDBlock.Begin()+Offset(ax,j);
       pea = a.DataBlock().Begin()+a.Offset(ax,j);
-      for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] -= pea[ka]; 
-    }
-  }
-  return(*this);
-}
+      if (fginv) 
+	for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] = pea[ka]-pe[k] ;
+      else
+	for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] -= pea[ka]; 
+    }
+  }
+  return(*this);
+}
+
 
 //! Multiply two TArrays (elements by elements)
@@ -654,6 +652,10 @@
 
 //! Divide two TArrays (elements by elements)
-template <class T>
-TArray<T>& TArray<T>::DivElt(const TArray<T>& a)
+/*! 
+Divide two TArrays *this = (*this)/a
+\param fginv == true : Perfoms the inverse division (*this = a/(*this)) 
+*/
+template <class T>
+TArray<T>& TArray<T>::DivElt(const TArray<T>& a, bool fginv)
 {
   if (NbDimensions() < 1) 
@@ -671,5 +673,8 @@
     pe = Data();
     pea = a.Data();
-    for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] /= pea[ka] ;
+    if (fginv) 
+      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] = pea[ka]/pe[k];
+    else 
+      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] /= pea[ka] ;
   }
   else {    // Non regular data spacing ...
@@ -682,5 +687,8 @@
       pe = mNDBlock.Begin()+Offset(ax,j);
       pea = a.DataBlock().Begin()+a.Offset(ax,j);
-      for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] /= pea[ka]; 
+      if (fginv) 
+	for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] = pea[ka]/pe[k]; 
+      else
+	for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] /= pea[ka]; 
     }
   }
@@ -835,22 +843,4 @@
 }
 
-//! Clone if \b a is not temporary, share if temporary
-template <class T>
-void TArray<T>::CloneOrShare(const TArray<T>& a)
-{
-  string exmsg = "TArray<T>::CloneOrShare()";
-  if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
-  mNDBlock.CloneOrShare(a.mNDBlock);
-}
-
-//! Share data with a
-template <class T>
-void TArray<T>::Share(const TArray<T>& a)
-{
-  string exmsg = "TArray<T>::Share()";
-  if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
-  mNDBlock.Share(a.mNDBlock);
-}
-
 
 
Index: /trunk/SophyaLib/TArray/tarray.h
===================================================================
--- /trunk/SophyaLib/TArray/tarray.h	(revision 969)
+++ /trunk/SophyaLib/TArray/tarray.h	(revision 970)
@@ -46,4 +46,9 @@
   // Gestion taille/Remplissage
   virtual void Clone(const TArray<T>& a); 
+  // partage les donnees si "a" temporaire, clone sinon.
+  void CloneOrShare(const TArray<T>& a);
+  // Share: partage les donnees de "a"
+
+  void Share(const TArray<T>& a);
   void ReSize(uint_4 ndim, uint_4 * siz, uint_4 step=1);
   void Realloc(uint_4 ndim, uint_4 * siz, uint_4 step=1, bool force=false);
@@ -122,5 +127,5 @@
   //! Add \b x to all elements
   inline  TArray<T>&  operator += (T x)            { return Add(x); }
-  virtual TArray<T>&  Sub(T x);
+  virtual TArray<T>&  Sub(T x, bool fginv=false);
   //! Substract \b x to all elements
   inline  TArray<T>&  operator -= (T x)            { return Sub(x); }
@@ -128,9 +133,7 @@
   //! Multiply all elements by \b x
   inline  TArray<T>&  operator *= (T x)            { return Mul(x); }
-  virtual TArray<T>&  Div(T x);
+  virtual TArray<T>&  Div(T x, bool fginv=false);
   //! Divide all elements by \b x
   inline  TArray<T>&  operator /= (T x)            { return Div(x); }
-  virtual TArray<T>&  SubInv(T x);  //   A ---> x-A
-  virtual TArray<T>&  DivInv(T x);  //   A ---> x/A
 
 // A += -=  (ajoute, soustrait element par element les deux tableaux )
@@ -138,10 +141,10 @@
   //! Operator TArray += TArray
   inline  TArray<T>&  operator += (const TArray<T>& a)  { return AddElt(a); }
-  virtual TArray<T>&  SubElt(const TArray<T>& a);
+  virtual TArray<T>&  SubElt(const TArray<T>& a, bool fginv=false);
   //! Operator TArray -= TArray
   inline  TArray<T>&  operator -= (const TArray<T>& a)  { return SubElt(a); }
 // Multiplication, division element par element les deux tableaux 
   virtual TArray<T>&  MulElt(const TArray<T>& a);
-  virtual TArray<T>&  DivElt(const TArray<T>& a);
+  virtual TArray<T>&  DivElt(const TArray<T>& a, bool fginv=false);
 // Recopie des valeurs, element par element
   virtual TArray<T>&  CopyElt(const TArray<T>& a);
@@ -159,8 +162,4 @@
 
 protected:
-  // partage les donnees si "a" temporaire, clone sinon.
-  void CloneOrShare(const TArray<T>& a);
-  // Share: partage les donnees de "a"
-  void Share(const TArray<T>& a);
 
   NDataBlock<T> mNDBlock; //!< Block for datas
@@ -180,35 +179,48 @@
   \brief Operator TArray = TArray + constant */
 template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
-    {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Add(b); return result;}
 
 /*! \ingroup TArray \fn operator+(T,const TArray<T>&)
   \brief Operator TArray = constant + TArray */
 template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
-    {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Add(b); return result;}
 
 /*! \ingroup TArray \fn operator-(const TArray<T>&,T)
   \brief Operator TArray = TArray - constant */
 template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
-    {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Sub(b); return result;}
 
 /*! \ingroup TArray \fn operator-(T,const TArray<T>&)
   \brief Operator TArray = constant - TArray */
 template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
-    {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Sub(b,true); return result;}
 
 /*! \ingroup TArray \fn operator*(const TArray<T>&,T)
   \brief Operator TArray = TArray * constant */
 template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
-    {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Mul(b); return result;}
 
 /*! \ingroup TArray \fn operator*(T,const TArray<T>&)
   \brief Operator TArray = constant * TArray */
 template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
-    {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Mul(b); return result;}
 
 /*! \ingroup TArray \fn operator/(const TArray<T>&,T)
   \brief Operator TArray = TArray / constant */
 template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
-    {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Div(b); return result;}
+
+/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
+  \brief Operator TArray = constant / TArray  */
+template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
+    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 
+    result.Div(b, true); return result;}
 
 ////////////////////////////////////////////////////////////////
@@ -219,5 +231,8 @@
 template <class T>
 inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
-    {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
+    { TArray<T> result; result.SetTemp(true); 
+    if (b.IsTemp())  { result.Share(b); result.AddElt(a); }
+    else { result.CloneOrShare(a); result.AddElt(b); }
+    return result; }
 
 /*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
@@ -225,5 +240,8 @@
 template <class T>
 inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
-    {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
+    { TArray<T> result; result.SetTemp(true); 
+    if (b.IsTemp())  { result.Share(b); result.SubElt(a, true); }
+    else { result.CloneOrShare(a); result.SubElt(b); }
+    return result; }
 
 
Index: /trunk/SophyaLib/TArray/tmatrix.cc
===================================================================
--- /trunk/SophyaLib/TArray/tmatrix.cc	(revision 969)
+++ /trunk/SophyaLib/TArray/tmatrix.cc	(revision 970)
@@ -1,3 +1,3 @@
-// $Id: tmatrix.cc,v 1.9 2000-04-21 16:31:25 ansari Exp $
+// $Id: tmatrix.cc,v 1.10 2000-04-26 17:55:10 ansari Exp $
 //                         C.Magneville          04/99
 #include "machdefs.h"
@@ -110,10 +110,10 @@
     throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
   TArray<T>::Set(a);
-  if (a.NbDimensions() == 1) {
+  if (NbDimensions() == 1) {
     size_[1] = 1;
     step_[1] = size_[0]*step_[0];
     ndim_ = 2;
   }
-  UpdateMemoryMapping(a, SameMemoryMapping);
+  UpdateMemoryMapping(*this, SameMemoryMapping);
   return(*this);
 }
Index: /trunk/SophyaLib/TArray/tmatrix.h
===================================================================
--- /trunk/SophyaLib/TArray/tmatrix.h	(revision 969)
+++ /trunk/SophyaLib/TArray/tmatrix.h	(revision 970)
@@ -136,5 +136,9 @@
 template <class T>
 inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
-    {TMatrix<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
+    {TMatrix<T> result; result.SetTemp(true); 
+    if (b.IsTemp())  { result.Share(b); result.AddElt(a); }
+    else { result.CloneOrShare(a); result.AddElt(b); }
+    return result; }
+
 
 /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
@@ -142,5 +146,8 @@
 template <class T>
 inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
-    {TMatrix<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
+    {TMatrix<T> result; result.SetTemp(true); 
+    if (b.IsTemp())  { result.Share(b); result.SubElt(a, true); }
+    else { result.CloneOrShare(a); result.SubElt(b); }
+    return result; }
 
 // Surcharge d'operateurs C = A * B
@@ -148,5 +155,5 @@
   \brief * : multiply matrixes \b a and \b b */
 template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
-{ TMatrix<T> result(a); result.SetTemp(true); return(result.Multiply(b)); }
+   { return(a.Multiply(b)); }
 
 // Typedef pour simplifier et compatibilite Peida
