Exemplo n.º 1
0
 def best_hermite(self):
     d = 2
     def function(X):
         return np.ones(shape=(X.shape[0], 1))  # np.prod(np.sin(3 * X), axis=1)
     ups = UnivariatePolynomialSpace(measure='h')
     ps = TensorPolynomialSpace(ups=ups, c_var=2, sampler='optimal')
     P = WeightedPolynomialApproximator(function=function, ps=ps)
     sparseindices = cartesian_product([range(2)] * d)
     P.expand(sparseindices)
     # P.plot_sampling_measure(N=200,L=10)
     P.plot_xy()
     P.plot()
     M = 100
     sparseindices = cartesian_product([range(3)] * d)
     P.expand(sparseindices)
     # P.plot_sampling_measure(N=200,L=10)
     P.plot_xy()
     P.plot(L=5)
     sparseindices = indices.simplex(6, n=d)
     P.expand(sparseindices)
     # P.plot_sampling_measure(N=200,L=20)
     P.plot_xy()
     P.plot(L=5)
     X = np.random.rand(M, d)
     Y = function(X.reshape((X.shape[0], d))).reshape((X.shape[0], 1))
     self.assertLess(np.linalg.norm(P(X) - Y) / np.sqrt(M), 0.1)
Exemplo n.º 2
0
 def test_cartesian_product(self):
     T = [[1, 2], [3, 4], [2]]
     TT = cartesian_product(T)
     self.assertEqual([mi.full_tuple() for mi in TT], [(1, 3, 2), (1, 4, 2),
                                                       (2, 3, 2),
                                                       (2, 4, 2)])
     TT = cartesian_product(T, (1, 3, 4))
     self.assertEqual([mi.full_tuple() for mi in TT], [(0, 1, 0, 3, 2),
                                                       (0, 1, 0, 4, 2),
                                                       (0, 2, 0, 3, 2),
                                                       (0, 2, 0, 4, 2)])
Exemplo n.º 3
0
 def adaptive(self, measure):
     def function(X):
         return np.prod(np.sin(3 * X), axis=1)
     ups = UnivariatePolynomialSpace(measure=measure)
     P = WeightedPolynomialApproximator(function, ps=ups)
     sparseindices = cartesian_product([range(4)] * 1)
     P.expand(sparseindices)
     ps = TensorPolynomialSpace(ups=ups,c_var=1)
     self.assertAlmostEqual(ps.plot_optimal_distribution(N=200), 1, delta=0.2)
     P.plot_xy()
     M = 100
     sparseindices = cartesian_product([range(4)] * 2)
     P.expand(sparseindices)
     self.assertAlmostEqual(P.plot_sampling_measure(N=200), 1, delta=0.2)
     P.plot_xy()
     X = np.random.rand(M, 2)
     Y = function(X.reshape((X.shape[0], 2))).reshape((X.shape[0], 1))
     self.assertLess(np.linalg.norm(P(X) - Y) / np.sqrt(M), 0.01)
Exemplo n.º 4
0
 def test_rectangle(self):
     self.assertCountEqual(indices.rectangle(L=[4, 4], n=2),
                           [mi for mi in cartesian_product([range(4)] * 2)])
     self.assertCountEqual(indices.rectangle(L=2, n=2), [
         MultiIndex(),
         MultiIndex((1, 0)),
         MultiIndex((0, 1)),
         MultiIndex((1, 1))
     ])
Exemplo n.º 5
0
 def test_returnshapes(self):
     cRandomTests = 100
     for __ in range(cRandomTests):
         A = []
         cSets = randint(0, 5)
         cElements = []
         for j in range(cSets):
             cElements.append(randint(0, 5))
             A.append(rand(cElements[j]))
         C = cartesian_product(A)
         self.assertEqual(len(C), np.prod(cElements))
Exemplo n.º 6
0
 def _pols_from_mi(self, mi):
     '''
     Convert multi-index to corresponding polynomials
     
     :param mi: Multi-index
     :return: List of polynomials corresponding to mi
     '''
     if self.reparametrization is True:
         if mi == MultiIndex():
                 return [mi]
         else:
             univariate_entries = []
             for dimension in mi.active_dims():
                 init_range = 2 ** (mi[dimension])-1
                 end_range = 2 ** (mi[dimension]+1)-1
                 univariate_entries.append(range(init_range, end_range))
             return cartesian_product(univariate_entries, mi.active_dims())
     elif self.reparametrization is False:
         return [mi]
     else:
         return self.reparametrization(mi)
Exemplo n.º 7
0
 def test_rectangles(self):
     d = 5
     L = 5
     sparseindices = cartesian_product([range(L)] * d)
     CR = combination_rule(sparseindices)
     self.assertEqual([mi for mi in CR], [MultiIndex((L - 1, ) * d)])