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

Example of the simple linear solvers using a simple $ 2 \times 2 $ matrix problem. 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 using a simple $ 2 \times 2 $ matrix problem.

Definition in file MatrixSolves.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 18 of file MatrixSolves.cpp.

19{
20
21 cout << "\n";
22 cout << "=== Matrix: Example linear solver ==================\n";
23 cout << "\n";
24
25 bool failed = false;
26 // tolerance for the test
27 const double tol = 1.e-10;
28
29 //
30 // SOLVE A SMALL "Dense"(!) 2X2 REAL SYSTEM with LAPACK or native
31 //
32 DenseMatrix<double> A( 2, 2, 0.0 );
33 DenseVector<double> B( 2, 0.0 );
34 A( 0, 0 ) = 1.;
35 A( 0, 1 ) = 2.;
36 A( 1, 0 ) = 3.;
37 A( 1, 1 ) = 4.;
38 B[ 0 ] = 5.;
39 B[ 1 ] = 11.;
40
41 std::cout << " Simple 2x2 (dense) system solved by native Gauss Jordan routine.\n";
42 DenseLinearSystem<double> small_system( &A, &B, "native" );
43
44 try
45 {
46 small_system.solve();
47 }
48 catch ( const std::runtime_error &error )
49 {
50 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
51 return 1;
52 }
53 DenseVector<double> answer( 2, 0.0 );
54 answer[ 0 ] = 1.0;
55 answer[ 1 ] = 2.0;
56 B.sub( answer );
57 if ( B.inf_norm() > tol )
58 {
59 std::cout << " Simple 2x2 system was not solved correctly\n";
60 std::cout << " residual vector's inf_norm = " << B.inf_norm() << "\n";
61 failed = true;
62 }
63 else
64 {
65 std::cout << " Simple 2x2 `dense' solver works.\n";
66 }
67
68
69 //
70 // SOLVE A SMALL "Dense"(!) 2X2 COMPLEX SYSTEM with LAPACK or native
71 //
72 DenseMatrix<D_complex> Ac( 2, 2, 0.0 );
73 DenseVector<D_complex> Bc( 2, 0.0 );
74 Ac( 0, 0 ) = 1.;
75 Ac( 0, 1 ) = 2.;
76 Ac( 1, 0 ) = 3.;
77 Ac( 1, 1 ) = 4.;
78 Bc[ 0 ] = 5.;
79 Bc[ 1 ] = 11.;
80
81 std::cout << " Simple 2x2 (dense) complex system solved by native Gauss Jordan routine.\n";
82 DenseLinearSystem<D_complex> small_complex_system( &Ac, &Bc, "native" );
83
84 try
85 {
86 small_complex_system.solve();
87 }
88 catch ( const std::runtime_error &error )
89 {
90 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
91 return 1;
92 }
93 DenseVector<D_complex> answer_complex( 2, 0.0 );
94 answer_complex[ 0 ] = 1.0;
95 answer_complex[ 1 ] = 2.0;
96 Bc.sub( answer_complex );
97 if ( Bc.inf_norm() > tol )
98 {
99 std::cout << " Simple 2x2 (complex) system was not solved correctly\n";
100 std::cout << " residual vector's inf_norm = " << Bc.inf_norm() << "\n";
101 failed = true;
102 }
103 else
104 {
105 std::cout << " Simple 2x2 `dense' complex solver works.\n";
106 }
107
108
109
110 //
111 // CONCLUDING PASS/FAIL
112 //
113 if ( failed )
114 {
115 cout << "\033[1;31;48m * FAILED \033[0m\n";
116 return 1;
117 }
118 else
119 {
120 cout << "\033[1;32;48m * PASSED \033[0m\n";
121 return 0;
122 }
123}
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