File: mterm.cpp

package info (click to toggle)
faust 0.9.46-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,256 kB
  • ctags: 9,961
  • sloc: cpp: 47,746; sh: 2,254; ansic: 1,503; makefile: 1,211; ruby: 950; yacc: 468; objc: 459; lex: 200; xml: 177
file content (485 lines) | stat: -rw-r--r-- 10,846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "mterm.hh"
#include "signals.hh"
#include "ppsig.hh"
#include "xtended.hh"
#include <assert.h>
//static void collectMulTerms (Tree& coef, map<Tree,int>& M, Tree t, bool invflag=false);

#undef TRACE

using namespace std;

typedef map<Tree,int> MP;

mterm::mterm ()            		: fCoef(sigInt(0)) {}
mterm::mterm (int k)            : fCoef(sigInt(k)) {}
mterm::mterm (double k)         : fCoef(sigReal(k)) {}	// cerr << "DOUBLE " << endl; }
mterm::mterm (const mterm& m)   : fCoef(m.fCoef), fFactors(m.fFactors) {}

/**
 * create a mterm from a tree sexpression
 */
mterm::mterm (Tree t) : fCoef(sigInt(1))
{
    //cerr << "mterm::mterm (Tree t) : " << ppsig(t) << endl;
	*this *= t; 
	//cerr << "MTERM(" << ppsig(t) << ") -> " << *this << endl;
}

/**
 * true if mterm doesn't represent number 0
 */
bool mterm::isNotZero() const
{
	return !isZero(fCoef);
}

/**
 * true if mterm doesn't represent number 0
 */
bool mterm::isNegative() const
{
	return !isGEZero(fCoef);
}

/**
 * print a mterm in a human readable format
 */
ostream& mterm::print(ostream& dst) const
{
	const char* sep = "";
	if (!isOne(fCoef) || fFactors.empty()) { dst << ppsig(fCoef); sep = " * "; }
	//if (true) { dst << ppsig(fCoef); sep = " * "; }
	for (MP::const_iterator p = fFactors.begin(); p != fFactors.end(); p++) {
		dst << sep << ppsig(p->first);
		if (p->second != 1) dst << "**" << p->second;
		sep = " * ";
	}
	return dst;
}

/**
 * Compute the "complexity" of a mterm, that is the number of
 * factors it contains (weighted by the importance of these factors)
 */
int mterm::complexity() const
{
	int c = isOne(fCoef) ? 0 : 1;
	for (MP::const_iterator p = fFactors.begin(); p != fFactors.end(); ++p) {
		c += (1+getSigOrder(p->first))*abs(p->second);
	}
	return c;
}

/**
 * match x^p with p:int
 */
static bool isSigPow(Tree sig, Tree& x, int& n)
{
	//cerr << "isSigPow("<< *sig << ')' << endl;
	xtended* p = (xtended*) getUserData(sig);
	if (p == gPowPrim) {
       if (isSigInt(sig->branch(1), &n)) {
			x = sig->branch(0);
			//cerr << "factor of isSigPow " << *x << endl;
			return true;
		}
	}
	return false;
}

/**
 * produce x^p with p:int
 */
static Tree sigPow(Tree x, int p)
{
	return tree(gPowPrim->symbol(), x, sigInt(p));
}

/**
 * Multiple a mterm by an expression tree t. Go down recursively looking 
 * for multiplications and divisions
 */
const mterm& mterm::operator *= (Tree t)
{
	int		op, n;
	Tree	x,y;

	assert(t!=0);

	if (isNum(t)) {
		fCoef = mulNums(fCoef,t);

	} else if (isSigBinOp(t, &op, x, y) && (op == kMul)) {
		*this *= x;
		*this *= y;

	} else if (isSigBinOp(t, &op, x, y) && (op == kDiv)) {
		*this *= x;
		*this /= y;

	} else {
		if (isSigPow(t,x,n)) {
			fFactors[x] += n;
		} else {
			fFactors[t] += 1;
		}
	}
    return *this;
}

/**
 * Divide a mterm by an expression tree t. Go down recursively looking 
 * for multiplications and divisions
 */
const mterm& mterm::operator /= (Tree t)
{
	//cerr << "division en place : " << *this << " / " << ppsig(t) << endl;
	int		op,n;
	Tree	x,y;

	assert(t!=0);

	if (isNum(t)) {
		fCoef = divExtendedNums(fCoef,t);

	} else if (isSigBinOp(t, &op, x, y) && (op == kMul)) {
		*this /= x;
		*this /= y;

	} else if (isSigBinOp(t, &op, x, y) && (op == kDiv)) {
		*this /= x;
		*this *= y;

	} else {
		if (isSigPow(t,x,n)) {
			fFactors[x] -= n;
		} else {
			fFactors[t] -= 1;
		}
	}
    return *this;
}

