1 //=================================================================================================
2 /*!
3 //  \file blazetest/mathtest/adaptors/uppermatrix/ColumnTest.h
4 //  \brief Header file for the UpperMatrix 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_UPPERMATRIX_COLUMNTEST_H_
36 #define _BLAZETEST_MATHTEST_ADAPTORS_UPPERMATRIX_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/typetraits/IsRowMajorMatrix.h>
52 #include <blaze/math/UpperMatrix.h>
53 #include <blazetest/system/Types.h>
54 
55 
56 namespace blazetest {
57 
58 namespace mathtest {
59 
60 namespace adaptors {
61 
62 namespace uppermatrix {
63 
64 //=================================================================================================
65 //
66 //  CLASS DEFINITION
67 //
68 //=================================================================================================
69 
70 //*************************************************************************************************
71 /*!\brief Auxiliary class for assignment tests to a single column of a UpperMatrix.
72 //
73 // This class performs assignment tests to a single column of a UpperMatrix. It performs a
74 // series of both compile time as well as runtime tests.
75 */
76 class ColumnTest
77 {
78  private:
79    //**Type definitions****************************************************************************
80    //! Type of the dense upper triangular matrix.
81    using DUT = blaze::UpperMatrix< blaze::DynamicMatrix<int,blaze::rowMajor> >;
82 
83    //! Opposite dense upper triangular matrix type.
84    using DOUT = DUT::OppositeType;
85 
86    //! Type of the sparse upper triangular matrix.
87    using SUT = blaze::UpperMatrix< blaze::CompressedMatrix<int,blaze::rowMajor> >;
88 
89    //! Opposite sparse upper triangular matrix type.
90    using SOUT = SUT::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 UT > void testAssignment();
111    template< typename UT > void testAddAssign ();
112    template< typename UT > void testSubAssign ();
113    template< typename UT > 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 UT > void init( UT& upper );
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 UpperMatrix.
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 UpperMatrix. In
158 // case an error is detected, a \a std::runtime_error exception is thrown.
159 */
160 template< typename UT >  // Type of the upper matrix
testAssignment()161 void ColumnTest::testAssignment()
162 {
163    //=====================================================================================
164    // Dense vector assignment
165    //=====================================================================================
166 
167    // ( 1 -4  7 )      ( 1 -2  7 )
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[0] = -2;
175       vec[1] =  8;
176 
177       UT upper;
178       init( upper );
179 
180       auto col1 = column( upper, 1UL );
181       col1 = vec;
182 
183       checkRows    ( upper, 3UL );
184       checkColumns ( upper, 3UL );
185       checkNonZeros( upper, 5UL );
186 
187       if( col1[0] != -2 || col1[1] != 8 || col1[2] != 0 ) {
188          std::ostringstream oss;
189          oss << " Test: " << test_ << "\n"
190              << " Error: Assignment to column failed\n"
191              << " Details:\n"
192              << "   Result:\n" << col1 << "\n"
193              << "   Expected result:\n( -2 8 0 )\n";
194          throw std::runtime_error( oss.str() );
195       }
196 
197       if( upper(0,0) != 1 || upper(0,1) != -2 || upper(0,2) != 7 ||
198           upper(1,0) != 0 || upper(1,1) !=  8 || upper(1,2) != 0 ||
199           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
200          std::ostringstream oss;
201          oss << " Test: " << test_ << "\n"
202              << " Error: Assignment to column failed\n"
203              << " Details:\n"
204              << "   Result:\n" << upper << "\n"
205              << "   Expected result:\n( 1 -2  7 )\n( 0  8  0 )\n( 0  0  3 )\n";
206          throw std::runtime_error( oss.str() );
207       }
208    }
209 
210    // ( 1 -4  7 )      ( 1 -2  7 )
211    // ( 0  2  0 )  =>  ( 0  8  0 )
212    // ( 0  0  3 )      ( 0  9  3 )
213    {
214       test_ = "Dense vector assignment test 2";
215 
216       blaze::DynamicVector<int,blaze::columnVector> vec( 3UL );
217       vec[0] = -2;
218       vec[1] =  8;
219       vec[2] =  9;
220 
221       UT upper;
222       init( upper );
223 
224       auto col1 = column( upper, 1UL );
225 
226       try {
227          col1 = vec;
228 
229          std::ostringstream oss;
230          oss << " Test: " << test_ << "\n"
231              << " Error: Assignment of invalid vector succeeded\n"
232              << " Details:\n"
233              << "   Result:\n" << upper << "\n";
234          throw std::runtime_error( oss.str() );
235       }
236       catch( std::invalid_argument& ) {}
237    }
238 
239 
240    //=====================================================================================
241    // Sparse vector assignment
242    //=====================================================================================
243 
244    // ( 1 -4  7 )      ( 1 -2  7 )
245    // ( 0  2  0 )  =>  ( 0  8  0 )
246    // ( 0  0  3 )      ( 0  0  3 )
247    {
248       test_ = "Sparse vector assignment test 1";
249 
250       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
251       vec[0] = -2;
252       vec[1] =  8;
253       vec.insert( 2UL, 0 );
254 
255       UT upper;
256       init( upper );
257 
258       auto col1 = column( upper, 1UL );
259       col1 = vec;
260 
261       checkRows    ( upper, 3UL );
262       checkColumns ( upper, 3UL );
263       checkNonZeros( upper, 5UL );
264 
265       if( col1[0] != -2 || col1[1] != 8 || col1[2] != 0 ) {
266          std::ostringstream oss;
267          oss << " Test: " << test_ << "\n"
268              << " Error: Assignment to column failed\n"
269              << " Details:\n"
270              << "   Result:\n" << col1 << "\n"
271              << "   Expected result:\n( -2 8 0 )\n";
272          throw std::runtime_error( oss.str() );
273       }
274 
275       if( upper(0,0) != 1 || upper(0,1) != -2 || upper(0,2) != 7 ||
276           upper(1,0) != 0 || upper(1,1) !=  8 || upper(1,2) != 0 ||
277           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
278          std::ostringstream oss;
279          oss << " Test: " << test_ << "\n"
280              << " Error: Assignment to column failed\n"
281              << " Details:\n"
282              << "   Result:\n" << upper << "\n"
283              << "   Expected result:\n( 1 -2  7 )\n( 0  8  0 )\n( 0  0  3 )\n";
284          throw std::runtime_error( oss.str() );
285       }
286    }
287 
288    // ( 1 -4  7 )      ( 1 -2  7 )
289    // ( 0  2  0 )  =>  ( 0  8  0 )
290    // ( 0  0  3 )      ( 0  9  3 )
291    {
292       test_ = "Sparse vector assignment test 2";
293 
294       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
295       vec[0] = -2;
296       vec[1] =  8;
297       vec[2] =  9;
298 
299       UT upper;
300       init( upper );
301 
302       auto col1 = column( upper, 1UL );
303 
304       try {
305          col1 = vec;
306 
307          std::ostringstream oss;
308          oss << " Test: " << test_ << "\n"
309              << " Error: Assignment of invalid vector succeeded\n"
310              << " Details:\n"
311              << "   Result:\n" << upper << "\n";
312          throw std::runtime_error( oss.str() );
313       }
314       catch( std::invalid_argument& ) {}
315    }
316 }
317 //*************************************************************************************************
318 
319 
320 //*************************************************************************************************
321 /*!\brief Test of the addition assignment to columns of a UpperMatrix.
322 //
323 // \return void
324 // \exception std::runtime_error Error detected.
325 //
326 // This function performs a test of the addition assignment to a single column of a UpperMatrix.
327 // In case an error is detected, a \a std::runtime_error exception is thrown.
328 */
329 template< typename UT >  // Type of the upper matrix
testAddAssign()330 void ColumnTest::testAddAssign()
331 {
332    //=====================================================================================
333    // Dense vector addition assignment
334    //=====================================================================================
335 
336    // ( 1 -4  7 )      ( 1 -6  7 )
337    // ( 0  2  0 )  =>  ( 0 10  0 )
338    // ( 0  0  3 )      ( 0  0  3 )
339    {
340       test_ = "Dense vector addition assignment test 1";
341 
342       blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
343       vec[0] = -2;
344       vec[1] =  8;
345 
346       UT upper;
347       init( upper );
348 
349       auto col1 = column( upper, 1UL );
350       col1 += vec;
351 
352       checkRows    ( upper, 3UL );
353       checkColumns ( upper, 3UL );
354       checkNonZeros( upper, 5UL );
355 
356       if( col1[0] != -6 || col1[1] != 10 || col1[2] != 0 ) {
357          std::ostringstream oss;
358          oss << " Test: " << test_ << "\n"
359              << " Error: Assignment to column failed\n"
360              << " Details:\n"
361              << "   Result:\n" << col1 << "\n"
362              << "   Expected result:\n( -6 10 0 )\n";
363          throw std::runtime_error( oss.str() );
364       }
365 
366       if( upper(0,0) != 1 || upper(0,1) != -6 || upper(0,2) != 7 ||
367           upper(1,0) != 0 || upper(1,1) != 10 || upper(1,2) != 0 ||
368           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
369          std::ostringstream oss;
370          oss << " Test: " << test_ << "\n"
371              << " Error: Assignment to column failed\n"
372              << " Details:\n"
373              << "   Result:\n" << upper << "\n"
374              << "   Expected result:\n( 1 -6  7 )\n( 0 10  0 )\n( 0  0  3 )\n";
375          throw std::runtime_error( oss.str() );
376       }
377    }
378 
379    // ( 1 -4  7 )      ( 1 -6  7 )
380    // ( 0  2  0 )  =>  ( 0 10  0 )
381    // ( 0  0  3 )      ( 0  9  3 )
382    {
383       test_ = "Dense vector addition assignment test 2";
384 
385       blaze::DynamicVector<int,blaze::columnVector> vec( 3UL );
386       vec[0] = -2;
387       vec[1] =  8;
388       vec[2] =  9;
389 
390       UT upper;
391       init( upper );
392 
393       auto col1 = column( upper, 1UL );
394 
395       try {
396          col1 += vec;
397 
398          std::ostringstream oss;
399          oss << " Test: " << test_ << "\n"
400              << " Error: Assignment of invalid vector succeeded\n"
401              << " Details:\n"
402              << "   Result:\n" << upper << "\n";
403          throw std::runtime_error( oss.str() );
404       }
405       catch( std::invalid_argument& ) {}
406    }
407 
408 
409    //=====================================================================================
410    // Sparse vector addition assignment
411    //=====================================================================================
412 
413    // ( 1 -4  7 )      ( 1 -6  7 )
414    // ( 0  2  0 )  =>  ( 0 10  0 )
415    // ( 0  0  3 )      ( 0  0  3 )
416    {
417       test_ = "Sparse vector addition assignment test 1";
418 
419       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
420       vec[0] = -2;
421       vec[1] =  8;
422       vec.insert( 2UL, 0 );
423 
424       UT upper;
425       init( upper );
426 
427       auto col1 = column( upper, 1UL );
428       col1 += vec;
429 
430       checkRows    ( upper, 3UL );
431       checkColumns ( upper, 3UL );
432       checkNonZeros( upper, 5UL );
433 
434       if( col1[0] != -6 || col1[1] != 10 || col1[2] != 0 ) {
435          std::ostringstream oss;
436          oss << " Test: " << test_ << "\n"
437              << " Error: Assignment to column failed\n"
438              << " Details:\n"
439              << "   Result:\n" << col1 << "\n"
440              << "   Expected result:\n( -6 10 0 )\n";
441          throw std::runtime_error( oss.str() );
442       }
443 
444       if( upper(0,0) != 1 || upper(0,1) != -6 || upper(0,2) != 7 ||
445           upper(1,0) != 0 || upper(1,1) != 10 || upper(1,2) != 0 ||
446           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
447          std::ostringstream oss;
448          oss << " Test: " << test_ << "\n"
449              << " Error: Assignment to column failed\n"
450              << " Details:\n"
451              << "   Result:\n" << upper << "\n"
452              << "   Expected result:\n( 1 -6  7 )\n( 0 10  0 )\n( 0  0  3 )\n";
453          throw std::runtime_error( oss.str() );
454       }
455    }
456 
457    // ( 1 -4  7 )      ( 1 -6  7 )
458    // ( 0  2  0 )  =>  ( 0 10  0 )
459    // ( 0  0  3 )      ( 0  9  3 )
460    {
461       test_ = "Sparse vector addition assignment test 2";
462 
463       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
464       vec[0] = -2;
465       vec[1] =  8;
466       vec[2] =  9;
467 
468       UT upper;
469       init( upper );
470 
471       auto col1 = column( upper, 1UL );
472 
473       try {
474          col1 += vec;
475 
476          std::ostringstream oss;
477          oss << " Test: " << test_ << "\n"
478              << " Error: Assignment of invalid vector succeeded\n"
479              << " Details:\n"
480              << "   Result:\n" << upper << "\n";
481          throw std::runtime_error( oss.str() );
482       }
483       catch( std::invalid_argument& ) {}
484    }
485 }
486 //*************************************************************************************************
487 
488 
489 //*************************************************************************************************
490 /*!\brief Test of the subtraction assignment to columns of a UpperMatrix.
491 //
492 // \return void
493 // \exception std::runtime_error Error detected.
494 //
495 // This function performs a test of the subtraction assignment to a single column of a
496 // UpperMatrix. In case an error is detected, a \a std::runtime_error exception is thrown.
497 */
498 template< typename UT >  // Type of the upper matrix
testSubAssign()499 void ColumnTest::testSubAssign()
500 {
501    //=====================================================================================
502    // Dense vector subtraction assignment
503    //=====================================================================================
504 
505    // ( 1 -4  7 )      ( 1 -2  7 )
506    // ( 0  2  0 )  =>  ( 0 -6  0 )
507    // ( 0  0  3 )      ( 0  0  3 )
508    {
509       test_ = "Dense vector subtraction assignment test 1";
510 
511       blaze::DynamicVector<int,blaze::columnVector> vec( 3UL, 0 );
512       vec[0] = -2;
513       vec[1] =  8;
514 
515       UT upper;
516       init( upper );
517 
518       auto col1 = column( upper, 1UL );
519       col1 -= vec;
520 
521       checkRows    ( upper, 3UL );
522       checkColumns ( upper, 3UL );
523       checkNonZeros( upper, 5UL );
524 
525       if( col1[0] != -2 || col1[1] != -6 || col1[2] != 0 ) {
526          std::ostringstream oss;
527          oss << " Test: " << test_ << "\n"
528              << " Error: Assignment to column failed\n"
529              << " Details:\n"
530              << "   Result:\n" << col1 << "\n"
531              << "   Expected result:\n( -2 -6 0 )\n";
532          throw std::runtime_error( oss.str() );
533       }
534 
535       if( upper(0,0) != 1 || upper(0,1) != -2 || upper(0,2) != 7 ||
536           upper(1,0) != 0 || upper(1,1) != -6 || upper(1,2) != 0 ||
537           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
538          std::ostringstream oss;
539          oss << " Test: " << test_ << "\n"
540              << " Error: Assignment to column failed\n"
541              << " Details:\n"
542              << "   Result:\n" << upper << "\n"
543              << "   Expected result:\n( 1 -2  7 )\n( 0 -6  0 )\n( 0  0  3 )\n";
544          throw std::runtime_error( oss.str() );
545       }
546    }
547 
548    // ( 1 -4  7 )      ( 1 -2  7 )
549    // ( 0  2  0 )  =>  ( 0 -6  0 )
550    // ( 0  0  3 )      ( 0  9  3 )
551    {
552       test_ = "Dense vector subtraction assignment test 2";
553 
554       blaze::DynamicVector<int,blaze::columnVector> vec( 3UL );
555       vec[0] = -2;
556       vec[1] =  8;
557       vec[2] =  9;
558 
559       UT upper;
560       init( upper );
561 
562       auto col1 = column( upper, 1UL );
563 
564       try {
565          col1 -= vec;
566 
567          std::ostringstream oss;
568          oss << " Test: " << test_ << "\n"
569              << " Error: Assignment of invalid vector succeeded\n"
570              << " Details:\n"
571              << "   Result:\n" << upper << "\n";
572          throw std::runtime_error( oss.str() );
573       }
574       catch( std::invalid_argument& ) {}
575    }
576 
577 
578    //=====================================================================================
579    // Sparse vector subtraction assignment
580    //=====================================================================================
581 
582    // ( 1 -4  7 )      ( 1 -2  7 )
583    // ( 0  2  0 )  =>  ( 0 -6  0 )
584    // ( 0  0  3 )      ( 0  0  3 )
585    {
586       test_ = "Sparse vector subtraction assignment test 1";
587 
588       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
589       vec[0] = -2;
590       vec[1] =  8;
591       vec.insert( 2UL, 0 );
592 
593       UT upper;
594       init( upper );
595 
596       auto col1 = column( upper, 1UL );
597       col1 -= vec;
598 
599       checkRows    ( upper, 3UL );
600       checkColumns ( upper, 3UL );
601       checkNonZeros( upper, 5UL );
602 
603       if( col1[0] != -2 || col1[1] != -6 || col1[2] != 0 ) {
604          std::ostringstream oss;
605          oss << " Test: " << test_ << "\n"
606              << " Error: Assignment to column failed\n"
607              << " Details:\n"
608              << "   Result:\n" << col1 << "\n"
609              << "   Expected result:\n( -2 -6 0 )\n";
610          throw std::runtime_error( oss.str() );
611       }
612 
613       if( upper(0,0) != 1 || upper(0,1) != -2 || upper(0,2) != 7 ||
614           upper(1,0) != 0 || upper(1,1) != -6 || upper(1,2) != 0 ||
615           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
616          std::ostringstream oss;
617          oss << " Test: " << test_ << "\n"
618              << " Error: Assignment to column failed\n"
619              << " Details:\n"
620              << "   Result:\n" << upper << "\n"
621              << "   Expected result:\n( 1 -2  7 )\n( 0 -6  0 )\n( 0  0  3 )\n";
622          throw std::runtime_error( oss.str() );
623       }
624    }
625 
626    // ( 1 -4  7 )      ( 1 -2  7 )
627    // ( 0  2  0 )  =>  ( 0 -6  0 )
628    // ( 0  0  3 )      ( 0  9  3 )
629    {
630       test_ = "Sparse vector subtraction assignment test 2";
631 
632       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
633       vec[0] = -2;
634       vec[1] =  8;
635       vec[2] =  9;
636 
637       UT upper;
638       init( upper );
639 
640       auto col1 = column( upper, 1UL );
641 
642       try {
643          col1 -= vec;
644 
645          std::ostringstream oss;
646          oss << " Test: " << test_ << "\n"
647              << " Error: Assignment of invalid vector succeeded\n"
648              << " Details:\n"
649              << "   Result:\n" << upper << "\n";
650          throw std::runtime_error( oss.str() );
651       }
652       catch( std::invalid_argument& ) {}
653    }
654 }
655 //*************************************************************************************************
656 
657 
658 //*************************************************************************************************
659 /*!\brief Test of the multiplication assignment to columns of a UpperMatrix.
660 //
661 // \return void
662 // \exception std::runtime_error Error detected.
663 //
664 // This function performs a test of the multiplication assignment to a single column of a
665 // UpperMatrix. In case an error is detected, a \a std::runtime_error exception is thrown.
666 */
667 template< typename UT >  // Type of the upper matrix
testMultAssign()668 void ColumnTest::testMultAssign()
669 {
670    //=====================================================================================
671    // Dense vector multiplication assignment
672    //=====================================================================================
673 
674    // ( 1 -4  7 )      ( 1  8  7 )
675    // ( 0  2  0 )  =>  ( 0 16  0 )
676    // ( 0  0  3 )      ( 0  0  3 )
677    {
678       test_ = "Dense vector multiplication assignment test";
679 
680       blaze::DynamicVector<int,blaze::columnVector> vec( 3UL );
681       vec[0] = -2;
682       vec[1] =  8;
683       vec[2] =  9;
684 
685       UT upper;
686       init( upper );
687 
688       auto col1 = column( upper, 1UL );
689       col1 *= vec;
690 
691       checkRows    ( upper, 3UL );
692       checkColumns ( upper, 3UL );
693       checkNonZeros( upper, 5UL );
694 
695       if( col1[0] != 8 || col1[1] != 16 || col1[2] != 0 ) {
696          std::ostringstream oss;
697          oss << " Test: " << test_ << "\n"
698              << " Error: Assignment to column failed\n"
699              << " Details:\n"
700              << "   Result:\n" << col1 << "\n"
701              << "   Expected result:\n( 8 16 0 )\n";
702          throw std::runtime_error( oss.str() );
703       }
704 
705       if( upper(0,0) != 1 || upper(0,1) !=  8 || upper(0,2) != 7 ||
706           upper(1,0) != 0 || upper(1,1) != 16 || upper(1,2) != 0 ||
707           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
708          std::ostringstream oss;
709          oss << " Test: " << test_ << "\n"
710              << " Error: Assignment to column failed\n"
711              << " Details:\n"
712              << "   Result:\n" << upper << "\n"
713              << "   Expected result:\n( 1  8  7 )\n( 0 16  0 )\n( 0  0  3 )\n";
714          throw std::runtime_error( oss.str() );
715       }
716    }
717 
718 
719    //=====================================================================================
720    // Sparse vector multiplication assignment
721    //=====================================================================================
722 
723    // ( 1 -4  7 )      ( 1  8  7 )
724    // ( 0  2  0 )  =>  ( 0 16  0 )
725    // ( 0  0  3 )      ( 0  0  3 )
726    {
727       test_ = "Sparse vector multiplication assignment test";
728 
729       blaze::CompressedVector<int,blaze::columnVector> vec( 3UL, 3UL );
730       vec[0] = -2;
731       vec[1] =  8;
732       vec[2] =  9;
733 
734       UT upper;
735       init( upper );
736 
737       auto col1 = column( upper, 1UL );
738       col1 *= vec;
739 
740       checkRows    ( upper, 3UL );
741       checkColumns ( upper, 3UL );
742       checkNonZeros( upper, 5UL );
743 
744       if( col1[0] != 8 || col1[1] != 16 || col1[2] != 0 ) {
745          std::ostringstream oss;
746          oss << " Test: " << test_ << "\n"
747              << " Error: Assignment to column failed\n"
748              << " Details:\n"
749              << "   Result:\n" << col1 << "\n"
750              << "   Expected result:\n( 8 16 0 )\n";
751          throw std::runtime_error( oss.str() );
752       }
753 
754       if( upper(0,0) != 1 || upper(0,1) !=  8 || upper(0,2) != 7 ||
755           upper(1,0) != 0 || upper(1,1) != 16 || upper(1,2) != 0 ||
756           upper(2,0) != 0 || upper(2,1) !=  0 || upper(2,2) != 3 ) {
757          std::ostringstream oss;
758          oss << " Test: " << test_ << "\n"
759              << " Error: Assignment to column failed\n"
760              << " Details:\n"
761              << "   Result:\n" << upper << "\n"
762              << "   Expected result:\n( 1  8  7 )\n( 0 16  0 )\n( 0  0  3 )\n";
763          throw std::runtime_error( oss.str() );
764       }
765    }
766 }
767 //*************************************************************************************************
768 
769 
770 //*************************************************************************************************
771 /*!\brief Checking the number of rows of the given matrix.
772 //
773 // \param matrix The matrix to be checked.
774 // \param expectedRows The expected number of rows of the matrix.
775 // \return void
776 // \exception std::runtime_error Error detected.
777 //
778 // This function checks the number of rows of the given matrix. In case the actual number of
779 // rows does not correspond to the given expected number of rows, a \a std::runtime_error
780 // exception is thrown.
781 */
782 template< typename Type >  // Type of the matrix
checkRows(const Type & matrix,size_t expectedRows)783 void ColumnTest::checkRows( const Type& matrix, size_t expectedRows ) const
784 {
785    if( matrix.rows() != expectedRows ) {
786       std::ostringstream oss;
787       oss << " Test: " << test_ << "\n"
788           << " Error: Invalid number of rows detected\n"
789           << " Details:\n"
790           << "   Number of rows         : " << matrix.rows() << "\n"
791           << "   Expected number of rows: " << expectedRows << "\n";
792       throw std::runtime_error( oss.str() );
793    }
794 }
795 //*************************************************************************************************
796 
797 
798 //*************************************************************************************************
799 /*!\brief Checking the number of columns of the given matrix.
800 //
801 // \param matrix The matrix to be checked.
802 // \param expectedColumns The expected number of columns of the matrix.
803 // \return void
804 // \exception std::runtime_error Error detected.
805 //
806 // This function checks the number of columns of the given matrix. In case the actual number of
807 // columns does not correspond to the given expected number of columns, a \a std::runtime_error
808 // exception is thrown.
809 */
810 template< typename Type >  // Type of the matrix
checkColumns(const Type & matrix,size_t expectedColumns)811 void ColumnTest::checkColumns( const Type& matrix, size_t expectedColumns ) const
812 {
813    if( matrix.columns() != expectedColumns ) {
814       std::ostringstream oss;
815       oss << " Test: " << test_ << "\n"
816           << " Error: Invalid number of columns detected\n"
817           << " Details:\n"
818           << "   Number of columns         : " << matrix.columns() << "\n"
819           << "   Expected number of columns: " << expectedColumns << "\n";
820       throw std::runtime_error( oss.str() );
821    }
822 }
823 //*************************************************************************************************
824 
825 
826 //*************************************************************************************************
827 /*!\brief Checking the number of non-zero elements of the given matrix.
828 //
829 // \param matrix The matrix to be checked.
830 // \param expectedNonZeros The expected number of non-zero elements of the matrix.
831 // \return void
832 // \exception std::runtime_error Error detected.
833 //
834 // This function checks the number of non-zero elements of the given matrix. In case the
835 // actual number of non-zero elements does not correspond to the given expected number,
836 // a \a std::runtime_error exception is thrown.
837 */
838 template< typename Type >  // Type of the matrix
checkNonZeros(const Type & matrix,size_t expectedNonZeros)839 void ColumnTest::checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const
840 {
841    if( nonZeros( matrix ) != expectedNonZeros ) {
842       std::ostringstream oss;
843       oss << " Test: " << test_ << "\n"
844           << " Error: Invalid number of non-zero elements\n"
845           << " Details:\n"
846           << "   Number of non-zeros         : " << nonZeros( matrix ) << "\n"
847           << "   Expected number of non-zeros: " << expectedNonZeros << "\n";
848       throw std::runtime_error( oss.str() );
849    }
850 
851    if( capacity( matrix ) < nonZeros( matrix ) ) {
852       std::ostringstream oss;
853       oss << " Test: " << test_ << "\n"
854           << " Error: Invalid capacity detected\n"
855           << " Details:\n"
856           << "   Number of non-zeros: " << nonZeros( matrix ) << "\n"
857           << "   Capacity           : " << capacity( matrix ) << "\n";
858       throw std::runtime_error( oss.str() );
859    }
860 }
861 //*************************************************************************************************
862 
863 
864 
865 
866 //=================================================================================================
867 //
868 //  UTILITY FUNCTIONS
869 //
870 //=================================================================================================
871 
872 //*************************************************************************************************
873 /*!\brief Initializing the given upper triangular matrix.
874 //
875 // \return void
876 //
877 // This function is called before each test case to initialize the given upper triangular
878 // matrix.
879 */
880 template< typename UT >
init(UT & upper)881 void ColumnTest::init( UT& upper )
882 {
883    upper.resize( 3UL );
884    upper(0,0) =  1;
885    upper(0,1) = -4;
886    upper(0,2) =  7;
887    upper(1,1) =  2;
888    upper(1,2) =  0;
889    upper(2,2) =  3;
890 }
891 //*************************************************************************************************
892 
893 
894 
895 
896 //=================================================================================================
897 //
898 //  GLOBAL TEST FUNCTIONS
899 //
900 //=================================================================================================
901 
902 //*************************************************************************************************
903 /*!\brief Testing the assignment to a single column of a UpperMatrix.
904 //
905 // \return void
906 */
runTest()907 void runTest()
908 {
909    ColumnTest();
910 }
911 //*************************************************************************************************
912 
913 
914 
915 
916 //=================================================================================================
917 //
918 //  MACRO DEFINITIONS
919 //
920 //=================================================================================================
921 
922 //*************************************************************************************************
923 /*! \cond BLAZE_INTERNAL */
924 /*!\brief Macro for the execution of the UpperMatrix column test.
925 */
926 #define RUN_UPPERMATRIX_COLUMN_TEST \
927    blazetest::mathtest::adaptors::uppermatrix::runTest()
928 /*! \endcond */
929 //*************************************************************************************************
930 
931 } // namespace uppermatrix
932 
933 } // namespace adaptors
934 
935 } // namespace mathtest
936 
937 } // namespace blazetest
938 
939 #endif
940