CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixSolves.cpp
Go to the documentation of this file.
1/// \file MatrixSolves.cpp
2/// \ingroup Test
3/// \ingroup Matrix
4/// Example of the simple linear solvers using
5/// a simple \f$ 2 \times 2 \f$ matrix problem.
6
7#include <cassert>
8
9#include <Timer.h>
10#include <Types.h>
11#include <Utility.h>
12#include <DenseLinearSystem.h>
13#include <BandedLinearSystem.h>
14
15using namespace CppNoddy;
16using namespace std;
17
18int main()
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}
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