/**
 * replace the content with a copy of m
 */
const mterm& mterm::operator = (const mterm& m)
{
	fCoef = m.fCoef;
	fFactors = m.fFactors;
    return *this;
}

/**
 * Clean a mterm by removing x**0 factors 
 */
void mterm::cleanup()
{
	if (isZero(fCoef)) {
		fFactors.clear();
	} else {
		for (MP::iterator p = fFactors.begin(); p != fFactors.end(); ) {
			if (p->second == 0) {
				fFactors.erase(p++);
			} else {
				p++;
			}
		}
	}
}

/**
 * Add in place an mterm. As we want the result to be
 * a mterm therefore essentially mterms of same signature can be added
 */
const mterm& mterm::operator += (const mterm& m)
{
	if (isZero(m.fCoef)) {
		// nothing to do
	} else if (isZero(fCoef)) 	{
		// copy of m
		fCoef = m.fCoef;
		fFactors = m.fFactors;
	} else {
		// only add mterms of same signature
		assert(signatureTree() == m.signatureTree());
		fCoef = addNums(fCoef, m.fCoef);
	}
	cleanup();
    return *this;
}

/**
 * Substract in place an mterm. As we want the result to be
 * a mterm therefore essentially mterms of same signature can be substracted
 */
const mterm& mterm::operator -= (const mterm& m)
{
	if (isZero(m.fCoef)) {
		// nothing to do
	} else if (isZero(fCoef)) 	{
		// minus of m
		fCoef = minusNum(m.fCoef);
		fFactors = m.fFactors;
	} else {
		// only add mterms of same signature
		assert(signatureTree() == m.signatureTree());
		fCoef = subNums(fCoef, m.fCoef);
	}
	cleanup();
    return *this;
}

/**
 * Multiply a mterm by the content of another mterm
 */
const mterm& mterm::operator *= (const mterm& m)
{
	fCoef = mulNums(fCoef,m.fCoef);
	for (MP::const_iterator p = m.fFactors.begin(); p != m.fFactors.end(); p++) {
		fFactors[p->first] += p->second;
	}
	cleanup();
    return *this;
}

/**
 * Divide a mterm by the content of another mterm
 */
const mterm& mterm::operator /= (const mterm& m)
{
	//cerr << "division en place : " << *this << " / " << m << endl;
	fCoef = divExtendedNums(fCoef,m.fCoef);
	for (MP::const_iterator p = m.fFactors.begin(); p != m.fFactors.end(); p++) {
		fFactors[p->first] -= p->second;
	}
	cleanup();
    return *this;
}

/**
 * Multiply two mterms
 */
mterm mterm::operator * (const mterm& m) const
{
    mterm r(*this);
    r *= m;
    return r;
}

/**
 * Divide two mterms
 */
mterm mterm::operator / (const mterm& m) const
{
    mterm r(*this);
    r /= m;
    return r;
}

/**
 * return the "common quantity" of two numbers
 */
static int common(int a, int b)
{
    if (a > 0 & b > 0) {
        return min(a,b);
    } else if (a < 0 & b < 0) {
        return max(a,b);
    } else {
        return 0;
    }
}


/**
 * return a mterm that is the greatest common divisor of two mterms
 */
mterm gcd (const mterm& m1, const mterm& m2)
{
	//cerr << "GCD of " << m1 << " and " << m2 << endl;

	Tree c = (m1.fCoef == m2.fCoef) ? m1.fCoef : tree(1);		// common coefficient (real gcd not needed)
	mterm R(c);
	for (MP::const_iterator p1 = m1.fFactors.begin(); p1 != m1.fFactors.end(); p1++) {
        Tree t = p1->first;
		MP::const_iterator p2 = m2.fFactors.find(t);
		if (p2 != m2.fFactors.end()) {
			int v1 = p1->second;
			int v2 = p2->second;
            int c = common(v1,v2);
            if (c != 0) {
                R.fFactors[t] = c;
            }
        }
    }
	//cerr << "GCD of " << m1 << " and " << m2 << " is : " << R << endl;
	return R;
}

/**
 * We say that a "contains" b if a/b > 0. For example 3 contains 2 and
 * -4 contains -2, but 3 doesn't contains -2 and -3 doesn't contains 1
 */
static bool contains(int a, int b)
{
	return (b == 0) || (a/b > 0);
}

/**
 * Check if M accept N has a divisor. We can say that N is
 * a divisor of M if M = N*Q and the complexity is preserved :
 * complexity(M) = complexity(N)+complexity(Q)
 * x**u has divisor x**v if u >= v
 * x**-u has divisor x**-v if -u <= -v
 */
