# Path: dev/bayesLib.py
# def int2flat(cameraMatrix, distCoeffs, model):
# def flat2CamMatrix(kFlat, model):
# def flat2int(X, Ns, model):
# def ext2flat(rVec, tVec):
# def flat2ext(X):
# def errorCuadraticoImagen(Xext, Xint, Ns, params, j, mahDist=False):
# def etotalExt(Xext, Xint, Ns, params, j):
# def errorCuadraticoInt(Xint, Ns, XextList, params, mahDist=False):
# def etotalInt(Xint, Ns, XextList, params):
#     def __init__(self, Ns, XextList, params, sampleador):
#     def nuevo(self, old, oldE):
#     def __init__(self, Xint, Ns, params, sampleador, priorExt, W):
#     def ePrior(self, Xext):
#     def etotalExt(self, Xext):
#     def nuevo(self, old, oldE):
# def procJint(Xint, Ns, XextList, params, ret):
# def procHint(Xint, Ns, XextList, params, ret):
# def procJext(Xext, Xint, Ns, params, j, ret):
# def procHext(Xext, Xint, Ns, params, j, ret):
# def jacobianos(Xint, Ns, XextList, params, hessianos=True):
# def varVar2(c, N):
# def varMahal(c1, n, c2, rank=False):
# def trasPerMAt(k,l):
# def varVarN(c, Nsamples):
#     X = np.concatenate((kFlat, dFlat))
#     X = np.concatenate((rFlat, tFlat))
#         S = np.linalg.inv(Cm)  # inverse of covariance matrix
#     A = vecA.reshape((k,l))
# class metHas:
# class metHasExt:


import numpy as np
import scipy.stats as sts
import scipy.special as spe
import matplotlib.pyplot as plt
import scipy.linalg as ln
from dev import bayesLib as bl
        ranking = sts.chi2.cdf(mahDist, 3)
        return mahDist, ranking
    else:
        return mahDist

# elimino uno de los componentes que es redundante # y lo multiplico
# mul = np.array([[1],[2],[1]])
ind = [0,1,3]
ciVar = varVar(ci, N)[ind] # * mul
cjVar = varVar(cj, N)[ind] # * mul
ciVar = ciVar.T[ind].T
cjVar = cjVar.T[ind].T

ciFlat = np.reshape(ci, -1)[ind]
cjFlat = np.reshape(cj, -1)[ind]
cFlatDif = ciFlat - cjFlat

A =  varVar(ci, N)
ln.svd(A)


ciPres = np.linalg.inv(ciVar)
cjPres = np.linalg.inv(cjVar)

dMahi = cFlatDif.dot(ciPres).dot(cFlatDif)
dMahj = cFlatDif.dot(cjPres).dot(cFlatDif)

varMahal(ci, N, cj)
varMahal(cj, N, ci)


# Path: dev/bayesLib.py
# def int2flat(cameraMatrix, distCoeffs, model):
# def flat2CamMatrix(kFlat, model):
# def flat2int(X, Ns, model):
# def ext2flat(rVec, tVec):
# def flat2ext(X):
# def errorCuadraticoImagen(Xext, Xint, Ns, params, j, mahDist=False):
# def etotalExt(Xext, Xint, Ns, params, j):
# def errorCuadraticoInt(Xint, Ns, XextList, params, mahDist=False):
# def etotalInt(Xint, Ns, XextList, params):
#     def __init__(self, Ns, XextList, params, sampleador):
#     def nuevo(self, old, oldE):
#     def __init__(self, Xint, Ns, params, sampleador, priorExt, W):
#     def ePrior(self, Xext):
#     def etotalExt(self, Xext):
#     def nuevo(self, old, oldE):
# def procJint(Xint, Ns, XextList, params, ret):
# def procHint(Xint, Ns, XextList, params, ret):
# def procJext(Xext, Xint, Ns, params, j, ret):
# def procHext(Xext, Xint, Ns, params, j, ret):
# def jacobianos(Xint, Ns, XextList, params, hessianos=True):
# def varVar2(c, N):
# def varMahal(c1, n, c2, rank=False):
# def trasPerMAt(k,l):
# def varVarN(c, Nsamples):
#     X = np.concatenate((kFlat, dFlat))
#     X = np.concatenate((rFlat, tFlat))
#         S = np.linalg.inv(Cm)  # inverse of covariance matrix
#     A = vecA.reshape((k,l))
# class metHas:
# class metHasExt:


# Path: dev/varianceOfmatrixvariance.py
import numpy as np
import scipy.stats as sts
import scipy.special as spe
import matplotlib.pyplot as plt
import scipy.linalg as ln
from dev import bayesLib as bl

        ranking = sts.chi2.cdf(mahDist, 3)
        return mahDist, ranking
    else:
        return mahDist

# elimino uno de los componentes que es redundante # y lo multiplico
# mul = np.array([[1],[2],[1]])
ind = [0,1,3]
ciVar = varVar(ci, N)[ind] # * mul
cjVar = varVar(cj, N)[ind] # * mul
ciVar = ciVar.T[ind].T
cjVar = cjVar.T[ind].T

ciFlat = np.reshape(ci, -1)[ind]
cjFlat = np.reshape(cj, -1)[ind]
cFlatDif = ciFlat - cjFlat

A =  varVar(ci, N)
ln.svd(A)


ciPres = np.linalg.inv(ciVar)
cjPres = np.linalg.inv(cjVar)

dMahi = cFlatDif.dot(ciPres).dot(cFlatDif)
dMahj = cFlatDif.dot(cjPres).dot(cFlatDif)

varMahal(ci, N, cj)
varMahal(cj, N, ci)


bl.varMahal(ci, N, cj, rank=True)