예제 #1
0
파일: square.py 프로젝트: 9578577/mvn
def _subSquare(vectors, var, full=False):
    """
    given a series of vectors, this function calculates:
        (variances,vectors)=numpy.linalg.eigh(vectors.H*vectors)
    it's a seperate function because if there are less vectors 
    than dimensions the process can be accelerated, it just takes some dancing

    it is based on this:

    >>> vectors=Matrix(helpers.ascomplex(numpy.random.randn(
    ...     numpy.random.randint(1,10),numpy.random.randint(1,10),2
    ... )))
    >>> cov = vectors.H*vectors
    >>> Xcov = vectors*vectors.H 
    >>> (Xval,Xvec) = numpy.linalg.eigh(Xcov)
    >>> vec = Xvec.H*vectors
    >>> assert vec.H*vec == cov
    """
    vectors = Matrix(vectors)
    shape = vectors.shape

    if not all(shape):
        val = numpy.zeros([0])
        vec = numpy.zeros([0, shape[1]])
        return (val, vec)
    
    eig = numpy.linalg.eigh

    if shape[0] >= shape[1] or full or not vectors.any() or (var < 0).any():
        scaled = Matrix(var[:, None]*numpy.array(vectors))
        
        cov = vectors.H*scaled
        
        (val, vec) = eig(cov)
        vec = vec.H

    elif not var.any():
        cov = vectors.H*vectors
        (_,vec) = eig(cov)
        vec = vec.H
        val = numpy.zeros(vec.shape[0])

    else:
        scaled = Matrix(scipy.sqrt(var)[:, None]*numpy.array(vectors))
        Xcov = scaled*scaled.H        
        #Xcov = var[:,None]*numpy.array(vectors)*vectors.H
        
        (_, Xvec) = eig(Xcov)
        
        Xscaled = (Xvec.H*scaled)
        val = helpers.mag2(Xscaled)

        vec = numpy.array(Xscaled)/scipy.sqrt(val[:, numpy.newaxis])

    
    return (val, vec)
예제 #2
0
파일: square.py 프로젝트: riviera2015/mvn
def _subSquare(vectors, var, full=False):
    """
    given a series of vectors, this function calculates:
        (variances,vectors)=numpy.linalg.eigh(vectors.H*vectors)
    it's a seperate function because if there are less vectors 
    than dimensions the process can be accelerated, it just takes some dancing

    it is based on this:

    >>> vectors=Matrix(helpers.ascomplex(numpy.random.randn(
    ...     numpy.random.randint(1,10),numpy.random.randint(1,10),2
    ... )))
    >>> cov = vectors.H*vectors
    >>> Xcov = vectors*vectors.H 
    >>> (Xval,Xvec) = numpy.linalg.eigh(Xcov)
    >>> vec = Xvec.H*vectors
    >>> assert vec.H*vec == cov
    """
    vectors = Matrix(vectors)
    shape = vectors.shape

    if not all(shape):
        val = numpy.zeros([0])
        vec = numpy.zeros([0, shape[1]])
        return (val, vec)

    eig = numpy.linalg.eigh

    if shape[0] >= shape[1] or full or not vectors.any() or (var < 0).any():
        scaled = Matrix(var[:, None] * numpy.array(vectors))

        cov = vectors.H * scaled

        (val, vec) = eig(cov)
        vec = vec.H

    elif not var.any():
        cov = vectors.H * vectors
        (_, vec) = eig(cov)
        vec = vec.H
        val = numpy.zeros(vec.shape[0])

    else:
        scaled = Matrix(scipy.sqrt(var)[:, None] * numpy.array(vectors))
        Xcov = scaled * scaled.H
        #Xcov = var[:,None]*numpy.array(vectors)*vectors.H

        (_, Xvec) = eig(Xcov)

        Xscaled = (Xvec.H * scaled)
        val = helpers.mag2(Xscaled)

        vec = numpy.array(Xscaled) / scipy.sqrt(val[:, numpy.newaxis])

    return (val, vec)
예제 #3
0
 def testDist2(self):
     if not fix.A.flat:
         self.assertTrue( 
             Matrix((fix.A**0).dist2(numpy.zeros((1,fix.ndim)))) == 
             helpers.mag2((fix.A**0).mean) 
         )
예제 #4
0
 def testScaled(self):
     self.assertTrue( fix.A.scaled.H*fix.A.scaled == abs(fix.A).cov )
     self.assertTrue( Matrix(helpers.mag2(fix.A.scaled)) == fix.A.var )
     self.assertTrue( fix.A.vectors.H*fix.A.scaled == fix.A.transform() )