Beispiel #1
0
def test_covtransform1():

    logger.info('Descartes to polar tranformation')

    ## Descartes coordinates
    X = 1, 4

    ##  their covarinac ematrix
    C = Ostap.SymMatrix(2)()
    C[0, 0] = 0.20
    C[0, 1] = 0.05
    C[1, 1] = 0.30

    R = C.correlations()

    ##  Polar coordinated
    r = lambda x, y: math.sqrt(x * x + y * y)
    phi = lambda x, y: math.atan2(y, x)

    C_polar = transform(C, X, r, phi)

    R_polar = C_polar.correlations()

    logger.info('Descartes Covariance  :\n%s' % C)
    logger.info('Descartes Correlation :\n%s' % R)
    logger.info('Polar     Covariance  :\n%s' % C_polar)
    logger.info('Polar     Correlation :\n%s' % R_polar)
Beispiel #2
0
def transform(C, X, *Y):
    """ Transform the covariance nmatrix C at point X to the variables Y(X)
    >>> X = 1 , 2
    >>> C = Ostap.SymMatrix(2)()
    >>> C [ 0 , 0 ] = 0.20
    >>> C [ 0 , 1 ] = 0.05
    >>> C [ 1 , 1 ] = 0.30
    >>> r   = lambda x , y : (x*x+y*y)**2
    >>> phi = lambda x , y : math.atan2 ( y , x )
    >>> C_polar = transform ( C , X , r , phi ) 
    """

    ny = len(Y)
    assert 1 <= ny, 'Invalid size of Y!'

    nx = len(X)
    if C is None and 1 <= nx:
        C = Ostap.SymMatrix(nx)()
        for i, x in enumerate(X):
            xx = VE(x)
            C[i, i] = xx.cov2()

    shape = C.shape
    assert shape[0] == shape[1] and shape[0] == nx, 'Invalid shape of matrix C!'

    CC = Ostap.SymMatrix(nx)()
    for i in range(CC.kRows):
        CC[i, i] = C(i, i)
        for j in range(i + 1, CC.kCols):
            v = 0.5 * (C(i, j) + C(j, i))
            CC[i, j] = v

    XX = Ostap.Vector(nx)()
    for i, x in enumerate(X):
        XX[i] = float(x)

    ## get vector-with-errors
    XX = Ostap.VectorE(nx)(XX, CC)

    R = XX.transform(*Y)

    return R.cov2()