1 | #ifndef GRING_SA_MULT_H |
---|
2 | #define GRING_SA_MULT_H |
---|
3 | /***************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | *****************************************/ |
---|
6 | /* $Id: ncSAMult.h,v 1.5 2008-07-18 17:12:37 motsak Exp $ */ |
---|
7 | #ifdef HAVE_PLURAL |
---|
8 | |
---|
9 | // #include <ncSAMult.h> // for CMultiplier etc classes |
---|
10 | |
---|
11 | #include <structs.h> |
---|
12 | #include <ring.h> |
---|
13 | #include <summator.h> // for CPolynomialSummator class |
---|
14 | #include <febase.h> // for Print! |
---|
15 | #include <p_polys.h> |
---|
16 | #include <p_Mult_q.h> |
---|
17 | |
---|
18 | #include <ncSACache.h> // for CCacheHash etc classes |
---|
19 | |
---|
20 | // //////////////////////////////////////////////////////////////////////// // |
---|
21 | // |
---|
22 | |
---|
23 | bool ncInitSpecialPairMultiplication(ring r); |
---|
24 | |
---|
25 | |
---|
26 | template <typename CExponent> |
---|
27 | class CMultiplier |
---|
28 | { |
---|
29 | protected: |
---|
30 | ring m_basering; |
---|
31 | int m_NVars; // N = number of variables |
---|
32 | |
---|
33 | public: |
---|
34 | CMultiplier(ring rBaseRing): m_basering(rBaseRing), m_NVars(rBaseRing->N) {}; |
---|
35 | virtual ~CMultiplier() {}; |
---|
36 | |
---|
37 | const ring GetBasering() const { return m_basering; }; |
---|
38 | inline int NVars() const { return m_NVars; } |
---|
39 | |
---|
40 | |
---|
41 | inline poly LM(const poly pTerm, const ring r, int i = 1) const |
---|
42 | { |
---|
43 | poly pMonom = p_LmInit(pTerm, r); |
---|
44 | pSetCoeff0(pMonom, n_Init(i, r)); |
---|
45 | return pMonom; |
---|
46 | } |
---|
47 | |
---|
48 | // Term * Exponent -> Monom * Exponent |
---|
49 | inline poly MultiplyTE(const poly pTerm, const CExponent expRight) |
---|
50 | { |
---|
51 | const ring r = GetBasering(); |
---|
52 | poly pMonom = LM(pTerm, r); |
---|
53 | |
---|
54 | poly result = p_Mult_nn(MultiplyME(pMonom, expRight), p_GetCoeff(pTerm, r), r); |
---|
55 | |
---|
56 | p_Delete(&pMonom, r); |
---|
57 | |
---|
58 | return result; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | // Exponent * Term -> Exponent * Monom |
---|
63 | inline poly MultiplyET(const CExponent expLeft, const poly pTerm) |
---|
64 | { |
---|
65 | const ring r = GetBasering(); |
---|
66 | poly pMonom = LM(pTerm, r); |
---|
67 | |
---|
68 | poly result = p_Mult_nn(MultiplyEM(expLeft, pMonom), p_GetCoeff(pTerm, r), r); |
---|
69 | |
---|
70 | p_Delete(&pMonom, r); |
---|
71 | return result; |
---|
72 | |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | // Main templates: |
---|
77 | |
---|
78 | // Poly * Exponent |
---|
79 | inline poly MultiplyPE(const poly pPoly, const CExponent expRight) |
---|
80 | { |
---|
81 | bool bUsePolynomial = TEST_OPT_NOT_BUCKETS || (pLength(pPoly) < MIN_LENGTH_BUCKET); |
---|
82 | CPolynomialSummator sum(GetBasering(), bUsePolynomial); |
---|
83 | |
---|
84 | for( poly q = pPoly; q !=NULL; q = pNext(q) ) |
---|
85 | sum += MultiplyTE(q, expRight); |
---|
86 | |
---|
87 | return sum; |
---|
88 | } |
---|
89 | |
---|
90 | // Exponent * Poly |
---|
91 | inline poly MultiplyEP(const CExponent expLeft, const poly pPoly) |
---|
92 | { |
---|
93 | bool bUsePolynomial = TEST_OPT_NOT_BUCKETS || (pLength(pPoly) < MIN_LENGTH_BUCKET); |
---|
94 | CPolynomialSummator sum(GetBasering(), bUsePolynomial); |
---|
95 | |
---|
96 | for( poly q = pPoly; q !=NULL; q = pNext(q) ) |
---|
97 | sum += MultiplyET(expLeft, q); |
---|
98 | |
---|
99 | return sum; |
---|
100 | } |
---|
101 | |
---|
102 | // Poly * Exponent |
---|
103 | inline poly MultiplyPEDestroy(poly pPoly, const CExponent expRight) |
---|
104 | { |
---|
105 | bool bUsePolynomial = TEST_OPT_NOT_BUCKETS || (pLength(pPoly) < MIN_LENGTH_BUCKET); |
---|
106 | CPolynomialSummator sum(GetBasering(), bUsePolynomial); |
---|
107 | |
---|
108 | for( ; pPoly!=NULL; pPoly = p_LmDeleteAndNext(pPoly, GetBasering()) ) |
---|
109 | sum += MultiplyTE(pPoly, expRight); |
---|
110 | |
---|
111 | return sum; |
---|
112 | } |
---|
113 | |
---|
114 | // Exponent * Poly |
---|
115 | inline poly MultiplyEPDestroy(const CExponent expLeft, poly pPoly) |
---|
116 | { |
---|
117 | bool bUsePolynomial = TEST_OPT_NOT_BUCKETS || (pLength(pPoly) < MIN_LENGTH_BUCKET); |
---|
118 | CPolynomialSummator sum(GetBasering(), bUsePolynomial); |
---|
119 | |
---|
120 | for( ; pPoly!=NULL; pPoly = p_LmDeleteAndNext(pPoly, GetBasering()) ) |
---|
121 | sum += MultiplyET(expLeft, pPoly); |
---|
122 | |
---|
123 | return sum; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | // protected: |
---|
128 | |
---|
129 | // Exponent * Exponent |
---|
130 | virtual poly MultiplyEE(const CExponent expLeft, const CExponent expRight) = 0; |
---|
131 | |
---|
132 | // Monom * Exponent |
---|
133 | virtual poly MultiplyME(const poly pMonom, const CExponent expRight) = 0; |
---|
134 | |
---|
135 | // Exponent * Monom |
---|
136 | virtual poly MultiplyEM(const CExponent expLeft, const poly pMonom) = 0; |
---|
137 | |
---|
138 | private: // no copy constuctors! |
---|
139 | CMultiplier(); |
---|
140 | CMultiplier(const CMultiplier&); |
---|
141 | CMultiplier& operator=(const CMultiplier&); |
---|
142 | |
---|
143 | }; |
---|
144 | |
---|
145 | |
---|
146 | class CSpecialPairMultiplier: public CMultiplier<int> |
---|
147 | { |
---|
148 | private: |
---|
149 | int m_i; |
---|
150 | int m_j; |
---|
151 | |
---|
152 | poly m_c_ij; |
---|
153 | poly m_d_ij; |
---|
154 | |
---|
155 | |
---|
156 | public: |
---|
157 | // 1 <= i < j <= NVars() |
---|
158 | CSpecialPairMultiplier(ring r, int i, int j); |
---|
159 | virtual ~CSpecialPairMultiplier(); |
---|
160 | |
---|
161 | inline int GetI() const { return m_i; } |
---|
162 | inline int GetJ() const { return m_j; } |
---|
163 | |
---|
164 | // protected: |
---|
165 | typedef int CExponent; |
---|
166 | |
---|
167 | // Exponent * Exponent |
---|
168 | // Computes: var(j)^{expLeft} * var(i)^{expRight} |
---|
169 | virtual poly MultiplyEE(const CExponent expLeft, const CExponent expRight) = 0; |
---|
170 | |
---|
171 | // Monom * Exponent |
---|
172 | // pMonom must be of the form: var(j)^{n} |
---|
173 | virtual poly MultiplyME(const poly pMonom, const CExponent expRight); |
---|
174 | |
---|
175 | // Exponent * Monom |
---|
176 | // pMonom must be of the form: var(i)^{m} |
---|
177 | virtual poly MultiplyEM(const CExponent expLeft, const poly pMonom); |
---|
178 | |
---|
179 | }; |
---|
180 | |
---|
181 | |
---|
182 | class CCommutativeSpecialPairMultiplier: public CSpecialPairMultiplier |
---|
183 | { |
---|
184 | public: |
---|
185 | CCommutativeSpecialPairMultiplier(ring r, int i, int j); |
---|
186 | virtual ~CCommutativeSpecialPairMultiplier(); |
---|
187 | |
---|
188 | // Exponent * Exponent |
---|
189 | virtual poly MultiplyEE(const int expLeft, const int expRight); |
---|
190 | }; |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | struct CPower // represents var(iVar)^{iPower} |
---|
197 | { |
---|
198 | int Var; |
---|
199 | int Power; |
---|
200 | |
---|
201 | CPower(int i, int n): Var(i), Power(n) {}; |
---|
202 | |
---|
203 | inline poly GetPoly(const ring r, int c = 1) const |
---|
204 | { |
---|
205 | poly p = p_ISet(c, r); |
---|
206 | p_SetExp(p, Var, Power, r); |
---|
207 | p_Setm(p, r); |
---|
208 | return p; |
---|
209 | }; |
---|
210 | |
---|
211 | }; |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | class CPowerMultiplier: public CMultiplier<CPower> |
---|
219 | { |
---|
220 | private: |
---|
221 | CSpecialPairMultiplier** m_specialpairs; // upper triangular submatrix of pairs 1 <= i < j <= N of a N x N matrix. |
---|
222 | |
---|
223 | |
---|
224 | public: |
---|
225 | CPowerMultiplier(ring r); |
---|
226 | virtual ~CPowerMultiplier(); |
---|
227 | |
---|
228 | inline CSpecialPairMultiplier* GetPair(int i, int j) const |
---|
229 | { |
---|
230 | assume( m_specialpairs != NULL ); |
---|
231 | assume( i > 0 ); |
---|
232 | assume( i < j ); |
---|
233 | assume( j <= NVars() ); |
---|
234 | |
---|
235 | return m_specialpairs[( (NVars() * ((i)-1) - ((i) * ((i)-1))/2 + (j)-1) - (i) )]; |
---|
236 | } |
---|
237 | |
---|
238 | inline CSpecialPairMultiplier*& GetPair(int i, int j) |
---|
239 | { |
---|
240 | assume( m_specialpairs != NULL ); |
---|
241 | assume( i > 0 ); |
---|
242 | assume( i < j ); |
---|
243 | assume( j <= NVars() ); |
---|
244 | |
---|
245 | return m_specialpairs[( (NVars() * ((i)-1) - ((i) * ((i)-1))/2 + (j)-1) - (i) )]; |
---|
246 | } |
---|
247 | |
---|
248 | // protected: |
---|
249 | typedef CPower CExponent; |
---|
250 | |
---|
251 | // Exponent * Exponent |
---|
252 | // Computes: var(j)^{expLeft} * var(i)^{expRight} |
---|
253 | virtual poly MultiplyEE(const CExponent expLeft, const CExponent expRight); |
---|
254 | |
---|
255 | // Monom * Exponent |
---|
256 | // pMonom may NOT be of the form: var(j)^{n}! |
---|
257 | virtual poly MultiplyME(const poly pMonom, const CExponent expRight); |
---|
258 | |
---|
259 | // Exponent * Monom |
---|
260 | // pMonom may NOT be of the form: var(i)^{m}! |
---|
261 | virtual poly MultiplyEM(const CExponent expLeft, const poly pMonom); |
---|
262 | |
---|
263 | |
---|
264 | }; |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | class CGlobalMultiplier: public CMultiplier<poly> |
---|
270 | { |
---|
271 | private: |
---|
272 | CGlobalCacheHash* m_cache; |
---|
273 | CPowerMultiplier* m_powers; |
---|
274 | |
---|
275 | public: |
---|
276 | typedef CMultiplier<poly> CBaseType; |
---|
277 | |
---|
278 | CGlobalMultiplier(ring r); |
---|
279 | virtual ~CGlobalMultiplier(); |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | // protected: |
---|
284 | typedef poly CExponent; |
---|
285 | |
---|
286 | // the following methods are literally equal! |
---|
287 | |
---|
288 | // Exponent * Exponent |
---|
289 | // TODO: handle components!!! |
---|
290 | virtual poly MultiplyEE(const CExponent expLeft, const CExponent expRight); |
---|
291 | |
---|
292 | // Monom * Exponent |
---|
293 | virtual poly MultiplyME(const poly pMonom, const CExponent expRight); |
---|
294 | |
---|
295 | // Exponent * Monom |
---|
296 | virtual poly MultiplyEM(const CExponent expLeft, const poly pMonom); |
---|
297 | |
---|
298 | }; |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | #endif // HAVE_PLURAL :( |
---|
303 | #endif // |
---|