1 #include <stdio.h>
2 #include <assert.h>
3 #include "tlib.hh"
4 #include "signals.hh"
5 #include "sigprint.hh"
6 #include "ppsig.hh"
7 #include "simplify.hh"
8 #include "normalize.hh"
9 #include "sigorderrules.hh"
10 #include <map>
11 #include <list>
13 #include "mterm.hh"
14 #include "aterm.hh"
16 #if 0
17 static void countAddTerm (map<Tree,Tree>& M, Tree t, bool invflag);
18 static void incTermCount (map<Tree,int>& M, Tree t, bool invflag);
19 static Tree buildPowTerm (Tree f, int q);
20 static Tree simplifyingReorganizingMul(Tree t1, Tree t2);
21 static Tree reorganizingMul(Tree k, Tree t);
23 #endif
25 #undef TRACE
27 /**
28 * Compute the Add-Normal form of a term t.
29 * \param t the term to be normalized
30 * \return the normalized term
31 */
33 {
34 #ifdef TRACE
35 cerr << "START normalizeAddTerm : " << ppsig(t) << endl;
36 #endif
38 aterm A(t);
39 //cerr << "ATERM IS : " << A << endl;
40 mterm D = A.greatestDivisor();
41 while (D.isNotZero() && D.complexity() > 0) {
42 //cerr << "GREAT DIV : " << D << endl;
43 A = A.factorize(D);
44 D = A.greatestDivisor();
45 }
46 return A.normalizedTree();
47 }
50 /**
51 * Compute the normal form of a 1-sample delay term s'.
52 * The normalisation rules are :
53 * 0' -> 0 /// INACTIVATE dec07 bug recursion
54 * (k*s)' -> k*s'
55 * (s/k)' -> s'/k
56 * \param s the term to be delayed by 1 sample
57 * \return the normalized term
58 */
59 Tree normalizeDelay1Term(Tree s)
60 {
61 return normalizeFixedDelayTerm(s, tree(1));
62 }
65 /**
66 * Compute the normal form of a fixed delay term (s@d).
67 * The normalisation rules are :
68 * s@0 -> s
69 * 0@d -> 0
70 * (k*s)@d -> k*(s@d)
71 * (s/k)@d -> (s@d)/k
72 * (s@n)@m -> s@(n+m)
73 * Note that the same rules can't be applied to
74 * + et - becaue the value of the first d samples
75 * would be wrong. We could also add delays such that
76 * \param s the term to be delayed
77 * \param d the value of the delay
78 * \return the normalized term
79 */
81 Tree normalizeFixedDelayTerm(Tree s, Tree d)
82 {
83 Tree x, y, r;
84 int i;
86 if (isZero(d) && ! isProj(s, &i, r)) {
88 return s;
90 } else if (isZero(s)) {
92 return s;
94 } else if (isSigMul(s, x, y)) {
96 if (getSigOrder(x) < 2) {
97 return /*simplify*/(sigMul(x,normalizeFixedDelayTerm(y,d)));
98 } else if (getSigOrder(y) < 2) {
99 return /*simplify*/(sigMul(y,normalizeFixedDelayTerm(x,d)));
100 } else {
101 return sigFixDelay(s,d);
102 }
104 } else if (isSigDiv(s, x, y)) {
106 if (getSigOrder(y) < 2) {
107 return /*simplify*/(sigDiv(normalizeFixedDelayTerm(x,d),y));
108 } else {
109 return sigFixDelay(s,d);
110 }
112 } else if (isSigFixDelay(s, x, y)) {
113 // (x@n)@m = x@(n+m)
114 // return sigFixDelay(x,tree(tree2int(d)+tree2int(y)));