Changeset 2564 in Sophya for trunk/SophyaLib/TArray/tarray.cc


Ignore:
Timestamp:
Jul 26, 2004, 7:30:40 PM (21 years ago)
Author:
ansari
Message:

Remplacement methodes Add/Mul/Sub/Div(T x) par AddCst/MulCst/SubCst/DivCst(T x, TArray<T> res) ds TArray - Reza 26 Juillet 2004

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/TArray/tarray.cc

    r2338 r2564  
    7070  \param siz[ndim] : size along each dimension
    7171  \param step : step (same for all dimensions)
    72  */
    73 template <class T>
    74 TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step)
    75   : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1))
     72  \param fzero : if \b true , set array elements to zero
     73 */
     74template <class T>
     75TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step, bool fzero)
     76  : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1), fzero)
    7677{
    7778  string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, sa_size_t)";
     
    8283/*!
    8384  \param nx,ny,nz,nt,nu : sizes along first, second, third, fourth and fifth dimension
    84  */
    85 template <class T>
    86 TArray<T>::TArray(sa_size_t nx, sa_size_t ny, sa_size_t nz, sa_size_t nt, sa_size_t nu)
     85  \param fzero : if \b true , set array elements to zero
     86 */
     87template <class T>
     88TArray<T>::TArray(sa_size_t nx, sa_size_t ny, sa_size_t nz, sa_size_t nt, sa_size_t nu, bool fzero)
    8789  : BaseArray() , mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)*((nt>0)?nt:1)*((nu>0)?nu:1))
    8890{
     
    268270  \param siz[ndim] : size along each dimension
    269271  \param step : step (same for all dimensions)
    270  */
    271 template <class T>
    272 void TArray<T>::ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step)
     272  \param fzero : if \b true , set array elements to zero
     273 */
     274template <class T>
     275void TArray<T>::ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step, bool fzero)
    273276{
    274277  if (arrtype_ != 0) {
     
    280283  string exmsg = "TArray<T>::ReSize(int_4 ...)";
    281284  if (!UpdateSizes(ndim, siz, step, 0, exmsg))  throw( ParmError(exmsg) );
    282   mNDBlock.ReSize(totsize_);   
     285  mNDBlock.ReSize(totsize_, fzero);   
    283286}
    284287
     
    287290  The array size and memory layout are copied from the array \b a.
    288291  \param a : Array used as template for setting the size and memory layout.
    289  */
    290 template <class T>
    291 void TArray<T>::ReSize(const BaseArray& a)
     292  \param pack : if \b true , create a packed array, else same memory layout as \b a.
     293  \param fzero : if \b true , set array elements to zero
     294 */
     295template <class T>
     296void TArray<T>::ReSize(const BaseArray& a, bool pack, bool fzero)
    292297{
    293298  if (arrtype_ != 0) {
     
    298303  }
    299304  string exmsg = "TArray<T>::ReSize(const TArray<T>&)";
    300   if (!UpdateSizes(a, exmsg))  throw( ParmError(exmsg) );
    301   mNDBlock.ReSize(totsize_);     
     305  if (pack) {
     306    sa_size_t siz[BASEARRAY_MAXNDIMS];
     307    for(int ksz=0; ksz<BASEARRAY_MAXNDIMS; ksz++) siz[ksz] = a.Size(ksz);
     308    if (!UpdateSizes(a.NbDimensions(), siz, 1, 0, exmsg))  throw( ParmError(exmsg) );
     309    mNDBlock.ReSize(totsize_, fzero);       
     310  }
     311  else {
     312    if (!UpdateSizes(a, exmsg))  throw( ParmError(exmsg) );
     313    mNDBlock.ReSize(totsize_);     
     314  }
    302315}
    303316
     
    491504}
    492505
    493 //! Add a constant value \b x to an array
    494 template <class T>
    495 TArray<T>& TArray<T>::Add(T x)
    496 {
    497   if (NbDimensions() < 1)
    498     throw RangeCheckError("TArray<T>::Add(T )  - Not Allocated Array ! ");
    499   T * pe;
    500   sa_size_t j,k;
    501   if (AvgStep() > 0)   {  // regularly spaced elements
    502     sa_size_t step = AvgStep();
    503     sa_size_t maxx = totsize_*step;
    504     pe = Data();
    505     for(k=0; k<maxx; k+=step )  pe[k] += x;
    506   }
    507   else {    // Non regular data spacing ...
    508     int_4 ka = MaxSizeKA();
    509     sa_size_t step = Step(ka);
    510     sa_size_t gpas = Size(ka)*step;
    511     sa_size_t naxa = Size()/Size(ka);
    512     for(j=0; j<naxa; j++)  {
    513       pe = mNDBlock.Begin()+Offset(ka,j);
    514       for(k=0; k<gpas; k+=step)  pe[k] += x;
    515     }
    516   }
    517   return(*this);
    518 }
    519 
    520 //! Substract a constant value \b x to an array
     506//! Add a constant value \b x to the source array and store the result in \b res.
    521507/*!
    522 Substract a constant from the *this = *this-x
    523 \param fginv == true : Perfoms the inverse subtraction (*this = x-(*this))
    524 */
    525 template <class T>
    526 TArray<T>& TArray<T>::Sub(T x, bool fginv)
    527 {
    528   if (NbDimensions() < 1)
    529     throw RangeCheckError("TArray<T>::Sub(T )  - Not Allocated Array ! ");
    530   T * pe;
    531   sa_size_t j,k;
    532   if (AvgStep() > 0)   {  // regularly spaced elements
    533     sa_size_t step = AvgStep();
    534     sa_size_t maxx = totsize_*step;
    535     pe = Data();
    536     if (fginv)
    537       for(k=0; k<maxx; k+=step )  pe[k] = x-pe[k];
    538     else
    539       for(k=0; k<maxx; k+=step )  pe[k] -= x;
    540   }
    541   else {    // Non regular data spacing ...
    542     int_4 ka = MaxSizeKA();
    543     sa_size_t step = Step(ka);
    544     sa_size_t gpas = Size(ka)*step;
    545     sa_size_t naxa = Size()/Size(ka);
    546     for(j=0; j<naxa; j++)  {
    547       pe = mNDBlock.Begin()+Offset(ka,j);
    548       if (fginv)
    549         for(k=0; k<gpas; k+=step)  pe[k] = x-pe[k];
     508Add a constant to the source array \b this and store the result in \b res (res = *this+x). 
     509If not initially allocated, the output array \b res is automatically
     510resized as a packed array with the same sizes as the source (this) array.
     511Returns a reference to the output array \b res.
     512\param x : constant to add to the array elements
     513\param res : Output array containing the result (res=this+x).
     514*/
     515template <class T>
     516TArray<T>& TArray<T>::AddCst(T x, TArray<T>& res) const
     517{
     518  if (NbDimensions() < 1)
     519    throw RangeCheckError("TArray<T>::AddCst(T,res)  - Not allocated source array ");
     520  if (res.NbDimensions() < 1)  res.SetSize(*this, true, false);
     521  bool smo;
     522  if (!CompareSizes(res, smo))
     523    throw(SzMismatchError("TArray<T>::AddCst(T, res) SizeMismatch(this,res) ")) ;
     524
     525  const T * pe;
     526  T * per;
     527  sa_size_t j,k,kr;
     528  if (smo && (IsPacked() > 0) && (res.IsPacked() > 0))   {  // regularly spaced elements
     529    sa_size_t maxx = totsize_;
     530    pe = Data();
     531    per = res.Data();
     532    for(k=0; k<maxx; k++) { *per = (*pe)+x;  per++;  pe++; }
     533  }
     534  else {    // Non regular data spacing ...
     535    int_4 ax,axr;
     536    sa_size_t step, stepr;
     537    sa_size_t gpas, naxr;
     538    GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxr);
     539    for(j=0; j<naxr; j++)  {
     540      pe = mNDBlock.Begin()+Offset(ax,j);
     541      per = res.DataBlock().Begin()+res.Offset(axr,j);
     542      for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = pe[k]+x;
     543    }
     544  }
     545  return(res);
     546}
     547
     548//! Subtract a constant value \b x from the source array and store the result in \b res.
     549/*!
     550Subtract a constant from the source array \b this and store the result in \b res (res = *this-x). 
     551If not initially allocated, the output array \b res is automatically
     552resized as a packed array with the same sizes as the source (this) array.
     553Returns a reference to the output array \b res.
     554\param x : constant to subtract from the array elements
     555\param res : Output array containing the result (res=this+x or res=x-this).
     556\param fginv == true : Invert subtraction argument order (*this = x-(*this))
     557*/
     558template <class T>
     559TArray<T>& TArray<T>::SubCst(T x, TArray<T>& res, bool fginv) const
     560{
     561  if (NbDimensions() < 1)
     562    throw RangeCheckError("TArray<T>::SubCst(T,res)  - Not allocated source array ");
     563  if (res.NbDimensions() < 1)  res.SetSize(*this, true, false);
     564  bool smo;
     565  if (!CompareSizes(res, smo))
     566    throw(SzMismatchError("TArray<T>::SubCst(T, res) SizeMismatch(this,res) ")) ;
     567
     568  const T * pe;
     569  T * per;
     570  sa_size_t j,k,kr;
     571  if (smo && (IsPacked() > 0) && (res.IsPacked() > 0))   {  // regularly spaced elements
     572    sa_size_t maxx = totsize_;
     573    pe = Data();
     574    per = res.Data();
     575    if (!fginv)
     576      for(k=0; k<maxx; k++) { *per = (*pe)-x;  per++;  pe++; }
     577    else
     578      for(k=0; k<maxx; k++) { *per = x-(*pe);  per++;  pe++; }
     579  }
     580  else {    // Non regular data spacing ...
     581    int_4 ax,axr;
     582    sa_size_t step, stepr;
     583    sa_size_t gpas, naxr;
     584    GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxr);
     585    for(j=0; j<naxr; j++)  {
     586      pe = mNDBlock.Begin()+Offset(ax,j);
     587      per = res.DataBlock().Begin()+res.Offset(axr,j);
     588      if (!fginv)
     589        for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = pe[k]-x;
    550590      else
    551         for(k=0; k<gpas; k+=step)  pe[k] -= x;
    552     }
    553   }
    554   return(*this);
    555 }
    556 
    557 //! Multiply an array by a constant value \b x
    558 template <class T>
    559 TArray<T>& TArray<T>::Mul(T x)
    560 {
    561   if (NbDimensions() < 1)
    562     throw RangeCheckError("TArray<T>::Mul(T )  - Not Allocated Array ! ");
    563   T * pe;
    564   sa_size_t j,k;
    565   if (AvgStep() > 0)   {  // regularly spaced elements
    566     sa_size_t step = AvgStep();
    567     sa_size_t maxx = totsize_*step;
    568     pe = Data();
    569     for(k=0; k<maxx; k+=step )  pe[k] *= x;
    570   }
    571   else {    // Non regular data spacing ...
    572     int_4 ka = MaxSizeKA();
    573     sa_size_t step = Step(ka);
    574     sa_size_t gpas = Size(ka)*step;
    575     sa_size_t naxa = Size()/Size(ka);
    576     for(j=0; j<naxa; j++)  {
    577       pe = mNDBlock.Begin()+Offset(ka,j);
    578       for(k=0; k<gpas; k+=step)  pe[k] *= x;
    579     }
    580   }
    581   return(*this);
    582 }
    583 
    584 //! Divide an array by a constant value \b x
     591        for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = x-pe[k];
     592    }
     593  }
     594  return(res);
     595}
     596
     597//! Multiply the source array by a constant value \b x and store the result in \b res.
    585598/*!
    586 Divide the array by a constant *this = *this/x
    587 \param fginv == true : Perfoms the inverse division (*this = x/(*this))
    588 */
    589 template <class T>
    590 TArray<T>& TArray<T>::Div(T x, bool fginv)
    591 {
    592   if (NbDimensions() < 1)
    593     throw RangeCheckError("TArray<T>::Div(T )  - Not Allocated Array ! ");
     599Multiply the source array \b this by a constant \b x and store the result in \b res (res = *this*x). 
     600If not initially allocated, the output array \b res is automatically
     601resized as a packed array with the same sizes as the source (this) array.
     602Returns a reference to the output array \b res.
     603\param x : Array elements are multiplied by x
     604\param res : Output array containing the result (res=this*x).
     605*/
     606template <class T>
     607TArray<T>& TArray<T>::MulCst(T x, TArray<T>& res) const
     608{
     609  if (NbDimensions() < 1)
     610    throw RangeCheckError("TArray<T>::MulCst(T,res)  - Not allocated source array ");
     611  if (res.NbDimensions() < 1)  res.SetSize(*this, true, false);
     612  bool smo;
     613  if (!CompareSizes(res, smo))
     614    throw(SzMismatchError("TArray<T>::MulCst(T, res) SizeMismatch(this,res) ")) ;
     615
     616  const T * pe;
     617  T * per;
     618  sa_size_t j,k,kr;
     619  if (smo && (IsPacked() > 0) && (res.IsPacked() > 0))   {  // regularly spaced elements
     620    sa_size_t maxx = totsize_;
     621    pe = Data();
     622    per = res.Data();
     623    for(k=0; k<maxx; k++) { *per = (*pe)*x;  per++;  pe++; }
     624  }
     625  else {    // Non regular data spacing ...
     626    int_4 ax,axr;
     627    sa_size_t step, stepr;
     628    sa_size_t gpas, naxr;
     629    GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxr);
     630    for(j=0; j<naxr; j++)  {
     631      pe = mNDBlock.Begin()+Offset(ax,j);
     632      per = res.DataBlock().Begin()+res.Offset(axr,j);
     633      for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = pe[k]*x;
     634    }
     635  }
     636  return(res);
     637}
     638
     639//! Divide the source array by a constant value \b x and store the result in \b res.
     640/*!
     641Divide the source array \b this by a constant \b x and store the result in \b res (res = *this/x). 
     642If not initially allocated, the output array \b res is automatically
     643resized as a packed array with the same sizes as the source (this) array.
     644Returns a reference to the output array \b res.
     645\param x : Array elements are divied by x
     646\param res : Output array containing the result (res=(*this)/x or res=x/(*this)).
     647\param fginv == true : Invert the operation order (res = x/(*this))
     648*/
     649template <class T>
     650TArray<T>& TArray<T>::DivCst(T x, TArray<T>& res, bool fginv) const
     651{
     652  if (NbDimensions() < 1)
     653    throw RangeCheckError("TArray<T>::DivCst(T,res)  - Not allocated source array ! ");
    594654  if (!fginv && (x == (T) 0) )
    595     throw MathExc("TArray<T>::Div(T )  - Divide by zero ! ");
    596   T * pe;
    597   sa_size_t j,k;
    598   if (AvgStep() > 0)   {  // regularly spaced elements
    599     sa_size_t step = AvgStep();
    600     sa_size_t maxx = totsize_*step;
    601     pe = Data();
    602     if (fginv)
    603       for(k=0; k<maxx; k+=step )  pe[k] = x/pe[k];
     655    throw MathExc("TArray<T>::DivCst(T,res)  - Divide by zero ! ");
     656  if (res.NbDimensions() < 1)  res.SetSize(*this, true, false);
     657  bool smo;
     658  if (!CompareSizes(res, smo))
     659    throw(SzMismatchError("TArray<T>::DivCst(T, res) SizeMismatch(this,res) ")) ;
     660
     661  const T * pe;
     662  T * per;
     663  sa_size_t j,k,kr;
     664  if (smo && (IsPacked() > 0) && (res.IsPacked() > 0))   {  // regularly spaced elements
     665    sa_size_t maxx = totsize_;
     666    pe = Data();
     667    per = res.Data();
     668    if (!fginv)
     669      for(k=0; k<maxx; k++) { *per = (*pe)/x;  per++;  pe++; }
    604670    else
    605       for(k=0; k<maxx; k+=step )  pe[k] /= x;
    606   }
    607   else {    // Non regular data spacing ...
    608     int_4 ka = MaxSizeKA();
    609     sa_size_t step = Step(ka);
    610     sa_size_t gpas = Size(ka)*step;
    611     sa_size_t naxa = Size()/Size(ka);
    612     for(j=0; j<naxa; j++)  {
    613       pe = mNDBlock.Begin()+Offset(ka,j);
    614       if (fginv)
    615         for(k=0; k<gpas; k+=step)  pe[k] = x/pe[k];
    616       else
    617         for(k=0; k<gpas; k+=step)  pe[k] /= x;
    618     }
    619   }
    620   return(*this);
    621 }
    622 
    623 
    624 //! Replace array elements values by their opposite ( a(i) -> -a(i) )
    625 template <class T>
    626 TArray<T>& TArray<T>::NegateElt()
    627 {
    628   if (NbDimensions() < 1)
    629     throw RangeCheckError("TArray<T>::NegateElt()  - Not Allocated Array ! ");
    630   T * pe;
    631   sa_size_t j,k;
    632   if (AvgStep() > 0)   {  // regularly spaced elements
    633     sa_size_t step = AvgStep();
    634     sa_size_t maxx = totsize_*step;
    635     pe = Data();
    636     for(k=0; k<maxx; k+=step )  pe[k] = -pe[k];
    637   }
    638   else {    // Non regular data spacing ...
    639     int_4 ka = MaxSizeKA();
    640     sa_size_t step = Step(ka);
    641     sa_size_t gpas = Size(ka)*step;
    642     sa_size_t naxa = Size()/Size(ka);
    643     for(j=0; j<naxa; j++)  {
    644       pe = mNDBlock.Begin()+Offset(ka,j);
    645       for(k=0; k<gpas; k+=step)  pe[k] = -pe[k];
    646     }
    647   }
    648   return(*this);
     671      for(k=0; k<maxx; k++) { *per = x/(*pe);  per++;  pe++; }
     672  }
     673  else {    // Non regular data spacing ...
     674    int_4 ax,axr;
     675    sa_size_t step, stepr;
     676    sa_size_t gpas, naxr;
     677    GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxr);
     678    for(j=0; j<naxr; j++)  {
     679      pe = mNDBlock.Begin()+Offset(ax,j);
     680      per = res.DataBlock().Begin()+res.Offset(axr,j);
     681      if (!fginv)
     682        for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = pe[k]/x;
     683      else
     684        for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = x/pe[k];
     685    }
     686  }
     687  return(res);
     688}
     689
     690
     691//! Stores the opposite of the source array in \b res (res=-(*this)).
     692/*!
     693If not initially allocated, the output array \b res is automatically
     694resized as a packed array with the same sizes as the source (this) array.
     695Returns a reference to the output array \b res.
     696*/
     697template <class T>
     698TArray<T>& TArray<T>::NegateElt(TArray<T>& res) const
     699{
     700  if (NbDimensions() < 1)
     701    throw RangeCheckError("TArray<T>::NegateElt(res)  - Not allocated source array ");
     702  if (res.NbDimensions() < 1)  res.SetSize(*this, true, false);
     703  bool smo;
     704  if (!CompareSizes(res, smo))
     705    throw(SzMismatchError("TArray<T>::NegateElt(res) SizeMismatch(this,res) ")) ;
     706
     707  const T * pe;
     708  T * per;
     709  sa_size_t j,k,kr;
     710  if (smo && (IsPacked() > 0) && (res.IsPacked() > 0))   {  // regularly spaced elements
     711    sa_size_t maxx = totsize_;
     712    pe = Data();
     713    per = res.Data();
     714    for(k=0; k<maxx; k++) { *per = -(*pe);  per++;  pe++; }
     715  }
     716  else {    // Non regular data spacing ...
     717    int_4 ax,axr;
     718    sa_size_t step, stepr;
     719    sa_size_t gpas, naxr;
     720    GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxr);
     721    for(j=0; j<naxr; j++)  {
     722      pe = mNDBlock.Begin()+Offset(ax,j);
     723      per = res.DataBlock().Begin()+res.Offset(axr,j);
     724      for(k=0, kr=0;  k<gpas;  k+=step, kr+=stepr)  per[kr] = -pe[k];
     725    }
     726  }
     727  return(res);
    649728}
    650729
Note: See TracChangeset for help on using the changeset viewer.