File: ps_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 (392 lines) | stat: -rw-r--r-- 8,333 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
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
/*1:*/
#line 5 "./ps_tensor.cweb"

#include "ps_tensor.h"
#include "fs_tensor.h"
#include "tl_exception.h"
#include "tl_static.h"
#include "stack_container.h"

/*2:*/
#line 38 "./ps_tensor.cweb"

UPSTensor::fill_method UPSTensor::decideFillMethod(const FSSparseTensor&t)
{
if(t.getFillFactor()> 0.08)
return first;
else
return second;
}

/*:2*/
#line 12 "./ps_tensor.cweb"
;
/*3:*/
#line 49 "./ps_tensor.cweb"

UPSTensor::UPSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const PerTensorDimens&ptd)
:UTensor(along_col,ptd.getNVX(),
t.nrows(),ptd.calcUnfoldMaxOffset(),ptd.dimen()),
tdims(ptd)
{
TL_RAISE_IF(coor.size()!=t.dimen(),
"Wrong coordinates length of stacks for UPSTensor slicing constructor");
TL_RAISE_IF(ss.sum()!=t.nvar(),
"Wrong length of stacks for UPSTensor slicing constructor");

if(first==decideFillMethod(t))
fillFromSparseOne(t,ss,coor);
else
fillFromSparseTwo(t,ss,coor);
}


/*:3*/
#line 13 "./ps_tensor.cweb"
;
/*4:*/
#line 69 "./ps_tensor.cweb"

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

UTensor::increment(v,tdims.getNVX());
}

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

UTensor::decrement(v,tdims.getNVX());
}

/*:4*/
#line 14 "./ps_tensor.cweb"
;
/*5:*/
#line 87 "./ps_tensor.cweb"

FTensor&UPSTensor::fold()const
{
TL_RAISE("Never should come to this place in UPSTensor::fold");
FFSTensor*nothing= new FFSTensor(0,0,0);
return*nothing;
}


/*:5*/
#line 15 "./ps_tensor.cweb"
;
/*6:*/
#line 97 "./ps_tensor.cweb"

int UPSTensor::getOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UPSTensor::getOffset");

return UTensor::getOffset(v,tdims.getNVX());
}

/*:6*/
#line 16 "./ps_tensor.cweb"
;
/*7:*/
#line 107 "./ps_tensor.cweb"

void UPSTensor::addTo(FGSTensor&out)const
{
TL_RAISE_IF(out.getDims()!=tdims,
"Tensors have incompatible dimens in UPSTensor::addTo");
for(index in= out.begin();in!=out.end();++in){
IntSequence vtmp(dimen());
tdims.getPer().apply(in.getCoor(),vtmp);
index tin(this,vtmp);
out.addColumn(*this,*tin,*in);
}
}

/*:7*/
#line 17 "./ps_tensor.cweb"
;
/*8:*/
#line 143 "./ps_tensor.cweb"

void UPSTensor::addTo(UGSTensor&out)const
{
TL_RAISE_IF(out.getDims()!=tdims,
"Tensors have incompatible dimens in UPSTensor::addTo");
int cols= tailIdentitySize();
int off= tdims.tailIdentity();
IntSequence outrun(out.dimen(),0);
IntSequence outrun_part(outrun,0,out.dimen()-off);
IntSequence nvmax_part(out.getDims().getNVX(),0,out.dimen()-off);
for(int out_col= 0;out_col<out.ncols();out_col+= cols){

IntSequence perrun(out.dimen());
tdims.getPer().apply(outrun,perrun);
index from(this,perrun);

ConstTwoDMatrix subfrom(*this,*from,cols);
TwoDMatrix subout(out,out_col,cols);

subout.add(1,subfrom);

UTensor::increment(outrun_part,nvmax_part);
}
}


/*:8*/
#line 18 "./ps_tensor.cweb"
;
/*9:*/
#line 172 "./ps_tensor.cweb"

int UPSTensor::tailIdentitySize()const
{
return tdims.getNVX().mult(dimen()-tdims.tailIdentity(),dimen());
}

/*:9*/
#line 19 "./ps_tensor.cweb"
;
/*10:*/
#line 184 "./ps_tensor.cweb"

void UPSTensor::fillFromSparseOne(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor)
{
IntSequence cumtmp(ss.size());
cumtmp[0]= 0;
for(int i= 1;i<ss.size();i++)
cumtmp[i]= cumtmp[i-1]+ss[i-1];
IntSequence cum(coor.size());
for(int i= 0;i<coor.size();i++)
cum[i]= cumtmp[coor[i]];

zeros();
for(Tensor::index run= begin();run!=end();++run){
IntSequence c(run.getCoor());
c.add(1,cum);
c.sort();
FSSparseTensor::const_iterator sl= t.getMap().lower_bound(c);
if(sl!=t.getMap().end()){
FSSparseTensor::const_iterator su= t.getMap().upper_bound(c);
for(FSSparseTensor::const_iterator srun= sl;srun!=su;++srun)
get((*srun).second.first,*run)= (*srun).second.second;
}
}
}

/*:10*/
#line 20 "./ps_tensor.cweb"
;
/*11:*/
#line 225 "./ps_tensor.cweb"

