PEXSI
 All Classes Namespaces Files Functions Variables Typedefs Pages
NumTns_impl.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012 The Regents of the University of California,
3  through Lawrence Berkeley National Laboratory.
4 
5 Authors: Lexing Ying, Mathias Jacquelin and Lin Lin
6 
7 This file is part of PEXSI. All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11 
12 (1) Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14 (2) Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 (3) Neither the name of the University of California, Lawrence Berkeley
18 National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 You are under no obligation whatsoever to provide any bug fixes, patches, or
34 upgrades to the features, functionality or performance of the source code
35 ("Enhancements") to anyone; however, if you choose to make your Enhancements
36 available either publicly, or directly to Lawrence Berkeley National
37 Laboratory, without imposing a separate written license agreement for such
38 Enhancements, then you hereby grant the following license: a non-exclusive,
39 royalty-free perpetual license to install, use, modify, prepare derivative
40 works, incorporate into other computer software, distribute, and sublicense
41 such enhancements or derivative works thereof, in binary and source code form.
42  */
46 #ifndef _PEXSI_NUMTNS_IMPL_HPP_
47 #define _PEXSI_NUMTNS_IMPL_HPP_
48 
49 
50 namespace PEXSI{
51 
52  template <class F> NumTns<F>::NumTns(Int m, Int n, Int p): m_(m), n_(n), p_(p), owndata_(true) {
53  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) {
54 #ifdef USE_ABORT
55  abort();
56 #endif
57  throw std::runtime_error("Cannot allocate memory.");}
58  } else data_=NULL;
59  }
60 
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) {
62  if(owndata_) {
63  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) {
64 #ifdef USE_ABORT
65  abort();
66 #endif
67  throw std::runtime_error("Cannot allocate memory.");}
68  } else data_=NULL;
69  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = data[i]; }
70  } else {
71  data_ = data;
72  }
73  }
74 
75  template <class F> NumTns<F>::NumTns(const NumTns<F>& C): m_(C.m_), n_(C.n_), p_(C.p_), owndata_(C.owndata_) {
76  if(owndata_) {
77  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_];
78  if( data_ == NULL ) {
79 #ifdef USE_ABORT
80  abort();
81 #endif
82  throw std::runtime_error("Cannot allocate memory.");}
83  } else data_=NULL;
84 
85  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
86  } else {
87  data_ = C.data_;
88  }
89  }
90 
91  template <class F> NumTns<F>::~NumTns() {
92  if(owndata_) {
93  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
94  }
95  }
96 
97  template <class F> NumTns<F>& NumTns<F>::operator=(const NumTns<F>& C) {
98  if(owndata_) {
99  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
100  }
101  m_ = C.m_; n_=C.n_; p_=C.p_; owndata_=C.owndata_;
102  if(owndata_) {
103  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) {
104 #ifdef USE_ABORT
105  abort();
106 #endif
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]; }
109  } else {
110  data_ = C.data_;
111  }
112  return *this;
113  }
114 
115  template <class F> void NumTns<F>::Resize(Int m, Int n, Int p) {
116  if( owndata_ == false ){
117 #ifdef USE_ABORT
118  abort();
119 #endif
120  throw std::logic_error("Tensor being resized must own data.");
121  }
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 ) {
126 #ifdef USE_ABORT
127  abort();
128 #endif
129  throw std::runtime_error("Cannot allocate memory.");}
130  } else data_=NULL;
131  }
132  }
133 
134  template <class F> const F& NumTns<F>::operator()(Int i, Int j, Int k) const {
135  if( i < 0 || i >= m_ ||
136  j < 0 || j >= n_ ||
137  k < 0 || k >= p_ ) {
138 #ifdef USE_ABORT
139  abort();
140 #endif
141  throw std::logic_error( "Index is out of bound." );
142  }
143  return data_[i+j*m_+k*m_*n_];
144  }
145 
146  template <class F> F& NumTns<F>::operator()(Int i, Int j, Int k) {
147  if( i < 0 || i >= m_ ||
148  j < 0 || j >= n_ ||
149  k < 0 || k >= p_ ) {
150 #ifdef USE_ABORT
151  abort();
152 #endif
153  throw std::logic_error( "Index is out of bound." );
154  }
155  return data_[i+j*m_+k*m_*n_];
156  }
157 
158 
159  template <class F> F* NumTns<F>::MatData (Int j) const {
160  if( j < 0 || j >= p_ ) {
161 #ifdef USE_ABORT
162  abort();
163 #endif
164  throw std::logic_error( "Index is out of bound." );
165  }
166  return &(data_[j*m_*n_]);
167  };
168 
169  template <class F> F* NumTns<F>::VecData (Int j, Int k) const {
170  if( j < 0 || j >= n_ ||
171  k < 0 || k >= p_ ) {
172 #ifdef USE_ABORT
173  abort();
174 #endif
175  throw std::logic_error( "Index is out of bound." );
176  }
177 
178  return &(data_[k*m_*n_+j*m_]);
179  };
180 
181  template <class F> inline void SetValue(NumTns<F>& T, F val)
182  {
183  F *ptr = T.data_;
184  for(Int i=0; i < T.m() * T.n() * T.p(); i++) *(ptr++) = val;
185 
186  return;
187  }
188 
189 
190 
191  template <class F> inline Real Energy(const NumTns<F>& T)
192  {
193  Real sum = 0;
194 
195  F *ptr = T.Data();
196  for(Int i=0; i < T.m() * T.n() * T.p(); i++)
197  sum += abs(ptr[i]) * abs(ptr[i]);
198 
199  return sum;
200  }
201 
202 } // namespace PEXSI
203 
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