Exemple #1
0
    def calc_part_factor(self):
        """
        Compute participation factor of states in eigenvalues

        Returns
        -------

        """
        mu, N = numpy.linalg.eig(self.As)
        # TODO: use scipy.sparse.linalg.eigs(self.As)

        N = matrix(N)
        n = len(mu)
        idx = range(n)

        W = matrix(spmatrix(1.0, idx, idx, (n, n), N.typecode))
        gesv(N, W)

        partfact = mul(abs(W.T), abs(N))

        b = matrix(1.0, (1, n))
        WN = b * partfact
        partfact = partfact.T

        for item in idx:
            mu_real = mu[item].real
            mu_imag = mu[item].imag
            mu[item] = complex(round(mu_real, 4), round(mu_imag, 4))
            partfact[item, :] /= WN[item]

        # participation factor
        self.mu = matrix(mu)
        self.part_fact = matrix(partfact)

        return self.mu, self.part_fact
Exemple #2
0
def denseSolve(A,b):
    ''' solves an Ax = b matrix system with gesv'''
    if isinstance(A,np.ndarray):
        aLocal = cvxopt.matrix(A)
        bLocal = cvxopt.matrix(b)
        lapack.gesv(aLocal,bLocal)
        return np.array(bLocal).flatten()
    else:
        return linsolve(A,b)
Exemple #3
0
def L1Regression(A, b, alpha, trace=False):
    """
    solves
    minimize_{x} ||A*x-b||^2_2 + alpha*||x||_1
   """
    (n, m) = A.size
    Id = spmatrix(1.0, range(m), range(m))  ##unit matrix
    G = matrix([A.T, -A.T])
    h = matrix(alpha, (2 * m, 1))
    solvers.options['show_progress'] = trace
    sol = solvers.qp(Id, b, G, h)
    z = sol['x']
    x = A.trans() * (b - z)
    A_tA = A.trans() * A
    lapack.gesv(A_tA, x)
    return x
Exemple #4
0
def L1Regression(A,b,alpha,trace=False):
   """
    solves
    minimize_{x} ||A*x-b||^2_2 + alpha*||x||_1
   """
   (n,m)=A.size
   Id = spmatrix(1.0, range(m),range(m)) ##unit matrix
   G = matrix([A.T,-A.T])
   h = matrix(alpha, (2*m,1))
   solvers.options['show_progress'] = trace
   sol= solvers.qp(Id, b, G, h)
   z= sol['x']
   x= A.trans()*(b-z)
   A_tA = A.trans()*A
   lapack.gesv(A_tA, x)
   return x 
Exemple #5
0
def compute_eigs(As):
    (mu, N) = eig(matrix(As))
    N = matrix(N)
    n = len(mu)
    idx = range(n)
    W = matrix(spmatrix(1.0, idx, idx, (n, n), v.typecode))
    gesv(N, W)
    pf = mul(abs(W.T), abs(V))
    b = matrix(1.0, (1, n))
    WN = b * pf
    pf = pf.T
    for item in idx:
        mur = mu[item].real
        mui = mu[item].imag
        mu[item] = complex(round(mur, 5), round(mui, 5))
        pf[item, :] /= WN[item]
Exemple #6
0
def PLS2D_getBeta(theta, ZtX, ZtY, XtX, ZtZ, XtY, YtX, YtZ, XtZ, YtY, n, P,
                  tinds, rinds, cinds):

    # Obtain Lambda
    Lambda = mapping2D(theta, tinds, rinds, cinds)

    # Obtain Lambda'
    Lambdat = spmatrix.trans(Lambda)

    # Obtain Lambda'Z'Y and Lambda'Z'X
    LambdatZtY = Lambdat * ZtY
    LambdatZtX = Lambdat * ZtX

    # Set the factorisation to use LL' instead of LDL'
    cholmod.options['supernodal'] = 2

    # Obtain the cholesky decomposition
    LambdatZtZLambda = Lambdat * ZtZ * Lambda
    I = spmatrix(1.0, range(Lambda.size[0]), range(Lambda.size[0]))
    chol_dict = sparse_chol2D(LambdatZtZLambda + I,
                              perm=P,
                              retF=True,
                              retP=False,
                              retL=False)
    F = chol_dict['F']

    # Obtain C_u (annoyingly solve writes over the second argument,
    # whereas spsolve outputs)
    Cu = LambdatZtY[P, :]
    cholmod.solve(F, Cu, sys=4)

    # Obtain RZX
    RZX = LambdatZtX[P, :]
    cholmod.solve(F, RZX, sys=4)

    # Obtain RXtRX
    RXtRX = XtX - matrix.trans(RZX) * RZX

    # Obtain beta estimates (note: gesv also replaces the second
    # argument)
    betahat = XtY - matrix.trans(RZX) * Cu
    try:
        lapack.posv(RXtRX, betahat)
    except:
        lapack.gesv(RXtRX, betahat)

    return (betahat)
