Changeset 9952bd in git
- Timestamp:
- Sep 10, 2012, 2:13:09 PM (10 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'ad2543eab51733612ba7d118afc77edca719600e')
- Children:
- 8d341e5425f8b7b26cdf77eec4ee78cbbb121c5c
- Parents:
- 6dffa9e5b66ac5a81df2b68ff1c38dafc918b1f9
- Location:
- libpolys
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
libpolys/coeffs/Enumerator.h
r6dffa9 r9952bd 22 22 /** @class IBaseEnumerator 23 23 * 24 * Base enumerator interface for simple iteration over a generic non-empty collection. 25 * 26 * Abstract API of enumerators for non-empty enumerable collections of standalone 27 * objects. Inspired by IEnumerator from C#. Usage parrten can be as 28 * follows: 24 * Base enumerator interface for simple iteration over a generic collection. 25 * 26 * Abstract API of enumerators for enumerable collections of standalone objects. 27 * Just like IEnumerator from C#. Usage pattern can be as follows: 29 28 * 30 29 * @code 31 * IBaseEnumerator itr = ...; 32 * itr.Reset(); // goes to the first element (must exist) 33 * do 30 * IBaseEnumerator& itr = ...; 31 * itr.Reset(); // goes to the "-1" element 32 * // NOTE: itr is not useable here! 33 * while( itr.MoveNext() ) 34 34 * { 35 35 * do something custom with itr... 36 36 * } 37 * while( itr.MoveNext() )38 37 * @endcode 39 38 * 40 * Note that the first element must exist and available directly after Reset() call.39 * Note that the Reset() 41 40 * 42 41 * @sa IEnumerator … … 51 50 virtual bool MoveNext() = 0; 52 51 53 /// Sets the enumerator to its initial position, which is at the first element in the collection. 52 /// Sets the enumerator to its initial position: -1, 53 /// which is before the first element in the collection. 54 54 virtual void Reset() = 0; 55 // virtual ~IEnumerator() {} // TODO: needed? 55 56 virtual ~IBaseEnumerator() {} // TODO: needed? 57 58 private: 59 IBaseEnumerator(const IBaseEnumerator&); 60 void operator=(const IBaseEnumerator&); 61 62 protected: 63 IBaseEnumerator(){} 64 65 /// Current position is inside the collection (not -1 or past the end) 66 virtual bool IsValid() const = 0; 56 67 }; 57 68 … … 78 89 /// Gets the current element in the collection (read only). 79 90 virtual const_reference Current() const = 0; 91 92 virtual ~IAccessor() {} // TODO: needed? 93 80 94 }; 81 95 82 96 /** @class IEnumerator 83 97 * 84 * Templated enumerator interface for simple iteration over a generic non-emptycollection of T's.85 * 86 * Abstract API of enumerators for non-emptyenumerable collections of standalone87 * objects . Inspired by IEnumerator from C#. Usage parrten can be as98 * Templated enumerator interface for simple iteration over a generic collection of T's. 99 * 100 * Abstract API of enumerators for generic enumerable collections of standalone 101 * objects of type T. Inspired by IEnumerator from C#. Usage parrten can be as 88 102 * follows: 89 103 * 90 104 * @code 91 * IEnumerator<T> itr = ...; 92 * itr.Reset(); // goes to the first element (must exist) 93 * do 105 * IEnumerator<T>& itr = ...; 106 * 107 * itr.Reset(); // goes before the first element, thus no itr.Current() is available here! 108 * 109 * while( itr.MoveNext() ) 94 110 * { 95 111 * use/change itr.Current()... 96 112 * } 97 * while( itr.MoveNext() )98 113 * @endcode 99 114 * -
libpolys/coeffs/longrat.cc
r6dffa9 r9952bd 2645 2645 assume(cf != NULL); 2646 2646 assume(getCoeffType(cf) == ID); 2647 2648 numberCollectionEnumerator.Reset(); 2649 2650 if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial? 2651 { 2652 c = n_Init(1, cf); 2653 return; 2654 } 2655 2647 2656 // all coeffs are given by integers!!! 2648 2657 … … 2651 2660 int s1,s; 2652 2661 s=2147483647; // max. int 2653 numberCollectionEnumerator.Reset(); 2662 2663 2654 2664 int lc_is_pos=nlGreaterZero(numberCollectionEnumerator.Current(),cf); 2665 2666 int normalcount = 0; 2655 2667 do 2656 2668 { 2657 cand1= numberCollectionEnumerator.Current(); 2658 if (SR_HDL(cand1)&SR_INT) { cand=cand1;break;} 2659 assume(cand1->s==3); // all coeffs should be integers 2669 number& n = numberCollectionEnumerator.Current(); 2670 nlNormalize(n, cf); ++normalcount; 2671 cand1 = n; 2672 2673 if (SR_HDL(cand1)&SR_INT) { cand=cand1; break; } 2674 assume(cand1->s==3); // all coeffs should be integers // ==0?!! after printing 2660 2675 s1=mpz_size1(cand1->z); 2661 2676 if (s>s1) … … 2666 2681 } while (numberCollectionEnumerator.MoveNext() ); 2667 2682 2683 // assume( nlGreaterZero(cand,cf) ); // cand may be a negative integer! 2684 2668 2685 cand=nlCopy(cand,cf); 2669 2686 // part 2: compute gcd(cand,all coeffs) 2687 2670 2688 numberCollectionEnumerator.Reset(); 2671 do 2672 { 2673 nlNormalize(numberCollectionEnumerator.Current(),cf); 2674 nlInpGcd(cand,numberCollectionEnumerator.Current(),cf); 2689 2690 while (numberCollectionEnumerator.MoveNext() ) 2691 { 2692 number& n = numberCollectionEnumerator.Current(); 2693 2694 if( (--normalcount) <= 0) 2695 nlNormalize(n, cf); 2696 2697 nlInpGcd(cand, n, cf); 2698 2699 assume( nlGreaterZero(cand,cf) ); 2700 2675 2701 if(nlIsOne(cand,cf)) 2676 2702 { 2677 c=cand; 2703 c = cand; 2704 2678 2705 if(!lc_is_pos) 2679 2706 { 2680 2707 // make the leading coeff positive 2681 c =nlNeg(c,cf);2708 c = nlNeg(c, cf); 2682 2709 numberCollectionEnumerator.Reset(); 2683 do 2710 2711 while (numberCollectionEnumerator.MoveNext() ) 2684 2712 { 2685 numberCollectionEnumerator.Current()=nlNeg(numberCollectionEnumerator.Current(),cf); 2686 } while (numberCollectionEnumerator.MoveNext() ); 2713 number& nn = numberCollectionEnumerator.Current(); 2714 nn = nlNeg(nn, cf); 2715 } 2687 2716 } 2688 2717 return; 2689 2718 } 2690 } while (numberCollectionEnumerator.MoveNext() );2719 } 2691 2720 2692 2721 // part3: all coeffs = all coeffs / cand 2693 if (!lc_is_pos) cand=nlNeg(cand,cf); 2694 c=cand; 2722 if (!lc_is_pos) 2723 cand = nlNeg(cand,cf); 2724 2725 c = cand; 2695 2726 numberCollectionEnumerator.Reset(); 2696 do 2697 { 2698 number t=nlIntDiv(numberCollectionEnumerator.Current(),cand,cf); 2699 nlDelete(&numberCollectionEnumerator.Current(),cf); 2700 numberCollectionEnumerator.Current()=t; 2701 } while (numberCollectionEnumerator.MoveNext() ); 2702 2727 2728 while (numberCollectionEnumerator.MoveNext() ) 2729 { 2730 number& n = numberCollectionEnumerator.Current(); 2731 number t=nlIntDiv(n, cand, cf); // simple integer exact division, no ratios to remain 2732 nlDelete(&n, cf); 2733 n = t; 2734 } 2703 2735 } 2704 2736 … … 2707 2739 assume(cf != NULL); 2708 2740 assume(getCoeffType(cf) == ID); 2741 2742 numberCollectionEnumerator.Reset(); 2743 2744 if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial? 2745 { 2746 c = n_Init(1, cf); 2747 return; 2748 } 2749 2709 2750 // all coeffs are given by integers after returning from this routine 2710 2751 … … 2712 2753 number cand; 2713 2754 cand=ALLOC_RNUMBER(); 2714 2755 #if defined(LDEBUG) 2715 2756 cand->debug=123456; 2716 2757 #endif 2717 2758 cand->s=3; 2718 2759 2719 2760 int s=0; 2720 mpz_t tmp; 2721 mpz_init(tmp); 2722 numberCollectionEnumerator.Reset(); 2761 // mpz_t tmp; mpz_init(tmp); // tmp = GMP int 2762 2723 2763 int lc_is_pos=nlGreaterZero(numberCollectionEnumerator.Current(),cf); 2764 2724 2765 do 2725 2766 { … … 2730 2771 nlNormalize(cand1, cf); 2731 2772 if ((!(SR_HDL(cand1)&SR_INT)) // not a short int 2732 && (cand1->s==1)) // and is rational2773 && (cand1->s==1)) // and is a normalised rational 2733 2774 { 2734 2775 if (s==0) // first denom, we meet 2735 2776 { 2736 mpz_init_set(cand->z, cand1->n);2777 mpz_init_set(cand->z, cand1->n); // cand->z = cand1->n 2737 2778 s=1; 2738 2779 } 2739 2780 else // we have already something 2740 2781 { 2741 mpz_gcd(tmp,cand->z,cand1->n); 2742 if (mpz_cmp_si(tmp,1)!=0) 2743 { 2744 mpz_divexact(cand->z,cand->z,tmp); 2745 } 2746 mpz_mul(cand->z,cand->z,cand1->n); 2782 mpz_lcm(cand->z, cand->z, cand1->n); 2783 /* 2784 mpz_gcd(tmp,cand->z,cand1->n); // tmp = GCD( cand->z, cand1->n ) 2785 2786 if (mpz_cmp_si(tmp,1)!=0) 2787 mpz_divexact(cand->z,cand->z,tmp); // cand->z /= tmp 2788 2789 mpz_mul(cand->z,cand->z,cand1->n); // cand->z *= cand1->n 2790 */ 2747 2791 } 2748 2792 } 2749 2793 } 2750 } while (numberCollectionEnumerator.MoveNext() ); 2794 } 2795 while (numberCollectionEnumerator.MoveNext() ); 2796 2751 2797 2752 2798 if (s==0) // nothing to do, all coeffs are already integers 2753 2799 { 2754 mpz_clear(tmp);2800 // mpz_clear(tmp); 2755 2801 FREE_RNUMBER(cand); 2756 2802 if (lc_is_pos) … … 2760 2806 // make the leading coeff positive 2761 2807 c=nlInit(-1,cf); 2808 2809 // TODO: incorporate the following into the loop below? 2762 2810 numberCollectionEnumerator.Reset(); 2763 do 2764 { 2765 numberCollectionEnumerator.Current()=nlNeg(numberCollectionEnumerator.Current(),cf); 2766 } while (numberCollectionEnumerator.MoveNext() ); 2811 while (numberCollectionEnumerator.MoveNext() ) 2812 { 2813 number& n = numberCollectionEnumerator.Current(); 2814 n = nlNeg(n, cf); 2815 } 2767 2816 } 2768 2817 return; 2769 2818 } 2770 cand=nlShort3(cand); 2819 2820 cand = nlShort3(cand); 2771 2821 2772 2822 // part2: all coeffs = all coeffs * cand 2773 2823 // make the lead coeff positive 2774 2824 numberCollectionEnumerator.Reset(); 2775 if (!nlGreaterZero(numberCollectionEnumerator.Current(),cf))2776 {2777 cand =nlNeg(cand,cf);2778 }2825 2826 if (!lc_is_pos) 2827 cand = nlNeg(cand, cf); 2828 2779 2829 c = cand; 2780 do 2830 2831 while (numberCollectionEnumerator.MoveNext() ) 2781 2832 { 2782 2833 number &n = numberCollectionEnumerator.Current(); 2783 2834 n_InpMult(n, cand, cf); 2784 } while (numberCollectionEnumerator.MoveNext() );2835 } 2785 2836 2786 2837 } -
libpolys/coeffs/numbers.cc
r6dffa9 r9952bd 131 131 assume(!( nCoeff_is_Q(r) || nCoeff_is_Q_a(r) )); 132 132 // all coeffs are given by integers!!! 133 assume( nCoeff_is_Ring(r) || nCoeff_is_Zp(r) || nCoeff_is_numeric(r) || nCoeff_is_GF(r) || nCoeff_is_Zp_a(r) );134 133 135 134 numberCollectionEnumerator.Reset(); 135 136 if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial? 137 { 138 c = n_Init(1, r); 139 return; 140 } 141 142 number &curr = numberCollectionEnumerator.Current(); 136 143 137 144 #ifdef HAVE_RINGS … … 141 148 if (nCoeff_has_Units(r)) 142 149 { 143 c = n_GetUnit( numberCollectionEnumerator.Current(), r);150 c = n_GetUnit(curr, r); 144 151 145 152 if (!n_IsOne(c, r)) … … 147 154 number inv = n_Invers(c, r); 148 155 149 do 156 n_InpMult(curr, inv, r); 157 158 while( numberCollectionEnumerator.MoveNext() ) 150 159 { 151 n_InpMult(numberCollectionEnumerator.Current(), inv, r); 160 number &n = numberCollectionEnumerator.Current(); 161 n_Normalize(n, r); // ? 162 n_InpMult(n, inv, r); // TODO: either this or directly divide!!!? 152 163 } 153 while( numberCollectionEnumerator.MoveNext() );154 164 155 165 n_Delete(&inv, r); … … 162 172 163 173 assume(!nCoeff_is_Ring(r)); 164 165 c = numberCollectionEnumerator.Current(); 174 assume(nCoeff_is_Zp(r) || nCoeff_is_numeric(r) || nCoeff_is_GF(r) || nCoeff_is_Zp_a(r)); 175 176 c = curr; 166 177 167 178 n_Normalize(c, r); … … 169 180 if (!n_IsOne(c, r)) 170 181 { 171 numberCollectionEnumerator.Current()= n_Init(1, r); // ???182 curr = n_Init(1, r); // ??? 172 183 173 184 number inv = n_Invers(c, r); … … 177 188 number &n = numberCollectionEnumerator.Current(); 178 189 n_Normalize(n, r); // ? 179 n_InpMult(n, inv, r); 190 n_InpMult(n, inv, r); // TODO: either this or directly divide!!!? 180 191 } 181 192 -
libpolys/polys/Makefile.am
r6dffa9 r9952bd 48 48 ext_fields/algext.cc ext_fields/transext.cc \ 49 49 clapsing.cc clapconv.cc \ 50 nc/old.gring.cc 50 nc/old.gring.cc PolyEnumerator.cc 51 51 52 52 LIBPOLYSHEADERS = monomials/ring.h monomials/monomials.h \ -
libpolys/polys/PolyEnumerator.h
r6dffa9 r9952bd 19 19 #include <coeffs/Enumerator.h> 20 20 #include <polys/monomials/monomials.h> 21 #include <reporter/reporter.h> // for assume etc. 21 22 22 23 /** @class CBasePolyEnumerator 23 24 * 24 * Base polynomial enumerator for simple iteration over terms of 25 * (non-zero) polynomials. 25 * Base polynomial enumerator for simple iteration over terms of polynomials. 26 26 * 27 * Note that the first element must exist directly after Reset() call.28 * Moreover, it doesn't inherit from IAccessor and thus doesn't29 * override Current().27 * Note that the first element desn't exist directly after Reset() call. 28 * 29 * The class doesn't inherit from IAccessor and thus doesn't override Current(). 30 30 * 31 31 * @sa IBaseEnumerator, @sa CPolyCoeffsEnumerator … … 34 34 { 35 35 private: 36 const poly m_poly; 36 const poly m_poly; ///< essentially immutable original iterable object 37 38 static const spolyrec m_prevposition_struct; ///< tag for "-1" position 37 39 38 40 protected: 39 poly m_position; 41 poly m_position; ///< current position in the iterable object 42 43 virtual bool IsValid() const 44 { 45 // not -1 or past the end position? 46 return (m_position != NULL) && (m_position != &m_prevposition_struct); 47 } 40 48 41 inline void Iterate() 49 public: 50 CBasePolyEnumerator(poly p): 51 IBaseEnumerator(), m_poly(p), m_position(const_cast<poly>(&m_prevposition_struct)) 42 52 { 43 if( m_position != NULL ) 44 pIter( m_position ); 45 } 46 public: 47 CBasePolyEnumerator(poly p): m_poly(p), m_position(p) { assume(p != NULL); } 53 assume( !IsValid() ); 54 } 48 55 49 56 /// Sets the position marker to the leading term. 50 virtual void Reset() { assume(m_poly!= NULL); m_position = m_poly; } 57 virtual void Reset() 58 { 59 m_position = const_cast<poly>(&m_prevposition_struct); 60 assume( !IsValid() ); 61 } 51 62 52 63 /// Advances the position to the next term of the polynomial. 53 64 /// returns true if the position marker was successfully advanced to the 54 /// next term ;65 /// next term which can be used; 55 66 /// false if the position marker has passed the end of the 56 67 /// polynomial. … … 58 69 { 59 70 assume( m_position != NULL ); 60 Iterate(); 61 return (m_position != NULL); 71 72 { 73 const poly p_next = pNext(m_position); 74 75 if (p_next != NULL) // not the last term? 76 { 77 m_position = p_next; 78 assume( IsValid() ); 79 return true; 80 } 81 } 82 83 if (m_position == &m_prevposition_struct) // -1 position? 84 { 85 assume( !IsValid() ); 86 m_position = m_poly; 87 return (m_position != NULL); 88 } 89 90 // else: past the end (or an empty polynomial) 91 m_position = NULL; 92 assume( !IsValid() ); 93 return false; 62 94 } 63 95 }; … … 71 103 * 72 104 * This is a polynomial enumerator for simple iteration over 73 * coefficients of (non-zero)polynomials.105 * coefficients of polynomials. 74 106 * 75 107 * It is required to inherit this class from IEnumerator<number> for … … 90 122 virtual IPolyCoeffsEnumerator::reference Current() 91 123 { 92 assume( m_position != NULL);124 assume( IsValid() ); 93 125 return pGetCoeff(m_position); 94 126 } … … 97 129 virtual IPolyCoeffsEnumerator::const_reference Current() const 98 130 { 99 assume( m_position != NULL);131 assume( IsValid() ); 100 132 return pGetCoeff(m_position); 101 133 } -
libpolys/polys/monomials/monomials.h
r6dffa9 r9952bd 9 9 10 10 #include <omalloc/omalloc.h> 11 #include <reporter/reporter.h> // for assume etc. 11 12 12 13 struct snumber;
Note: See TracChangeset
for help on using the changeset viewer.