PEXSI
 All Classes Namespaces Files Functions Variables Typedefs Pages
NumTns_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_NUMTNS_IMPL_HPP_
47 #define _PEXSI_NUMTNS_IMPL_HPP_
48 
49 
50 namespace PEXSI{
51 
52  template <class F> NumTns<F>::NumTns(Int m, Int n, Int p): m_(m), n_(n), p_(p), owndata_(true) {
53  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;
54  }
55 
56  template <class F> NumTns<F>::NumTns(Int m, Int n, Int p, bool owndata, F* data): m_(m), n_(n), p_(p), owndata_(owndata) {
57  if(owndata_) {
58  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;
59  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = data[i]; }
60  } else {
61  data_ = data;
62  }
63  }
64 
65  template <class F> NumTns<F>::NumTns(const NumTns<F>& C): m_(C.m_), n_(C.n_), p_(C.p_), owndata_(C.owndata_) {
66  if(owndata_) {
67  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;
68  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
69  } else {
70  data_ = C.data_;
71  }
72  }
73 
74  template <class F> NumTns<F>::~NumTns() {
75  if(owndata_) {
76  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
77  }
78  }
79 
80  template <class F> NumTns<F>& NumTns<F>::operator=(const NumTns<F>& C) {
81  if(owndata_) {
82  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
83  }
84  m_ = C.m_; n_=C.n_; p_=C.p_; owndata_=C.owndata_;
85  if(owndata_) {
86  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;
87  if(m_>0 && n_>0 && p_>0) { for(Int i=0; i<m_*n_*p_; i++) data_[i] = C.data_[i]; }
88  } else {
89  data_ = C.data_;
90  }
91  return *this;
92  }
93 
94  template <class F> void NumTns<F>::Resize(Int m, Int n, Int p) {
95  if( owndata_ == false ){
96  throw std::logic_error("Tensor being resized must own data.");
97  }
98  if(m_!=m || n_!=n || p_!=p) {
99  if(m_>0 && n_>0 && p_>0) { delete[] data_; data_ = NULL; }
100  m_ = m; n_ = n; p_=p;
101  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;
102  }
103  }
104 
105  template <class F> const F& NumTns<F>::operator()(Int i, Int j, Int k) const {
106  if( i < 0 || i >= m_ ||
107  j < 0 || j >= n_ ||
108  k < 0 || k >= p_ ) {
109  throw std::logic_error( "Index is out of bound." );
110  }
111  return data_[i+j*m_+k*m_*n_];
112  }
113 
114  template <class F> F& NumTns<F>::operator()(Int i, Int j, Int k) {
115  if( i < 0 || i >= m_ ||
116  j < 0 || j >= n_ ||
117  k < 0 || k >= p_ ) {
118  throw std::logic_error( "Index is out of bound." );
119  }
120  return data_[i+j*m_+k*m_*n_];
121  }
122 
123 
124  template <class F> F* NumTns<F>::MatData (Int j) const {
125  if( j < 0 || j >= p_ ) {
126  throw std::logic_error( "Index is out of bound." );
127  }
128  return &(data_[j*m_*n_]);
129  };
130 
131  template <class F> F* NumTns<F>::VecData (Int j, Int k) const {
132  if( j < 0 || j >= n_ ||
133  k < 0 || k >= p_ ) {
134  throw std::logic_error( "Index is out of bound." );
135  }
136 
137  return &(data_[k*m_*n_+j*m_]);
138  };
139 
140  template <class F> inline void SetValue(NumTns<F>& T, F val)
141  {
142  F *ptr = T.data_;
143  for(Int i=0; i < T.m() * T.n() * T.p(); i++) *(ptr++) = val;
144 
145  return;
146  }
147 
148 
149 
150  template <class F> inline Real Energy(const NumTns<F>& T)
151  {
152  Real sum = 0;
153 
154  F *ptr = T.Data();
155  for(Int i=0; i < T.m() * T.n() * T.p(); i++)
156  sum += abs(ptr[i]) * abs(ptr[i]);
157 
158  return sum;
159  }
160 
161 } // namespace PEXSI
162 
163 #endif // _PEXSI_NUMTNS_IMPL_HPP_
Int n_
The size of second dimension.
Definition: NumTns.hpp:70
Int m_
The size of the first dimension.
Definition: NumTns.hpp:67
Real Energy(const NumMat< F > &M)
Energy computes the L2 norm of a matrix (treated as a vector).
Definition: NumMat_impl.hpp:142
bool owndata_
Whether it owns the data.
Definition: NumTns.hpp:76
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: NumMat_impl.hpp:137
Numerical tensor.
Definition: NumTns.hpp:63
F * data_
The pointer for the actual data.
Definition: NumTns.hpp:79
Int p_
The size of third dimension.
Definition: NumTns.hpp:73