bool mterm::hasDivisor (const mterm& n) const
{
	for (MP::const_iterator p1 = n.fFactors.begin(); p1 != n.fFactors.end(); p1++) {
		// for each factor f**q of m
        Tree 	f = p1->first; 	
		int 	v = p1->second;
		
		// check that f is also a factor of *this
		MP::const_iterator p2 = fFactors.find(f);
		if (p2 == fFactors.end()) return false;
		
		// analyze quantities
		int u = p2->second;
		if (! contains(u,v) ) return false;
    }
	return true;
}

/**
 * produce the canonical tree correspoding to a mterm
 */
 
/**
 * Build a power term of type f**q -> (((f.f).f)..f) with q>0
 */
static Tree buildPowTerm(Tree f, int q)
{
	assert(f);
	assert(q>0);
	if (q>1) {
		return sigPow(f, q);
	} else {
		return f;
	}
}

/**
 * Combine R and A doing R = R*A or R = A
 */
static void combineMulLeft(Tree& R, Tree A)
{
	if (R && A) 	R = sigMul(R,A);
	else if (A)		R = A;
    else exit(1);
}

/**
 * Combine R and A doing R = R*A or R = A
 */
static void combineDivLeft(Tree& R, Tree A)
{
	if (R && A) 	R = sigDiv(R,A);
	else if (A)		R = sigDiv(tree(1.0f),A);
    else exit(1);
}

/**	
 * Do M = M * f**q or D = D * f**-q
 */
static void combineMulDiv(Tree& M, Tree& D, Tree f, int q)
{
	#ifdef TRACE
	cerr << "combineMulDiv (" << M << "/"  << D << "*" << ppsig(f)<< "**" << q << endl;
	#endif
	if (f) {
        assert(q != 0);
		if (q > 0) {
			combineMulLeft(M, buildPowTerm(f,q));
		} else if (q < 0) {
			combineMulLeft(D, buildPowTerm(f,-q));
		}
	}
}	
	
			
/**
 * returns a normalized (canonical) tree expression of structure :
 * 		((v1/v2)*(c1/c2))*(s1/s2)
 */
Tree mterm::signatureTree() const
{
	return normalizedTree(true);
}
	
/**
 * returns a normalized (canonical) tree expression of structure :
 * 		((k*(v1/v2))*(c1/c2))*(s1/s2)
 * In signature mode the fCoef factor is ommited
 * In negativeMode the sign of the fCoef factor is inverted
 */
Tree mterm::normalizedTree(bool signatureMode, bool negativeMode) const
{
	if (fFactors.empty() || isZero(fCoef)) {
		// it's a pure number
		if (signatureMode) 	return tree(1);
		if (negativeMode)	return minusNum(fCoef);
		else				return fCoef;
	} else {
		// it's not a pure number, it has factors
		Tree A[4], B[4];
		
		// group by order
		for (int order = 0; order < 4; order++) {
			A[order] = 0; B[order] = 0;
			for (MP::const_iterator p = fFactors.begin(); p != fFactors.end(); p++) {
				Tree 	f = p->first;		// f = factor
				int		q = p->second;		// q = power of f
				if (f && q && getSigOrder(f)==order) {
					
					combineMulDiv (A[order], B[order], f, q);
				}
			}
		}
		if (A[0] != 0) cerr << "A[0] == " << *A[0] << endl; 
		if (B[0] != 0) cerr << "B[0] == " << *B[0] << endl; 
		// en principe ici l'order zero est vide car il correspond au coef numerique
		assert(A[0] == 0);
		assert(B[0] == 0);
		
		// we only use a coeficient if it differes from 1 and if we are not in signature mode
		if (! (signatureMode | isOne(fCoef))) {
			A[0] = (negativeMode) ? minusNum(fCoef) : fCoef;
		}
		
		if (signatureMode) {
			A[0] = 0;
		} else if (negativeMode) {
			if (isMinusOne(fCoef)) { A[0] = 0; } else { A[0] = minusNum(fCoef); }
		} else if (isOne(fCoef)) {
			A[0] = 0;
		} else {
			A[0] = fCoef;
		}
					
		// combine each order separately : R[i] = A[i]/B[i]
		Tree RR = 0;
		for (int order = 0; order < 4; order++) {
			if (A[order] && B[order]) 	combineMulLeft(RR,sigDiv(A[order],B[order]));
			else if (A[order])			combineMulLeft(RR,A[order]);
			else if (B[order])			combineDivLeft(RR,B[order]);
		}
		if (RR == 0) RR = tree(1); // a verifier *******************
			
		assert(RR);
        //cerr << "Normalized Tree of " << *this << " is " << ppsig(RR) << endl;
		return RR;
	}
}