# Path: stationary/utils/math_helpers.py
# def normalize(x):
#     s = float(sum(x))
#     for j in range(len(x)):
#         x[j] /= s
#     return x
# 
# def q_divergence(q):
#     """
#     Returns the divergence function corresponding to the parameter value q. For
#     q == 0 this function is one-half the squared Euclidean distance. For q == 1
#     this function returns the KL-divergence.
#     """
# 
#     if q == 0:
#         def d(x, y):
#             return 0.5 * numpy.dot((x - y), (x - y))
#         return d
#     if q == 1:
#         return kl_divergence
#     if q == 2:
#         def d(x, y):
#             s = 0.
#             for i in range(len(x)):
#                 s += log(x[i] / y[i]) + 1 - x[i] / y[i]
#             return -s
#         return d
#     q = float(q)
# 
#     def d(x, y):
#         s = 0.
#         for i in range(len(x)):
#             s += (numpy.power(y[i], 2 - q) - numpy.power(x[i], 2 - q)) / (2 - q)
#             s -= numpy.power(y[i], 1 - q) * (y[i] - x[i])
#         s = -s / (1 - q)
#         return s
#     return d


from collections import defaultdict, Callable
from .math_helpers import normalize, q_divergence
import numpy

    Returns
    -------
    Dictionary mapping states to D(E(state), state)
    """

    dist = q_divergence(q_d)
    e = defaultdict(float)

    if isinstance(edges, list):
        for x, y, w in edges:
            e[x] += numpy.array(y) * w
    elif isinstance(edges, Callable):
        if not states:
            raise ValueError(
                "Keyword argument `states` required with edge_func")
        for x in states:
            e[x] = 0.
            for y in states:
                w = edges(x, y)
                e[x] += numpy.array(y) * w
    d = dict()
    for state, v in e.items():
        # Some divergences do not play well on the boundary
        if not boundary:
            p = 1.
            for s in state:
                p *= s
            if p == 0:
                continue

# Path: stationary/utils/math_helpers.py
# def normalize(x):
#     s = float(sum(x))
#     for j in range(len(x)):
#         x[j] /= s
#     return x
# 
# def q_divergence(q):
#     """
#     Returns the divergence function corresponding to the parameter value q. For
#     q == 0 this function is one-half the squared Euclidean distance. For q == 1
#     this function returns the KL-divergence.
#     """
# 
#     if q == 0:
#         def d(x, y):
#             return 0.5 * numpy.dot((x - y), (x - y))
#         return d
#     if q == 1:
#         return kl_divergence
#     if q == 2:
#         def d(x, y):
#             s = 0.
#             for i in range(len(x)):
#                 s += log(x[i] / y[i]) + 1 - x[i] / y[i]
#             return -s
#         return d
#     q = float(q)
# 
#     def d(x, y):
#         s = 0.
#         for i in range(len(x)):
#             s += (numpy.power(y[i], 2 - q) - numpy.power(x[i], 2 - q)) / (2 - q)
#             s -= numpy.power(y[i], 1 - q) * (y[i] - x[i])
#         s = -s / (1 - q)
#         return s
#     return d


# Path: stationary/utils/expected_divergence_.py
from collections import defaultdict, Callable
from .math_helpers import normalize, q_divergence
import numpy


    Returns
    -------
    Dictionary mapping states to D(E(state), state)
    """

    dist = q_divergence(q_d)
    e = defaultdict(float)

    if isinstance(edges, list):
        for x, y, w in edges:
            e[x] += numpy.array(y) * w
    elif isinstance(edges, Callable):
        if not states:
            raise ValueError(
                "Keyword argument `states` required with edge_func")
        for x in states:
            e[x] = 0.
            for y in states:
                w = edges(x, y)
                e[x] += numpy.array(y) * w
    d = dict()
    for state, v in e.items():
        # Some divergences do not play well on the boundary
        if not boundary:
            p = 1.
            for s in state:
                p *= s
            if p == 0:
                continue

        d[state] = dist(normalize(v), normalize(list(state)))