# Path: Utility.py
# def l2(x):
# def apply_scale(w, a, b):
# def logabsdiffexp(x, y):
#     def f(x, y):
# def logit_mean(x):
# def digest(x):
# def tree():
# def lift_tree(src, dst):
# def init_latex_rendering():
# def draw_confidence(a, m, S, levels = [0.95]):


import numpy as np
    import ctypes
            import sys; sys.exit()
from scipy import optimize as opt
from Utility import logsumexp, logabsdiffexp, digest, apply_scale
        k = np.where(col < c)[0][0]
        i = np.where(A[:,j] > A[:,k])[0][0]
        A[i,j] = False
        A[i,k] = True
        col[j] -= 1
        col[k] += 1

    # Undo the sort
    A = A[:,oo]
 
    # Verify that the procedure found a satisfying matrix
    assert(np.all(r == np.sum(A, axis = 1)))
    assert(np.all(c_unsorted == np.sum(A, axis = 0)))

    return A

##############################################################################
# Adapting Matlab code provided by Matt Harrison (matt_harrison@brown.edu).
##############################################################################

def clear_cache():
    _dict_canonical_scalings = {}
    _dict_conjugate = {}

_dict_canonical_scalings = {}
def canonical_scalings(w, r, c):
    """From weights and margins, find scalings to balance a matrix.

TODO: describe "rc" method.
    """

# Path: Utility.py
# def l2(x):
# def apply_scale(w, a, b):
# def logabsdiffexp(x, y):
#     def f(x, y):
# def logit_mean(x):
# def digest(x):
# def tree():
# def lift_tree(src, dst):
# def init_latex_rendering():
# def draw_confidence(a, m, S, levels = [0.95]):


# Path: BinaryMatrix.py
import numpy as np
    import ctypes
            import sys; sys.exit()
from scipy import optimize as opt
from Utility import logsumexp, logabsdiffexp, digest, apply_scale

        k = np.where(col < c)[0][0]
        i = np.where(A[:,j] > A[:,k])[0][0]
        A[i,j] = False
        A[i,k] = True
        col[j] -= 1
        col[k] += 1

    # Undo the sort
    A = A[:,oo]
 
    # Verify that the procedure found a satisfying matrix
    assert(np.all(r == np.sum(A, axis = 1)))
    assert(np.all(c_unsorted == np.sum(A, axis = 0)))

    return A

##############################################################################
# Adapting Matlab code provided by Matt Harrison (matt_harrison@brown.edu).
##############################################################################

def clear_cache():
    _dict_canonical_scalings = {}
    _dict_conjugate = {}

_dict_canonical_scalings = {}
def canonical_scalings(w, r, c):
    """From weights and margins, find scalings to balance a matrix.

TODO: describe "rc" method.
    """

    w_hash = digest(w)