Ejemplo n.º 1
0
def hodge_decomposition(omega):
    """
    For a given p-cochain \omega there is a unique decomposition
    
    \omega = d(\alpha) + \delta(\beta) (+) h
    
    for p-1 cochain \alpha, p+1 cochain \beta, and harmonic p-cochain h.
    
    This function returns (non-unique) representatives \beta, \gamma, and h 
    which satisfy the equation above.
    
    Example:
        #decompose a random 1-cochain
        sc = SimplicialComplex(...)
        omega = sc.get_cochain(1)
        omega.[:] = rand(*omega.shape)
        (alpha,beta,h) = hodge_decomposition(omega)

    """
    sc = omega.complex
    p = omega.k
    alpha = sc.get_cochain(p - 1)
    beta = sc.get_cochain(p + 1)

    # Solve for alpha
    A = delta(d(sc.get_cochain_basis(p - 1))).v
    b = delta(omega).v
    alpha.v = cg(A, b, tol=1e-8)[0]

    # Solve for beta
    A = d(delta(sc.get_cochain_basis(p + 1))).v
    b = d(omega).v
    beta.v = cg(A, b, tol=1e-8)[0]

    # Solve for h
    h = omega - d(alpha) - delta(beta)

    return (alpha, beta, h)
Ejemplo n.º 2
0
def hodge_decomposition(omega):
    """
    For a given p-cochain \omega there is a unique decomposition
    
    \omega = d(\alpha) + \delta(\beta) (+) h
    
    for p-1 cochain \alpha, p+1 cochain \beta, and harmonic p-cochain h.
    
    This function returns (non-unique) representatives \beta, \gamma, and h 
    which satisfy the equation above.
    
    Example:
        #decompose a random 1-cochain
        sc = SimplicialComplex(...)
        omega = sc.get_cochain(1)
        omega.[:] = rand(*omega.shape)
        (alpha,beta,h) = hodge_decomposition(omega)

    """
    sc = omega.complex    
    p = omega.k
    alpha = sc.get_cochain(p - 1)
    beta  = sc.get_cochain(p + 1)    

    # Solve for alpha
    A = delta(d(sc.get_cochain_basis(p - 1))).v
    b = delta(omega).v
    alpha.v = cg( A, b, tol=1e-8 )[0]

    # Solve for beta
    A = d(delta(sc.get_cochain_basis(p + 1))).v
    b = d(omega).v
    beta.v = cg( A, b, tol=1e-8 )[0]
    
    # Solve for h
    h = omega - d(alpha) - delta(beta)    
    
    return (alpha,beta,h)
def plotjson(fn):
    """
    plotjson: make plots from json output of fiedler.py

    fn: the filename of the json file
    """

    fo=open(fn)
    data=json.load(fo)
    fo.close()
    if "adj" in data:
        (A,adj,Npts) = fiedler.adj_mat(data["adj"])
        #scew symetricise

        A = (A.T - A)/2

        A=A.tocoo()
        pos=A.data>0
        skew = numpy.column_stack((A.row[pos],A.col[pos],A.data[pos])).tolist()
        
        # #method from hodge decomposition driver.py
        # sc = simplicial_complex(([[el] for el in range(0,A.shape[0])],numpy.column_stack((A.row[pos],A.col[pos])).tolist()))
        # omega = sc.get_cochain(1)
        # omega.v[:] = A.data[pos]
        # p = omega.k
        # alpha = sc.get_cochain(p - 1)
        # #beta  = sc.get_cochain(p + 1)    

        # # Solve for alpha
        # A2 = delta(d(sc.get_cochain_basis(p - 1))).v
        # b = delta(omega).v
        # rank=cg( A2, b, tol=1e-8 )[0]

        # method from ranking driver.py

        asc = abstract_simplicial_complex([numpy.column_stack((A.row[pos],A.col[pos])).tolist()])
        B1 = asc.chain_complex()[1] # boundary matrix
        rank = lsqr(B1.T, A.data[pos])[0] # solve least squares problem
        
        sc = simplicial_complex(([[el] for el in range(0,A.shape[0])],numpy.column_stack((A.row[pos],A.col[pos])).tolist()))
        omega = sc.get_cochain(1)
        omega.v[:] = A.data[pos]
        p = omega.k
        alpha = sc.get_cochain(p - 1)
        
        alpha.v = rank
        v = A.data[pos]-d(alpha).v
        
        cyclic_adj_list=numpy.column_stack((A.row[pos],A.col[pos],v)).tolist()
        div_adj_list=numpy.column_stack((A.row[pos],A.col[pos],d(alpha).v)).tolist()

        data["hodge"]=list(rank)
        data["hodgerank"]=list(numpy.argsort(numpy.argsort(rank)))
        fo = open(fn,"w")
        json.dump(data,fo, indent=2)
        fo.close()

        fn=fn+".abstract"
        #fiedler.doPlots(numpy.array(data["f1"]),numpy.array(data["f2"]),numpy.array(data["d"]),cyclic_adj_list,fn+".decomp.cyclic.",widths=[6],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),numpy.array(data["f2"]),numpy.array(data["d"]),div_adj_list,fn+".decomp.acyclic.",widths=[6],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),numpy.array(data["f2"]),numpy.array(data["d"]),data["adj"],fn+".decomp.acyclic.over.all.",widths=[6],vsdeg=False,nByi=data["nByi"],adj_list2=div_adj_list,directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),cyclic_adj_list,fn+".decomp.harmonic.v.grad.",widths=[6],heights=[2],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),skew,fn+".decomp.skew.v.grad.",widths=[6],heights=[2],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),data["adj"],fn+".decomp.acyclic.over.all.v.grad.",widths=[6],heights=[2],vsdeg=False,nByi=data["nByi"],adj_list2=div_adj_list,directed=True)
        fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),data["adj"],fn+".all.v.grad.",widths=[24],heights=[6],vsdeg=False,nByi=data["nByi"],directed=False)