File: rfs_tensor.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 (205 lines) | stat: -rw-r--r-- 5,409 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
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
@q $Id: rfs_tensor.cweb 148 2005-04-19 15:12:26Z kamenik $ @>
@q Copyright 2004, Ondra Kamenik @>

@ Start of {\tt rfs\_tensor.cpp} file.

@c
#include "rfs_tensor.h"
#include "kron_prod.h"
#include "tl_exception.h"

@<|FRTensor| conversion from unfolded@>;
@<|FRTensor::unfold| code@>;
@<|FRTensor::increment| code@>;
@<|FRTensor::decrement| code@>;
@<|URTensor| conversion from folded@>;
@<|URTensor::fold| code@>;
@<|URTensor| increment and decrement@>;
@<|URTensor::getOffset| code@>;
@<|URSingleTensor| constructor 1 code@>;
@<|URSingleTensor| constructor 2 code@>;
@<|URSingleTensor::fold| code@>;
@<|FRSingleTensor| conversion from unfolded@>;

@ The conversion from unfolded to folded sums up all data from
unfolded corresponding to one folded index. So we go through all the
rows in the unfolded tensor |ut|, make an index of the folded tensor
by sorting the coordinates, and add the row.
 
@<|FRTensor| conversion from unfolded@>=
FRTensor::FRTensor(const URTensor& ut)
	: FTensor(along_row, IntSequence(ut.dimen(), ut.nvar()),
			  FFSTensor::calcMaxOffset(ut.nvar(), ut.dimen()), ut.ncols(),
			  ut.dimen()),
	  nv(ut.nvar())
{
	zeros();
	for (index in = ut.begin(); in != ut.end(); ++in) {
		IntSequence vtmp(in.getCoor());
		vtmp.sort();
		index tar(this, vtmp);
		addRow(ut, *in, *tar);
	}
}

@ Here just make a new instance and return the reference.
@<|FRTensor::unfold| code@>=
UTensor& FRTensor::unfold() const
{
	return *(new URTensor(*this));
}

@ Incrementing is easy. The same as for |FFSTensor|.

@<|FRTensor::increment| code@>=
void FRTensor::increment(IntSequence& v) const
{
	TL_RAISE_IF(v.size() != dimen(),
				"Wrong input/output vector size in FRTensor::increment");

	UTensor::increment(v, nv);
	v.monotone();
}

@ Decrement calls static |FTensor::decrement|.

@<|FRTensor::decrement| code@>=
void FRTensor::decrement(IntSequence& v) const
{
	TL_RAISE_IF(v.size() != dimen(),
				"Wrong input/output vector size in FRTensor::decrement");

	FTensor::decrement(v, nv);
}


@ Here we convert folded full symmetry tensor to unfolded. We copy all
columns of folded tensor to unfolded and leave other columns
(duplicates) zero. In this way, if the unfolded tensor is folded back,
we should get the same data.

@<|URTensor| conversion from folded@>=
URTensor::URTensor(const FRTensor& ft)
	: UTensor(along_row, IntSequence(ft.dimen(), ft.nvar()),
			  UFSTensor::calcMaxOffset(ft.nvar(), ft.dimen()), ft.ncols(),
			  ft.dimen()),
	  nv(ft.nvar())
{
	zeros();
	for (index src = ft.begin(); src != ft.end(); ++src) {
		index in(this, src.getCoor());
		copyRow(ft, *src, *in);
	}
}

@ Here we just return a reference to new instance of folded tensor.
@<|URTensor::fold| code@>=
FTensor& URTensor::fold() const
{
	return *(new FRTensor(*this));
}

@ Here we just call |UTensor| respective static methods.
@<|URTensor| increment and decrement@>=
void URTensor::increment(IntSequence& v) const
{
	TL_RAISE_IF(v.size() != dimen(),
				"Wrong input/output vector size in URTensor::increment");

	UTensor::increment(v, nv);
}

void URTensor::decrement(IntSequence& v) const
{
	TL_RAISE_IF(v.size() != dimen(),
				"Wrong input/output vector size in URTensor::decrement");

	UTensor::decrement(v, nv);
}

@ 
@<|URTensor::getOffset| code@>=
int URTensor::getOffset(const IntSequence& v) const
{
	TL_RAISE_IF(v.size() != dimen(),
				"Wrong input vector size in URTensor::getOffset");

	return UTensor::getOffset(v, nv);
}

@ Here we construct $v_1\otimes v_2\otimes\ldots\otimes v_n$, where
$v_1,v_2,\ldots,v_n$ are stored in |vector<ConstVector>|.

@<|URSingleTensor| constructor 1 code@>=
URSingleTensor::URSingleTensor(const vector<ConstVector>& cols)
	: URTensor(1, cols[0].length(), cols.size())
{
	if (dimen() == 1) {
		getData() = cols[0];
		return;
	}

	Vector* last = new Vector(cols[cols.size()-1]);
	for (int i = cols.size()-2; i > 0; i--) {
		Vector* newlast = new Vector(Tensor::power(nvar(), cols.size()-i));
		KronProd::kronMult(cols[i], ConstVector(*last), *newlast);
		delete last;
		last = newlast;
	}
	KronProd::kronMult(cols[0], ConstVector(*last), getData());
	delete last;
}

@ Here we construct $v\otimes\ldots\otimes v$, where the number of $v$
copies is |d|.

@<|URSingleTensor| constructor 2 code@>=
URSingleTensor::URSingleTensor(const ConstVector& v, int d)
	: URTensor(1, v.length(), d)
{
	if (d == 1) {
		getData() = v;
		return;
	}

	Vector* last = new Vector(v);
	for (int i = d-2; i > 0; i--) {
		Vector* newlast = new Vector(last->length()*v.length());
		KronProd::kronMult(v, ConstVector(*last), *newlast);
		delete last;
		last = newlast;
	}
	KronProd::kronMult(v, ConstVector(*last), getData());
	delete last;
}

@ Here we construct |FRSingleTensor| from |URSingleTensor| and return
its reference.

@<|URSingleTensor::fold| code@>=
FTensor& URSingleTensor::fold() const
{
	return *(new FRSingleTensor(*this));
}



@ The conversion from unfolded |URSingleTensor| to folded
|FRSingleTensor| is completely the same as conversion from |URTensor|
to |FRTensor|, only we do not copy rows but elements.
 
@<|FRSingleTensor| conversion from unfolded@>=
FRSingleTensor::FRSingleTensor(const URSingleTensor& ut)
	: FRTensor(1, ut.nvar(), ut.dimen())
{
	zeros();
	for (index in = ut.begin(); in != ut.end(); ++in) {
		IntSequence vtmp(in.getCoor());
		vtmp.sort();
		index tar(this, vtmp);
		get(*tar, 0) += ut.get(*in, 0);
	}
}


@ End of {\tt rfs\_tensor.cpp} file.