Esempio n. 1
0
    def __init__(self, num_dims, ranges=None, indices=None):

        self.num_dims = num_dims
        if ranges is None:
            self.ranges = get_symmetric_hypercube(0, 1, num_dims)
            self.ranges = self.ranges.reshape((num_dims, 2))
        else:
            if indices is None:
                if len(ranges) == 1:
                    self.ranges = numpy.empty((num_dims, 2), numpy.double)
                    self.ranges[:, 0] = ranges[0][0]
                    self.ranges[:, 1] = ranges[0][1]
                else:
                    msg = "ranges are inconsistent with num_dims. "
                    msg += "Specify indices or provide a range for each "
                    msg += "dimension"
                    assert len(ranges) == num_dims, msg
                    self.ranges = numpy.array(ranges, numpy.double)
            else:
                msg = "indices and ranges are inconsistent"
                assert len(indices) == len(ranges), msg

                self.ranges = numpy.empty((num_dims, 2), numpy.double)
                dims = set()  # used for error checking
                for i in xrange(len(indices)):
                    for j in xrange(len(indices[i])):
                        msg = "the range of the " + str(indices[i][j])
                        msg += "dimension is not in ascending order"
                        assert ranges[i][0] < ranges[i][1], msg
                        self.ranges[indices[i][j], 0] = ranges[i][0]
                        self.ranges[indices[i][j], 1] = ranges[i][1]
                        dims.add(indices[i][j])

                msg = "ranges and indices are inconsistent with num_dims"
                assert len(dims) == self.num_dims, msg
