#include "Tensor.hh" #include "Tensor3.hh" /* const Exception Tensor::_outOfRange = MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable, "Tensor index out of range", "Tensor::Tensor"); const Exception Tensor::_outOfRank = MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable, "Tensor rank out of range", "Tensor::Tensor"); const Exception Tensor::_lowRank = MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable, "Do not use Tensor for rank <= 3", "Tensor::Tensor"); */ Tensor::Tensor(std::vector size, double val) : _data(NULL), _size(size) { SetTensor(size, val); } Tensor::Tensor(int size[], int rank, double val) : _data(NULL) { std::vector sizeV(size, size+rank); SetTensor(sizeV, val); } Tensor::Tensor(int size, int rank, double val) : _data(NULL) { std::vector sizeV(rank, size); SetTensor(sizeV, val); } Tensor::~Tensor() { for(int i=0; i<_size[0]; i++) if(_data[i]) delete _data[i]; if(_data && _size[0]>0) delete [] _data; } void Tensor::SetTensor(std::vector size, double val) { _size = size; if(size.size() < 4) throw(MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable, "Do not use Tensor for rank <= 3", "Tensor::Tensor")); else if(size.size() == 4) { _data = new Tensor*[size[0]]; for(int i=0; i newSize; for(unsigned int i=1; iSetAllFields(value); } void Tensor::Print(std::ostream& out) const { for(unsigned int i=0; i<_size.size(); i++) out << "\n"; for(int i=0; i<_size[0]; i++) out << (*_data[i]); } std::ostream& operator<<(std::ostream& out, const Tensor& t1) { t1.Print(out); return out; } bool Tensor::IsZero() const { for(int i=0; i<_size[0]; i++) if(!_data[i]->IsZero()) return false; return true; } Tensor operator/(const Tensor& t1, const double& d1) { Tensor tOut = t1; for(int i=0; i Tensor::Exponential(int maxOrder) const { //:T_i:^n is of order n*(i-2)+1 //maxPower can be non-integer double maxPower = (maxOrder-1)/(double)(GetRank()-2); int nFact = 1; //n! std::vector out; // sum(:t:^n) out.push_back(this->Clone()); for(int n=2; (double)n<=maxPower; n++) { nFact *= n; Tensor* current = out.back(); out.push_back(new Tensor(current->Poisson(*this)/nFact)); } return out; } CLHEP::HepVector Tensor::ExponentialPoisson(CLHEP::HepVector v, int maxOrder) const { std::vector ExpT = Exponential(maxOrder); CLHEP::HepVector out; for(unsigned int i=0; iPoisson(v); return out; } CLHEP::HepVector Tensor::Poisson(CLHEP::HepVector v) const; { int dimension = v.num_row(); CLHEP::HepVector vOut(dimension); std::vector index(_size.size()); return vOut; } */ /* CLHEP::HepVector Poisson(CLHEP::HepVector v) const; CLHEP::HepMatrix Poisson(CLHEP::HepMatrix m) const; Tensor Poisson(Tensor t) const; */ //Turn int index into an index for element of symmetric tensor std::vector Tensor::IndexToIndices(int index, int tensorSize, int startDimension) { index++; std::vector indices(1,-1); std::vector subIndices(1,0); int hex = tensorSize ; for(int i=0; i(indices.size()+1, 0); indices.back()--;} if (indices.back() == hex-1) { int j=indices.size()-1; while(indices[j] == hex-1) j--; for(int k=indices.size()-1; k>=j; k--) indices[k] = indices[j]+1; } else indices.back()++; } // std::cout << std::endl; for(unsigned int i=0; i indices(0); //convert from decimal to heximal int hex = tensorSize - 1; for(int i=0; i<=index; i++) { int j = indices.size()-1; if(j >= 0) while(indices[j] == hex && j>=0) j--; if(j >= 0) indices[j]++; else indices = std::vector(indices.size()+1); } return indices; } */ double Tensor::Get(std::vector position) const { if(int(position.size()) != GetRank()) throw(MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable, "Get rank different to Tensor rank", "Tensor::Get")); const Tensor* val = this; if(GetRank()>3) { for(unsigned int i=position.size()-1; i>3; i--) val = val->_data[position[i]]; val = val->_data[position[3]]; } return ( *((Tensor3*)val) )[position[0]][position[1]][position[2]]; } void Tensor::Set (std::vector position, double value) { Tensor* val = this; if(GetRank()>3) { for(unsigned int i=position.size()-1; i>3; i--) val = val->_data[position[i]]; val = val->_data[position[3]]; } ( *((Tensor3*)val) )[position[0]][position[1]][position[2]] = value; }