File: sparse_tensor.cpp

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 (301 lines) | stat: -rw-r--r-- 6,108 bytes parent folder | download | duplicates (4)
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
/*1:*/
#line 6 "./sparse_tensor.cweb"

#include "sparse_tensor.h"
#include "fs_tensor.h"
#include "tl_exception.h"

#include <cmath> 

/*2:*/
#line 30 "./sparse_tensor.cweb"

void SparseTensor::insert(const IntSequence&key,int r,double c)
{
TL_RAISE_IF(r<0||r>=nr,
"Row number out of dimension of tensor in SparseTensor::insert");
TL_RAISE_IF(key.size()!=dimen(),
"Wrong length of key in SparseTensor::insert");
TL_RAISE_IF(!std::isfinite(c),
"Insertion of non-finite value in SparseTensor::insert");

iterator first_pos= m.lower_bound(key);
/*3:*/
#line 50 "./sparse_tensor.cweb"

iterator last_pos= m.upper_bound(key);
for(iterator it= first_pos;it!=last_pos;++it)
if((*it).second.first==r){
TL_RAISE("Duplicate <key, r> insertion in SparseTensor::insert");
return;
}

/*:3*/
#line 41 "./sparse_tensor.cweb"
;
m.insert(first_pos,Map::value_type(key,Item(r,c)));
if(first_nz_row> r)
first_nz_row= r;
if(last_nz_row<r)
last_nz_row= r;
}

/*:2*/
#line 13 "./sparse_tensor.cweb"
;
/*4:*/
#line 59 "./sparse_tensor.cweb"

bool SparseTensor::isFinite()const
{
bool res= true;
const_iterator run= m.begin();
while(res&&run!=m.end()){
if(!std::isfinite((*run).second.second))
res= false;
++run;
}
return res;
}

/*:4*/
#line 14 "./sparse_tensor.cweb"
;
/*5:*/
#line 75 "./sparse_tensor.cweb"

double SparseTensor::getFoldIndexFillFactor()const
{
int cnt= 0;
const_iterator start_col= m.begin();
while(start_col!=m.end()){
cnt++;
const IntSequence&key= (*start_col).first;
start_col= m.upper_bound(key);
}

return((double)cnt)/ncols();
}

/*:5*/
#line 15 "./sparse_tensor.cweb"
;
/*6:*/
#line 92 "./sparse_tensor.cweb"

double SparseTensor::getUnfoldIndexFillFactor()const
{
int cnt= 0;
const_iterator start_col= m.begin();
while(start_col!=m.end()){
const IntSequence&key= (*start_col).first;
Symmetry s(key);
cnt+= Tensor::noverseq(s);
start_col= m.upper_bound(key);
}

return((double)cnt)/ncols();
}



/*:6*/
#line 16 "./sparse_tensor.cweb"
;
/*7:*/
#line 110 "./sparse_tensor.cweb"

void SparseTensor::print()const
{
printf("Fill: %3.2f %%\n",100*getFillFactor());
const_iterator start_col= m.begin();
while(start_col!=m.end()){
const IntSequence&key= (*start_col).first;
printf("Column: ");key.print();
const_iterator end_col= m.upper_bound(key);
int cnt= 1;
for(const_iterator run= start_col;run!=end_col;++run,cnt++){
if((cnt/7)*7==cnt)
printf("\n");
printf("%d(%6.2g)  ",(*run).second.first,(*run).second.second);
}
printf("\n");
start_col= end_col;
}
}



/*:7*/
#line 17 "./sparse_tensor.cweb"
;
/*8:*/
#line 133 "./sparse_tensor.cweb"

FSSparseTensor::FSSparseTensor(int d,int nvar,int r)
:SparseTensor(d,r,FFSTensor::calcMaxOffset(nvar,d)),
nv(nvar),sym(d)
{}

/*:8*/
#line 18 "./sparse_tensor.cweb"
;
/*9:*/
#line 140 "./sparse_tensor.cweb"

