functions.hh

Go to the documentation of this file.
00001 /*************************************************************************
00002 
00003 Copyright Rice University, 2004 - 2015.
00004 All rights reserved.
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a
00007 copy of this software and associated documentation files (the "Software"),
00008 to deal in the Software without restriction, including without limitation
00009 the rights to use, copy, modify, merge, publish, distribute, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, provided that the above copyright notice(s) and this
00012 permission notice appear in all copies of the Software and that both the
00013 above copyright notice(s) and this permission notice appear in supporting
00014 documentation.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
00019 RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
00020 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
00021 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00022 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00023 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
00024 THIS SOFTWARE.
00025 
00026 Except as contained in this notice, the name of a copyright holder shall
00027 not be used in advertising or otherwise to promote the sale, use or other
00028 dealings in this Software without prior written authorization of the
00029 copyright holder.
00030 
00031 **************************************************************************/
00032 
00033 #ifndef __RVL_FCNS
00034 #define __RVL_FCNS
00035 
00036 #include "utility.hh"
00037 #include "local.hh"
00038 
00039 namespace RVL {
00040 
00043   template<class Scalar>
00044   class RVLCopy: public BinaryLocalFunctionObject<Scalar> {
00045   private:
00046     RVLCopy(const RVLCopy<Scalar> &) {}
00047   public:
00048     RVLCopy() {}
00049     ~RVLCopy() {}
00050   
00054     using RVL::BinaryLocalEvaluation<Scalar>::operator();
00055     void operator()(LocalDataContainer<Scalar> & x,
00056             LocalDataContainer<Scalar> const & y) {
00057       if (x.getSize() > y.getSize()) {
00058     RVLException e; e<<"Error: RVLCopy\n";
00059     e<<"input shorter than output - copy does not cover\n";
00060     throw e;
00061       }
00062       else {
00063     size_t n = x.getSize();
00064     Scalar * px = x.getData();
00065     Scalar const * py = y.getData();
00066     for (size_t i=0;i<n;i++) {
00067       px[i]=py[i];
00068     }
00069       }
00070     }
00071     string getName() const  { return "RVLCopy"; }
00072   };
00073 
00074   template<>
00075   class RVLCopy<float>: public BinaryLocalFunctionObject<float> {
00076   private:
00077     RVLCopy(const RVLCopy<float> &) {}
00078   public:
00079     RVLCopy() {}
00080     ~RVLCopy() {}
00081   
00085     using RVL::BinaryLocalEvaluation<float>::operator();
00086     void operator()(LocalDataContainer<float> & x,
00087             LocalDataContainer<float> const & y) {
00088       if (x.getSize() > y.getSize()) {
00089     RVLException e; e<<"Error: RVLCopy\n";
00090     e<<"input shorter than output - copy does not cover\n";
00091     throw e;
00092       }
00093       else {
00094     float * px = x.getData();
00095     float const * py = y.getData();
00096     memcpy(px, py, x.getSize()*sizeof(float));
00097       }
00098     }
00099     string getName() const  { return "RVLCopy<float>"; }
00100   };
00101 
00102   template<>
00103   class RVLCopy<double>: public BinaryLocalFunctionObject<double> {
00104   private:
00105     RVLCopy(const RVLCopy<double> &) {}
00106   public:
00107     RVLCopy() {}
00108     ~RVLCopy() {}
00109   
00113     using RVL::BinaryLocalEvaluation<double>::operator();
00114     void operator()(LocalDataContainer<double> & x,
00115             LocalDataContainer<double> const & y) {
00116       if (x.getSize() > y.getSize()) {
00117     RVLException e; e<<"Error: RVLCopy\n";
00118     e<<"input shorter than output - copy does not cover\n";
00119     throw e;
00120       }
00121       else {
00122     double * px = x.getData();
00123     double const * py = y.getData();
00124     memcpy(px, py, x.getSize()*sizeof(double));
00125       }
00126     }
00127     string getName() const  { return "RVLCopy<double>"; }
00128   };
00129 
00132   template<class Scalar>
00133   class RVLScale: public UnaryLocalFunctionObject<Scalar> {
00134   private:
00135     Scalar c;
00136     RVLScale();
00137     RVLScale(const RVLScale<Scalar> &);
00138   public:
00139     RVLScale(Scalar _c): c(_c) {}
00140     ~RVLScale() {}
00141 
00145     using RVL::UnaryLocalEvaluation<Scalar>::operator();
00146     void operator()(LocalDataContainer<Scalar> & x) {
00147       size_t n = x.getSize();
00148       Scalar * px = x.getData();
00149       for (size_t i=0;i<n;i++) {
00150     px[i]=c*(px[i]);
00151       }
00152     }
00153     string getName() const  { return "RVLScale"; }
00154   };
00155 
00159   template<class Scalar>
00160   class RVLMax
00161     : public UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar> {
00162   public:
00163     RVLMax()
00164       : UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(-numeric_limits<Scalar>::max()) {}
00165     RVLMax(Scalar _res) 
00166       : UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(_res) {}
00167     RVLMax(const RVLMax<Scalar> & m)
00168       : UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(m) {}
00169     ~RVLMax() {}
00170 
00171     // override base class
00172     void setValue() { ScalarRedn<Scalar>::setValue(-numeric_limits<Scalar>::max()); }
00173     using RVL::UnaryLocalConstEval<Scalar>::operator();
00174     void operator() (LocalDataContainer<Scalar> const & x) {
00175       Scalar maxr = ScalarRedn<Scalar>::getValue();
00176       size_t n=x.getSize();
00177       Scalar const * px = x.getData();
00178       for (size_t i=0;i<n;i++) maxr = max<Scalar>(maxr,px[i]);
00179       ScalarRedn<Scalar>::setValue(maxr);
00180     }
00181 
00182     string getName() const  { return "RVLMax"; }
00183     
00184   };
00185   
00189   template<class Scalar>
00190   class RVLMin: public UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar> {
00191   public:
00192     RVLMin()
00193       : UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(numeric_limits<Scalar>::max()) {}
00194     RVLMin(Scalar _res)
00195       : UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(_res) { }
00196     RVLMin(const RVLMin<Scalar> & m)
00197       : UnaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(m) {}
00198     ~RVLMin() {}
00199   
00200     void setValue() { ScalarRedn<Scalar>::setValue(numeric_limits<Scalar>::max()); }
00201     using RVL::UnaryLocalConstEval<Scalar>::operator();
00202     void operator() (LocalDataContainer<Scalar> const & x) {
00203       Scalar minr = ScalarRedn<Scalar>::getValue();
00204       size_t n=x.getSize();
00205       Scalar const * px = x.getData();
00206       for (size_t i=0;i<n;i++) { minr=min<Scalar>(minr,px[i]); }
00207       ScalarRedn<Scalar>::setValue(minr);
00208     }
00209 
00210     string getName() const  { return "RVLMin"; }
00211   };
00212 
00215   template<class Scalar>
00216   class RVLL2innerProd: public BinaryLocalFunctionObjectScalarRedn<Scalar,Scalar> {
00217 
00218   private:
00219     mutable Scalar scale;
00220   public:
00225     RVLL2innerProd(Scalar _scale = ScalarFieldTraits<Scalar>::One(), 
00226            Scalar _init = ScalarFieldTraits<Scalar>::Zero())
00227       :  BinaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(_init),
00228      scale(_scale) {}
00229     RVLL2innerProd(const RVLL2innerProd<Scalar> & ipfo) 
00230       :  BinaryLocalFunctionObjectScalarRedn<Scalar,Scalar>(ipfo),
00231      scale(ipfo.scale) {}
00232     ~RVLL2innerProd() {}
00233 
00234     void setValue() { ScalarRedn<Scalar>::setValue(ScalarFieldTraits<Scalar>::Zero()); }
00235 
00236     using RVL::BinaryLocalConstEval<Scalar>::operator();
00237     void operator() (LocalDataContainer<Scalar> const & v, 
00238              LocalDataContainer<Scalar> const & w) {
00239       try {
00240     size_t n=v.getSize();
00241     if (n != w.getSize()) {
00242       RVLException e; e<<"Error: RVLL2innerProd::operator()\n";
00243       e<<"operands do not have same dimension\n";
00244       e<<"\noperand 1:\n";
00245       v.write(e);
00246       e<<"\noperand 2:\n";
00247       w.write(e);
00248       throw e;
00249     }
00250     Scalar const * pv = v.getData();
00251     Scalar const * pw = w.getData();
00252     Scalar raw = ScalarFieldTraits<Scalar>::Zero();
00253     Scalar ip = this->getValue();
00254     for (size_t i=0;i<n;i++) {
00255       raw += pv[i]*pw[i];
00256     }
00257     ip += scale*raw;
00258     ScalarRedn<Scalar>::setValue(ip);
00259       }
00260       catch (RVLException & e) {
00261     e<<"\ncalled from RVLL2innerProduct::operator()\n";
00262     throw e;
00263       }
00264     }
00265 
00267     Scalar getScale() const { return scale; }
00268 
00270     void setScale(Scalar newscale) { scale = newscale; }
00271 
00272     string getName() const  { return "basic rvl L2innerProd"; }
00273 
00274   };
00275 
00278   template<class Scalar>
00279   class RVLL2innerProd<complex<Scalar> >: 
00280     public BinaryLocalFunctionObjectScalarRedn<complex<Scalar>, complex<Scalar> > {
00281   private:
00282     Scalar scale;
00283   public:
00288     RVLL2innerProd(Scalar _scale = ScalarFieldTraits<Scalar>::One(), 
00289            complex<Scalar> _init = complex<Scalar>(ScalarFieldTraits<Scalar>::Zero()))
00290       : BinaryLocalFunctionObjectScalarRedn<complex<Scalar>, complex<Scalar> > (_init),
00291      scale(_scale) {}
00292     RVLL2innerProd(const RVLL2innerProd<complex<Scalar> > & ipfo)
00293       : BinaryLocalFunctionObjectScalarRedn<complex<Scalar>, complex<Scalar> > (ipfo), 
00294      scale(ipfo.scale) {}
00295     ~RVLL2innerProd() {}
00296 
00297     void setValue() { ScalarRedn<complex<Scalar> >::setValue(complex<Scalar>(ScalarFieldTraits<Scalar>::Zero())); }
00298 
00299     using RVL::BinaryLocalConstEval< complex<Scalar> >::operator();
00300     void operator() (LocalDataContainer<complex<Scalar> > const & v, 
00301              LocalDataContainer<complex<Scalar> > const & w) {
00302       try {
00303     size_t n=v.getSize();
00304     if (n != w.getSize()) {
00305       RVLException e; e<<"Error: RVLL2innerProd<complex>::operator()\n";
00306       e<<"operands do not have same dimension\n";
00307       e<<"\noperand 1:\n";
00308       v.write(e);
00309       e<<"\noperand 2:\n";
00310       w.write(e);
00311       throw e;
00312     }
00313     complex<Scalar> const * pv = v.getData();
00314     complex<Scalar> const * pw = w.getData();
00315     complex<Scalar> raw = complex<Scalar>(ScalarFieldTraits<Scalar>::Zero());
00316     complex<Scalar> ip = BinaryLocalFunctionObjectScalarRedn<complex<Scalar>, complex<Scalar> >::getValue();
00317     for (size_t i=0;i<n;i++) {
00318       raw += pv[i]*conj(pw[i]);
00319     }
00320     ip += scale*raw;
00321     ScalarRedn<complex<Scalar> >::setValue(ip);
00322       }
00323       catch (RVLException & e) {
00324     e<<"\ncalled from RVLL2innerProduct::operator()\n";
00325     throw e;
00326       }
00327     }
00328 
00330     Scalar getScale() const { return scale; }
00331 
00333     void setScale(Scalar newscale) { scale = newscale; }
00334 
00335     string getName() const  { return "basic rvl L2innerProd <complex>"; }
00336 
00337   };
00338 
00341   template<class Scalar>
00342   class RVLAddAccumulate: public BinaryLocalFunctionObject<Scalar> {
00343     
00344   public:
00345     RVLAddAccumulate() {}
00346     RVLAddAccumulate(const RVLAddAccumulate<Scalar> &) {}
00347     virtual ~RVLAddAccumulate() {}
00348 
00349     using RVL::BinaryLocalEvaluation<Scalar>::operator();
00350     void operator() (LocalDataContainer<Scalar> & v,
00351              LocalDataContainer<Scalar> const & w) {
00352       size_t n=v.getSize();
00353       if (n != w.getSize()) {
00354     RVLException e;
00355     e<<"Error: RVLAddAccumulate::operator()\n";
00356     e<<"inputs of different sizes\n";
00357     e<<"first input:\n";
00358     v.write(e);
00359     e<<"second input:\n";
00360     w.write(e);
00361     throw e;
00362       }
00363       Scalar * pv = v.getData();
00364       Scalar const * pw = w.getData();
00365       for (size_t i=0;i<n;i++) {
00366     pv[i] += pw[i];
00367       }
00368     }
00369     string getName() const  { string sname =  "RVLAddAccumulate"; return sname; }
00370   };
00371 
00375   template<class Scalar>
00376   class RVLAssignConst: public UnaryLocalFunctionObject<Scalar> {
00377   private:
00378     Scalar c;
00379     RVLAssignConst();
00380     RVLAssignConst(const RVLAssignConst<Scalar> &);
00381   public:
00382     RVLAssignConst(Scalar _c): c(_c) {}
00383     ~RVLAssignConst() {}
00384 
00385     using RVL::UnaryLocalEvaluation<Scalar>::operator();
00386     void operator() (LocalDataContainer<Scalar> & v) {
00387       size_t n = v.getSize();
00388       Scalar * pv = v.getData();
00389       for (size_t i=0;i<n;i++) {
00390     pv[i]=c;
00391       }
00392     }
00393 
00394     string getName() const { return "RVLAssignConst"; }
00395 
00396   };
00397 
00401   template<class Scalar>
00402   class RVLRandomize: public UnaryLocalFunctionObject<Scalar> {
00403   private:
00404     Scalar a;
00405     Scalar w;
00406 
00407     RVLRandomize(const RVLRandomize<Scalar> &) {}
00408     
00409   public:
00410     RVLRandomize(): a(0), w(1) {}
00411     RVLRandomize( long seed, Scalar _a = 0, Scalar _b = 1 )
00412       : a(_a), w(_b-_a) { PlantSeeds(seed); }
00413     ~RVLRandomize() {}
00414   
00415     virtual bool readsData(size_t i=0) { return false; }
00416 
00417     using RVL::UnaryLocalEvaluation<Scalar>::operator();
00418     void operator() (LocalDataContainer<Scalar> & v) {
00419       size_t n = v.getSize();
00420       Scalar * pv = v.getData();
00421       if( a == ScalarFieldTraits<Scalar>::Zero()) {
00422     if( w == ScalarFieldTraits<Scalar>::One()) {
00423       for (size_t i=0;i<n;i++) {
00424         //      pv[i]=rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00425         pv[i]=Random();
00426       }
00427     }
00428     else {
00429       for (size_t i=0;i<n;i++) {
00430         //      pv[i]=w*rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00431         pv[i]=w*Random();
00432       }
00433     }
00434       } else if (w == 1) {
00435     for (size_t i=0;i<n;i++) {
00436       //      pv[i]=rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One()) + a;
00437       pv[i]=Random() + a;
00438     }
00439       } else {
00440     for (size_t i=0;i<n;i++) {
00441       //      pv[i]=w*rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One()) + a;
00442       pv[i]=w*Random() + a;
00443     }
00444       }
00445     }
00446     
00447     string getName() const { return "RVLRandomize"; }
00448     
00449   };
00450 
00454   template<class Scalar>
00455   class RVLRandomize<complex<Scalar> >: public UnaryLocalFunctionObject<complex<Scalar> >{
00456   private:
00457     Scalar a;
00458     Scalar w;
00459 
00460     RVLRandomize(const RVLRandomize<complex<Scalar> >&) {}
00461     
00462   public:
00463     RVLRandomize()
00464       : a(ScalarFieldTraits<Scalar>::Zero()), w(ScalarFieldTraits<Scalar>::One()) {}
00465     RVLRandomize( //unsigned int seed, 
00466          long seed, 
00467          Scalar _a = ScalarFieldTraits<Scalar>::Zero(), 
00468          Scalar _b = ScalarFieldTraits<Scalar>::One() )
00469       : a(_a), w(_b-_a) { //srand(seed); }
00470       PlantSeeds(seed); }
00471     ~RVLRandomize() {}
00472   
00473     using RVL::UnaryLocalEvaluation< complex<Scalar> >::operator();
00474     void operator() (LocalDataContainer<complex<Scalar> > & v) {
00475       size_t n = v.getSize();
00476       complex<Scalar> * pv = v.getData();
00477       Scalar rex;
00478       Scalar imx;
00479       if( a == ScalarFieldTraits<Scalar>::Zero()) {
00480     if( w == ScalarFieldTraits<Scalar>::One()) {
00481       for (size_t i=0;i<n;i++) {
00482         //rex = rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00483         rex = Random();
00484         //imx = rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00485         imx = Random();
00486         pv[i]=complex<Scalar>(rex,imx);
00487       }
00488     }
00489     else {
00490       for (size_t i=0;i<n;i++) {
00491         //rex = w*rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00492         rex = w*Random();
00493         //imx = w*rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00494         imx = w*Random();
00495         pv[i]=complex<Scalar>(rex,imx);
00496       }
00497     }
00498       } else if (w == 1) {
00499     for (size_t i=0;i<n;i++) {
00500       //rex = a + rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00501       rex = a + Random();
00502       //imx = a + rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00503       imx = a + Random();
00504       pv[i]=complex<Scalar>(rex,imx);
00505     }
00506       } else {
00507     for (size_t i=0;i<n;i++) {
00508       //rex = a + w*rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00509       rex = a + w*Random();
00510       //imx = a + w*rand()/(RAND_MAX+ScalarFieldTraits<Scalar>::One());
00511       imx = a + w*Random();
00512       pv[i]=complex<Scalar>(rex,imx);
00513     }
00514       }
00515     }
00516     
00517     string getName() const { return "RVLRandomize<complex>"; }
00518     
00519   };
00520 
00523   template<>
00524   class RVLRandomize<int>: public UnaryLocalFunctionObject<int> {
00525   private:
00526     RVLRandomize(const RVLRandomize<int> &) {}
00527   public:
00528     RVLRandomize() {}
00529     ~RVLRandomize() {}
00530 
00531     using RVL::UnaryLocalEvaluation<int>::operator();
00532     virtual bool readsData(size_t i=0) { return false; }
00533 
00534     void operator() (LocalDataContainer<int> & v) {
00535       size_t n = v.getSize();
00536       int * pv = v.getData();
00537       for (size_t i=0;i<n;i++) {
00538     pv[i]=rand();
00539       }
00540     }
00541 
00542     string getName() const  { return "RVLRandomize<int>"; }
00543   };
00544 
00548   template<class Scalar>
00549   class ASCIIReader: public UnaryLocalFunctionObject<Scalar> {
00550   private:
00551     ifstream from;
00552     string file;
00553   
00554     ASCIIReader() {}
00555     ASCIIReader(const ASCIIReader &) {}
00556   public:
00557     ASCIIReader(string fname): from(fname.c_str(),ios::in),file(fname) {
00558       if (!from) {
00559     RVLException e; e<<"Error: ASCIIReader constructor\n";
00560     e<<"failed to open file "<<fname<<"\n";
00561     throw e;
00562       }
00563       // skip the first line as it is simply used to indicate dimension
00564       string l;
00565       getline(from,l);
00566     }
00567     ~ASCIIReader() { from.close(); }
00568 
00569     using RVL::UnaryLocalEvaluation<Scalar>::operator();
00570     void operator() (LocalDataContainer<Scalar> & v) {
00571       size_t n = v.getSize();
00572       Scalar * pv = v.getData();
00573       size_t i=0;
00574       while (i<n && from>>pv[i]) {
00575     // cout<<"i = "<<i<<" v = "<<pv[i]<<endl;
00576     i++;
00577       }
00578       if (i!=n) {
00579     RVLException e; e<<"Error: ASCIIReader::operator()\n";
00580     e<<"failed to read "<<n<<" scalars from file \""<<file<<"\"\n";
00581     throw e;
00582       }
00583     }
00584 
00585     string getName() const  { return "ASCIIReader"; }
00586   };
00587 
00594   template<class Scalar>
00595   class ASCIIWriter: public UnaryLocalFunctionObjectConstEval<Scalar> {
00596   private:
00597     ofstream to;
00598     string file;
00599     ASCIIWriter() {}
00600     ASCIIWriter(const ASCIIWriter &) {}
00601   public:
00602     ASCIIWriter(string fname): to(fname.c_str(),ios::out), file(fname) {
00603       if (!to) {
00604     RVLException e; e<<"Error: ASCIIWriter constructor\n";
00605     e<<"failed to open file "<<fname<<"\n";
00606     throw e;
00607       }
00608     }
00609     ~ASCIIWriter() { to.close(); }
00610 
00611     using RVL::UnaryLocalConstEval<Scalar>::operator();
00612     void operator() (LocalDataContainer<Scalar> const & v) {
00613       size_t n = v.getSize();
00614       to<<n<<endl;
00615       Scalar const * const pv = v.getData();
00616       to.setf(ios::right,ios::adjustfield);
00617       size_t i=0;
00618       while (i<n && to<<pv[i]<<endl) i++;
00619       if (i!=n) {
00620     RVLException e; e<<"Error: ASCIIWriter::operator()\n";
00621     e<<"failed to write "<<n<<" scalars to file \""<<file<<"\"\n";
00622     throw e;
00623       }
00624       to.flush();
00625     }
00626 
00627     string getName() const  { return "ASCIIWriter"; }
00628   };
00629 
00634   template<class Scalar>
00635   class BinaryReader: public UnaryLocalFunctionObjectConstEval<Scalar> {
00636   private:
00637     FILE * fp;
00638     string file;
00639     mutable long first;
00640     BinaryReader(){}
00641     BinaryReader(const BinaryReader<Scalar> &){}
00642   public:
00643 
00644     BinaryReader(char const * fname, long _first=0L)
00645       : file(fname), first(_first) {
00646       if (!(fp=fopen(fname,"r+"))) {
00647     RVLException e; e<<"Error: BinaryReader constructor\n";
00648     e<<"failed to open file "<<fname<<"\n";
00649     throw e;
00650       }
00651       if (fseek(fp,first,SEEK_SET)) {
00652     RVLException e; e<<"Error: BinaryReader constructor\n";
00653     e<<"failed to seek to specified first position = "<<first<<" in file "<<fname<<"\n";
00654     throw e;
00655       }
00656     }
00657     BinaryReader(const string fname, long _first=0L)
00658       : file(fname), first(_first) {
00659       if (!(fp=fopen(fname.c_str(),"r+"))) {
00660     RVLException e; e<<"Error: BinaryReader constructor\n";
00661     e<<"failed to open file "<<fname<<"\n";
00662     throw e;
00663       }
00664       if (fseek(fp,first,SEEK_SET)) {
00665     RVLException e; e<<"Error: BinaryReader constructor\n";
00666     e<<"failed to seek to specified first position = "<<first<<" in file "<<fname<<"\n";
00667     throw e;
00668       }
00669     }
00670     ~BinaryReader() {fclose(fp);}
00671 
00673     bool seek(size_t firstword) {
00674       first = firstword * sizeof(Scalar);
00675       if (fseek(fp,first,SEEK_SET)) return false;
00676       return true;
00677     }
00678 
00679     using RVL::UnaryLocalEvaluation<Scalar>::operator();
00680     void operator() (LocalDataContainer<Scalar> & v) {
00681       size_t n = v.getSize();
00682       Scalar * pv = v.getData();
00683       if (n != fread(pv,sizeof(Scalar),n,fp)) {
00684     RVLException e; e<<"Error: BinaryReader::operator()\n";
00685     e<<"failed to read "<<n<<" "<<sizeof(Scalar)<<"-byte words starting at position "<<first<<" from file "<<file<<"\n";
00686       throw e;
00687       }
00688     }
00689 
00690     string getName() const  { return "BinaryReader"; }
00691 
00692   };
00693 
00698   template<class Scalar>
00699   class BinaryWriter: public UnaryLocalConstEval<Scalar> {
00700   private:
00701     FILE * fp;
00702     string file;
00703     long first;
00704     BinaryWriter(){}
00705     BinaryWriter(const BinaryWriter<Scalar> &){}
00706   public:
00707 
00708     BinaryWriter(char const * fname, long _first=0L)
00709       : file(fname), first(_first) {
00710       if (!(fp=fopen(fname,"w+"))) {
00711     RVLException e; e<<"Error: BinaryWriter constructor\n";
00712     e<<"failed to open file "<<fname<<"\n";
00713     throw e;
00714       }
00715       if (fseek(fp,first,SEEK_SET)) {
00716     RVLException e; e<<"Error: BinaryWriter constructor\n";
00717     e<<"failed to seek to specified first position = "<<first<<" in file "<<fname<<"\n";
00718     throw e;
00719       }
00720     }
00721 
00722     BinaryWriter(const string fname, long _first=0L): 
00723       file(fname), first(_first) {
00724       if (!(fp=fopen(fname.c_str(),"w+"))) {
00725     RVLException e; e<<"Error: BinaryWriter constructor\n";
00726     e<<"failed to open file "<<fname<<"\n";
00727     throw e;
00728       }
00729       if (fseek(fp,first,SEEK_SET)) {
00730     RVLException e; e<<"Error: BinaryWriter constructor\n";
00731     e<<"failed to seek to specified first position = "<<first<<" in file "<<fname<<"\n";
00732     throw e;
00733       }
00734     }
00735     ~BinaryWriter() {fclose(fp);}
00736 
00738     bool seek(size_t firstword) {
00739       first = firstword * sizeof(Scalar);
00740       if (fseek(fp,first,SEEK_SET)) return false;
00741       return true;
00742     }
00743 
00744     using RVL::UnaryLocalConstEval<Scalar>::operator();
00745     void operator() (LocalDataContainer<Scalar> const & v) {
00746       size_t n = v.getSize();
00747       Scalar const * pv = v.getData();
00748       if (n != fwrite(pv,sizeof(Scalar),n,fp)) {
00749     RVLException e; e<<"Error: BinaryWriter::operator()\n";
00750     e<<"failed to write "<<n<<" "<<sizeof(Scalar)<<"-byte words starting at position "<<first<<" to file "<<file<<"\n";
00751     throw e;
00752       }
00753       fflush(fp);
00754     }
00755 
00756     string getName() const  { return "BinaryWriter"; }
00757 
00758   };
00759 
00763   template<class Scalar>
00764   class RVLBoxMaxStep: 
00765     public QuaternaryLocalFunctionObjectScalarRedn<Scalar, Scalar> {
00766 
00767   public:
00768     RVLBoxMaxStep() {}
00769     RVLBoxMaxStep(const RVLBoxMaxStep<Scalar> & b) {}
00770     ~RVLBoxMaxStep() {}
00771 
00772     using RVL::QuaternaryLocalConstEval<Scalar>::operator();    
00773     void operator()(LocalDataContainer<Scalar> const & x,
00774             LocalDataContainer<Scalar> const & dx,
00775             LocalDataContainer<Scalar> const & xmin,
00776             LocalDataContainer<Scalar> const & xmax) {
00777       try {
00778     size_t n = x.getSize();
00779     if ((n != dx.getSize()) ||
00780         (n != xmin.getSize()) ||
00781         (n != xmax.getSize())) {
00782       RVLException e;
00783       e<<"Error: GridMaxStep::operator()\n";
00784       e<<"incompatible inputs\n";
00785       throw e;
00786     }
00787     
00788     Scalar step = numeric_limits<Scalar>::max();
00789     Scalar const * px    = x.getData();
00790     Scalar const * pdx   = dx.getData();
00791     Scalar const * pxmin = xmin.getData();
00792     Scalar const * pxmax = xmax.getData();
00793     
00794     for (size_t i=0;i<n;i++) {
00795       Scalar tmp;
00796       if (pdx[i] > ScalarFieldTraits<Scalar>::Zero() && 
00797           !ProtectedDivision<Scalar>(pxmax[i]-px[i],pdx[i],tmp)) {
00798         if (tmp > ScalarFieldTraits<Scalar>::Zero()) { step = min(step,tmp); }
00799         else { step = ScalarFieldTraits<Scalar>::Zero(); }
00800       }
00801       if (pdx[i] < ScalarFieldTraits<Scalar>::Zero() &&
00802           !ProtectedDivision<Scalar>(pxmin[i]-px[i],pdx[i],tmp)) {
00803         if (tmp > ScalarFieldTraits<Scalar>::Zero()) { step = min(step,tmp); }
00804         else { step = ScalarFieldTraits<Scalar>::Zero(); }
00805       }
00806     }
00807     setValue(step);
00808       }
00809       catch (RVLException & e) {
00810     e<<"\ncalled from RVLBoxMaxStep::operator()\n";
00811     throw e;
00812       }
00813     }
00814    
00815     string getName() const { string tmp = "RVLBoxMaxStep"; return tmp; }
00816   };
00817 
00819   template<class Scalar>
00820   class ElementwiseMultiply: public TernaryLocalFunctionObject<Scalar> {
00821 
00822   public:
00823     ElementwiseMultiply() {}
00824     ~ElementwiseMultiply() {}
00825     
00826     using RVL::TernaryLocalEvaluation<Scalar>::operator();
00827     void operator() (LocalDataContainer<Scalar> & u, 
00828              LocalDataContainer<Scalar> const & v, 
00829              LocalDataContainer<Scalar> const & w) {
00830       Scalar * up = u.getData(); 
00831       Scalar const * vp = v.getData();
00832       Scalar const * wp = w.getData();
00833       size_t size = u.getSize();
00834       if( (size > v.getSize())||(size > w.getSize())) {
00835     RVLException e;
00836     e << "Error in ElementwiseMultiply::operator() - target LDC has more elements than source LDC.\n";
00837     throw e;
00838       }
00839       for(size_t i = 0; i < size; i++)
00840     up[i] = vp[i]*wp[i];
00841     }
00842 
00843     string getName() const  { string s = "ElementwiseMultiply"; return s; }
00844   };
00845 
00847   template<class Scalar>
00848   class ElementwiseDivision: public TernaryLocalFunctionObject<Scalar> {
00849 
00850   public:
00851     ElementwiseDivision() {}
00852     ~ElementwiseDivision() {}
00853     
00854     using RVL::TernaryLocalEvaluation<Scalar>::operator();
00855     void operator() (LocalDataContainer<Scalar> & u, 
00856              LocalDataContainer<Scalar> const & v, 
00857              LocalDataContainer<Scalar> const & w) {
00858       Scalar * up = u.getData();
00859       Scalar const * vp = v.getData(); 
00860       Scalar const * wp = w.getData();
00861       size_t size = u.getSize();
00862       if( (size > v.getSize())||(size > w.getSize())) {
00863     RVLException e;
00864     e << "Error: ElementwiseDivision::operator()\n";
00865     e << "target LDC has more elements than source LDC.\n";
00866     throw e;
00867       }
00868       for(size_t i = 0; i < size; i++) {
00869     if (ProtectedDivision<Scalar>(vp[i],wp[i],up[i])) {
00870       RVLException e;
00871       e<<"Error: ElementwiseDivision::operator()\n";
00872       e<<"zerodivide in element "<<i<<"\n";
00873       throw e;
00874     }
00875       }
00876     }
00877 
00878     string getName() const  { string s = "ElementwiseDivision"; return s; }
00879   };
00880 
00883   template<class Scalar>
00884   class ElementwiseSqrtAbs: public BinaryLocalFunctionObject<Scalar> {
00885 
00886   public:
00887     ElementwiseSqrtAbs() {}
00888     ~ElementwiseSqrtAbs() {}
00889     
00890     using RVL::BinaryLocalEvaluation<Scalar>::operator();
00891     void operator() (LocalDataContainer<Scalar> & u, 
00892              LocalDataContainer<Scalar> const & v) {
00893       Scalar * up = u.getData();
00894       Scalar const * vp = v.getData(); 
00895       size_t size = u.getSize();
00896       if (size > v.getSize()) {
00897     RVLException e;
00898     e << "Error: ElementwiseSqrtAbs::operator()\n";
00899     e << "target LDC has more elements than source LDC.\n";
00900     throw e;
00901       }
00902       for(size_t i = 0; i < size; i++) {
00903     up[i]=sqrt(abs(vp[i]));
00904       }
00905     }
00906 
00907     string getName() const  { string s = "ElementwiseSqrtAbs"; return s; }
00908   };
00909 
00910 }
00911       
00912 #endif
00913 
00914 
00915 
00916   

Generated on 5 Jan 2017 for LocalRVL by  doxygen 1.4.7