File: PowerSeries.h

package info (click to toggle)
singular 1%3A4.1.1-p2%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,856 kB
  • sloc: cpp: 288,280; ansic: 17,387; lisp: 4,242; yacc: 1,654; python: 1,608; makefile: 1,424; lex: 1,387; perl: 632; sh: 567; xml: 182
file content (175 lines) | stat: -rw-r--r-- 4,274 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef POWER_SERIES_HEADER
#define POWER_SERIES_HEADER
#include "kernel/mod2.h"
#include "Poly.h"
template <class traits> class PowerSeriesInputIterator:
public std::
iterator<
  std::input_iterator_tag,
  typename traits::expansion_type,
  int,
  shared_ptr<const typename traits::expansion_type>,
  const typename traits::expansion_type
  >
{
 private:
  typedef typename traits::denominator_type denominator_type;
  typedef typename traits::numerator_type numerator_type;
  typedef typename traits::expansion_type expansion_type;
  denominator_type denominator;
  numerator_type numerator;
  denominator_type toPot;
  int state;
  expansion_type data;
  denominator_type lastPot;
 public:
  PowerSeriesInputIterator(numerator_type num_arg,
                           denominator_type den_arg):
    data(den_arg.getRing()),
    lastPot(den_arg.getRing()),
    numerator(num_arg),
    denominator(den_arg)
    {
      ring r=denominator.getRing();
      //not the lead coef    Number c=denominator.leadCoef();
      Number c(1,r);
      typename traits::denominator_type::iterator it=denominator.begin();
      typename traits::denominator_type::iterator end=denominator.end();
      while(it!=end)
      {
        if ((*it).isConstant())
	{
          //change this type
          c=denominator_type(*it).leadCoef();
          break;
        }
        ++it;
      }
      c=Number(1,r)/c;
      numerator*=c;
      denominator*=c;
      toPot=denominator+denominator_type(-1,r);
      toPot*=Number(-1,r);
      //change this type
      lastPot=denominator_type(1,r);
      data=numerator;
      state=0;
    }
  PowerSeriesInputIterator()
  {
    state=-1;
  }
  void shorten()
  {
    typename expansion_type::iterator it=data.begin();
    typename expansion_type::iterator end=data.end();
    ring r=data.getRing();
    expansion_type remove(r);
    while(it!=end)
    {
      if(it->lmTotalDegree()<state)
      {
        remove+=expansion_type(*it);
      }
      it++;
    }
    remove*=Number(-1,r);
    data+=remove;
  }
  expansion_type getValue()
  {
    typename expansion_type::iterator it=data.begin();
    typename expansion_type::iterator end=data.end();
    ring r=data.getRing();
    expansion_type res(r);
    while(it!=end)
    {
      if(it->lmTotalDegree()==state)
        {
          res+=expansion_type(*it);
        }
      it++;
    }
    return res;
  }
  PowerSeriesInputIterator& operator++()
  {
    state++;
    shorten();
    lastPot*=toPot;
    data+=lastPot*numerator;
    return *this;
  }
  //bad if this are iterators for different PowerSeries
  bool operator==(const PowerSeriesInputIterator& t2)
  {
    return state==t2.state;
  }
  bool operator!=(const PowerSeriesInputIterator& t2)
  {
    return state!=t2.state;
  }
  PowerSeriesInputIterator operator++(int)
  {
    PowerSeriesInputIterator it(*this);
    ++(*this);
    return it;
  }
  const expansion_type operator*()
  {
    return expansion_type(getValue());
  }
  shared_ptr<const expansion_type> operator->()
  {
    return shared_ptr<const expansion_type>(new expansion_type(getValue()));
  }
};


template<class traits> class PowerSeriesBase
{
 public:
  typedef typename traits::denominator_type denominator_type;
  typedef typename traits::numerator_type numerator_type;
 protected:
  denominator_type denominator;
  numerator_type numerator;
 public:
  PowerSeriesBase(){}
  PowerSeriesBase(const numerator_type &a, const denominator_type & b):numerator(a),denominator(b)
  {
    assume(a.getRing()==b.getRing());
    //asume b!=NULL
  }
  typedef PowerSeriesInputIterator<traits> iterator;
  iterator begin()
  {
    return iterator(numerator,denominator);
  }
  iterator end()
  {
    return iterator();
  }
};
class PowerSeriesPolyTraits;
class PowerSeriesVectorTraits;
typedef PowerSeriesBase<PowerSeriesPolyTraits> PowerSeries;
typedef PowerSeriesBase<PowerSeriesVectorTraits> VectorPowerSeries;
class PowerSeriesPolyTraits
{
 public:
  typedef Poly numerator_type;
  typedef Poly denominator_type;
  typedef PowerSeries create_type;
  typedef Poly expansion_type;
};
class PowerSeriesVectorTraits
{
 public:
  typedef Vector numerator_type;
  typedef Poly denominator_type;
  typedef VectorPowerSeries create_type;
  typedef Vector expansion_type;
};

#endif