FSSparseTensor::FSSparseTensor(const FSSparseTensor&t)
:SparseTensor(t),
nv(t.nvar()),sym(t.sym)
{}

/*:9*/
#line 19 "./sparse_tensor.cweb"
;
/*10:*/
#line 147 "./sparse_tensor.cweb"

void FSSparseTensor::insert(const IntSequence&key,int r,double c)
{
TL_RAISE_IF(!key.isSorted(),
"Key is not sorted in FSSparseTensor::insert");
TL_RAISE_IF(key[key.size()-1]>=nv||key[0]<0,
"Wrong value of the key in FSSparseTensor::insert");
SparseTensor::insert(key,r,c);
}

/*:10*/
#line 20 "./sparse_tensor.cweb"
;
/*11:*/
#line 171 "./sparse_tensor.cweb"

void FSSparseTensor::multColumnAndAdd(const Tensor&t,Vector&v)const
{
/*12:*/
#line 195 "./sparse_tensor.cweb"

TL_RAISE_IF(v.length()!=nrows(),
"Wrong size of output vector in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.dimen()!=dimen(),
"Wrong dimension of tensor in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.ncols()!=1,
"The input tensor is not single-column in FSSparseTensor::multColumnAndAdd");


/*:12*/
#line 174 "./sparse_tensor.cweb"
;
for(Tensor::index it= t.begin();it!=t.end();++it){
int ind= *it;
double a= t.get(ind,0);
if(a!=0.0){
IntSequence key(it.getCoor());
key.sort();
/*13:*/
#line 205 "./sparse_tensor.cweb"

TL_RAISE_IF(key[0]<0||key[key.size()-1]>=nv,
"Wrong coordinates of index in FSSparseTensor::multColumnAndAdd");

/*:13*/
#line 181 "./sparse_tensor.cweb"
;
const_iterator first_pos= m.lower_bound(key);
const_iterator last_pos= m.upper_bound(key);
for(const_iterator cit= first_pos;cit!=last_pos;++cit){
int r= (*cit).second.first;
double c= (*cit).second.second;
v[r]+= c*a;
}
}
}
}


/*:11*/
#line 21 "./sparse_tensor.cweb"
;
/*14:*/
#line 210 "./sparse_tensor.cweb"

void FSSparseTensor::print()const
{
printf("FS Sparse tensor: dim=%d, nv=%d, (%dx%d)\n",dim,nv,nr,nc);
SparseTensor::print();
}

/*:14*/
#line 22 "./sparse_tensor.cweb"
;
/*15:*/
#line 218 "./sparse_tensor.cweb"

GSSparseTensor::GSSparseTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td)
:SparseTensor(td.dimen(),t.nrows(),td.calcFoldMaxOffset()),
tdims(td)
{
/*16:*/
#line 241 "./sparse_tensor.cweb"

IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];

IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}


/*:16*/
#line 224 "./sparse_tensor.cweb"
;

FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb.lessEq((*run).first)&&(*run).first.lessEq(ub)){
IntSequence c((*run).first);
c.add(-1,lb);
insert(c,(*run).second.first,(*run).second.second);
}
}

}

/*:15*/
#line 23 "./sparse_tensor.cweb"
;
/*17:*/
#line 255 "./sparse_tensor.cweb"

void GSSparseTensor::insert(const IntSequence&s,int r,double c)
{
TL_RAISE_IF(!s.less(tdims.getNVX()),
"Wrong coordinates of index in GSSparseTensor::insert");
SparseTensor::insert(s,r,c);
}

/*:17*/
#line 24 "./sparse_tensor.cweb"
;
/*18:*/
#line 264 "./sparse_tensor.cweb"

void GSSparseTensor::print()const
{
printf("GS Sparse tensor: (%dx%d)\nSymmetry: ",nr,nc);
tdims.getSym().print();
printf("NVS: ");
tdims.getNVS().print();
SparseTensor::print();
}

/*:18*/
#line 25 "./sparse_tensor.cweb"
;

/*:1*/