Commit 90d4cebc authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Added examples for tridiagonal matrix.

parent ca9ae964
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>


template< typename Device >
void initializerListExample()
{
   TNL::Matrices::SparseMatrix< double, Device > matrix {
      {  1,  2,  3,  4,  5 }, // row capacities
      6 };                    // number of matrix columns

   for( int row = 0; row < matrix.getRows(); row++ )
      for( int column = 0; column <= row; column++ )
         matrix.setElement( row, column, row - column + 1 );
   std::cout << "General sparse matrix: " << std::endl << matrix << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Creating matrices on CPU ... " << std::endl;
   initializerListExample< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Creating matrices on CUDA GPU ... " << std::endl;
   initializerListExample< TNL::Devices::Cuda >();
#endif
}
+1 −0
Original line number Diff line number Diff line
TridiagonalMatrixExample_Constructor_init_list_1.cpp
 No newline at end of file
+31 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>


template< typename Device >
void initializerListExample()
{
   TNL::Matrices::SparseMatrix< double, Device > matrix (
      5, // number of matrix rows
      5, // number of matrix columns
      {  // matrix elements definition
         {  0,  0,  2.0 },
         {  1,  0, -1.0 }, {  1,  1,  2.0 }, {  1,  2, -1.0 },
         {  2,  1, -1.0 }, {  2,  2,  2.0 }, {  2,  3, -1.0 },
         {  3,  2, -1.0 }, {  3,  3,  2.0 }, {  3,  4, -1.0 },
         {  4,  4,  2.0 } } );

   std::cout << "General sparse matrix: " << std::endl << matrix << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Creating matrices on CPU ... " << std::endl;
   initializerListExample< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Creating matrices on CUDA GPU ... " << std::endl;
   initializerListExample< TNL::Devices::Cuda >();
#endif
}
+1 −0
Original line number Diff line number Diff line
TridiagonalMatrixExample_Constructor_init_list_2.cpp
 No newline at end of file
+38 −0
Original line number Diff line number Diff line
#include <iostream>
#include <map>
#include <utility>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>


template< typename Device >
void initializerListExample()
{
   std::map< std::pair< int, int >, double > map;
   map.insert( std::make_pair( std::make_pair( 0, 0 ),  2.0 ) );
   map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) );
   map.insert( std::make_pair( std::make_pair( 1, 1 ),  2.0 ) );
   map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) );
   map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) );
   map.insert( std::make_pair( std::make_pair( 2, 2 ),  2.0 ) );
   map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) );
   map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) );
   map.insert( std::make_pair( std::make_pair( 3, 3 ),  2.0 ) );
   map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) );
   map.insert( std::make_pair( std::make_pair( 4, 4 ),  2.0 ) );

   TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, map );

   std::cout << "General sparse matrix: " << std::endl << matrix << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Creating matrices on CPU ... " << std::endl;
   initializerListExample< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Creating matrices on CUDA GPU ... " << std::endl;
   initializerListExample< TNL::Devices::Cuda >();
#endif
}
Loading