Exemple #7
0
def part_factor(As):
    """Compute participation factor of states in eigenvalues"""
    mu, N = numpy.linalg.eig(As)
    N = matrix(N)
    n = len(mu)
    idx = range(n)
    W = matrix(spmatrix(1.0, idx, idx, (n, n), N.typecode))
    gesv(N, W)
    partfact = mul(abs(W.T), abs(N))
    b = matrix(1.0, (1, n))
    WN = b * partfact
    partfact = partfact.T

    for item in idx:
        mu_real = mu[item].real
        mu_imag = mu[item].imag
        mu[item] = complex(round(mu_real, 4), round(mu_imag, 4))
        partfact[item, :] /= WN[item]

    # participation factor:
    return matrix(mu), matrix(partfact)
def compute_eig(As):
    mu, N = eig(matrix(As))
    N = matrix(N)
    n = len(mu)
    idx = range(n)
    W = matrix(spmatrix(1.0, idx, idx, (n, n), N.typecode))
    gesv(N, W)
    # W = np.linalg.pinv(N)
    # W = matrix(W)
    pf = mul(abs(W.T), abs(N))
    b = matrix(1.0, (1, n))
    WN = b * pf
    pf = pf.T

    for item in idx:
        mur = mu[item].real
        mui = mu[item].imag
        mu[item] = complex(round(mur, 5), round(mui, 5))
        pf[item, :] /= WN[item]

        # print(pf)
    return pf
Exemple #9
0
    def calc_part_factor(self, As=None):
        """
        Compute participation factor of states in eigenvalues

        Returns
        -------

        """
        if As is None:
            As = self.As
        mu, N = np.linalg.eig(As)

        N = matrix(N)
        n = len(mu)
        idx = range(n)

        mu_complex = np.array([0] * n, dtype=complex)
        W = matrix(spmatrix(1.0, idx, idx, As.shape, N.typecode))
        gesv(N, W)

        partfact = mul(abs(W.T), abs(N))

        b = matrix(1.0, (1, n))
        WN = b * partfact
        partfact = partfact.T

        for item in idx:
            mu_real = float(mu[item].real)
            mu_imag = float(mu[item].imag)
            mu_complex[item] = complex(round(mu_real, 5), round(mu_imag, 5))
            partfact[item, :] /= WN[item]

        # participation factor
        self.mu = matrix(mu_complex)
        self.part_fact = matrix(partfact)

        return self.mu, self.part_fact
Exemple #10
0
Solution:
     (x, y, z) = ( 3/10, 2/5, 0)

CVXOPT  cvxopt.lapack.gesv(A, B[, ipiv = None]) Solves
    A X = B,
where A and B are real or complex matrices, with A square and nonsingular.
On entry, B contains the right-hand side B; on exit it contains the solution X.

To run:
python pyalad/test_linear_solver.py

