[2c477e6] | 1 | #ifndef POWER_SERIES_HEADER |
---|
| 2 | #define POWER_SERIES_HEADER |
---|
[599326] | 3 | #include <kernel/mod2.h> |
---|
| 4 | #include <kernel/Poly.h> |
---|
[9ec8a1] | 5 | template <class traits> class PowerSeriesInputIterator: |
---|
| 6 | public std:: |
---|
| 7 | iterator< |
---|
| 8 | std::input_iterator_tag, |
---|
| 9 | typename traits::expansion_type, |
---|
[a9c298] | 10 | int, |
---|
[9ec8a1] | 11 | shared_ptr<const typename traits::expansion_type>, |
---|
[a9c298] | 12 | const typename traits::expansion_type |
---|
[9ec8a1] | 13 | > { |
---|
[2c477e6] | 14 | private: |
---|
[9ec8a1] | 15 | typedef typename traits::denominator_type denominator_type; |
---|
| 16 | typedef typename traits::numerator_type numerator_type; |
---|
| 17 | typedef typename traits::expansion_type expansion_type; |
---|
| 18 | denominator_type denominator; |
---|
| 19 | numerator_type numerator; |
---|
| 20 | denominator_type toPot; |
---|
[2c477e6] | 21 | int state; |
---|
[9ec8a1] | 22 | expansion_type data; |
---|
| 23 | denominator_type lastPot; |
---|
[2c477e6] | 24 | public: |
---|
[a9c298] | 25 | PowerSeriesInputIterator(numerator_type num_arg, |
---|
| 26 | denominator_type den_arg): |
---|
| 27 | data(den_arg.getRing()), |
---|
[9ec8a1] | 28 | lastPot(den_arg.getRing()), |
---|
| 29 | numerator(num_arg), |
---|
| 30 | denominator(den_arg){ |
---|
| 31 | |
---|
[2c477e6] | 32 | ring r=denominator.getRing(); |
---|
[9ec8a1] | 33 | |
---|
[2c477e6] | 34 | //not the lead coef Number c=denominator.leadCoef(); |
---|
| 35 | Number c(1,r); |
---|
[9ec8a1] | 36 | typename traits::denominator_type::iterator it=denominator.begin(); |
---|
| 37 | typename traits::denominator_type::iterator end=denominator.end(); |
---|
[2c477e6] | 38 | while(it!=end){ |
---|
[a9c298] | 39 | |
---|
[2c477e6] | 40 | if ((*it).isConstant()){ |
---|
[a9c298] | 41 | //change this type |
---|
| 42 | c=denominator_type(*it).leadCoef(); |
---|
| 43 | |
---|
| 44 | break; |
---|
[2c477e6] | 45 | } |
---|
[a9c298] | 46 | |
---|
[2c477e6] | 47 | ++it; |
---|
[a9c298] | 48 | |
---|
| 49 | |
---|
| 50 | |
---|
[2c477e6] | 51 | } |
---|
| 52 | c=Number(1,r)/c; |
---|
| 53 | numerator*=c; |
---|
| 54 | denominator*=c; |
---|
[9ec8a1] | 55 | toPot=denominator+denominator_type(-1,r); |
---|
[a9c298] | 56 | |
---|
[2c477e6] | 57 | toPot*=Number(-1,r); |
---|
[9ec8a1] | 58 | //change this type |
---|
| 59 | lastPot=denominator_type(1,r); |
---|
[2c477e6] | 60 | data=numerator; |
---|
| 61 | state=0; |
---|
[a9c298] | 62 | |
---|
[2c477e6] | 63 | } |
---|
| 64 | PowerSeriesInputIterator(){ |
---|
| 65 | state=-1; |
---|
| 66 | } |
---|
| 67 | void shorten(){ |
---|
[9ec8a1] | 68 | typename expansion_type::iterator it=data.begin(); |
---|
| 69 | typename expansion_type::iterator end=data.end(); |
---|
[2c477e6] | 70 | ring r=data.getRing(); |
---|
[9ec8a1] | 71 | expansion_type remove(r); |
---|
[2c477e6] | 72 | while(it!=end){ |
---|
| 73 | if(it->lmTotalDegree()<state){ |
---|
[a9c298] | 74 | remove+=expansion_type(*it); |
---|
[2c477e6] | 75 | } |
---|
| 76 | it++; |
---|
| 77 | } |
---|
| 78 | remove*=Number(-1,r); |
---|
| 79 | data+=remove; |
---|
| 80 | } |
---|
[9ec8a1] | 81 | expansion_type getValue(){ |
---|
| 82 | typename expansion_type::iterator it=data.begin(); |
---|
| 83 | typename expansion_type::iterator end=data.end(); |
---|
[2c477e6] | 84 | ring r=data.getRing(); |
---|
[9ec8a1] | 85 | expansion_type res(r); |
---|
[2c477e6] | 86 | while(it!=end){ |
---|
| 87 | if(it->lmTotalDegree()==state) |
---|
[a9c298] | 88 | { |
---|
| 89 | res+=expansion_type(*it); |
---|
| 90 | } |
---|
[2c477e6] | 91 | it++; |
---|
| 92 | } |
---|
| 93 | return res; |
---|
| 94 | } |
---|
| 95 | PowerSeriesInputIterator& operator++(){ |
---|
| 96 | state++; |
---|
| 97 | shorten(); |
---|
| 98 | lastPot*=toPot; |
---|
[a9c298] | 99 | |
---|
[2c477e6] | 100 | data+=lastPot*numerator; |
---|
[a9c298] | 101 | |
---|
| 102 | |
---|
[2c477e6] | 103 | return *this; |
---|
[a9c298] | 104 | |
---|
[2c477e6] | 105 | } |
---|
| 106 | //bad if this are iterators for different PowerSeries |
---|
| 107 | bool operator==(const PowerSeriesInputIterator& t2){ |
---|
| 108 | return state==t2.state; |
---|
| 109 | } |
---|
| 110 | bool operator!=(const PowerSeriesInputIterator& t2){ |
---|
| 111 | return state!=t2.state; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | PowerSeriesInputIterator operator++(int){ |
---|
| 117 | PowerSeriesInputIterator it(*this); |
---|
| 118 | ++(*this); |
---|
| 119 | return it; |
---|
| 120 | } |
---|
[9ec8a1] | 121 | const expansion_type operator*(){ |
---|
| 122 | return expansion_type(getValue()); |
---|
[2c477e6] | 123 | } |
---|
[9ec8a1] | 124 | shared_ptr<const expansion_type> operator->(){ |
---|
| 125 | return shared_ptr<const expansion_type>(new expansion_type(getValue())); |
---|
[2c477e6] | 126 | } |
---|
| 127 | }; |
---|
[9ec8a1] | 128 | |
---|
| 129 | |
---|
| 130 | template<class traits> class PowerSeriesBase{ |
---|
| 131 | public: |
---|
| 132 | typedef typename traits::denominator_type denominator_type; |
---|
| 133 | typedef typename traits::numerator_type numerator_type; |
---|
[2c477e6] | 134 | protected: |
---|
[9ec8a1] | 135 | denominator_type denominator; |
---|
| 136 | numerator_type numerator; |
---|
[2c477e6] | 137 | public: |
---|
[a9c298] | 138 | |
---|
[9ec8a1] | 139 | PowerSeriesBase(){ |
---|
[2c477e6] | 140 | } |
---|
[9ec8a1] | 141 | PowerSeriesBase(const numerator_type &a, const denominator_type & b):numerator(a),denominator(b){ |
---|
[2c477e6] | 142 | assume(a.getRing()==b.getRing()); |
---|
| 143 | //asume b!=NULL |
---|
| 144 | } |
---|
[9ec8a1] | 145 | typedef PowerSeriesInputIterator<traits> iterator; |
---|
[2c477e6] | 146 | iterator begin(){ |
---|
| 147 | return iterator(numerator,denominator); |
---|
| 148 | } |
---|
| 149 | iterator end(){ |
---|
| 150 | return iterator(); |
---|
| 151 | } |
---|
[a9c298] | 152 | |
---|
[2c477e6] | 153 | |
---|
| 154 | }; |
---|
[9ec8a1] | 155 | class PowerSeriesPolyTraits; |
---|
| 156 | class PowerSeriesVectorTraits; |
---|
| 157 | typedef PowerSeriesBase<PowerSeriesPolyTraits> PowerSeries; |
---|
| 158 | typedef PowerSeriesBase<PowerSeriesVectorTraits> VectorPowerSeries; |
---|
| 159 | class PowerSeriesPolyTraits{ |
---|
| 160 | public: |
---|
| 161 | typedef Poly numerator_type; |
---|
| 162 | typedef Poly denominator_type; |
---|
| 163 | typedef PowerSeries create_type; |
---|
| 164 | typedef Poly expansion_type; |
---|
| 165 | }; |
---|
| 166 | class PowerSeriesVectorTraits{ |
---|
| 167 | public: |
---|
| 168 | typedef Vector numerator_type; |
---|
| 169 | typedef Poly denominator_type; |
---|
| 170 | typedef VectorPowerSeries create_type; |
---|
| 171 | typedef Vector expansion_type; |
---|
| 172 | }; |
---|
[2c477e6] | 173 | |
---|
| 174 | |
---|
| 175 | #endif |
---|