test_differentiation.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 TestDifferentiation(unittest.TestCase):
  10. def test_deriv_wrt_map(self):
  11. expansion1 = null_expansion_2var_order3.copy()
  12. coeff1 = [0., 1., 4., -5., 3.5, 1., -2., 3., 0., 7.]
  13. expansion1.coeff = coeff1
  14. expansion2 = null_expansion_2var_order3.copy()
  15. coeff2 = [0., -3., 0., 6., -1.5, 2., 3., -4., 8., 1.]
  16. expansion2.coeff = coeff2
  17. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  18. if map1.deriv_wrt_var(1, 2) != taylor_map.RealTaylorMap([expansion1.deriv_wrt_var(1, 2),
  19. expansion2.deriv_wrt_var(1, 2)]):
  20. self.fail()
  21. def test_integ_wrt_map(self):
  22. expansion1 = null_expansion_2var_order3.copy()
  23. coeff1 = [0., 1., 4., -5., 3.5, 1., -2., 3., 0., 7.]
  24. expansion1.coeff = coeff1
  25. expansion2 = null_expansion_2var_order3.copy()
  26. coeff2 = [0., -3., 0., 6., -1.5, 2., 3., -4., 8., 1.]
  27. expansion2.coeff = coeff2
  28. map1 = taylor_map.RealTaylorMap([expansion1, expansion2])
  29. if map1.integ_wrt_var(1, 2) != taylor_map.RealTaylorMap([expansion1.integ_wrt_var(1, 2),
  30. expansion2.integ_wrt_var(1, 2)]):
  31. self.fail()
  32. def test_integ_deriv_integ_univariate(self):
  33. expansion1 = factory_taylor.zero_expansion(1, 5)
  34. coeff1 = [0., 1.5, -4., 2., -1., 3.]
  35. expansion1.coeff = coeff1
  36. deriv = expansion1.deriv_once_wrt_var()
  37. same = deriv.integ_once_wrt_var()
  38. if same != expansion1:
  39. self.fail()
  40. def test_rigorous_integ_univariate(self):
  41. expansion1 = factory_taylor.zero_expansion(1, 5)
  42. coeff1 = [0., 1.5, -4., 2., -1., 3.]
  43. expansion1.coeff = coeff1
  44. expansion2 = expansion1.rigorous_integ_once_wrt_var()
  45. if expansion2 != expansion1.prolong_one_order().integ_once_wrt_var():
  46. self.fail()
  47. if expansion2.truncated(expansion1.order) != expansion1.integ_once_wrt_var():
  48. self.fail()
  49. def test_rigorous_integ_univariate_map(self):
  50. expansion1 = factory_taylor.zero_expansion(1, 5)
  51. coeff1 = [0., 1.5, -4., 2., -1., 3.]
  52. expansion1.coeff = coeff1
  53. expansion2 = factory_taylor.zero_expansion(1, 5)
  54. coeff2 = [3., 1.5, -4., 2, -1., 6.]
  55. expansion2.coeff = coeff2
  56. map_expans = taylor_map.RealTaylorMap([expansion1, expansion2])
  57. map_integ = taylor_map.RealTaylorMap([expansion1.rigorous_integ_once_wrt_var(),
  58. expansion2.rigorous_integ_once_wrt_var()])
  59. if map_expans.rigorous_integ_once_wrt_var(0) != map_integ:
  60. self.fail()
  61. def test_rigorous_integ_bivariate(self):
  62. expansion1 = null_expansion_2var_order3.copy()
  63. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  64. expansion1.coeff = coeff1
  65. expansion2 = expansion1.rigorous_integ_once_wrt_var(1)
  66. if expansion2 != expansion1.prolong_one_order().integ_once_wrt_var(1):
  67. self.fail()
  68. if expansion2.truncated(expansion1.order) != expansion1.integ_once_wrt_var(1):
  69. self.fail()
  70. def test_rigorous_integ_bivariate_map(self):
  71. expansion1 = null_expansion_2var_order3.copy()
  72. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  73. expansion1.coeff = coeff1
  74. expansion2 = null_expansion_2var_order3.copy()
  75. coeff2 = [0., -1., 2., -2., 4., 5., 2., -1., -7., 8.]
  76. expansion2.coeff = coeff2
  77. map_expans = taylor_map.RealTaylorMap([expansion1, expansion2])
  78. map_integ = taylor_map.RealTaylorMap([expansion1.rigorous_integ_once_wrt_var(0),
  79. expansion2.rigorous_integ_once_wrt_var(0)])
  80. if map_expans.rigorous_integ_once_wrt_var(0) != map_integ:
  81. self.fail()
  82. def test_deriv_bivariate(self):
  83. expansion = null_expansion_2var_order3.copy()
  84. coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  85. expansion.coeff = coeff
  86. deriv1 = expansion.deriv_once_wrt_var(0)
  87. coeff1 = deriv1.coeff
  88. deriv2 = expansion.deriv_once_wrt_var(1)
  89. coeff2 = deriv2.coeff
  90. if not np.array_equal(coeff1[:6], [3., -10., -1., -6., 2., 6.]):
  91. self.fail()
  92. if not np.array_equal(coeff2[:6], [4., -1., -6., 1., 12., -21.]):
  93. self.fail()
  94. if not np.array_equal(coeff1[6:], np.zeros(deriv1.dim_alg - 6)):
  95. self.fail()
  96. if not np.array_equal(coeff2[6:], np.zeros(deriv1.dim_alg - 6)):
  97. self.fail()
  98. def test_deriv_trivariate(self):
  99. expansion = null_expansion_3var_order2.copy()
  100. coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  101. expansion.coeff = coeff
  102. deriv1 = expansion.deriv_once_wrt_var(0)
  103. coeff1 = deriv1.coeff
  104. if not np.array_equal(coeff1[:4], [3., -2., -3., -2.]):
  105. self.fail()
  106. if not np.array_equal(coeff1[4:], np.zeros(deriv1.dim_alg - 4)):
  107. self.fail()
  108. def test_integ_bivariate(self):
  109. expansion = null_expansion_2var_order3.copy()
  110. coeff = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  111. expansion.coeff = coeff
  112. integ1 = expansion.integ_once_wrt_var(0)
  113. coeff1 = integ1.coeff
  114. integ2 = expansion.integ_once_wrt_var(1)
  115. coeff2 = integ2.coeff
  116. if not np.array_equal(coeff1[:10], [0., 2., 0., 1.5, 4., 0., -5./3., -0.5, -3., 0.]):
  117. self.fail()
  118. if not np.array_equal(coeff2[:10], [0., 0., 2., 0., 3., 2., 0., -5., -0.5, -1.]):
  119. self.fail()
  120. def test_integ_trivariate(self):
  121. expansion1 = null_expansion_3var_order2.copy()
  122. coeff1 = [2., 3., 4., -5., -1., -3., -2., 1., 6., -7.]
  123. expansion1.coeff = coeff1
  124. expansion2 = null_expansion_3var_order2.copy()
  125. coeff2 = [1., -4., 0., 5., 6., 8., -3., 2., 9., -1.]
  126. expansion2.coeff = coeff2
  127. integ1 = expansion1.integ_once_wrt_var(0)
  128. integ2 = expansion2.integ_once_wrt_var(0)
  129. integ_map = taylor_map.RealTaylorMap([expansion1, expansion2]).integ_once_wrt_var(0)
  130. if integ1 != integ_map[0] or integ2 != integ_map[1]:
  131. self.fail()
  132. def test_schwartz_bivariate(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. deriv12 = expansion1.deriv_once_wrt_var(0).deriv_once_wrt_var(1)
  137. deriv21 = expansion1.deriv_once_wrt_var(1).deriv_once_wrt_var(0)
  138. if deriv12 != deriv21:
  139. self.fail()
  140. if __name__ == '__main__':
  141. unittest.main()