Generated on Tue Dec 16 13:34:02 2008 for ell-3.0.0 by doxygen 1.5.1

src/ell/spinglass/S_SG_NPP.cc

Go to the documentation of this file.
00001 #include "S_SG_NPP.hh"
00002 
00003 namespace ell
00004 {
00005 
00006     const std::string S_SG_NPP::ID = std::string("ell::S_SG_NPP");
00007     
00008     S_SG_NPP::S_SG_NPP( const WeightType* weights_
00009                         , const bool random_
00010                         , const double symmShift_)
00011      :  S_SG_Ising(weights_->size(), random_),
00012         weights(weights_), symmShift(symmShift_)
00013     {
00014     }
00015     
00016     S_SG_NPP::S_SG_NPP( const WeightType* weights_
00017                         , const std::string& spinStr
00018                         , const double symmShift_)
00019      :  S_SG_Ising(spinStr), weights(weights_), symmShift(symmShift_)
00020     {
00021     }
00022     
00023     S_SG_NPP::S_SG_NPP(const S_SG_NPP& s2)
00024      :  S_SG_Ising(s2), weights(s2.weights), symmShift(s2.symmShift)
00025     {
00026     }
00027     
00028     
00029     S_SG_NPP::~S_SG_NPP()
00030     {
00031     }
00032     
00033     const std::string & 
00034     S_SG_NPP::getID( void ) const {
00035         return S_SG_NPP::ID; 
00036     }
00037 
00038     bool
00039     S_SG_NPP::operator== (const State& state2) const {
00040         const S_SG_NPP* s2 = dynamic_cast<const S_SG_NPP*>(&state2);
00041         return  (this == &state2) 
00042                 || ( s2 != NULL
00043                     && S_SG_Ising::operator==(state2)
00044                     && weights == s2->weights
00045                     && symmShift == s2->symmShift
00046                 );
00047     }
00048     
00049     bool
00050     S_SG_NPP::operator!= (const State& state2) const {
00051         const S_SG_NPP* s2 = dynamic_cast<const S_SG_NPP*>(&state2);
00052         return  (this != &state2) 
00053                 && ( s2 == NULL
00054                     || S_SG_Ising::operator!=(state2)
00055                     || weights != s2->weights
00056                     || symmShift != s2->symmShift
00057                 );
00058     }
00059 
00060     void
00061     S_SG_NPP::operator= (const S_SG_NPP& sg2) {
00062         if (this == &sg2)
00063             return;
00064         S_SG_Ising::operator=(sg2); // copy spins
00065         weights = sg2.weights;  // copy weights
00066         symmShift = sg2.symmShift; // copy symmShift weight
00067     }
00068     
00069     double      
00070     S_SG_NPP::getEnergy() const {
00071         double sum = symmShift;
00072         for (size_t i=0; i<weights->size(); i++) {
00073             sum += weights->at(i) * ( spin[i]==S_SG_Ising::SPIN_UP ? 1 : -1 ) ;
00074         }
00075         return (sum >= 0.0 ? sum : -sum);
00076     }
00077 
00078         /* Returns a pointer to a clone of the current state. */
00079     S_SG_NPP*
00080     S_SG_NPP::clone(State* toFill) const {
00081         if (toFill == NULL) {
00082             return new S_SG_NPP(*this);
00083         }
00084           // cast toFill
00085         assertbiu( dynamic_cast<S_SG_NPP*>(toFill) != NULL
00086                     , "given State is no S_SG_NPP instance");
00087         S_SG_NPP* s = static_cast<S_SG_NPP*>(toFill);
00088         
00089           // copy data
00090         s->operator=(*this);
00091         
00092         return s;
00093     }
00094 
00097     State* 
00098     S_SG_NPP::fromString(const std::string& stringRep) const {
00099         return new S_SG_NPP(weights, stringRep, symmShift);
00100     }   
00101     
00102     State* 
00103     S_SG_NPP::getRandomNeighbor(State* inPlaceNeigh) const {
00104         S_SG_NPP *s2 = NULL;
00105          // copy myself into inPlaceNeigh
00106         if (inPlaceNeigh == NULL) {
00107             s2 = new S_SG_NPP(*this);
00108             inPlaceNeigh = s2;
00109         } else {
00110             s2 = dynamic_cast<S_SG_NPP*>(inPlaceNeigh);
00111             assertbiu(s2 != NULL, "given state is no S_SG_Ising");
00112             *s2 = *this;
00113         }
00114         
00115         return S_SG_Ising::getRandomNeighbor(s2);
00116     }
00117     
00118       // Access to a specific neighbor.
00119       // @param index the index of the neighbor in [0, getNeighborNumber())
00120       // @param neigh if != NULL this state should be converted into the 
00121       //              neighbor
00122       // @return the new neighbor state or NULL if the index is >= 
00123       // getNeighborNumber()
00124     State* 
00125     S_SG_NPP::getNeighbor(const size_t index, State* neigh) const {
00126         if (index >= spin.size())
00127             return NULL;
00128         S_SG_NPP* ret = dynamic_cast<S_SG_NPP*>(neigh);
00129         if (ret == NULL) { // create copy
00130             if (neigh != NULL)
00131                 delete neigh;
00132             ret = new S_SG_NPP(*this);
00133             neigh = ret;
00134         } else {
00135             ret->weights = this->weights;
00136             ret->symmShift = this->symmShift;
00137         }
00138         return S_SG_Ising::getNeighbor(index, ret);
00139     }
00140     
00141     
00142       // Applies the necessary changes to a given state to get the neighbor.
00143       // Note: neigh is expected to be a copy (i.e. *this == *neigh) and
00144       // therefore direct changes are possible or to be NULL. If 
00145       // (neigh==NULL) a copy of this has to be generated, modified, and
00146       // returned.
00147       // @param index the neighbor to generate
00148       // @param neigh a copy of this the changes should be applied to
00149       // @return the neighbor state of the given index or NULL if the index is
00150       //         out of range
00151     State* 
00152     S_SG_NPP::applyNeighborChange(const size_t index, State* const neigh) const {
00153         assertbiu(neigh != NULL, "no state to fill given");
00154         if (index >= spin.size())
00155             return NULL;
00156         S_SG_NPP* ret = NULL;
00157         if (neigh == NULL) {
00158             return neigh;
00159         } else {
00160             ret = dynamic_cast<S_SG_NPP*>(neigh);
00161         }
00162         assertbiu(ret != NULL, "given neigh is no S_SG_Ising object");
00163         assertbiu(*ret == *this, "given neigh is not equal to this S_SG_Ising object");
00164         return S_SG_Ising::applyNeighborChange(index, neigh);
00165     }
00166     
00167 
00168     State*  
00169     S_SG_NPP::uncompress(const CSequence& cseq, State* toFill) const {
00170         S_SG_NPP* sgi = NULL;
00171         if (toFill == NULL) {
00172             sgi = new S_SG_NPP(*this);
00173         } else {
00174             sgi = dynamic_cast<S_SG_NPP*>(toFill);
00175             assertbiu(sgi!=NULL, "given State toFill is no S_SG_NPP instance");
00176         }
00177         sgi->weights = this->weights;
00178         sgi->symmShift = this->symmShift;
00179         return S_SG_Ising::uncompress(cseq, sgi);
00180     }
00181     
00182       // Access to the weight shift used for symmetry breaking.
00183       // @return the weight shift
00184     const double 
00185     S_SG_NPP::getSymmShift(void) const
00186     {
00187         return symmShift;
00188     }
00189     
00190       // Access to the weights used.
00191       // @return the weights
00192     const S_SG_NPP::WeightType* const 
00193     S_SG_NPP::getWeights(void) const
00194     {
00195         return weights;
00196     }
00197     
00198 
00199 } // namespace ell