예제 #1
0
def SolveWeighted(A, B, C, D, lambd):
    BScale = El.SparseMatrix()
    El.Copy(B, BScale)
    El.Scale(lambd, BScale)

    DScale = El.Matrix()
    El.Copy(D, DScale)
    El.Scale(lambd, DScale)

    AEmb = El.VCat(A, BScale)
    CEmb = El.VCat(C, DScale)

    X = El.LeastSquares(AEmb, CEmb, ctrl)

    El.Copy(C, E)
    El.Multiply(El.NORMAL, -1., A, X, 1., E)
    residNorm = El.FrobeniusNorm(E)
    if display:
        El.Display(E, "C - A X")
    print "lambda=", lambd, ": || C - A X ||_F / || C ||_F =", residNorm / CNorm

    El.Copy(D, E)
    El.Multiply(El.NORMAL, -1., B, X, 1., E)
    equalNorm = El.FrobeniusNorm(E)
    if display:
        El.Display(E, "D - B X")
    print "lambda=", lambd, ": || D - B X ||_F / || D ||_F =", equalNorm / DNorm
예제 #2
0
파일: GLM.py 프로젝트: xuanhan863/Elemental
def SolveWeighted(A, B, D, lambd):
    AScale = El.DistSparseMatrix()
    El.Copy(A, AScale)
    El.Scale(lambd, AScale)
    AEmb = El.HCat(AScale, B)
    if display:
        El.Display(AEmb, "[lambda*A, B]")
    if output:
        El.Print(AEmb, "[lambda*A, B]")

    ctrl.alpha = baseAlpha
    if worldRank == 0:
        print "lambda=", lambd, ": ctrl.alpha=", ctrl.alpha
    XEmb = El.LeastSquares(AEmb, D, ctrl)

    X = XEmb[0:n0 * n1, 0:numRHS]
    Y = XEmb[n0 * n1:n0 * n1 + numColsB, 0:numRHS]
    El.Scale(lambd, X)

    YNorm = El.FrobeniusNorm(Y)
    if worldRank == 0:
        print "lambda=", lambd, ": || Y ||_F =", YNorm

    El.Copy(D, E)
    El.Multiply(El.NORMAL, -1., A, X, 1., E)
    El.Multiply(El.NORMAL, -1., B, Y, 1., E)
    residNorm = El.FrobeniusNorm(E)
    if worldRank == 0:
        print "lambda=", lambd, ": || D - A X - B Y ||_F / || D ||_F =", residNorm / DNorm
예제 #3
0
def SolveWeighted(A,B,C,D,lambd):
  BScale = El.DistSparseMatrix()
  El.Copy( B, BScale )
  El.Scale( lambd, BScale )

  DScale = El.DistMultiVec()
  El.Copy( D, DScale ) 
  El.Scale( lambd, DScale )

  AEmb = El.VCat(A,BScale)
  CEmb = El.VCat(C,DScale)
  if output:
    El.Print( AEmb, "AEmb" )

  ctrl.alpha = baseAlpha
  if worldRank == 0:
    print('lambda={}, ctrl.alpha={}'.format(lambd,ctrl.alpha))
  X=El.LeastSquares(AEmb,CEmb,ctrl)

  El.Copy( C, E )
  El.Multiply( El.NORMAL, -1., A, X, 1., E )
  residNorm = El.FrobeniusNorm( E )
  if display:
    El.Display( E, "C - A X" )
  if output:
    El.Print( E, "C - A X" )
  if worldRank == 0:
    print('lambda={}: || C - A X ||_F / || C ||_F = {}'.format(lambd, \
      residNorm/CNorm))

  El.Copy( D, E )
  El.Multiply( El.NORMAL, -1., B, X, 1., E )
  equalNorm = El.FrobeniusNorm( E )
  if display:
    El.Display( E, "D - B X" )
  if output:
    El.Print( E, "D - B X" )
  if worldRank == 0:
    print('lambda={}: || D - B X ||_F / || D ||_F = {}'.format(lambd, \
      equalNorm/DNorm))
예제 #4
0
def CreateFactor(height,width):
  F = El.DistSparseMatrix()
  El.Zeros( F, height, width )
  localHeight = F.LocalHeight()
  F.Reserve(localHeight*width)
  for iLoc in xrange(localHeight):
    i = F.GlobalRow(iLoc)
    for j in xrange(width):
      F.QueueLocalUpdate( iLoc, j, math.log(i+j+1.) )

  F.ProcessQueues()
  # NOTE: Without this rescaling, the problem is much harder, as the variables
  #       s becomes quite large 
  #       (|| F^T x ||_2 <= u, u^2 <= t, would imply u and t are large).
  FFrob = El.FrobeniusNorm( F )
  El.Scale( 1./FFrob, F )
  return F
예제 #5
0
파일: GLM.py 프로젝트: xuanhan863/Elemental
A = FD2D(n0, n1)
B = Constraints(numColsB, n0, n1)
if display:
    El.Display(A, "A")
    El.Display(B, "B")
if output:
    El.Print(A, "A")
    El.Print(B, "B")

