/************************************************************************* Copyright Rice University, 2004 - 2015. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, provided that the above copyright notice(s) and this permission notice appear in all copies of the Software and that both the above copyright notice(s) and this permission notice appear in supporting documentation. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder. **************************************************************************/ #ifndef __RVL_FCNS #define __RVL_FCNS #include "utility.hh" #include "local.hh" namespace RVL { /** This BFO copies the second LDC onto the first */ template class RVLCopy: public BinaryLocalFunctionObject { private: RVLCopy(const RVLCopy &) {} public: RVLCopy() {} ~RVLCopy() {} /** PRECONDITION: x.getSize() <= y.getSize() POSTCONDITION: x[i] == y[i] for all i in range */ using RVL::BinaryLocalEvaluation::operator(); void operator()(LocalDataContainer & x, LocalDataContainer const & y) { if (x.getSize() > y.getSize()) { RVLException e; e<<"Error: RVLCopy\n"; e<<"input shorter than output - copy does not cover\n"; throw e; } else { size_t n = x.getSize(); Scalar * px = x.getData(); Scalar const * py = y.getData(); for (size_t i=0;i class RVLCopy: public BinaryLocalFunctionObject { private: RVLCopy(const RVLCopy &) {} public: RVLCopy() {} ~RVLCopy() {} /** PRECONDITION: x.getSize() <= y.getSize() POSTCONDITION: x[i] == y[i] for all i in range */ using RVL::BinaryLocalEvaluation::operator(); void operator()(LocalDataContainer & x, LocalDataContainer const & y) { if (x.getSize() > y.getSize()) { RVLException e; e<<"Error: RVLCopy\n"; e<<"input shorter than output - copy does not cover\n"; throw e; } else { float * px = x.getData(); float const * py = y.getData(); memcpy(px, py, x.getSize()*sizeof(float)); } } string getName() const { return "RVLCopy"; } }; template<> class RVLCopy: public BinaryLocalFunctionObject { private: RVLCopy(const RVLCopy &) {} public: RVLCopy() {} ~RVLCopy() {} /** PRECONDITION: x.getSize() <= y.getSize() POSTCONDITION: x[i] == y[i] for all i in range */ using RVL::BinaryLocalEvaluation::operator(); void operator()(LocalDataContainer & x, LocalDataContainer const & y) { if (x.getSize() > y.getSize()) { RVLException e; e<<"Error: RVLCopy\n"; e<<"input shorter than output - copy does not cover\n"; throw e; } else { double * px = x.getData(); double const * py = y.getData(); memcpy(px, py, x.getSize()*sizeof(double)); } } string getName() const { return "RVLCopy"; } }; /** This UFO scales an LDC */ template class RVLScale: public UnaryLocalFunctionObject { private: Scalar c; RVLScale(); RVLScale(const RVLScale &); public: RVLScale(Scalar _c): c(_c) {} ~RVLScale() {} /** PRECONDITION: x is an LDC POSTCONDITION: x[i] == c * x[i] for all i in range */ using RVL::UnaryLocalEvaluation::operator(); void operator()(LocalDataContainer & x) { size_t n = x.getSize(); Scalar * px = x.getData(); for (size_t i=0;i class RVLMax : public UnaryLocalFunctionObjectScalarRedn { public: RVLMax() : UnaryLocalFunctionObjectScalarRedn(-numeric_limits::max()) {} RVLMax(Scalar _res) : UnaryLocalFunctionObjectScalarRedn(_res) {} RVLMax(const RVLMax & m) : UnaryLocalFunctionObjectScalarRedn(m) {} ~RVLMax() {} // override base class void setValue() { ScalarRedn::setValue(-numeric_limits::max()); } using RVL::UnaryLocalConstEval::operator(); void operator() (LocalDataContainer const & x) { Scalar maxr = ScalarRedn::getValue(); size_t n=x.getSize(); Scalar const * px = x.getData(); for (size_t i=0;i(maxr,px[i]); ScalarRedn::setValue(maxr); } string getName() const { return "RVLMax"; } }; /** This UFOSR and Accumulation finds the minimum element of an LDC Works of course only for Scalar types that have a min function.. */ template class RVLMin: public UnaryLocalFunctionObjectScalarRedn { public: RVLMin() : UnaryLocalFunctionObjectScalarRedn(numeric_limits::max()) {} RVLMin(Scalar _res) : UnaryLocalFunctionObjectScalarRedn(_res) { } RVLMin(const RVLMin & m) : UnaryLocalFunctionObjectScalarRedn(m) {} ~RVLMin() {} void setValue() { ScalarRedn::setValue(numeric_limits::max()); } using RVL::UnaryLocalConstEval::operator(); void operator() (LocalDataContainer const & x) { Scalar minr = ScalarRedn::getValue(); size_t n=x.getSize(); Scalar const * px = x.getData(); for (size_t i=0;i(minr,px[i]); } ScalarRedn::setValue(minr); } string getName() const { return "RVLMin"; } }; /** This BFOSR and Accumulation computes the inner product of two LDCs. */ template class RVLL2innerProd: public BinaryLocalFunctionObjectScalarRedn { private: mutable Scalar scale; public: /** \param _scale a scale factor applied to the inner product. Defaults to 1 \param _init uses 0 as the default initial value, but can be set otherwise. */ RVLL2innerProd(Scalar _scale = ScalarFieldTraits::One(), Scalar _init = ScalarFieldTraits::Zero()) : BinaryLocalFunctionObjectScalarRedn(_init), scale(_scale) {} RVLL2innerProd(const RVLL2innerProd & ipfo) : BinaryLocalFunctionObjectScalarRedn(ipfo), scale(ipfo.scale) {} ~RVLL2innerProd() {} void setValue() { ScalarRedn::setValue(ScalarFieldTraits::Zero()); } using RVL::BinaryLocalConstEval::operator(); void operator() (LocalDataContainer const & v, LocalDataContainer const & w) { try { size_t n=v.getSize(); if (n != w.getSize()) { RVLException e; e<<"Error: RVLL2innerProd::operator()\n"; e<<"operands do not have same dimension\n"; e<<"\noperand 1:\n"; v.write(e); e<<"\noperand 2:\n"; w.write(e); throw e; } Scalar const * pv = v.getData(); Scalar const * pw = w.getData(); Scalar raw = ScalarFieldTraits::Zero(); Scalar ip = this->getValue(); for (size_t i=0;i::setValue(ip); } catch (RVLException & e) { e<<"\ncalled from RVLL2innerProduct::operator()\n"; throw e; } } /** added to enable faithful copy in LinAlg package copy constructors */ Scalar getScale() const { return scale; } /** added to separate instantiation from initialization. */ void setScale(Scalar newscale) { scale = newscale; } string getName() const { return "basic rvl L2innerProd"; } }; /** Complex specialization of L2 Inner Product. Note: scale is REAL. */ template class RVLL2innerProd >: public BinaryLocalFunctionObjectScalarRedn, complex > { private: Scalar scale; public: /** \param _scale a scale factor applied to the inner product. Defaults to 1 \param _init uses 0 as the default initial value, but can be set otherwise. */ RVLL2innerProd(Scalar _scale = ScalarFieldTraits::One(), complex _init = complex(ScalarFieldTraits::Zero())) : BinaryLocalFunctionObjectScalarRedn, complex > (_init), scale(_scale) {} RVLL2innerProd(const RVLL2innerProd > & ipfo) : BinaryLocalFunctionObjectScalarRedn, complex > (ipfo), scale(ipfo.scale) {} ~RVLL2innerProd() {} void setValue() { ScalarRedn >::setValue(complex(ScalarFieldTraits::Zero())); } using RVL::BinaryLocalConstEval< complex >::operator(); void operator() (LocalDataContainer > const & v, LocalDataContainer > const & w) { try { size_t n=v.getSize(); if (n != w.getSize()) { RVLException e; e<<"Error: RVLL2innerProd::operator()\n"; e<<"operands do not have same dimension\n"; e<<"\noperand 1:\n"; v.write(e); e<<"\noperand 2:\n"; w.write(e); throw e; } complex const * pv = v.getData(); complex const * pw = w.getData(); complex raw = complex(ScalarFieldTraits::Zero()); complex ip = BinaryLocalFunctionObjectScalarRedn, complex >::getValue(); for (size_t i=0;i >::setValue(ip); } catch (RVLException & e) { e<<"\ncalled from RVLL2innerProduct::operator()\n"; throw e; } } /** added to enable faithful copy in LinAlg package copy constructors */ Scalar getScale() const { return scale; } /** added to separate instantiation from initialization. */ void setScale(Scalar newscale) { scale = newscale; } string getName() const { return "basic rvl L2innerProd "; } }; /** This BFO does v += w */ template class RVLAddAccumulate: public BinaryLocalFunctionObject { public: RVLAddAccumulate() {} RVLAddAccumulate(const RVLAddAccumulate &) {} virtual ~RVLAddAccumulate() {} using RVL::BinaryLocalEvaluation::operator(); void operator() (LocalDataContainer & v, LocalDataContainer const & w) { size_t n=v.getSize(); if (n != w.getSize()) { RVLException e; e<<"Error: RVLAddAccumulate::operator()\n"; e<<"inputs of different sizes\n"; e<<"first input:\n"; v.write(e); e<<"second input:\n"; w.write(e); throw e; } Scalar * pv = v.getData(); Scalar const * pw = w.getData(); for (size_t i=0;i class RVLAssignConst: public UnaryLocalFunctionObject { private: Scalar c; RVLAssignConst(); RVLAssignConst(const RVLAssignConst &); public: RVLAssignConst(Scalar _c): c(_c) {} ~RVLAssignConst() {} using RVL::UnaryLocalEvaluation::operator(); void operator() (LocalDataContainer & v) { size_t n = v.getSize(); Scalar * pv = v.getData(); for (size_t i=0;i class RVLRandomize: public UnaryLocalFunctionObject { private: Scalar a; Scalar w; RVLRandomize(const RVLRandomize &) {} public: RVLRandomize(): a(0), w(1) {} RVLRandomize( long seed, Scalar _a = 0, Scalar _b = 1 ) : a(_a), w(_b-_a) { PlantSeeds(seed); } ~RVLRandomize() {} virtual bool readsData(size_t i=0) { return false; } using RVL::UnaryLocalEvaluation::operator(); void operator() (LocalDataContainer & v) { size_t n = v.getSize(); Scalar * pv = v.getData(); if( a == ScalarFieldTraits::Zero()) { if( w == ScalarFieldTraits::One()) { for (size_t i=0;i::One()); pv[i]=Random(); } } else { for (size_t i=0;i::One()); pv[i]=w*Random(); } } } else if (w == 1) { for (size_t i=0;i::One()) + a; pv[i]=Random() + a; } } else { for (size_t i=0;i::One()) + a; pv[i]=w*Random() + a; } } } string getName() const { return "RVLRandomize"; } }; /** Assigns random values in [a, b] to each entry in the LDC where the defaults are a = 0, b = 1. */ template class RVLRandomize >: public UnaryLocalFunctionObject >{ private: Scalar a; Scalar w; RVLRandomize(const RVLRandomize >&) {} public: RVLRandomize() : a(ScalarFieldTraits::Zero()), w(ScalarFieldTraits::One()) {} RVLRandomize( //unsigned int seed, long seed, Scalar _a = ScalarFieldTraits::Zero(), Scalar _b = ScalarFieldTraits::One() ) : a(_a), w(_b-_a) { //srand(seed); } PlantSeeds(seed); } ~RVLRandomize() {} using RVL::UnaryLocalEvaluation< complex >::operator(); void operator() (LocalDataContainer > & v) { size_t n = v.getSize(); complex * pv = v.getData(); Scalar rex; Scalar imx; if( a == ScalarFieldTraits::Zero()) { if( w == ScalarFieldTraits::One()) { for (size_t i=0;i::One()); rex = Random(); //imx = rand()/(RAND_MAX+ScalarFieldTraits::One()); imx = Random(); pv[i]=complex(rex,imx); } } else { for (size_t i=0;i::One()); rex = w*Random(); //imx = w*rand()/(RAND_MAX+ScalarFieldTraits::One()); imx = w*Random(); pv[i]=complex(rex,imx); } } } else if (w == 1) { for (size_t i=0;i::One()); rex = a + Random(); //imx = a + rand()/(RAND_MAX+ScalarFieldTraits::One()); imx = a + Random(); pv[i]=complex(rex,imx); } } else { for (size_t i=0;i::One()); rex = a + w*Random(); //imx = a + w*rand()/(RAND_MAX+ScalarFieldTraits::One()); imx = a + w*Random(); pv[i]=complex(rex,imx); } } } string getName() const { return "RVLRandomize"; } }; /** Specialization for assigning random integers in [0, RAND_MAX] */ template<> class RVLRandomize: public UnaryLocalFunctionObject { private: RVLRandomize(const RVLRandomize &) {} public: RVLRandomize() {} ~RVLRandomize() {} using RVL::UnaryLocalEvaluation::operator(); virtual bool readsData(size_t i=0) { return false; } void operator() (LocalDataContainer & v) { size_t n = v.getSize(); int * pv = v.getData(); for (size_t i=0;i"; } }; /** Read a vector from a text file, where the first line is expected to contain data on the dimension of the vector. */ template class ASCIIReader: public UnaryLocalFunctionObject { private: ifstream from; string file; ASCIIReader() {} ASCIIReader(const ASCIIReader &) {} public: ASCIIReader(string fname): from(fname.c_str(),ios::in),file(fname) { if (!from) { RVLException e; e<<"Error: ASCIIReader constructor\n"; e<<"failed to open file "<::operator(); void operator() (LocalDataContainer & v) { size_t n = v.getSize(); Scalar * pv = v.getData(); size_t i=0; while (i>pv[i]) { // cout<<"i = "< class ASCIIWriter: public UnaryLocalFunctionObjectConstEval { private: ofstream to; string file; ASCIIWriter() {} ASCIIWriter(const ASCIIWriter &) {} public: ASCIIWriter(string fname): to(fname.c_str(),ios::out), file(fname) { if (!to) { RVLException e; e<<"Error: ASCIIWriter constructor\n"; e<<"failed to open file "<::operator(); void operator() (LocalDataContainer const & v) { size_t n = v.getSize(); to< class BinaryReader: public UnaryLocalFunctionObjectConstEval { private: FILE * fp; string file; mutable long first; BinaryReader(){} BinaryReader(const BinaryReader &){} public: BinaryReader(char const * fname, long _first=0L) : file(fname), first(_first) { if (!(fp=fopen(fname,"r+"))) { RVLException e; e<<"Error: BinaryReader constructor\n"; e<<"failed to open file "<::operator(); void operator() (LocalDataContainer & v) { size_t n = v.getSize(); Scalar * pv = v.getData(); if (n != fread(pv,sizeof(Scalar),n,fp)) { RVLException e; e<<"Error: BinaryReader::operator()\n"; e<<"failed to read "< class BinaryWriter: public UnaryLocalConstEval { private: FILE * fp; string file; long first; BinaryWriter(){} BinaryWriter(const BinaryWriter &){} public: BinaryWriter(char const * fname, long _first=0L) : file(fname), first(_first) { if (!(fp=fopen(fname,"w+"))) { RVLException e; e<<"Error: BinaryWriter constructor\n"; e<<"failed to open file "<::operator(); void operator() (LocalDataContainer const & v) { size_t n = v.getSize(); Scalar const * pv = v.getData(); if (n != fwrite(pv,sizeof(Scalar),n,fp)) { RVLException e; e<<"Error: BinaryWriter::operator()\n"; e<<"failed to write "< class RVLBoxMaxStep: public QuaternaryLocalFunctionObjectScalarRedn { public: RVLBoxMaxStep() {} RVLBoxMaxStep(const RVLBoxMaxStep & b) {} ~RVLBoxMaxStep() {} using RVL::QuaternaryLocalConstEval::operator(); void operator()(LocalDataContainer const & x, LocalDataContainer const & dx, LocalDataContainer const & xmin, LocalDataContainer const & xmax) { try { size_t n = x.getSize(); if ((n != dx.getSize()) || (n != xmin.getSize()) || (n != xmax.getSize())) { RVLException e; e<<"Error: GridMaxStep::operator()\n"; e<<"incompatible inputs\n"; throw e; } Scalar step = numeric_limits::max(); Scalar const * px = x.getData(); Scalar const * pdx = dx.getData(); Scalar const * pxmin = xmin.getData(); Scalar const * pxmax = xmax.getData(); for (size_t i=0;i ScalarFieldTraits::Zero() && !ProtectedDivision(pxmax[i]-px[i],pdx[i],tmp)) { if (tmp > ScalarFieldTraits::Zero()) { step = min(step,tmp); } else { step = ScalarFieldTraits::Zero(); } } if (pdx[i] < ScalarFieldTraits::Zero() && !ProtectedDivision(pxmin[i]-px[i],pdx[i],tmp)) { if (tmp > ScalarFieldTraits::Zero()) { step = min(step,tmp); } else { step = ScalarFieldTraits::Zero(); } } } setValue(step); } catch (RVLException & e) { e<<"\ncalled from RVLBoxMaxStep::operator()\n"; throw e; } } string getName() const { string tmp = "RVLBoxMaxStep"; return tmp; } }; /** Performs an elementwise multiplication. \f$u = v.*w\f$ */ template class ElementwiseMultiply: public TernaryLocalFunctionObject { public: ElementwiseMultiply() {} ~ElementwiseMultiply() {} using RVL::TernaryLocalEvaluation::operator(); void operator() (LocalDataContainer & u, LocalDataContainer const & v, LocalDataContainer const & w) { Scalar * up = u.getData(); Scalar const * vp = v.getData(); Scalar const * wp = w.getData(); size_t size = u.getSize(); if( (size > v.getSize())||(size > w.getSize())) { RVLException e; e << "Error in ElementwiseMultiply::operator() - target LDC has more elements than source LDC.\n"; throw e; } for(size_t i = 0; i < size; i++) up[i] = vp[i]*wp[i]; } string getName() const { string s = "ElementwiseMultiply"; return s; } }; /** Performs an elementwise safeguarded division. \f$u = v./w\f$ */ template class ElementwiseDivision: public TernaryLocalFunctionObject { public: ElementwiseDivision() {} ~ElementwiseDivision() {} using RVL::TernaryLocalEvaluation::operator(); void operator() (LocalDataContainer & u, LocalDataContainer const & v, LocalDataContainer const & w) { Scalar * up = u.getData(); Scalar const * vp = v.getData(); Scalar const * wp = w.getData(); size_t size = u.getSize(); if( (size > v.getSize())||(size > w.getSize())) { RVLException e; e << "Error: ElementwiseDivision::operator()\n"; e << "target LDC has more elements than source LDC.\n"; throw e; } for(size_t i = 0; i < size; i++) { if (ProtectedDivision(vp[i],wp[i],up[i])) { RVLException e; e<<"Error: ElementwiseDivision::operator()\n"; e<<"zerodivide in element "< class ElementwiseSqrtAbs: public BinaryLocalFunctionObject { public: ElementwiseSqrtAbs() {} ~ElementwiseSqrtAbs() {} using RVL::BinaryLocalEvaluation::operator(); void operator() (LocalDataContainer & u, LocalDataContainer const & v) { Scalar * up = u.getData(); Scalar const * vp = v.getData(); size_t size = u.getSize(); if (size > v.getSize()) { RVLException e; e << "Error: ElementwiseSqrtAbs::operator()\n"; e << "target LDC has more elements than source LDC.\n"; throw e; } for(size_t i = 0; i < size; i++) { up[i]=sqrt(abs(vp[i])); } } string getName() const { string s = "ElementwiseSqrtAbs"; return s; } }; } #endif