Re: [eigen] cast ArrayXd* to ArrayXXd*

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Do not know exactly how to provide more context.

The pointers deal with the problem to return an array from a function at
the context of my application.

This is such a function:


--8<---8<----

union TIntensity { double           number;
                   Eigen::ArrayXd*  arrayXd;
                   Eigen::ArrayXXd* arrayXXd;
                   };



Eigen::ArrayXXd*
CRecord::calc_reflectivity( OUTCOME outcome,
                            bool    respect_tofRange,
                            bool    respect_qzRange
                                             ) {
   assert(outcome==TOF_SCALE || outcome==MATRIX);
   TRANGE tRange;
   XRANGE smXRange;
   if(respect_qzRange==true)
      tRange = TRANGE_QZ;
      else
      if(respect_tofRange==true)
         tRange = TRANGE_TOF;
         else
         tRange = TRANGE_NONE;
   if(outcome==MATRIX)
      smXRange = XRANGE_NONE;
      else
      smXRange = XRANGE_SPX;

   Eigen::ArrayXXd* smInt;
   TIntensity tmp = this->calc_intensity( ITEM_SM,
                                          outcome,
                                          smXRange,
                                          tRange );
   if( outcome==TOF_SCALE ) {
      smInt = new Eigen::ArrayXXd( *(tmp.arrayXd) );
      delete(tmp.arrayXd);
      }
      else {
      smInt = tmp.arrayXXd;
      }
   Eigen::ArrayXd*  mtInt   = (this->calc_intensity( ITEM_MT,
                                                     TOF_SCALE,
                                                     XRANGE_MPX,
                                                     tRange )).arrayXd;
   double smNoise = (this->calc_intensity( ITEM_SM,
                                           NUMBER,
                                           XRANGE_NX,
                                           tRange )).number;
   double mtNoise = (this->calc_intensity( ITEM_MT,
                                           NUMBER,
                                           XRANGE_NX,
                                           tRange )).number;
   // intensity = intensity - noise  (remove noise)
   *smInt = (*smInt)-smNoise;
   *mtInt = (*mtInt)-mtNoise;
   // removing of noise might create bad values so:
   Eigen::ArrayXXd* mtInt2D = new Eigen::ArrayXXd( smInt->rows(),
                                                   smInt->cols() );
   mtInt2D->colwise()       = *mtInt;
   *smInt   = smInt->binaryExpr(
      *mtInt2D, ptr_fun<double,double,double>(auxFn_setSmToZero));
   *mtInt2D = smInt->binaryExpr(
      *mtInt2D, ptr_fun<double,double,double>(auxFn_setMtToNearZero ));
   // refl = sm / mt * fact
   *smInt = (*smInt)/(*mtInt2D)*this->getReflFactor();
   delete(mtInt   );
   delete(mtInt2D );
   return smInt;
   }

--8<---8<----

Lg Holger


On 09/26/2012 04:22 PM, Christoph Hertzberg wrote:
> On 26.09.2012 14:50, Holger Herrlich wrote:
>>
>> Hi, did I really have to:
>>
>> Eigen::ArrayXXd* smInt;
>> Eigen::ArrayXd*  tmp = fn_create();
>> smInt = new Eigen::ArrayXXd( *(tmp) );
>> delete(tmp.arrayXd);
>>
>> to cast from Array<double,len> to Array<double,len,1> when using
>> pointers?
> 
> Well, there are many ways, but it would require a bit more context to
> find the best way in your case.
> Quite likely you don't need to use pointers at all (using plain pointers
> always bears the risk of memory leaks) and I don't know (m)any cases
> where ArrayXXd is accepted but ArrayXd is not (meaning I don't see why
> you need this cast).
> 
> Christoph
> 
> 
> 



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/