D = El.DistMultiVec()
El.Uniform(D, A.Height(), numRHS)
if display:
    El.Display(D, "D")
if output:
    El.Print(D, "D")
DNorm = El.FrobeniusNorm(D)

baseAlpha = 1e-4
ctrl = El.LeastSquaresCtrl_d()
ctrl.alpha = baseAlpha
ctrl.progress = True
ctrl.equilibrate = True
ctrl.solveCtrl.relTol = 1e-10
ctrl.solveCtrl.relTolRefine = 1e-12
ctrl.solveCtrl.progress = True
startGLM = El.mpi.Time()
X, Y = El.GLM(A, B, D, ctrl)
endGLM = El.mpi.Time()
if worldRank == 0:
    print "GLM time:", endGLM - startGLM, "seconds"
if display:
예제 #6
0

A = FD2D(n0, n1)
B = Constraints(numRowsB, n0, n1)
if display:
    El.Display(A, "A")
    El.Display(B, "B")

C = El.Matrix()
D = El.Matrix()
El.Uniform(C, A.Height(), numRHS)
El.Uniform(D, B.Height(), numRHS)
if display:
    El.Display(C, "C")
    El.Display(D, "D")
CNorm = El.FrobeniusNorm(C)
DNorm = El.FrobeniusNorm(D)

ctrl = El.LeastSquaresCtrl_d()
ctrl.progress = True
ctrl.solveCtrl.relTol = 1e-10
ctrl.solveCtrl.relTolRefine = 1e-12
ctrl.solveCtrl.progress = True

startLSE = El.mpi.Time()
X = El.LSE(A, B, C, D, ctrl)
endLSE = El.mpi.Time()
print "LSE time:", endLSE - startLSE, "seconds"
if display:
    El.Display(X, "X")
예제 #7
0
            if x1 > 0:
                A.QueueUpdate(sLoc, sRel - N0, -3)
            if x1 + 1 < N1:
                A.QueueUpdate(sLoc, sRel + N0, 3)

        # The dense last column
        A.QueueUpdate(sLoc, width - 1, -10 / height)

    A.ProcessQueues()
    return A


# Define a random (affine) hyperplane
wGen = El.DistMultiVec()
El.Gaussian(wGen, n0 * n1, 1)
wGenNorm = El.FrobeniusNorm(wGen)
El.Scale(1. / wGenNorm, wGen)
# TODO: Add support for mpi::Broadcast and randomly generate this
offset = 0.3147

A = StackedFD2D(n0, n1)

# Label the points based upon their location relative to the hyperplane
d = El.DistMultiVec()
El.Ones(d, 2 * n0 * n1, 1)
El.Multiply(El.NORMAL, 1., A, wGen, -offset, d)
El.EntrywiseMap(d, lambda alpha: 1. if alpha > 0 else -1.)

if display:
    El.Display(wGen, "wGen")
    if worldRank == 0:
예제 #8
0
파일: LLL.py 프로젝트: xzflin/Elemental
            startTime = El.mpi.Time()
            mode = El.LLL_FULL
            U, R, info = El.LLL(B, mode, ctrl)
            runTime = El.mpi.Time() - startTime
            print "  runtime: %f seconds" % runTime
            print "  delta=", info.delta
            print "  eta  =", info.eta
            print "  nullity: ", info.nullity
            print "  num swaps: ", info.numSwaps
            if output:
                El.Print(U, "U")
                El.Print(B, "BNew")
                El.Print(R, "R")

            # Test how small the first column is compared to the others
            b1Norm = El.FrobeniusNorm(B[:, 0])
            print "  || b_1 ||_2 =", b1Norm
            numLower = 0
            minInd = 0
            minNorm = b1Norm
            for j in xrange(1, n):
                bjNorm = El.FrobeniusNorm(B[:, j])
                if bjNorm < b1Norm:
                    numLower = numLower + 1
                if bjNorm < minNorm:
                    minNorm = bjNorm
                    minInd = j
            if numLower == 0:
                print "  b1 was the smallest column"
            else:
                print "  %d columns were smaller than b1, with || b_%d ||_2 = %f the smallest" % (
예제 #9
0
  A.ProcessQueues()
  return A

A = StackedFD2D(n0,n1)
if display:
  El.Display( A, "A" )
if output:
  El.Print( A, "A" )

y = El.DistMultiVec()
El.Uniform( y, 2*n0*n1, 1 )
if display:
  El.Display( y, "y" )
if output:
  El.Print( y, "y" )

AAdj = El.DistSparseMatrix()
El.Adjoint( A, AAdj )
z = El.DistMultiVec()
El.Uniform( z, A.Width(), 1 )
El.Multiply( El.ADJOINT, 1., A, y, 0., z )
zNrm2 = El.FrobeniusNorm( z )
El.Multiply( El.NORMAL, -1., AAdj, y, 1., z )
eNrm2 = El.FrobeniusNorm( z )
if worldRank == 0:
  print('|| A^H y ||_2 = {}'.format(zNrm2))
  print('|| error ||_2 = {}'.format(eNrm2))

El.Finalize()