Generated on Mon Jun 23 17:17:53 2008 for ell-2.3.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     S_SG_NPP::S_SG_NPP(const WeightType* weights_, bool random )
00007      :  S_SG_Ising(weights_->size(), random),
00008         weights(weights_)
00009     {
00010     }
00011     
00012     S_SG_NPP::S_SG_NPP(const WeightType* weights_, const std::string& spinStr)
00013      :  S_SG_Ising(spinStr), weights(weights_) 
00014     {
00015     }
00016     
00017     S_SG_NPP::S_SG_NPP(const S_SG_NPP& s2)
00018      :  S_SG_Ising(s2), weights(s2.weights)
00019     {
00020     }
00021     
00022     
00023     S_SG_NPP::~S_SG_NPP()
00024     {
00025     }
00026 
00027     bool
00028     S_SG_NPP::operator== (const State& state2) const {
00029         const S_SG_NPP* s2 = dynamic_cast<const S_SG_NPP*>(&state2);
00030         return  (this == &state2) 
00031                 || ( s2 != NULL
00032                     && S_SG_Ising::operator==(state2)
00033                     && weights == s2->weights
00034                 );
00035     }
00036     
00037     bool
00038     S_SG_NPP::operator!= (const State& state2) const {
00039         const S_SG_NPP* s2 = dynamic_cast<const S_SG_NPP*>(&state2);
00040         return  (this != &state2) 
00041                 && ( s2 == NULL
00042                     || S_SG_Ising::operator!=(state2)
00043                     || weights != s2->weights
00044                 );
00045     }
00046 
00047     void
00048     S_SG_NPP::operator= (const S_SG_NPP& sg2) {
00049         if (this == &sg2)
00050             return;
00051         S_SG_Ising::operator=(sg2); // copy spins
00052         weights = sg2.weights;  // copy weights
00053     }
00054     
00055     double      
00056     S_SG_NPP::getEnergy() const {
00057         double sum = 0.0;
00058         for (size_t i=0; i<weights->size(); i++) {
00059             sum += weights->at(i) * ( spin[i]==S_SG_Ising::SPIN_UP ? 1 : -1 ) ;
00060         }
00061         return (sum > 0 ? sum : -sum);
00062     }
00063 
00064         /* Returns a pointer to a clone of the current state. */
00065     State*
00066     S_SG_NPP::clone() const {
00067         return new S_SG_NPP(*this);
00068     }
00069 
00072     State* 
00073     S_SG_NPP::fromString(const std::string& stringRep) const {
00074         return new S_SG_NPP(weights, stringRep);
00075     }   
00076     
00077     State* 
00078     S_SG_NPP::getRandomNeighbor(State* inPlaceNeigh) const {
00079         S_SG_NPP *s2 = NULL;
00080          // copy myself into inPlaceNeigh
00081         if (inPlaceNeigh == NULL) {
00082             s2 = new S_SG_NPP(*this);
00083             inPlaceNeigh = s2;
00084         } else {
00085             s2 = dynamic_cast<S_SG_NPP*>(inPlaceNeigh);
00086             assertbiu(s2 != NULL, "given state is no S_SG_Ising");
00087             *s2 = *this;
00088         }
00089         
00090         return S_SG_Ising::getRandomNeighbor(s2);
00091     }
00092     
00093       // Access to a specific neighbor.
00094       // @param index the index of the neighbor in [0, getNeighborNumber())
00095       // @param neigh if != NULL this state should be converted into the 
00096       //              neighbor
00097       // @return the new neighbor state or NULL if the index is >= 
00098       // getNeighborNumber()
00099     State* 
00100     S_SG_NPP::getNeighbor(const size_t index, State* neigh) const {
00101         if (index >= spin.size())
00102             return NULL;
00103         S_SG_NPP* ret = dynamic_cast<S_SG_NPP*>(neigh);
00104         if (ret == NULL) { // create copy
00105             if (neigh != NULL)
00106                 delete neigh;
00107             ret = new S_SG_NPP(*this);
00108             neigh = ret;
00109         }
00110         ret->weights = this->weights;
00111         return S_SG_Ising::getNeighbor(index, ret);
00112     }
00113     
00114     
00115       // Applies the necessary changes to a given state to get the neighbor.
00116       // Note: neigh is expected to be a copy (i.e. *this == *neigh) and
00117       // therefore direct changes are possible or to be NULL. If 
00118       // (neigh==NULL) a copy of this has to be generated, modified, and
00119       // returned.
00120       // @param index the neighbor to generate
00121       // @param neigh a copy of this the changes should be applied to
00122       // @return the neighbor state of the given index or NULL if the index is
00123       //         out of range
00124     State* 
00125     S_SG_NPP::applyNeighborChange(const size_t index, State* const neigh) const {
00126         assertbiu(neigh != NULL, "no state to fill given");
00127         if (index >= spin.size())
00128             return NULL;
00129         S_SG_NPP* ret = NULL;
00130         if (neigh == NULL) {
00131             return neigh;
00132         } else {
00133             ret = dynamic_cast<S_SG_NPP*>(neigh);
00134         }
00135         assertbiu(ret != NULL, "given neigh is no S_SG_Ising object");
00136         return S_SG_Ising::applyNeighborChange(index, neigh);
00137     }
00138     
00139 
00140     State*  
00141     S_SG_NPP::uncompress(const CSequence& cseq, State* toFill) const {
00142         S_SG_NPP* sgi = dynamic_cast<S_SG_NPP*>(toFill);
00143         if (sgi == NULL) {
00144             if (toFill != NULL)
00145                 delete toFill;
00146             sgi = new S_SG_NPP(*this);
00147             toFill = sgi;
00148         }
00149         sgi->weights = this->weights;
00150         return S_SG_Ising::uncompress(cseq, toFill);
00151     }
00152     
00153     
00154 
00155 } // namespace ell