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 () : m_(0), owndata_(true), data_(NULL), bufsize_(0)
59 {
60  m_ = 0;
61 } // ----- end of method NumVec<F>::NumVec -----
62 
63 
64 template <class F> NumVec<F>::NumVec ( Int m ) : m_(m), owndata_(true), data_(NULL), bufsize_(0)
65 {
66 
67  this->allocate();
68 
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), data_(NULL), bufsize_(0)
72 {
73 
74  this->allocate(data);
75 
76 } // ----- end of method NumVec<F>::NumVec -----
77 
78 template <class F> NumVec<F>::NumVec ( const NumVec<F>& C ) : m_(C.m_), owndata_(C.owndata_), data_(NULL), bufsize_(0)
79 {
80  this->allocate(C.data_);
81 } // ----- end of method NumVec<F>::NumVec -----
82 
83 
84 template < class F > NumVec<F>::~NumVec ( )
85 {
86  this->deallocate();
87 
88 } // ----- end of method NumVec<F>::~NumVec -----
89 
90 
91 template < class F > inline NumVec<F>& NumVec<F>::operator = ( const NumVec& C )
92 {
93  this->deallocate();
94  m_ = C.m_;
95  owndata_ = C.owndata_;
96  this->allocate(C.data_);
97 
98  return *this;
99 } // ----- end of method NumVec<F>::operator= -----
100 
101 
102 template < class F > inline void NumVec<F>::Resize ( const Int m )
103 {
104  if( owndata_ == false ){
105  ErrorHandling("Vector being resized must own data.");
106  }
107  if(m > bufsize_) {
108  this->deallocate();
109  m_ = m;
110  this->allocate();
111  }
112  else{
113  m_ = m;
114  }
115  return ;
116 } // ----- end of method NumVec<F>::Resize -----
117 
118 template < class F > inline void NumVec<F>::Clear ( )
119 {
120  if( owndata_ == false ){
121  data_ = NULL;
122  }
123  else{
124  this->deallocate();
125  }
126  return ;
127 } // ----- end of method NumVec<F>::Clear -----
128 
129 
130 
131 template <class F> inline F& NumVec<F>::operator() ( Int i )
132 {
133  if( i < 0 || i >= m_ ){
134  ErrorHandling( "Index is out of bound." );
135  }
136  return data_[i];
137 
138 } // ----- end of method NumVec<F>::operator() -----
139 
140 
141 template <class F>
142 inline const F&
143 NumVec<F>::operator() ( Int i ) const
144 {
145  if( i < 0 || i >= m_ ){
146  ErrorHandling( "Index is out of bound." );
147  }
148  return data_[i];
149 
150 } // ----- end of method NumVec<F>::operator() -----
151 
152 
153 template <class F> inline F& NumVec<F>::operator[] ( Int i )
154 {
155  if( i < 0 || i >= m_ ){
156  ErrorHandling( "Index is out of bound." );
157  }
158  return data_[i];
159 } // ----- end of method NumVec<F>::operator[] -----
160 
161 
162 template <class F> inline const F& NumVec<F>::operator[] ( Int i ) const
163 {
164  if( i < 0 || i >= m_ ){
165  ErrorHandling( "Index is out of bound." );
166  }
167  return data_[i];
168 
169 } // ----- end of method NumVec<F>::operator[] -----
170 
171 template <class F> inline void NumVec<F>::allocate(F* data) {
172  if(owndata_) {
173  if(m_>0) {
174  data_ = new F[m_];
175  if( data_ == NULL ) {
176  ErrorHandling("Cannot allocate memory.");
177  }
178  }
179  else{
180  data_=NULL;
181  }
182  if(data!=NULL){
183  std::copy(data,data+m_,data_);
184  }
185  }
186  else {
187  data_ = data;
188  }
189  bufsize_ = m_;
190 } // ----- end of method NumVec<F>::allocate -----
191 
192 template <class F> inline void NumVec<F>::deallocate() {
193  if(owndata_) {
194  if(bufsize_>0) { delete[] data_; data_ = NULL; bufsize_=0; m_=0; }
195  }
196 } // ----- end of method NumVec<F>::deallocate -----
197 
198 
199 
200 
201 template <class F> inline void SetValue( NumVec<F>& vec, F val )
202 {
203  std::fill(vec.Data(),vec.Data()+vec.m(),val);
204 }
205 
206 template <class F> inline Real Energy( const NumVec<F>& vec )
207 {
208  Real sum = 0;
209  for(Int i=0; i<vec.m(); i++){
210  sum += std::abs(vec(i)*vec(i));
211  }
212  return sum;
213 }
214 
215 
216 } // namespace PEXSI
217 
218 #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:158
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: NumMat_impl.hpp:153