CppNoddy  0.92
Loading...
Searching...
No Matches
DenseVector.cpp
Go to the documentation of this file.
1/// \file DenseVector.cpp
2/// \ingroup Tests
3/// \ingroup Vector
4/// Some simple sanity checks for the NVector class
5/// with both double and complex types.
6
7#include <algorithm>
8
9#include <Types.h>
10#include <Timer.h>
11#include <Utility.h>
12#include <Functors.h>
13#include "../Utils_Fill.h"
14
15using namespace CppNoddy;
16using namespace std;
17
18int main()
19{
20
21 cout << "\n";
22 cout << "=== Vector: (dense) double/complex Example =========\n";
23 cout << "\n";
24
25 const unsigned N = 1000000; // size of vectors
26
27 cout << " Using vectors of size " << N << " \n";
28
29 const double tol = 1.e-13;
30 bool failed = false;
31
33 // a real vector of random entries
34 DenseVector<double> V( N, 0.0 );
36 // a complex vector initialised with the same real vector
37 DenseVector<D_complex> CExample( V );
38
39 // another real vector of random entries
40 DenseVector<double> DExample( N, 0.0 );
41 Utils_Fill::fill_random( DExample );
42
43 const unsigned M = 100; // number of repeats
44 for ( unsigned i = 0; i < M; ++i )
45 {
46 Utility::dot( V, DExample );
47 }
48
49 cout << " \nComplex vectors \n";
50 cout << " Testing norms, nearest_index, max/minabs_index\n";
51 if ( abs( CExample.inf_norm() - *min_element( CExample.begin(), CExample.end(), absDiff_predicate<D_complex>( 1.0 ) ) ) >
52 tol )
53 {
54 std::cout << " - Failed inf_norm/nearest_index Example" << "\n";
55 failed = true;
56 }
57 if ( abs( CExample.inf_norm() - *max_element( CExample.begin(), CExample.end(), abs_predicate<D_complex>() ) )
58 > tol )
59 {
60 std::cout << " - Failed inf_norm/maxabs_index Example" << "\n";
61 failed = true;
62 }
63
64 CExample.one_norm();
65 CExample.two_norm();
66 CExample.inf_norm();
67
68 cout << "\n";
69 cout << " Double vectors \n";
70
71 cout << " Testing norms, nearest_index, max/minabs_index\n";
72 if ( abs( DExample.inf_norm() - *min_element( DExample.begin(), DExample.end(), absDiff_predicate<double>( 1.0 ) ) )
73 > tol )
74 {
75 std::cout << " - Failed inf_norm/nearest_index Example" << "\n";
76 failed = true;
77 }
78 if ( abs( DExample.inf_norm() - *max_element( DExample.begin(), DExample.end(), abs_predicate<double>() ) )
79 > tol )
80 {
81 std::cout << " - Failed inf_norm/maxabs_index Example" << "\n";
82 failed = true;
83 }
84
85 DExample.one_norm();
86 DExample.two_norm();
87 DExample.inf_norm();
88
89 if ( failed )
90 {
91 cout << "\033[1;31;48m * FAILED \033[0m\n";
92 return 1;
93 }
94 else
95 {
96 cout << "\033[1;32;48m * PASSED \033[0m\n";
97 return 0;
98 }
99
100}
@ V
Definition: BVPKarman.cpp:20
Some Function Objects that CppNoddy makes use of in algorithms applied to STL containers.
int main()
Definition: DenseVector.cpp:18
A spec for the CppNoddy Timer object.
A spec for a collection of utility functions.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
double one_norm() const
l1-norm.
Definition: DenseVector.cpp:49
double inf_norm() const
Infinity norm.
Definition: DenseVector.cpp:59
elt_iter begin()
Pass through to the storage container.
Definition: DenseVector.h:82
double two_norm() const
l2-norm.
Definition: DenseVector.cpp:54
elt_iter end()
Pass through to the storage container.
Definition: DenseVector.h:102
A function object predicate that first computes the absolute difference between two elements and a sp...
Definition: Functors.h:30
A function object predicate that compares the absolute value of two elements and returns a true of el...
Definition: Functors.h:15
_Type dot(const DenseVector< _Type > &X, const DenseVector< _Type > &Y)
Templated dot product.
Definition: Utility.h:314
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...
void time_seed()
initialise RNG
Definition: Utils_Fill.h:14
void fill_random(CppNoddy::SparseVector< double > &V, const unsigned &num_of_elts)
Definition: Utils_Fill.h:53

© 2012

R.E. Hewitt