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