test_basic_operation.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. import unittest
  2. import numpy as np
  3. from swiftt.taylor import taylor_map, factory_taylor
  4. tol_coeff = 1.e-12
  5. null_expansion_2var_order2 = factory_taylor.zero_expansion(2, 2)
  6. null_expansion_2var_order3 = factory_taylor.zero_expansion(2, 3)
  7. null_expansion_3var_order2 = factory_taylor.zero_expansion(3, 2)
  8. null_expansion_4var_order5 = factory_taylor.zero_expansion(4, 5)
  9. class TestBasicOperation(unittest.TestCase):
  10. def test_eval_univariate(self):
  11. expansion = factory_taylor.zero_expansion(n_var=1, order=3)
  12. coeff = [2., -3., 5., 4]
  13. expansion.coeff = coeff
  14. if expansion.pointwise_eval(2.) != 48. or expansion(2.) != 48.:
  15. self.fail()
  16. def test_add_expansion_univariate(self):
  17. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  18. coeff1 = [2., -3., 5., 4]
  19. expansion1.coeff = coeff1
  20. expansion2 = factory_taylor.zero_expansion(n_var=1, order=3)
  21. coeff2 = [1., 7., -6., 0.]
  22. expansion2.coeff = coeff2
  23. sum = expansion1 + expansion2
  24. coeff = sum.coeff
  25. if not np.array_equal(coeff, np.array(coeff1) + np.array(coeff2)):
  26. self.fail()
  27. def test_add_const_univariate(self):
  28. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  29. coeff1 = [2., -3., 5., 4]
  30. expansion1.coeff = coeff1
  31. expansion2 = expansion1 + 6.
  32. coeff = expansion2.coeff
  33. if coeff[0] != 8.:
  34. self.fail()
  35. if not np.array_equal(coeff[1:], coeff1[1:]):
  36. self.fail()
  37. def test_linearly_combine_expansion_univariate(self):
  38. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  39. coeff1 = [2., -3., 5., 4]
  40. expansion1.coeff = coeff1
  41. expansion2 = factory_taylor.zero_expansion(n_var=1, order=3)
  42. coeff2 = [1., 7., -6., 0.]
  43. expansion2.coeff = coeff2
  44. a = 2.
  45. b = -1.
  46. expansion = expansion1.linearly_combine_with_another(a, expansion2, b)
  47. coeff = expansion.coeff
  48. if not np.array_equal(coeff, a * np.array(coeff1) + b * np.array(coeff2)):
  49. self.fail()
  50. expansion = expansion1.linearly_combine_with_many(a, [expansion2], [b])
  51. coeff = expansion.coeff
  52. if not np.array_equal(coeff, a * np.array(coeff1) + b * np.array(coeff2)):
  53. self.fail()
  54. def test_sub_expansion_univariate(self):
  55. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  56. coeff1 = [2., -3., 5., 4]
  57. expansion1.coeff = coeff1
  58. expansion2 = factory_taylor.zero_expansion(n_var=1, order=3)
  59. coeff2 = [1., 7., -6., 0.]
  60. expansion2.coeff = coeff2
  61. diff = expansion1 - expansion2
  62. coeff = diff.coeff
  63. if not np.array_equal(coeff, np.array(coeff1) - np.array(coeff2)):
  64. self.fail()
  65. def test_sub_const_univariate(self):
  66. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  67. coeff1 = [2., -3., 5., 4]
  68. expansion1.coeff = coeff1
  69. a = 6.
  70. expansion2 = expansion1 - a
  71. coeff = expansion2.coeff
  72. if coeff[0] != coeff1[0] - a:
  73. self.fail()
  74. if not np.array_equal(coeff1[1:], coeff[1:]):
  75. self.fail()
  76. def test_radd_const_univariate(self):
  77. expansion1 = factory_taylor.zero_expansion(1, 5)
  78. coeff1 = [2., -3., 5., 4, 0., 1.]
  79. expansion1.coeff = coeff1
  80. a = 1
  81. sum = a + expansion1
  82. coeff = sum.coeff
  83. if coeff[0] != coeff1[0] + a:
  84. self.fail()
  85. if not np.array_equal(coeff1[1:], coeff[1:]):
  86. self.fail()
  87. def test_rsub_const_univariate(self):
  88. expansion1 = factory_taylor.zero_expansion(1, 5)
  89. coeff1 = [2., -3., 5., 4, 0., 1.]
  90. expansion1.coeff = coeff1
  91. a = 1.
  92. diff = a - expansion1
  93. coeff = diff.coeff
  94. if coeff[0] != a - coeff1[0]:
  95. self.fail()
  96. if not np.array_equal(coeff1[1:], -np.array(coeff[1:])):
  97. self.fail()
  98. def test_rmul_const_univariate(self):
  99. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  100. coeff1 = [2., -3., 5., 4]
  101. expansion1.coeff = coeff1
  102. factor = -2.
  103. mul = factor * expansion1
  104. coeff = mul.coeff
  105. if not np.array_equal(coeff, factor * np.array(coeff1)):
  106. self.fail()
  107. def test_mul_expansion_univariate(self):
  108. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  109. coeff1 = [2., -3., 5., 4]
  110. expansion1.coeff = coeff1
  111. expansion2 = factory_taylor.zero_expansion(n_var=1, order=3)
  112. coeff2 = [1., 7., -6., 0.]
  113. expansion2.coeff = coeff2
  114. mul = expansion1 * expansion2
  115. coeff = mul.coeff
  116. if coeff[0] != 2. or coeff[1] != 11. or coeff[2] != -28. or coeff[3] != 57.:
  117. self.fail()
  118. def test_div_real_univariate(self):
  119. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  120. coeff1 = [2., -3., 5., 4]
  121. expansion1.coeff = coeff1
  122. factor = 2.
  123. div = expansion1 / factor
  124. coeff = div.coeff
  125. if not np.array_equal(coeff, np.array(coeff1) / factor):
  126. self.fail()
  127. def test_div_expansion_univariate(self):
  128. expansion1 = factory_taylor.zero_expansion(n_var=1, order=3)
  129. coeff1 = [1., 0., 0., 0]
  130. expansion1.coeff = coeff1
  131. expansion2 = factory_taylor.zero_expansion(n_var=1, order=3)
  132. coeff2 = [0.5, 1., 0., 0.]
  133. expansion2.coeff = coeff2
  134. div = expansion1 / expansion2
  135. coeff = div.coeff
  136. if coeff[0] != 2. or coeff[1] != -4. or coeff[2] != 8. or coeff[3] != -16.:
  137. self.fail()
  138. def test_div_univariate(self):
  139. expansion1 = factory_taylor.zero_expansion(1, 5)
  140. coeff1 = [2., -3., 5., 4, 0., 1.]
  141. expansion1.coeff = coeff1
  142. expansion2 = expansion1 ** (-1)
  143. div = 1. / expansion1
  144. if expansion2 != div:
  145. self.fail()
  146. def test_pown_univariate(self):
  147. expansion1 = factory_taylor.zero_expansion(1, 5)
  148. coeff1 = [2., -3., 5., 4, 0.5, 1.]
  149. expansion1.coeff = coeff1
  150. product = expansion1.copy()
  151. for i in range(2, 6):
  152. expansion2 = expansion1**i
  153. product *= expansion1
  154. if product != expansion2:
  155. self.fail()
  156. def test_pown_bivariate(self):
  157. expansion1 = null_expansion_2var_order2.copy()
  158. coeff1 = [2., -3., 5., 4, 0.5, 1.]
  159. expansion1.coeff = coeff1
  160. product = expansion1.copy()
  161. for i in range(2, 6):
  162. expansion2 = expansion1**i
  163. product *= expansion1
  164. if product != expansion2:
  165. self.fail()
  166. def test_pow_bivariate(self):
  167. expansion1 = null_expansion_2var_order2.copy()
  168. coeff1 = [2., -3., 5., 4, 0.5, 1.]
  169. expansion1.coeff = coeff1
  170. product = expansion1.copy()
  171. for i in range(2, 6):
  172. expansion2 = expansion1**float(i)
  173. product *= expansion1
  174. if product != expansion2:
  175. self.fail()
  176. def test_lin_univariate(self):
  177. expansion1 = factory_taylor.zero_expansion(1, 5)
  178. coeff1 = [2., -3., 5., 4, 0., 1.]
  179. expansion1.coeff = coeff1
  180. expansion2 = expansion1.get_linear_part()
  181. coeff = expansion2.coeff
  182. if coeff[1] != coeff1[1]:
  183. self.fail()
  184. if not np.array_equal(coeff[2:], np.zeros(len(coeff) - 2)):
  185. self.fail()
  186. def test_lin_nonlin(self):
  187. expansion1 = null_expansion_2var_order2.copy()
  188. coeff1 = [3., 1.5, -4., 2, -1., 7.]
  189. expansion1.coeff = coeff1
  190. if expansion1 != 3. + expansion1.get_linear_part() + expansion1.get_nonlin_part():
  191. self.fail()
  192. if expansion1 != expansion1.get_affine_part() + expansion1.get_nonlin_part():
  193. self.fail()
  194. def test_add_complex_univariate(self):
  195. expansion1 = factory_taylor.zero_expansion(1, 5, ["x"], np.complex128)
  196. coeff1 = [complex(3., 1.), 1.5, -4., 2, -1., 6.]
  197. expansion1.coeff = coeff1
  198. el = complex(-1.5, 10.)
  199. added = expansion1 + el
  200. coeff = added.coeff
  201. if coeff[0] != coeff1[0] + el:
  202. self.fail()
  203. if not np.array_equal(coeff[1:], coeff1[1:]):
  204. self.fail()
  205. def test_mul_complex_univariate(self):
  206. expansion1 = factory_taylor.zero_expansion(1, 3, ["x"], np.complex128)
  207. coeff1 = [complex(3., 2.), 1.5, -4., 2.]
  208. expansion1.coeff = coeff1
  209. factor = complex(-2., 3.)
  210. multiplied = expansion1 * factor
  211. coeff = multiplied.coeff
  212. if not np.array_equal(coeff, factor * np.array(coeff1)):
  213. self.fail()
  214. def test_multiplicative_inverse_univariate(self):
  215. expansion = factory_taylor.zero_expansion(n_var=1, order=10)
  216. expansion.coeff = [2., 1., 3., -1., 5., -4., 7., -0.5, 1., -3., 8.]
  217. inv_const = 1. / expansion.const
  218. scaled_expans = expansion * inv_const
  219. iter_mul_inverse = 2. - scaled_expans
  220. for __ in range(1, expansion.order // 2 + 1):
  221. iter_mul_inverse *= 2. - scaled_expans * iter_mul_inverse
  222. iter_mul_inverse *= inv_const
  223. if not np.array_equal(iter_mul_inverse.coeff, (1. / expansion).coeff):
  224. self.fail()
  225. def test_multiplicative_inverse_bivariate(self):
  226. expansion = null_expansion_2var_order3.copy()
  227. expansion.coeff = [2., 1., 3., -1., 5., -4., 7., -0.5, 1., 2.]
  228. inv_const = 1. / expansion.const
  229. scaled_expans = expansion * inv_const
  230. iter_mul_inverse = 2. - scaled_expans
  231. for __ in range(1, expansion.order // 2 + 1):
  232. iter_mul_inverse *= 2. - scaled_expans * iter_mul_inverse
  233. iter_mul_inverse *= inv_const
  234. if not np.array_equal(iter_mul_inverse.coeff, (1. / expansion).coeff):
  235. self.fail()
  236. # def test_complex_map_inversion(self):
  237. # order = 20
  238. # z = complex(0.5, 1.)
  239. # complex_map = factory.create_unknown_map(order, [z])
  240. # square = expansion_map.ComplexMap([complex_map[0] * complex_map[0]])
  241. # inverted = square.get_nilpo_part().inverse()
  242. # composed = square(inverted)
  243. # coeff = composed[0].coeff
  244. # if cnp.polar(coeff[1] - complex(1., 0.))[0] > tol_coeff:
  245. # return self.fail()
  246. # for i in range(2, len(coeff)):
  247. # if cnp.polar(coeff[i])[0] > tol_coeff:
  248. # self.fail()
  249. def test_pow2_bivariate(self):
  250. expansion1 = null_expansion_2var_order3.copy()
  251. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  252. expansion1.coeff = coeff1
  253. if expansion1**2 != expansion1 * expansion1:
  254. self.fail()
  255. def test_divide_by_var(self):
  256. expansion1 = null_expansion_2var_order2.copy()
  257. coeff1 = [0., 0., 4., 0., -1., -3.]
  258. expansion1.coeff = coeff1
  259. expansion2 = null_expansion_2var_order2.copy()
  260. coeff2 = [4., -1., -3., 0., 0., 0.]
  261. expansion2.coeff = coeff2
  262. if expansion1.divided_by_var(1) != expansion2:
  263. self.fail()
  264. def test_eval_bivariate(self):
  265. expansion1 = null_expansion_2var_order2.copy()
  266. coeff1 = [2., 3., 4., -5., -1., -3.]
  267. expansion1.coeff = coeff1
  268. x = [1., -2.]
  269. if expansion1.pointwise_eval(x) != -18. or expansion1(x) != -18.:
  270. self.fail()
  271. def test_massive_eval_univariate(self):
  272. expans = factory_taylor.zero_expansion(1, 6).create_expansion_with_coeff([1., 2., -3., 4., 5., -6., 7.])
  273. Xs = np.array([0., 1., -5., 10., -2.])
  274. evaluated = expans.massive_eval(Xs)
  275. for el, x in zip(evaluated, Xs):
  276. if expans.pointwise_eval(x) != el:
  277. self.fail()
  278. def test_massive_eval_trivariate(self):
  279. expansion1 = null_expansion_2var_order3.copy()
  280. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  281. expansion1.coeff = coeff1
  282. x = np.random.rand(4, 2)
  283. eval_all = expansion1.massive_eval(x)
  284. for i, el in enumerate(eval_all):
  285. if np.fabs(el - expansion1.pointwise_eval(x[i, :])) > 1.e-8:
  286. print(expansion1.pointwise_eval(x[i, :]))
  287. self.fail()
  288. def test_massive_eval_map(self):
  289. expansion1 = null_expansion_3var_order2.copy()
  290. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  291. expansion1.coeff = coeff1
  292. expansion2 = null_expansion_3var_order2.copy()
  293. coeff2 = [1., -2., 1., 4., 2., 0., -6., 1., -2., 3.]
  294. expansion2.coeff = coeff2
  295. list_expansion = [expansion1, expansion2]
  296. map_expansion = taylor_map.RealTaylorMap(list_expansion)
  297. x = np.random.rand(5, 3)
  298. eval_both = map_expansion.massive_eval(x)
  299. for i, expans in enumerate(list_expansion):
  300. for j, el in enumerate(x):
  301. evaluated = expans.pointwise_eval(el)
  302. if np.fabs(eval_both[j, i] - evaluated) > 1.e-8:
  303. self.fail()
  304. def test_mul_bivariate(self):
  305. expansion1 = null_expansion_2var_order2.copy()
  306. coeff1 = [2., 3., 4., -5., -1., -3.]
  307. expansion2 = null_expansion_2var_order2.copy()
  308. coeff2 = [-1., 0., 1., 2., 3., -1.]
  309. expansion1.coeff = coeff1
  310. expansion2.coeff = coeff2
  311. expansion = expansion1 * expansion2
  312. coeff = expansion.coeff
  313. if not np.array_equal(coeff, [-2., -3., -2., 9., 10., 5.]):
  314. self.fail()
  315. def test_add_maps(self):
  316. expansion1 = null_expansion_2var_order2.copy()
  317. coeff1 = [3., -1., 4., 2., 5., 1.]
  318. expansion1.coeff = coeff1
  319. expansion2 = null_expansion_2var_order2.copy()
  320. coeff2 = [-2., 6., 3., -2., 1., -1.]
  321. expansion2.coeff = coeff2
  322. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  323. map2 = taylor_map.RealTaylorMap([expansion2, expansion1])
  324. sum1 = map1 + map2
  325. sum2 = map2 + map1
  326. if sum1 != sum2:
  327. return self.fail()
  328. def test_sub_maps(self):
  329. expansion1 = null_expansion_2var_order2.copy()
  330. coeff1 = [3., -1., 4., 2., 5., 1.]
  331. expansion1.coeff = coeff1
  332. expansion2 = null_expansion_2var_order2.copy()
  333. coeff2 = [-2., 6., 3., -2., 1., -1.]
  334. expansion2.coeff = coeff2
  335. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  336. map2 = taylor_map.RealTaylorMap([expansion2, expansion1])
  337. diff1 = map1 - map2
  338. diff2 = map2 - map1
  339. if diff1 != -diff2:
  340. return self.fail()
  341. def test_add_map_nparray(self):
  342. expansion1 = null_expansion_2var_order2.copy()
  343. coeff1 = [3., -1., 4., 2., 5., 1.]
  344. expansion1.coeff = coeff1
  345. expansion2 = null_expansion_2var_order2.copy()
  346. coeff2 = [-2., 6., 3., -2., 1., -1.]
  347. expansion2.coeff = coeff2
  348. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  349. added = np.array([2., 4.])
  350. sum1 = map1 + added
  351. map1.const = map1.const + added
  352. if sum1 != map1:
  353. self.fail()
  354. def test_add_map_float(self):
  355. expansion1 = null_expansion_2var_order2.copy()
  356. coeff1 = [3., -1., 4., 2., 5., 1.]
  357. expansion1.coeff = coeff1
  358. expansion2 = null_expansion_2var_order2.copy()
  359. coeff2 = [-2., 6., 3., -2., 1., -1.]
  360. expansion2.coeff = coeff2
  361. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  362. added = 6.
  363. sum1 = map1 + added
  364. map1.const = map1.const + np.ones(2) * added
  365. if sum1 != map1:
  366. self.fail()
  367. def test_add_map_expansion(self):
  368. expansion1 = null_expansion_2var_order2.copy()
  369. coeff1 = [3., -1., 4., 2., 5., 1.]
  370. expansion1.coeff = coeff1
  371. expansion2 = null_expansion_2var_order2.copy()
  372. coeff2 = [-2., 6., 3., -2., 1., -1.]
  373. expansion2.coeff = coeff2
  374. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  375. expansion = null_expansion_2var_order2.copy()
  376. coeff = [2., 1., -3., 4., 0., 7.]
  377. expansion.coeff = coeff
  378. sum1 = map1 + expansion
  379. if sum1 != taylor_map.RealTaylorMap([expansion1 + expansion, expansion2 + expansion]):
  380. self.fail()
  381. def test_sub_map_nparray(self):
  382. expansion1 = null_expansion_2var_order2.copy()
  383. coeff1 = [3., -1., 4., 2., 5., 1.]
  384. expansion1.coeff = coeff1
  385. expansion2 = null_expansion_2var_order2.copy()
  386. coeff2 = [-2., 6., 3., -2., 1., -1.]
  387. expansion2.coeff = coeff2
  388. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  389. subtracted = np.array([2., 4.])
  390. sum1 = map1 - subtracted
  391. map1.const = map1.const - subtracted
  392. if sum1 != map1:
  393. self.fail()
  394. def test_sub_map_float(self):
  395. expansion1 = null_expansion_2var_order2.copy()
  396. coeff1 = [3., -1., 4., 2., 5., 1.]
  397. expansion1.coeff = coeff1
  398. expansion2 = null_expansion_2var_order2.copy()
  399. coeff2 = [-2., 6., 3., -2., 1., -1.]
  400. expansion2.coeff = coeff2
  401. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  402. subtracted = 6.
  403. sum1 = map1 - subtracted
  404. map1.const = map1.const - np.ones(2) * subtracted
  405. if sum1 != map1:
  406. self.fail()
  407. def test_sub_map_expansion(self):
  408. expansion1 = null_expansion_2var_order2.copy()
  409. coeff1 = [3., -1., 4., 2., 5., 1.]
  410. expansion1.coeff = coeff1
  411. expansion2 = null_expansion_2var_order2.copy()
  412. coeff2 = [-2., 6., 3., -2., 1., -1.]
  413. expansion2.coeff = coeff2
  414. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  415. expansion = null_expansion_2var_order2.copy()
  416. coeff = [2., 1., +3., 4., 0., 7.]
  417. expansion.coeff = coeff
  418. sub = map1 - expansion
  419. if sub != taylor_map.RealTaylorMap([expansion1 - expansion, expansion2 - expansion]):
  420. self.fail()
  421. def test_mul_map_float(self):
  422. expansion1 = null_expansion_2var_order2.copy()
  423. coeff1 = [3., -1., 4., 2., 5., 1.]
  424. expansion1.coeff = coeff1
  425. expansion2 = null_expansion_2var_order2.copy()
  426. coeff2 = [-2., 6., 3., -2., 1., -1.]
  427. expansion2.coeff = coeff2
  428. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  429. factor = 6.
  430. product = map1 * factor
  431. if product != taylor_map.RealTaylorMap([factor * expansion1, factor * expansion2]):
  432. self.fail()
  433. def test_mul_map_expansion(self):
  434. expansion1 = null_expansion_2var_order2.copy()
  435. coeff1 = [3., -1., 4., 2., 5., 1.]
  436. expansion1.coeff = coeff1
  437. expansion2 = null_expansion_2var_order2.copy()
  438. coeff2 = [-2., 6., 3., -2., 1., -1.]
  439. expansion2.coeff = coeff2
  440. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  441. expansion = null_expansion_2var_order2.copy()
  442. coeff = [2., 1., -3., -4., 0., 7.]
  443. expansion.coeff = coeff
  444. product = map1 * expansion
  445. if product != taylor_map.RealTaylorMap([expansion1 * expansion, expansion2 * expansion]):
  446. self.fail()
  447. def test_pow2_map_expansion(self):
  448. expansion1 = null_expansion_2var_order2.copy()
  449. coeff1 = [3., -1., 4., 2., 5., 1.]
  450. expansion1.coeff = coeff1
  451. expansion2 = null_expansion_2var_order2.copy()
  452. coeff2 = [-2., 6., 3., -2., 1., -1.]
  453. expansion2.coeff = coeff2
  454. squared_map = taylor_map.RealTaylorMap([expansion1, expansion2]) ** 2
  455. if squared_map[0] != expansion1 ** 2:
  456. self.fail()
  457. if squared_map[1] != expansion2 ** 2:
  458. self.fail()
  459. def test_eval_map(self):
  460. expansion1 = null_expansion_2var_order2.copy()
  461. coeff1 = [3., -1., 4., 2., 5., 1.]
  462. expansion1.coeff = coeff1
  463. expansion2 = null_expansion_2var_order2.copy()
  464. coeff2 = [-2., 6., 3., -2., 1., -1.]
  465. expansion2.coeff = coeff2
  466. map = taylor_map.RealTaylorMap([expansion1, expansion2])
  467. eval0 = map.pointwise_eval(np.zeros(2))
  468. expected = [coeff1[0], coeff2[0]]
  469. for i, el in enumerate(map.const):
  470. if el != eval0[i] or el != expected[i]:
  471. self.fail()
  472. x = np.array([0.5, 3.])
  473. evalx = map.pointwise_eval(x)
  474. for i, el in enumerate(evalx):
  475. if el != map[i].pointwise_eval(x):
  476. self.fail()
  477. def test_truediv_map_float(self):
  478. expansion1 = null_expansion_2var_order2.copy()
  479. coeff1 = [2., 3., 4., -5., -1., -3.]
  480. expansion1.coeff = coeff1
  481. expansion2 = null_expansion_2var_order2.copy()
  482. coeff2 = [-2., 6., 3., -2., 1., -1.]
  483. expansion2.coeff = coeff2
  484. factor = 2.
  485. map_expansion = taylor_map.RealTaylorMap([expansion1, expansion2])
  486. divided_map = map_expansion / factor
  487. for i, expansion in enumerate(divided_map):
  488. if expansion != map_expansion[i] / factor:
  489. self.fail()
  490. def test_truediv_map_expansion(self):
  491. expansion1 = null_expansion_2var_order2.copy()
  492. coeff1 = [2., 3., 4., -5., -1., -3.]
  493. expansion1.coeff = coeff1
  494. expansion2 = null_expansion_2var_order2.copy()
  495. coeff2 = [-2., 6., 3., -2., 1., -1.]
  496. expansion2.coeff = coeff2
  497. expansion = null_expansion_2var_order2.copy()
  498. coeff = [2., 1., -3., -4., 0., 7.]
  499. expansion.coeff = coeff
  500. map_expansion = taylor_map.RealTaylorMap([expansion1, expansion2])
  501. divided_map = map_expansion / expansion
  502. for i, new_expansion in enumerate(divided_map):
  503. if new_expansion != map_expansion[i] / expansion:
  504. self.fail()
  505. def test_dot_map_float(self):
  506. expansion1 = null_expansion_2var_order2.copy()
  507. coeff1 = [2., 3., 4., -5., -1., -3.]
  508. expansion1.coeff = coeff1
  509. expansion2 = null_expansion_2var_order2.copy()
  510. coeff2 = [-2., 6., 3., -2., 1., -1.]
  511. expansion2.coeff = coeff2
  512. map_expansion = taylor_map.RealTaylorMap([expansion1, expansion2])
  513. dotted = np.array([2., -1.])
  514. if map_expansion.dot(dotted) != dotted[0] * expansion1 + dotted[1] * expansion2:
  515. return self.fail()
  516. def test_dot_map_pow2(self):
  517. expansion1 = null_expansion_2var_order2.copy()
  518. coeff1 = [2., 3., 4., -5., -1., -3.]
  519. expansion1.coeff = coeff1
  520. expansion2 = null_expansion_2var_order2.copy()
  521. coeff2 = [-2., 6., 3., -2., 1., -1.]
  522. expansion2.coeff = coeff2
  523. map_expansion = taylor_map.RealTaylorMap([expansion1, expansion2])
  524. if map_expansion.dot(map_expansion) != expansion1**2 + expansion2**2:
  525. return self.fail()
  526. if __name__ == '__main__':
  527. unittest.main()