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