CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixSolves_lapack.cpp
Go to the documentation of this file.
1/// \file MatrixSolves_lapack.cpp
2/// \ingroup Test
3/// \ingroup Matrix
4/// Example of the simple linear solvers implemented
5/// for dense, banded and sparse matrix objects. To begin
6/// with a simple \f$ 2 \times 2 \f$ matrix problem is solved.
7/// Then a penta-diagonal problem is solved using the
8/// dense, banded and sparse containers. The native linear Gaussian
9/// elimination solvers are used unless the PETSC_D/Z compiler
10/// options are used, in which case the linear solver phase
11/// calls the PETSc library as appropriate.
12
13#include <cassert>
14
15#include <Timer.h>
16#include <Types.h>
17#include <Utility.h>
18#include <DenseLinearSystem.h>
19#include <BandedLinearSystem.h>
20
21using namespace CppNoddy;
22using namespace std;
23
24int main()
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}
Specification of the linear system class.
Specification of the linear system class.
int main()
A spec for the CppNoddy Timer object.
A spec for a collection of utility functions.
A linear system class for vector right-hand sides.
void solve()
Solve the sparse system.
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 inf_norm() const
Infinity norm.
Definition: DenseVector.cpp:59
void sub(const DenseVector< _Type > &x)
Subtract a vector, element wise, equivalent to -=.
Definition: DenseVector.cpp:44
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt