File: tensor.h

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 (162 lines) | stat: -rw-r--r-- 3,712 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
/*1:*/
#line 47 "./tensor.hweb"


#ifndef TENSOR_H
#define TENSOR_H

#include "int_sequence.h"
#include "twod_matrix.h"

/*2:*/
#line 89 "./tensor.hweb"

template<class _Tptr> class _index{
typedef _index<_Tptr> _Self;
_Tptr tensor;
int offset;
IntSequence coor;
public:
_index(_Tptr t,int n)
:tensor(t),offset(0),coor(n,0){}
_index(_Tptr t,const IntSequence&cr,int c)
:tensor(t),offset(c),coor(cr){}
_index(_Tptr t,const IntSequence&cr)
:tensor(t),offset(tensor->getOffset(cr)),coor(cr){}
_index(const _index&ind)
:tensor(ind.tensor),offset(ind.offset),coor(ind.coor){}
const _Self&operator= (const _Self&in)
{tensor= in.tensor;offset= in.offset;coor= in.coor;
return*this;}
_Self&operator++()
{tensor->increment(coor);offset++;return*this;}
_Self&operator--()
{tensor->decrement(coor);offset--;return*this;}
int operator*()const
{return offset;}
bool operator==(const _index&n)const
{return offset==n.offset;}
bool operator!=(const _index&n)const
{return offset!=n.offset;}
const IntSequence&getCoor()const
{return coor;}
void print()const
{printf("%4d: ",offset);coor.print();}
};

/*:2*/
#line 55 "./tensor.hweb"
;
/*3:*/
#line 143 "./tensor.hweb"

class Tensor:public TwoDMatrix{
public:
enum indor{along_row,along_col};
typedef _index<const Tensor*> index;
protected:
const index in_beg;
const index in_end;
int dim;
public:
Tensor(indor io,const IntSequence&last,int r,int c,int d)
:TwoDMatrix(r,c),
in_beg(this,d),
in_end(this,last,(io==along_row)?r:c),
dim(d){}
Tensor(indor io,const IntSequence&first,const IntSequence&last,
int r,int c,int d)
:TwoDMatrix(r,c),
in_beg(this,first,0),
in_end(this,last,(io==along_row)?r:c),
dim(d){}
Tensor(int first_row,int num,Tensor&t)
:TwoDMatrix(first_row,num,t),
in_beg(t.in_beg),
in_end(t.in_end),
dim(t.dim){}
Tensor(const Tensor&t)
:TwoDMatrix(t),
in_beg(this,t.in_beg.getCoor(),*(t.in_beg)),
in_end(this,t.in_end.getCoor(),*(t.in_end)),
dim(t.dim){}
virtual~Tensor(){}
virtual void increment(IntSequence&v)const= 0;
virtual void decrement(IntSequence&v)const= 0;
virtual int getOffset(const IntSequence&v)const= 0;
int dimen()const
{return dim;}

const index&begin()const
{return in_beg;}
const index&end()const
{return in_end;}

static int noverk(int n,int k);
static int power(int a,int b);
static int noverseq(const IntSequence&s)
{
IntSequence seq(s);
return noverseq_ip((IntSequence&)s);
}
private:
static int noverseq_ip(IntSequence&s);
};

/*:3*/
#line 56 "./tensor.hweb"
;
/*4:*/
#line 203 "./tensor.hweb"

class FTensor;
class UTensor:public Tensor{
public:
UTensor(indor io,const IntSequence&last,int r,int c,int d)
:Tensor(io,last,r,c,d){}
UTensor(const UTensor&ut)
:Tensor(ut){}
UTensor(int first_row,int num,UTensor&t)
:Tensor(first_row,num,t){}
virtual~UTensor(){}
virtual FTensor&fold()const= 0;

static void increment(IntSequence&v,int nv);
static void decrement(IntSequence&v,int nv);
static void increment(IntSequence&v,const IntSequence&nvmx);
static void decrement(IntSequence&v,const IntSequence&nvmx);
static int getOffset(const IntSequence&v,int nv);
static int getOffset(const IntSequence&v,const IntSequence&nvmx);
};

/*:4*/
#line 57 "./tensor.hweb"
;
/*5:*/
#line 233 "./tensor.hweb"

class FTensor:public Tensor{
public:
FTensor(indor io,const IntSequence&last,int r,int c,int d)
:Tensor(io,last,r,c,d){}
FTensor(const FTensor&ft)
:Tensor(ft){}
FTensor(int first_row,int num,FTensor&t)
:Tensor(first_row,num,t){}
virtual~FTensor(){}
virtual UTensor&unfold()const= 0;

static void decrement(IntSequence&v,int nv);
static int getOffset(const IntSequence&v,int nv)
{IntSequence vtmp(v);return getOffsetRecurse(vtmp,nv);}
private:
static int getOffsetRecurse(IntSequence&v,int nv);
};

/*:5*/
#line 58 "./tensor.hweb"
;

#endif

/*:1*/