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  ErrorHandling("Cannot allocate memory.");}
55  } else data_=NULL;
56 }
57 
58 template <class F> NumTns<F>::NumTns(Int m, Int n, Int p, bool owndata, F* data): m_(m), n_(n), p_(p), owndata_(owndata) {
59  if(owndata_) {
60  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) {
61  ErrorHandling("Cannot allocate memory.");}
62  } else data_=NULL;
63  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = data[i]; }
64  } else {
65  data_ = data;
66  }
67 }
68 
69 template <class F> NumTns<F>::NumTns(const NumTns<F>& C): m_(C.m_), n_(C.n_), p_(C.p_), owndata_(C.owndata_) {
70  if(owndata_) {
71  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_];
72  if( data_ == NULL ) {
73  ErrorHandling("Cannot allocate memory.");}
74  } else data_=NULL;
75 
76  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
77  } else {
78  data_ = C.data_;
79  }
80 }
81 
82 template <class F> NumTns<F>::~NumTns() {
83  if(owndata_) {
84  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
85  }
86 }
87 
88 template <class F> NumTns<F>& NumTns<F>::operator=(const NumTns<F>& C) {
89  if(owndata_) {
90  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
91  }
92  m_ = C.m_; n_=C.n_; p_=C.p_; owndata_=C.owndata_;
93  if(owndata_) {
94  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) {
95  ErrorHandling("Cannot allocate memory.");} } else data_=NULL;
96  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
97  } else {
98  data_ = C.data_;
99  }
100  return *this;
101 }
102 
103 template <class F> void NumTns<F>::Resize(Int m, Int n, Int p) {
104  if( owndata_ == false ){
105  ErrorHandling("Tensor being resized must own data.");
106  }
107  if(m_!=m || n_!=n || p_!=p) {
108  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
109  m_ = m; n_ = n; p_=p;
110  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) {
111  ErrorHandling("Cannot allocate memory.");}
112  } else data_=NULL;
113  }
114 }
115 
116 template <class F> const F& NumTns<F>::operator()(Int i, Int j, Int k) const {
117  if( i < 0 || i >= m_ ||
118  j < 0 || j >= n_ ||
119  k < 0 || k >= p_ ) {
120  ErrorHandling( "Index is out of bound." );
121  }
122  return data_[i+j*m_+k*m_*n_];
123 }
124 
125 template <class F> F& NumTns<F>::operator()(Int i, Int j, Int k) {
126  if( i < 0 || i >= m_ ||
127  j < 0 || j >= n_ ||
128  k < 0 || k >= p_ ) {
129  ErrorHandling( "Index is out of bound." );
130  }
131  return data_[i+j*m_+k*m_*n_];
132 }
133 
134 
135 template <class F> F* NumTns<F>::MatData (Int j) const {
136  if( j < 0 || j >= p_ ) {
137  ErrorHandling( "Index is out of bound." );
138  }
139  return &(data_[j*m_*n_]);
140 };
141 
142 template <class F> F* NumTns<F>::VecData (Int j, Int k) const {
143  if( j < 0 || j >= n_ ||
144  k < 0 || k >= p_ ) {
145  ErrorHandling( "Index is out of bound." );
146  }
147 
148  return &(data_[k*m_*n_+j*m_]);
149 };
150 
151 template <class F> inline void SetValue(NumTns<F>& T, F val)
152 {
153  F *ptr = T.data_;
154  for(Int i=0; i < T.m() * T.n() * T.p(); i++) *(ptr++) = val;
155 
156  return;
157 }
158 
159 
160 
161 template <class F> inline Real Energy(const NumTns<F>& T)
162 {
163  Real sum = 0;
164 
165  F *ptr = T.Data();
166  for(Int i=0; i < T.m() * T.n() * T.p(); i++)
167  sum += abs(ptr[i]) * abs(ptr[i]);
168 
169  return sum;
170 }
171 
172 } // namespace PEXSI
173 
174 #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:158
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:153
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