Skip to content

Commit

Permalink
Modify base matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanWRh committed May 26, 2024
1 parent d288ae8 commit fba9a40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
25 changes: 23 additions & 2 deletions source/module_hamilt_lcao/module_hcontainer/base_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ assert(this->value_begin != nullptr);
template <typename T>
void BaseMatrix<T>::add_array(T* array)
{
// if allocated, save data from array into matrix
// if whole matrix and 2d-block format, save data from array into matrix either
#ifdef __DEBUG
assert(this->value_begin != nullptr);
#endif
// if allocated, add data from array into matrix
// if whole matrix and 2d-block format, add data from array into matrix either
for (int i = 0; i < nrow_local * ncol_local; ++i)
{
value_begin[i] += array[i];
Expand All @@ -120,13 +123,19 @@ void BaseMatrix<T>::add_array(T* array)
template <typename T>
void BaseMatrix<T>::add_element(int mu, int nu, const T& value)
{
#ifdef __DEBUG
assert(this->value_begin != nullptr);
#endif
int index = mu * this->ncol_local + nu;
value_begin[index] += value;
}

template <typename T>
T& BaseMatrix<T>::get_value(const size_t& i_row, const size_t& j_col) const
{
#ifdef __DEBUG
assert(this->value_begin != nullptr);
#endif
int index = i_row * this->ncol_local + j_col;
return value_begin[index];
}
Expand All @@ -145,6 +154,12 @@ BaseMatrix<T>& BaseMatrix<T>::operator=(const BaseMatrix<T>& other)
{
this->nrow_local = other.nrow_local;
this->ncol_local = other.ncol_local;

if (this->allocated)
{
delete[] this->value_begin;
}

if (other.allocated)
{
this->value_begin = new T[nrow_local * ncol_local];
Expand Down Expand Up @@ -172,6 +187,12 @@ BaseMatrix<T>& BaseMatrix<T>::operator=(BaseMatrix<T>&& other) noexcept
{
this->nrow_local = other.nrow_local;
this->ncol_local = other.ncol_local;

if (this->allocated)
{
delete[] this->value_begin;
}

this->value_begin = other.value_begin;
this->allocated = other.allocated;
if (other.allocated)
Expand Down
7 changes: 5 additions & 2 deletions source/module_hamilt_lcao/module_hcontainer/base_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class BaseMatrix
void set_zero();

/**
* @brief save an array to the matrix
* @brief add an array to the matrix
*
* @param array array to be saved
* @param array array to be added
*/
void add_array(T* array);

/**
* @brief add a single element to the matrix
*
Expand All @@ -52,6 +53,7 @@ class BaseMatrix
* @param value value to be added
*/
void add_element(int mu, int nu, const T& value);

// for inside matrix
/**
* @brief get value from a whole matrix
Expand All @@ -61,6 +63,7 @@ class BaseMatrix
* @return T&
*/
T& get_value(const size_t& i_row, const size_t& j_col) const;

/**
* @brief get pointer of value from a submatrix
*/
Expand Down

0 comments on commit fba9a40

Please sign in to comment.