PEXSI
 All Classes Namespaces Files Functions Variables Friends Pages
numtns_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 _NUMTNS_DECL_HPP_
47 #define _NUMTNS_DECL_HPP_
48 
49 #include "environment.hpp"
50 #include "nummat_impl.hpp"
51 
52 // TODO Move the things from decl to impl
53 
54 namespace PEXSI{
55 
63  template <class F>
64  class NumTns
65  {
66  public:
68  Int m_;
69 
71  Int n_;
72 
74  Int p_;
75 
77  bool owndata_;
78 
80  F* data_;
81  public:
82  NumTns(Int m=0, Int n=0, Int p=0): m_(m), n_(n), p_(p), owndata_(true) {
83  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
84  }
85  NumTns(Int m, Int n, Int p, bool owndata, F* data): m_(m), n_(n), p_(p), owndata_(owndata) {
86  if(owndata_) {
87  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
88  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = data[i]; }
89  } else {
90  data_ = data;
91  }
92  }
93  NumTns(const NumTns& C): m_(C.m_), n_(C.n_), p_(C.p_), owndata_(C.owndata_) {
94  if(owndata_) {
95  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
96  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
97  } else {
98  data_ = C.data_;
99  }
100  }
101  ~NumTns() {
102  if(owndata_) {
103  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
104  }
105  }
106  NumTns& operator=(const NumTns& C) {
107  if(owndata_) {
108  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
109  }
110  m_ = C.m_; n_=C.n_; p_=C.p_; owndata_=C.owndata_;
111  if(owndata_) {
112  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
113  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
114  } else {
115  data_ = C.data_;
116  }
117  return *this;
118  }
119  void Resize(Int m, Int n, Int p) {
120  if( owndata_ == false ){
121  throw std::logic_error("Tensor being resized must own data.");
122  }
123  if(m_!=m || n_!=n || p_!=p) {
124  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
125  m_ = m; n_ = n; p_=p;
126  if(m_>0 && n_>0 && p_>0) { data_ = new F[m_*n_*p_]; if( data_ == NULL ) throw std::runtime_error("Cannot allocate memory."); } else data_=NULL;
127  }
128  }
129  const F& operator()(Int i, Int j, Int k) const {
130  if( i < 0 || i >= m_ ||
131  j < 0 || j >= n_ ||
132  k < 0 || k >= p_ ) {
133  throw std::logic_error( "Index is out of bound." );
134  }
135  return data_[i+j*m_+k*m_*n_];
136  }
137  F& operator()(Int i, Int j, Int k) {
138  if( i < 0 || i >= m_ ||
139  j < 0 || j >= n_ ||
140  k < 0 || k >= p_ ) {
141  throw std::logic_error( "Index is out of bound." );
142  }
143  return data_[i+j*m_+k*m_*n_];
144  }
145 
146  // Int isempty() const {return (m_==0) && (n_==0) && (p_==0);}
147 
148  F* Data() const { return data_; }
149 
150  F* MatData (Int j) const {
151  if( j < 0 || j >= p_ ) {
152  throw std::logic_error( "Index is out of bound." );
153  }
154  return &(data_[j*m_*n_]);
155  };
156 
157  F* VecData (Int j, Int k) const {
158  if( j < 0 || j >= n_ ||
159  k < 0 || k >= p_ ) {
160  throw std::logic_error( "Index is out of bound." );
161  }
162 
163  return &(data_[k*m_*n_+j*m_]);
164  };
165 
166  Int m() const { return m_; }
167  Int n() const { return n_; }
168  Int p() const { return p_; }
169 
170  };
171 
172 
173  typedef NumTns<bool> BolNumTns;
174  typedef NumTns<Int> IntNumTns;
175  typedef NumTns<Real> DblNumTns;
176  typedef NumTns<Complex> CpxNumTns;
177 
178  // *********************************************************************
179  // Utility functions
180  // *********************************************************************
182  template <class F> inline void SetValue(NumTns<F>& T, F val);
183 
185  template <class F> inline Real Energy(const NumTns<F>& T);
186 
187 } // namespace PEXSI
188 
189 #endif // _NUMTNS_DECL_HPP_
Environmental variables.
Int n_
The size of second dimension.
Definition: numtns_decl.hpp:71
Int m_
The size of the first dimension.
Definition: numtns_decl.hpp:68
Real Energy(const NumMat< F > &M)
Energy computes the L2 norm of a matrix (treated as a vector).
Definition: nummat_impl.hpp:70
bool owndata_
Whether it owns the data.
Definition: numtns_decl.hpp:77
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: nummat_impl.hpp:61
Numerical tensor.
Definition: numtns_decl.hpp:64
Implementation of numerical matrix.
F * data_
The pointer for the actual data.
Definition: numtns_decl.hpp:80
Int p_
The size of third dimension.
Definition: numtns_decl.hpp:74