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 )
throw std::runtime_error(
"Cannot allocate memory."); }
else data_=NULL;
55 if(data!=NULL){std::copy(data,data+m_*n_,data_);}
63 if(bufsize_>0) {
delete[] data_; data_ = NULL; }
67 template <
class F>
NumMat<F>::NumMat(Int m, Int n): m_(m), n_(n), owndata_(true) {
71 template <
class F> NumMat<F>::NumMat(Int m, Int n,
bool owndata, F* data): m_(m), n_(n), owndata_(owndata) {
75 template <
class F> NumMat<F>::NumMat(
const NumMat& C): m_(C.m_), n_(C.n_), owndata_(C.owndata_) {
79 template <
class F> NumMat<F>::~NumMat() {
83 template <
class F> NumMat<F>& NumMat<F>::Copy(
const NumMat<F>& C) {
85 m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
86 this->allocate(C.data_);
90 template <
class F> NumMat<F>& NumMat<F>::operator=(
const NumMat<F>& C) {
92 m_ = C.m_; n_=C.n_; owndata_=C.owndata_;
93 this->allocate(C.data_);
97 template <
class F>
void NumMat<F>::Resize(Int m, Int n) {
98 if( owndata_ ==
false ){
99 throw std::logic_error(
"Matrix being resized must own data.");
112 template <
class F>
const F& NumMat<F>::operator()(Int i, Int j)
const {
113 if( i < 0 || i >= m_ ||
115 throw std::logic_error(
"Index is out of bound." );
117 return data_[i+j*m_];
120 template <
class F> F& NumMat<F>::operator()(Int i, Int j) {
121 if( i < 0 || i >= m_ ||
123 throw std::logic_error(
"Index is out of bound." );
125 return data_[i+j*m_];
128 template <
class F> F* NumMat<F>::VecData(Int j)
const
130 if( j < 0 || j >= n_ ) {
131 throw std::logic_error(
"Index is out of bound." );
133 return &(data_[j*m_]);
139 std::fill(M.Data(),M.Data()+M.m()*M.n(),val);
146 for (Int i=0; i < M.m()*M.n(); i++)
147 sum += abs(ptr[i]) * abs(ptr[i]);
152 template <
class F>
inline void
153 Transpose (
const NumMat<F>& A, NumMat<F>& B )
156 PushCallStack(
"Transpose");
158 if( A.m() != B.n() || A.n() != B.m() ){
159 B.Resize( A.n(), A.m() );
164 Int m = A.m(), n = A.n();
166 for( Int i = 0; i < m; i++ ){
167 for( Int j = 0; j < n; j++ ){
168 Bdata[ j + n*i ] = Adata[ i + j*m ];
179 template <
class F>
inline void
180 Symmetrize( NumMat<F>& A )
183 PushCallStack(
"Symmetrize");
185 if( A.m() != A.n() ){
186 throw std::logic_error(
"The matrix to be symmetrized should be a square matrix." );
197 for( Int i = 0; i < A.m() * A.n(); i++ ){
198 *Adata = half * (*Adata + *Bdata);
212 #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:142
void deallocate()
Helper function freeing memory pointed by the data_ attribute.
Definition: NumMat_impl.hpp:61
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:137
Numerical matrix.
Definition: NumMat.hpp:61