SharedHnorm.ipynbOpen in CoCalc
Jupyter notebook Hnorm.ipynb
import numpy as np
from cvxopt import matrix, solvers
def Hnorm(A):
    # Regner ut Hilbert-norm til numpy matrise A
    rad, kol = A.shape
    nullr = np.zeros((rad,rad))
    nullk = np.zeros((kol,kol))
    C = np.bmat([[nullr,A],[A.T,nullk]])

    h0 = [ -0.5*matrix(C) ]

    dim = rad+kol
    c = matrix(np.ones(dim,))
    # Vil lage diagonalmatriser A_i med 1 på plass (i,i), 0 ellers
    diagonal = np.zeros(dim,)
    diagonal[0] = 1
    A0 = np.diag(diagonal)
    G = A0
    for i in xrange(1,dim):
        A0 = np.diag(np.roll(diagonal,i))
        G = np.bmat([[G],[A0]])
    # G0 er nå blokk matrise med A_i'ene stabla nedover i radene.
    G0 = [ matrix(-1.0*G) ]
    solvers.options['show_progress']=False
    sol = solvers.sdp(c, Gs=G0, hs=h0)
    norm = -1
    if sol['status'] == 'optimal':
        norm = sol['primal objective']
    return norm

def ekstrem_ell_infty_recursive(N):
    ekstrem_punkt = np.zeros((2**(N-1),N),dtype=np.int)
    if N == 1:
        ekstrem_punkt[0] = 1
    else:
        ekstrem_punkt[:,0] = np.ones((2**(N-1),),dtype=np.int)
        E = ekstrem_ell_infty_recursive(N-1)
        ekstrem_punkt[:2**(N-2),1:] = E.copy()
        ekstrem_punkt[2**(N-2):,1:] = -1*E.copy()   
    return ekstrem_punkt
    
def ekstrem_ell_infty2(N):
    return ekstrem_ell_infty_recursive(N).transpose()

def Mnorm(A):
    # Regn ut norm max(|Ax|_1, x kolonne i E)
    rad, kol = A.shape
    E = ekstrem_ell_infty2(kol)

    # Ta absolutt verdi av elementene og summer kolonnevis
    return np.max(np.sum(np.abs(A*E),axis=0))
    # Eventuelt 
    # return reduce(max,np.sum(np.abs(A*E),axis=0))

def finnKG(matrise):
    """Tar ei matrise på formen '{{1,1},{1,-1}}' (en streng)
    og returnerer Mnorm, Hnorm og forholdet Hnorm/Mnorm"""
    M = matrise
    M = M.replace('},{',';')
    M = M.replace('{{','[')
    M = M.replace('}}',']')
    M = np.mat(M)
    norm_M = Mnorm(M)
    norm_H = Hnorm(M)
    KG = norm_H/norm_M
    return (norm_M, norm_H, KG)







map(finnKG,[be1,be2,be3,be4,be5,be6,be7,be8,be9,be10,be11])
[(16, 21.7434394278883, 1.3589649642430188), (16, 21.521007521761025, 1.345062970110064), (16, 21.565524184339818, 1.3478452615212386), (16, 21.449662347757, 1.3406038967348124), (16, 21.321063304933624, 1.3325664565583515), (16, 21.085316134684202, 1.3178322584177626), (16, 22.185441249379032, 1.3865900780861895), (16, 21.355919259260297, 1.3347449537037686), (16, 21.71765823678706, 1.3573536397991912), (16, 21.89243348092789, 1.3682770925579932), (16, 21.57494794143257, 1.3484342463395356)]















be1='{{-1,-1,1,1,0,0,0,0,0},{-1,0,0,0,1,0,1,1,0},{0,0,0,1,1,-1,1,0,0},{0,0,1,0,0,0,0,0,1},{1,0,0,1,0,1,0,0,1},{1,0,0,0,0,0,1,0,0},{0,0,1,-1,1,1,0,0,0},{0,1,1,0,1,0,0,0,-1},{0,0,0,0,0,1,1,-1,-1}}'
map(finnKG,[be1])
[(16, 21.915301159801068, 1.3697063224875667)]












be180='{{0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0},{0,0,-1,0,1,0,0,0},{0,0,0,-1,0,1,0,0},{1,0,0,1,-1,0,0,1},{0,1,1,0,0,-1,1,0},{0,0,1,1,1,0,-1,0},{0,0,1,1,0,1,0,-1}}'
map(finnKG,[be180])
[(12, 16.9705624961966, 1.4142135413497166)]