4
const Squeal Tensor::_outOfRange = Squeal(Squeal::recoverable, "Tensor index out of range", "Tensor::Tensor");
5
const Squeal Tensor::_outOfRank = Squeal(Squeal::recoverable, "Tensor rank out of range", "Tensor::Tensor");
6
const Squeal Tensor::_lowRank = Squeal(Squeal::recoverable, "Do not use Tensor for rank <= 3", "Tensor::Tensor");
9
Tensor::Tensor(std::vector<int> size, double val) : _data(NULL), _size(size)
14
Tensor::Tensor(int size[], int rank, double val) : _data(NULL)
16
std::vector<int> sizeV(size, size+rank);
17
SetTensor(sizeV, val);
20
Tensor::Tensor(int size, int rank, double val) : _data(NULL)
22
std::vector<int> sizeV(rank, size);
23
SetTensor(sizeV, val);
28
for(int i=0; i<_size[0]; i++)
29
if(_data[i]) delete _data[i];
30
if(_data && _size[0]>0) delete [] _data;
33
void Tensor::SetTensor(std::vector<int> size, double val)
36
if(size.size() < 4) throw(_lowRank);
37
else if(size.size() == 4)
39
_data = new Tensor*[size[0]];
40
for(int i=0; i<size[0]; i++)
41
_data[i] = new Tensor3(size[1], size[2], size[3], val);
45
_data = new Tensor*[size[0]];
46
std::vector<int> newSize;
47
for(unsigned int i=1; i<size.size(); i++)
48
newSize.push_back(size[i]);
49
for(int i=0; i<size[0]; i++)
50
_data[i] = new Tensor(newSize, val);
57
void Tensor::SetAllFields(double value)
59
for(int i=0; i<_size[0]; i++)
60
_data[i]->SetAllFields(value);
64
void Tensor::Print(std::ostream& out) const
66
for(unsigned int i=0; i<_size.size(); i++)
68
for(int i=0; i<_size[0]; i++)
72
std::ostream& operator<<(std::ostream& out, const Tensor& t1)
78
bool Tensor::IsZero() const
80
for(int i=0; i<_size[0]; i++)
81
if(!_data[i]->IsZero()) return false;
85
Tensor operator/(const Tensor& t1, const double& d1)
88
for(int i=0; i<t1._size[0]; i++)
89
(*tOut._data[i]) = (*tOut._data[i])/d1;
93
Tensor Tensor::Poisson(Tensor t1) const
95
//:T_i::T_j: is of order i+j-3
96
int rankOut = GetRank() + t1.GetRank() - 3;
97
Tensor tOut(6, rankOut, 0);
98
//:T_i::T_j: = dT_i/du_p d/dConj{u_p} [dT_j/du_q d/dConj{u_q}]
99
//so tOut[i1][i2]...[ik] = dGdUi(j, v)*Delta(i, GetConjugate(j))*pow(-1, j+1);
101
//THIS IS THE BIT I AM WORKING ON - TRYING TO FIGURE IT OUT!
107
std::vector<Tensor*> Tensor::Exponential(int maxOrder) const
109
//:T_i:^n is of order n*(i-2)+1
110
//maxPower can be non-integer
111
double maxPower = (maxOrder-1)/(double)(GetRank()-2);
113
std::vector<Tensor*> out; // sum(:t:^n)
114
out.push_back(this->Clone());
115
for(int n=2; (double)n<=maxPower; n++)
118
Tensor* current = out.back();
119
out.push_back(new Tensor(current->Poisson(*this)/nFact));
124
CLHEP::HepVector Tensor::ExponentialPoisson(CLHEP::HepVector v, int maxOrder) const
126
std::vector<Tensor*> ExpT = Exponential(maxOrder);
127
CLHEP::HepVector out;
128
for(unsigned int i=0; i<ExpT.size(); i++)
129
out += ExpT[i]->Poisson(v);
133
CLHEP::HepVector Tensor::Poisson(CLHEP::HepVector v) const;
135
int dimension = v.num_row();
136
CLHEP::HepVector vOut(dimension);
137
std::vector<int> index(_size.size());
142
CLHEP::HepVector Poisson(CLHEP::HepVector v) const;
143
CLHEP::HepMatrix Poisson(CLHEP::HepMatrix m) const;
144
Tensor Poisson(Tensor t) const;
147
//Turn int index into an index for element of symmetric tensor
148
std::vector<int> Tensor::IndexToIndices(int index, int tensorSize, int startDimension)
151
std::vector<int> indices(1,-1);
152
std::vector<int> subIndices(1,0);
153
int hex = tensorSize ;
154
for(int i=0; i<index; i++)
156
// for(unsigned int i=0; i<indices.size(); i++) std::cout << indices[i] << " ";
157
if (indices.front() == hex-1) { indices = std::vector<int>(indices.size()+1, 0); indices.back()--;}
158
if (indices.back() == hex-1)
160
int j=indices.size()-1;
161
while(indices[j] == hex-1) j--;
162
for(int k=indices.size()-1; k>=j; k--) indices[k] = indices[j]+1;
164
else indices.back()++;
166
// std::cout << std::endl;
167
for(unsigned int i=0; i<indices.size(); i++) indices[i] += startDimension;
171
std::vector<int> indices(0);
172
//convert from decimal to heximal
173
int hex = tensorSize - 1;
174
for(int i=0; i<=index; i++)
176
int j = indices.size()-1;
177
if(j >= 0) while(indices[j] == hex && j>=0) j--;
178
if(j >= 0) indices[j]++;
179
else indices = std::vector<int>(indices.size()+1);
185
double Tensor::Get(std::vector<int> position) const
187
if(int(position.size()) != GetRank()) throw(Squeal(Squeal::recoverable, "Get rank different to Tensor rank", "Tensor::Get"));
188
const Tensor* val = this;
191
for(unsigned int i=position.size()-1; i>3; i--)
192
val = val->_data[position[i]];
193
val = val->_data[position[3]];
195
return ( *((Tensor3*)val) )[position[0]][position[1]][position[2]];
198
void Tensor::Set (std::vector<int> position, double value)
203
for(unsigned int i=position.size()-1; i>3; i--)
204
val = val->_data[position[i]];
205
val = val->_data[position[3]];
207
( *((Tensor3*)val) )[position[0]][position[1]][position[2]] = value;