//
//  convergence_criteria.h
//
//
//  Created by Mehdi Stapleton on 2015-07-03.
//
//

#ifndef __convergence_criteria__
#define __convergence_criteria__

#include <vector>
#include <cmath>

//---------------------------------------------------------------
//                      TYPE DEFINITIONS
//---------------------------------------------------------------


//---------------------------------------------------------------
//                  STOP CONDITION FUNCTIONS
//---------------------------------------------------------------

template< typename T>
T relative_norm_delta(T* curr, T* next, uint32_t nPts)
{
    T relNorm  = 0;
    T nextNorm = 0;
    
    for(uint32_t i=0; i< nPts; i++)
    {
        relNorm  += pow(curr[i] - next[i], 2.0);
        nextNorm += pow(next[i], 2.0);
    }
    
    if(nextNorm == 0)
    {
        if(relNorm == 0)
        {
            relNorm = 0;
        }
        else
        {
            //leave relative norm = difference norm
        }
    }
    else
    {
        relNorm /= nextNorm;
    }
    
    relNorm = sqrt(relNorm);

    return relNorm;
}



#endif /* defined(__convergence_criteria__) */
