46 #ifndef _PEXSI_NUMMAT_IMPL_HPP_
47 #define _PEXSI_NUMMAT_IMPL_HPP_
54 if(m_>0 && n_>0) { data_ =
new F[m_*n_];
if( data_ == NULL ) {
58 throw std::runtime_error(
"Cannot allocate memory.");}
60 if(data!=NULL){std::copy(data,data+m_*n_,data_);}
68 if(bufsize_>0) {
delete[] data_; data_ = NULL; bufsize_ = 0; m_=0; n_=0; }
72 template <
class F>
NumMat<F>::NumMat(Int m, Int n): m_(m), n_(n), owndata_(true) {
76 template <
class F> NumMat<F>::NumMat(Int m, Int n,
bool owndata, F* data): m_(m), n_(n), owndata_(owndata) {
80 template <
class F> NumMat<F>::NumMat(
const NumMat& C): m_(C.m_), n_(C.n_), owndata_(C.owndata_) {
84 template <
class F> NumMat<F>::~NumMat() {
88 template <
class F> NumMat<F>& NumMat<F>::Copy(
const NumMat<F>& C) {
90 m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
91 this->allocate(C.data_);
95 template <
class F> NumMat<F>& NumMat<F>::operator=(
const NumMat<F>& C) {
97 m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
98 this->allocate(C.data_);
102 template <
class F>
void NumMat<F>::Resize(Int m, Int n) {
103 if( owndata_ ==
false ){
107 throw std::logic_error(
"Matrix being resized must own data.");
120 template <
class F>
void NumMat<F>::Clear() {
121 if( owndata_ ==
false ){
125 throw std::logic_error(
"Matrix being cleared must own data.");
137 template <
class F>
const F& NumMat<F>::operator()(Int i, Int j)
const {
138 if( i < 0 || i >= m_ ||
143 throw std::logic_error(
"Index is out of bound." );
145 return data_[i+j*m_];
148 template <
class F> F& NumMat<F>::operator()(Int i, Int j) {
149 if( i < 0 || i >= m_ ||
154 throw std::logic_error(
"Index is out of bound." );
156 return data_[i+j*m_];
159 template <
class F> F* NumMat<F>::VecData(Int j)
const
161 if( j < 0 || j >= n_ ) {
165 throw std::logic_error(
"Index is out of bound." );
167 return &(data_[j*m_]);
173 std::fill(M.Data(),M.Data()+M.m()*M.n(),val);
180 for (Int i=0; i < M.m()*M.n(); i++)
181 sum += abs(ptr[i]) * abs(ptr[i]);
186 template <
class F>
inline void
187 Transpose (
const NumMat<F>& A, NumMat<F>& B )
190 PushCallStack(
"Transpose");
192 if( A.m() != B.n() || A.n() != B.m() ){
193 B.Resize( A.n(), A.m() );
198 Int m = A.m(), n = A.n();
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 ];
213 template <
class F>
inline void
214 Symmetrize( NumMat<F>& A )
217 PushCallStack(
"Symmetrize");
219 if( A.m() != A.n() ){
223 throw std::logic_error(
"The matrix to be symmetrized should be a square matrix." );
234 for( Int i = 0; i < A.m() * A.n(); i++ ){
235 *Adata = half * (*Adata + *Bdata);
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