Exemplo n.º 1
0
 def test_n_samples(self):
     # 
     # 1D
     #
     
     # Define mesh
     mesh = Mesh1D(resolution=(10,))
     
     # Define function  
     f = Explicit([lambda x: x, lambda x: -2+2*x**2], dim=1)
     n_samples = f.n_samples()
     
     k = Kernel(f)
     
     n_points = 101
     x0, x1 = mesh.bounding_box()
     x = np.linspace(x0,x1,n_points)
     
     self.assertEqual(k.eval(x).shape, (n_points, n_samples))
     self.assertTrue(np.allclose(k.eval(x)[:,0],x))
     self.assertTrue(np.allclose(k.eval(x)[:,1], -2+2*x**2))
Exemplo n.º 2
0
    def test_eval(self):

        #
        # Evaluate single univariate/bivariate functions in 1 or 2 dimensions
        #
        fns = {
            1: {
                1: lambda x: x**2,
                2: lambda x, y: x + y
            },
            2: {
                1: lambda x: x[:, 0]**2 + x[:, 1]**2,
                2: lambda x, y: x[:, 0] * y[:, 0] + x[:, 1] * y[:, 1]
            }
        }

        # Singletons
        x = {1: {1: 2, 2: (3, 4)}, 2: {1: (1, 2), 2: ((1, 2), (3, 4))}}

        vals = {1: {1: 4, 2: 7}, 2: {1: 5, 2: 11}}

        for dim in [1, 2]:
            #
            # Iterate over dimension
            #
            for n_variables in [1, 2]:
                #
                # Iterate over number of variables
                #
                fn = fns[dim][n_variables]
                f = Explicit(fn, n_variables=n_variables, dim=dim)

                xn = x[dim][n_variables]
                self.assertEqual(f.eval(xn), vals[dim][n_variables])

        #
        # Evaluate sampled functions
        #
        fns = {
            1: {
                1: lambda x, a: a * x**2,
                2: lambda x, y, a: a * (x + y)
            },
            2: {
                1: lambda x, a: a * (x[:, 0]**2 + x[:, 1]**2),
                2: lambda x, y, a: a * (x[:, 0] * y[:, 0] + x[:, 1] * y[:, 1])
            }
        }

        pars = [{'a': 1}, {'a': 2}]

        #
        # Singletons
        #
        x = {1: {1: 2, 2: (3, 4)}, 2: {1: (1, 2), 2: ((1, 2), (3, 4))}}

        vals = {1: {1: 4, 2: 7}, 2: {1: 5, 2: 11}}

        for dim in [1, 2]:
            #
            # Iterate over dimension
            #
            for n_variables in [1, 2]:
                #
                # Iterate over number of variables
                #
                fn = fns[dim][n_variables]
                f = Explicit(fn,
                             parameters=pars,
                             n_variables=n_variables,
                             dim=dim)

                xn = x[dim][n_variables]
                self.assertEqual(f.eval(xn)[0][0], vals[dim][n_variables])
                self.assertEqual(f.eval(xn)[0][1], 2 * vals[dim][n_variables])

        #
        # 2 points
        #
        n_points = 2
        x = {
            1: {
                1: [(2, ), (2, )],
                2: ([(3, ), (3, )], [(4, ), (4, )])
            },
            2: {
                1: [(1, 2), (1, 2)],
                2: ([(1, 2), (1, 2)], [(3, 4), (3, 4)])
            }
        }
        for dim in [1, 2]:
            #
            # Iterate over dimension
            #
            for n_variables in [1, 2]:
                #
                # Iterate over number of variables
                #
                fn = fns[dim][n_variables]
                f = Explicit(fn,
                             parameters=pars,
                             n_variables=n_variables,
                             dim=dim)

                xn = x[dim][n_variables]
                self.assertEqual(f.eval(xn).shape[0], n_points)
                self.assertEqual(f.eval(xn).shape[1], f.n_samples())

                for i in range(f.n_samples()):
                    for j in range(2):
                        val = pars[i]['a'] * vals[dim][n_variables]
                        self.assertEqual(f.eval(xn)[j, i], val)