PEXSI
 All Classes Namespaces Files Functions Variables Friends Pages
numvec_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 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 _NUMVEC_IMPL_HPP_
47 #define _NUMVEC_IMPL_HPP_
48 
49 #include "numvec_decl.hpp"
50 
51 namespace PEXSI{
52 
53 // Templated form of numerical vectors
54 //
55 // The main advantage of this portable NumVec structure is that it can
56 // either own (owndata == true) or view (owndata == false) a piece of
57 // data.
58 
59 
60 
61 
62 template <class F> NumVec<F>::NumVec ( Int m ) : m_(m), owndata_(true)
63 {
64 #ifndef _RELEASE_
65  PushCallStack("NumVec<F>::NumVec");
66 #endif // ifndef _RELEASE_
67 
68  this->allocate();
69 
70 #ifndef _RELEASE_
71  PopCallStack();
72 #endif // ifndef _RELEASE_
73 } // ----- end of method NumVec<F>::NumVec -----
74 
75 template <class F> NumVec<F>::NumVec ( Int m, bool owndata, F* data ) : m_(m), owndata_(owndata)
76 {
77 #ifndef _RELEASE_
78  PushCallStack("NumVec<F>::NumVec");
79 #endif // ifndef _RELEASE_
80 
81  this->allocate(data);
82 
83 #ifndef _RELEASE_
84  PopCallStack();
85 #endif // ifndef _RELEASE_
86 } // ----- end of method NumVec<F>::NumVec -----
87 
88 template <class F> NumVec<F>::NumVec ( const NumVec<F>& C ) : m_(C.m_), owndata_(C.owndata_)
89 {
90 #ifndef _RELEASE_
91  PushCallStack("NumVec<F>::NumVec");
92 #endif // ifndef _RELEASE_
93  this->allocate(C.data_);
94 #ifndef _RELEASE_
95  PopCallStack();
96 #endif // ifndef _RELEASE_
97 } // ----- end of method NumVec<F>::NumVec -----
98 
99 
100 template < class F > NumVec<F>::~NumVec ( )
101 {
102 #ifndef _RELEASE_
103  PushCallStack("NumVec<F>::~NumVec");
104 #endif // ifndef _RELEASE_
105  this->deallocate();
106 #ifndef _RELEASE_
107  PopCallStack();
108 #endif // ifndef _RELEASE_
109 
110 } // ----- end of method NumVec<F>::~NumVec -----
111 
112 
113 template < class F > inline NumVec<F>& NumVec<F>::operator = ( const NumVec& C )
114 {
115 #ifndef _RELEASE_
116  PushCallStack("NumVec<F>::operator=");
117 #endif // ifndef _RELEASE_
118  this->deallocate();
119  m_ = C.m_;
120  owndata_ = C.owndata_;
121  this->allocate(C.data_);
122 #ifndef _RELEASE_
123  PopCallStack();
124 #endif // ifndef _RELEASE_
125 
126  return *this;
127 } // ----- end of method NumVec<F>::operator= -----
128 
129 
130 template < class F > inline void NumVec<F>::Resize ( const Int m )
131 {
132 #ifndef _RELEASE_
133  PushCallStack("NumVec<F>::Resize");
134 #endif // ifndef _RELEASE_
135  if( owndata_ == false ){
136  throw std::logic_error("Vector being resized must own data.");
137  }
138  if(m > bufsize_) {
139  this->deallocate();
140  m_ = m;
141  this->allocate();
142  }
143  else{
144  m_ = m;
145  }
146 #ifndef _RELEASE_
147  PopCallStack();
148 #endif // ifndef _RELEASE_
149  return ;
150 } // ----- end of method NumVec<F>::Resize -----
151 
152 
153 template <class F> inline F& NumVec<F>::operator() ( Int i )
154 {
155 #ifndef _RELEASE_
156  PushCallStack("NumVec<F>::operator()");
157  if( i < 0 || i >= m_ ){
158  throw std::logic_error( "Index is out of bound." );
159  }
160  PopCallStack();
161 #endif // ifndef _RELEASE_
162  return data_[i];
163 
164 } // ----- end of method NumVec<F>::operator() -----
165 
166 
167 template <class F>
168 inline const F&
169 NumVec<F>::operator() ( Int i ) const
170 {
171 #ifndef _RELEASE_
172  PushCallStack("NumVec<F>::operator()");
173  if( i < 0 || i >= m_ ){
174  throw std::logic_error( "Index is out of bound." );
175  }
176  PopCallStack();
177 #endif // ifndef _RELEASE_
178  return data_[i];
179 
180 } // ----- end of method NumVec<F>::operator() -----
181 
182 
183 template <class F> inline F& NumVec<F>::operator[] ( Int i )
184 {
185 #ifndef _RELEASE_
186  PushCallStack("NumVec<F>::operator[]");
187  if( i < 0 || i >= m_ ){
188  throw std::logic_error( "Index is out of bound." );
189  }
190  PopCallStack();
191 #endif // ifndef _RELEASE_
192  return data_[i];
193 } // ----- end of method NumVec<F>::operator[] -----
194 
195 
196 template <class F> inline const F& NumVec<F>::operator[] ( Int i ) const
197 {
198 #ifndef _RELEASE_
199  PushCallStack("NumVec<F>::operator[]");
200  if( i < 0 || i >= m_ ){
201  throw std::logic_error( "Index is out of bound." );
202  }
203  PopCallStack();
204 #endif // ifndef _RELEASE_
205  return data_[i];
206 
207 } // ----- end of method NumVec<F>::operator[] -----
208 
209 template <class F> inline void NumVec<F>::allocate(F* data) {
210  if(owndata_) {
211  if(m_>0) { data_ = new F[m_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
212  if(data!=NULL){std::copy(data,data+m_,data_);}
213  } else {
214  data_ = data;
215  }
216  bufsize_ = m_;
217 } // ----- end of method NumVec<F>::allocate -----
218 
219 template <class F> inline void NumVec<F>::deallocate() {
220  if(owndata_) {
221  if(bufsize_>0) { delete[] data_; data_ = NULL; }
222  }
223 } // ----- end of method NumVec<F>::deallocate -----
224 
225 
226 
227 
228 template <class F> inline void SetValue( NumVec<F>& vec, F val )
229 {
230  std::fill(vec.Data(),vec.Data()+vec.m(),val);
231 }
232 
233 template <class F> inline Real Energy( const NumVec<F>& vec )
234 {
235  Real sum = 0;
236  for(Int i=0; i<vec.m(); i++){
237  sum += std::abs(vec(i)*vec(i));
238  }
239  return sum;
240 }
241 
242 
243 } // namespace PEXSI
244 
245 #endif // _NUMVEC_IMPL_HPP_
Numerical vector.
Definition: numvec_decl.hpp:61
Real Energy(const NumMat< F > &M)
Energy computes the L2 norm of a matrix (treated as a vector).
Definition: nummat_impl.hpp:70
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: nummat_impl.hpp:61
Numerical vector.