3 //static void collectMulTerms (Tree& coef, map<Tree,int>& M, Tree t, bool invflag=false);
9 typedef map
<Tree
,mterm
> SM
;
16 * create a aterm from a tree expression
21 cerr
<< "aterm::aterm (" << ppsig(t
)<< ")" << endl
;
25 cerr
<< "aterm::aterm (" << ppsig(t
)<< ") : -> " << *this << endl
;
31 * Add two terms trying to simplify the result
33 static Tree
simplifyingAdd(Tree t1
, Tree t2
)
38 if (isNum(t1
) && isNum(t2
)) {
39 return addNums(t1
,t2
);
41 } else if (isZero(t1
)) {
44 } else if (isZero(t2
)) {
47 } else if (t1
<= t2
) {
48 return sigAdd(t1
, t2
);
51 return sigAdd(t2
, t1
);
56 * return the corresponding normalized expression tree
59 Tree
aterm::normalizedTree() const
61 // store positive and negative tems by order and sign
62 // positive terms are stored in P[]
63 // negative terms are inverted (made positive) and stored in N[]
67 for (int order
= 0; order
< 4; order
++) P
[order
] = N
[order
] = tree(0);
69 // sum by order and sign
70 for (SM::const_iterator p
= fSig2MTerms
.begin(); p
!= fSig2MTerms
.end(); p
++) {
71 const mterm
& m
= p
->second
;
73 Tree t
= m
.normalizedTree(false, true);
74 int order
= getSigOrder(t
);
75 N
[order
] = simplifyingAdd(N
[order
],t
);
77 Tree t
= m
.normalizedTree();
78 int order
= getSigOrder(t
);
79 P
[order
] = simplifyingAdd(P
[order
],t
);
85 for (int order
= 0; order
< 4; order
++) {
86 if (!isZero(P
[order
])) {
87 SUM
= simplifyingAdd(SUM
,P
[order
]);
89 if (!isZero(N
[order
])) { // we have to substract
90 if (isZero(SUM
) && (order
< 3)) {
91 // we postpone substraction
92 N
[order
+1] = simplifyingAdd(N
[order
], N
[order
+1]);
94 SUM
= sigSub(SUM
, N
[order
]);
105 * print an aterm in a human readable format
107 ostream
& aterm::print(ostream
& dst
) const
109 if (fSig2MTerms
.empty()) {
112 const char* sep
= "";
113 for (SM::const_iterator p
= fSig2MTerms
.begin(); p
!= fSig2MTerms
.end(); p
++) {
114 dst
<< sep
<< p
->second
;
124 * Add in place an additive expression tree Go down t recursively looking
125 * for additions and substractions
127 const aterm
& aterm::operator += (Tree t
)
134 if (isSigBinOp(t
, &op
, x
, y
) && (op
== kAdd
)) {
138 } else if (isSigBinOp(t
, &op
, x
, y
) && (op
== kSub
)) {
151 * Substract in place an additive expression tree Go down t recursively looking
152 * for additions and substractions
154 const aterm
& aterm::operator -= (Tree t
)
161 if (isSigBinOp(t
, &op
, x
, y
) && (op
== kAdd
)) {
165 } else if (isSigBinOp(t
, &op
, x
, y
) && (op
== kSub
)) {
178 * Add in place an mterm
180 const aterm
& aterm::operator += (const mterm
& m
)
183 cerr
<< *this << " aterm::+= " << m
<< endl
;
185 Tree sig
= m
.signatureTree();
187 cerr
<< "signature " << *sig
<< endl
;
189 SM::const_iterator p
= fSig2MTerms
.find(sig
);
190 if (p
== fSig2MTerms
.end()) {
192 fSig2MTerms
.insert(make_pair(sig
,m
));
194 fSig2MTerms
[sig
] += m
;
201 * Substract in place an mterm
203 const aterm
& aterm::operator -= (const mterm
& m
)
205 //cerr << *this << " aterm::-= " << m << endl;
206 Tree sig
= m
.signatureTree();
207 //cerr << "signature " << *sig << endl;
208 SM::const_iterator p
= fSig2MTerms
.find(sig
);
209 if (p
== fSig2MTerms
.end()) {
211 fSig2MTerms
.insert(make_pair(sig
,m
*mterm(-1)));
213 fSig2MTerms
[sig
] -= m
;
218 mterm
aterm::greatestDivisor() const
220 int maxComplexity
= 0;
223 for (SM::const_iterator p1
= fSig2MTerms
.begin(); p1
!= fSig2MTerms
.end(); p1
++) {
224 for (SM::const_iterator p2
= p1
; p2
!= fSig2MTerms
.end(); p2
++) {
226 mterm g
= gcd(p1
->second
,p2
->second
);
227 if (g
.complexity()>maxComplexity
) {
228 maxComplexity
= g
.complexity();
234 //cerr << "greatestDivisor of " << *this << " is " << maxGCD << endl;
239 * reorganize the aterm by factorizing d
241 aterm
aterm::factorize(const mterm
& d
)
243 //cerr << "factorize : " << *this << " with " << d << endl;
247 // separate the multiple of m from the others
248 for (SM::const_iterator p1
= fSig2MTerms
.begin(); p1
!= fSig2MTerms
.end(); p1
++) {
249 mterm t
= p1
->second
;
250 if (t
.hasDivisor(d
)) {
252 //cerr << "q = " << q << endl;
254 //cerr << "step Q = " << Q << endl;
257 //cerr << "step A = " << A << endl;
261 // combines the two parts
262 //cerr << "d.normalizedTree() " << ppsig(d.normalizedTree()) << endl;
263 //cerr << "Q.normalizedTree() " << ppsig(Q.normalizedTree()) << endl;
264 //Tree tt = sigMul(d.normalizedTree(), Q.normalizedTree());
265 //cerr << "tt " << *tt << endl;
268 A
+= sigMul(d
.normalizedTree(), Q
.normalizedTree());
269 //cerr << "Final A = " << A << endl;
270 //cerr << "Final Tree " << *(A.normalizedTree()) << endl;