diff --git a/MathLib/LinAlg/Sparse/Sparsity.h b/MathLib/LinAlg/Sparse/Sparsity.h new file mode 100644 index 0000000000000000000000000000000000000000..e9a05ffecbe555a21c3de6f94a920e15dc5f8676 --- /dev/null +++ b/MathLib/LinAlg/Sparse/Sparsity.h @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + * + * \file Sparsity.h + * + * Created on 2012-06-25 by Norihiro Watanabe + */ + +#ifndef SPARSITY_H_ +#define SPARSITY_H_ + +#include <vector> +#include <set> + +namespace MathLib +{ + +/** + * \brief Row-major sparse pattern + */ +typedef std::vector<std::set<std::size_t> > RowMajorSparsity; + +/** + * convert a row-major sparsity to CRS data + * + * @tparam INTTYPE index type + * @param row_major_entries Row-major sparse pattern + * @param n_rows The number of rows + * @param row_ptr Pointer to row index + * @param col_idx Pointer to column index + * @param nonzero The number of non-zero entries + * @param data Pointer to non-zero values + */ +template<class INTTYPE> +void convertRowMajorSparsityToCRS(const RowMajorSparsity &row_major_entries, std::size_t &n_rows, INTTYPE* &row_ptr, INTTYPE* &col_idx, std::size_t &nonzero, double* &data); + +} // end namespace MathLib + +#include "Sparsity.tpp" + +#endif // SPARSITY_H_ diff --git a/MathLib/LinAlg/Sparse/Sparsity.tpp b/MathLib/LinAlg/Sparse/Sparsity.tpp new file mode 100644 index 0000000000000000000000000000000000000000..6a41e5e611053997b4f52fc81fd694bdf0f70f47 --- /dev/null +++ b/MathLib/LinAlg/Sparse/Sparsity.tpp @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + * + * \file Sparsity.tpp + * + * Created on 2012-12-03 by Norihiro Watanabe + */ + +#ifndef SPARSITY_TPP_ +#define SPARSITY_TPP_ + +#include <cassert> + +namespace MathLib +{ + +template<class INTTYPE> +void convertRowMajorSparsityToCRS(const RowMajorSparsity &row_major_entries, std::size_t &n_rows, INTTYPE* &row_ptr, INTTYPE* &col_idx, std::size_t &nonzero, double* &data) +{ + n_rows = row_major_entries.size(); + assert(n_rows > 0); + if (n_rows==0) return; + + // get the number of nonzero entries and store the locations in the nonzero + // vector that start a row + nonzero = 0; + row_ptr = new INTTYPE[n_rows+1]; + for (std::size_t i=0; i<n_rows; i++) { + row_ptr[i] = nonzero; // starting point of the row + nonzero += row_major_entries[i].size(); // entries at the i th row + } + row_ptr[n_rows] = nonzero; + + // store column indexes of nonzero entries + col_idx = new INTTYPE[nonzero]; + size_t cnt_entries = 0; + for (std::size_t i=0; i<n_rows; i++) { + const std::set<std::size_t> &setConnection = row_major_entries[i]; + for (std::set<std::size_t>::iterator it=setConnection.begin(); it!=setConnection.end(); ++it) { + col_idx[cnt_entries++] = *it; + } + } + + // allocate memory for nonzero entries + data = new double[nonzero]; + for (std::size_t i=0; i<nonzero; i++) + data[i] = .0; +} + +} // end namespace MathLib + +#endif // SPARSITY_TPP_