PEXSI
 All Classes Namespaces Files Functions Variables Friends Pages
nummat_decl.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 _NUMMAT_DECL_HPP_
47 #define _NUMMAT_DECL_HPP_
48 
49 #include "environment.hpp"
50 
51 // TODO Move the things from decl to impl
52 
53 //#ifdef _NUMMAT_VECTOR_
54 //#define bool char
55 //#endif
56 
57 
58 namespace PEXSI{
59 
60 
61 #ifdef _NUMMAT_VECTOR_
62  // template <typename F>
63  // struct NumMatType
64  // {
65  // using type = F;
66  // };
67  //
68  //
69  // template <>
70  // struct NumMatType<bool>
71  // {
72  // using type = char;
73  // };
74  //
75  //template<typename t, typename... p>
76  //using fixed_vector = std::vector<typename NumMatType<F>::type, p...>;
77 
78 
79  //template <class F>class NumMat;
80  //typedef NumMat<bool> NumMat<char>;
81 
82 #endif
83 
84 
92  template <class F>
93  class NumMat
94  {
95  public:
96 
97 
98  Int bufsize_;
99 
101  bool owndata_;
102 
104  Int m_;
105 
107  Int n_;
108 
109 #ifdef _NUMMAT_VECTOR_
110  std::vector<F> * container_;
112  F* data_;
113 #else
114 
115 
117  F* data_;
118 
119 
120 
121  inline void allocate(F* data=NULL) {
122  if(owndata_) {
123 #ifdef _NUMMAT_VECTOR_
124  container_ = new std::vector<F>(m_*n_);
125  if(data!=NULL){std::copy(data,data+m_*n_,container_->begin());}
126  data_=&(*container_)[0];
127 #else
128  if(m_>0 && n_>0) { data_ = new F[m_*n_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
129  if(data!=NULL){std::copy(data,data+m_*n_,data_);}
130 #endif
131  } else {
132  data_ = data;
133  }
134 #ifndef _NUMMAT_VECTOR_
135  bufsize_ = m_*n_;
136 #endif
137  }
138  inline void deallocate(){
139  if(owndata_) {
140 #ifdef _NUMMAT_VECTOR_
141  delete container_;
142 #else
143  if(bufsize_>0) { delete[] data_; data_ = NULL; }
144 #endif
145  }
146  }
147 
148  public:
149  NumMat(Int m=0, Int n=0): m_(m), n_(n), owndata_(true) {
150  this->allocate();
151  }
152 
153  NumMat(Int m, Int n, bool owndata, F* data): m_(m), n_(n), owndata_(owndata) {
154  this->allocate(data);
155  }
156 
157  NumMat(const NumMat& C): m_(C.m_), n_(C.n_), owndata_(C.owndata_) {
158  this->allocate(C.data_);
159  }
160  ~NumMat() {
161  this->deallocate();
162  }
163 
164  NumMat& Copy(const NumMat& C) {
165  this->deallocate();
166  m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
167  this->allocate(C.data_);
168  return *this;
169  }
170 
171  NumMat& operator=(const NumMat& C) {
172  this->deallocate();
173  m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
174  this->allocate(C.data_);
175  return *this;
176  }
177 
178 
179  void Resize(Int m, Int n) {
180  if( owndata_ == false ){
181  throw std::logic_error("Matrix being resized must own data.");
182  }
183 
184 
185 #ifdef _NUMMAT_VECTOR_
186  if(container_->size()<m*n)
187  {
188  container_->resize(m*n);
189  data_=&(*container_)[0];
190  }
191  m_ = m; n_ = n;
192 #else
193  if(m*n > bufsize_) {
194  this->deallocate();
195  m_ = m; n_ = n;
196  this->allocate();
197  }
198  else{
199  m_ = m; n_ = n;
200  }
201 #endif
202  }
203  const F& operator()(Int i, Int j) const {
204  if( i < 0 || i >= m_ ||
205  j < 0 || j >= n_ ) {
206  throw std::logic_error( "Index is out of bound." );
207  }
208  return data_[i+j*m_];
209  }
210 
211  F& operator()(Int i, Int j) {
212  if( i < 0 || i >= m_ ||
213  j < 0 || j >= n_ ) {
214  throw std::logic_error( "Index is out of bound." );
215  }
216  return data_[i+j*m_];
217  }
218 
219 #ifdef _NUMMAT_VECTOR_
220  std::vector<F> * Container() const { return container_; }
221 #endif
222  F* Data() const { return data_; }
223 
224  F* VecData(Int j) const
225  {
226  if( j < 0 || j >= n_ ) {
227  throw std::logic_error( "Index is out of bound." );
228  }
229  return &(data_[j*m_]);
230  }
231 
232  Int m() const { return m_; }
233  Int n() const { return n_; }
234 
235  Int Size() const {return m_*n_;}
236  Int ByteSize() const { return m_*n_*sizeof(F);}
237 #ifdef _NUMMAT_VECTOR_
238  Int AllocatedSize() const {return container_->capacity();}
239 #else
240  Int AllocatedSize() const {return bufsize_;}
241 #endif
242 
243 //#else
244 
372 #endif
373 
374  };
375 
376  // Commonly used
377 #ifdef _NUMMAT_VECTOR_
378  typedef NumMat<char> BolNumMat;
379 #else
380  typedef NumMat<bool> BolNumMat;
381 #endif
382  typedef NumMat<Int> IntNumMat;
383  typedef NumMat<Real> DblNumMat;
384  typedef NumMat<Complex> CpxNumMat;
385 
386  // *********************************************************************
387  // Utility functions
388  // *********************************************************************
390  template <class F> inline void SetValue(NumMat<F>& M, F val);
391 
393  template <class F> inline Real Energy(const NumMat<F>& M);
394 
395 } // namespace PEXSI
396 
397 #endif // _NUMMAT_DECL_HPP_
Environmental variables.
Real Energy(const NumMat< F > &M)
Energy computes the L2 norm of a matrix (treated as a vector).
Definition: nummat_impl.hpp:70
F * data_
The pointer for the actual data.
Definition: nummat_decl.hpp:117
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: nummat_impl.hpp:61
bool owndata_
Whether it owns the data.
Definition: nummat_decl.hpp:101
Numerical matrix.
Definition: nummat_decl.hpp:93
Int m_
The size of the first dimension.
Definition: nummat_decl.hpp:104
Int n_
The size of second dimension.
Definition: nummat_decl.hpp:107