test_miscellaneous.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import unittest
  2. import numpy as np
  3. from swiftt.interval import Interval
  4. from swiftt.taylor import taylor_map, factory_taylor
  5. from swiftt.math_algebra import cos, sin, exp, sqrt
  6. tol_coeff = 1.e-12
  7. null_expansion_2var_order2 = factory_taylor.zero_expansion(2, 2)
  8. null_expansion_2var_order3 = factory_taylor.zero_expansion(2, 3)
  9. null_expansion_3var_order2 = factory_taylor.zero_expansion(3, 2)
  10. null_expansion_4var_order5 = factory_taylor.zero_expansion(4, 5)
  11. class TestMiscellaneous(unittest.TestCase):
  12. def test_depth_univariate(self):
  13. expansion1 = factory_taylor.zero_expansion(1, 6)
  14. coeff1 = [0., 0., 0., 4, 0., 5., 8.]
  15. expansion1.coeff = coeff1
  16. if expansion1.create_null_expansion().total_depth != 7:
  17. self.fail()
  18. if expansion1.total_depth != 3:
  19. self.fail()
  20. def test_depth_bivariate(self):
  21. expansion1 = null_expansion_2var_order2.copy()
  22. coeff1 = [0., 0., 0., 4, 0., 5.]
  23. expansion1.coeff = coeff1
  24. if expansion1.create_null_expansion().total_depth != expansion1.order + 1:
  25. self.fail()
  26. if expansion1.total_depth != 2:
  27. self.fail()
  28. def test_effective_order_bivariate(self):
  29. expansion1 = null_expansion_2var_order2.copy()
  30. coeff1 = [2., 1., 0., 0, 0., 0.]
  31. expansion1.coeff = coeff1
  32. if expansion1.create_null_expansion().effective_order != 0:
  33. self.fail()
  34. if expansion1.effective_order != 1:
  35. self.fail()
  36. def test_const_nilpo(self):
  37. expansion1 = null_expansion_2var_order2.copy()
  38. coeff1 = [3., 1.5, -4., 2, -1., 7.]
  39. expansion1.coeff = coeff1
  40. expansion2 = expansion1.get_const_part() + expansion1.get_nilpo_part()
  41. if expansion1 != expansion2:
  42. self.fail()
  43. def test_low_high_order_part(self):
  44. expansion1 = null_expansion_2var_order3.copy()
  45. coeff1 = [3., 1.5, -4., 2., -1., 7., 0., -3., 5., 1.]
  46. expansion1.coeff = coeff1
  47. expansion2 = expansion1.get_low_order_part(2) + expansion1.get_high_order_part(3)
  48. if expansion1 != expansion2:
  49. self.fail()
  50. def test_low_order_part_map(self):
  51. expansion1 = null_expansion_2var_order2.copy()
  52. coeff1 = [3., -1., 4., 2., 5., 1.]
  53. expansion1.coeff = coeff1
  54. expansion2 = null_expansion_2var_order2.copy()
  55. coeff2 = [-2., 6., 3., -2., 1., -1.]
  56. expansion2.coeff = coeff2
  57. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  58. if map1.get_low_order_part(1) != taylor_map.RealTaylorMap([expansion1.get_low_order_part(1),
  59. expansion2.get_low_order_part(1)]):
  60. self.fail()
  61. def test_high_order_part_map(self):
  62. expansion1 = null_expansion_2var_order2.copy()
  63. coeff1 = [3., -1., 4., 2., 5., 1.]
  64. expansion1.coeff = coeff1
  65. expansion2 = null_expansion_2var_order2.copy()
  66. coeff2 = [-2., 6., 3., -2., 1., -1.]
  67. expansion2.coeff = coeff2
  68. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  69. if map1.get_high_order_part(2) != taylor_map.RealTaylorMap([expansion1.get_high_order_part(2),
  70. expansion2.get_high_order_part(2)]):
  71. self.fail()
  72. def test_bounder_univariate(self):
  73. expansion1 = factory_taylor.zero_expansion(1, 5)
  74. coeff1 = [-0.5, 1.5, -4., 2., 1., 3.]
  75. expansion1.coeff = coeff1
  76. bounds = expansion1.bounder(Interval(-1., 1.))
  77. expected_lb = coeff1[0] - coeff1[1] + coeff1[2] - coeff1[3] - 0. - coeff1[5]
  78. expected_ub = coeff1[0] + coeff1[1] + 0. + coeff1[3] + coeff1[4] + coeff1[5]
  79. if bounds.lb != expected_lb:
  80. self.fail()
  81. if bounds.ub != expected_ub:
  82. self.fail()
  83. def test_bounder_univariate_map(self):
  84. expansion1 = factory_taylor.zero_expansion(1, 5)
  85. coeff1 = [-0.5, 1.5, -4., 2., 1., 3.]
  86. expansion1.coeff = coeff1
  87. expansion2 = factory_taylor.zero_expansion(1, 5)
  88. coeff2 = [2., 1., 4., 0., 8., -5.]
  89. expansion2.coeff = coeff2
  90. map_expans = taylor_map.RealTaylorMap([expansion1, expansion2])
  91. bounds1 = expansion1.bounder(Interval(-1., 1.))
  92. bounds2 = expansion2.bounder(Interval(-1., 1.))
  93. bounds_map = map_expans.bounder([Interval(-1., 1.)])
  94. if bounds1 != bounds_map[0]:
  95. self.fail()
  96. if bounds2 != bounds_map[1]:
  97. self.fail()
  98. def test_bounder_bivariate_map(self):
  99. expansion1 = null_expansion_2var_order3.copy()
  100. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  101. expansion1.coeff = coeff1
  102. expansion2 = null_expansion_2var_order3.copy()
  103. coeff2 = [0., -1., 2., -2., 4., 5., 2., -1., -7., 8.]
  104. expansion2.coeff = coeff2
  105. intervals = [Interval(-1., 1.), Interval(-2., 3.)]
  106. bounders_map = taylor_map.RealTaylorMap([expansion1, expansion2]).bounder(intervals)
  107. if bounders_map[0] != expansion1.bounder(intervals):
  108. self.fail()
  109. if bounders_map[1] != expansion2.bounder(intervals):
  110. self.fail()
  111. def test_truncated_prolong_univariate(self):
  112. old_order = 5
  113. expansion1 = factory_taylor.zero_expansion(1, old_order)
  114. coeff1 = [3., 1.5, -4., 2, -1., 6.]
  115. expansion1.coeff = coeff1
  116. new_order = 2
  117. trunc = expansion1.truncated(new_order)
  118. same = trunc.prolong(expansion1.order)
  119. coeff = same.coeff
  120. if not np.array_equal(coeff[:new_order + 1], coeff1[:new_order + 1]):
  121. self.fail()
  122. if not np.array_equal(coeff[new_order + 1:], np.zeros(old_order - new_order)):
  123. self.fail()
  124. def test_remove_contribution(self):
  125. expansion = null_expansion_2var_order2.copy()
  126. coeff = [0., 3., 4., -5., 0., -3.]
  127. expansion.coeff = coeff
  128. expansion1 = expansion.contrib_removed([0])
  129. expansion2 = expansion.contrib_removed([1])
  130. if expansion1 + expansion2 != expansion:
  131. self.fail()
  132. def test_remove_contributions(self):
  133. expansion1 = null_expansion_2var_order3.copy()
  134. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  135. expansion1.coeff = coeff1
  136. if expansion1.pointwise_eval([1., 0.]) != expansion1.contrib_removed([1]).pointwise_eval([1., 1.]):
  137. self.fail()
  138. def test_remove_contributions2(self):
  139. expansion1 = null_expansion_4var_order5.copy()
  140. expansion1.coeff = np.random.rand(expansion1.dim_alg)
  141. if expansion1.contrib_removed([1, 2]) != expansion1.contrib_removed([2, 1]):
  142. self.fail()
  143. def test_remove_last_contribution_map(self):
  144. expansion1 = null_expansion_2var_order2.copy()
  145. coeff1 = [3., -1., 4., 2., 5., 1.]
  146. expansion1.coeff = coeff1
  147. expansion2 = null_expansion_2var_order2.copy()
  148. coeff2 = [-2., 6., 3., -2., 1., -1.]
  149. expansion2.coeff = coeff2
  150. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  151. if map1.last_contrib_removed() != taylor_map.RealTaylorMap([expansion1.last_contrib_removed(),
  152. expansion2.last_contrib_removed()]):
  153. self.fail()
  154. def test_remove_dependency(self):
  155. expansion1 = null_expansion_2var_order3.copy()
  156. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  157. expansion1.coeff = coeff1
  158. dx1 = -3.
  159. dx2 = 2.
  160. if expansion1.var_eval(0, dx1).var_eval(1, dx2).const != \
  161. expansion1.pointwise_eval([dx1, dx2]):
  162. self.fail()
  163. def test_remove_last_dependency(self):
  164. expansion1 = null_expansion_2var_order3.copy()
  165. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  166. expansion1.coeff = coeff1
  167. expansion2 = null_expansion_2var_order3.copy()
  168. coeff2 = [-58., 25., 0., -3., 0., 0., -2., 0., 0., 0.]
  169. expansion2.coeff = coeff2
  170. if expansion1.last_var_eval(2.) != expansion2:
  171. self.fail()
  172. def test_remove_dependency_contribution(self):
  173. expansion1 = null_expansion_2var_order3.copy()
  174. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  175. expansion1.coeff = coeff1
  176. if expansion1.last_contrib_removed() != expansion1.last_var_eval(0.):
  177. self.fail()
  178. def test_remove_last_dependency_map(self):
  179. expansion1 = null_expansion_2var_order2.copy()
  180. coeff1 = [3., -1., 4., 2., 5., 1.]
  181. expansion1.coeff = coeff1
  182. expansion2 = null_expansion_2var_order2.copy()
  183. coeff2 = [-2., 6., 3., -2., 1., -1.]
  184. expansion2.coeff = coeff2
  185. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  186. dx2 = 2.
  187. if map1.last_var_eval(dx2) != taylor_map.RealTaylorMap([expansion1.last_var_eval(dx2),
  188. expansion2.last_var_eval(dx2)]):
  189. self.fail()
  190. def test_gradient(self):
  191. expansion1 = null_expansion_2var_order2.copy()
  192. coeff1 = [3., -1., 4., 2., 5., 1.]
  193. expansion1.coeff = coeff1
  194. grad = [coeff1[1], coeff1[2]]
  195. for i, el in enumerate(expansion1.gradient):
  196. if el != grad[i]:
  197. self.fail()
  198. def test_hessian(self):
  199. n_var = 2
  200. expansion1 = null_expansion_2var_order3.copy()
  201. coeff1 = [3., -1., 4., 2., 5., 3., 0., 0., 1., -2.]
  202. expansion1.coeff = coeff1
  203. expected_hessian = np.array([[2. * coeff1[3], coeff1[4]], [coeff1[4], 2. * coeff1[5]]])
  204. hessian = expansion1.hessian
  205. for i in range(0, n_var):
  206. for j in range(0, n_var):
  207. if hessian[i, j] != expected_hessian[i, j]:
  208. self.fail()
  209. def test_monomial(self):
  210. expansion = null_expansion_2var_order3.copy()
  211. coeff1 = [0., 0., 0., 0., 0., 0., 0., 0., 5., 0.]
  212. expansion1 = expansion.create_expansion_with_coeff(coeff1)
  213. expansion2 = expansion.create_monomial_expansion_from_exponent([1, 2], 5.)
  214. if expansion1 != expansion2:
  215. self.fail()
  216. def test_truncate_bivariate(self):
  217. expansion1 = null_expansion_2var_order3.copy()
  218. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  219. expansion1.coeff = coeff1
  220. expansion2 = expansion1.truncated(2)
  221. coeff2 = expansion2.coeff
  222. coeff = expansion1.get_low_order_part(2).coeff
  223. if not np.array_equal(coeff1[:len(coeff2)], coeff[:len(coeff2)]):
  224. self.fail()
  225. if not np.array_equal(coeff2[:len(coeff2)], coeff[:len(coeff2)]):
  226. self.fail()
  227. if len(coeff2) > expansion2.dim_alg:
  228. self.fail()
  229. def test_truncate_prolong_trivariate(self):
  230. expansion = null_expansion_3var_order2.copy()
  231. coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  232. expansion.coeff = coeff
  233. expansion2 = expansion.truncated(expansion.order - 1).prolong(expansion.order)
  234. if expansion != expansion2 + expansion.get_high_order_part(expansion.order):
  235. self.fail()
  236. def test_prolong_trivariate(self):
  237. expansion = null_expansion_3var_order2.copy()
  238. coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  239. expansion.coeff = coeff
  240. if expansion.prolong_one_order() != expansion.prolong(expansion.order + 1):
  241. self.fail()
  242. def test_truncate_taylor(self):
  243. expansion1 = null_expansion_4var_order5.copy()
  244. coeff = np.random.rand(expansion1.dim_alg)
  245. expansion1.coeff = coeff
  246. expansion2 = expansion1.truncated(expansion1.order - 1)
  247. def fun(p):
  248. return exp(p**0.8 - 3. / p)
  249. expansion1 = fun(expansion1)
  250. expansion1 = expansion1.truncated(expansion1.order - 1)
  251. expansion2 = fun(expansion2)
  252. coeff1 = expansion1.coeff
  253. coeff2 = expansion2.coeff
  254. if not np.array_equal(coeff2[:len(coeff1)], coeff1):
  255. self.fail()
  256. def test_remove_unknown_univariate(self):
  257. expansion = factory_taylor.zero_expansion(n_var=1, order=3)
  258. coeff = [2., -3., 5., 4]
  259. expansion.coeff = coeff
  260. expansion2 = null_expansion_2var_order3.copy()
  261. expansion2.coeff = [2., -3., 0., 5., 0., 0., 4., 0., 0., 0.]
  262. if expansion2.last_var_removed() != expansion:
  263. self.fail()
  264. def test_append_unknown_univariate(self):
  265. expansion = factory_taylor.zero_expansion(n_var=1, order=3)
  266. coeff = [2., -3., 5., 4]
  267. expansion.coeff = coeff
  268. expansion2 = null_expansion_2var_order3.copy()
  269. expansion2.coeff = [2., -3., 0., 5., 0., 0., 4., 0., 0., 0.]
  270. if expansion.var_appended("x2") != expansion2:
  271. self.fail()
  272. def test_append_unknown_bivariate(self):
  273. expansion1 = null_expansion_2var_order2.copy()
  274. coeff1 = [2., 3., 4., -5., -1., -3.]
  275. expansion1.coeff = coeff1
  276. expansion2 = expansion1.var_appended("x3")
  277. coeff2 = expansion2.coeff
  278. if not np.array_equal(coeff1[:3], coeff2[:3]):
  279. self.fail()
  280. if coeff2[4] != -5. or coeff2[5] != -1. or coeff2[7] != -3.:
  281. self.fail()
  282. if coeff2[3] != 0 or coeff2[6] != 0 or coeff2[8] != 0 or coeff2[9] != 0:
  283. self.fail()
  284. def test_another_unknown(self):
  285. expansion1 = null_expansion_3var_order2.copy()
  286. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  287. expansion1.coeff = coeff1
  288. expansion2 = expansion1.var_appended("x4")
  289. expansion = expansion2.last_var_removed()
  290. if expansion != expansion1:
  291. self.fail()
  292. def test_remove_last_unknown(self):
  293. expansion1 = null_expansion_3var_order2.copy()
  294. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  295. expansion1.coeff = coeff1
  296. expansion2 = null_expansion_2var_order2.copy()
  297. coeff2 = [2., 3., 4., -1., -3., 1.]
  298. expansion2.coeff = coeff2
  299. if expansion1.last_var_removed() != expansion2:
  300. self.fail()
  301. def test_expansion_from_smaller_algebra(self):
  302. expansion = factory_taylor.zero_expansion(1, 3, ["x1"])
  303. coeff1 = [1., 2., 3., -4.]
  304. expansion.coeff = coeff1
  305. expansion = null_expansion_2var_order3.copy().create_expansion_from_smaller_algebra(expansion)
  306. coeff2 = expansion.coeff
  307. expected_coeff = [coeff1[0], coeff1[1], 0., coeff1[2], 0., 0., coeff1[3], 0., 0., 0.]
  308. if not np.array_equal(coeff2, expected_coeff):
  309. self.fail()
  310. def test_map_from_smaller_algebra(self):
  311. expansion1 = factory_taylor.zero_expansion(1, 3, ["x1"])
  312. coeff1 = [1., 2., 3., -4.]
  313. expansion1.coeff = coeff1
  314. expansion2 = expansion1.copy()
  315. coeff2 = [0., -2., 0., 5.]
  316. expansion2.coeff = coeff2
  317. expansions = taylor_map.RealTaylorMap([expansion1, expansion2])
  318. new_expansions = taylor_map.RealTaylorMap([null_expansion_2var_order3.copy()]).create_map_from_smaller_algebra(expansions)
  319. for i, expansion in enumerate(new_expansions):
  320. if expansion != null_expansion_2var_order3.copy().create_expansion_from_smaller_algebra(expansions[i]):
  321. self.fail()
  322. def test_non_regression(self):
  323. x, y, z = factory_taylor.create_unknown_map(order=4, consts=[1., 2., -1.])
  324. g = exp((sin(x) * cos(y) + 1.) / sqrt(1. + x**2 + y**2 + z**2))
  325. g = g.deriv_once_wrt_var(1).integ_once_wrt_var(0).integ_once_wrt_var(2)
  326. regre_coeff = [0., 0., 0., 0., 0., 0.,
  327. -0.45942301, 0., 0., 0., 0., 0.,
  328. -0.02996078, 0., 0.57760928, -0.05369175, 0., 0.,
  329. 0., 0., 0., 0., 0.06033602, 0.,
  330. 0.04807805, 0.01270756, 0., -0.19353301, 0.11813837, 0.0072345,
  331. 0., 0., 0., 0., 0.]
  332. if not np.allclose(regre_coeff, g.coeff):
  333. self.fail()
  334. if __name__ == '__main__':
  335. unittest.main()