__author__ = 'matt'
import sys
from math import sqrt,exp
from cairs_coor import Coor

class MeanConstant(object):
    def __init__(self,mean=0.0):
        self.mean=mean
        print('Prior has a constant mean of %s'%mean)
        self.extend_v=None
        self.pos_v=None
        self.d=None

    def f_mu(self,d):
        """
        #vector of domain extend
        mean function
        :param d: domain
        :type d: Domain
        """

        self.extend_v = [d.extend.x,
                    d.extend.y,
                    d.extend.time]
        # vector of positions
        self.pos_v = [d.position.x,
                 d.position.y,
                 d.position.time]
        self.d=d
        lower=[]
        for i,val in enumerate(self.pos_v):
            if self.extend_v[i] != 0.0:
                lower.append(val)
        upper = []

        for i,val in enumerate(self.pos_v):
            if (self.extend_v[i] + val) != 0.0:
                upper.append(val)

        TODO cubature



    def f_int(self,v):
        """
        :brief : construct function to integrate over
        :param v: numbers
        :type v: list
        :return:
        :rtype:
        """
        kk=1
        for i in range(0,2,1):
            if self.extend_v[i] != 0.0:
                self.pos_v[i] = v[kk]
                kk += 1
        coorTMP=Coor(self.pos_v[1],self.pos_v[2],self.pos_v[3])
        self.f_mu(Coor.rotate(coorTMP,self.d.position,self.d.angle))


class CovExponentional(object):
    def __init__(self,sigma=10.0,l_spatial=3000.0,l_temporal=600*1000,gamma=1.0):
        """
         separable gamma-exponential

        ## Equation (4.18) in Rasmussen, C. E. and Williams, C. K. I. (2006) Gaussian processes
        ## for machine learning, Massachusett, MIT Press.
        @:param sigma -standard deviation of GP
        @:param l_spatioal -spatial correlation length
        @:param l_temporal -temporal correlation length [milliseconds]
        @:param gamma -exponent for smoothness in [0, 2]
        """
        self.sigma=sigma
        self.l_spatial=l_spatial
        self.temporal=l_temporal
        self.gamma=gamma
        if gamma<0 or gamma>2:
            sys.exit("Gamma must bi in [0,2]")

        #TODO check if time is in milliseconds
        print("- Prior has a separable gamma-exponential covariance function with:")
        print("   standard deviation: %s"%sigma)
        print("   spatial correlation length: %s"%l_spatial)
        print("   temporal correlation length [s]: %s"%(l_temporal/1000))
        print("   gamma: %s"%gamma)

    def f_cov(self,c1,c2):
        """
        :param c1: coordination
        :type c1: Coord
        :param c2: coordination
        :type c2: Coord
        :return: covariance between two points
        :rtype: number
        """
        #covariance
        var=self.sigma * self.sigma
        dist_spatial = sqrt((c1.x-c2.x)**2 + (c1.y-c2.y)**2)
        dist_temporal = abs(c1.time - c2.time)
        cov=var* exp(- (dist_spatial/self.l_spatial)**self.gamma - (dist_temporal/self.l_spatial)**self.gamma)
        return cov
