Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnergyMap.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/scoring/EnergyMap.hh
11 /// @brief Vector of scores declaration
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 
15 #ifndef INCLUDED_core_scoring_EnergyMap_hh
16 #define INCLUDED_core_scoring_EnergyMap_hh
17 
18 // Unit Headers
20 
21 // Package Headers
23 
24 // AUTO-REMOVED #include <string>
25 #include <sstream>
26 
27 #include <core/types.hh>
28 #include <utility/vector1.hh>
29 
30 
31 namespace core {
32 namespace scoring {
33 
34 // silly class that's lightweight, manages its memory, and initializes its
35 // energies to 0.
36 
37 /// @brief A vector for storing energy data, initially all values are 0
38 /// @note: several methods using EMapVector objects add in values,
39 /// be sure to use the zero method between uses
40 ///
41 /// Common Methods:
42 /// EMapVector.zero
44 {
45 public:
46  typedef Real const * const_iterator;
47  typedef Real* iterator;
48 
49 public:
50  ///@brief default constructor, initializes the energies to 0
52  {
53  clear();
54  }
55 
56  EMapVector( EMapVector const & src ) {
57  for ( Size ii = 0; ii < n_score_types; ++ii ) {
58  map_[ ii ] = src.map_[ ii ];
59  }
60  }
61 
62  EMapVector const & operator = ( EMapVector const & rhs ) {
63  if ( &rhs == this ) return *this;
64  for ( Size ii = 0; ii < n_score_types; ++ii ) {
65  map_[ ii ] = rhs.map_[ ii ];
66  }
67  return *this;
68  }
69  /// @brief const-iterator to the begining of the vector of energies
70  const_iterator begin() const { return &(map_[0]);}
71  /// @brief const-iterator to the end of the vector of energies
72  const_iterator end() const { return ( &(map_[0]) + n_score_types ); }
73  /// @brief non-const-iterator to the begining of the vector of energies
74  iterator begin() { return &(map_[0]);}
75  /// @brief non-const-iterator to the end of the vector of energies
76  iterator end() { return ( &(map_[0]) + n_score_types ); }
77 
78  /// @brief Returns the value for ScoreType <st>
79  ///
80  /// example(s):
81  /// emap.get(fa_sol)
82  /// See also:
83  /// EMapVector
84  /// EMapVector.set
85  /// ScoreFunction
86  /// create_score_function
87  Real get(ScoreType st) { return (*this)[st]; }
88  /// @brief Sets the value for ScoreType <st> to <val>
89  ///
90  /// example(s):
91  /// emap.set(fa_sol,13.37)
92  /// See also:
93  /// EMapVector
94  /// EMapVector.get
95  /// ScoreFunction
96  /// create_score_function
97  Real set(ScoreType st, Real val) { return (*this)[st] = val; }
98 
99  /// @brief [] operator for getting a non-const reference to the energy for a ScoreType
101  {
102  assert( st > 0 && st <= n_score_types );
103  return map_[ st-1 ];
104  }
105 
106  /// @brief [] operator for getting the value for a ScoreType
108  {
109  assert( st > 0 && st <= n_score_types );
110  return map_[ st-1 ];
111  }
112 
113  /// @brief Zero a subset of the positions, as in calibrating a scale
114  void
115  zero( ScoreTypes const & l )
116  {
117  for ( ScoreTypes::const_iterator iter=l.begin(), iter_end=l.end();
118  iter != iter_end; ++iter ) {
119  operator[]( *iter ) = 0.0;
120  }
121  }
122 
123  /// @brief Set every value to zero
124  ///
125  /// example(s):
126  /// emap.zero()
127  /// See also:
128  /// EMapVector
129  /// EMapVector.get
130  /// EMapVector.set
131  /// ScoreFunction
132  /// create_score_function
133  void
135  {
136  clear();
137  }
138 
139  /// @brief Set every value to zero
140  ///
141  /// example(s):
142  /// emap.zero()
143  /// See also:
144  /// EMapVector
145  /// EMapVector.get
146  /// EMapVector.set
147  /// ScoreFunction
148  /// create_score_function
149  void
151  {
152  for (int ii = 0; ii < n_score_types; ++ii ) {
153  map_[ ii ] = 0;
154  }
155  //memset( map_, 0.0, n_score_types );
156  }
157 
158  /// @brief Returns the dot product of this object with EMapVector <src>
159  /// @note: useful for multiplying weights with scores
160  ///
161  /// example(s):
162  /// we = scorefxn.weights()
163  /// emap.dot(we)
164  /// See also:
165  /// EMapVector
166  /// ScoreFunction
167  /// ScoreFunction.weights
168  /// create_score_function
169  inline
170  Real
171  dot( EMapVector const & src ) const
172  {
173  Real total(0.0);
174  for ( int ii=0; ii< n_score_types; ++ii ) {
175  total += map_[ii] * src.map_[ii];
176  }
177  return total;
178  }
179 
180  /// @brief dot product of two EMapVectors
181  /// over a subset of the score types
182  inline
183  Real
184  dot( EMapVector const & src, ScoreTypes const & l ) const
185  {
186  Real total(0.0);
187  for ( ScoreTypes::const_iterator iter=l.begin(), iter_end=l.end();
188  iter != iter_end; ++iter ) {
189  total += operator[]( *iter ) * src[ *iter ];
190  }
191  return total;
192  }
193 
194  /// @brief += operator, for summing energies
195  inline
196  void
197  operator += ( EMapVector const & src )
198  {
199  for ( int ii=0; ii< n_score_types; ++ii ) {
200  map_[ii] += src.map_[ii];
201  }
202  }
203 
204  /// @brief -= operator, for subtracting energies
205  void
206  operator -= ( EMapVector const & src )
207  {
208  for ( int ii=0; ii< n_score_types; ++ii ) {
209  map_[ii] -= src.map_[ii];
210  }
211  }
212 
213  /// @brief *= operator, for performing multiplication of a vector by a scalar
214  void
215  operator *= ( Real scalar )
216  {
217  for ( int ii = 0; ii < n_score_types; ++ii ) {
218  map_[ii] *= scalar;
219  }
220  }
221 
222  /// @brief *= operator, for performing element-by-element multiplication of two vectors
223  void
224  operator *= ( EMapVector const & src )
225  {
226  for ( int ii = 0; ii < n_score_types; ++ii ) {
227  map_[ii] *= src.map_[ii];
228  }
229  }
230 
231 
232  /// @brief * operator, for performing multiplication of a vector by a scalar
233  EMapVector
234  operator * ( Real scalar )
235  {
236  EMapVector result(*this);
237  result *= scalar;
238  return result;
239  }
240 
241  /// @brief * operator, for performing element-by-element multiplication of two vectors
242  EMapVector
243  operator * ( EMapVector const & src )
244  {
245  EMapVector result(*this);
246  result *= src;
247  return result;
248  }
249 
250 
251  /// @brief == operator for comparing two energy maps element by element
252  inline
253  bool
254  operator == ( EMapVector const & src ) const
255  {
256  for ( int ii = 0; ii < n_score_types; ++ii ) {
257  if( map_[ii] != src.map_[ii] ) return false;
258  }
259  return true;
260  }
261 
262  /// @brief != operator for comparing two energy maps element by element
263  inline
264  bool
265  operator != ( EMapVector const & src ) const
266  {
267  for ( int ii = 0; ii < n_score_types; ++ii ) {
268  if( map_[ii] != src.map_[ii] ) return true;
269  }
270  return false;
271  }
272 
273 
274  /// @brief print the contents of an emap vector to standard out
275  void
276  print() const;
277 
278  /// @brief accumulate a subset of the positions
279  void
280  accumulate( EMapVector const & src, ScoreTypes const & l )
281  {
282  for ( ScoreTypes::const_iterator iter=l.begin(), iter_end=l.end();
283  iter != iter_end; ++iter ) {
284  operator[]( *iter ) += src[ *iter ];
285  }
286  }
287 
288  /// @brief accumulate a subset of the positions with a common weight factor
289  inline
290  void
291  accumulate( EMapVector const & src, ScoreTypes const & l, Real const wt )
292  {
293  for ( ScoreTypes::const_iterator iter=l.begin(), iter_end=l.end();
294  iter != iter_end; ++iter ) {
295  operator[]( *iter ) += wt * src[ *iter ];
296  }
297  }
298 
299  /// @brief Returns the sum of this vector
300  ///
301  /// example(s):
302  /// emap.sum()
303  /// See also:
304  /// EMapVector
305  /// EMapVector.get
306  /// EMapVector.set
307  /// ScoreFunction
308  /// create_score_function
309  Real
310  sum()
311  {
312  Real total( 0.0 );
313  for ( int ii = 0; ii < n_score_types; ++ii ) {
314  total += map_[ii];
315  }
316  return total;
317  }
318 
319  /// @brief accumulate the squared values of a subset of the positions
320  Real
322  {
323  Real total( 0.0 );
324  for ( ScoreTypes::const_iterator iter=l.begin(), iter_end=l.end();
325  iter != iter_end; ++iter ) {
326  // could use numeric::square
327  Real const val( operator[]( *iter ) );
328  total += val * val;
329  }
330  return total;
331  }
332 
333  /// @brief Prints the non-zero positions of the energy map
334  /// @brief Set every value to zero
335  ///
336  /// example(s):
337  /// emap.show_nonzero()
338  /// See also:
339  /// EMapVector
340  /// EMapVector.get
341  /// ScoreFunction
342  /// create_score_function
343  void
344  show_nonzero( std::ostream & out ) const
345  {
346  //out << "EnergyMap::show_nonzero():";
347  for ( int ii = 1; ii <= n_score_types; ++ii ) {
348  Real const val( operator[]( ScoreType(ii) ) );
349  if ( val != 0.0 ) {
350  out << ' ' << ScoreType(ii) << ": " << val;
351  }
352  }
353  //out << '\n';
354  }
355 
356  /// @brief convert the non-zero positions of the energy map to a string
358  show_nonzero() const
359  {
360  std::ostringstream os;
361  show_nonzero(os);
362  return os.str();
363  }
364 
365  /// @brief write the energies in this energy map to the output stream for those
366  /// score types that have non-zero values in the "weights" energy map.
367  void
368  show_if_nonzero_weight( std::ostream & out, EMapVector const & weights ) const;
369 
370  /// @brief write the weighted energies in this energy map to the output stream for those
371  /// score types that have non-zero values in the "weights" energy map.
372  void
373  show_weighted( std::ostream & out, EMapVector const & weights ) const;
374 
375  /// @brief convert the weighted energies in this energy map to a string
376  /// for those score types that have non-zero values in the "weights" energy map.
378  weighted_string_of( EMapVector const & weights ) const;
379 
380  /// Grant access to private data to the TwoBodyEMapVector
381  //friend class TwoBodyEMapVector;
382 
383 private:
384 
385 #ifdef USEBOOSTSERIALIZE
386  friend class boost::serialization::access;
387 
388  template<class Archive>
389  void serialize(Archive & ar, const unsigned int version) {
390  ar & map_;
391  }
392 #endif
393 
394  /// EMapVector is an array. EMapVector[score_type] = value. Can be used for storing either energy or weight for each score_type.
396 };
397 
398 
399 /// output operator (index;value)
400 inline
401 std::ostream &
402 operator << ( std::ostream & ost, EMapVector const & emap )
403 {
404  for ( int ii = 1; ii <= n_score_types; ++ii )
405  {
406  ost << "( " << ScoreType(ii) << "; " << emap[ ScoreType (ii) ] << ") ";
407  }
408  return ost;
409 }
410 
411 
412 
413 } // namespace scoring
414 } // namespace core
415 
416 #endif // INCLUDED_core_scoring_EnergyMap_HH