1 //=================================================================================================
2 /*!
3 // \file blazetest/mathtest/views/submatrix/SparseTest.h
4 // \brief Header file for the Submatrix sparse test
5 //
6 // Copyright (C) 2012-2020 Klaus Iglberger - All Rights Reserved
7 //
8 // This file is part of the Blaze library. You can redistribute it and/or modify it under
9 // the terms of the New (Revised) BSD License. Redistribution and use in source and binary
10 // forms, with or without modification, are permitted provided that the following conditions
11 // are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
16 // of conditions and the following disclaimer in the documentation and/or other materials
17 // provided with the distribution.
18 // 3. Neither the names of the Blaze development group nor the names of its contributors
19 // may be used to endorse or promote products derived from this software without specific
20 // prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 */
33 //=================================================================================================
34
35 #ifndef _BLAZETEST_MATHTEST_VIEWS_SUBMATRIX_SPARSETEST_H_
36 #define _BLAZETEST_MATHTEST_VIEWS_SUBMATRIX_SPARSETEST_H_
37
38
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42
43 #include <sstream>
44 #include <stdexcept>
45 #include <string>
46 #include <blaze/math/constraints/SparseMatrix.h>
47 #include <blaze/math/CompressedMatrix.h>
48 #include <blaze/math/Submatrix.h>
49 #include <blazetest/system/Types.h>
50
51
52 namespace blazetest {
53
54 namespace mathtest {
55
56 namespace views {
57
58 namespace submatrix {
59
60 //=================================================================================================
61 //
62 // CLASS DEFINITION
63 //
64 //=================================================================================================
65
66 //*************************************************************************************************
67 /*!\brief Auxiliary class for all tests of the sparse Submatrix specialization.
68 //
69 // This class represents a test suite for the blaze::Submatrix class template specialization for
70 // sparse submatrices. It performs a series of both compile time as well as runtime tests.
71 */
72 class SparseTest
73 {
74 public:
75 //**Constructors********************************************************************************
76 /*!\name Constructors */
77 //@{
78 explicit SparseTest();
79 // No explicitly declared copy constructor.
80 //@}
81 //**********************************************************************************************
82
83 //**Destructor**********************************************************************************
84 // No explicitly declared destructor.
85 //**********************************************************************************************
86
87 private:
88 //**Test functions******************************************************************************
89 /*!\name Test functions */
90 //@{
91 void testConstructors();
92 void testAssignment();
93 void testAddAssign();
94 void testSubAssign();
95 void testSchurAssign();
96 void testMultAssign();
97 void testScaling();
98 void testFunctionCall();
99 void testIterator();
100 void testNonZeros();
101 void testReset();
102 void testClear();
103 void testReserve();
104 void testTrim();
105 void testSet();
106 void testInsert();
107 void testAppend();
108 void testErase();
109 void testFind();
110 void testLowerBound();
111 void testUpperBound();
112 void testTranspose();
113 void testCTranspose();
114 void testIsDefault();
115 void testIsSame();
116 void testSubmatrix();
117 void testRow();
118 void testRows();
119 void testColumn();
120 void testColumns();
121 void testBand();
122
123 template< typename Type >
124 void checkRows( const Type& matrix, size_t expectedRows ) const;
125
126 template< typename Type >
127 void checkColumns( const Type& matrix, size_t expectedColumns ) const;
128
129 template< typename Type >
130 void checkCapacity( const Type& matrix, size_t minCapacity ) const;
131
132 template< typename Type >
133 void checkCapacity( const Type& matrix, size_t index, size_t minCapacity ) const;
134
135 template< typename Type >
136 void checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const;
137
138 template< typename Type >
139 void checkNonZeros( const Type& matrix, size_t index, size_t expectedNonZeros ) const;
140 //@}
141 //**********************************************************************************************
142
143 //**Utility functions***************************************************************************
144 /*!\name Utility functions */
145 //@{
146 void initialize();
147 //@}
148 //**********************************************************************************************
149
150 //**Type definitions****************************************************************************
151 using MT = blaze::CompressedMatrix<int,blaze::rowMajor>; //!< Row-major compressed matrix type
152 using OMT = MT::OppositeType; //!< Column-major compressed matrix type
153 using SMT = blaze::Submatrix<MT>; //!< Sparse submatrix type for row-major matrices.
154 using OSMT = blaze::Submatrix<OMT>; //!< Sparse submatrix type for column-major matrices.
155 //**********************************************************************************************
156
157 //**Member variables****************************************************************************
158 /*!\name Member variables */
159 //@{
160 MT mat_; //!< Row-major compressed matrix.
161 /*!< The \f$ 5 \times 4 \f$ matrix is initialized as
162 \f[\left(\begin{array}{*{4}{c}}
163 0 & 0 & 0 & 0 \\
164 0 & 1 & 0 & 0 \\
165 -2 & 0 & -3 & 0 \\
166 0 & 4 & 5 & -6 \\
167 7 & -8 & 9 & 10 \\
168 \end{array}\right)\f]. */
169 OMT tmat_; //!< Column-major compressed matrix.
170 /*!< The \f$ 4 \times 5 \f$ matrix is initialized as
171 \f[\left(\begin{array}{*{4}{c}}
172 0 & 0 & -2 & 0 & 7 \\
173 0 & 1 & 0 & 4 & -8 \\
174 0 & 0 & -3 & 5 & 9 \\
175 0 & 0 & 0 & -6 & 10 \\
176 \end{array}\right)\f]. */
177
178 std::string test_; //!< Label of the currently performed test.
179 //@}
180 //**********************************************************************************************
181
182 //**Compile time checks*************************************************************************
183 /*! \cond BLAZE_INTERNAL */
184 BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE( MT );
185 BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE( OMT );
186 BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE( SMT );
187 BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE( OSMT );
188 /*! \endcond */
189 //**********************************************************************************************
190 };
191 //*************************************************************************************************
192
193
194
195
196 //=================================================================================================
197 //
198 // TEST FUNCTIONS
199 //
200 //=================================================================================================
201
202 //*************************************************************************************************
203 /*!\brief Checking the number of rows of the given sparse matrix.
204 //
205 // \param matrix The sparse matrix to be checked.
206 // \param expectedRows The expected number of rows of the sparse matrix.
207 // \return void
208 // \exception std::runtime_error Error detected.
209 //
210 // This function checks the number of rows of the given sparse matrix. In case the
211 // actual number of rows does not correspond to the given expected number of rows, a
212 // \a std::runtime_error exception is thrown.
213 */
214 template< typename Type > // Type of the sparse matrix
checkRows(const Type & matrix,size_t expectedRows)215 void SparseTest::checkRows( const Type& matrix, size_t expectedRows ) const
216 {
217 if( rows( matrix ) != expectedRows ) {
218 std::ostringstream oss;
219 oss << " Test: " << test_ << "\n"
220 << " Error: Invalid number of rows detected\n"
221 << " Details:\n"
222 << " Number of rows : " << rows( matrix ) << "\n"
223 << " Expected number of rows: " << expectedRows << "\n";
224 throw std::runtime_error( oss.str() );
225 }
226 }
227 //*************************************************************************************************
228
229
230 //*************************************************************************************************
231 /*!\brief Checking the number of columns of the given sparse matrix.
232 //
233 // \param matrix The sparse matrix to be checked.
234 // \param expectedColumns The expected number of columns of the sparse matrix.
235 // \return void
236 // \exception std::runtime_error Error detected.
237 //
238 // This function checks the number of columns of the given sparse matrix. In case the
239 // actual number of columns does not correspond to the given expected number of columns,
240 // a \a std::runtime_error exception is thrown.
241 */
242 template< typename Type > // Type of the sparse matrix
checkColumns(const Type & matrix,size_t expectedColumns)243 void SparseTest::checkColumns( const Type& matrix, size_t expectedColumns ) const
244 {
245 if( columns( matrix ) != expectedColumns ) {
246 std::ostringstream oss;
247 oss << " Test: " << test_ << "\n"
248 << " Error: Invalid number of columns detected\n"
249 << " Details:\n"
250 << " Number of columns : " << columns( matrix ) << "\n"
251 << " Expected number of columns: " << expectedColumns << "\n";
252 throw std::runtime_error( oss.str() );
253 }
254 }
255 //*************************************************************************************************
256
257
258 //*************************************************************************************************
259 /*!\brief Checking the capacity of the given sparse matrix.
260 //
261 // \param matrix The sparse matrix to be checked.
262 // \param minCapacity The expected minimum capacity of the sparse matrix.
263 // \return void
264 // \exception std::runtime_error Error detected.
265 //
266 // This function checks the capacity of the given sparse matrix. In case the actual capacity
267 // is smaller than the given expected minimum capacity, a \a std::runtime_error exception is
268 // thrown.
269 */
270 template< typename Type > // Type of the sparse matrix
checkCapacity(const Type & matrix,size_t minCapacity)271 void SparseTest::checkCapacity( const Type& matrix, size_t minCapacity ) const
272 {
273 if( capacity( matrix ) < minCapacity ) {
274 std::ostringstream oss;
275 oss << " Test: " << test_ << "\n"
276 << " Error: Invalid capacity detected\n"
277 << " Details:\n"
278 << " Capacity : " << capacity( matrix ) << "\n"
279 << " Expected minimum capacity: " << minCapacity << "\n";
280 throw std::runtime_error( oss.str() );
281 }
282 }
283 //*************************************************************************************************
284
285
286 //*************************************************************************************************
287 /*!\brief Checking the capacity of a specific row/column of the given sparse matrix.
288 //
289 // \param matrix The sparse matrix to be checked.
290 // \param index The row/column to be checked.
291 // \param minCapacity The expected minimum capacity of the specified row/column.
292 // \return void
293 // \exception std::runtime_error Error detected.
294 //
295 // This function checks the capacity of a specific row/column of the given sparse matrix.
296 // In case the actual capacity is smaller than the given expected minimum capacity, a
297 // \a std::runtime_error exception is thrown.
298 */
299 template< typename Type > // Type of the compressed matrix
checkCapacity(const Type & matrix,size_t index,size_t minCapacity)300 void SparseTest::checkCapacity( const Type& matrix, size_t index, size_t minCapacity ) const
301 {
302 if( capacity( matrix, index ) < minCapacity ) {
303 std::ostringstream oss;
304 oss << " Test: " << test_ << "\n"
305 << " Error: Invalid capacity detected in "
306 << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
307 << " Details:\n"
308 << " Capacity : " << capacity( matrix, index ) << "\n"
309 << " Expected minimum capacity: " << minCapacity << "\n";
310 throw std::runtime_error( oss.str() );
311 }
312 }
313 //*************************************************************************************************
314
315
316 //*************************************************************************************************
317 /*!\brief Checking the number of non-zero elements of the given sparse matrix.
318 //
319 // \param matrix The sparse matrix to be checked.
320 // \param expectedNonZeros The expected number of non-zero elements of the sparse matrix.
321 // \return void
322 // \exception std::runtime_error Error detected.
323 //
324 // This function checks the number of non-zero elements of the given sparse matrix. In
325 // case the actual number of non-zero elements does not correspond to the given expected
326 // number, a \a std::runtime_error exception is thrown.
327 */
328 template< typename Type > // Type of the sparse matrix
checkNonZeros(const Type & matrix,size_t expectedNonZeros)329 void SparseTest::checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const
330 {
331 if( nonZeros( matrix ) != expectedNonZeros ) {
332 std::ostringstream oss;
333 oss << " Test: " << test_ << "\n"
334 << " Error: Invalid number of non-zero elements\n"
335 << " Details:\n"
336 << " Number of non-zeros : " << nonZeros( matrix ) << "\n"
337 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
338 throw std::runtime_error( oss.str() );
339 }
340
341 if( capacity( matrix ) < nonZeros( matrix ) ) {
342 std::ostringstream oss;
343 oss << " Test: " << test_ << "\n"
344 << " Error: Invalid capacity detected\n"
345 << " Details:\n"
346 << " Number of non-zeros: " << nonZeros( matrix ) << "\n"
347 << " Capacity : " << capacity( matrix ) << "\n";
348 throw std::runtime_error( oss.str() );
349 }
350 }
351 //*************************************************************************************************
352
353
354 //*************************************************************************************************
355 /*!\brief Checking the number of non-zero elements in a specific row/column of the given sparse matrix.
356 //
357 // \param matrix The sparse matrix to be checked.
358 // \param index The row/column to be checked.
359 // \param expectedNonZeros The expected number of non-zero elements in the specified row/column.
360 // \return void
361 // \exception std::runtime_error Error detected.
362 //
363 // This function checks the number of non-zero elements in the specified row/column of the
364 // given sparse matrix. In case the actual number of non-zero elements does not correspond
365 // to the given expected number, a \a std::runtime_error exception is thrown.
366 */
367 template< typename Type > // Type of the sparse matrix
checkNonZeros(const Type & matrix,size_t index,size_t expectedNonZeros)368 void SparseTest::checkNonZeros( const Type& matrix, size_t index, size_t expectedNonZeros ) const
369 {
370 if( nonZeros( matrix, index ) != expectedNonZeros ) {
371 std::ostringstream oss;
372 oss << " Test: " << test_ << "\n"
373 << " Error: Invalid number of non-zero elements in "
374 << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
375 << " Details:\n"
376 << " Number of non-zeros : " << nonZeros( matrix, index ) << "\n"
377 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
378 throw std::runtime_error( oss.str() );
379 }
380
381 if( capacity( matrix, index ) < nonZeros( matrix, index ) ) {
382 std::ostringstream oss;
383 oss << " Test: " << test_ << "\n"
384 << " Error: Invalid capacity detected in "
385 << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
386 << " Details:\n"
387 << " Number of non-zeros: " << nonZeros( matrix, index ) << "\n"
388 << " Capacity : " << capacity( matrix, index ) << "\n";
389 throw std::runtime_error( oss.str() );
390 }
391 }
392 //*************************************************************************************************
393
394
395
396
397 //=================================================================================================
398 //
399 // GLOBAL TEST FUNCTIONS
400 //
401 //=================================================================================================
402
403 //*************************************************************************************************
404 /*!\brief Testing the functionality of the sparse Submatrix specialization.
405 //
406 // \return void
407 */
runTest()408 void runTest()
409 {
410 SparseTest();
411 }
412 //*************************************************************************************************
413
414
415
416
417 //=================================================================================================
418 //
419 // MACRO DEFINITIONS
420 //
421 //=================================================================================================
422
423 //*************************************************************************************************
424 /*! \cond BLAZE_INTERNAL */
425 /*!\brief Macro for the execution of the Submatrix sparse test.
426 */
427 #define RUN_SUBMATRIX_SPARSE_TEST \
428 blazetest::mathtest::views::submatrix::runTest()
429 /*! \endcond */
430 //*************************************************************************************************
431
432 } // namespace submatrix
433
434 } // namespace views
435
436 } // namespace mathtest
437
438 } // namespace blazetest
439
440 #endif
441