CppNoddy  0.92
Loading...
Searching...
No Matches
Functions
MatrixSolves_lapack.cpp File Reference

Example of the simple linear solvers implemented for dense, banded and sparse matrix objects. More...

#include <cassert>
#include <Timer.h>
#include <Types.h>
#include <Utility.h>
#include <DenseLinearSystem.h>
#include <BandedLinearSystem.h>

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

Example of the simple linear solvers implemented for dense, banded and sparse matrix objects.

To begin with a simple $ 2 \times 2 $ matrix problem is solved. Then a penta-diagonal problem is solved using the dense, banded and sparse containers. The native linear Gaussian elimination solvers are used unless the PETSC_D/Z compiler options are used, in which case the linear solver phase calls the PETSc library as appropriate.

Definition in file MatrixSolves_lapack.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 24 of file MatrixSolves_lapack.cpp.

25{
26
27 cout << "\n";
28 cout << "=== Matrix: Example linear solver ==================\n";
29 cout << "\n";
30
31 bool failed = false;
32 // tolerance for the test
33 const double tol = 1.e-10;
34
35 //
36 // SOLVE A SMALL "Dense"(!) 2X2 REAL SYSTEM with LAPACK or native
37 //
38 DenseMatrix<double> A( 2, 2, 0.0 );
39 DenseVector<double> B( 2, 0.0 );
40 A( 0, 0 ) = 1.;
41 A( 0, 1 ) = 2.;
42 A( 1, 0 ) = 3.;
43 A( 1, 1 ) = 4.;
44 B[ 0 ] = 5.;
45 B[ 1 ] = 11.;
46
47 std::cout << " Simple 2x2 system solved by LAPACK LU.\n";
48 DenseLinearSystem<double> small_system( &A, &B, "lapack" );
49
50 try
51 {
52 small_system.solve();
53 }
54 catch (const std::runtime_error &error )
55 {
56 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
57 return 1;
58 }
59 DenseVector<double> answer( 2, 0.0 );
60 answer[ 0 ] = 1.0;
61 answer[ 1 ] = 2.0;
62 B.sub( answer );
63 if ( B.inf_norm() > tol )
64 {
65 std::cout << " Simple 2x2 system was not solved correctly\n";
66 std::cout << " residual vector's inf_norm = " << B.inf_norm() << "\n";
67 failed = true;
68 }
69 else
70 {
71 std::cout << " Simple 2x2 `dense' solver works.\n";
72 }
73
74 //
75 // CONCLUDING PASS/FAIL
76 //
77 if ( failed )
78 {
79 cout << "\033[1;31;48m * FAILED \033[0m\n";
80 return 1;
81 }
82 else
83 {
84 cout << "\033[1;32;48m * PASSED \033[0m\n";
85 return 0;
86 }
87}
A linear system class for vector right-hand sides.
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
double A(1.0)
initial hump amplitude

References CppNoddy::DenseVector< _Type >::inf_norm(), CppNoddy::DenseLinearSystem< _Type >::solve(), and CppNoddy::DenseVector< _Type >::sub().

© 2012

R.E. Hewitt