File: tl_static.cweb

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (89 lines) | stat: -rw-r--r-- 2,025 bytes parent folder | download | duplicates (5)
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
@q $Id: tl_static.cweb 200 2005-05-12 12:28:19Z kamenik $ @>
@q Copyright 2004, Ondra Kamenik @>

@ Start of {\tt tl\_static.cpp} file.
@c
#include "tl_static.h"
#include "tl_exception.h"

TLStatic tls;
@<|TLStatic| methods@>;
@<|PascalTriangle| constructor code@>;
@<|PascalTriangle::noverk| code@>;

@ Note that we allow for repeated calls of |init|. This is not normal
and the only purpose of allowing this is the test suite.

@<|TLStatic| methods@>=
TLStatic::TLStatic()
{
	ebundle = NULL;
	pbundle = NULL;
	ptriang = NULL;
}

TLStatic::~TLStatic()
{
	if (ebundle)
		delete ebundle;
	if (pbundle)
		delete pbundle;
	if (ptriang)
		delete ptriang;
}

void TLStatic::init(int dim, int nvar)
{
	if (ebundle)
		ebundle->generateUpTo(dim);
	else
		ebundle = new EquivalenceBundle(dim);

	if (pbundle)
		pbundle->generateUpTo(dim);
	else
		pbundle = new PermutationBundle(dim);

	if (ptriang)
		delete ptriang;
	ptriang = new PascalTriangle(nvar, dim);
}

@ The coefficients are stored in |data| row by row where a row are
coeffs with the same $k$.

We first initialize the first row with ones. Then for each other row
we initialize the first item to one, and other items are a sum of
coefficients of $n-1$ which is in code |i+j-1|.

@<|PascalTriangle| constructor code@>=
PascalTriangle::PascalTriangle(int n, int k)
	: data(new int[(n+1)*(k+1)]), kmax(k), nmax(n)
{
	for (int i = 0; i <= n; i++)
		data[i] = 1;
	for (int j = 1; j <= k; j++) {
		data[j*(nmax+1)] = 1;
		for (int i = 1; i <= n; i++)
			data[j*(nmax+1)+i] = noverk(i+j-1,j) + noverk(i+j-1,j-1);
	}
}

@ Clear. Recall, that there are |nmax+1| items in a row.
@<|PascalTriangle::noverk| code@>=
int PascalTriangle::noverk(int n, int k) const
{
	TL_RAISE_IF(k > n || n < 0,
				"Wrong arguments for PascalTriangle::noverk");

	if (k <= kmax && n-k <= nmax)
		return data[k*(nmax+1)+n-k];

	if (n-k <= kmax && k <= nmax)
		return data[(n-k)*(nmax+1)+k];

	TL_RAISE("n or k out of range in PascalTriangle::noverk");
	return 0;
}

@ End of {\tt tl\_static.cpp} file.