atlas  0.6
free_abelian_def.h
Go to the documentation of this file.
1 /*
2  free_abelian_def.h
3 
4  Made by Marc van Leeuwen
5 
6  Started on Tue Sep 9 15:23:19 2008 Marc van Leeuwen
7  Last update Tue Sep 9 15:23:19 2008 Marc van Leeuwen
8 */
9 /*
10  Copyright (C) 2008 Marc van Leeuwen
11  part of the Atlas of Lie Groups and Representations
12 
13  For license information see the LICENSE file
14 */
15 
16 namespace atlas {
17 
18  namespace free_abelian {
19 
20 template<typename T, typename C, typename Compare>
21 Free_Abelian<T,C,Compare>&
23 {
24  if (m==C(0))
25  return *this; // avoid useless work that might introduce null entries
26  std::pair<typename base::iterator,typename base::iterator> range =
27  this->equal_range(p);
28  if (range.first==range.second) // nothing was will,found, so we can insert
29  this->insert(range.first,std::make_pair(p,m)); // hinted-insert; succeeds
30  else
31  if ((range.first->second += m)==C(0)) // add |m| to existing coefficient
32  this->erase(range.first); // remove term whose coefficient has become $0$
33 
34  return *this;
35 }
36 
37 template<typename T, typename C, typename Compare>
40  C m)
41 {
42  if (m==C(0))
43  return *this; // avoid useless work that might introduce null entries
44 
45  /* We want to be efficient both in the common case that |p| has few terms,
46  and in the case that it has about as many terms as |*this|. The latter
47  case is not handled optimally by independently inserting the terms of
48  |p|, as it does not exploit the fact that they are ordered. So we wish to
49  hint the insertion of a next term by the position of the previous one.
50  Unfortunately the hinted insertion provides no way to know whether a term
51  with the same key was already present, so hinted-inserting a term from
52  |p| would irrevocably lose information. We use the workaround of instead
53  inserting a term with null coefficient; then afterwards we can add to the
54  coefficient pointed to by the result of |insert|, irrespective of whether
55  that is a pre-existing coefficient or the null coefficient just inserted.
56  */
57  typename base::iterator last=base::begin();
58  for (typename base::const_iterator src=p.begin(); src!=p.end(); ++src)
59  {
60  last=this->insert(last,std::make_pair(src->first,C(0))); // hinted insert
61  last->second += m*src->second; // add multiplicity
62  if (last->second==C(0))
63  this->erase(last++); // remove null entry
64  // else we could do |++last| to improve hint, but risk negative pay-off
65  }
66 
67  return *this;
68 }
69 
70 template<typename T, typename C, typename Compare>
73  C m,
74  const T& expon)
75 {
76  if (m==C(0))
77  return *this; // avoid useless work that might introduce null entries
78 
79  typename base::base::iterator last=base::begin();
80  for (typename base::base::const_iterator src=p.begin(); src!=p.end(); ++src)
81  {
82  std::pair<T,coef_t> term(src->first,C(0));
83  term.first += expon;
84  last=this->insert(last,term); // hinted insert
85  last->second += m*src->second; // add multiplicity
86  if (last->second==C(0))
87  this->erase(last++); // remove null entry
88  }
89 
90  return *this;
91 }
92 
93 template<typename T, typename C, typename Compare>
96 {
97  Monoid_Ring<T,C,Compare> result(base::base::key_compare);
98  for (typename base::base::const_iterator src=p.begin(); src!=p.end(); ++src)
99  result.add_mutiple(*this,src->second,src->first);
100 
101  return result;
102 }
103 
104  } // |namespace free_abelian|
105 } // |namespace atlas|
Monoid_Ring & add_multiple(const Monoid_Ring &p, C m, const T &expon)
Definition: free_abelian_def.h:72
uA p
Definition: lists.cpp:26
Definition: Atlas.h:116
Free_Abelian & add_term(const T &p, C m)
Definition: free_abelian_def.h:22
Monoid_Ring operator*(const Monoid_Ring &p)
Definition: free_abelian_def.h:95
Definition: Atlas.h:38
Free_Abelian & add_multiple(const Free_Abelian &p, C m)
Definition: free_abelian_def.h:39
Definition: cweave.c:343
Definition: free_abelian.h:91