test_interpolate.py 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  1. from numpy.testing import (assert_, assert_equal, assert_almost_equal,
  2. assert_array_almost_equal, assert_array_equal,
  3. assert_allclose, suppress_warnings)
  4. from pytest import raises as assert_raises
  5. import pytest
  6. from numpy import mgrid, pi, sin, ogrid, poly1d, linspace
  7. import numpy as np
  8. from scipy.interpolate import (interp1d, interp2d, lagrange, PPoly, BPoly,
  9. splrep, splev, splantider, splint, sproot, Akima1DInterpolator,
  10. NdPPoly, BSpline)
  11. from scipy.special import poch, gamma
  12. from scipy.interpolate import _ppoly
  13. from scipy._lib._gcutils import assert_deallocated, IS_PYPY
  14. from scipy.integrate import nquad
  15. from scipy.special import binom
  16. class TestInterp2D:
  17. def test_interp2d(self):
  18. y, x = mgrid[0:2:20j, 0:pi:21j]
  19. z = sin(x+0.5*y)
  20. with suppress_warnings() as sup:
  21. sup.filter(DeprecationWarning)
  22. II = interp2d(x, y, z)
  23. assert_almost_equal(II(1.0, 2.0), sin(2.0), decimal=2)
  24. v, u = ogrid[0:2:24j, 0:pi:25j]
  25. assert_almost_equal(II(u.ravel(), v.ravel()),
  26. sin(u+0.5*v), decimal=2)
  27. def test_interp2d_meshgrid_input(self):
  28. # Ticket #703
  29. x = linspace(0, 2, 16)
  30. y = linspace(0, pi, 21)
  31. z = sin(x[None, :] + y[:, None]/2.)
  32. with suppress_warnings() as sup:
  33. sup.filter(DeprecationWarning)
  34. II = interp2d(x, y, z)
  35. assert_almost_equal(II(1.0, 2.0), sin(2.0), decimal=2)
  36. def test_interp2d_meshgrid_input_unsorted(self):
  37. np.random.seed(1234)
  38. x = linspace(0, 2, 16)
  39. y = linspace(0, pi, 21)
  40. z = sin(x[None, :] + y[:, None] / 2.)
  41. with suppress_warnings() as sup:
  42. sup.filter(DeprecationWarning)
  43. ip1 = interp2d(x.copy(), y.copy(), z, kind='cubic')
  44. np.random.shuffle(x)
  45. z = sin(x[None, :] + y[:, None]/2.)
  46. ip2 = interp2d(x.copy(), y.copy(), z, kind='cubic')
  47. np.random.shuffle(x)
  48. np.random.shuffle(y)
  49. z = sin(x[None, :] + y[:, None] / 2.)
  50. ip3 = interp2d(x, y, z, kind='cubic')
  51. x = linspace(0, 2, 31)
  52. y = linspace(0, pi, 30)
  53. assert_equal(ip1(x, y), ip2(x, y))
  54. assert_equal(ip1(x, y), ip3(x, y))
  55. def test_interp2d_eval_unsorted(self):
  56. y, x = mgrid[0:2:20j, 0:pi:21j]
  57. z = sin(x + 0.5*y)
  58. with suppress_warnings() as sup:
  59. sup.filter(DeprecationWarning)
  60. func = interp2d(x, y, z)
  61. xe = np.array([3, 4, 5])
  62. ye = np.array([5.3, 7.1])
  63. assert_allclose(func(xe, ye), func(xe, ye[::-1]))
  64. assert_raises(ValueError, func, xe, ye[::-1], 0, 0, True)
  65. def test_interp2d_linear(self):
  66. # Ticket #898
  67. a = np.zeros([5, 5])
  68. a[2, 2] = 1.0
  69. x = y = np.arange(5)
  70. with suppress_warnings() as sup:
  71. sup.filter(DeprecationWarning)
  72. b = interp2d(x, y, a, 'linear')
  73. assert_almost_equal(b(2.0, 1.5), np.array([0.5]), decimal=2)
  74. assert_almost_equal(b(2.0, 2.5), np.array([0.5]), decimal=2)
  75. def test_interp2d_bounds(self):
  76. x = np.linspace(0, 1, 5)
  77. y = np.linspace(0, 2, 7)
  78. z = x[None, :]**2 + y[:, None]
  79. ix = np.linspace(-1, 3, 31)
  80. iy = np.linspace(-1, 3, 33)
  81. with suppress_warnings() as sup:
  82. sup.filter(DeprecationWarning)
  83. b = interp2d(x, y, z, bounds_error=True)
  84. assert_raises(ValueError, b, ix, iy)
  85. b = interp2d(x, y, z, fill_value=np.nan)
  86. iz = b(ix, iy)
  87. mx = (ix < 0) | (ix > 1)
  88. my = (iy < 0) | (iy > 2)
  89. assert_(np.isnan(iz[my, :]).all())
  90. assert_(np.isnan(iz[:, mx]).all())
  91. assert_(np.isfinite(iz[~my, :][:, ~mx]).all())
  92. class TestInterp1D:
  93. def setup_method(self):
  94. self.x5 = np.arange(5.)
  95. self.x10 = np.arange(10.)
  96. self.y10 = np.arange(10.)
  97. self.x25 = self.x10.reshape((2,5))
  98. self.x2 = np.arange(2.)
  99. self.y2 = np.arange(2.)
  100. self.x1 = np.array([0.])
  101. self.y1 = np.array([0.])
  102. self.y210 = np.arange(20.).reshape((2, 10))
  103. self.y102 = np.arange(20.).reshape((10, 2))
  104. self.y225 = np.arange(20.).reshape((2, 2, 5))
  105. self.y25 = np.arange(10.).reshape((2, 5))
  106. self.y235 = np.arange(30.).reshape((2, 3, 5))
  107. self.y325 = np.arange(30.).reshape((3, 2, 5))
  108. # Edge updated test matrix 1
  109. # array([[ 30, 1, 2, 3, 4, 5, 6, 7, 8, -30],
  110. # [ 30, 11, 12, 13, 14, 15, 16, 17, 18, -30]])
  111. self.y210_edge_updated = np.arange(20.).reshape((2, 10))
  112. self.y210_edge_updated[:, 0] = 30
  113. self.y210_edge_updated[:, -1] = -30
  114. # Edge updated test matrix 2
  115. # array([[ 30, 30],
  116. # [ 2, 3],
  117. # [ 4, 5],
  118. # [ 6, 7],
  119. # [ 8, 9],
  120. # [ 10, 11],
  121. # [ 12, 13],
  122. # [ 14, 15],
  123. # [ 16, 17],
  124. # [-30, -30]])
  125. self.y102_edge_updated = np.arange(20.).reshape((10, 2))
  126. self.y102_edge_updated[0, :] = 30
  127. self.y102_edge_updated[-1, :] = -30
  128. self.fill_value = -100.0
  129. def test_validation(self):
  130. # Make sure that appropriate exceptions are raised when invalid values
  131. # are given to the constructor.
  132. # These should all work.
  133. for kind in ('nearest', 'nearest-up', 'zero', 'linear', 'slinear',
  134. 'quadratic', 'cubic', 'previous', 'next'):
  135. interp1d(self.x10, self.y10, kind=kind)
  136. interp1d(self.x10, self.y10, kind=kind, fill_value="extrapolate")
  137. interp1d(self.x10, self.y10, kind='linear', fill_value=(-1, 1))
  138. interp1d(self.x10, self.y10, kind='linear',
  139. fill_value=np.array([-1]))
  140. interp1d(self.x10, self.y10, kind='linear',
  141. fill_value=(-1,))
  142. interp1d(self.x10, self.y10, kind='linear',
  143. fill_value=-1)
  144. interp1d(self.x10, self.y10, kind='linear',
  145. fill_value=(-1, -1))
  146. interp1d(self.x10, self.y10, kind=0)
  147. interp1d(self.x10, self.y10, kind=1)
  148. interp1d(self.x10, self.y10, kind=2)
  149. interp1d(self.x10, self.y10, kind=3)
  150. interp1d(self.x10, self.y210, kind='linear', axis=-1,
  151. fill_value=(-1, -1))
  152. interp1d(self.x2, self.y210, kind='linear', axis=0,
  153. fill_value=np.ones(10))
  154. interp1d(self.x2, self.y210, kind='linear', axis=0,
  155. fill_value=(np.ones(10), np.ones(10)))
  156. interp1d(self.x2, self.y210, kind='linear', axis=0,
  157. fill_value=(np.ones(10), -1))
  158. # x array must be 1D.
  159. assert_raises(ValueError, interp1d, self.x25, self.y10)
  160. # y array cannot be a scalar.
  161. assert_raises(ValueError, interp1d, self.x10, np.array(0))
  162. # Check for x and y arrays having the same length.
  163. assert_raises(ValueError, interp1d, self.x10, self.y2)
  164. assert_raises(ValueError, interp1d, self.x2, self.y10)
  165. assert_raises(ValueError, interp1d, self.x10, self.y102)
  166. interp1d(self.x10, self.y210)
  167. interp1d(self.x10, self.y102, axis=0)
  168. # Check for x and y having at least 1 element.
  169. assert_raises(ValueError, interp1d, self.x1, self.y10)
  170. assert_raises(ValueError, interp1d, self.x10, self.y1)
  171. # Bad fill values
  172. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  173. fill_value=(-1, -1, -1)) # doesn't broadcast
  174. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  175. fill_value=[-1, -1, -1]) # doesn't broadcast
  176. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  177. fill_value=np.array((-1, -1, -1))) # doesn't broadcast
  178. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  179. fill_value=[[-1]]) # doesn't broadcast
  180. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  181. fill_value=[-1, -1]) # doesn't broadcast
  182. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  183. fill_value=np.array([])) # doesn't broadcast
  184. assert_raises(ValueError, interp1d, self.x10, self.y10, kind='linear',
  185. fill_value=()) # doesn't broadcast
  186. assert_raises(ValueError, interp1d, self.x2, self.y210, kind='linear',
  187. axis=0, fill_value=[-1, -1]) # doesn't broadcast
  188. assert_raises(ValueError, interp1d, self.x2, self.y210, kind='linear',
  189. axis=0, fill_value=(0., [-1, -1])) # above doesn't bc
  190. def test_init(self):
  191. # Check that the attributes are initialized appropriately by the
  192. # constructor.
  193. assert_(interp1d(self.x10, self.y10).copy)
  194. assert_(not interp1d(self.x10, self.y10, copy=False).copy)
  195. assert_(interp1d(self.x10, self.y10).bounds_error)
  196. assert_(not interp1d(self.x10, self.y10, bounds_error=False).bounds_error)
  197. assert_(np.isnan(interp1d(self.x10, self.y10).fill_value))
  198. assert_equal(interp1d(self.x10, self.y10, fill_value=3.0).fill_value,
  199. 3.0)
  200. assert_equal(interp1d(self.x10, self.y10, fill_value=(1.0, 2.0)).fill_value,
  201. (1.0, 2.0))
  202. assert_equal(interp1d(self.x10, self.y10).axis, 0)
  203. assert_equal(interp1d(self.x10, self.y210).axis, 1)
  204. assert_equal(interp1d(self.x10, self.y102, axis=0).axis, 0)
  205. assert_array_equal(interp1d(self.x10, self.y10).x, self.x10)
  206. assert_array_equal(interp1d(self.x10, self.y10).y, self.y10)
  207. assert_array_equal(interp1d(self.x10, self.y210).y, self.y210)
  208. def test_assume_sorted(self):
  209. # Check for unsorted arrays
  210. interp10 = interp1d(self.x10, self.y10)
  211. interp10_unsorted = interp1d(self.x10[::-1], self.y10[::-1])
  212. assert_array_almost_equal(interp10_unsorted(self.x10), self.y10)
  213. assert_array_almost_equal(interp10_unsorted(1.2), np.array([1.2]))
  214. assert_array_almost_equal(interp10_unsorted([2.4, 5.6, 6.0]),
  215. interp10([2.4, 5.6, 6.0]))
  216. # Check assume_sorted keyword (defaults to False)
  217. interp10_assume_kw = interp1d(self.x10[::-1], self.y10[::-1],
  218. assume_sorted=False)
  219. assert_array_almost_equal(interp10_assume_kw(self.x10), self.y10)
  220. interp10_assume_kw2 = interp1d(self.x10[::-1], self.y10[::-1],
  221. assume_sorted=True)
  222. # Should raise an error for unsorted input if assume_sorted=True
  223. assert_raises(ValueError, interp10_assume_kw2, self.x10)
  224. # Check that if y is a 2-D array, things are still consistent
  225. interp10_y_2d = interp1d(self.x10, self.y210)
  226. interp10_y_2d_unsorted = interp1d(self.x10[::-1], self.y210[:, ::-1])
  227. assert_array_almost_equal(interp10_y_2d(self.x10),
  228. interp10_y_2d_unsorted(self.x10))
  229. def test_linear(self):
  230. for kind in ['linear', 'slinear']:
  231. self._check_linear(kind)
  232. def _check_linear(self, kind):
  233. # Check the actual implementation of linear interpolation.
  234. interp10 = interp1d(self.x10, self.y10, kind=kind)
  235. assert_array_almost_equal(interp10(self.x10), self.y10)
  236. assert_array_almost_equal(interp10(1.2), np.array([1.2]))
  237. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  238. np.array([2.4, 5.6, 6.0]))
  239. # test fill_value="extrapolate"
  240. extrapolator = interp1d(self.x10, self.y10, kind=kind,
  241. fill_value='extrapolate')
  242. assert_allclose(extrapolator([-1., 0, 9, 11]),
  243. [-1, 0, 9, 11], rtol=1e-14)
  244. opts = dict(kind=kind,
  245. fill_value='extrapolate',
  246. bounds_error=True)
  247. assert_raises(ValueError, interp1d, self.x10, self.y10, **opts)
  248. def test_linear_dtypes(self):
  249. # regression test for gh-5898, where 1D linear interpolation has been
  250. # delegated to numpy.interp for all float dtypes, and the latter was
  251. # not handling e.g. np.float128.
  252. for dtyp in np.sctypes["float"]:
  253. x = np.arange(8, dtype=dtyp)
  254. y = x
  255. yp = interp1d(x, y, kind='linear')(x)
  256. assert_equal(yp.dtype, dtyp)
  257. assert_allclose(yp, y, atol=1e-15)
  258. # regression test for gh-14531, where 1D linear interpolation has been
  259. # has been extended to delegate to numpy.interp for integer dtypes
  260. x = [0, 1, 2]
  261. y = [np.nan, 0, 1]
  262. yp = interp1d(x, y)(x)
  263. assert_allclose(yp, y, atol=1e-15)
  264. def test_slinear_dtypes(self):
  265. # regression test for gh-7273: 1D slinear interpolation fails with
  266. # float32 inputs
  267. dt_r = [np.float16, np.float32, np.float64]
  268. dt_rc = dt_r + [np.complex64, np.complex128]
  269. spline_kinds = ['slinear', 'zero', 'quadratic', 'cubic']
  270. for dtx in dt_r:
  271. x = np.arange(0, 10, dtype=dtx)
  272. for dty in dt_rc:
  273. y = np.exp(-x/3.0).astype(dty)
  274. for dtn in dt_r:
  275. xnew = x.astype(dtn)
  276. for kind in spline_kinds:
  277. f = interp1d(x, y, kind=kind, bounds_error=False)
  278. assert_allclose(f(xnew), y, atol=1e-7,
  279. err_msg="%s, %s %s" % (dtx, dty, dtn))
  280. def test_cubic(self):
  281. # Check the actual implementation of spline interpolation.
  282. interp10 = interp1d(self.x10, self.y10, kind='cubic')
  283. assert_array_almost_equal(interp10(self.x10), self.y10)
  284. assert_array_almost_equal(interp10(1.2), np.array([1.2]))
  285. assert_array_almost_equal(interp10(1.5), np.array([1.5]))
  286. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  287. np.array([2.4, 5.6, 6.0]),)
  288. def test_nearest(self):
  289. # Check the actual implementation of nearest-neighbour interpolation.
  290. # Nearest asserts that half-integer case (1.5) rounds down to 1
  291. interp10 = interp1d(self.x10, self.y10, kind='nearest')
  292. assert_array_almost_equal(interp10(self.x10), self.y10)
  293. assert_array_almost_equal(interp10(1.2), np.array(1.))
  294. assert_array_almost_equal(interp10(1.5), np.array(1.))
  295. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  296. np.array([2., 6., 6.]),)
  297. # test fill_value="extrapolate"
  298. extrapolator = interp1d(self.x10, self.y10, kind='nearest',
  299. fill_value='extrapolate')
  300. assert_allclose(extrapolator([-1., 0, 9, 11]),
  301. [0, 0, 9, 9], rtol=1e-14)
  302. opts = dict(kind='nearest',
  303. fill_value='extrapolate',
  304. bounds_error=True)
  305. assert_raises(ValueError, interp1d, self.x10, self.y10, **opts)
  306. def test_nearest_up(self):
  307. # Check the actual implementation of nearest-neighbour interpolation.
  308. # Nearest-up asserts that half-integer case (1.5) rounds up to 2
  309. interp10 = interp1d(self.x10, self.y10, kind='nearest-up')
  310. assert_array_almost_equal(interp10(self.x10), self.y10)
  311. assert_array_almost_equal(interp10(1.2), np.array(1.))
  312. assert_array_almost_equal(interp10(1.5), np.array(2.))
  313. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  314. np.array([2., 6., 6.]),)
  315. # test fill_value="extrapolate"
  316. extrapolator = interp1d(self.x10, self.y10, kind='nearest-up',
  317. fill_value='extrapolate')
  318. assert_allclose(extrapolator([-1., 0, 9, 11]),
  319. [0, 0, 9, 9], rtol=1e-14)
  320. opts = dict(kind='nearest-up',
  321. fill_value='extrapolate',
  322. bounds_error=True)
  323. assert_raises(ValueError, interp1d, self.x10, self.y10, **opts)
  324. def test_previous(self):
  325. # Check the actual implementation of previous interpolation.
  326. interp10 = interp1d(self.x10, self.y10, kind='previous')
  327. assert_array_almost_equal(interp10(self.x10), self.y10)
  328. assert_array_almost_equal(interp10(1.2), np.array(1.))
  329. assert_array_almost_equal(interp10(1.5), np.array(1.))
  330. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  331. np.array([2., 5., 6.]),)
  332. # test fill_value="extrapolate"
  333. extrapolator = interp1d(self.x10, self.y10, kind='previous',
  334. fill_value='extrapolate')
  335. assert_allclose(extrapolator([-1., 0, 9, 11]),
  336. [np.nan, 0, 9, 9], rtol=1e-14)
  337. # Tests for gh-9591
  338. interpolator1D = interp1d(self.x10, self.y10, kind="previous",
  339. fill_value='extrapolate')
  340. assert_allclose(interpolator1D([-1, -2, 5, 8, 12, 25]),
  341. [np.nan, np.nan, 5, 8, 9, 9])
  342. interpolator2D = interp1d(self.x10, self.y210, kind="previous",
  343. fill_value='extrapolate')
  344. assert_allclose(interpolator2D([-1, -2, 5, 8, 12, 25]),
  345. [[np.nan, np.nan, 5, 8, 9, 9],
  346. [np.nan, np.nan, 15, 18, 19, 19]])
  347. interpolator2DAxis0 = interp1d(self.x10, self.y102, kind="previous",
  348. axis=0, fill_value='extrapolate')
  349. assert_allclose(interpolator2DAxis0([-2, 5, 12]),
  350. [[np.nan, np.nan],
  351. [10, 11],
  352. [18, 19]])
  353. opts = dict(kind='previous',
  354. fill_value='extrapolate',
  355. bounds_error=True)
  356. assert_raises(ValueError, interp1d, self.x10, self.y10, **opts)
  357. # Tests for gh-16813
  358. interpolator1D = interp1d([0, 1, 2],
  359. [0, 1, -1], kind="previous",
  360. fill_value='extrapolate',
  361. assume_sorted=True)
  362. assert_allclose(interpolator1D([-2, -1, 0, 1, 2, 3, 5]),
  363. [np.nan, np.nan, 0, 1, -1, -1, -1])
  364. interpolator1D = interp1d([2, 0, 1], # x is not ascending
  365. [-1, 0, 1], kind="previous",
  366. fill_value='extrapolate',
  367. assume_sorted=False)
  368. assert_allclose(interpolator1D([-2, -1, 0, 1, 2, 3, 5]),
  369. [np.nan, np.nan, 0, 1, -1, -1, -1])
  370. interpolator2D = interp1d(self.x10, self.y210_edge_updated,
  371. kind="previous",
  372. fill_value='extrapolate')
  373. assert_allclose(interpolator2D([-1, -2, 5, 8, 12, 25]),
  374. [[np.nan, np.nan, 5, 8, -30, -30],
  375. [np.nan, np.nan, 15, 18, -30, -30]])
  376. interpolator2DAxis0 = interp1d(self.x10, self.y102_edge_updated,
  377. kind="previous",
  378. axis=0, fill_value='extrapolate')
  379. assert_allclose(interpolator2DAxis0([-2, 5, 12]),
  380. [[np.nan, np.nan],
  381. [10, 11],
  382. [-30, -30]])
  383. def test_next(self):
  384. # Check the actual implementation of next interpolation.
  385. interp10 = interp1d(self.x10, self.y10, kind='next')
  386. assert_array_almost_equal(interp10(self.x10), self.y10)
  387. assert_array_almost_equal(interp10(1.2), np.array(2.))
  388. assert_array_almost_equal(interp10(1.5), np.array(2.))
  389. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  390. np.array([3., 6., 6.]),)
  391. # test fill_value="extrapolate"
  392. extrapolator = interp1d(self.x10, self.y10, kind='next',
  393. fill_value='extrapolate')
  394. assert_allclose(extrapolator([-1., 0, 9, 11]),
  395. [0, 0, 9, np.nan], rtol=1e-14)
  396. # Tests for gh-9591
  397. interpolator1D = interp1d(self.x10, self.y10, kind="next",
  398. fill_value='extrapolate')
  399. assert_allclose(interpolator1D([-1, -2, 5, 8, 12, 25]),
  400. [0, 0, 5, 8, np.nan, np.nan])
  401. interpolator2D = interp1d(self.x10, self.y210, kind="next",
  402. fill_value='extrapolate')
  403. assert_allclose(interpolator2D([-1, -2, 5, 8, 12, 25]),
  404. [[0, 0, 5, 8, np.nan, np.nan],
  405. [10, 10, 15, 18, np.nan, np.nan]])
  406. interpolator2DAxis0 = interp1d(self.x10, self.y102, kind="next",
  407. axis=0, fill_value='extrapolate')
  408. assert_allclose(interpolator2DAxis0([-2, 5, 12]),
  409. [[0, 1],
  410. [10, 11],
  411. [np.nan, np.nan]])
  412. opts = dict(kind='next',
  413. fill_value='extrapolate',
  414. bounds_error=True)
  415. assert_raises(ValueError, interp1d, self.x10, self.y10, **opts)
  416. # Tests for gh-16813
  417. interpolator1D = interp1d([0, 1, 2],
  418. [0, 1, -1], kind="next",
  419. fill_value='extrapolate',
  420. assume_sorted=True)
  421. assert_allclose(interpolator1D([-2, -1, 0, 1, 2, 3, 5]),
  422. [0, 0, 0, 1, -1, np.nan, np.nan])
  423. interpolator1D = interp1d([2, 0, 1], # x is not ascending
  424. [-1, 0, 1], kind="next",
  425. fill_value='extrapolate',
  426. assume_sorted=False)
  427. assert_allclose(interpolator1D([-2, -1, 0, 1, 2, 3, 5]),
  428. [0, 0, 0, 1, -1, np.nan, np.nan])
  429. interpolator2D = interp1d(self.x10, self.y210_edge_updated,
  430. kind="next",
  431. fill_value='extrapolate')
  432. assert_allclose(interpolator2D([-1, -2, 5, 8, 12, 25]),
  433. [[30, 30, 5, 8, np.nan, np.nan],
  434. [30, 30, 15, 18, np.nan, np.nan]])
  435. interpolator2DAxis0 = interp1d(self.x10, self.y102_edge_updated,
  436. kind="next",
  437. axis=0, fill_value='extrapolate')
  438. assert_allclose(interpolator2DAxis0([-2, 5, 12]),
  439. [[30, 30],
  440. [10, 11],
  441. [np.nan, np.nan]])
  442. def test_zero(self):
  443. # Check the actual implementation of zero-order spline interpolation.
  444. interp10 = interp1d(self.x10, self.y10, kind='zero')
  445. assert_array_almost_equal(interp10(self.x10), self.y10)
  446. assert_array_almost_equal(interp10(1.2), np.array(1.))
  447. assert_array_almost_equal(interp10(1.5), np.array(1.))
  448. assert_array_almost_equal(interp10([2.4, 5.6, 6.0]),
  449. np.array([2., 5., 6.]))
  450. def bounds_check_helper(self, interpolant, test_array, fail_value):
  451. # Asserts that a ValueError is raised and that the error message
  452. # contains the value causing this exception.
  453. assert_raises(ValueError, interpolant, test_array)
  454. try:
  455. interpolant(test_array)
  456. except ValueError as err:
  457. assert ("{}".format(fail_value) in str(err))
  458. def _bounds_check(self, kind='linear'):
  459. # Test that our handling of out-of-bounds input is correct.
  460. extrap10 = interp1d(self.x10, self.y10, fill_value=self.fill_value,
  461. bounds_error=False, kind=kind)
  462. assert_array_equal(extrap10(11.2), np.array(self.fill_value))
  463. assert_array_equal(extrap10(-3.4), np.array(self.fill_value))
  464. assert_array_equal(extrap10([[[11.2], [-3.4], [12.6], [19.3]]]),
  465. np.array(self.fill_value),)
  466. assert_array_equal(extrap10._check_bounds(
  467. np.array([-1.0, 0.0, 5.0, 9.0, 11.0])),
  468. np.array([[True, False, False, False, False],
  469. [False, False, False, False, True]]))
  470. raises_bounds_error = interp1d(self.x10, self.y10, bounds_error=True,
  471. kind=kind)
  472. self.bounds_check_helper(raises_bounds_error, -1.0, -1.0)
  473. self.bounds_check_helper(raises_bounds_error, 11.0, 11.0)
  474. self.bounds_check_helper(raises_bounds_error, [0.0, -1.0, 0.0], -1.0)
  475. self.bounds_check_helper(raises_bounds_error, [0.0, 1.0, 21.0], 21.0)
  476. raises_bounds_error([0.0, 5.0, 9.0])
  477. def _bounds_check_int_nan_fill(self, kind='linear'):
  478. x = np.arange(10).astype(np.int_)
  479. y = np.arange(10).astype(np.int_)
  480. c = interp1d(x, y, kind=kind, fill_value=np.nan, bounds_error=False)
  481. yi = c(x - 1)
  482. assert_(np.isnan(yi[0]))
  483. assert_array_almost_equal(yi, np.r_[np.nan, y[:-1]])
  484. def test_bounds(self):
  485. for kind in ('linear', 'cubic', 'nearest', 'previous', 'next',
  486. 'slinear', 'zero', 'quadratic'):
  487. self._bounds_check(kind)
  488. self._bounds_check_int_nan_fill(kind)
  489. def _check_fill_value(self, kind):
  490. interp = interp1d(self.x10, self.y10, kind=kind,
  491. fill_value=(-100, 100), bounds_error=False)
  492. assert_array_almost_equal(interp(10), 100)
  493. assert_array_almost_equal(interp(-10), -100)
  494. assert_array_almost_equal(interp([-10, 10]), [-100, 100])
  495. # Proper broadcasting:
  496. # interp along axis of length 5
  497. # other dim=(2, 3), (3, 2), (2, 2), or (2,)
  498. # one singleton fill_value (works for all)
  499. for y in (self.y235, self.y325, self.y225, self.y25):
  500. interp = interp1d(self.x5, y, kind=kind, axis=-1,
  501. fill_value=100, bounds_error=False)
  502. assert_array_almost_equal(interp(10), 100)
  503. assert_array_almost_equal(interp(-10), 100)
  504. assert_array_almost_equal(interp([-10, 10]), 100)
  505. # singleton lower, singleton upper
  506. interp = interp1d(self.x5, y, kind=kind, axis=-1,
  507. fill_value=(-100, 100), bounds_error=False)
  508. assert_array_almost_equal(interp(10), 100)
  509. assert_array_almost_equal(interp(-10), -100)
  510. if y.ndim == 3:
  511. result = [[[-100, 100]] * y.shape[1]] * y.shape[0]
  512. else:
  513. result = [[-100, 100]] * y.shape[0]
  514. assert_array_almost_equal(interp([-10, 10]), result)
  515. # one broadcastable (3,) fill_value
  516. fill_value = [100, 200, 300]
  517. for y in (self.y325, self.y225):
  518. assert_raises(ValueError, interp1d, self.x5, y, kind=kind,
  519. axis=-1, fill_value=fill_value, bounds_error=False)
  520. interp = interp1d(self.x5, self.y235, kind=kind, axis=-1,
  521. fill_value=fill_value, bounds_error=False)
  522. assert_array_almost_equal(interp(10), [[100, 200, 300]] * 2)
  523. assert_array_almost_equal(interp(-10), [[100, 200, 300]] * 2)
  524. assert_array_almost_equal(interp([-10, 10]), [[[100, 100],
  525. [200, 200],
  526. [300, 300]]] * 2)
  527. # one broadcastable (2,) fill_value
  528. fill_value = [100, 200]
  529. assert_raises(ValueError, interp1d, self.x5, self.y235, kind=kind,
  530. axis=-1, fill_value=fill_value, bounds_error=False)
  531. for y in (self.y225, self.y325, self.y25):
  532. interp = interp1d(self.x5, y, kind=kind, axis=-1,
  533. fill_value=fill_value, bounds_error=False)
  534. result = [100, 200]
  535. if y.ndim == 3:
  536. result = [result] * y.shape[0]
  537. assert_array_almost_equal(interp(10), result)
  538. assert_array_almost_equal(interp(-10), result)
  539. result = [[100, 100], [200, 200]]
  540. if y.ndim == 3:
  541. result = [result] * y.shape[0]
  542. assert_array_almost_equal(interp([-10, 10]), result)
  543. # broadcastable (3,) lower, singleton upper
  544. fill_value = (np.array([-100, -200, -300]), 100)
  545. for y in (self.y325, self.y225):
  546. assert_raises(ValueError, interp1d, self.x5, y, kind=kind,
  547. axis=-1, fill_value=fill_value, bounds_error=False)
  548. interp = interp1d(self.x5, self.y235, kind=kind, axis=-1,
  549. fill_value=fill_value, bounds_error=False)
  550. assert_array_almost_equal(interp(10), 100)
  551. assert_array_almost_equal(interp(-10), [[-100, -200, -300]] * 2)
  552. assert_array_almost_equal(interp([-10, 10]), [[[-100, 100],
  553. [-200, 100],
  554. [-300, 100]]] * 2)
  555. # broadcastable (2,) lower, singleton upper
  556. fill_value = (np.array([-100, -200]), 100)
  557. assert_raises(ValueError, interp1d, self.x5, self.y235, kind=kind,
  558. axis=-1, fill_value=fill_value, bounds_error=False)
  559. for y in (self.y225, self.y325, self.y25):
  560. interp = interp1d(self.x5, y, kind=kind, axis=-1,
  561. fill_value=fill_value, bounds_error=False)
  562. assert_array_almost_equal(interp(10), 100)
  563. result = [-100, -200]
  564. if y.ndim == 3:
  565. result = [result] * y.shape[0]
  566. assert_array_almost_equal(interp(-10), result)
  567. result = [[-100, 100], [-200, 100]]
  568. if y.ndim == 3:
  569. result = [result] * y.shape[0]
  570. assert_array_almost_equal(interp([-10, 10]), result)
  571. # broadcastable (3,) lower, broadcastable (3,) upper
  572. fill_value = ([-100, -200, -300], [100, 200, 300])
  573. for y in (self.y325, self.y225):
  574. assert_raises(ValueError, interp1d, self.x5, y, kind=kind,
  575. axis=-1, fill_value=fill_value, bounds_error=False)
  576. for ii in range(2): # check ndarray as well as list here
  577. if ii == 1:
  578. fill_value = tuple(np.array(f) for f in fill_value)
  579. interp = interp1d(self.x5, self.y235, kind=kind, axis=-1,
  580. fill_value=fill_value, bounds_error=False)
  581. assert_array_almost_equal(interp(10), [[100, 200, 300]] * 2)
  582. assert_array_almost_equal(interp(-10), [[-100, -200, -300]] * 2)
  583. assert_array_almost_equal(interp([-10, 10]), [[[-100, 100],
  584. [-200, 200],
  585. [-300, 300]]] * 2)
  586. # broadcastable (2,) lower, broadcastable (2,) upper
  587. fill_value = ([-100, -200], [100, 200])
  588. assert_raises(ValueError, interp1d, self.x5, self.y235, kind=kind,
  589. axis=-1, fill_value=fill_value, bounds_error=False)
  590. for y in (self.y325, self.y225, self.y25):
  591. interp = interp1d(self.x5, y, kind=kind, axis=-1,
  592. fill_value=fill_value, bounds_error=False)
  593. result = [100, 200]
  594. if y.ndim == 3:
  595. result = [result] * y.shape[0]
  596. assert_array_almost_equal(interp(10), result)
  597. result = [-100, -200]
  598. if y.ndim == 3:
  599. result = [result] * y.shape[0]
  600. assert_array_almost_equal(interp(-10), result)
  601. result = [[-100, 100], [-200, 200]]
  602. if y.ndim == 3:
  603. result = [result] * y.shape[0]
  604. assert_array_almost_equal(interp([-10, 10]), result)
  605. # one broadcastable (2, 2) array-like
  606. fill_value = [[100, 200], [1000, 2000]]
  607. for y in (self.y235, self.y325, self.y25):
  608. assert_raises(ValueError, interp1d, self.x5, y, kind=kind,
  609. axis=-1, fill_value=fill_value, bounds_error=False)
  610. for ii in range(2):
  611. if ii == 1:
  612. fill_value = np.array(fill_value)
  613. interp = interp1d(self.x5, self.y225, kind=kind, axis=-1,
  614. fill_value=fill_value, bounds_error=False)
  615. assert_array_almost_equal(interp(10), [[100, 200], [1000, 2000]])
  616. assert_array_almost_equal(interp(-10), [[100, 200], [1000, 2000]])
  617. assert_array_almost_equal(interp([-10, 10]), [[[100, 100],
  618. [200, 200]],
  619. [[1000, 1000],
  620. [2000, 2000]]])
  621. # broadcastable (2, 2) lower, broadcastable (2, 2) upper
  622. fill_value = ([[-100, -200], [-1000, -2000]],
  623. [[100, 200], [1000, 2000]])
  624. for y in (self.y235, self.y325, self.y25):
  625. assert_raises(ValueError, interp1d, self.x5, y, kind=kind,
  626. axis=-1, fill_value=fill_value, bounds_error=False)
  627. for ii in range(2):
  628. if ii == 1:
  629. fill_value = (np.array(fill_value[0]), np.array(fill_value[1]))
  630. interp = interp1d(self.x5, self.y225, kind=kind, axis=-1,
  631. fill_value=fill_value, bounds_error=False)
  632. assert_array_almost_equal(interp(10), [[100, 200], [1000, 2000]])
  633. assert_array_almost_equal(interp(-10), [[-100, -200],
  634. [-1000, -2000]])
  635. assert_array_almost_equal(interp([-10, 10]), [[[-100, 100],
  636. [-200, 200]],
  637. [[-1000, 1000],
  638. [-2000, 2000]]])
  639. def test_fill_value(self):
  640. # test that two-element fill value works
  641. for kind in ('linear', 'nearest', 'cubic', 'slinear', 'quadratic',
  642. 'zero', 'previous', 'next'):
  643. self._check_fill_value(kind)
  644. def test_fill_value_writeable(self):
  645. # backwards compat: fill_value is a public writeable attribute
  646. interp = interp1d(self.x10, self.y10, fill_value=123.0)
  647. assert_equal(interp.fill_value, 123.0)
  648. interp.fill_value = 321.0
  649. assert_equal(interp.fill_value, 321.0)
  650. def _nd_check_interp(self, kind='linear'):
  651. # Check the behavior when the inputs and outputs are multidimensional.
  652. # Multidimensional input.
  653. interp10 = interp1d(self.x10, self.y10, kind=kind)
  654. assert_array_almost_equal(interp10(np.array([[3., 5.], [2., 7.]])),
  655. np.array([[3., 5.], [2., 7.]]))
  656. # Scalar input -> 0-dim scalar array output
  657. assert_(isinstance(interp10(1.2), np.ndarray))
  658. assert_equal(interp10(1.2).shape, ())
  659. # Multidimensional outputs.
  660. interp210 = interp1d(self.x10, self.y210, kind=kind)
  661. assert_array_almost_equal(interp210(1.), np.array([1., 11.]))
  662. assert_array_almost_equal(interp210(np.array([1., 2.])),
  663. np.array([[1., 2.], [11., 12.]]))
  664. interp102 = interp1d(self.x10, self.y102, axis=0, kind=kind)
  665. assert_array_almost_equal(interp102(1.), np.array([2.0, 3.0]))
  666. assert_array_almost_equal(interp102(np.array([1., 3.])),
  667. np.array([[2., 3.], [6., 7.]]))
  668. # Both at the same time!
  669. x_new = np.array([[3., 5.], [2., 7.]])
  670. assert_array_almost_equal(interp210(x_new),
  671. np.array([[[3., 5.], [2., 7.]],
  672. [[13., 15.], [12., 17.]]]))
  673. assert_array_almost_equal(interp102(x_new),
  674. np.array([[[6., 7.], [10., 11.]],
  675. [[4., 5.], [14., 15.]]]))
  676. def _nd_check_shape(self, kind='linear'):
  677. # Check large N-D output shape
  678. a = [4, 5, 6, 7]
  679. y = np.arange(np.prod(a)).reshape(*a)
  680. for n, s in enumerate(a):
  681. x = np.arange(s)
  682. z = interp1d(x, y, axis=n, kind=kind)
  683. assert_array_almost_equal(z(x), y, err_msg=kind)
  684. x2 = np.arange(2*3*1).reshape((2,3,1)) / 12.
  685. b = list(a)
  686. b[n:n+1] = [2,3,1]
  687. assert_array_almost_equal(z(x2).shape, b, err_msg=kind)
  688. def test_nd(self):
  689. for kind in ('linear', 'cubic', 'slinear', 'quadratic', 'nearest',
  690. 'zero', 'previous', 'next'):
  691. self._nd_check_interp(kind)
  692. self._nd_check_shape(kind)
  693. def _check_complex(self, dtype=np.complex_, kind='linear'):
  694. x = np.array([1, 2.5, 3, 3.1, 4, 6.4, 7.9, 8.0, 9.5, 10])
  695. y = x * x ** (1 + 2j)
  696. y = y.astype(dtype)
  697. # simple test
  698. c = interp1d(x, y, kind=kind)
  699. assert_array_almost_equal(y[:-1], c(x)[:-1])
  700. # check against interpolating real+imag separately
  701. xi = np.linspace(1, 10, 31)
  702. cr = interp1d(x, y.real, kind=kind)
  703. ci = interp1d(x, y.imag, kind=kind)
  704. assert_array_almost_equal(c(xi).real, cr(xi))
  705. assert_array_almost_equal(c(xi).imag, ci(xi))
  706. def test_complex(self):
  707. for kind in ('linear', 'nearest', 'cubic', 'slinear', 'quadratic',
  708. 'zero', 'previous', 'next'):
  709. self._check_complex(np.complex64, kind)
  710. self._check_complex(np.complex128, kind)
  711. @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
  712. def test_circular_refs(self):
  713. # Test interp1d can be automatically garbage collected
  714. x = np.linspace(0, 1)
  715. y = np.linspace(0, 1)
  716. # Confirm interp can be released from memory after use
  717. with assert_deallocated(interp1d, x, y) as interp:
  718. interp([0.1, 0.2])
  719. del interp
  720. def test_overflow_nearest(self):
  721. # Test that the x range doesn't overflow when given integers as input
  722. for kind in ('nearest', 'previous', 'next'):
  723. x = np.array([0, 50, 127], dtype=np.int8)
  724. ii = interp1d(x, x, kind=kind)
  725. assert_array_almost_equal(ii(x), x)
  726. def test_local_nans(self):
  727. # check that for local interpolation kinds (slinear, zero) a single nan
  728. # only affects its local neighborhood
  729. x = np.arange(10).astype(float)
  730. y = x.copy()
  731. y[6] = np.nan
  732. for kind in ('zero', 'slinear'):
  733. ir = interp1d(x, y, kind=kind)
  734. vals = ir([4.9, 7.0])
  735. assert_(np.isfinite(vals).all())
  736. def test_spline_nans(self):
  737. # Backwards compat: a single nan makes the whole spline interpolation
  738. # return nans in an array of the correct shape. And it doesn't raise,
  739. # just quiet nans because of backcompat.
  740. x = np.arange(8).astype(float)
  741. y = x.copy()
  742. yn = y.copy()
  743. yn[3] = np.nan
  744. for kind in ['quadratic', 'cubic']:
  745. ir = interp1d(x, y, kind=kind)
  746. irn = interp1d(x, yn, kind=kind)
  747. for xnew in (6, [1, 6], [[1, 6], [3, 5]]):
  748. xnew = np.asarray(xnew)
  749. out, outn = ir(x), irn(x)
  750. assert_(np.isnan(outn).all())
  751. assert_equal(out.shape, outn.shape)
  752. def test_all_nans(self):
  753. # regression test for gh-11637: interp1d core dumps with all-nan `x`
  754. x = np.ones(10) * np.nan
  755. y = np.arange(10)
  756. with assert_raises(ValueError):
  757. interp1d(x, y, kind='cubic')
  758. def test_read_only(self):
  759. x = np.arange(0, 10)
  760. y = np.exp(-x / 3.0)
  761. xnew = np.arange(0, 9, 0.1)
  762. # Check both read-only and not read-only:
  763. for xnew_writeable in (True, False):
  764. xnew.flags.writeable = xnew_writeable
  765. x.flags.writeable = False
  766. for kind in ('linear', 'nearest', 'zero', 'slinear', 'quadratic',
  767. 'cubic'):
  768. f = interp1d(x, y, kind=kind)
  769. vals = f(xnew)
  770. assert_(np.isfinite(vals).all())
  771. @pytest.mark.parametrize(
  772. "kind", ("linear", "nearest", "nearest-up", "previous", "next")
  773. )
  774. def test_single_value(self, kind):
  775. # https://github.com/scipy/scipy/issues/4043
  776. f = interp1d([1.5], [6], kind=kind, bounds_error=False,
  777. fill_value=(2, 10))
  778. assert_array_equal(f([1, 1.5, 2]), [2, 6, 10])
  779. # check still error if bounds_error=True
  780. f = interp1d([1.5], [6], kind=kind, bounds_error=True)
  781. with assert_raises(ValueError, match="x_new is above"):
  782. f(2.0)
  783. class TestLagrange:
  784. def test_lagrange(self):
  785. p = poly1d([5,2,1,4,3])
  786. xs = np.arange(len(p.coeffs))
  787. ys = p(xs)
  788. pl = lagrange(xs,ys)
  789. assert_array_almost_equal(p.coeffs,pl.coeffs)
  790. class TestAkima1DInterpolator:
  791. def test_eval(self):
  792. x = np.arange(0., 11.)
  793. y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.])
  794. ak = Akima1DInterpolator(x, y)
  795. xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2,
  796. 8.6, 9.9, 10.])
  797. yi = np.array([0., 1.375, 2., 1.5, 1.953125, 2.484375,
  798. 4.1363636363636366866103344, 5.9803623910336236590978842,
  799. 5.5067291516462386624652936, 5.2031367459745245795943447,
  800. 4.1796554159017080820603951, 3.4110386597938129327189927,
  801. 3.])
  802. assert_allclose(ak(xi), yi)
  803. def test_eval_2d(self):
  804. x = np.arange(0., 11.)
  805. y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.])
  806. y = np.column_stack((y, 2. * y))
  807. ak = Akima1DInterpolator(x, y)
  808. xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2,
  809. 8.6, 9.9, 10.])
  810. yi = np.array([0., 1.375, 2., 1.5, 1.953125, 2.484375,
  811. 4.1363636363636366866103344,
  812. 5.9803623910336236590978842,
  813. 5.5067291516462386624652936,
  814. 5.2031367459745245795943447,
  815. 4.1796554159017080820603951,
  816. 3.4110386597938129327189927, 3.])
  817. yi = np.column_stack((yi, 2. * yi))
  818. assert_allclose(ak(xi), yi)
  819. def test_eval_3d(self):
  820. x = np.arange(0., 11.)
  821. y_ = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.])
  822. y = np.empty((11, 2, 2))
  823. y[:, 0, 0] = y_
  824. y[:, 1, 0] = 2. * y_
  825. y[:, 0, 1] = 3. * y_
  826. y[:, 1, 1] = 4. * y_
  827. ak = Akima1DInterpolator(x, y)
  828. xi = np.array([0., 0.5, 1., 1.5, 2.5, 3.5, 4.5, 5.1, 6.5, 7.2,
  829. 8.6, 9.9, 10.])
  830. yi = np.empty((13, 2, 2))
  831. yi_ = np.array([0., 1.375, 2., 1.5, 1.953125, 2.484375,
  832. 4.1363636363636366866103344,
  833. 5.9803623910336236590978842,
  834. 5.5067291516462386624652936,
  835. 5.2031367459745245795943447,
  836. 4.1796554159017080820603951,
  837. 3.4110386597938129327189927, 3.])
  838. yi[:, 0, 0] = yi_
  839. yi[:, 1, 0] = 2. * yi_
  840. yi[:, 0, 1] = 3. * yi_
  841. yi[:, 1, 1] = 4. * yi_
  842. assert_allclose(ak(xi), yi)
  843. def test_degenerate_case_multidimensional(self):
  844. # This test is for issue #5683.
  845. x = np.array([0, 1, 2])
  846. y = np.vstack((x, x**2)).T
  847. ak = Akima1DInterpolator(x, y)
  848. x_eval = np.array([0.5, 1.5])
  849. y_eval = ak(x_eval)
  850. assert_allclose(y_eval, np.vstack((x_eval, x_eval**2)).T)
  851. def test_extend(self):
  852. x = np.arange(0., 11.)
  853. y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.])
  854. ak = Akima1DInterpolator(x, y)
  855. match = "Extending a 1-D Akima interpolator is not yet implemented"
  856. with pytest.raises(NotImplementedError, match=match):
  857. ak.extend(None, None)
  858. class TestPPolyCommon:
  859. # test basic functionality for PPoly and BPoly
  860. def test_sort_check(self):
  861. c = np.array([[1, 4], [2, 5], [3, 6]])
  862. x = np.array([0, 1, 0.5])
  863. assert_raises(ValueError, PPoly, c, x)
  864. assert_raises(ValueError, BPoly, c, x)
  865. def test_ctor_c(self):
  866. # wrong shape: `c` must be at least 2D
  867. with assert_raises(ValueError):
  868. PPoly([1, 2], [0, 1])
  869. def test_extend(self):
  870. # Test adding new points to the piecewise polynomial
  871. np.random.seed(1234)
  872. order = 3
  873. x = np.unique(np.r_[0, 10 * np.random.rand(30), 10])
  874. c = 2*np.random.rand(order+1, len(x)-1, 2, 3) - 1
  875. for cls in (PPoly, BPoly):
  876. pp = cls(c[:,:9], x[:10])
  877. pp.extend(c[:,9:], x[10:])
  878. pp2 = cls(c[:, 10:], x[10:])
  879. pp2.extend(c[:, :10], x[:10])
  880. pp3 = cls(c, x)
  881. assert_array_equal(pp.c, pp3.c)
  882. assert_array_equal(pp.x, pp3.x)
  883. assert_array_equal(pp2.c, pp3.c)
  884. assert_array_equal(pp2.x, pp3.x)
  885. def test_extend_diff_orders(self):
  886. # Test extending polynomial with different order one
  887. np.random.seed(1234)
  888. x = np.linspace(0, 1, 6)
  889. c = np.random.rand(2, 5)
  890. x2 = np.linspace(1, 2, 6)
  891. c2 = np.random.rand(4, 5)
  892. for cls in (PPoly, BPoly):
  893. pp1 = cls(c, x)
  894. pp2 = cls(c2, x2)
  895. pp_comb = cls(c, x)
  896. pp_comb.extend(c2, x2[1:])
  897. # NB. doesn't match to pp1 at the endpoint, because pp1 is not
  898. # continuous with pp2 as we took random coefs.
  899. xi1 = np.linspace(0, 1, 300, endpoint=False)
  900. xi2 = np.linspace(1, 2, 300)
  901. assert_allclose(pp1(xi1), pp_comb(xi1))
  902. assert_allclose(pp2(xi2), pp_comb(xi2))
  903. def test_extend_descending(self):
  904. np.random.seed(0)
  905. order = 3
  906. x = np.sort(np.random.uniform(0, 10, 20))
  907. c = np.random.rand(order + 1, x.shape[0] - 1, 2, 3)
  908. for cls in (PPoly, BPoly):
  909. p = cls(c, x)
  910. p1 = cls(c[:, :9], x[:10])
  911. p1.extend(c[:, 9:], x[10:])
  912. p2 = cls(c[:, 10:], x[10:])
  913. p2.extend(c[:, :10], x[:10])
  914. assert_array_equal(p1.c, p.c)
  915. assert_array_equal(p1.x, p.x)
  916. assert_array_equal(p2.c, p.c)
  917. assert_array_equal(p2.x, p.x)
  918. def test_shape(self):
  919. np.random.seed(1234)
  920. c = np.random.rand(8, 12, 5, 6, 7)
  921. x = np.sort(np.random.rand(13))
  922. xp = np.random.rand(3, 4)
  923. for cls in (PPoly, BPoly):
  924. p = cls(c, x)
  925. assert_equal(p(xp).shape, (3, 4, 5, 6, 7))
  926. # 'scalars'
  927. for cls in (PPoly, BPoly):
  928. p = cls(c[..., 0, 0, 0], x)
  929. assert_equal(np.shape(p(0.5)), ())
  930. assert_equal(np.shape(p(np.array(0.5))), ())
  931. assert_raises(ValueError, p, np.array([[0.1, 0.2], [0.4]], dtype=object))
  932. def test_complex_coef(self):
  933. np.random.seed(12345)
  934. x = np.sort(np.random.random(13))
  935. c = np.random.random((8, 12)) * (1. + 0.3j)
  936. c_re, c_im = c.real, c.imag
  937. xp = np.random.random(5)
  938. for cls in (PPoly, BPoly):
  939. p, p_re, p_im = cls(c, x), cls(c_re, x), cls(c_im, x)
  940. for nu in [0, 1, 2]:
  941. assert_allclose(p(xp, nu).real, p_re(xp, nu))
  942. assert_allclose(p(xp, nu).imag, p_im(xp, nu))
  943. def test_axis(self):
  944. np.random.seed(12345)
  945. c = np.random.rand(3, 4, 5, 6, 7, 8)
  946. c_s = c.shape
  947. xp = np.random.random((1, 2))
  948. for axis in (0, 1, 2, 3):
  949. m = c.shape[axis+1]
  950. x = np.sort(np.random.rand(m+1))
  951. for cls in (PPoly, BPoly):
  952. p = cls(c, x, axis=axis)
  953. assert_equal(p.c.shape,
  954. c_s[axis:axis+2] + c_s[:axis] + c_s[axis+2:])
  955. res = p(xp)
  956. targ_shape = c_s[:axis] + xp.shape + c_s[2+axis:]
  957. assert_equal(res.shape, targ_shape)
  958. # deriv/antideriv does not drop the axis
  959. for p1 in [cls(c, x, axis=axis).derivative(),
  960. cls(c, x, axis=axis).derivative(2),
  961. cls(c, x, axis=axis).antiderivative(),
  962. cls(c, x, axis=axis).antiderivative(2)]:
  963. assert_equal(p1.axis, p.axis)
  964. # c array needs two axes for the coefficients and intervals, so
  965. # 0 <= axis < c.ndim-1; raise otherwise
  966. for axis in (-1, 4, 5, 6):
  967. for cls in (BPoly, PPoly):
  968. assert_raises(ValueError, cls, **dict(c=c, x=x, axis=axis))
  969. class TestPolySubclassing:
  970. class P(PPoly):
  971. pass
  972. class B(BPoly):
  973. pass
  974. def _make_polynomials(self):
  975. np.random.seed(1234)
  976. x = np.sort(np.random.random(3))
  977. c = np.random.random((4, 2))
  978. return self.P(c, x), self.B(c, x)
  979. def test_derivative(self):
  980. pp, bp = self._make_polynomials()
  981. for p in (pp, bp):
  982. pd = p.derivative()
  983. assert_equal(p.__class__, pd.__class__)
  984. ppa = pp.antiderivative()
  985. assert_equal(pp.__class__, ppa.__class__)
  986. def test_from_spline(self):
  987. np.random.seed(1234)
  988. x = np.sort(np.r_[0, np.random.rand(11), 1])
  989. y = np.random.rand(len(x))
  990. spl = splrep(x, y, s=0)
  991. pp = self.P.from_spline(spl)
  992. assert_equal(pp.__class__, self.P)
  993. def test_conversions(self):
  994. pp, bp = self._make_polynomials()
  995. pp1 = self.P.from_bernstein_basis(bp)
  996. assert_equal(pp1.__class__, self.P)
  997. bp1 = self.B.from_power_basis(pp)
  998. assert_equal(bp1.__class__, self.B)
  999. def test_from_derivatives(self):
  1000. x = [0, 1, 2]
  1001. y = [[1], [2], [3]]
  1002. bp = self.B.from_derivatives(x, y)
  1003. assert_equal(bp.__class__, self.B)
  1004. class TestPPoly:
  1005. def test_simple(self):
  1006. c = np.array([[1, 4], [2, 5], [3, 6]])
  1007. x = np.array([0, 0.5, 1])
  1008. p = PPoly(c, x)
  1009. assert_allclose(p(0.3), 1*0.3**2 + 2*0.3 + 3)
  1010. assert_allclose(p(0.7), 4*(0.7-0.5)**2 + 5*(0.7-0.5) + 6)
  1011. def test_periodic(self):
  1012. c = np.array([[1, 4], [2, 5], [3, 6]])
  1013. x = np.array([0, 0.5, 1])
  1014. p = PPoly(c, x, extrapolate='periodic')
  1015. assert_allclose(p(1.3), 1 * 0.3 ** 2 + 2 * 0.3 + 3)
  1016. assert_allclose(p(-0.3), 4 * (0.7 - 0.5) ** 2 + 5 * (0.7 - 0.5) + 6)
  1017. assert_allclose(p(1.3, 1), 2 * 0.3 + 2)
  1018. assert_allclose(p(-0.3, 1), 8 * (0.7 - 0.5) + 5)
  1019. def test_read_only(self):
  1020. c = np.array([[1, 4], [2, 5], [3, 6]])
  1021. x = np.array([0, 0.5, 1])
  1022. xnew = np.array([0, 0.1, 0.2])
  1023. PPoly(c, x, extrapolate='periodic')
  1024. for writeable in (True, False):
  1025. x.flags.writeable = writeable
  1026. f = PPoly(c, x)
  1027. vals = f(xnew)
  1028. assert_(np.isfinite(vals).all())
  1029. def test_descending(self):
  1030. def binom_matrix(power):
  1031. n = np.arange(power + 1).reshape(-1, 1)
  1032. k = np.arange(power + 1)
  1033. B = binom(n, k)
  1034. return B[::-1, ::-1]
  1035. np.random.seed(0)
  1036. power = 3
  1037. for m in [10, 20, 30]:
  1038. x = np.sort(np.random.uniform(0, 10, m + 1))
  1039. ca = np.random.uniform(-2, 2, size=(power + 1, m))
  1040. h = np.diff(x)
  1041. h_powers = h[None, :] ** np.arange(power + 1)[::-1, None]
  1042. B = binom_matrix(power)
  1043. cap = ca * h_powers
  1044. cdp = np.dot(B.T, cap)
  1045. cd = cdp / h_powers
  1046. pa = PPoly(ca, x, extrapolate=True)
  1047. pd = PPoly(cd[:, ::-1], x[::-1], extrapolate=True)
  1048. x_test = np.random.uniform(-10, 20, 100)
  1049. assert_allclose(pa(x_test), pd(x_test), rtol=1e-13)
  1050. assert_allclose(pa(x_test, 1), pd(x_test, 1), rtol=1e-13)
  1051. pa_d = pa.derivative()
  1052. pd_d = pd.derivative()
  1053. assert_allclose(pa_d(x_test), pd_d(x_test), rtol=1e-13)
  1054. # Antiderivatives won't be equal because fixing continuity is
  1055. # done in the reverse order, but surely the differences should be
  1056. # equal.
  1057. pa_i = pa.antiderivative()
  1058. pd_i = pd.antiderivative()
  1059. for a, b in np.random.uniform(-10, 20, (5, 2)):
  1060. int_a = pa.integrate(a, b)
  1061. int_d = pd.integrate(a, b)
  1062. assert_allclose(int_a, int_d, rtol=1e-13)
  1063. assert_allclose(pa_i(b) - pa_i(a), pd_i(b) - pd_i(a),
  1064. rtol=1e-13)
  1065. roots_d = pd.roots()
  1066. roots_a = pa.roots()
  1067. assert_allclose(roots_a, np.sort(roots_d), rtol=1e-12)
  1068. def test_multi_shape(self):
  1069. c = np.random.rand(6, 2, 1, 2, 3)
  1070. x = np.array([0, 0.5, 1])
  1071. p = PPoly(c, x)
  1072. assert_equal(p.x.shape, x.shape)
  1073. assert_equal(p.c.shape, c.shape)
  1074. assert_equal(p(0.3).shape, c.shape[2:])
  1075. assert_equal(p(np.random.rand(5, 6)).shape, (5, 6) + c.shape[2:])
  1076. dp = p.derivative()
  1077. assert_equal(dp.c.shape, (5, 2, 1, 2, 3))
  1078. ip = p.antiderivative()
  1079. assert_equal(ip.c.shape, (7, 2, 1, 2, 3))
  1080. def test_construct_fast(self):
  1081. np.random.seed(1234)
  1082. c = np.array([[1, 4], [2, 5], [3, 6]], dtype=float)
  1083. x = np.array([0, 0.5, 1])
  1084. p = PPoly.construct_fast(c, x)
  1085. assert_allclose(p(0.3), 1*0.3**2 + 2*0.3 + 3)
  1086. assert_allclose(p(0.7), 4*(0.7-0.5)**2 + 5*(0.7-0.5) + 6)
  1087. def test_vs_alternative_implementations(self):
  1088. np.random.seed(1234)
  1089. c = np.random.rand(3, 12, 22)
  1090. x = np.sort(np.r_[0, np.random.rand(11), 1])
  1091. p = PPoly(c, x)
  1092. xp = np.r_[0.3, 0.5, 0.33, 0.6]
  1093. expected = _ppoly_eval_1(c, x, xp)
  1094. assert_allclose(p(xp), expected)
  1095. expected = _ppoly_eval_2(c[:,:,0], x, xp)
  1096. assert_allclose(p(xp)[:,0], expected)
  1097. def test_from_spline(self):
  1098. np.random.seed(1234)
  1099. x = np.sort(np.r_[0, np.random.rand(11), 1])
  1100. y = np.random.rand(len(x))
  1101. spl = splrep(x, y, s=0)
  1102. pp = PPoly.from_spline(spl)
  1103. xi = np.linspace(0, 1, 200)
  1104. assert_allclose(pp(xi), splev(xi, spl))
  1105. # make sure .from_spline accepts BSpline objects
  1106. b = BSpline(*spl)
  1107. ppp = PPoly.from_spline(b)
  1108. assert_allclose(ppp(xi), b(xi))
  1109. # BSpline's extrapolate attribute propagates unless overridden
  1110. t, c, k = spl
  1111. for extrap in (None, True, False):
  1112. b = BSpline(t, c, k, extrapolate=extrap)
  1113. p = PPoly.from_spline(b)
  1114. assert_equal(p.extrapolate, b.extrapolate)
  1115. def test_derivative_simple(self):
  1116. np.random.seed(1234)
  1117. c = np.array([[4, 3, 2, 1]]).T
  1118. dc = np.array([[3*4, 2*3, 2]]).T
  1119. ddc = np.array([[2*3*4, 1*2*3]]).T
  1120. x = np.array([0, 1])
  1121. pp = PPoly(c, x)
  1122. dpp = PPoly(dc, x)
  1123. ddpp = PPoly(ddc, x)
  1124. assert_allclose(pp.derivative().c, dpp.c)
  1125. assert_allclose(pp.derivative(2).c, ddpp.c)
  1126. def test_derivative_eval(self):
  1127. np.random.seed(1234)
  1128. x = np.sort(np.r_[0, np.random.rand(11), 1])
  1129. y = np.random.rand(len(x))
  1130. spl = splrep(x, y, s=0)
  1131. pp = PPoly.from_spline(spl)
  1132. xi = np.linspace(0, 1, 200)
  1133. for dx in range(0, 3):
  1134. assert_allclose(pp(xi, dx), splev(xi, spl, dx))
  1135. def test_derivative(self):
  1136. np.random.seed(1234)
  1137. x = np.sort(np.r_[0, np.random.rand(11), 1])
  1138. y = np.random.rand(len(x))
  1139. spl = splrep(x, y, s=0, k=5)
  1140. pp = PPoly.from_spline(spl)
  1141. xi = np.linspace(0, 1, 200)
  1142. for dx in range(0, 10):
  1143. assert_allclose(pp(xi, dx), pp.derivative(dx)(xi),
  1144. err_msg="dx=%d" % (dx,))
  1145. def test_antiderivative_of_constant(self):
  1146. # https://github.com/scipy/scipy/issues/4216
  1147. p = PPoly([[1.]], [0, 1])
  1148. assert_equal(p.antiderivative().c, PPoly([[1], [0]], [0, 1]).c)
  1149. assert_equal(p.antiderivative().x, PPoly([[1], [0]], [0, 1]).x)
  1150. def test_antiderivative_regression_4355(self):
  1151. # https://github.com/scipy/scipy/issues/4355
  1152. p = PPoly([[1., 0.5]], [0, 1, 2])
  1153. q = p.antiderivative()
  1154. assert_equal(q.c, [[1, 0.5], [0, 1]])
  1155. assert_equal(q.x, [0, 1, 2])
  1156. assert_allclose(p.integrate(0, 2), 1.5)
  1157. assert_allclose(q(2) - q(0), 1.5)
  1158. def test_antiderivative_simple(self):
  1159. np.random.seed(1234)
  1160. # [ p1(x) = 3*x**2 + 2*x + 1,
  1161. # p2(x) = 1.6875]
  1162. c = np.array([[3, 2, 1], [0, 0, 1.6875]]).T
  1163. # [ pp1(x) = x**3 + x**2 + x,
  1164. # pp2(x) = 1.6875*(x - 0.25) + pp1(0.25)]
  1165. ic = np.array([[1, 1, 1, 0], [0, 0, 1.6875, 0.328125]]).T
  1166. # [ ppp1(x) = (1/4)*x**4 + (1/3)*x**3 + (1/2)*x**2,
  1167. # ppp2(x) = (1.6875/2)*(x - 0.25)**2 + pp1(0.25)*x + ppp1(0.25)]
  1168. iic = np.array([[1/4, 1/3, 1/2, 0, 0],
  1169. [0, 0, 1.6875/2, 0.328125, 0.037434895833333336]]).T
  1170. x = np.array([0, 0.25, 1])
  1171. pp = PPoly(c, x)
  1172. ipp = pp.antiderivative()
  1173. iipp = pp.antiderivative(2)
  1174. iipp2 = ipp.antiderivative()
  1175. assert_allclose(ipp.x, x)
  1176. assert_allclose(ipp.c.T, ic.T)
  1177. assert_allclose(iipp.c.T, iic.T)
  1178. assert_allclose(iipp2.c.T, iic.T)
  1179. def test_antiderivative_vs_derivative(self):
  1180. np.random.seed(1234)
  1181. x = np.linspace(0, 1, 30)**2
  1182. y = np.random.rand(len(x))
  1183. spl = splrep(x, y, s=0, k=5)
  1184. pp = PPoly.from_spline(spl)
  1185. for dx in range(0, 10):
  1186. ipp = pp.antiderivative(dx)
  1187. # check that derivative is inverse op
  1188. pp2 = ipp.derivative(dx)
  1189. assert_allclose(pp.c, pp2.c)
  1190. # check continuity
  1191. for k in range(dx):
  1192. pp2 = ipp.derivative(k)
  1193. r = 1e-13
  1194. endpoint = r*pp2.x[:-1] + (1 - r)*pp2.x[1:]
  1195. assert_allclose(pp2(pp2.x[1:]), pp2(endpoint),
  1196. rtol=1e-7, err_msg="dx=%d k=%d" % (dx, k))
  1197. def test_antiderivative_vs_spline(self):
  1198. np.random.seed(1234)
  1199. x = np.sort(np.r_[0, np.random.rand(11), 1])
  1200. y = np.random.rand(len(x))
  1201. spl = splrep(x, y, s=0, k=5)
  1202. pp = PPoly.from_spline(spl)
  1203. for dx in range(0, 10):
  1204. pp2 = pp.antiderivative(dx)
  1205. spl2 = splantider(spl, dx)
  1206. xi = np.linspace(0, 1, 200)
  1207. assert_allclose(pp2(xi), splev(xi, spl2),
  1208. rtol=1e-7)
  1209. def test_antiderivative_continuity(self):
  1210. c = np.array([[2, 1, 2, 2], [2, 1, 3, 3]]).T
  1211. x = np.array([0, 0.5, 1])
  1212. p = PPoly(c, x)
  1213. ip = p.antiderivative()
  1214. # check continuity
  1215. assert_allclose(ip(0.5 - 1e-9), ip(0.5 + 1e-9), rtol=1e-8)
  1216. # check that only lowest order coefficients were changed
  1217. p2 = ip.derivative()
  1218. assert_allclose(p2.c, p.c)
  1219. def test_integrate(self):
  1220. np.random.seed(1234)
  1221. x = np.sort(np.r_[0, np.random.rand(11), 1])
  1222. y = np.random.rand(len(x))
  1223. spl = splrep(x, y, s=0, k=5)
  1224. pp = PPoly.from_spline(spl)
  1225. a, b = 0.3, 0.9
  1226. ig = pp.integrate(a, b)
  1227. ipp = pp.antiderivative()
  1228. assert_allclose(ig, ipp(b) - ipp(a))
  1229. assert_allclose(ig, splint(a, b, spl))
  1230. a, b = -0.3, 0.9
  1231. ig = pp.integrate(a, b, extrapolate=True)
  1232. assert_allclose(ig, ipp(b) - ipp(a))
  1233. assert_(np.isnan(pp.integrate(a, b, extrapolate=False)).all())
  1234. def test_integrate_readonly(self):
  1235. x = np.array([1, 2, 4])
  1236. c = np.array([[0., 0.], [-1., -1.], [2., -0.], [1., 2.]])
  1237. for writeable in (True, False):
  1238. x.flags.writeable = writeable
  1239. P = PPoly(c, x)
  1240. vals = P.integrate(1, 4)
  1241. assert_(np.isfinite(vals).all())
  1242. def test_integrate_periodic(self):
  1243. x = np.array([1, 2, 4])
  1244. c = np.array([[0., 0.], [-1., -1.], [2., -0.], [1., 2.]])
  1245. P = PPoly(c, x, extrapolate='periodic')
  1246. I = P.antiderivative()
  1247. period_int = I(4) - I(1)
  1248. assert_allclose(P.integrate(1, 4), period_int)
  1249. assert_allclose(P.integrate(-10, -7), period_int)
  1250. assert_allclose(P.integrate(-10, -4), 2 * period_int)
  1251. assert_allclose(P.integrate(1.5, 2.5), I(2.5) - I(1.5))
  1252. assert_allclose(P.integrate(3.5, 5), I(2) - I(1) + I(4) - I(3.5))
  1253. assert_allclose(P.integrate(3.5 + 12, 5 + 12),
  1254. I(2) - I(1) + I(4) - I(3.5))
  1255. assert_allclose(P.integrate(3.5, 5 + 12),
  1256. I(2) - I(1) + I(4) - I(3.5) + 4 * period_int)
  1257. assert_allclose(P.integrate(0, -1), I(2) - I(3))
  1258. assert_allclose(P.integrate(-9, -10), I(2) - I(3))
  1259. assert_allclose(P.integrate(0, -10), I(2) - I(3) - 3 * period_int)
  1260. def test_roots(self):
  1261. x = np.linspace(0, 1, 31)**2
  1262. y = np.sin(30*x)
  1263. spl = splrep(x, y, s=0, k=3)
  1264. pp = PPoly.from_spline(spl)
  1265. r = pp.roots()
  1266. r = r[(r >= 0 - 1e-15) & (r <= 1 + 1e-15)]
  1267. assert_allclose(r, sproot(spl), atol=1e-15)
  1268. def test_roots_idzero(self):
  1269. # Roots for piecewise polynomials with identically zero
  1270. # sections.
  1271. c = np.array([[-1, 0.25], [0, 0], [-1, 0.25]]).T
  1272. x = np.array([0, 0.4, 0.6, 1.0])
  1273. pp = PPoly(c, x)
  1274. assert_array_equal(pp.roots(),
  1275. [0.25, 0.4, np.nan, 0.6 + 0.25])
  1276. # ditto for p.solve(const) with sections identically equal const
  1277. const = 2.
  1278. c1 = c.copy()
  1279. c1[1, :] += const
  1280. pp1 = PPoly(c1, x)
  1281. assert_array_equal(pp1.solve(const),
  1282. [0.25, 0.4, np.nan, 0.6 + 0.25])
  1283. def test_roots_all_zero(self):
  1284. # test the code path for the polynomial being identically zero everywhere
  1285. c = [[0], [0]]
  1286. x = [0, 1]
  1287. p = PPoly(c, x)
  1288. assert_array_equal(p.roots(), [0, np.nan])
  1289. assert_array_equal(p.solve(0), [0, np.nan])
  1290. assert_array_equal(p.solve(1), [])
  1291. c = [[0, 0], [0, 0]]
  1292. x = [0, 1, 2]
  1293. p = PPoly(c, x)
  1294. assert_array_equal(p.roots(), [0, np.nan, 1, np.nan])
  1295. assert_array_equal(p.solve(0), [0, np.nan, 1, np.nan])
  1296. assert_array_equal(p.solve(1), [])
  1297. def test_roots_repeated(self):
  1298. # Check roots repeated in multiple sections are reported only
  1299. # once.
  1300. # [(x + 1)**2 - 1, -x**2] ; x == 0 is a repeated root
  1301. c = np.array([[1, 0, -1], [-1, 0, 0]]).T
  1302. x = np.array([-1, 0, 1])
  1303. pp = PPoly(c, x)
  1304. assert_array_equal(pp.roots(), [-2, 0])
  1305. assert_array_equal(pp.roots(extrapolate=False), [0])
  1306. def test_roots_discont(self):
  1307. # Check that a discontinuity across zero is reported as root
  1308. c = np.array([[1], [-1]]).T
  1309. x = np.array([0, 0.5, 1])
  1310. pp = PPoly(c, x)
  1311. assert_array_equal(pp.roots(), [0.5])
  1312. assert_array_equal(pp.roots(discontinuity=False), [])
  1313. # ditto for a discontinuity across y:
  1314. assert_array_equal(pp.solve(0.5), [0.5])
  1315. assert_array_equal(pp.solve(0.5, discontinuity=False), [])
  1316. assert_array_equal(pp.solve(1.5), [])
  1317. assert_array_equal(pp.solve(1.5, discontinuity=False), [])
  1318. def test_roots_random(self):
  1319. # Check high-order polynomials with random coefficients
  1320. np.random.seed(1234)
  1321. num = 0
  1322. for extrapolate in (True, False):
  1323. for order in range(0, 20):
  1324. x = np.unique(np.r_[0, 10 * np.random.rand(30), 10])
  1325. c = 2*np.random.rand(order+1, len(x)-1, 2, 3) - 1
  1326. pp = PPoly(c, x)
  1327. for y in [0, np.random.random()]:
  1328. r = pp.solve(y, discontinuity=False, extrapolate=extrapolate)
  1329. for i in range(2):
  1330. for j in range(3):
  1331. rr = r[i,j]
  1332. if rr.size > 0:
  1333. # Check that the reported roots indeed are roots
  1334. num += rr.size
  1335. val = pp(rr, extrapolate=extrapolate)[:,i,j]
  1336. cmpval = pp(rr, nu=1,
  1337. extrapolate=extrapolate)[:,i,j]
  1338. msg = "(%r) r = %s" % (extrapolate, repr(rr),)
  1339. assert_allclose((val-y) / cmpval, 0, atol=1e-7,
  1340. err_msg=msg)
  1341. # Check that we checked a number of roots
  1342. assert_(num > 100, repr(num))
  1343. def test_roots_croots(self):
  1344. # Test the complex root finding algorithm
  1345. np.random.seed(1234)
  1346. for k in range(1, 15):
  1347. c = np.random.rand(k, 1, 130)
  1348. if k == 3:
  1349. # add a case with zero discriminant
  1350. c[:,0,0] = 1, 2, 1
  1351. for y in [0, np.random.random()]:
  1352. w = np.empty(c.shape, dtype=complex)
  1353. _ppoly._croots_poly1(c, w)
  1354. if k == 1:
  1355. assert_(np.isnan(w).all())
  1356. continue
  1357. res = 0
  1358. cres = 0
  1359. for i in range(k):
  1360. res += c[i,None] * w**(k-1-i)
  1361. cres += abs(c[i,None] * w**(k-1-i))
  1362. with np.errstate(invalid='ignore'):
  1363. res /= cres
  1364. res = res.ravel()
  1365. res = res[~np.isnan(res)]
  1366. assert_allclose(res, 0, atol=1e-10)
  1367. def test_extrapolate_attr(self):
  1368. # [ 1 - x**2 ]
  1369. c = np.array([[-1, 0, 1]]).T
  1370. x = np.array([0, 1])
  1371. for extrapolate in [True, False, None]:
  1372. pp = PPoly(c, x, extrapolate=extrapolate)
  1373. pp_d = pp.derivative()
  1374. pp_i = pp.antiderivative()
  1375. if extrapolate is False:
  1376. assert_(np.isnan(pp([-0.1, 1.1])).all())
  1377. assert_(np.isnan(pp_i([-0.1, 1.1])).all())
  1378. assert_(np.isnan(pp_d([-0.1, 1.1])).all())
  1379. assert_equal(pp.roots(), [1])
  1380. else:
  1381. assert_allclose(pp([-0.1, 1.1]), [1-0.1**2, 1-1.1**2])
  1382. assert_(not np.isnan(pp_i([-0.1, 1.1])).any())
  1383. assert_(not np.isnan(pp_d([-0.1, 1.1])).any())
  1384. assert_allclose(pp.roots(), [1, -1])
  1385. class TestBPoly:
  1386. def test_simple(self):
  1387. x = [0, 1]
  1388. c = [[3]]
  1389. bp = BPoly(c, x)
  1390. assert_allclose(bp(0.1), 3.)
  1391. def test_simple2(self):
  1392. x = [0, 1]
  1393. c = [[3], [1]]
  1394. bp = BPoly(c, x) # 3*(1-x) + 1*x
  1395. assert_allclose(bp(0.1), 3*0.9 + 1.*0.1)
  1396. def test_simple3(self):
  1397. x = [0, 1]
  1398. c = [[3], [1], [4]]
  1399. bp = BPoly(c, x) # 3 * (1-x)**2 + 2 * x (1-x) + 4 * x**2
  1400. assert_allclose(bp(0.2),
  1401. 3 * 0.8*0.8 + 1 * 2*0.2*0.8 + 4 * 0.2*0.2)
  1402. def test_simple4(self):
  1403. x = [0, 1]
  1404. c = [[1], [1], [1], [2]]
  1405. bp = BPoly(c, x)
  1406. assert_allclose(bp(0.3), 0.7**3 +
  1407. 3 * 0.7**2 * 0.3 +
  1408. 3 * 0.7 * 0.3**2 +
  1409. 2 * 0.3**3)
  1410. def test_simple5(self):
  1411. x = [0, 1]
  1412. c = [[1], [1], [8], [2], [1]]
  1413. bp = BPoly(c, x)
  1414. assert_allclose(bp(0.3), 0.7**4 +
  1415. 4 * 0.7**3 * 0.3 +
  1416. 8 * 6 * 0.7**2 * 0.3**2 +
  1417. 2 * 4 * 0.7 * 0.3**3 +
  1418. 0.3**4)
  1419. def test_periodic(self):
  1420. x = [0, 1, 3]
  1421. c = [[3, 0], [0, 0], [0, 2]]
  1422. # [3*(1-x)**2, 2*((x-1)/2)**2]
  1423. bp = BPoly(c, x, extrapolate='periodic')
  1424. assert_allclose(bp(3.4), 3 * 0.6**2)
  1425. assert_allclose(bp(-1.3), 2 * (0.7/2)**2)
  1426. assert_allclose(bp(3.4, 1), -6 * 0.6)
  1427. assert_allclose(bp(-1.3, 1), 2 * (0.7/2))
  1428. def test_descending(self):
  1429. np.random.seed(0)
  1430. power = 3
  1431. for m in [10, 20, 30]:
  1432. x = np.sort(np.random.uniform(0, 10, m + 1))
  1433. ca = np.random.uniform(-0.1, 0.1, size=(power + 1, m))
  1434. # We need only to flip coefficients to get it right!
  1435. cd = ca[::-1].copy()
  1436. pa = BPoly(ca, x, extrapolate=True)
  1437. pd = BPoly(cd[:, ::-1], x[::-1], extrapolate=True)
  1438. x_test = np.random.uniform(-10, 20, 100)
  1439. assert_allclose(pa(x_test), pd(x_test), rtol=1e-13)
  1440. assert_allclose(pa(x_test, 1), pd(x_test, 1), rtol=1e-13)
  1441. pa_d = pa.derivative()
  1442. pd_d = pd.derivative()
  1443. assert_allclose(pa_d(x_test), pd_d(x_test), rtol=1e-13)
  1444. # Antiderivatives won't be equal because fixing continuity is
  1445. # done in the reverse order, but surely the differences should be
  1446. # equal.
  1447. pa_i = pa.antiderivative()
  1448. pd_i = pd.antiderivative()
  1449. for a, b in np.random.uniform(-10, 20, (5, 2)):
  1450. int_a = pa.integrate(a, b)
  1451. int_d = pd.integrate(a, b)
  1452. assert_allclose(int_a, int_d, rtol=1e-12)
  1453. assert_allclose(pa_i(b) - pa_i(a), pd_i(b) - pd_i(a),
  1454. rtol=1e-12)
  1455. def test_multi_shape(self):
  1456. c = np.random.rand(6, 2, 1, 2, 3)
  1457. x = np.array([0, 0.5, 1])
  1458. p = BPoly(c, x)
  1459. assert_equal(p.x.shape, x.shape)
  1460. assert_equal(p.c.shape, c.shape)
  1461. assert_equal(p(0.3).shape, c.shape[2:])
  1462. assert_equal(p(np.random.rand(5,6)).shape,
  1463. (5,6)+c.shape[2:])
  1464. dp = p.derivative()
  1465. assert_equal(dp.c.shape, (5, 2, 1, 2, 3))
  1466. def test_interval_length(self):
  1467. x = [0, 2]
  1468. c = [[3], [1], [4]]
  1469. bp = BPoly(c, x)
  1470. xval = 0.1
  1471. s = xval / 2 # s = (x - xa) / (xb - xa)
  1472. assert_allclose(bp(xval), 3 * (1-s)*(1-s) + 1 * 2*s*(1-s) + 4 * s*s)
  1473. def test_two_intervals(self):
  1474. x = [0, 1, 3]
  1475. c = [[3, 0], [0, 0], [0, 2]]
  1476. bp = BPoly(c, x) # [3*(1-x)**2, 2*((x-1)/2)**2]
  1477. assert_allclose(bp(0.4), 3 * 0.6*0.6)
  1478. assert_allclose(bp(1.7), 2 * (0.7/2)**2)
  1479. def test_extrapolate_attr(self):
  1480. x = [0, 2]
  1481. c = [[3], [1], [4]]
  1482. bp = BPoly(c, x)
  1483. for extrapolate in (True, False, None):
  1484. bp = BPoly(c, x, extrapolate=extrapolate)
  1485. bp_d = bp.derivative()
  1486. if extrapolate is False:
  1487. assert_(np.isnan(bp([-0.1, 2.1])).all())
  1488. assert_(np.isnan(bp_d([-0.1, 2.1])).all())
  1489. else:
  1490. assert_(not np.isnan(bp([-0.1, 2.1])).any())
  1491. assert_(not np.isnan(bp_d([-0.1, 2.1])).any())
  1492. class TestBPolyCalculus:
  1493. def test_derivative(self):
  1494. x = [0, 1, 3]
  1495. c = [[3, 0], [0, 0], [0, 2]]
  1496. bp = BPoly(c, x) # [3*(1-x)**2, 2*((x-1)/2)**2]
  1497. bp_der = bp.derivative()
  1498. assert_allclose(bp_der(0.4), -6*(0.6))
  1499. assert_allclose(bp_der(1.7), 0.7)
  1500. # derivatives in-place
  1501. assert_allclose([bp(0.4, nu=1), bp(0.4, nu=2), bp(0.4, nu=3)],
  1502. [-6*(1-0.4), 6., 0.])
  1503. assert_allclose([bp(1.7, nu=1), bp(1.7, nu=2), bp(1.7, nu=3)],
  1504. [0.7, 1., 0])
  1505. def test_derivative_ppoly(self):
  1506. # make sure it's consistent w/ power basis
  1507. np.random.seed(1234)
  1508. m, k = 5, 8 # number of intervals, order
  1509. x = np.sort(np.random.random(m))
  1510. c = np.random.random((k, m-1))
  1511. bp = BPoly(c, x)
  1512. pp = PPoly.from_bernstein_basis(bp)
  1513. for d in range(k):
  1514. bp = bp.derivative()
  1515. pp = pp.derivative()
  1516. xp = np.linspace(x[0], x[-1], 21)
  1517. assert_allclose(bp(xp), pp(xp))
  1518. def test_deriv_inplace(self):
  1519. np.random.seed(1234)
  1520. m, k = 5, 8 # number of intervals, order
  1521. x = np.sort(np.random.random(m))
  1522. c = np.random.random((k, m-1))
  1523. # test both real and complex coefficients
  1524. for cc in [c.copy(), c*(1. + 2.j)]:
  1525. bp = BPoly(cc, x)
  1526. xp = np.linspace(x[0], x[-1], 21)
  1527. for i in range(k):
  1528. assert_allclose(bp(xp, i), bp.derivative(i)(xp))
  1529. def test_antiderivative_simple(self):
  1530. # f(x) = x for x \in [0, 1),
  1531. # (x-1)/2 for x \in [1, 3]
  1532. #
  1533. # antiderivative is then
  1534. # F(x) = x**2 / 2 for x \in [0, 1),
  1535. # 0.5*x*(x/2 - 1) + A for x \in [1, 3]
  1536. # where A = 3/4 for continuity at x = 1.
  1537. x = [0, 1, 3]
  1538. c = [[0, 0], [1, 1]]
  1539. bp = BPoly(c, x)
  1540. bi = bp.antiderivative()
  1541. xx = np.linspace(0, 3, 11)
  1542. assert_allclose(bi(xx),
  1543. np.where(xx < 1, xx**2 / 2.,
  1544. 0.5 * xx * (xx/2. - 1) + 3./4),
  1545. atol=1e-12, rtol=1e-12)
  1546. def test_der_antider(self):
  1547. np.random.seed(1234)
  1548. x = np.sort(np.random.random(11))
  1549. c = np.random.random((4, 10, 2, 3))
  1550. bp = BPoly(c, x)
  1551. xx = np.linspace(x[0], x[-1], 100)
  1552. assert_allclose(bp.antiderivative().derivative()(xx),
  1553. bp(xx), atol=1e-12, rtol=1e-12)
  1554. def test_antider_ppoly(self):
  1555. np.random.seed(1234)
  1556. x = np.sort(np.random.random(11))
  1557. c = np.random.random((4, 10, 2, 3))
  1558. bp = BPoly(c, x)
  1559. pp = PPoly.from_bernstein_basis(bp)
  1560. xx = np.linspace(x[0], x[-1], 10)
  1561. assert_allclose(bp.antiderivative(2)(xx),
  1562. pp.antiderivative(2)(xx), atol=1e-12, rtol=1e-12)
  1563. def test_antider_continuous(self):
  1564. np.random.seed(1234)
  1565. x = np.sort(np.random.random(11))
  1566. c = np.random.random((4, 10))
  1567. bp = BPoly(c, x).antiderivative()
  1568. xx = bp.x[1:-1]
  1569. assert_allclose(bp(xx - 1e-14),
  1570. bp(xx + 1e-14), atol=1e-12, rtol=1e-12)
  1571. def test_integrate(self):
  1572. np.random.seed(1234)
  1573. x = np.sort(np.random.random(11))
  1574. c = np.random.random((4, 10))
  1575. bp = BPoly(c, x)
  1576. pp = PPoly.from_bernstein_basis(bp)
  1577. assert_allclose(bp.integrate(0, 1),
  1578. pp.integrate(0, 1), atol=1e-12, rtol=1e-12)
  1579. def test_integrate_extrap(self):
  1580. c = [[1]]
  1581. x = [0, 1]
  1582. b = BPoly(c, x)
  1583. # default is extrapolate=True
  1584. assert_allclose(b.integrate(0, 2), 2., atol=1e-14)
  1585. # .integrate argument overrides self.extrapolate
  1586. b1 = BPoly(c, x, extrapolate=False)
  1587. assert_(np.isnan(b1.integrate(0, 2)))
  1588. assert_allclose(b1.integrate(0, 2, extrapolate=True), 2., atol=1e-14)
  1589. def test_integrate_periodic(self):
  1590. x = np.array([1, 2, 4])
  1591. c = np.array([[0., 0.], [-1., -1.], [2., -0.], [1., 2.]])
  1592. P = BPoly.from_power_basis(PPoly(c, x), extrapolate='periodic')
  1593. I = P.antiderivative()
  1594. period_int = I(4) - I(1)
  1595. assert_allclose(P.integrate(1, 4), period_int)
  1596. assert_allclose(P.integrate(-10, -7), period_int)
  1597. assert_allclose(P.integrate(-10, -4), 2 * period_int)
  1598. assert_allclose(P.integrate(1.5, 2.5), I(2.5) - I(1.5))
  1599. assert_allclose(P.integrate(3.5, 5), I(2) - I(1) + I(4) - I(3.5))
  1600. assert_allclose(P.integrate(3.5 + 12, 5 + 12),
  1601. I(2) - I(1) + I(4) - I(3.5))
  1602. assert_allclose(P.integrate(3.5, 5 + 12),
  1603. I(2) - I(1) + I(4) - I(3.5) + 4 * period_int)
  1604. assert_allclose(P.integrate(0, -1), I(2) - I(3))
  1605. assert_allclose(P.integrate(-9, -10), I(2) - I(3))
  1606. assert_allclose(P.integrate(0, -10), I(2) - I(3) - 3 * period_int)
  1607. def test_antider_neg(self):
  1608. # .derivative(-nu) ==> .andiderivative(nu) and vice versa
  1609. c = [[1]]
  1610. x = [0, 1]
  1611. b = BPoly(c, x)
  1612. xx = np.linspace(0, 1, 21)
  1613. assert_allclose(b.derivative(-1)(xx), b.antiderivative()(xx),
  1614. atol=1e-12, rtol=1e-12)
  1615. assert_allclose(b.derivative(1)(xx), b.antiderivative(-1)(xx),
  1616. atol=1e-12, rtol=1e-12)
  1617. class TestPolyConversions:
  1618. def test_bp_from_pp(self):
  1619. x = [0, 1, 3]
  1620. c = [[3, 2], [1, 8], [4, 3]]
  1621. pp = PPoly(c, x)
  1622. bp = BPoly.from_power_basis(pp)
  1623. pp1 = PPoly.from_bernstein_basis(bp)
  1624. xp = [0.1, 1.4]
  1625. assert_allclose(pp(xp), bp(xp))
  1626. assert_allclose(pp(xp), pp1(xp))
  1627. def test_bp_from_pp_random(self):
  1628. np.random.seed(1234)
  1629. m, k = 5, 8 # number of intervals, order
  1630. x = np.sort(np.random.random(m))
  1631. c = np.random.random((k, m-1))
  1632. pp = PPoly(c, x)
  1633. bp = BPoly.from_power_basis(pp)
  1634. pp1 = PPoly.from_bernstein_basis(bp)
  1635. xp = np.linspace(x[0], x[-1], 21)
  1636. assert_allclose(pp(xp), bp(xp))
  1637. assert_allclose(pp(xp), pp1(xp))
  1638. def test_pp_from_bp(self):
  1639. x = [0, 1, 3]
  1640. c = [[3, 3], [1, 1], [4, 2]]
  1641. bp = BPoly(c, x)
  1642. pp = PPoly.from_bernstein_basis(bp)
  1643. bp1 = BPoly.from_power_basis(pp)
  1644. xp = [0.1, 1.4]
  1645. assert_allclose(bp(xp), pp(xp))
  1646. assert_allclose(bp(xp), bp1(xp))
  1647. def test_broken_conversions(self):
  1648. # regression test for gh-10597: from_power_basis only accepts PPoly etc.
  1649. x = [0, 1, 3]
  1650. c = [[3, 3], [1, 1], [4, 2]]
  1651. pp = PPoly(c, x)
  1652. with assert_raises(TypeError):
  1653. PPoly.from_bernstein_basis(pp)
  1654. bp = BPoly(c, x)
  1655. with assert_raises(TypeError):
  1656. BPoly.from_power_basis(bp)
  1657. class TestBPolyFromDerivatives:
  1658. def test_make_poly_1(self):
  1659. c1 = BPoly._construct_from_derivatives(0, 1, [2], [3])
  1660. assert_allclose(c1, [2., 3.])
  1661. def test_make_poly_2(self):
  1662. c1 = BPoly._construct_from_derivatives(0, 1, [1, 0], [1])
  1663. assert_allclose(c1, [1., 1., 1.])
  1664. # f'(0) = 3
  1665. c2 = BPoly._construct_from_derivatives(0, 1, [2, 3], [1])
  1666. assert_allclose(c2, [2., 7./2, 1.])
  1667. # f'(1) = 3
  1668. c3 = BPoly._construct_from_derivatives(0, 1, [2], [1, 3])
  1669. assert_allclose(c3, [2., -0.5, 1.])
  1670. def test_make_poly_3(self):
  1671. # f'(0)=2, f''(0)=3
  1672. c1 = BPoly._construct_from_derivatives(0, 1, [1, 2, 3], [4])
  1673. assert_allclose(c1, [1., 5./3, 17./6, 4.])
  1674. # f'(1)=2, f''(1)=3
  1675. c2 = BPoly._construct_from_derivatives(0, 1, [1], [4, 2, 3])
  1676. assert_allclose(c2, [1., 19./6, 10./3, 4.])
  1677. # f'(0)=2, f'(1)=3
  1678. c3 = BPoly._construct_from_derivatives(0, 1, [1, 2], [4, 3])
  1679. assert_allclose(c3, [1., 5./3, 3., 4.])
  1680. def test_make_poly_12(self):
  1681. np.random.seed(12345)
  1682. ya = np.r_[0, np.random.random(5)]
  1683. yb = np.r_[0, np.random.random(5)]
  1684. c = BPoly._construct_from_derivatives(0, 1, ya, yb)
  1685. pp = BPoly(c[:, None], [0, 1])
  1686. for j in range(6):
  1687. assert_allclose([pp(0.), pp(1.)], [ya[j], yb[j]])
  1688. pp = pp.derivative()
  1689. def test_raise_degree(self):
  1690. np.random.seed(12345)
  1691. x = [0, 1]
  1692. k, d = 8, 5
  1693. c = np.random.random((k, 1, 2, 3, 4))
  1694. bp = BPoly(c, x)
  1695. c1 = BPoly._raise_degree(c, d)
  1696. bp1 = BPoly(c1, x)
  1697. xp = np.linspace(0, 1, 11)
  1698. assert_allclose(bp(xp), bp1(xp))
  1699. def test_xi_yi(self):
  1700. assert_raises(ValueError, BPoly.from_derivatives, [0, 1], [0])
  1701. def test_coords_order(self):
  1702. xi = [0, 0, 1]
  1703. yi = [[0], [0], [0]]
  1704. assert_raises(ValueError, BPoly.from_derivatives, xi, yi)
  1705. def test_zeros(self):
  1706. xi = [0, 1, 2, 3]
  1707. yi = [[0, 0], [0], [0, 0], [0, 0]] # NB: will have to raise the degree
  1708. pp = BPoly.from_derivatives(xi, yi)
  1709. assert_(pp.c.shape == (4, 3))
  1710. ppd = pp.derivative()
  1711. for xp in [0., 0.1, 1., 1.1, 1.9, 2., 2.5]:
  1712. assert_allclose([pp(xp), ppd(xp)], [0., 0.])
  1713. def _make_random_mk(self, m, k):
  1714. # k derivatives at each breakpoint
  1715. np.random.seed(1234)
  1716. xi = np.asarray([1. * j**2 for j in range(m+1)])
  1717. yi = [np.random.random(k) for j in range(m+1)]
  1718. return xi, yi
  1719. def test_random_12(self):
  1720. m, k = 5, 12
  1721. xi, yi = self._make_random_mk(m, k)
  1722. pp = BPoly.from_derivatives(xi, yi)
  1723. for order in range(k//2):
  1724. assert_allclose(pp(xi), [yy[order] for yy in yi])
  1725. pp = pp.derivative()
  1726. def test_order_zero(self):
  1727. m, k = 5, 12
  1728. xi, yi = self._make_random_mk(m, k)
  1729. assert_raises(ValueError, BPoly.from_derivatives,
  1730. **dict(xi=xi, yi=yi, orders=0))
  1731. def test_orders_too_high(self):
  1732. m, k = 5, 12
  1733. xi, yi = self._make_random_mk(m, k)
  1734. BPoly.from_derivatives(xi, yi, orders=2*k-1) # this is still ok
  1735. assert_raises(ValueError, BPoly.from_derivatives, # but this is not
  1736. **dict(xi=xi, yi=yi, orders=2*k))
  1737. def test_orders_global(self):
  1738. m, k = 5, 12
  1739. xi, yi = self._make_random_mk(m, k)
  1740. # ok, this is confusing. Local polynomials will be of the order 5
  1741. # which means that up to the 2nd derivatives will be used at each point
  1742. order = 5
  1743. pp = BPoly.from_derivatives(xi, yi, orders=order)
  1744. for j in range(order//2+1):
  1745. assert_allclose(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12))
  1746. pp = pp.derivative()
  1747. assert_(not np.allclose(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12)))
  1748. # now repeat with `order` being even: on each interval, it uses
  1749. # order//2 'derivatives' @ the right-hand endpoint and
  1750. # order//2+1 @ 'derivatives' the left-hand endpoint
  1751. order = 6
  1752. pp = BPoly.from_derivatives(xi, yi, orders=order)
  1753. for j in range(order//2):
  1754. assert_allclose(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12))
  1755. pp = pp.derivative()
  1756. assert_(not np.allclose(pp(xi[1:-1] - 1e-12), pp(xi[1:-1] + 1e-12)))
  1757. def test_orders_local(self):
  1758. m, k = 7, 12
  1759. xi, yi = self._make_random_mk(m, k)
  1760. orders = [o + 1 for o in range(m)]
  1761. for i, x in enumerate(xi[1:-1]):
  1762. pp = BPoly.from_derivatives(xi, yi, orders=orders)
  1763. for j in range(orders[i] // 2 + 1):
  1764. assert_allclose(pp(x - 1e-12), pp(x + 1e-12))
  1765. pp = pp.derivative()
  1766. assert_(not np.allclose(pp(x - 1e-12), pp(x + 1e-12)))
  1767. def test_yi_trailing_dims(self):
  1768. m, k = 7, 5
  1769. xi = np.sort(np.random.random(m+1))
  1770. yi = np.random.random((m+1, k, 6, 7, 8))
  1771. pp = BPoly.from_derivatives(xi, yi)
  1772. assert_equal(pp.c.shape, (2*k, m, 6, 7, 8))
  1773. def test_gh_5430(self):
  1774. # At least one of these raises an error unless gh-5430 is
  1775. # fixed. In py2k an int is implemented using a C long, so
  1776. # which one fails depends on your system. In py3k there is only
  1777. # one arbitrary precision integer type, so both should fail.
  1778. orders = np.int32(1)
  1779. p = BPoly.from_derivatives([0, 1], [[0], [0]], orders=orders)
  1780. assert_almost_equal(p(0), 0)
  1781. orders = np.int64(1)
  1782. p = BPoly.from_derivatives([0, 1], [[0], [0]], orders=orders)
  1783. assert_almost_equal(p(0), 0)
  1784. orders = 1
  1785. # This worked before; make sure it still works
  1786. p = BPoly.from_derivatives([0, 1], [[0], [0]], orders=orders)
  1787. assert_almost_equal(p(0), 0)
  1788. orders = 1
  1789. class TestNdPPoly:
  1790. def test_simple_1d(self):
  1791. np.random.seed(1234)
  1792. c = np.random.rand(4, 5)
  1793. x = np.linspace(0, 1, 5+1)
  1794. xi = np.random.rand(200)
  1795. p = NdPPoly(c, (x,))
  1796. v1 = p((xi,))
  1797. v2 = _ppoly_eval_1(c[:,:,None], x, xi).ravel()
  1798. assert_allclose(v1, v2)
  1799. def test_simple_2d(self):
  1800. np.random.seed(1234)
  1801. c = np.random.rand(4, 5, 6, 7)
  1802. x = np.linspace(0, 1, 6+1)
  1803. y = np.linspace(0, 1, 7+1)**2
  1804. xi = np.random.rand(200)
  1805. yi = np.random.rand(200)
  1806. v1 = np.empty([len(xi), 1], dtype=c.dtype)
  1807. v1.fill(np.nan)
  1808. _ppoly.evaluate_nd(c.reshape(4*5, 6*7, 1),
  1809. (x, y),
  1810. np.array([4, 5], dtype=np.intc),
  1811. np.c_[xi, yi],
  1812. np.array([0, 0], dtype=np.intc),
  1813. 1,
  1814. v1)
  1815. v1 = v1.ravel()
  1816. v2 = _ppoly2d_eval(c, (x, y), xi, yi)
  1817. assert_allclose(v1, v2)
  1818. p = NdPPoly(c, (x, y))
  1819. for nu in (None, (0, 0), (0, 1), (1, 0), (2, 3), (9, 2)):
  1820. v1 = p(np.c_[xi, yi], nu=nu)
  1821. v2 = _ppoly2d_eval(c, (x, y), xi, yi, nu=nu)
  1822. assert_allclose(v1, v2, err_msg=repr(nu))
  1823. def test_simple_3d(self):
  1824. np.random.seed(1234)
  1825. c = np.random.rand(4, 5, 6, 7, 8, 9)
  1826. x = np.linspace(0, 1, 7+1)
  1827. y = np.linspace(0, 1, 8+1)**2
  1828. z = np.linspace(0, 1, 9+1)**3
  1829. xi = np.random.rand(40)
  1830. yi = np.random.rand(40)
  1831. zi = np.random.rand(40)
  1832. p = NdPPoly(c, (x, y, z))
  1833. for nu in (None, (0, 0, 0), (0, 1, 0), (1, 0, 0), (2, 3, 0),
  1834. (6, 0, 2)):
  1835. v1 = p((xi, yi, zi), nu=nu)
  1836. v2 = _ppoly3d_eval(c, (x, y, z), xi, yi, zi, nu=nu)
  1837. assert_allclose(v1, v2, err_msg=repr(nu))
  1838. def test_simple_4d(self):
  1839. np.random.seed(1234)
  1840. c = np.random.rand(4, 5, 6, 7, 8, 9, 10, 11)
  1841. x = np.linspace(0, 1, 8+1)
  1842. y = np.linspace(0, 1, 9+1)**2
  1843. z = np.linspace(0, 1, 10+1)**3
  1844. u = np.linspace(0, 1, 11+1)**4
  1845. xi = np.random.rand(20)
  1846. yi = np.random.rand(20)
  1847. zi = np.random.rand(20)
  1848. ui = np.random.rand(20)
  1849. p = NdPPoly(c, (x, y, z, u))
  1850. v1 = p((xi, yi, zi, ui))
  1851. v2 = _ppoly4d_eval(c, (x, y, z, u), xi, yi, zi, ui)
  1852. assert_allclose(v1, v2)
  1853. def test_deriv_1d(self):
  1854. np.random.seed(1234)
  1855. c = np.random.rand(4, 5)
  1856. x = np.linspace(0, 1, 5+1)
  1857. p = NdPPoly(c, (x,))
  1858. # derivative
  1859. dp = p.derivative(nu=[1])
  1860. p1 = PPoly(c, x)
  1861. dp1 = p1.derivative()
  1862. assert_allclose(dp.c, dp1.c)
  1863. # antiderivative
  1864. dp = p.antiderivative(nu=[2])
  1865. p1 = PPoly(c, x)
  1866. dp1 = p1.antiderivative(2)
  1867. assert_allclose(dp.c, dp1.c)
  1868. def test_deriv_3d(self):
  1869. np.random.seed(1234)
  1870. c = np.random.rand(4, 5, 6, 7, 8, 9)
  1871. x = np.linspace(0, 1, 7+1)
  1872. y = np.linspace(0, 1, 8+1)**2
  1873. z = np.linspace(0, 1, 9+1)**3
  1874. p = NdPPoly(c, (x, y, z))
  1875. # differentiate vs x
  1876. p1 = PPoly(c.transpose(0, 3, 1, 2, 4, 5), x)
  1877. dp = p.derivative(nu=[2])
  1878. dp1 = p1.derivative(2)
  1879. assert_allclose(dp.c,
  1880. dp1.c.transpose(0, 2, 3, 1, 4, 5))
  1881. # antidifferentiate vs y
  1882. p1 = PPoly(c.transpose(1, 4, 0, 2, 3, 5), y)
  1883. dp = p.antiderivative(nu=[0, 1, 0])
  1884. dp1 = p1.antiderivative(1)
  1885. assert_allclose(dp.c,
  1886. dp1.c.transpose(2, 0, 3, 4, 1, 5))
  1887. # differentiate vs z
  1888. p1 = PPoly(c.transpose(2, 5, 0, 1, 3, 4), z)
  1889. dp = p.derivative(nu=[0, 0, 3])
  1890. dp1 = p1.derivative(3)
  1891. assert_allclose(dp.c,
  1892. dp1.c.transpose(2, 3, 0, 4, 5, 1))
  1893. def test_deriv_3d_simple(self):
  1894. # Integrate to obtain function x y**2 z**4 / (2! 4!)
  1895. c = np.ones((1, 1, 1, 3, 4, 5))
  1896. x = np.linspace(0, 1, 3+1)**1
  1897. y = np.linspace(0, 1, 4+1)**2
  1898. z = np.linspace(0, 1, 5+1)**3
  1899. p = NdPPoly(c, (x, y, z))
  1900. ip = p.antiderivative((1, 0, 4))
  1901. ip = ip.antiderivative((0, 2, 0))
  1902. xi = np.random.rand(20)
  1903. yi = np.random.rand(20)
  1904. zi = np.random.rand(20)
  1905. assert_allclose(ip((xi, yi, zi)),
  1906. xi * yi**2 * zi**4 / (gamma(3)*gamma(5)))
  1907. def test_integrate_2d(self):
  1908. np.random.seed(1234)
  1909. c = np.random.rand(4, 5, 16, 17)
  1910. x = np.linspace(0, 1, 16+1)**1
  1911. y = np.linspace(0, 1, 17+1)**2
  1912. # make continuously differentiable so that nquad() has an
  1913. # easier time
  1914. c = c.transpose(0, 2, 1, 3)
  1915. cx = c.reshape(c.shape[0], c.shape[1], -1).copy()
  1916. _ppoly.fix_continuity(cx, x, 2)
  1917. c = cx.reshape(c.shape)
  1918. c = c.transpose(0, 2, 1, 3)
  1919. c = c.transpose(1, 3, 0, 2)
  1920. cx = c.reshape(c.shape[0], c.shape[1], -1).copy()
  1921. _ppoly.fix_continuity(cx, y, 2)
  1922. c = cx.reshape(c.shape)
  1923. c = c.transpose(2, 0, 3, 1).copy()
  1924. # Check integration
  1925. p = NdPPoly(c, (x, y))
  1926. for ranges in [[(0, 1), (0, 1)],
  1927. [(0, 0.5), (0, 1)],
  1928. [(0, 1), (0, 0.5)],
  1929. [(0.3, 0.7), (0.6, 0.2)]]:
  1930. ig = p.integrate(ranges)
  1931. ig2, err2 = nquad(lambda x, y: p((x, y)), ranges,
  1932. opts=[dict(epsrel=1e-5, epsabs=1e-5)]*2)
  1933. assert_allclose(ig, ig2, rtol=1e-5, atol=1e-5,
  1934. err_msg=repr(ranges))
  1935. def test_integrate_1d(self):
  1936. np.random.seed(1234)
  1937. c = np.random.rand(4, 5, 6, 16, 17, 18)
  1938. x = np.linspace(0, 1, 16+1)**1
  1939. y = np.linspace(0, 1, 17+1)**2
  1940. z = np.linspace(0, 1, 18+1)**3
  1941. # Check 1-D integration
  1942. p = NdPPoly(c, (x, y, z))
  1943. u = np.random.rand(200)
  1944. v = np.random.rand(200)
  1945. a, b = 0.2, 0.7
  1946. px = p.integrate_1d(a, b, axis=0)
  1947. pax = p.antiderivative((1, 0, 0))
  1948. assert_allclose(px((u, v)), pax((b, u, v)) - pax((a, u, v)))
  1949. py = p.integrate_1d(a, b, axis=1)
  1950. pay = p.antiderivative((0, 1, 0))
  1951. assert_allclose(py((u, v)), pay((u, b, v)) - pay((u, a, v)))
  1952. pz = p.integrate_1d(a, b, axis=2)
  1953. paz = p.antiderivative((0, 0, 1))
  1954. assert_allclose(pz((u, v)), paz((u, v, b)) - paz((u, v, a)))
  1955. def _ppoly_eval_1(c, x, xps):
  1956. """Evaluate piecewise polynomial manually"""
  1957. out = np.zeros((len(xps), c.shape[2]))
  1958. for i, xp in enumerate(xps):
  1959. if xp < 0 or xp > 1:
  1960. out[i,:] = np.nan
  1961. continue
  1962. j = np.searchsorted(x, xp) - 1
  1963. d = xp - x[j]
  1964. assert_(x[j] <= xp < x[j+1])
  1965. r = sum(c[k,j] * d**(c.shape[0]-k-1)
  1966. for k in range(c.shape[0]))
  1967. out[i,:] = r
  1968. return out
  1969. def _ppoly_eval_2(coeffs, breaks, xnew, fill=np.nan):
  1970. """Evaluate piecewise polynomial manually (another way)"""
  1971. a = breaks[0]
  1972. b = breaks[-1]
  1973. K = coeffs.shape[0]
  1974. saveshape = np.shape(xnew)
  1975. xnew = np.ravel(xnew)
  1976. res = np.empty_like(xnew)
  1977. mask = (xnew >= a) & (xnew <= b)
  1978. res[~mask] = fill
  1979. xx = xnew.compress(mask)
  1980. indxs = np.searchsorted(breaks, xx)-1
  1981. indxs = indxs.clip(0, len(breaks))
  1982. pp = coeffs
  1983. diff = xx - breaks.take(indxs)
  1984. V = np.vander(diff, N=K)
  1985. values = np.array([np.dot(V[k, :], pp[:, indxs[k]]) for k in range(len(xx))])
  1986. res[mask] = values
  1987. res.shape = saveshape
  1988. return res
  1989. def _dpow(x, y, n):
  1990. """
  1991. d^n (x**y) / dx^n
  1992. """
  1993. if n < 0:
  1994. raise ValueError("invalid derivative order")
  1995. elif n > y:
  1996. return 0
  1997. else:
  1998. return poch(y - n + 1, n) * x**(y - n)
  1999. def _ppoly2d_eval(c, xs, xnew, ynew, nu=None):
  2000. """
  2001. Straightforward evaluation of 2-D piecewise polynomial
  2002. """
  2003. if nu is None:
  2004. nu = (0, 0)
  2005. out = np.empty((len(xnew),), dtype=c.dtype)
  2006. nx, ny = c.shape[:2]
  2007. for jout, (x, y) in enumerate(zip(xnew, ynew)):
  2008. if not ((xs[0][0] <= x <= xs[0][-1]) and
  2009. (xs[1][0] <= y <= xs[1][-1])):
  2010. out[jout] = np.nan
  2011. continue
  2012. j1 = np.searchsorted(xs[0], x) - 1
  2013. j2 = np.searchsorted(xs[1], y) - 1
  2014. s1 = x - xs[0][j1]
  2015. s2 = y - xs[1][j2]
  2016. val = 0
  2017. for k1 in range(c.shape[0]):
  2018. for k2 in range(c.shape[1]):
  2019. val += (c[nx-k1-1,ny-k2-1,j1,j2]
  2020. * _dpow(s1, k1, nu[0])
  2021. * _dpow(s2, k2, nu[1]))
  2022. out[jout] = val
  2023. return out
  2024. def _ppoly3d_eval(c, xs, xnew, ynew, znew, nu=None):
  2025. """
  2026. Straightforward evaluation of 3-D piecewise polynomial
  2027. """
  2028. if nu is None:
  2029. nu = (0, 0, 0)
  2030. out = np.empty((len(xnew),), dtype=c.dtype)
  2031. nx, ny, nz = c.shape[:3]
  2032. for jout, (x, y, z) in enumerate(zip(xnew, ynew, znew)):
  2033. if not ((xs[0][0] <= x <= xs[0][-1]) and
  2034. (xs[1][0] <= y <= xs[1][-1]) and
  2035. (xs[2][0] <= z <= xs[2][-1])):
  2036. out[jout] = np.nan
  2037. continue
  2038. j1 = np.searchsorted(xs[0], x) - 1
  2039. j2 = np.searchsorted(xs[1], y) - 1
  2040. j3 = np.searchsorted(xs[2], z) - 1
  2041. s1 = x - xs[0][j1]
  2042. s2 = y - xs[1][j2]
  2043. s3 = z - xs[2][j3]
  2044. val = 0
  2045. for k1 in range(c.shape[0]):
  2046. for k2 in range(c.shape[1]):
  2047. for k3 in range(c.shape[2]):
  2048. val += (c[nx-k1-1,ny-k2-1,nz-k3-1,j1,j2,j3]
  2049. * _dpow(s1, k1, nu[0])
  2050. * _dpow(s2, k2, nu[1])
  2051. * _dpow(s3, k3, nu[2]))
  2052. out[jout] = val
  2053. return out
  2054. def _ppoly4d_eval(c, xs, xnew, ynew, znew, unew, nu=None):
  2055. """
  2056. Straightforward evaluation of 4-D piecewise polynomial
  2057. """
  2058. if nu is None:
  2059. nu = (0, 0, 0, 0)
  2060. out = np.empty((len(xnew),), dtype=c.dtype)
  2061. mx, my, mz, mu = c.shape[:4]
  2062. for jout, (x, y, z, u) in enumerate(zip(xnew, ynew, znew, unew)):
  2063. if not ((xs[0][0] <= x <= xs[0][-1]) and
  2064. (xs[1][0] <= y <= xs[1][-1]) and
  2065. (xs[2][0] <= z <= xs[2][-1]) and
  2066. (xs[3][0] <= u <= xs[3][-1])):
  2067. out[jout] = np.nan
  2068. continue
  2069. j1 = np.searchsorted(xs[0], x) - 1
  2070. j2 = np.searchsorted(xs[1], y) - 1
  2071. j3 = np.searchsorted(xs[2], z) - 1
  2072. j4 = np.searchsorted(xs[3], u) - 1
  2073. s1 = x - xs[0][j1]
  2074. s2 = y - xs[1][j2]
  2075. s3 = z - xs[2][j3]
  2076. s4 = u - xs[3][j4]
  2077. val = 0
  2078. for k1 in range(c.shape[0]):
  2079. for k2 in range(c.shape[1]):
  2080. for k3 in range(c.shape[2]):
  2081. for k4 in range(c.shape[3]):
  2082. val += (c[mx-k1-1,my-k2-1,mz-k3-1,mu-k4-1,j1,j2,j3,j4]
  2083. * _dpow(s1, k1, nu[0])
  2084. * _dpow(s2, k2, nu[1])
  2085. * _dpow(s3, k3, nu[2])
  2086. * _dpow(s4, k4, nu[3]))
  2087. out[jout] = val
  2088. return out