void UPSTensor::fillFromSparseTwo(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor)
{
IntSequence coor_srt(coor);
coor_srt.sort();
IntSequence cum(ss.size());
cum[0]= 0;
for(int i= 1;i<ss.size();i++)
cum[i]= cum[i-1]+ss[i-1];
IntSequence lb_srt(coor.size());
IntSequence ub_srt(coor.size());
for(int i= 0;i<coor.size();i++){
lb_srt[i]= cum[coor_srt[i]];
ub_srt[i]= cum[coor_srt[i]]+ss[coor_srt[i]]-1;
}

const PermutationSet&pset= tls.pbundle->get(coor.size());
vector<const Permutation*> pp= pset.getPreserving(coor);

Permutation unsort(coor);
zeros();
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb_srt);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub_srt);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb_srt.lessEq((*run).first)&&(*run).first.lessEq(ub_srt)){
IntSequence c((*run).first);
c.add(-1,lb_srt);
unsort.apply(c);
for(unsigned int i= 0;i<pp.size();i++){
IntSequence cp(coor.size());
pp[i]->apply(c,cp);
Tensor::index ind(this,cp);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of UPSTensor");
get((*run).second.first,*ind)= (*run).second.second;
}
}
}
}


/*:11*/
#line 21 "./ps_tensor.cweb"
;
/*12:*/
#line 270 "./ps_tensor.cweb"

void PerTensorDimens2::setDimensionSizes()
{
const IntSequence&nvs= getNVS();
for(int i= 0;i<numSyms();i++){
TensorDimens td(syms[i],nvs);
ds[i]= td.calcFoldMaxOffset();
}
}

/*:12*/
#line 22 "./ps_tensor.cweb"
;
/*13:*/
#line 287 "./ps_tensor.cweb"

int PerTensorDimens2::calcOffset(const IntSequence&coor)const
{
TL_RAISE_IF(coor.size()!=dimen(),
"Wrong length of coordinates in PerTensorDimens2::calcOffset");
IntSequence cc(coor);
int ret= 0;
int off= 0;
for(int i= 0;i<numSyms();i++){
TensorDimens td(syms[i],getNVS());
IntSequence c(cc,off,off+syms[i].dimen());
int a= td.calcFoldOffset(c);
ret= ret*ds[i]+a;
off+= syms[i].dimen();
}
return ret;
}

/*:13*/
#line 23 "./ps_tensor.cweb"
;
/*14:*/
#line 306 "./ps_tensor.cweb"

void PerTensorDimens2::print()const
{
printf("nvmax: ");nvmax.print();
printf("per:   ");per.print();
printf("syms:  ");syms.print();
printf("dims:  ");ds.print();
}

/*:14*/
#line 24 "./ps_tensor.cweb"
;
/*15:*/
#line 320 "./ps_tensor.cweb"

void FPSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong length of coordinates in FPSTensor::increment");
UTensor::increment(v,tdims.getNVX());
int off= 0;
for(int i= 0;i<tdims.numSyms();i++){
IntSequence c(v,off,off+tdims.getSym(i).dimen());
c.pmonotone(tdims.getSym(i));
off+= tdims.getSym(i).dimen();
}
}


/*:15*/
#line 25 "./ps_tensor.cweb"
;
/*16:*/
#line 336 "./ps_tensor.cweb"

void FPSTensor::decrement(IntSequence&v)const
{
TL_RAISE("FPSTensor::decrement not implemented");
}

/*:16*/
#line 26 "./ps_tensor.cweb"
;
/*17:*/
#line 343 "./ps_tensor.cweb"

UTensor&FPSTensor::unfold()const
{
TL_RAISE("Unfolding of FPSTensor not implemented");
UFSTensor*nothing= new UFSTensor(0,0,0);
return*nothing;
}

/*:17*/
#line 27 "./ps_tensor.cweb"
;
/*18:*/
#line 352 "./ps_tensor.cweb"

int FPSTensor::getOffset(const IntSequence&v)const
{
return tdims.calcOffset(v);
}

/*:18*/
#line 28 "./ps_tensor.cweb"
;
/*19:*/
#line 364 "./ps_tensor.cweb"

void FPSTensor::addTo(FGSTensor&out)const
{
for(index tar= out.begin();tar!=out.end();++tar){
IntSequence coor(dimen());
tdims.getPer().apply(tar.getCoor(),coor);
index src(this,coor);
out.addColumn(*this,*src,*tar);
}
}

/*:19*/
#line 29 "./ps_tensor.cweb"
;
/*20:*/
#line 395 "./ps_tensor.cweb"

FPSTensor::FPSTensor(const TensorDimens&td,const Equivalence&e,const Permutation&p,
const GSSparseTensor&a,const KronProdAll&kp)
:FTensor(along_col,PerTensorDimens(td,Permutation(e,p)).getNVX(),
a.nrows(),kp.ncols(),td.dimen()),
tdims(td,e,p)
{
zeros();

UGSTensor dummy(0,a.getDims());
for(Tensor::index run= dummy.begin();run!=dummy.end();++run){
Tensor::index fold_ind= dummy.getFirstIndexOf(run);
const IntSequence&c= fold_ind.getCoor();
GSSparseTensor::const_iterator sl= a.getMap().lower_bound(c);
if(sl!=a.getMap().end()){
Vector*row_prod= kp.multRows(run.getCoor());
GSSparseTensor::const_iterator su= a.getMap().upper_bound(c);
for(GSSparseTensor::const_iterator srun= sl;srun!=su;++srun){
Vector out_row((*srun).second.first,*this);
out_row.add((*srun).second.second,*row_prod);
}
delete row_prod;
}
}
}


/*:20*/
#line 30 "./ps_tensor.cweb"
;

/*:1*/