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 ) {
55 ErrorHandling(
"Cannot allocate memory.");}
57 if(data!=NULL){std::copy(data,data+m_*n_,data_);}
65 if(bufsize_>0) {
delete[] data_; data_ = NULL; bufsize_ = 0; m_=0; n_=0; }
69 template <
class F>
NumMat<F>::NumMat(Int m, Int n): m_(m), n_(n), owndata_(true) {
73 template <
class F> NumMat<F>::NumMat(Int m, Int n,
bool owndata, F* data): m_(m), n_(n), owndata_(owndata) {
77 template <
class F> NumMat<F>::NumMat(
const NumMat& C): m_(C.m_), n_(C.n_), owndata_(C.owndata_) {
81 template <
class F> NumMat<F>::~NumMat() {
85 template <
class F> NumMat<F>& NumMat<F>::Copy(
const NumMat<F>& C) {
87 m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
88 this->allocate(C.data_);
92 template <
class F> NumMat<F>& NumMat<F>::operator=(
const NumMat<F>& C) {
94 m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
95 this->allocate(C.data_);
99 template <
class F>
void NumMat<F>::Resize(Int m, Int n) {
100 if( owndata_ ==
false ){
101 ErrorHandling(
"Matrix being resized must own data.");
114 template <
class F>
void NumMat<F>::Clear() {
115 if( owndata_ ==
false ){
116 ErrorHandling(
"Matrix being cleared must own data.");
128 template <
class F>
const F& NumMat<F>::operator()(Int i, Int j)
const {
129 if( i < 0 || i >= m_ ||
131 ErrorHandling(
"Index is out of bound." );
133 return data_[i+j*m_];
136 template <
class F> F& NumMat<F>::operator()(Int i, Int j) {
137 if( i < 0 || i >= m_ ||
139 ErrorHandling(
"Index is out of bound." );
141 return data_[i+j*m_];
144 template <
class F> F* NumMat<F>::VecData(Int j)
const
146 if( j < 0 || j >= n_ ) {
147 ErrorHandling(
"Index is out of bound." );
149 return &(data_[j*m_]);
155 std::fill(M.Data(),M.Data()+M.m()*M.n(),val);
162 for (Int i=0; i < M.m()*M.n(); i++)
163 sum += abs(ptr[i]) * abs(ptr[i]);
168 template <
class F>
inline void
169 Transpose (
const NumMat<F>& A, NumMat<F>& B )
171 if( A.m() != B.n() || A.n() != B.m() ){
172 B.Resize( A.n(), A.m() );
177 Int m = A.m(), n = A.n();
179 for( Int i = 0; i < m; i++ ){
180 for( Int j = 0; j < n; j++ ){
182 Bdata[ j + n*i ] = Adata[ i + j*m ];
190 template <
class F>
inline void
191 Symmetrize( NumMat<F>& A )
193 if( A.m() != A.n() ){
194 ErrorHandling(
"The matrix to be symmetrized should be a square matrix." );
205 for( Int i = 0; i < A.m() * A.n(); i++ ){
206 *Adata = half * (*Adata + *Bdata);
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