Esempio n. 2
0
if '__main__' in __name__:
    import polynomial_chaos.polynomial_chaos_expansion as PCE
    from interpolation.lagrange_interpolation import multivariate_lagrange_interpolation
    import test_functions
    import utilities.visualisation as plt
    import numpy
    import scipy
    import pylab
    from utilities.math_utils import get_symmetric_hypercube, hypercube_map

    num_dims = 2
    max_order = 30
    domain = get_symmetric_hypercube( -3., 3., num_dims )
    pce = PCE.PolynomialChaosExpansion( num_dims, order = max_order, 
                                        basis = 'legendre', 
                                        func_domain = domain )

    x = numpy.linspace( -3., 3., 50 )
    [X,Y] = numpy.meshgrid( x, x )
    samples = numpy.vstack( ( X.reshape( ( 1, X.shape[0]*X.shape[1] ) ), 
                              Y.reshape( ( 1, Y.shape[0]*Y.shape[1]) ) ) )

    #num_samples=int(numpy.round(scipy.misc.comb(max_order+num_dims,num_dims)))
    #num_samples = 2500
    #samples = numpy.random.uniform( -3., 3., size = num_samples * num_dims )
    #samples = numpy.resize( samples, ( num_dims, num_samples ) )

    sample_vals = test_functions.matlab_peaks( samples )
    #sample_vals = numpy.sum( samples**2, axis = 0 )
    print sample_vals.shape
    def test_tensor_product_basis( self ):

        # test orthogonal basis functions
        num_dims = 2
        #domain = TensorProductDomain( num_dims, ranges = [[-1.,1.]] )
        domain = get_symmetric_hypercube( -1., 1., num_dims )
        poly_1d = OrthogPolyVector();
        poly_1d.resize( 1 )
        poly_1d[0] = JacobiPolynomial1D( 0.5, 0.5 )
        #basis = TensorProductBasis( num_dims, poly_1d )
        basis = TensorProductBasis()
        basis.set_domain( domain )
        basis.set_bases( poly_1d, [numpy.arange( num_dims,dtype=numpy.int32 )], 
                         num_dims )
        #assert basis.num_dims == num_dims
        x = numpy.random.uniform( -1., 1., ( num_dims, 20 ) )
        index = PolynomialIndex( numpy.array( [[1, 1]], numpy.int32 ) )
        true_val = poly_1d[0].value_set( x[0,:], index.level(0), -1., 1. ) * \
            poly_1d[0].value_set( x[1,:], index.level(1), -1., 1. )
        assert numpy.allclose( basis.value_set( x, index), true_val )
        #true_grad = poly_1d[0].gradient_set( x[0,:], index.level( 0 ), -1., 1. ) * \
        #    poly_1d[0].value_set( x[1,:], index.level(1), -1., 1. )
        #assert numpy.allclose( basis.gradient( x, index, 0), 
        #                       true_grad )
        #true_grad = poly_1d[0].value_set( x[0,:], index.level(0), -1., 1. ) * \
        #    poly_1d[0].gradient_set( x[1,:], index.level(1), -1., 1. )
        #assert numpy.allclose( basis.gradient( x, index, 1), 
        #                       true_grad )

        num_dims = 2
        poly_1d = OrthogPolyVector();
        poly_1d.resize( 2 )
        poly_1d[0] = JacobiPolynomial1D( 0.5, 0.5 )
        poly_1d[1] = JacobiPolynomial1D( 0.5, 1.5 )
        #basis = TensorProductBasis( num_dims, poly_1d )
        basis = TensorProductBasis()
        basis.set_domain( domain )
        basis.set_bases( poly_1d, [numpy.array([0],numpy.int32),
                                   numpy.array([1],numpy.int32)], 
                         num_dims )
        x_1 = numpy.random.uniform( -1., 1., ( 1, 20 ) )
        x_2 = numpy.random.normal( 0., 1., ( 1, 20 ) )
        x = numpy.vstack( ( x_1, x_2 ) )
        index = PolynomialIndex( numpy.array( [[1,1]], numpy.int32 ) )
        true_val = poly_1d[0].value_set( x[0,:], index.level(0), -1., 1. ) * \
            poly_1d[1].value_set( x[1,:], index.level(1), -1., 1. )
        assert numpy.allclose( basis.value_set( x, index ), true_val )
        #true_grad = poly_1d[0].gradient_set( x[0,:], index.level(0), -1., 1. ) * \
        #    poly_1d[1].value_set( x[1,:], index.level(1), -1., 1. )
        #assert numpy.allclose( basis.gradient( x, index, 0), 
        #                       true_grad )
        #true_grad = poly_1d[0].value_set( x[0,:], index.level(0), -1., 1. ) * \
        #    poly_1d[1].gradient_set( x[1,:], index.level(1), -1., 1. )
        #assert numpy.allclose( basis.gradient( x, index, 1), 
        #                       true_grad )
        num_dims = 3
        domain = get_symmetric_hypercube( -1., 1., num_dims )
        basis = TensorProductBasis()
        basis.set_domain( domain )
        basis.set_bases( poly_1d, [numpy.array([0,2],numpy.int32),
                                   numpy.array([1],numpy.int32)], 
                         num_dims )
        indices = PolyIndexVector()
        indices.resize( 3 )
        indices[0] = PolynomialIndex( numpy.array( [[1,1]], numpy.int32 ) )
        indices[1] = PolynomialIndex( numpy.array( [[2,1]], numpy.int32 ) )
        indices[2] = PolynomialIndex( numpy.array( [[0,2],[2,3]], numpy.int32 ) )
        indices[2] = PolynomialIndex( numpy.array( [[0,3],[1,1],[2,2]], 
                                                   numpy.int32 ) )
        x_1 = numpy.random.uniform( -1., 1., ( 1, 20 ) )
        x_2 = numpy.random.normal( 0., 1., ( 1, 20 ) )
        x_3 = numpy.random.normal( 0., 1., ( 1, 20 ) )
        x = numpy.vstack( ( x_1, x_2, x_3 ) )
        vals_m = basis.value_set_multiple( x, indices )
        for i, index in enumerate( indices ):
            true_vals = poly_1d[0].value_set( x[0,:], index.level(0), 
                      domain[0], domain[1] ) * poly_1d[1].value_set( x[1,:], index.level(1), domain[2], domain[3] ) *  poly_1d[0].value_set( x[2,:], index.level(2), 
                      domain[4], domain[5] )
            vals = basis.value_set( x, index )
            assert numpy.allclose( true_vals, vals )
            assert numpy.allclose( true_vals, vals_m[:,i] )
    def test_polynomial_chaos_expansion( self ):

        # test 1D bounded domain
        num_dims = 1
        domain = get_symmetric_hypercube( 0., 1., num_dims )

        poly_1d = OrthogPolyVector();
        poly_1d.resize( 1 )
        poly_1d[0] = LegendrePolynomial1D()

        basis = TensorProductBasis()
        basis.set_domain( domain )
        basis.set_bases( poly_1d, [numpy.array([0],numpy.int32)], 
                         num_dims )

        pce = PolynomialChaosExpansion()
        pce.domain( domain )
        pce.basis( basis )
        pce.define_isotropic_expansion( 2, 1. );
        pce.set_coefficients( numpy.ones( ( pce.num_terms(), 1 ) ) )

        num_test_points = 20
        test_points = \
            numpy.linspace( 0., 1., num_test_points ).reshape(1,num_test_points)
        pred_vals = pce.evaluate_set( test_points )

        x = 2. * test_points - 1.
        test_vals = numpy.ones( num_test_points ) + x[0,:] + \
            0.5 * ( 3.*x[0,:]**2 - 1. )
        assert numpy.allclose( test_vals, pred_vals )
        test_mean = 1.
        test_variance = 1./3. + 1./5.
        assert numpy.allclose( pce.mean(), test_mean )
        assert numpy.allclose( pce.variance(), test_variance )
        
        # test 2D bounded domain
        num_dims = 2
        domain = get_symmetric_hypercube( 0., 1., num_dims )
        poly_1d = OrthogPolyVector();
        poly_1d.resize( 1 )
        poly_1d[0] = LegendrePolynomial1D()
        basis = TensorProductBasis()
        basis.set_domain( domain )
        basis.set_bases( poly_1d, [numpy.array([0,1],numpy.int32)], 
                         num_dims )
        pce = PolynomialChaosExpansion()
        pce.domain( domain )
        pce.basis( basis )
        order = 2
        pce.define_isotropic_expansion( order, 1. );
        pce.set_coefficients( numpy.ones( ( pce.num_terms(), 1 ) ) )
        num_test_points = 20
        test_points = numpy.random.uniform( 0., 1., ( num_dims, num_test_points))
        pred_vals = pce.evaluate_set( test_points )
        x = 2. * test_points - 1.
        test_vals = numpy.ones( num_test_points ) + x[0,:] + x[1,:] + \
            0.5 * ( 3.*x[0,:]**2 - 1. ) + 0.5 * ( 3.*x[1,:]**2 - 1. ) + \
            x[0,:] * x[1,:]
        assert numpy.allclose( test_vals, pred_vals )
        test_mean = 1.
        test_variance = 2. * 1./3. + 1./9. + 2. * 1./5.
        assert numpy.allclose( pce.mean(), test_mean )
        assert numpy.allclose( pce.variance(), test_variance )