Line data Source code
1 : /*
2 : * Morpheus_Vector_normTest.cpp
3 : *
4 : * Created on: Jul 28, 2016
5 : * Author: amklinv
6 : */
7 :
8 : #include "Morpheus_Vector.h"
9 : #include <cmath>
10 : #include <iostream>
11 :
12 : // Returns true if | a-b | < tol, false otherwise
13 : bool approxEqual(double a, double b, double tol);
14 :
15 1 : int main()
16 : {
17 1 : bool testPassed = true;
18 1 : int numEntries = 5;
19 :
20 : // Create a vector with 10 entries
21 1 : Morpheus::Vector vec(numEntries);
22 :
23 : // Set the entries in this vector
24 : // Each entry is 1/sqrt(n)
25 1 : double rootN = std::sqrt(numEntries);
26 1 : double invRootN = 1./rootN;
27 6 : for(int i=0; i<numEntries; i++) {
28 5 : vec[i] = invRootN;
29 : }
30 :
31 : // 1-norm should be sqrt(n)
32 : // infinity-norm should be 1/sqrt(n)
33 : // 2-norm should be 1
34 1 : double norm1 = vec.norm1();
35 1 : double normInf = vec.normInf();
36 1 : double norm2 = vec.norm2();
37 :
38 1 : if(!approxEqual(norm1, sqrt(numEntries), 1e-10))
39 : {
40 0 : std::cout << "ERROR: The 1-norm is incorrect\n";
41 0 : testPassed = false;
42 : }
43 1 : if(!approxEqual(normInf, invRootN, 1e-10))
44 : {
45 0 : std::cout << "ERROR: The infinity-norm is incorrect\n";
46 0 : testPassed = false;
47 : }
48 1 : if(!approxEqual(norm2, 1, 1e-10))
49 : {
50 0 : std::cout << "ERROR: The 2-norm is incorrect\n";
51 0 : testPassed = false;
52 : }
53 :
54 1 : if(testPassed)
55 1 : std::cout << "Norm test: PASSED!\n";
56 : else
57 0 : std::cout << "Norm test: FAILED!\n";
58 : }
59 :
60 :
61 : // Returns true if | a-b | < tol, false otherwise
62 3 : bool approxEqual(double a, double b, double tol)
63 : {
64 3 : if(std::abs(a-b) < tol)
65 3 : return true;
66 0 : return false;
67 3 : }
|