"""

tA = np.array([[2, 1, 3], [2, 6, 8], [6, 8, 18]], dtype=float)
tb = np.array([1, 3, 5], dtype=float)

print tA
print tb

A = matrix(tA)
b = matrix(tb)
print A
print b

x = matrix(b)
print x

lapack.gesv(A, x)
print A * x
print(3. / 10, 2. / 5, 0)
Exemple #11
0
    def find_ellipsoid(self):
    
        # return None if not enough data is available
        if(self.pa.size[1] < 3 or self.pb.size[1] < 3):
            return None
        
        # homogenize coordinates
        pah = self.homogenize(self.pa)
        pbh = self.homogenize(self.pb)
        dim = pah.size[0]
        num_pah = pah.size[1]
        num_pbh = pbh.size[1]
        
        # get the c vector
        num_vars = 1+(dim-1)*(dim-1)+2*num_pah+dim*dim
        c = matrix([0.0]*num_vars, (num_vars,1))
        c[0,0] = -self.c1
        
        for i in xrange(dim-1):
            for j in xrange(dim-1):
                if(i == j):
                    c[1+i*(dim-1)+j,0] = self.c2
        for i in xrange(num_pah):
            c[1+(dim-1)*(dim-1)+i,0] = self.c3
        for i in xrange(num_pah):
            c[1+(dim-1)*(dim-1)+num_pah+i,0] = 0.0
        for i in xrange(dim*dim):
            c[1+(dim-1)*(dim-1)+num_pah+num_pah+i,0] = 0.0

        # get Gl and hl
        # separation constraint
        Gl_t = []
        for i in xrange(num_pah):
            row = [0.0]*(1+(dim-1)*(dim-1)+num_pah)+[0.0]*num_pah
            row[(1+(dim-1)*(dim-1)+num_pah)+i] = -1.0
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(pah[j,i]*pah[k,i])
            Gl_t.append(row)
        for i in xrange(num_pbh):
            row = [1.0]+[0.0]*((dim-1)*(dim-1)+2*num_pah)
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(-1.0*pbh[j,i]*pbh[k,i])
            Gl_t.append(row)
        # l1 norm constraint
        for i in xrange(num_pah):
            row = [0.0]*num_vars
            row[1+(dim-1)*(dim-1)+i] = -1.0
            row[1+(dim-1)*(dim-1)+num_pah+i] = -1.0
            Gl_t.append(row)
        for i in xrange(num_pah):
            row = [0.0]*num_vars
            row[1+(dim-1)*(dim-1)+i] = -1.0
            row[1+(dim-1)*(dim-1)+num_pah+i] = 1.0
            Gl_t.append(row)
        # construct Gl and hl
        Gl = matrix(Gl_t).trans()
        col = [0.0]*(num_pah+num_pbh)+[-1.0]*num_pah+[1.0]*num_pah
        hl = matrix(col,(3*num_pah+num_pbh,1))
        
        # get Gs and hs
        # positive semidefinite constraint
        # E must be positive semidefinite
        Gs = []
        for i in xrange(1+(dim-1)*(dim-1)+2*num_pah):
            Gs.append([0.0]*(dim*dim))
        for i in xrange(dim*dim):
            v = [0.0]*(dim*dim)
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == cpos):
                v[i] = -1.0
            else:
                v[i] = -0.5
                v[cpos*dim+rpos] = -0.5
            Gs.append(v)
        Gs = [matrix(Gs)]
        hs = [matrix([0.0]*(dim*dim),(dim,dim))]
        
        # block matrix must be positive semidefinite
        Gs2 = []
        Gs2.append([0.0]*(((dim-1)*2)*((dim-1)*2)))
        for i in xrange((dim-1)*(dim-1)):
            v = [0.0]*(((dim-1)*2)*((dim-1)*2))
            rpos = int(math.floor(float(i)/float(dim-1)))
            cpos = i % (dim-1)
            if(rpos == cpos):
                v[(rpos+dim-1)*(dim-1)*2+(dim-1)+cpos] = -1.0
            else:
                v[(rpos+dim-1)*(dim-1)*2+(dim-1)+cpos] = -0.5
                v[(cpos+dim-1)*(dim-1)*2+(dim-1)+rpos] = -0.5
            Gs2.append(v)
            
        for i in xrange(2*num_pah):
            Gs2.append([0.0]*(((dim-1)*2)*((dim-1)*2)))
        for i in xrange(dim*dim):
            v = [0.0]*(((dim-1)*2)*((dim-1)*2))
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == 0 or cpos == 0):
                Gs2.append(v)
            else:
                rpos = rpos-1
                cpos = cpos-1
                if(rpos == cpos):
                    v[rpos*(dim-1)*2+cpos] = -1.0
                else:
                    v[rpos*(dim-1)*2+cpos] = -0.5
                    v[cpos*(dim-1)*2+rpos] = -0.5
                Gs2.append(v)
            
        Gs.append(matrix(Gs2))
        zero_block = matrix([0.0]*((dim-1)*(dim-1)),((dim-1), (dim-1)))
        eye_block = spmatrix(1.0, range(dim-1), range(dim-1))
        hs2 = matrix([[zero_block, eye_block], [eye_block, zero_block]])
        hs.append(hs2)
        
        # get A and b
        # symmetry constraint
        A_t = []
        for i in xrange(dim):
            for j in xrange(i, dim):
                if(i != j):
                    v = [0.0]*(num_vars)
                    v[1+(dim-1)*(dim-1)+2*num_pah+i*dim+j] = 1.0
                    v[1+(dim-1)*(dim-1)+2*num_pah+j*dim+i] = -1.0
                    A_t.append(v)
        for i in xrange(dim-1):
            for j in xrange(i, dim-1):
                if(i != j):
                    v = [0.0]*(num_vars)
                    v[1+i*(dim-1)+j] = 1.0
                    v[1+j*(dim-1)+i] = -1.0
                    A_t.append(v)
        A = matrix(A_t).trans()
        b = matrix([0.0]*A.size[0],(A.size[0],1))
        
        # solve it
        passed = False
        ntime = 10
        while(passed == False and ntime > 0):
            try:
                sol = solvers.sdp(c, Gl, hl, Gs, hs, A, b)
                passed = True
            except ZeroDivisionError:
                time.sleep(0.001)
                ntime = ntime-1
            except Exception:
                time.sleep(0.001)
                ntime = ntime-1
        if(passed == False):
            tmp_nrow = self.pa.size[0]
            tmp_c = mean_ps(self.pa).trans()
            tmp_E = matrix(spmatrix(1.0, range(tmp_nrow), range(tmp_nrow)))
            tmp_rho = 1.0
            return {'c':tmp_c, 'E':tmp_E, 'rho':tmp_rho}
        
        # parse out solution
        x = sol['x']
        k = x[0]
        E_hat = matrix(x[1+(dim-1)*(dim-1)+2*num_pah:], (dim,dim))
        F = E_hat[1:,1:]
        v = E_hat[1:,0]
        s = E_hat[0,0]
        ipiv = matrix(0, (dim-1,1))
        gesv(-F, v, ipiv)
        c = v
        btm = 1-(s-c.trans()*F*c)+0.00000001
        for i in xrange(F.size[0]):
            for j in xrange(F.size[1]):
                F[i,j] = F[i,j]/btm
        E = F
        rho = k
        
        # function return
        return {'c':c, 'E':E, 'rho':rho}
Exemple #12
0
__author__ = 'stefan'
from cvxopt import matrix, normal
from cvxopt.lapack import gesv, getrs
n = 10
A = normal(n,n)
b = normal(n)
ipiv = matrix(0, (n,1))
x = +b
gesv(A, x, ipiv)               # x = A^{-1}*b
x2 = +b
getrs(A, ipiv, x2, trans='T')  # x2 = A^{-T}*b
x += x2
print(x)
Exemple #13
0
    def find_ellipsoid(self):
        
        # homogenize coordinates
        pah = self.homogenize(self.pa)
        pbh = self.homogenize(self.pb)
        dim = pah.size[0]
        num_pah = pah.size[1]
        num_pbh = pbh.size[1]
        
        # get the c vector
        c = matrix([-1.0]+[0.0]*(dim*dim), (1+dim*dim,1))
        
        # get Gl and hl
        # separation constraint
        Gl_t = []
        for i in xrange(num_pah):
            row = [0.0]
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(pah[j,i]*pah[k,i])
            Gl_t.append(row)
        for i in xrange(num_pbh):
            row = [1.0]
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(-1.0*pbh[j,i]*pbh[k,i])
            Gl_t.append(row)
        Gl = matrix(Gl_t).trans()
        hl = matrix([1.0]*num_pah+[0.0]*num_pbh,(num_pah+num_pbh,1))
        
        # get Gs and hs
        # positive semidefinite constraint
        Gs = []
        Gs.append([0.0]*(dim*dim))
        for i in xrange(dim*dim):
            v = [0.0]*(dim*dim)
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == cpos):
                v[i] = -1.0
            else:
                v[i] = -0.5
                v[cpos*dim+rpos] = -0.5
            Gs.append(v)
        Gs = [matrix(Gs)]
        hs = [matrix([0.0]*(dim*dim),(dim,dim))]
        
        # get A and b
        # symmetry constraint
        A_t = []
        for i in xrange(dim):
            for j in xrange(i, dim):
                if(i != j):
                    v = [0.0]*(1+dim*dim)
                    v[1+i*dim+j] = 1.0
                    v[1+j*dim+i] = -1.0
                    A_t.append(v)
        A = matrix(A_t).trans()
        b = matrix([0.0]*A.size[0],(A.size[0],1))
        
        # solve it
        passed = False
        ntime = 10
        while(passed == False and ntime > 0):
            try:
                sol = solvers.sdp(c, Gl, hl, Gs, hs, A, b)
                passed = True
            except ZeroDivisionError:
                time.sleep(0.001)
                ntime = ntime-1
            except Exception:
                time.sleep(0.001)
                ntime = ntime-1
        if(passed == False):
            return None

        # parse out solution
        x = sol['x']
        k = x[0]
        E_hat = matrix(x[1:], (dim,dim))
        F = E_hat[1:,1:]
        v = E_hat[1:,0]
        s = E_hat[0,0]
        ipiv = matrix(0, (dim-1,1))
        gesv(-F, v, ipiv)
        c = v
        btm = 1-(s-c.trans()*F*c)+0.00000001
        for i in xrange(F.size[0]):
            for j in xrange(F.size[1]):
                F[i,j] = F[i,j]/btm
        E = F
        rho = k
        
        # function return
        return {'c':c, 'E':E, 'rho':rho}
Exemple #14
0
def PeLS2D(theta, ZtX, ZtY, XtX, ZtZ, XtY, YtX, YtZ, XtZ, YtY, n, P, I, tinds,
           rinds, cinds):

    # Obtain Lambda
    Lambda = mapping2D(theta, tinds, rinds, cinds)

    # Obtain Lambda'
    Lambdat = spmatrix.trans(Lambda)

    # Obtain Lambda'Z'Y and Lambda'Z'X
    LambdatZtY = Lambdat * ZtY
    LambdatZtX = Lambdat * ZtX

    # Obtain the cholesky decomposition
    LambdatZtZLambda = Lambdat * (ZtZ * Lambda)
    chol_dict = sparse_chol2D(LambdatZtZLambda + I,
                              perm=P,
                              retF=True,
                              retP=False,
                              retL=False)
    F = chol_dict['F']

    # Obtain C_u (annoyingly solve writes over the second argument,
    # whereas spsolve outputs)
    Cu = LambdatZtY[P, :]
    cholmod.solve(F, Cu, sys=4)

    # Obtain RZX
    RZX = LambdatZtX[P, :]
    cholmod.solve(F, RZX, sys=4)

    # Obtain RXtRX
    RXtRX = XtX - matrix.trans(RZX) * RZX

    # Obtain beta estimates (note: gesv also replaces the second
    # argument)
    betahat = XtY - matrix.trans(RZX) * Cu
    try:
        lapack.posv(RXtRX, betahat)
    except:
        lapack.gesv(RXtRX, betahat)

    # Obtain u estimates
    uhat = Cu - RZX * betahat
    cholmod.solve(F, uhat, sys=5)
    cholmod.solve(F, uhat, sys=8)

    # Obtain b estimates
    bhat = Lambda * uhat

    # Obtain residuals sum of squares
    resss = YtY - 2 * YtX * betahat - 2 * YtZ * bhat + 2 * matrix.trans(
        betahat) * XtZ * bhat + matrix.trans(
            betahat) * XtX * betahat + matrix.trans(bhat) * ZtZ * bhat

    # Obtain penalised residual sum of squares
    pss = resss + matrix.trans(uhat) * uhat

    # Obtain Log(|L|^2)
    logdet = 2 * sum(cvxopt.log(cholmod.diag(F)))

    # Obtain log likelihood
    logllh = -logdet / 2 - n / 2 * (1 + np.log(2 * np.pi * pss[0, 0]) -
                                    np.log(n))

    return (-logllh)
Exemple #15
0
def PLS2D_getSigma2(theta, ZtX, ZtY, XtX, ZtZ, XtY, YtX, YtZ, XtZ, YtY, n, P,
                    I, tinds, rinds, cinds):

    # Obtain Lambda
    #t1 = time.time()
    Lambda = mapping2D(theta, tinds, rinds, cinds)
    #t2 = time.time()
    #print(t2-t1)#3.170967102050781e-05   9

    # Obtain Lambda'
    #t1 = time.time()
    Lambdat = spmatrix.trans(Lambda)
    #t2 = time.time()
    #print(t2-t1)# 3.5762786865234375e-06

    # Obtain Lambda'Z'Y and Lambda'Z'X
    #t1 = time.time()
    LambdatZtY = Lambdat * ZtY
    LambdatZtX = Lambdat * ZtX
    #t2 = time.time()
    #print(t2-t1)#1.049041748046875e-05   13

    # Obtain the cholesky decomposition
    #t1 = time.time()
    LambdatZtZLambda = Lambdat * (ZtZ * Lambda)
    #t2 = time.time()
    #print(t2-t1)#3.790855407714844e-05   2

    #t1 = time.time()
    chol_dict = sparse_chol2D(LambdatZtZLambda + I,
                              perm=P,
                              retF=True,
                              retP=False,
                              retL=False)
    F = chol_dict['F']
    #t2 = time.time()
    #print(t2-t1)#0.0001342296600341797   1

    # Obtain C_u (annoyingly solve writes over the second argument,
    # whereas spsolve outputs)
    #t1 = time.time()
    Cu = LambdatZtY[P, :]
    cholmod.solve(F, Cu, sys=4)
    #t2 = time.time()
    #print(t2-t1)#1.5974044799804688e-05   5

    # Obtain RZX
    #t1 = time.time()
    RZX = LambdatZtX[P, :]
    cholmod.solve(F, RZX, sys=4)
    #t2 = time.time()
    #print(t2-t1)#1.2159347534179688e-05   7

    # Obtain RXtRX
    #t1 = time.time()
    RXtRX = XtX - matrix.trans(RZX) * RZX
    #t2 = time.time()
    #print(t2-t1)#9.775161743164062e-06  11

    # Obtain beta estimates (note: gesv also replaces the second
    # argument)
    #t1 = time.time()
    betahat = XtY - matrix.trans(RZX) * Cu
    try:
        lapack.posv(RXtRX, betahat)
    except:
        lapack.gesv(RXtRX, betahat)
    #t2 = time.time()
    #print(t2-t1)#1.7404556274414062e-05   6

    # Obtain u estimates
    #t1 = time.time()
    uhat = Cu - RZX * betahat
    cholmod.solve(F, uhat, sys=5)
    cholmod.solve(F, uhat, sys=8)
    #t2 = time.time()
    #print(t2-t1)#1.2874603271484375e-05   8

    # Obtain b estimates
    #t1 = time.time()
    bhat = Lambda * uhat
    #t2 = time.time()
    #print(t2-t1)#2.86102294921875e-06  15

    # Obtain residuals sum of squares
    #t1 = time.time()
    resss = YtY - 2 * YtX * betahat - 2 * YtZ * bhat + 2 * matrix.trans(
        betahat) * XtZ * bhat + matrix.trans(
            betahat) * XtX * betahat + matrix.trans(bhat) * ZtZ * bhat
    #t2 = time.time()
    #print(t2-t1)#3.409385681152344e-05   4

    # Obtain penalised residual sum of squares
    #t1 = time.time()
    pss = resss + matrix.trans(uhat) * uhat

    return (pss / n)
	def solve_SAT2(self,cnf,number_of_variables,number_of_clauses):
		#Solves CNFSAT by a Polynomial Time Approximation scheme:
		#	- Encode each clause as a linear equation in n variables: missing variables and negated variables are 0, others are 1
		#	- Solve previous system of equations by least squares algorithm to fit a line
		#	- Variable value above 0.5 is set to 1 and less than 0.5 is set to 0
		#	- Rounded of assignment array satisfies the CNFSAT with high probability
		#Returns: a tuple with set of satisfying assignments
		satass=[]
		x=[]
		self.solve_SAT(cnf,number_of_variables,number_of_clauses)
		for clause in self.cnfparsed:
			equation=[]
			for n in xrange(number_of_variables):
				equation.append(0)
			#print "clause:",clause
			for literal in clause:
				if literal[0] != "!":
					equation[int(literal[1:])-1]=1
				else:
					equation[int(literal[2:])-1]=0
			self.equationsA.append(equation)
		for n in xrange(number_of_clauses):
			self.equationsB.append(1)
		a = np.array(self.equationsA)
                b = np.array(self.equationsB)
		init_guess = []
		for n in xrange(number_of_variables):
			init_guess.append(0.00000000001)
		initial_guess = np.array(init_guess)
		self.A=a
		self.B=b
		#print "a:",a
		#print "b:",b
                #print "a.shape:",a.shape
                #print "b.shape:",b.shape
		matrixa=matrix(a,tc='d')
		matrixb=matrix(b,tc='d')

		x=None
		if number_of_variables == number_of_clauses:
			if self.Algorithm=="lsqr()":
				#x = np.dot(np.linalg.inv(a),b)
				#x = gmres(a,b) 
				#x = lgmres(a,b) 
				#x = minres(a,b) 
				#x = bicg(a,b) 
				#x = cg(a,b) 
				#x = cgs(a,b) 
				#x = bicgstab(a,b)
                		x = lsqr(a,b,atol=0,btol=0,conlim=0,show=True)
			if self.Algorithm=="lapack()":
				try:
					x = gesv(matrixa,matrixb)
					#x = gels(matrixa,matrixb)
					#x = sysv(matrixa,matrixb)
					#x = getrs(matrixa,matrixb)
					x = [matrixb]
				except:
					print "Exception:",sys.exc_info()
					return None 
			if self.Algorithm=="l1regls()":
				l1x = l1regls(matrixa,matrixb)
				ass=[]
				x=[]
				for n in l1x:
					ass.append(n)
				x.append(ass)
			if self.Algorithm=="lsmr()":
                		x = lsmr(a,b,atol=0,btol=0,conlim=0,show=True,x0=initial_guess)
		else:
			if self.Algorithm=="solve()":
                		x = solve(a,b)
			if self.Algorithm=="lstsq()":
                		x = lstsq(a,b,lapack_driver='gelsy')
			if self.Algorithm=="lsqr()":
                		x = lsqr(a,b,atol=0,btol=0,conlim=0,show=True)
			if self.Algorithm=="lsmr()":
                		#x = lsmr(a,b,atol=0.1,btol=0.1,maxiter=5,conlim=10,show=True)
                		x = lsmr(a,b,atol=0,btol=0,conlim=0,show=True,x0=initial_guess)
			if self.Algorithm=="spsolve()":
				x = dsolve.spsolve(csc_matrix(a),b)
			if self.Algorithm=="pinv2()":
				x=[]
				#pseudoinverse_a=pinv(a)
				pseudoinverse_a=pinv2(a,check_finite=False)
				x.append(matmul(pseudoinverse_a,b))
			if self.Algorithm=="lsq_linear()":
				x = lsq_linear(a,b,lsq_solver='exact')
			if self.Algorithm=="lapack()":
				try:
					#x = gesv(matrixa,matrixb)
					x = gels(matrixa,matrixb)
					#x = sysv(matrixa,matrixb)
					#x = getrs(matrixa,matrixb)
					x = [matrixb]
				except:
					print "Exception:",sys.exc_info()
					return None 
			if self.Algorithm=="l1regls()":
				l1x = l1regls(matrixa,matrixb)
				ass=[]
				x=[]
				for n in l1x:
					ass.append(n)
				x.append(ass)

		print "solve_SAT2(): ",self.Algorithm,": x:",x
		if x is None:
			return None
		cnt=0
		binary_parity=0
		real_parity=0.0
		if rounding_threshold == "Randomized":
			randomized_rounding_threshold=float(random.randint(1,100000))/100000.0
		else:
			min_assignment=min(x[0])
			max_assignment=max(x[0])
			randomized_rounding_threshold=(min_assignment + max_assignment)/2
		print "randomized_rounding_threshold = ", randomized_rounding_threshold
		print "approximate assignment :",x[0]
		for e in x[0]:
			if e > randomized_rounding_threshold:
				satass.append(1)
				binary_parity += 1
			else:
				satass.append(0)
				binary_parity += 0
			real_parity += e
			cnt+=1
		print "solve_SAT2(): real_parity = ",real_parity
		print "solve_SAT2(): binary_parity = ",binary_parity
		return (satass,real_parity,binary_parity,x[0])
    def solve_SAT2(self, cnf, number_of_variables, number_of_clauses):
        #Solves CNFSAT by a Polynomial Time Approximation scheme:
        #	- Encode each clause as a linear equation in n variables: missing variables and negated variables are 0, others are 1
        #	- Solve previous system of equations by least squares algorithm to fit a line
        #	- Variable value above 0.5 is set to 1 and less than 0.5 is set to 0
        #	- Rounded of assignment array satisfies the CNFSAT with high probability
        #Returns: a tuple with set of satisfying assignments
        satass = []
        x = []
        self.solve_SAT(cnf, number_of_variables, number_of_clauses)
        for clause in self.cnfparsed:
            equation = []
            for n in xrange(number_of_variables):
                equation.append(0)
            #print "clause:",clause
            for literal in clause:
                if literal[0] != "!":
                    equation[int(literal[1:]) - 1] = 1
                else:
                    equation[int(literal[2:]) - 1] = 0
            self.equationsA.append(equation)
        for n in xrange(number_of_clauses):
            self.equationsB.append(1)
        a = np.array(self.equationsA)
        b = np.array(self.equationsB)
        init_guess = []
        for n in xrange(number_of_variables):
            init_guess.append(0.00000000001)
        initial_guess = np.array(init_guess)
        self.A = a
        self.B = b
        #print "a:",a
        #print "b:",b
        #print "a.shape:",a.shape
        #print "b.shape:",b.shape
        matrixa = matrix(a, tc='d')
        matrixb = matrix(b, tc='d')

        x = None
        if number_of_variables == number_of_clauses:
            if self.Algorithm == "lsqr()":
                #x = np.dot(np.linalg.inv(a),b)
                #x = gmres(a,b)
                #x = lgmres(a,b)
                #x = minres(a,b)
                #x = bicg(a,b)
                #x = cg(a,b)
                #x = cgs(a,b)
                #x = bicgstab(a,b)
                x = lsqr(a, b, atol=0, btol=0, conlim=0, show=True)
            if self.Algorithm == "lapack()":
                try:
                    x = gesv(matrixa, matrixb)
                    #x = gels(matrixa,matrixb)
                    #x = sysv(matrixa,matrixb)
                    #x = getrs(matrixa,matrixb)
                    x = [matrixb]
                except:
                    print "Exception:", sys.exc_info()
                    return None
            if self.Algorithm == "l1regls()":
                l1x = l1regls(matrixa, matrixb)
                ass = []
                x = []
                for n in l1x:
                    ass.append(n)
                x.append(ass)
            if self.Algorithm == "lsmr()":
                x = lsmr(a,
                         b,
                         atol=0,
                         btol=0,
                         conlim=0,
                         show=True,
                         x0=initial_guess)
        else:
            if self.Algorithm == "solve()":
                x = solve(a, b)
            if self.Algorithm == "lstsq()":
                x = lstsq(a, b, lapack_driver='gelsy')
            if self.Algorithm == "lsqr()":
                x = lsqr(a, b, atol=0, btol=0, conlim=0, show=True)
            if self.Algorithm == "lsmr()":
                #x = lsmr(a,b,atol=0.1,btol=0.1,maxiter=5,conlim=10,show=True)
                x = lsmr(a,
                         b,
                         atol=0,
                         btol=0,
                         conlim=0,
                         show=True,
                         x0=initial_guess)
            if self.Algorithm == "spsolve()":
                x = dsolve.spsolve(csc_matrix(a), b)
            if self.Algorithm == "pinv2()":
                x = []
                #pseudoinverse_a=pinv(a)
                pseudoinverse_a = pinv2(a, check_finite=False)
                x.append(matmul(pseudoinverse_a, b))
            if self.Algorithm == "lsq_linear()":
                x = lsq_linear(a, b, lsq_solver='exact')
            if self.Algorithm == "lapack()":
                try:
                    #x = gesv(matrixa,matrixb)
                    x = gels(matrixa, matrixb)
                    #x = sysv(matrixa,matrixb)
                    #x = getrs(matrixa,matrixb)
                    x = [matrixb]
                except:
                    print "Exception:", sys.exc_info()
                    return None
            if self.Algorithm == "l1regls()":
                l1x = l1regls(matrixa, matrixb)
                ass = []
                x = []
                for n in l1x:
                    ass.append(n)
                x.append(ass)

        print "solve_SAT2(): ", self.Algorithm, ": x:", x
        if x is None:
            return None
        cnt = 0
        binary_parity = 0
        real_parity = 0.0
        if rounding_threshold == "Randomized":
            randomized_rounding_threshold = float(random.randint(
                1, 100000)) / 100000.0
        else:
            min_assignment = min(x[0])
            max_assignment = max(x[0])
            randomized_rounding_threshold = (min_assignment +
                                             max_assignment) / 2
        print "randomized_rounding_threshold = ", randomized_rounding_threshold
        print "approximate assignment :", x[0]
        for e in x[0]:
            if e > randomized_rounding_threshold:
                satass.append(1)
                binary_parity += 1
            else:
                satass.append(0)
                binary_parity += 0
            real_parity += e
            cnt += 1
        print "solve_SAT2(): real_parity = ", real_parity
        print "solve_SAT2(): binary_parity = ", binary_parity
        return (satass, real_parity, binary_parity, x[0])