123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- import unittest
- import numpy as np
- from swiftt.interval import Interval
- from swiftt.taylor import taylor_map, factory_taylor
- from swiftt.math_algebra import cos, sin, exp, sqrt
- tol_coeff = 1.e-12
- null_expansion_2var_order2 = factory_taylor.zero_expansion(2, 2)
- null_expansion_2var_order3 = factory_taylor.zero_expansion(2, 3)
- null_expansion_3var_order2 = factory_taylor.zero_expansion(3, 2)
- null_expansion_4var_order5 = factory_taylor.zero_expansion(4, 5)
- class TestMiscellaneous(unittest.TestCase):
- def test_depth_univariate(self):
- expansion1 = factory_taylor.zero_expansion(1, 6)
- coeff1 = [0., 0., 0., 4, 0., 5., 8.]
- expansion1.coeff = coeff1
- if expansion1.create_null_expansion().total_depth != 7:
- self.fail()
- if expansion1.total_depth != 3:
- self.fail()
- def test_depth_bivariate(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [0., 0., 0., 4, 0., 5.]
- expansion1.coeff = coeff1
- if expansion1.create_null_expansion().total_depth != expansion1.order + 1:
- self.fail()
- if expansion1.total_depth != 2:
- self.fail()
- def test_effective_order_bivariate(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [2., 1., 0., 0, 0., 0.]
- expansion1.coeff = coeff1
- if expansion1.create_null_expansion().effective_order != 0:
- self.fail()
- if expansion1.effective_order != 1:
- self.fail()
- def test_const_nilpo(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [3., 1.5, -4., 2, -1., 7.]
- expansion1.coeff = coeff1
- expansion2 = expansion1.get_const_part() + expansion1.get_nilpo_part()
- if expansion1 != expansion2:
- self.fail()
- def test_low_high_order_part(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [3., 1.5, -4., 2., -1., 7., 0., -3., 5., 1.]
- expansion1.coeff = coeff1
- expansion2 = expansion1.get_low_order_part(2) + expansion1.get_high_order_part(3)
- if expansion1 != expansion2:
- self.fail()
- def test_low_order_part_map(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [3., -1., 4., 2., 5., 1.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order2.copy()
- coeff2 = [-2., 6., 3., -2., 1., -1.]
- expansion2.coeff = coeff2
- map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
- if map1.get_low_order_part(1) != taylor_map.RealTaylorMap([expansion1.get_low_order_part(1),
- expansion2.get_low_order_part(1)]):
- self.fail()
- def test_high_order_part_map(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [3., -1., 4., 2., 5., 1.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order2.copy()
- coeff2 = [-2., 6., 3., -2., 1., -1.]
- expansion2.coeff = coeff2
- map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
- if map1.get_high_order_part(2) != taylor_map.RealTaylorMap([expansion1.get_high_order_part(2),
- expansion2.get_high_order_part(2)]):
- self.fail()
- def test_bounder_univariate(self):
- expansion1 = factory_taylor.zero_expansion(1, 5)
- coeff1 = [-0.5, 1.5, -4., 2., 1., 3.]
- expansion1.coeff = coeff1
- bounds = expansion1.bounder(Interval(-1., 1.))
- expected_lb = coeff1[0] - coeff1[1] + coeff1[2] - coeff1[3] - 0. - coeff1[5]
- expected_ub = coeff1[0] + coeff1[1] + 0. + coeff1[3] + coeff1[4] + coeff1[5]
- if bounds.lb != expected_lb:
- self.fail()
- if bounds.ub != expected_ub:
- self.fail()
- def test_bounder_univariate_map(self):
- expansion1 = factory_taylor.zero_expansion(1, 5)
- coeff1 = [-0.5, 1.5, -4., 2., 1., 3.]
- expansion1.coeff = coeff1
- expansion2 = factory_taylor.zero_expansion(1, 5)
- coeff2 = [2., 1., 4., 0., 8., -5.]
- expansion2.coeff = coeff2
- map_expans = taylor_map.RealTaylorMap([expansion1, expansion2])
- bounds1 = expansion1.bounder(Interval(-1., 1.))
- bounds2 = expansion2.bounder(Interval(-1., 1.))
- bounds_map = map_expans.bounder([Interval(-1., 1.)])
- if bounds1 != bounds_map[0]:
- self.fail()
- if bounds2 != bounds_map[1]:
- self.fail()
- def test_bounder_bivariate_map(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order3.copy()
- coeff2 = [0., -1., 2., -2., 4., 5., 2., -1., -7., 8.]
- expansion2.coeff = coeff2
- intervals = [Interval(-1., 1.), Interval(-2., 3.)]
- bounders_map = taylor_map.RealTaylorMap([expansion1, expansion2]).bounder(intervals)
- if bounders_map[0] != expansion1.bounder(intervals):
- self.fail()
- if bounders_map[1] != expansion2.bounder(intervals):
- self.fail()
- def test_truncated_prolong_univariate(self):
- old_order = 5
- expansion1 = factory_taylor.zero_expansion(1, old_order)
- coeff1 = [3., 1.5, -4., 2, -1., 6.]
- expansion1.coeff = coeff1
- new_order = 2
- trunc = expansion1.truncated(new_order)
- same = trunc.prolong(expansion1.order)
- coeff = same.coeff
- if not np.array_equal(coeff[:new_order + 1], coeff1[:new_order + 1]):
- self.fail()
- if not np.array_equal(coeff[new_order + 1:], np.zeros(old_order - new_order)):
- self.fail()
- def test_remove_contribution(self):
- expansion = null_expansion_2var_order2.copy()
- coeff = [0., 3., 4., -5., 0., -3.]
- expansion.coeff = coeff
- expansion1 = expansion.contrib_removed([0])
- expansion2 = expansion.contrib_removed([1])
- if expansion1 + expansion2 != expansion:
- self.fail()
- def test_remove_contributions(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- if expansion1.pointwise_eval([1., 0.]) != expansion1.contrib_removed([1]).pointwise_eval([1., 1.]):
- self.fail()
- def test_remove_contributions2(self):
- expansion1 = null_expansion_4var_order5.copy()
- expansion1.coeff = np.random.rand(expansion1.dim_alg)
- if expansion1.contrib_removed([1, 2]) != expansion1.contrib_removed([2, 1]):
- self.fail()
- def test_remove_last_contribution_map(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [3., -1., 4., 2., 5., 1.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order2.copy()
- coeff2 = [-2., 6., 3., -2., 1., -1.]
- expansion2.coeff = coeff2
- map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
- if map1.last_contrib_removed() != taylor_map.RealTaylorMap([expansion1.last_contrib_removed(),
- expansion2.last_contrib_removed()]):
- self.fail()
- def test_remove_dependency(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- dx1 = -3.
- dx2 = 2.
- if expansion1.var_eval(0, dx1).var_eval(1, dx2).const != \
- expansion1.pointwise_eval([dx1, dx2]):
- self.fail()
- def test_remove_last_dependency(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order3.copy()
- coeff2 = [-58., 25., 0., -3., 0., 0., -2., 0., 0., 0.]
- expansion2.coeff = coeff2
- if expansion1.last_var_eval(2.) != expansion2:
- self.fail()
- def test_remove_dependency_contribution(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- if expansion1.last_contrib_removed() != expansion1.last_var_eval(0.):
- self.fail()
- def test_remove_last_dependency_map(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [3., -1., 4., 2., 5., 1.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order2.copy()
- coeff2 = [-2., 6., 3., -2., 1., -1.]
- expansion2.coeff = coeff2
- map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
- dx2 = 2.
- if map1.last_var_eval(dx2) != taylor_map.RealTaylorMap([expansion1.last_var_eval(dx2),
- expansion2.last_var_eval(dx2)]):
- self.fail()
- def test_gradient(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [3., -1., 4., 2., 5., 1.]
- expansion1.coeff = coeff1
- grad = [coeff1[1], coeff1[2]]
- for i, el in enumerate(expansion1.gradient):
- if el != grad[i]:
- self.fail()
- def test_hessian(self):
- n_var = 2
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [3., -1., 4., 2., 5., 3., 0., 0., 1., -2.]
- expansion1.coeff = coeff1
- expected_hessian = np.array([[2. * coeff1[3], coeff1[4]], [coeff1[4], 2. * coeff1[5]]])
- hessian = expansion1.hessian
- for i in range(0, n_var):
- for j in range(0, n_var):
- if hessian[i, j] != expected_hessian[i, j]:
- self.fail()
- def test_monomial(self):
- expansion = null_expansion_2var_order3.copy()
- coeff1 = [0., 0., 0., 0., 0., 0., 0., 0., 5., 0.]
- expansion1 = expansion.create_expansion_with_coeff(coeff1)
- expansion2 = expansion.create_monomial_expansion_from_exponent([1, 2], 5.)
- if expansion1 != expansion2:
- self.fail()
- def test_truncate_bivariate(self):
- expansion1 = null_expansion_2var_order3.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- expansion2 = expansion1.truncated(2)
- coeff2 = expansion2.coeff
- coeff = expansion1.get_low_order_part(2).coeff
- if not np.array_equal(coeff1[:len(coeff2)], coeff[:len(coeff2)]):
- self.fail()
- if not np.array_equal(coeff2[:len(coeff2)], coeff[:len(coeff2)]):
- self.fail()
- if len(coeff2) > expansion2.dim_alg:
- self.fail()
- def test_truncate_prolong_trivariate(self):
- expansion = null_expansion_3var_order2.copy()
- coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion.coeff = coeff
- expansion2 = expansion.truncated(expansion.order - 1).prolong(expansion.order)
- if expansion != expansion2 + expansion.get_high_order_part(expansion.order):
- self.fail()
- def test_prolong_trivariate(self):
- expansion = null_expansion_3var_order2.copy()
- coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion.coeff = coeff
- if expansion.prolong_one_order() != expansion.prolong(expansion.order + 1):
- self.fail()
- def test_truncate_taylor(self):
- expansion1 = null_expansion_4var_order5.copy()
- coeff = np.random.rand(expansion1.dim_alg)
- expansion1.coeff = coeff
- expansion2 = expansion1.truncated(expansion1.order - 1)
- def fun(p):
- return exp(p**0.8 - 3. / p)
- expansion1 = fun(expansion1)
- expansion1 = expansion1.truncated(expansion1.order - 1)
- expansion2 = fun(expansion2)
- coeff1 = expansion1.coeff
- coeff2 = expansion2.coeff
- if not np.array_equal(coeff2[:len(coeff1)], coeff1):
- self.fail()
- def test_remove_unknown_univariate(self):
- expansion = factory_taylor.zero_expansion(n_var=1, order=3)
- coeff = [2., -3., 5., 4]
- expansion.coeff = coeff
- expansion2 = null_expansion_2var_order3.copy()
- expansion2.coeff = [2., -3., 0., 5., 0., 0., 4., 0., 0., 0.]
- if expansion2.last_var_removed() != expansion:
- self.fail()
- def test_append_unknown_univariate(self):
- expansion = factory_taylor.zero_expansion(n_var=1, order=3)
- coeff = [2., -3., 5., 4]
- expansion.coeff = coeff
- expansion2 = null_expansion_2var_order3.copy()
- expansion2.coeff = [2., -3., 0., 5., 0., 0., 4., 0., 0., 0.]
- if expansion.var_appended("x2") != expansion2:
- self.fail()
- def test_append_unknown_bivariate(self):
- expansion1 = null_expansion_2var_order2.copy()
- coeff1 = [2., 3., 4., -5., -1., -3.]
- expansion1.coeff = coeff1
- expansion2 = expansion1.var_appended("x3")
- coeff2 = expansion2.coeff
- if not np.array_equal(coeff1[:3], coeff2[:3]):
- self.fail()
- if coeff2[4] != -5. or coeff2[5] != -1. or coeff2[7] != -3.:
- self.fail()
- if coeff2[3] != 0 or coeff2[6] != 0 or coeff2[8] != 0 or coeff2[9] != 0:
- self.fail()
- def test_another_unknown(self):
- expansion1 = null_expansion_3var_order2.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- expansion2 = expansion1.var_appended("x4")
- expansion = expansion2.last_var_removed()
- if expansion != expansion1:
- self.fail()
- def test_remove_last_unknown(self):
- expansion1 = null_expansion_3var_order2.copy()
- coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
- expansion1.coeff = coeff1
- expansion2 = null_expansion_2var_order2.copy()
- coeff2 = [2., 3., 4., -1., -3., 1.]
- expansion2.coeff = coeff2
- if expansion1.last_var_removed() != expansion2:
- self.fail()
- def test_expansion_from_smaller_algebra(self):
- expansion = factory_taylor.zero_expansion(1, 3, ["x1"])
- coeff1 = [1., 2., 3., -4.]
- expansion.coeff = coeff1
- expansion = null_expansion_2var_order3.copy().create_expansion_from_smaller_algebra(expansion)
- coeff2 = expansion.coeff
- expected_coeff = [coeff1[0], coeff1[1], 0., coeff1[2], 0., 0., coeff1[3], 0., 0., 0.]
- if not np.array_equal(coeff2, expected_coeff):
- self.fail()
- def test_map_from_smaller_algebra(self):
- expansion1 = factory_taylor.zero_expansion(1, 3, ["x1"])
- coeff1 = [1., 2., 3., -4.]
- expansion1.coeff = coeff1
- expansion2 = expansion1.copy()
- coeff2 = [0., -2., 0., 5.]
- expansion2.coeff = coeff2
- expansions = taylor_map.RealTaylorMap([expansion1, expansion2])
- new_expansions = taylor_map.RealTaylorMap([null_expansion_2var_order3.copy()]).create_map_from_smaller_algebra(expansions)
- for i, expansion in enumerate(new_expansions):
- if expansion != null_expansion_2var_order3.copy().create_expansion_from_smaller_algebra(expansions[i]):
- self.fail()
- def test_non_regression(self):
- x, y, z = factory_taylor.create_unknown_map(order=4, consts=[1., 2., -1.])
- g = exp((sin(x) * cos(y) + 1.) / sqrt(1. + x**2 + y**2 + z**2))
- g = g.deriv_once_wrt_var(1).integ_once_wrt_var(0).integ_once_wrt_var(2)
- regre_coeff = [0., 0., 0., 0., 0., 0.,
- -0.45942301, 0., 0., 0., 0., 0.,
- -0.02996078, 0., 0.57760928, -0.05369175, 0., 0.,
- 0., 0., 0., 0., 0.06033602, 0.,
- 0.04807805, 0.01270756, 0., -0.19353301, 0.11813837, 0.0072345,
- 0., 0., 0., 0., 0.]
- if not np.allclose(regre_coeff, g.coeff):
- self.fail()
- if __name__ == '__main__':
- unittest.main()
|