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