-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yanguohang
committed
Jan 4, 2019
0 parents
commit 845bf4f
Showing
91 changed files
with
14,346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2006 Hong Kong University of Science & Technology | ||
// All rights reserved. | ||
// | ||
// This file is part of CholmodWrapper | ||
// (http://ihome.ust.hk/~fuhb/software.htm#cholmod_wrapper); | ||
// you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation; version 2.1 of the License. | ||
// See the file LICENSE.LGPL distributed with CholmodWrapper. | ||
// | ||
// Licensees holding a valid commercial license may use this file in | ||
// accordance with the commercial license agreement provided with the software. | ||
// | ||
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
// | ||
// Author(s) : Hongbo Fu, [email protected], 2006 | ||
|
||
// ===================================================================== | ||
#ifndef CHOLMOD_CONVERSION_H | ||
#define CHOLMOD_CONVERSION_H | ||
#include <cholmod.h> | ||
#include "Sparse_coordinate_matrix.h" | ||
|
||
/// Convert sparse matrix from coordinate format to cholmod_triplet. | ||
/// Mainly for internal use. | ||
template <class T> | ||
cholmod_triplet* create_cholmod_triplet( | ||
const Sparse_coordinate_matrix<T>& coor_matrix, cholmod_common* c) | ||
{ | ||
Sparse_coordinate_matrix<T> local_copy(coor_matrix); | ||
local_copy.remove_bogus_nonzero_entries(); | ||
size_t upper_bound_of_nnz = local_copy.upper_bound_of_nnz(); | ||
|
||
// stype = 0: cholmod_triplet is "unsymmetric": | ||
// use both upper and lower triangular parts | ||
// stype > 0: matrix is square and symmetric. | ||
// use the lower triangular part to store non-zero entries | ||
int stype = local_copy.is_symmetric() ? 1 : 0; | ||
|
||
cholmod_triplet* triplet_matrix = cholmod_allocate_triplet( | ||
local_copy.row_dimension(), local_copy.column_dimension(), | ||
upper_bound_of_nnz, stype, CHOLMOD_REAL, c); | ||
triplet_matrix->nnz = upper_bound_of_nnz; | ||
|
||
if (upper_bound_of_nnz == 0) return triplet_matrix; // empty | ||
|
||
int* Ti = static_cast<int*> (triplet_matrix->i); | ||
int* Tj = static_cast<int*> (triplet_matrix->j); | ||
T* Tx = static_cast<T*> (triplet_matrix->x); | ||
int n = 0; | ||
for (typename Sparse_coordinate_matrix<T>::Coordinate_const_iterator it = | ||
local_copy.coords_begin(); it != local_copy.coords_end(); ++it, ++n) | ||
{ | ||
Ti[n] = it->i; Tj[n] = it->j; Tx[n] = it->value; | ||
} | ||
return triplet_matrix; | ||
} | ||
|
||
/// Convert sparse matrix from coordinate format to compressed-column form. | ||
/// This conversion function is not implemented as a member function of | ||
/// Sparse_coordinate_matrix, as I want Sparse_coordinate_matrix more general | ||
/// instead of depending on cholmod. | ||
/// Mainly for internal use. | ||
template <class T> | ||
cholmod_sparse* create_cholmod_sparse( | ||
const Sparse_coordinate_matrix<T>& coor_matrix, cholmod_common* c) | ||
{ | ||
cholmod_triplet* triplet_matrix = create_cholmod_triplet(coor_matrix, c); | ||
if (triplet_matrix == 0) return 0; // empty matrix | ||
|
||
cholmod_sparse* output = cholmod_triplet_to_sparse( | ||
triplet_matrix, static_cast<int>(triplet_matrix->nnz), c); | ||
|
||
cholmod_free_triplet(&triplet_matrix, c); | ||
|
||
return output; | ||
} | ||
|
||
#endif // CHOLMOD_CONVERSION_H | ||
// ===================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) 2006 Hong Kong University of Science & Technology | ||
// All rights reserved. | ||
// | ||
// This file is part of CholmodWrapper | ||
// (http://ihome.ust.hk/~fuhb/software.htm#cholmod_wrapper); | ||
// you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation; version 2.1 of the License. | ||
// See the file LICENSE.LGPL distributed with CholmodWrapper. | ||
// | ||
// Licensees holding a valid commercial license may use this file in | ||
// accordance with the commercial license agreement provided with the software. | ||
// | ||
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
// | ||
// Author(s) : Hongbo Fu, [email protected], 2006 | ||
|
||
// ===================================================================== | ||
#ifndef CHOLMOD_DENSE_MATRIX_H | ||
#define CHOLMOD_DENSE_MATRIX_H | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include <cholmod.h> | ||
|
||
// ===================================================================== | ||
#pragma warning (push) | ||
#pragma warning (disable: 4018) | ||
|
||
/// class Cholmod_dense_matrix is a simple array of array. | ||
/// Recommend using set/get functions to access the elements. | ||
template <class T> | ||
class Cholmod_dense_matrix : public std::vector< std::vector<T> > | ||
{ | ||
// -- public types ----------------------------- | ||
public: | ||
typedef T NT; | ||
|
||
// -- public operations ------------------------ | ||
public: | ||
// Create a vector initialized with zeros | ||
Cholmod_dense_matrix(int row, int col) | ||
: m_row_dimension(row), m_column_dimension(col) | ||
{ | ||
assert(row > 0 && col > 0); | ||
resize(col, std::vector<T>(row, 0.0)); // memory allocation | ||
} | ||
|
||
/// Convert to a cholmod_dense matrix | ||
cholmod_dense* create_cholmod_dense(cholmod_common* c) const | ||
{ | ||
cholmod_dense* output = | ||
cholmod_zeros(m_row_dimension, m_column_dimension, CHOLMOD_REAL, c); | ||
for (int i = 0; i < output->nrow; ++i) | ||
{ | ||
for (int j = 0; j < output->ncol; ++j) | ||
((T*)output->x)[i + j * output->d] = (*this)[j][i]; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
/// Copy values of a single column or single row cholmod_dense | ||
/// matrix to Cholmod_dense_matrix | ||
void assign(const cholmod_dense* cd_matrix) | ||
{ | ||
assert(cd_matrix != 0); | ||
assert(cd_matrix->nrow == m_row_dimension && | ||
cd_matrix->ncol == m_column_dimension); | ||
|
||
for (int i = 0; i < cd_matrix->nrow; ++i) | ||
{ | ||
for (int j = 0; j < cd_matrix->ncol; ++j) | ||
(*this)[j][i] = ((T*)cd_matrix->x)[i + j * cd_matrix->d]; | ||
} | ||
} | ||
|
||
inline void set(int row, int col, T val) { (*this)[col][row] = val; } | ||
inline T& get(int row, int col) { return (*this)[col][row]; } | ||
inline const T& get(int row, int col) const { return (*this)[col][row]; } | ||
|
||
/// Return the matrix number of rows | ||
int row_dimension() const { return m_row_dimension; } | ||
/// Return the matrix number of columns | ||
int column_dimension() const { return m_column_dimension; } | ||
|
||
// -- private variables ------------------------ | ||
private: | ||
int m_row_dimension; | ||
int m_column_dimension; | ||
}; | ||
|
||
template <class T> | ||
std::ostream& operator << (std::ostream& out, const Cholmod_dense_matrix<T>& matrix) | ||
{ | ||
for (int i = 0; i < matrix.row_dimension(); ++i) | ||
{ | ||
out << "Row " << i << ":"; | ||
for (int j = 0; j < matrix.column_dimension(); ++j) | ||
out << " " << matrix.get(i, j); | ||
out << "\n"; | ||
} | ||
return out; | ||
} | ||
|
||
|
||
#pragma warning(pop) | ||
// ===================================================================== | ||
|
||
#endif // CHOLMOD_DENSE_MATRIX_H | ||
// ===================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2006 Hong Kong University of Science & Technology | ||
// All rights reserved. | ||
// | ||
// This file is part of CholmodWrapper | ||
// (http://ihome.ust.hk/~fuhb/software.htm#cholmod_wrapper); | ||
// you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation; version 2.1 of the License. | ||
// See the file LICENSE.LGPL distributed with CholmodWrapper. | ||
// | ||
// Licensees holding a valid commercial license may use this file in | ||
// accordance with the commercial license agreement provided with the software. | ||
// | ||
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
// | ||
// Author(s) : Hongbo Fu, [email protected], 2006 | ||
|
||
// ===================================================================== | ||
#ifndef CHOLMOD_DENSE_VECTOR_H | ||
#define CHOLMOD_DENSE_VECTOR_H | ||
|
||
#include <vector> | ||
#include <cholmod.h> | ||
|
||
/// class Cholmod_dense_vector is a simple array, derived from std::vector. | ||
/// | ||
/// Concept: Model of the SparseLinearAlgebraTraits_d::Vector concept. | ||
template <class T> | ||
class Cholmod_dense_vector : public std::vector<T> | ||
{ | ||
private: | ||
typedef std::vector<T> Super_class; | ||
|
||
// -- public types ----------------------------- | ||
public: | ||
typedef T NT; | ||
|
||
// -- public operations ------------------------ | ||
public: | ||
// Create a vector initialized with zeros | ||
Cholmod_dense_vector(int dimension) | ||
: std::vector<T>(dimension, 0.0) | ||
{ | ||
} | ||
|
||
Cholmod_dense_vector(const std::vector<T> vec) | ||
{ | ||
resize(vec.size()); | ||
std::copy(vec.begin(), vec.end(), this->begin()); | ||
} | ||
|
||
int dimension() const { return Super_class::size(); } | ||
inline void set(int i, T val) { (*this)[i] = val; } | ||
inline T get(int i) const { return (*this)[i]; } | ||
|
||
/// Convert to a cholmod_dense matrix | ||
cholmod_dense* create_cholmod_dense(cholmod_common* c) const | ||
{ | ||
cholmod_dense* output = cholmod_zeros(Super_class::size(), 1, CHOLMOD_REAL, c); | ||
std::copy(Super_class::begin(), Super_class::end(), (T*)output->x); | ||
|
||
return output; | ||
} | ||
|
||
/// Copy values of a single column or single row cholmod_dense matrix | ||
/// to Cholmod_dense_vector | ||
void assign(const cholmod_dense* cd_matrix) | ||
{ | ||
assert(cd_matrix != 0); | ||
assert( | ||
(Super_class::size() == cd_matrix->nrow && cd_matrix->ncol == 1) || | ||
(Super_class::size() == cd_matrix->ncol && cd_matrix->nrow == 1)); | ||
|
||
T* ptr = (T*)cd_matrix->x; | ||
std::copy(ptr, ptr + size(), Super_class::begin()); | ||
} | ||
}; | ||
|
||
#endif // CHOLMOD_DENSE_VECTOR_H | ||
// ===================================================================== |
Oops, something went wrong.