#include "gtest/gtest.h" #include "src/storage/SparseMatrix.h" #include "src/exceptions/InvalidStateException.h" #include "src/exceptions/OutOfRangeException.h" TEST(SparseMatrixBuilder, CreationWithDimensions) { storm::storage::SparseMatrixBuilder matrixBuilder(3, 4, 5); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_EQ(3, matrix.getRowCount()); ASSERT_EQ(4, matrix.getColumnCount()); ASSERT_EQ(5, matrix.getEntryCount()); } TEST(SparseMatrixBuilder, CreationWithoutNumberOfEntries) { storm::storage::SparseMatrixBuilder matrixBuilder(3, 4); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_EQ(3, matrix.getRowCount()); ASSERT_EQ(4, matrix.getColumnCount()); ASSERT_EQ(5, matrix.getEntryCount()); } TEST(SparseMatrixBuilder, CreationWithNumberOfRows) { storm::storage::SparseMatrixBuilder matrixBuilder(3); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_EQ(3, matrix.getRowCount()); ASSERT_EQ(4, matrix.getColumnCount()); ASSERT_EQ(5, matrix.getEntryCount()); } TEST(SparseMatrixBuilder, CreationWithoutDimensions) { storm::storage::SparseMatrixBuilder matrixBuilder; ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 3, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 2, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_EQ(2, matrix.getRowCount()); ASSERT_EQ(4, matrix.getColumnCount()); ASSERT_EQ(5, matrix.getEntryCount()); } TEST(SparseMatrixBuilder, AddNextValue) { storm::storage::SparseMatrixBuilder matrixBuilder1(3, 4, 5); ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 2, 1.2)); ASSERT_THROW(matrixBuilder1.addNextValue(0, 4, 0.5), storm::exceptions::OutOfRangeException); ASSERT_THROW(matrixBuilder1.addNextValue(3, 1, 0.5), storm::exceptions::OutOfRangeException); storm::storage::SparseMatrixBuilder matrixBuilder2(3, 4); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2)); ASSERT_THROW(matrixBuilder2.addNextValue(0, 4, 0.5), storm::exceptions::OutOfRangeException); ASSERT_THROW(matrixBuilder2.addNextValue(3, 1, 0.5), storm::exceptions::OutOfRangeException); storm::storage::SparseMatrixBuilder matrixBuilder3(3); ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(2, 4, 0.5)); ASSERT_THROW(matrixBuilder3.addNextValue(3, 1, 0.2), storm::exceptions::OutOfRangeException); storm::storage::SparseMatrixBuilder matrixBuilder4; ASSERT_NO_THROW(matrixBuilder4.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(2, 4, 0.5)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(3, 1, 0.2)); } TEST(SparseMatrix, Build) { storm::storage::SparseMatrixBuilder matrixBuilder1(3, 4, 5); ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder1.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder1.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder1.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder1.addNextValue(1, 3, 0.2)); ASSERT_NO_THROW(matrixBuilder1.build()); storm::storage::SparseMatrixBuilder matrixBuilder2(3, 4, 5); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7)); ASSERT_THROW(matrixBuilder2.build(), storm::exceptions::InvalidStateException); storm::storage::SparseMatrixBuilder matrixBuilder3; ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix3; ASSERT_NO_THROW(matrix3 = matrixBuilder3.build()); ASSERT_EQ(2, matrix3.getRowCount()); ASSERT_EQ(4, matrix3.getColumnCount()); ASSERT_EQ(5, matrix3.getEntryCount()); storm::storage::SparseMatrixBuilder matrixBuilder4; ASSERT_NO_THROW(matrixBuilder4.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder4.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix4; ASSERT_NO_THROW(matrix4 = matrixBuilder4.build(4)); ASSERT_EQ(4, matrix4.getRowCount()); ASSERT_EQ(4, matrix4.getColumnCount()); ASSERT_EQ(5, matrix4.getEntryCount()); storm::storage::SparseMatrixBuilder matrixBuilder5; ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix5; ASSERT_NO_THROW(matrix5 = matrixBuilder5.build(0, 6)); ASSERT_EQ(2, matrix5.getRowCount()); ASSERT_EQ(6, matrix5.getColumnCount()); ASSERT_EQ(5, matrix5.getEntryCount()); } TEST(SparseMatrix, CreationWithMovingContents) { std::vector> columnsAndValues; columnsAndValues.emplace_back(1, 1.0); columnsAndValues.emplace_back(2, 1.2); columnsAndValues.emplace_back(0, 0.5); columnsAndValues.emplace_back(1, 0.7); columnsAndValues.emplace_back(3, 0.2); ASSERT_NO_THROW(storm::storage::SparseMatrix matrix(4, {0, 2, 5, 5}, columnsAndValues, {0, 1, 2, 3})); storm::storage::SparseMatrix matrix(4, {0, 2, 5, 5}, columnsAndValues, {0, 1, 2, 3}); ASSERT_EQ(3, matrix.getRowCount()); ASSERT_EQ(4, matrix.getColumnCount()); ASSERT_EQ(5, matrix.getEntryCount()); ASSERT_EQ(3, matrix.getRowGroupCount()); } TEST(SparseMatrix, CopyConstruct) { storm::storage::SparseMatrixBuilder matrixBuilder; ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_NO_THROW(storm::storage::SparseMatrix copy(matrix)); storm::storage::SparseMatrix copy(matrix); ASSERT_TRUE(matrix == copy); } TEST(SparseMatrix, CopyAssign) { storm::storage::SparseMatrixBuilder matrixBuilder; ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_NO_THROW(storm::storage::SparseMatrix copy = matrix); storm::storage::SparseMatrix copy = matrix; ASSERT_TRUE(matrix == copy); } TEST(SparseMatrix, MakeAbsorbing) { storm::storage::SparseMatrixBuilder matrixBuilder(3, 4, 5); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.2)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); storm::storage::BitVector absorbingRows(3); absorbingRows.set(1); ASSERT_NO_THROW(matrix.makeRowsAbsorbing(absorbingRows)); storm::storage::SparseMatrixBuilder matrixBuilder2(3, 4, 3); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 1)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); ASSERT_TRUE(matrix == matrix2); } TEST(SparseMatrix, MakeRowGroupAbsorbing) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9, true); ASSERT_NO_THROW(matrixBuilder.newRowGroup(0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.newRowGroup(2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.newRowGroup(4)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); storm::storage::BitVector absorbingRowGroups(3); absorbingRowGroups.set(1); ASSERT_NO_THROW(matrix.makeRowGroupsAbsorbing(absorbingRowGroups)); storm::storage::SparseMatrixBuilder matrixBuilder2(0, 0, 0, true); ASSERT_NO_THROW(matrixBuilder2.newRowGroup(0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder2.newRowGroup(2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 1, 1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 1, 1)); ASSERT_NO_THROW(matrixBuilder2.newRowGroup(4)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); ASSERT_TRUE(matrix == matrix2); } TEST(SparseMatrix, ConstrainedRowSumVector) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); storm::storage::BitVector columnConstraint(4); columnConstraint.set(1); columnConstraint.set(3); ASSERT_NO_THROW(std::vector constrainedRowSum = matrix.getConstrainedRowSumVector(storm::storage::BitVector(5, true), columnConstraint)); std::vector constrainedRowSum = matrix.getConstrainedRowSumVector(storm::storage::BitVector(5, true), columnConstraint); ASSERT_TRUE(constrainedRowSum == std::vector({1.0, 0.7, 0, 0, 0.5})); storm::storage::SparseMatrixBuilder matrixBuilder2(5, 4, 9, true); ASSERT_NO_THROW(matrixBuilder2.newRowGroup(0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder2.newRowGroup(2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 3, 1.2)); ASSERT_NO_THROW(matrixBuilder2.newRowGroup(4)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); storm::storage::BitVector rowGroupConstraint(3); rowGroupConstraint.set(1); storm::storage::BitVector columnConstraint2(4); columnConstraint2.set(2); columnConstraint2.set(3); ASSERT_NO_THROW(std::vector constrainedRowSum2 = matrix2.getConstrainedRowGroupSumVector(rowGroupConstraint, columnConstraint2)); std::vector constrainedRowSum2 = matrix2.getConstrainedRowGroupSumVector(rowGroupConstraint, columnConstraint2); ASSERT_TRUE(constrainedRowSum2 == std::vector({0, 2.3})); } TEST(SparseMatrix, Submatrix) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9, true); ASSERT_NO_THROW(matrixBuilder.newRowGroup(0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.newRowGroup(1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.newRowGroup(2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.newRowGroup(4)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); std::vector rowGroupIndices = {0, 1, 2, 4, 5}; storm::storage::BitVector rowGroupConstraint(4); rowGroupConstraint.set(2); rowGroupConstraint.set(3); storm::storage::BitVector columnConstraint(4); columnConstraint.set(0); columnConstraint.set(3); ASSERT_NO_THROW(storm::storage::SparseMatrix matrix2 = matrix.getSubmatrix(true, rowGroupConstraint, columnConstraint, false)); storm::storage::SparseMatrix matrix2 = matrix.getSubmatrix(true, rowGroupConstraint, columnConstraint, false); storm::storage::SparseMatrixBuilder matrixBuilder3(3, 2, 3, true); ASSERT_NO_THROW(matrixBuilder3.newRowGroup(0)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder3.newRowGroup(2)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(2, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(2, 1, 0.3)); storm::storage::SparseMatrix matrix3; ASSERT_NO_THROW(matrix3 = matrixBuilder3.build()); ASSERT_TRUE(matrix2 == matrix3); std::vector rowGroupToIndexMapping = {0, 0, 1, 0}; ASSERT_NO_THROW(storm::storage::SparseMatrix matrix4 = matrix.selectRowsFromRowGroups(rowGroupToIndexMapping)); storm::storage::SparseMatrix matrix4 = matrix.selectRowsFromRowGroups(rowGroupToIndexMapping); storm::storage::SparseMatrixBuilder matrixBuilder5(4, 4, 8); ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(2, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(3, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(3, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder5.addNextValue(3, 3, 0.3)); storm::storage::SparseMatrix matrix5; ASSERT_NO_THROW(matrix5 = matrixBuilder5.build()); ASSERT_TRUE(matrix4 == matrix5); } TEST(SparseMatrix, Transpose) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_NO_THROW(storm::storage::SparseMatrix transposeResult = matrix.transpose()); storm::storage::SparseMatrix transposeResult = matrix.transpose(); storm::storage::SparseMatrixBuilder matrixBuilder2(4, 5, 9); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 4, 0.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 4, 0.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, 1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 3, 1.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 4, 0.3)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); ASSERT_TRUE(transposeResult == matrix2); } TEST(SparseMatrix, EquationSystem) { storm::storage::SparseMatrixBuilder matrixBuilder(4, 4, 7); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 0, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 2, 0.99)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 3, 0.11)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_NO_THROW(matrix.convertToEquationSystem()); storm::storage::SparseMatrixBuilder matrixBuilder2(4, 4, 7); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 0, 1 - 1.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, -1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 1 - 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 3, -0.7)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, -0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 2, 1 - 0.99)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 3, 1 - 0.11)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); ASSERT_TRUE(matrix == matrix2); } TEST(SparseMatrix, JacobiDecomposition) { storm::storage::SparseMatrixBuilder matrixBuilder(4, 4, 7); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 0, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 3, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 2, 0.99)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 3, 0.11)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_NO_THROW(matrix.getJacobiDecomposition()); std::pair, storm::storage::SparseMatrix> jacobiDecomposition = matrix.getJacobiDecomposition(); storm::storage::SparseMatrixBuilder luBuilder(4, 4, 3); ASSERT_NO_THROW(luBuilder.addNextValue(0, 1, 1.2)); ASSERT_NO_THROW(luBuilder.addNextValue(1, 3, 0.7)); ASSERT_NO_THROW(luBuilder.addNextValue(2, 0, 0.5)); storm::storage::SparseMatrix lu; ASSERT_NO_THROW(lu = luBuilder.build()); storm::storage::SparseMatrixBuilder dinvBuilder(4, 4, 4); ASSERT_NO_THROW(dinvBuilder.addNextValue(0, 0, 1 / 1.1)); ASSERT_NO_THROW(dinvBuilder.addNextValue(1, 1, 1 / 0.5)); ASSERT_NO_THROW(dinvBuilder.addNextValue(2, 2, 1 / 0.99)); ASSERT_NO_THROW(dinvBuilder.addNextValue(3, 3, 1 / 0.11)); storm::storage::SparseMatrix dinv; ASSERT_NO_THROW(dinv = dinvBuilder.build()); ASSERT_TRUE(lu == jacobiDecomposition.first); ASSERT_TRUE(dinv == jacobiDecomposition.second); } TEST(SparseMatrix, PointwiseMultiplicationVector) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); storm::storage::SparseMatrixBuilder matrixBuilder2(5, 4, 9); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); ASSERT_NO_THROW(std::vector pointwiseProductRowSums = matrix.getPointwiseProductRowSumVector(matrix2)); std::vector pointwiseProductRowSums = matrix.getPointwiseProductRowSumVector(matrix2); std::vector correctResult = {1.0*1.0+1.2*1.2, 0.5*0.5+0.7*0.7, 0.5*0.5, 1.1*1.1, 0.1*0.1+0.2*0.2+0.3*0.3}; ASSERT_TRUE(pointwiseProductRowSums == correctResult); } TEST(SparseMatrix, MatrixVectorMultiply) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); std::vector x = {1, 0.3, 1.4, 7.1}; std::vector result(matrix.getRowCount()); ASSERT_NO_THROW(matrix.multiplyWithVector(x, result)); std::vector correctResult = {1.0*0.3+1.2*1.4, 0.5*1+0.7*0.3, 0.5*1, 1.1*1.4, 0.1*1+0.2*0.3+0.3*7.1}; ASSERT_TRUE(result == correctResult); } TEST(SparseMatrix, Iteration) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 9); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); for (auto const& entry : matrix.getRow(4)) { if (entry.first == 0) { ASSERT_EQ(0.1, entry.second); } else if (entry.first == 1) { ASSERT_EQ(0.2, entry.second); } else if (entry.first == 3) { ASSERT_EQ(0.3, entry.second); } else { ASSERT_TRUE(false); } } for (storm::storage::SparseMatrix::iterator it = matrix.begin(4), ite = matrix.end(4); it != ite; ++it) { if (it->first == 0) { ASSERT_EQ(0.1, it->second); } else if (it->first == 1) { ASSERT_EQ(0.2, it->second); } else if (it->first == 3) { ASSERT_EQ(0.3, it->second); } else { ASSERT_TRUE(false); } } } TEST(SparseMatrix, RowSum) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 8); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); ASSERT_EQ(0, matrix.getRowSum(2)); ASSERT_EQ(0.1+0.2+0.3, matrix.getRowSum(4)); } TEST(SparseMatrix, IsSubmatrix) { storm::storage::SparseMatrixBuilder matrixBuilder(5, 4, 8); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 1.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 1.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 1, 0.2)); ASSERT_NO_THROW(matrixBuilder.addNextValue(4, 3, 0.3)); storm::storage::SparseMatrix matrix; ASSERT_NO_THROW(matrix = matrixBuilder.build()); storm::storage::SparseMatrixBuilder matrixBuilder2(5, 4, 5); ASSERT_NO_THROW(matrixBuilder2.addNextValue(0, 1, 1.0)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder2.addNextValue(4, 1, 0.2)); storm::storage::SparseMatrix matrix2; ASSERT_NO_THROW(matrix2 = matrixBuilder2.build()); ASSERT_TRUE(matrix2.isSubmatrixOf(matrix)); storm::storage::SparseMatrixBuilder matrixBuilder3(5, 4, 5); ASSERT_NO_THROW(matrixBuilder3.addNextValue(0, 3, 1.0)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 0, 0.5)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(1, 1, 0.7)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(4, 0, 0.1)); ASSERT_NO_THROW(matrixBuilder3.addNextValue(4, 1, 0.2)); storm::storage::SparseMatrix matrix3; ASSERT_NO_THROW(matrix3 = matrixBuilder3.build()); ASSERT_FALSE(matrix3.isSubmatrixOf(matrix)); ASSERT_FALSE(matrix3.isSubmatrixOf(matrix2)); }