46 #ifndef _PEXSI_NUMTNS_IMPL_HPP_
47 #define _PEXSI_NUMTNS_IMPL_HPP_
52 template <
class F> NumTns<F>::NumTns(Int m, Int n, Int p): m_(m), n_(n), p_(p), owndata_(true) {
57 throw std::runtime_error(
"Cannot allocate memory.");}
61 template <
class F> NumTns<F>::NumTns(Int m, Int n, Int p,
bool owndata, F* data): m_(m), n_(n), p_(p), owndata_(owndata) {
67 throw std::runtime_error(
"Cannot allocate memory.");}
75 template <
class F> NumTns<F>::NumTns(
const NumTns<F>& C): m_(C.m_), n_(C.n_), p_(C.p_), owndata_(C.owndata_) {
82 throw std::runtime_error(
"Cannot allocate memory.");}
91 template <
class F> NumTns<F>::~NumTns() {
93 if(m_>0 && n_>0 && p_>0) {
delete[] data_; data_ = NULL; }
97 template <
class F> NumTns<F>& NumTns<F>::operator=(
const NumTns<F>& C) {
99 if(m_>0 && n_>0 && p_>0) {
delete[] data_; data_ = NULL; }
101 m_ = C.m_; n_=C.n_; p_=C.p_; owndata_=C.owndata_;
103 if(m_>0 && n_>0 && p_>0) { data_ =
new F[m_*n_*p_];
if( data_ == NULL ) {
107 throw std::runtime_error(
"Cannot allocate memory.");} }
else data_=NULL;
108 if(m_>0 && n_>0 && p_>0) {
for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
115 template <
class F>
void NumTns<F>::Resize(Int m, Int n, Int p) {
116 if( owndata_ ==
false ){
120 throw std::logic_error(
"Tensor being resized must own data.");
122 if(m_!=m || n_!=n || p_!=p) {
123 if(m_>0 && n_>0 && p_>0) {
delete[] data_; data_ = NULL; }
124 m_ = m; n_ = n; p_=p;
125 if(m_>0 && n_>0 && p_>0) { data_ =
new F[m_*n_*p_];
if( data_ == NULL ) {
129 throw std::runtime_error(
"Cannot allocate memory.");}
134 template <
class F>
const F& NumTns<F>::operator()(Int i, Int j, Int k)
const {
135 if( i < 0 || i >= m_ ||
141 throw std::logic_error(
"Index is out of bound." );
143 return data_[i+j*m_+k*m_*n_];
146 template <
class F> F& NumTns<F>::operator()(Int i, Int j, Int k) {
147 if( i < 0 || i >= m_ ||
153 throw std::logic_error(
"Index is out of bound." );
155 return data_[i+j*m_+k*m_*n_];
159 template <
class F> F* NumTns<F>::MatData (Int j)
const {
160 if( j < 0 || j >= p_ ) {
164 throw std::logic_error(
"Index is out of bound." );
166 return &(data_[j*m_*n_]);
169 template <
class F> F* NumTns<F>::VecData (Int j, Int k)
const {
170 if( j < 0 || j >= n_ ||
175 throw std::logic_error(
"Index is out of bound." );
178 return &(data_[k*m_*n_+j*m_]);
184 for(Int i=0; i < T.m() * T.n() * T.p(); i++) *(ptr++) = val;
196 for(Int i=0; i < T.m() * T.n() * T.p(); i++)
197 sum += abs(ptr[i]) * abs(ptr[i]);
204 #endif // _PEXSI_NUMTNS_IMPL_HPP_
Int n_
The size of second dimension.
Definition: NumTns.hpp:70
Int m_
The size of the first dimension.
Definition: NumTns.hpp:67
Real Energy(const NumMat< F > &M)
Energy computes the L2 norm of a matrix (treated as a vector).
Definition: NumMat_impl.hpp:176
bool owndata_
Whether it owns the data.
Definition: NumTns.hpp:76
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: NumMat_impl.hpp:171
Numerical tensor.
Definition: NumTns.hpp:63
F * data_
The pointer for the actual data.
Definition: NumTns.hpp:79
Int p_
The size of third dimension.
Definition: NumTns.hpp:73