1 //=================================================================================================
2 /*!
3 // \file blazetest/mathtest/adaptors/diagonalmatrix/ColumnTest.h
4 // \brief Header file for the DiagonalMatrix column 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_ADAPTORS_DIAGONALMATRIX_COLUMNTEST_H_
36 #define _BLAZETEST_MATHTEST_ADAPTORS_DIAGONALMATRIX_COLUMNTEST_H_
37
38
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42
43 #include <sstream>
44 #include <stdexcept>
45 #include <string>
46 #include <blaze/math/CompressedMatrix.h>
47 #include <blaze/math/CompressedVector.h>
48 #include <blaze/math/Column.h>
49 #include <blaze/math/DynamicMatrix.h>
50 #include <blaze/math/DynamicVector.h>
51 #include <blaze/math/DiagonalMatrix.h>
52 #include <blaze/math/typetraits/IsRowMajorMatrix.h>
53 #include <blazetest/system/Types.h>
54
55
56 namespace blazetest {
57
58 namespace mathtest {
59
60 namespace adaptors {
61
62 namespace diagonalmatrix {
63
64 //=================================================================================================
65 //
66 // CLASS DEFINITION
67 //
68 //=================================================================================================
69
70 //*************************************************************************************************
71 /*!\brief Auxiliary class for assignment tests to a single column of a DiagonalMatrix.
72 //
73 // This class performs assignment tests to a single column of a DiagonalMatrix. It performs a series
74 // of both compile time as well as runtime tests.
75 */
76 class ColumnTest
77 {
78 private:
79 //**Type definitions****************************************************************************
80 //! Type of the dense diagonal matrix.
81 using DDT = blaze::DiagonalMatrix< blaze::DynamicMatrix<int,blaze::rowMajor> >;
82
83 //! Opposite dense diagonal matrix type.
84 using DODT = DDT::OppositeType;
85
86 //! Type of the sparse diagonal matrix.
87 using SDT = blaze::DiagonalMatrix< blaze::CompressedMatrix<int,blaze::rowMajor> >;
88
89 //! Opposite sparse diagonal matrix type.
90 using SODT = SDT::OppositeType;
91 //**********************************************************************************************
92
93 public:
94 //**Constructors********************************************************************************
95 /*!\name Constructors */
96 //@{
97 explicit ColumnTest();
98 // No explicitly declared copy constructor.
99 //@}
100 //**********************************************************************************************
101
102 //**Destructor**********************************************************************************
103 // No explicitly declared destructor.
104 //**********************************************************************************************
105
106 private:
107 //**Test functions******************************************************************************
108 /*!\name Test functions */
109 //@{
110 template< typename DT > void testAssignment();
111 template< typename DT > void testAddAssign ();
112 template< typename DT > void testSubAssign ();
113 template< typename DT > void testMultAssign();
114
115 template< typename Type >
116 void checkRows( const Type& matrix, size_t expectedRows ) const;
117
118 template< typename Type >
119 void checkColumns( const Type& matrix, size_t expectedColumns ) const;
120
121 template< typename Type >
122 void checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const;
123 //@}
124 //**********************************************************************************************
125
126 //**Utility functions***************************************************************************
127 /*!\name Utility functions */
128 //@{
129 template< typename DT > void init( DT& diag );
130 //@}
131 //**********************************************************************************************
132
133 //**Member variables****************************************************************************
134 /*!\name Member variables */
135 //@{
136 std::string test_; //!< Label of the currently performed test.
137 //@}
138 //**********************************************************************************************
139 };
140 //*************************************************************************************************
141
142
143
144
145 //=================================================================================================
146 //
147 // TEST FUNCTIONS
148 //
149 //=================================================================================================
150
151 //*************************************************************************************************
152 /*!\brief Test of the assignment to columns of a DiagonalMatrix.
153 //
154 // \return void
155 // \exception std::runtime_error Error detected.
156 //
157 // This function performs a test of the assignment to a single column of a DiagonalMatrix. In case
158 // an error is detected, a \a std::runtime_error exception is thrown.
159 */
160 template< typename DT > // Type of the diagonal matrix
testAssignment()161 void ColumnTest::testAssignment()
162 {
163 //=====================================================================================
164 // Dense vector assignment
165 //=====================================================================================
166
167 // ( 1 0 0 ) ( 1 0 0 )
168 // ( 0 2 0 ) => ( 0 8 0 )
169 // ( 0 0 3 ) ( 0 0 3 )
170 {
171 test_ = "Dense vector assignment test 1";
172
173 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
174 vec[1] = 8;
175
176 DT diag;
177 init( diag );
178
179 auto col1 = column( diag, 1UL );
180 col1 = vec;
181
182 checkRows ( diag, 3UL );
183 checkColumns ( diag, 3UL );
184 checkNonZeros( diag, 3UL );
185
186 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
187 std::ostringstream oss;
188 oss << " Test: " << test_ << "\n"
189 << " Error: Assignment to column failed\n"
190 << " Details:\n"
191 << " Result:\n" << col1 << "\n"
192 << " Expected result:\n( 0 8 0 )\n";
193 throw std::runtime_error( oss.str() );
194 }
195
196 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
197 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
198 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
199 std::ostringstream oss;
200 oss << " Test: " << test_ << "\n"
201 << " Error: Assignment to column failed\n"
202 << " Details:\n"
203 << " Result:\n" << diag << "\n"
204 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
205 throw std::runtime_error( oss.str() );
206 }
207 }
208
209 // ( 1 0 0 ) ( 1 9 0 )
210 // ( 0 2 0 ) => ( 0 8 0 )
211 // ( 0 0 3 ) ( 0 0 3 )
212 {
213 test_ = "Dense vector assignment test 2";
214
215 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
216 vec[0] = 9;
217 vec[1] = 8;
218
219 DT diag;
220 init( diag );
221
222 auto col1 = column( diag, 1UL );
223
224 try {
225 col1 = vec;
226
227 std::ostringstream oss;
228 oss << " Test: " << test_ << "\n"
229 << " Error: Assignment of invalid vector succeeded\n"
230 << " Details:\n"
231 << " Result:\n" << diag << "\n";
232 throw std::runtime_error( oss.str() );
233 }
234 catch( std::invalid_argument& ) {}
235 }
236
237 // ( 1 0 0 ) ( 1 0 0 )
238 // ( 0 2 0 ) => ( 0 8 0 )
239 // ( 0 0 3 ) ( 0 2 3 )
240 {
241 test_ = "Dense vector assignment test 3";
242
243 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
244 vec[1] = 8;
245 vec[2] = 2;
246
247 DT diag;
248 init( diag );
249
250 auto col1 = column( diag, 1UL );
251
252 try {
253 col1 = vec;
254
255 std::ostringstream oss;
256 oss << " Test: " << test_ << "\n"
257 << " Error: Assignment of invalid vector succeeded\n"
258 << " Details:\n"
259 << " Result:\n" << diag << "\n";
260 throw std::runtime_error( oss.str() );
261 }
262 catch( std::invalid_argument& ) {}
263 }
264
265
266 //=====================================================================================
267 // Sparse vector assignment
268 //=====================================================================================
269
270 // ( 1 0 0 ) ( 1 0 0 )
271 // ( 0 2 0 ) => ( 0 8 0 )
272 // ( 0 0 3 ) ( 0 0 3 )
273 {
274 test_ = "Sparse vector assignment test 1";
275
276 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
277 vec[1] = 8;
278 vec.insert( 0UL, 0 );
279 vec.insert( 2UL, 0 );
280
281 DT diag;
282 init( diag );
283
284 auto col1 = column( diag, 1UL );
285 col1 = vec;
286
287 checkRows ( diag, 3UL );
288 checkColumns ( diag, 3UL );
289 checkNonZeros( diag, 3UL );
290
291 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
292 std::ostringstream oss;
293 oss << " Test: " << test_ << "\n"
294 << " Error: Assignment to column failed\n"
295 << " Details:\n"
296 << " Result:\n" << col1 << "\n"
297 << " Expected result:\n( 0 8 0 )\n";
298 throw std::runtime_error( oss.str() );
299 }
300
301 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
302 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
303 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
304 std::ostringstream oss;
305 oss << " Test: " << test_ << "\n"
306 << " Error: Assignment to column failed\n"
307 << " Details:\n"
308 << " Result:\n" << diag << "\n"
309 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
310 throw std::runtime_error( oss.str() );
311 }
312 }
313
314 // ( 1 0 0 ) ( 1 9 0 )
315 // ( 0 2 0 ) => ( 0 8 0 )
316 // ( 0 0 3 ) ( 0 0 3 )
317 {
318 test_ = "Sparse vector assignment test 2";
319
320 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
321 vec[0] = 9;
322 vec[1] = 8;
323 vec.insert( 2UL, 0 );
324
325 DT diag;
326 init( diag );
327
328 auto col1 = column( diag, 1UL );
329
330 try {
331 col1 = vec;
332
333 std::ostringstream oss;
334 oss << " Test: " << test_ << "\n"
335 << " Error: Assignment of invalid vector succeeded\n"
336 << " Details:\n"
337 << " Result:\n" << diag << "\n";
338 throw std::runtime_error( oss.str() );
339 }
340 catch( std::invalid_argument& ) {}
341 }
342
343 // ( 1 0 0 ) ( 1 0 0 )
344 // ( 0 2 0 ) => ( 0 8 0 )
345 // ( 0 0 3 ) ( 0 2 3 )
346 {
347 test_ = "Sparse vector assignment test 3";
348
349 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
350 vec[1] = 8;
351 vec[2] = 2;
352 vec.insert( 0UL, 0 );
353
354 DT diag;
355 init( diag );
356
357 auto col1 = column( diag, 1UL );
358
359 try {
360 col1 = vec;
361
362 std::ostringstream oss;
363 oss << " Test: " << test_ << "\n"
364 << " Error: Assignment of invalid vector succeeded\n"
365 << " Details:\n"
366 << " Result:\n" << diag << "\n";
367 throw std::runtime_error( oss.str() );
368 }
369 catch( std::invalid_argument& ) {}
370 }
371 }
372 //*************************************************************************************************
373
374
375 //*************************************************************************************************
376 /*!\brief Test of the addition assignment to columns of a DiagonalMatrix.
377 //
378 // \return void
379 // \exception std::runtime_error Error detected.
380 //
381 // This function performs a test of the addition assignment to a single column of a DiagonalMatrix.
382 // In case an error is detected, a \a std::runtime_error exception is thrown.
383 */
384 template< typename DT > // Type of the diagonal matrix
testAddAssign()385 void ColumnTest::testAddAssign()
386 {
387 //=====================================================================================
388 // Dense vector addition assignment
389 //=====================================================================================
390
391 // ( 1 0 0 ) ( 1 0 0 )
392 // ( 0 2 0 ) => ( 0 8 0 )
393 // ( 0 0 3 ) ( 0 0 3 )
394 {
395 test_ = "Dense vector addition assignment test 1";
396
397 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
398 vec[1] = 6;
399
400 DT diag;
401 init( diag );
402
403 auto col1 = column( diag, 1UL );
404 col1 += vec;
405
406 checkRows ( diag, 3UL );
407 checkColumns ( diag, 3UL );
408 checkNonZeros( diag, 3UL );
409
410 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
411 std::ostringstream oss;
412 oss << " Test: " << test_ << "\n"
413 << " Error: Assignment to column failed\n"
414 << " Details:\n"
415 << " Result:\n" << col1 << "\n"
416 << " Expected result:\n( 0 8 0 )\n";
417 throw std::runtime_error( oss.str() );
418 }
419
420 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
421 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
422 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
423 std::ostringstream oss;
424 oss << " Test: " << test_ << "\n"
425 << " Error: Assignment to column failed\n"
426 << " Details:\n"
427 << " Result:\n" << diag << "\n"
428 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
429 throw std::runtime_error( oss.str() );
430 }
431 }
432
433 // ( 1 0 0 ) ( 1 9 0 )
434 // ( 0 2 0 ) => ( 0 8 0 )
435 // ( 0 0 3 ) ( 0 0 3 )
436 {
437 test_ = "Dense vector addition assignment test 2";
438
439 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
440 vec[0] = 9;
441 vec[1] = 6;
442
443 DT diag;
444 init( diag );
445
446 auto col1 = column( diag, 1UL );
447
448 try {
449 col1 += vec;
450
451 std::ostringstream oss;
452 oss << " Test: " << test_ << "\n"
453 << " Error: Assignment of invalid vector succeeded\n"
454 << " Details:\n"
455 << " Result:\n" << diag << "\n";
456 throw std::runtime_error( oss.str() );
457 }
458 catch( std::invalid_argument& ) {}
459 }
460
461 // ( 1 0 0 ) ( 1 0 0 )
462 // ( 0 2 0 ) => ( 0 8 0 )
463 // ( 0 0 3 ) ( 0 2 3 )
464 {
465 test_ = "Dense vector addition assignment test 3";
466
467 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
468 vec[1] = 6;
469 vec[2] = 2;
470
471 DT diag;
472 init( diag );
473
474 auto col1 = column( diag, 1UL );
475
476 try {
477 col1 += vec;
478
479 std::ostringstream oss;
480 oss << " Test: " << test_ << "\n"
481 << " Error: Assignment of invalid vector succeeded\n"
482 << " Details:\n"
483 << " Result:\n" << diag << "\n";
484 throw std::runtime_error( oss.str() );
485 }
486 catch( std::invalid_argument& ) {}
487 }
488
489
490 //=====================================================================================
491 // Sparse vector addition assignment
492 //=====================================================================================
493
494 // ( 1 0 0 ) ( 1 0 0 )
495 // ( 0 2 0 ) => ( 0 8 0 )
496 // ( 0 0 3 ) ( 0 0 3 )
497 {
498 test_ = "Sparse vector addition assignment test 1";
499
500 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
501 vec[1] = 6;
502 vec.insert( 0UL, 0 );
503 vec.insert( 2UL, 0 );
504
505 DT diag;
506 init( diag );
507
508 auto col1 = column( diag, 1UL );
509 col1 += vec;
510
511 checkRows ( diag, 3UL );
512 checkColumns ( diag, 3UL );
513 checkNonZeros( diag, 3UL );
514
515 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
516 std::ostringstream oss;
517 oss << " Test: " << test_ << "\n"
518 << " Error: Assignment to column failed\n"
519 << " Details:\n"
520 << " Result:\n" << col1 << "\n"
521 << " Expected result:\n( 0 8 0 )\n";
522 throw std::runtime_error( oss.str() );
523 }
524
525 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
526 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
527 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
528 std::ostringstream oss;
529 oss << " Test: " << test_ << "\n"
530 << " Error: Assignment to column failed\n"
531 << " Details:\n"
532 << " Result:\n" << diag << "\n"
533 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
534 throw std::runtime_error( oss.str() );
535 }
536 }
537
538 // ( 1 0 0 ) ( 1 9 0 )
539 // ( 0 2 0 ) => ( 0 8 0 )
540 // ( 0 0 3 ) ( 0 0 3 )
541 {
542 test_ = "Sparse vector addition assignment test 2";
543
544 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
545 vec[0] = 9;
546 vec[1] = 6;
547 vec.insert( 2UL, 0 );
548
549 DT diag;
550 init( diag );
551
552 auto col1 = column( diag, 1UL );
553
554 try {
555 col1 += vec;
556
557 std::ostringstream oss;
558 oss << " Test: " << test_ << "\n"
559 << " Error: Assignment of invalid vector succeeded\n"
560 << " Details:\n"
561 << " Result:\n" << diag << "\n";
562 throw std::runtime_error( oss.str() );
563 }
564 catch( std::invalid_argument& ) {}
565 }
566
567 // ( 1 0 0 ) ( 1 0 0 )
568 // ( 0 2 0 ) => ( 0 8 0 )
569 // ( 0 0 3 ) ( 0 2 3 )
570 {
571 test_ = "Sparse vector addition assignment test 3";
572
573 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
574 vec[1] = 6;
575 vec[2] = 2;
576 vec.insert( 0UL, 0 );
577
578 DT diag;
579 init( diag );
580
581 auto col1 = column( diag, 1UL );
582
583 try {
584 col1 += vec;
585
586 std::ostringstream oss;
587 oss << " Test: " << test_ << "\n"
588 << " Error: Assignment of invalid vector succeeded\n"
589 << " Details:\n"
590 << " Result:\n" << diag << "\n";
591 throw std::runtime_error( oss.str() );
592 }
593 catch( std::invalid_argument& ) {}
594 }
595 }
596 //*************************************************************************************************
597
598
599 //*************************************************************************************************
600 /*!\brief Test of the subtraction assignment to columns of a DiagonalMatrix.
601 //
602 // \return void
603 // \exception std::runtime_error Error detected.
604 //
605 // This function performs a test of the subtraction assignment to a single column of a
606 // DiagonalMatrix. In case an error is detected, a \a std::runtime_error exception is thrown.
607 */
608 template< typename DT > // Type of the diagonal matrix
testSubAssign()609 void ColumnTest::testSubAssign()
610 {
611 //=====================================================================================
612 // Dense vector subtraction assignment
613 //=====================================================================================
614
615 // ( 1 0 0 ) ( 1 0 0 )
616 // ( 0 2 0 ) => ( 0 8 0 )
617 // ( 0 0 3 ) ( 0 0 3 )
618 {
619 test_ = "Dense vector subtraction assignment test 1";
620
621 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
622 vec[1] = -6;
623
624 DT diag;
625 init( diag );
626
627 auto col1 = column( diag, 1UL );
628 col1 -= vec;
629
630 checkRows ( diag, 3UL );
631 checkColumns ( diag, 3UL );
632 checkNonZeros( diag, 3UL );
633
634 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
635 std::ostringstream oss;
636 oss << " Test: " << test_ << "\n"
637 << " Error: Assignment to column failed\n"
638 << " Details:\n"
639 << " Result:\n" << col1 << "\n"
640 << " Expected result:\n( 0 8 0 )\n";
641 throw std::runtime_error( oss.str() );
642 }
643
644 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
645 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
646 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
647 std::ostringstream oss;
648 oss << " Test: " << test_ << "\n"
649 << " Error: Assignment to column failed\n"
650 << " Details:\n"
651 << " Result:\n" << diag << "\n"
652 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
653 throw std::runtime_error( oss.str() );
654 }
655 }
656
657 // ( 1 0 0 ) ( 1 9 0 )
658 // ( 0 2 0 ) => ( 0 8 0 )
659 // ( 0 0 3 ) ( 0 0 3 )
660 {
661 test_ = "Dense vector subtraction assignment test 2";
662
663 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
664 vec[0] = -9;
665 vec[1] = -6;
666
667 DT diag;
668 init( diag );
669
670 auto col1 = column( diag, 1UL );
671
672 try {
673 col1 -= vec;
674
675 std::ostringstream oss;
676 oss << " Test: " << test_ << "\n"
677 << " Error: Assignment of invalid vector succeeded\n"
678 << " Details:\n"
679 << " Result:\n" << diag << "\n";
680 throw std::runtime_error( oss.str() );
681 }
682 catch( std::invalid_argument& ) {}
683 }
684
685 // ( 1 0 0 ) ( 1 0 0 )
686 // ( 0 2 0 ) => ( 0 8 0 )
687 // ( 0 0 3 ) ( 0 2 3 )
688 {
689 test_ = "Dense vector subtraction assignment test 3";
690
691 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
692 vec[1] = -6;
693 vec[2] = -2;
694
695 DT diag;
696 init( diag );
697
698 auto col1 = column( diag, 1UL );
699
700 try {
701 col1 -= vec;
702
703 std::ostringstream oss;
704 oss << " Test: " << test_ << "\n"
705 << " Error: Assignment of invalid vector succeeded\n"
706 << " Details:\n"
707 << " Result:\n" << diag << "\n";
708 throw std::runtime_error( oss.str() );
709 }
710 catch( std::invalid_argument& ) {}
711 }
712
713
714 //=====================================================================================
715 // Sparse vector subtraction assignment
716 //=====================================================================================
717
718 // ( 1 0 0 ) ( 1 0 0 )
719 // ( 0 2 0 ) => ( 0 8 0 )
720 // ( 0 0 3 ) ( 0 0 3 )
721 {
722 test_ = "Sparse vector subtraction assignment test 1";
723
724 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
725 vec[1] = -6;
726 vec.insert( 0UL, 0 );
727 vec.insert( 2UL, 0 );
728
729 DT diag;
730 init( diag );
731
732 auto col1 = column( diag, 1UL );
733 col1 -= vec;
734
735 checkRows ( diag, 3UL );
736 checkColumns ( diag, 3UL );
737 checkNonZeros( diag, 3UL );
738
739 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
740 std::ostringstream oss;
741 oss << " Test: " << test_ << "\n"
742 << " Error: Assignment to column failed\n"
743 << " Details:\n"
744 << " Result:\n" << col1 << "\n"
745 << " Expected result:\n( 0 8 0 )\n";
746 throw std::runtime_error( oss.str() );
747 }
748
749 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
750 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
751 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
752 std::ostringstream oss;
753 oss << " Test: " << test_ << "\n"
754 << " Error: Assignment to column failed\n"
755 << " Details:\n"
756 << " Result:\n" << diag << "\n"
757 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
758 throw std::runtime_error( oss.str() );
759 }
760 }
761
762 // ( 1 0 0 ) ( 1 9 0 )
763 // ( 0 2 0 ) => ( 0 8 0 )
764 // ( 0 0 3 ) ( 0 0 3 )
765 {
766 test_ = "Sparse vector subtraction assignment test 2";
767
768 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
769 vec[0] = -9;
770 vec[1] = -6;
771 vec.insert( 2UL, 0 );
772
773 DT diag;
774 init( diag );
775
776 auto col1 = column( diag, 1UL );
777
778 try {
779 col1 -= vec;
780
781 std::ostringstream oss;
782 oss << " Test: " << test_ << "\n"
783 << " Error: Assignment of invalid vector succeeded\n"
784 << " Details:\n"
785 << " Result:\n" << diag << "\n";
786 throw std::runtime_error( oss.str() );
787 }
788 catch( std::invalid_argument& ) {}
789 }
790
791 // ( 1 0 0 ) ( 1 0 0 )
792 // ( 0 2 0 ) => ( 0 8 0 )
793 // ( 0 0 3 ) ( 0 2 3 )
794 {
795 test_ = "Sparse vector subtraction assignment test 3";
796
797 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
798 vec[1] = -6;
799 vec[2] = -2;
800 vec.insert( 0UL, 0 );
801
802 DT diag;
803 init( diag );
804
805 auto col1 = column( diag, 1UL );
806
807 try {
808 col1 -= vec;
809
810 std::ostringstream oss;
811 oss << " Test: " << test_ << "\n"
812 << " Error: Assignment of invalid vector succeeded\n"
813 << " Details:\n"
814 << " Result:\n" << diag << "\n";
815 throw std::runtime_error( oss.str() );
816 }
817 catch( std::invalid_argument& ) {}
818 }
819 }
820 //*************************************************************************************************
821
822
823 //*************************************************************************************************
824 /*!\brief Test of the multiplication assignment to columns of a DiagonalMatrix.
825 //
826 // \return void
827 // \exception std::runtime_error Error detected.
828 //
829 // This function performs a test of the multiplication assignment to a single column of a
830 // DiagonalMatrix. In case an error is detected, a \a std::runtime_error exception is thrown.
831 */
832 template< typename DT > // Type of the diagonal matrix
testMultAssign()833 void ColumnTest::testMultAssign()
834 {
835 //=====================================================================================
836 // Dense vector multiplication assignment
837 //=====================================================================================
838
839 // ( 1 0 0 ) ( 1 0 0 )
840 // ( 0 2 0 ) => ( 0 8 0 )
841 // ( 0 0 3 ) ( 0 0 3 )
842 {
843 test_ = "Dense vector multiplication assignment test";
844
845 blaze::DynamicVector<int,blaze::columnVector> vec( 3UL );
846 vec[0] = 9;
847 vec[1] = 4;
848 vec[2] = 2;
849
850 DT diag;
851 init( diag );
852
853 auto col1 = column( diag, 1UL );
854 col1 *= vec;
855
856 checkRows ( diag, 3UL );
857 checkColumns ( diag, 3UL );
858 checkNonZeros( diag, 3UL );
859
860 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
861 std::ostringstream oss;
862 oss << " Test: " << test_ << "\n"
863 << " Error: Assignment to column failed\n"
864 << " Details:\n"
865 << " Result:\n" << col1 << "\n"
866 << " Expected result:\n( 0 8 0 )\n";
867 throw std::runtime_error( oss.str() );
868 }
869
870 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
871 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
872 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
873 std::ostringstream oss;
874 oss << " Test: " << test_ << "\n"
875 << " Error: Assignment to column failed\n"
876 << " Details:\n"
877 << " Result:\n" << diag << "\n"
878 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
879 throw std::runtime_error( oss.str() );
880 }
881 }
882
883
884 //=====================================================================================
885 // Sparse vector multiplication assignment
886 //=====================================================================================
887
888 // ( 1 0 0 ) ( 1 0 0 )
889 // ( 0 2 0 ) => ( 0 8 0 )
890 // ( 0 0 3 ) ( 0 0 3 )
891 {
892 test_ = "Sparse vector multiplication assignment test";
893
894 blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
895 vec[0] = 9;
896 vec[1] = 4;
897 vec[2] = 2;
898
899 DT diag;
900 init( diag );
901
902 auto col1 = column( diag, 1UL );
903 col1 *= vec;
904
905 checkRows ( diag, 3UL );
906 checkColumns ( diag, 3UL );
907 checkNonZeros( diag, 3UL );
908
909 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
910 std::ostringstream oss;
911 oss << " Test: " << test_ << "\n"
912 << " Error: Assignment to column failed\n"
913 << " Details:\n"
914 << " Result:\n" << col1 << "\n"
915 << " Expected result:\n( 0 8 0 )\n";
916 throw std::runtime_error( oss.str() );
917 }
918
919 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
920 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
921 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
922 std::ostringstream oss;
923 oss << " Test: " << test_ << "\n"
924 << " Error: Assignment to column failed\n"
925 << " Details:\n"
926 << " Result:\n" << diag << "\n"
927 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
928 throw std::runtime_error( oss.str() );
929 }
930 }
931 }
932 //*************************************************************************************************
933
934
935 //*************************************************************************************************
936 /*!\brief Checking the number of rows of the given matrix.
937 //
938 // \param matrix The matrix to be checked.
939 // \param expectedRows The expected number of rows of the matrix.
940 // \return void
941 // \exception std::runtime_error Error detected.
942 //
943 // This function checks the number of rows of the given matrix. In case the actual number of
944 // rows does not correspond to the given expected number of rows, a \a std::runtime_error
945 // exception is thrown.
946 */
947 template< typename Type > // Type of the matrix
checkRows(const Type & matrix,size_t expectedRows)948 void ColumnTest::checkRows( const Type& matrix, size_t expectedRows ) const
949 {
950 if( matrix.rows() != expectedRows ) {
951 std::ostringstream oss;
952 oss << " Test: " << test_ << "\n"
953 << " Error: Invalid number of rows detected\n"
954 << " Details:\n"
955 << " Number of rows : " << matrix.rows() << "\n"
956 << " Expected number of rows: " << expectedRows << "\n";
957 throw std::runtime_error( oss.str() );
958 }
959 }
960 //*************************************************************************************************
961
962
963 //*************************************************************************************************
964 /*!\brief Checking the number of columns of the given matrix.
965 //
966 // \param matrix The matrix to be checked.
967 // \param expectedColumns The expected number of columns of the matrix.
968 // \return void
969 // \exception std::runtime_error Error detected.
970 //
971 // This function checks the number of columns of the given matrix. In case the actual number of
972 // columns does not correspond to the given expected number of columns, a \a std::runtime_error
973 // exception is thrown.
974 */
975 template< typename Type > // Type of the matrix
checkColumns(const Type & matrix,size_t expectedColumns)976 void ColumnTest::checkColumns( const Type& matrix, size_t expectedColumns ) const
977 {
978 if( matrix.columns() != expectedColumns ) {
979 std::ostringstream oss;
980 oss << " Test: " << test_ << "\n"
981 << " Error: Invalid number of columns detected\n"
982 << " Details:\n"
983 << " Number of columns : " << matrix.columns() << "\n"
984 << " Expected number of columns: " << expectedColumns << "\n";
985 throw std::runtime_error( oss.str() );
986 }
987 }
988 //*************************************************************************************************
989
990
991 //*************************************************************************************************
992 /*!\brief Checking the number of non-zero elements of the given matrix.
993 //
994 // \param matrix The matrix to be checked.
995 // \param expectedNonZeros The expected number of non-zero elements of the matrix.
996 // \return void
997 // \exception std::runtime_error Error detected.
998 //
999 // This function checks the number of non-zero elements of the given matrix. In case the
1000 // actual number of non-zero elements does not correspond to the given expected number,
1001 // a \a std::runtime_error exception is thrown.
1002 */
1003 template< typename Type > // Type of the matrix
checkNonZeros(const Type & matrix,size_t expectedNonZeros)1004 void ColumnTest::checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const
1005 {
1006 if( nonZeros( matrix ) != expectedNonZeros ) {
1007 std::ostringstream oss;
1008 oss << " Test: " << test_ << "\n"
1009 << " Error: Invalid number of non-zero elements\n"
1010 << " Details:\n"
1011 << " Number of non-zeros : " << nonZeros( matrix ) << "\n"
1012 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
1013 throw std::runtime_error( oss.str() );
1014 }
1015
1016 if( capacity( matrix ) < nonZeros( matrix ) ) {
1017 std::ostringstream oss;
1018 oss << " Test: " << test_ << "\n"
1019 << " Error: Invalid capacity detected\n"
1020 << " Details:\n"
1021 << " Number of non-zeros: " << nonZeros( matrix ) << "\n"
1022 << " Capacity : " << capacity( matrix ) << "\n";
1023 throw std::runtime_error( oss.str() );
1024 }
1025 }
1026 //*************************************************************************************************
1027
1028
1029
1030
1031 //=================================================================================================
1032 //
1033 // UTILITY FUNCTIONS
1034 //
1035 //=================================================================================================
1036
1037 //*************************************************************************************************
1038 /*!\brief Initializing the given diagonal matrix.
1039 //
1040 // \return void
1041 //
1042 // This function is called before each test case to initialize the given diagonal matrix.
1043 */
1044 template< typename DT >
init(DT & diag)1045 void ColumnTest::init( DT& diag )
1046 {
1047 diag.resize( 3UL );
1048 diag(0,0) = 1;
1049 diag(1,1) = 2;
1050 diag(2,2) = 3;
1051 }
1052 //*************************************************************************************************
1053
1054
1055
1056
1057 //=================================================================================================
1058 //
1059 // GLOBAL TEST FUNCTIONS
1060 //
1061 //=================================================================================================
1062
1063 //*************************************************************************************************
1064 /*!\brief Testing the assignment to a single column of a DiagonalMatrix.
1065 //
1066 // \return void
1067 */
runTest()1068 void runTest()
1069 {
1070 ColumnTest();
1071 }
1072 //*************************************************************************************************
1073
1074
1075
1076
1077 //=================================================================================================
1078 //
1079 // MACRO DEFINITIONS
1080 //
1081 //=================================================================================================
1082
1083 //*************************************************************************************************
1084 /*! \cond BLAZE_INTERNAL */
1085 /*!\brief Macro for the execution of the DiagonalMatrix column test.
1086 */
1087 #define RUN_DIAGONALMATRIX_COLUMN_TEST \
1088 blazetest::mathtest::adaptors::diagonalmatrix::runTest()
1089 /*! \endcond */
1090 //*************************************************************************************************
1091
1092 } // namespace diagonalmatrix
1093
1094 } // namespace adaptors
1095
1096 } // namespace mathtest
1097
1098 } // namespace blazetest
1099
1100 #endif
1101