View on GitHub

computational-mathematics

Contains basic implementations of a number of standard procedures in scientific computing and computational mathematics.

Routine Name: vectorErrorLInf

Author: Christopher Johnson

Language: C++11. Tested with g++ compiler.

Declared in “vectorCode.h”

Description/Purpose: Calculates the l-infinity norm of the difference between input Vectors.

Input: Vector u and Vector v, typedef of std::vector<double>;

Output: A double, indicating the l-infinity norm of the error between input vectors.

Usage/Example: The user must first define 2 Vectors, after which the method may be called on the vectors.

Vector a = {1,2,3};
Vector a_bar = {1.1, 1.9, 3.2};
std::cout << "l-infity error: " << vectorErrorLInf(a,a_bar) << std::endl;

Output from the lines above:

l2 error: 0.244949

Implementation/Code: The following is the code for vectorErrorLInf(v)

double vectorErrorL1 (Vector u, Vector v)
{
    auto err = vectorDiff(u,v);
    return vectorNormLInf(err);
}

The code for vectorDiff(u,v) is automatically inlined by the compiler, causing it to be inserted into the vector error method without having to call the function at runtime. This increases the running speed, at the cost of a slightly larger executable.

inline std::vector<double> vectorDiff (Vector u, Vector v)
{
    if (u.size() != v.size())
        return Vector();

    Vector diff (u.size(), 0);
    for(int i=0; i<u.size(); i++)
        diff[i] = v[i]-u[i];
    return diff;
}

Last Modified: October/2017