Browse Source
Added EigenAdapter and a Test for the Adapter.
Added EigenAdapter and a Test for the Adapter.
Fixed a type in EigenDtmcPrctlModelChecker.h Added missing transitions in one example input filetempestpy_adaptions
PBerger
12 years ago
4 changed files with 156 additions and 2 deletions
-
57src/adapters/EigenAdapter.h
-
2src/modelChecker/EigenDtmcPrctlModelChecker.h
-
3src/storage/SparseMatrix.h
-
96test/eigen/EigenAdapterTest.cpp
@ -0,0 +1,57 @@ |
|||
/* |
|||
* EigenAdapter.h |
|||
* |
|||
* Created on: 21.01.2013 |
|||
* Author: Philipp Berger |
|||
*/ |
|||
|
|||
#ifndef STORM_ADAPTERS_EIGENADAPTER_H_ |
|||
#define STORM_ADAPTERS_EIGENADAPTER_H_ |
|||
|
|||
#include "src/storage/SparseMatrix.h" |
|||
#include "Eigen/Sparse" |
|||
|
|||
#include "log4cplus/logger.h" |
|||
#include "log4cplus/loggingmacros.h" |
|||
|
|||
extern log4cplus::Logger logger; |
|||
|
|||
namespace storm { |
|||
|
|||
namespace adapters { |
|||
|
|||
class EigenAdapter { |
|||
public: |
|||
/*! |
|||
* Converts a sparse matrix into the sparse matrix in the eigen format. |
|||
* @return A pointer to a row-major sparse matrix in eigen format. |
|||
*/ |
|||
template<class T> |
|||
static Eigen::SparseMatrix<T, Eigen::RowMajor, int_fast32_t>* toEigenSparseMatrix(storm::storage::SparseMatrix<T> const& matrix) { |
|||
uint_fast64_t realNonZeros = matrix.getNonZeroEntryCount(); |
|||
LOG4CPLUS_DEBUG(logger, "Converting matrix with " << realNonZeros << " non-zeros to Eigen format."); |
|||
|
|||
// Prepare the resulting matrix. |
|||
Eigen::SparseMatrix<T, Eigen::RowMajor, int_fast32_t>* result = new Eigen::SparseMatrix<T, Eigen::RowMajor, int_fast32_t>(matrix.rowCount, matrix.colCount); |
|||
|
|||
result->resizeNonZeros(realNonZeros); |
|||
//result->reserve(realNonZeros); |
|||
|
|||
// Copy Row Indications |
|||
std::copy(matrix.rowIndications.begin(), matrix.rowIndications.end(), (result->outerIndexPtr())); |
|||
// Copy Columns Indications |
|||
std::copy(matrix.columnIndications.begin(), matrix.columnIndications.end(), (result->innerIndexPtr())); |
|||
// And do the same thing with the actual values. |
|||
std::copy(matrix.valueStorage.begin(), matrix.valueStorage.end(), (result->valuePtr())); |
|||
|
|||
LOG4CPLUS_DEBUG(logger, "Done converting matrix to Eigen format."); |
|||
|
|||
return result; |
|||
} |
|||
}; |
|||
|
|||
} //namespace adapters |
|||
|
|||
} //namespace storm |
|||
|
|||
#endif /* STORM_ADAPTERS_GMMXXADAPTER_H_ */ |
@ -0,0 +1,96 @@ |
|||
#include "gtest/gtest.h"
|
|||
|
|||
#include "Eigen/Sparse"
|
|||
#include "src/adapters/EigenAdapter.h"
|
|||
#include "src/exceptions/InvalidArgumentException.h"
|
|||
#include "boost/integer/integer_mask.hpp"
|
|||
|
|||
#define STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE 5
|
|||
#define STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE 5
|
|||
|
|||
TEST(EigenAdapterTest, SimpleDenseSquareCopy) { |
|||
// 5 rows
|
|||
storm::storage::SparseMatrix<double> sm(STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE); |
|||
|
|||
double values[STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE]; |
|||
sm.initialize(STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE); |
|||
|
|||
int row = 0; |
|||
int col = 0; |
|||
for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) { |
|||
values[i] = static_cast<double>(i + 1); |
|||
|
|||
sm.addNextValue(row, col, values[i]); |
|||
++col; |
|||
if (col == STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) { |
|||
++row; |
|||
col = 0; |
|||
} |
|||
} |
|||
sm.finalize(); |
|||
|
|||
auto esm = storm::adapters::EigenAdapter::toEigenSparseMatrix(sm); |
|||
|
|||
ASSERT_EQ(esm->rows(), STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE); |
|||
ASSERT_EQ(esm->cols(), STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE); |
|||
ASSERT_EQ(esm->nonZeros(), STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE); |
|||
|
|||
row = 0; |
|||
col = 0; |
|||
for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) { |
|||
ASSERT_EQ(values[i], esm->coeff(row, col)); |
|||
++col; |
|||
if (col == STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) { |
|||
++row; |
|||
col = 0; |
|||
} |
|||
} |
|||
} |
|||
|
|||
TEST(EigenAdapterTest, SimpleSparseSquareCopy) { |
|||
// 5 rows
|
|||
storm::storage::SparseMatrix<double> sm(STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE); |
|||
|
|||
double values[STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE]; |
|||
sm.initialize((STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2); |
|||
|
|||
int row = 0; |
|||
int col = 0; |
|||
|
|||
bool everySecondElement = true; |
|||
|
|||
for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) { |
|||
values[i] = static_cast<double>(i + 1); |
|||
if (everySecondElement) { |
|||
sm.addNextValue(row, col, values[i]); |
|||
} |
|||
everySecondElement = !everySecondElement; |
|||
++col; |
|||
if (col == STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) { |
|||
++row; |
|||
col = 0; |
|||
} |
|||
} |
|||
sm.finalize(); |
|||
|
|||
auto esm = storm::adapters::EigenAdapter::toEigenSparseMatrix(sm); |
|||
|
|||
ASSERT_EQ(esm->rows(), STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE); |
|||
ASSERT_EQ(esm->cols(), STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE); |
|||
ASSERT_EQ(esm->nonZeros(), (STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2); |
|||
|
|||
row = 0; |
|||
col = 0; |
|||
everySecondElement = true; |
|||
for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) { |
|||
if (everySecondElement) { |
|||
ASSERT_EQ(values[i], esm->coeff(row, col)); |
|||
} |
|||
everySecondElement = !everySecondElement; |
|||
++col; |
|||
if (col == STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) { |
|||
++row; |
|||
col = 0; |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue