test_basic.py 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656
  1. # this program corresponds to special.py
  2. ### Means test is not done yet
  3. # E Means test is giving error (E)
  4. # F Means test is failing (F)
  5. # EF Means test is giving error and Failing
  6. #! Means test is segfaulting
  7. # 8 Means test runs forever
  8. ### test_besselpoly
  9. ### test_mathieu_a
  10. ### test_mathieu_even_coef
  11. ### test_mathieu_odd_coef
  12. ### test_modfresnelp
  13. ### test_modfresnelm
  14. # test_pbdv_seq
  15. ### test_pbvv_seq
  16. ### test_sph_harm
  17. import itertools
  18. import platform
  19. import sys
  20. import numpy as np
  21. from numpy import (array, isnan, r_, arange, finfo, pi, sin, cos, tan, exp,
  22. log, zeros, sqrt, asarray, inf, nan_to_num, real, arctan, float_)
  23. import pytest
  24. from pytest import raises as assert_raises
  25. from numpy.testing import (assert_equal, assert_almost_equal,
  26. assert_array_equal, assert_array_almost_equal, assert_approx_equal,
  27. assert_, assert_allclose, assert_array_almost_equal_nulp,
  28. suppress_warnings)
  29. from scipy import special
  30. import scipy.special._ufuncs as cephes
  31. from scipy.special import ellipe, ellipk, ellipkm1
  32. from scipy.special import elliprc, elliprd, elliprf, elliprg, elliprj
  33. from scipy.special import mathieu_odd_coef, mathieu_even_coef
  34. from scipy.special._testutils import with_special_errors, \
  35. assert_func_equal, FuncData
  36. import math
  37. class TestCephes:
  38. def test_airy(self):
  39. cephes.airy(0)
  40. def test_airye(self):
  41. cephes.airye(0)
  42. def test_binom(self):
  43. n = np.array([0.264, 4, 5.2, 17])
  44. k = np.array([2, 0.4, 7, 3.3])
  45. nk = np.array(np.broadcast_arrays(n[:,None], k[None,:])
  46. ).reshape(2, -1).T
  47. rknown = np.array([[-0.097152, 0.9263051596159367, 0.01858423645695389,
  48. -0.007581020651518199],[6, 2.0214389119675666, 0, 2.9827344527963846],
  49. [10.92, 2.22993515861399, -0.00585728, 10.468891352063146],
  50. [136, 3.5252179590758828, 19448, 1024.5526916174495]])
  51. assert_func_equal(cephes.binom, rknown.ravel(), nk, rtol=1e-13)
  52. # Test branches in implementation
  53. np.random.seed(1234)
  54. n = np.r_[np.arange(-7, 30), 1000*np.random.rand(30) - 500]
  55. k = np.arange(0, 102)
  56. nk = np.array(np.broadcast_arrays(n[:,None], k[None,:])
  57. ).reshape(2, -1).T
  58. assert_func_equal(cephes.binom,
  59. cephes.binom(nk[:,0], nk[:,1] * (1 + 1e-15)),
  60. nk,
  61. atol=1e-10, rtol=1e-10)
  62. def test_binom_2(self):
  63. # Test branches in implementation
  64. np.random.seed(1234)
  65. n = np.r_[np.logspace(1, 300, 20)]
  66. k = np.arange(0, 102)
  67. nk = np.array(np.broadcast_arrays(n[:,None], k[None,:])
  68. ).reshape(2, -1).T
  69. assert_func_equal(cephes.binom,
  70. cephes.binom(nk[:,0], nk[:,1] * (1 + 1e-15)),
  71. nk,
  72. atol=1e-10, rtol=1e-10)
  73. def test_binom_exact(self):
  74. @np.vectorize
  75. def binom_int(n, k):
  76. n = int(n)
  77. k = int(k)
  78. num = int(1)
  79. den = int(1)
  80. for i in range(1, k+1):
  81. num *= i + n - k
  82. den *= i
  83. return float(num/den)
  84. np.random.seed(1234)
  85. n = np.arange(1, 15)
  86. k = np.arange(0, 15)
  87. nk = np.array(np.broadcast_arrays(n[:,None], k[None,:])
  88. ).reshape(2, -1).T
  89. nk = nk[nk[:,0] >= nk[:,1]]
  90. assert_func_equal(cephes.binom,
  91. binom_int(nk[:,0], nk[:,1]),
  92. nk,
  93. atol=0, rtol=0)
  94. def test_binom_nooverflow_8346(self):
  95. # Test (binom(n, k) doesn't overflow prematurely */
  96. dataset = [
  97. (1000, 500, 2.70288240945436551e+299),
  98. (1002, 501, 1.08007396880791225e+300),
  99. (1004, 502, 4.31599279169058121e+300),
  100. (1006, 503, 1.72468101616263781e+301),
  101. (1008, 504, 6.89188009236419153e+301),
  102. (1010, 505, 2.75402257948335448e+302),
  103. (1012, 506, 1.10052048531923757e+303),
  104. (1014, 507, 4.39774063758732849e+303),
  105. (1016, 508, 1.75736486108312519e+304),
  106. (1018, 509, 7.02255427788423734e+304),
  107. (1020, 510, 2.80626776829962255e+305),
  108. (1022, 511, 1.12140876377061240e+306),
  109. (1024, 512, 4.48125455209897109e+306),
  110. (1026, 513, 1.79075474304149900e+307),
  111. (1028, 514, 7.15605105487789676e+307)
  112. ]
  113. dataset = np.asarray(dataset)
  114. FuncData(cephes.binom, dataset, (0, 1), 2, rtol=1e-12).check()
  115. def test_bdtr(self):
  116. assert_equal(cephes.bdtr(1,1,0.5),1.0)
  117. def test_bdtri(self):
  118. assert_equal(cephes.bdtri(1,3,0.5),0.5)
  119. def test_bdtrc(self):
  120. assert_equal(cephes.bdtrc(1,3,0.5),0.5)
  121. def test_bdtrin(self):
  122. assert_equal(cephes.bdtrin(1,0,1),5.0)
  123. def test_bdtrik(self):
  124. cephes.bdtrik(1,3,0.5)
  125. def test_bei(self):
  126. assert_equal(cephes.bei(0),0.0)
  127. def test_beip(self):
  128. assert_equal(cephes.beip(0),0.0)
  129. def test_ber(self):
  130. assert_equal(cephes.ber(0),1.0)
  131. def test_berp(self):
  132. assert_equal(cephes.berp(0),0.0)
  133. def test_besselpoly(self):
  134. assert_equal(cephes.besselpoly(0,0,0),1.0)
  135. def test_beta(self):
  136. assert_equal(cephes.beta(1,1),1.0)
  137. assert_allclose(cephes.beta(-100.3, 1e-200), cephes.gamma(1e-200))
  138. assert_allclose(cephes.beta(0.0342, 171), 24.070498359873497,
  139. rtol=1e-13, atol=0)
  140. def test_betainc(self):
  141. assert_equal(cephes.betainc(1,1,1),1.0)
  142. assert_allclose(cephes.betainc(0.0342, 171, 1e-10), 0.55269916901806648)
  143. def test_betaln(self):
  144. assert_equal(cephes.betaln(1,1),0.0)
  145. assert_allclose(cephes.betaln(-100.3, 1e-200), cephes.gammaln(1e-200))
  146. assert_allclose(cephes.betaln(0.0342, 170), 3.1811881124242447,
  147. rtol=1e-14, atol=0)
  148. def test_betaincinv(self):
  149. assert_equal(cephes.betaincinv(1,1,1),1.0)
  150. assert_allclose(cephes.betaincinv(0.0342, 171, 0.25),
  151. 8.4231316935498957e-21, rtol=3e-12, atol=0)
  152. def test_beta_inf(self):
  153. assert_(np.isinf(special.beta(-1, 2)))
  154. def test_btdtr(self):
  155. assert_equal(cephes.btdtr(1,1,1),1.0)
  156. def test_btdtri(self):
  157. assert_equal(cephes.btdtri(1,1,1),1.0)
  158. def test_btdtria(self):
  159. assert_equal(cephes.btdtria(1,1,1),5.0)
  160. def test_btdtrib(self):
  161. assert_equal(cephes.btdtrib(1,1,1),5.0)
  162. def test_cbrt(self):
  163. assert_approx_equal(cephes.cbrt(1),1.0)
  164. def test_chdtr(self):
  165. assert_equal(cephes.chdtr(1,0),0.0)
  166. def test_chdtrc(self):
  167. assert_equal(cephes.chdtrc(1,0),1.0)
  168. def test_chdtri(self):
  169. assert_equal(cephes.chdtri(1,1),0.0)
  170. def test_chdtriv(self):
  171. assert_equal(cephes.chdtriv(0,0),5.0)
  172. def test_chndtr(self):
  173. assert_equal(cephes.chndtr(0,1,0),0.0)
  174. # Each row holds (x, nu, lam, expected_value)
  175. # These values were computed using Wolfram Alpha with
  176. # CDF[NoncentralChiSquareDistribution[nu, lam], x]
  177. values = np.array([
  178. [25.00, 20.0, 400, 4.1210655112396197139e-57],
  179. [25.00, 8.00, 250, 2.3988026526832425878e-29],
  180. [0.001, 8.00, 40., 5.3761806201366039084e-24],
  181. [0.010, 8.00, 40., 5.45396231055999457039e-20],
  182. [20.00, 2.00, 107, 1.39390743555819597802e-9],
  183. [22.50, 2.00, 107, 7.11803307138105870671e-9],
  184. [25.00, 2.00, 107, 3.11041244829864897313e-8],
  185. [3.000, 2.00, 1.0, 0.62064365321954362734],
  186. [350.0, 300., 10., 0.93880128006276407710],
  187. [100.0, 13.5, 10., 0.99999999650104210949],
  188. [700.0, 20.0, 400, 0.99999999925680650105],
  189. [150.0, 13.5, 10., 0.99999999999999983046],
  190. [160.0, 13.5, 10., 0.99999999999999999518], # 1.0
  191. ])
  192. cdf = cephes.chndtr(values[:, 0], values[:, 1], values[:, 2])
  193. assert_allclose(cdf, values[:, 3], rtol=1e-12)
  194. assert_almost_equal(cephes.chndtr(np.inf, np.inf, 0), 2.0)
  195. assert_almost_equal(cephes.chndtr(2, 1, np.inf), 0.0)
  196. assert_(np.isnan(cephes.chndtr(np.nan, 1, 2)))
  197. assert_(np.isnan(cephes.chndtr(5, np.nan, 2)))
  198. assert_(np.isnan(cephes.chndtr(5, 1, np.nan)))
  199. def test_chndtridf(self):
  200. assert_equal(cephes.chndtridf(0,0,1),5.0)
  201. def test_chndtrinc(self):
  202. assert_equal(cephes.chndtrinc(0,1,0),5.0)
  203. def test_chndtrix(self):
  204. assert_equal(cephes.chndtrix(0,1,0),0.0)
  205. def test_cosdg(self):
  206. assert_equal(cephes.cosdg(0),1.0)
  207. def test_cosm1(self):
  208. assert_equal(cephes.cosm1(0),0.0)
  209. def test_cotdg(self):
  210. assert_almost_equal(cephes.cotdg(45),1.0)
  211. def test_dawsn(self):
  212. assert_equal(cephes.dawsn(0),0.0)
  213. assert_allclose(cephes.dawsn(1.23), 0.50053727749081767)
  214. def test_diric(self):
  215. # Test behavior near multiples of 2pi. Regression test for issue
  216. # described in gh-4001.
  217. n_odd = [1, 5, 25]
  218. x = np.array(2*np.pi + 5e-5).astype(np.float32)
  219. assert_almost_equal(special.diric(x, n_odd), 1.0, decimal=7)
  220. x = np.array(2*np.pi + 1e-9).astype(np.float64)
  221. assert_almost_equal(special.diric(x, n_odd), 1.0, decimal=15)
  222. x = np.array(2*np.pi + 1e-15).astype(np.float64)
  223. assert_almost_equal(special.diric(x, n_odd), 1.0, decimal=15)
  224. if hasattr(np, 'float128'):
  225. # No float128 available in 32-bit numpy
  226. x = np.array(2*np.pi + 1e-12).astype(np.float128)
  227. assert_almost_equal(special.diric(x, n_odd), 1.0, decimal=19)
  228. n_even = [2, 4, 24]
  229. x = np.array(2*np.pi + 1e-9).astype(np.float64)
  230. assert_almost_equal(special.diric(x, n_even), -1.0, decimal=15)
  231. # Test at some values not near a multiple of pi
  232. x = np.arange(0.2*np.pi, 1.0*np.pi, 0.2*np.pi)
  233. octave_result = [0.872677996249965, 0.539344662916632,
  234. 0.127322003750035, -0.206011329583298]
  235. assert_almost_equal(special.diric(x, 3), octave_result, decimal=15)
  236. def test_diric_broadcasting(self):
  237. x = np.arange(5)
  238. n = np.array([1, 3, 7])
  239. assert_(special.diric(x[:, np.newaxis], n).shape == (x.size, n.size))
  240. def test_ellipe(self):
  241. assert_equal(cephes.ellipe(1),1.0)
  242. def test_ellipeinc(self):
  243. assert_equal(cephes.ellipeinc(0,1),0.0)
  244. def test_ellipj(self):
  245. cephes.ellipj(0,1)
  246. def test_ellipk(self):
  247. assert_allclose(ellipk(0), pi/2)
  248. def test_ellipkinc(self):
  249. assert_equal(cephes.ellipkinc(0,0),0.0)
  250. def test_erf(self):
  251. assert_equal(cephes.erf(0), 0.0)
  252. def test_erf_symmetry(self):
  253. x = 5.905732037710919
  254. assert_equal(cephes.erf(x) + cephes.erf(-x), 0.0)
  255. def test_erfc(self):
  256. assert_equal(cephes.erfc(0), 1.0)
  257. def test_exp10(self):
  258. assert_approx_equal(cephes.exp10(2),100.0)
  259. def test_exp2(self):
  260. assert_equal(cephes.exp2(2),4.0)
  261. def test_expm1(self):
  262. assert_equal(cephes.expm1(0),0.0)
  263. assert_equal(cephes.expm1(np.inf), np.inf)
  264. assert_equal(cephes.expm1(-np.inf), -1)
  265. assert_equal(cephes.expm1(np.nan), np.nan)
  266. def test_expm1_complex(self):
  267. expm1 = cephes.expm1
  268. assert_equal(expm1(0 + 0j), 0 + 0j)
  269. assert_equal(expm1(complex(np.inf, 0)), complex(np.inf, 0))
  270. assert_equal(expm1(complex(np.inf, 1)), complex(np.inf, np.inf))
  271. assert_equal(expm1(complex(np.inf, 2)), complex(-np.inf, np.inf))
  272. assert_equal(expm1(complex(np.inf, 4)), complex(-np.inf, -np.inf))
  273. assert_equal(expm1(complex(np.inf, 5)), complex(np.inf, -np.inf))
  274. assert_equal(expm1(complex(1, np.inf)), complex(np.nan, np.nan))
  275. assert_equal(expm1(complex(0, np.inf)), complex(np.nan, np.nan))
  276. assert_equal(expm1(complex(np.inf, np.inf)), complex(np.inf, np.nan))
  277. assert_equal(expm1(complex(-np.inf, np.inf)), complex(-1, 0))
  278. assert_equal(expm1(complex(-np.inf, np.nan)), complex(-1, 0))
  279. assert_equal(expm1(complex(np.inf, np.nan)), complex(np.inf, np.nan))
  280. assert_equal(expm1(complex(0, np.nan)), complex(np.nan, np.nan))
  281. assert_equal(expm1(complex(1, np.nan)), complex(np.nan, np.nan))
  282. assert_equal(expm1(complex(np.nan, 1)), complex(np.nan, np.nan))
  283. assert_equal(expm1(complex(np.nan, np.nan)), complex(np.nan, np.nan))
  284. @pytest.mark.xfail(reason='The real part of expm1(z) bad at these points')
  285. def test_expm1_complex_hard(self):
  286. # The real part of this function is difficult to evaluate when
  287. # z.real = -log(cos(z.imag)).
  288. y = np.array([0.1, 0.2, 0.3, 5, 11, 20])
  289. x = -np.log(np.cos(y))
  290. z = x + 1j*y
  291. # evaluate using mpmath.expm1 with dps=1000
  292. expected = np.array([-5.5507901846769623e-17+0.10033467208545054j,
  293. 2.4289354732893695e-18+0.20271003550867248j,
  294. 4.5235500262585768e-17+0.30933624960962319j,
  295. 7.8234305217489006e-17-3.3805150062465863j,
  296. -1.3685191953697676e-16-225.95084645419513j,
  297. 8.7175620481291045e-17+2.2371609442247422j])
  298. found = cephes.expm1(z)
  299. # this passes.
  300. assert_array_almost_equal_nulp(found.imag, expected.imag, 3)
  301. # this fails.
  302. assert_array_almost_equal_nulp(found.real, expected.real, 20)
  303. def test_fdtr(self):
  304. assert_equal(cephes.fdtr(1, 1, 0), 0.0)
  305. # Computed using Wolfram Alpha: CDF[FRatioDistribution[1e-6, 5], 10]
  306. assert_allclose(cephes.fdtr(1e-6, 5, 10), 0.9999940790193488,
  307. rtol=1e-12)
  308. def test_fdtrc(self):
  309. assert_equal(cephes.fdtrc(1, 1, 0), 1.0)
  310. # Computed using Wolfram Alpha:
  311. # 1 - CDF[FRatioDistribution[2, 1/10], 1e10]
  312. assert_allclose(cephes.fdtrc(2, 0.1, 1e10), 0.27223784621293512,
  313. rtol=1e-12)
  314. def test_fdtri(self):
  315. assert_allclose(cephes.fdtri(1, 1, [0.499, 0.501]),
  316. array([0.9937365, 1.00630298]), rtol=1e-6)
  317. # From Wolfram Alpha:
  318. # CDF[FRatioDistribution[1/10, 1], 3] = 0.8756751669632105666874...
  319. p = 0.8756751669632105666874
  320. assert_allclose(cephes.fdtri(0.1, 1, p), 3, rtol=1e-12)
  321. @pytest.mark.xfail(reason='Returns nan on i686.')
  322. def test_fdtri_mysterious_failure(self):
  323. assert_allclose(cephes.fdtri(1, 1, 0.5), 1)
  324. def test_fdtridfd(self):
  325. assert_equal(cephes.fdtridfd(1,0,0),5.0)
  326. def test_fresnel(self):
  327. assert_equal(cephes.fresnel(0),(0.0,0.0))
  328. def test_gamma(self):
  329. assert_equal(cephes.gamma(5),24.0)
  330. def test_gammainccinv(self):
  331. assert_equal(cephes.gammainccinv(5,1),0.0)
  332. def test_gammaln(self):
  333. cephes.gammaln(10)
  334. def test_gammasgn(self):
  335. vals = np.array([-4, -3.5, -2.3, 1, 4.2], np.float64)
  336. assert_array_equal(cephes.gammasgn(vals), np.sign(cephes.rgamma(vals)))
  337. def test_gdtr(self):
  338. assert_equal(cephes.gdtr(1,1,0),0.0)
  339. def test_gdtr_inf(self):
  340. assert_equal(cephes.gdtr(1,1,np.inf),1.0)
  341. def test_gdtrc(self):
  342. assert_equal(cephes.gdtrc(1,1,0),1.0)
  343. def test_gdtria(self):
  344. assert_equal(cephes.gdtria(0,1,1),0.0)
  345. def test_gdtrib(self):
  346. cephes.gdtrib(1,0,1)
  347. # assert_equal(cephes.gdtrib(1,0,1),5.0)
  348. def test_gdtrix(self):
  349. cephes.gdtrix(1,1,.1)
  350. def test_hankel1(self):
  351. cephes.hankel1(1,1)
  352. def test_hankel1e(self):
  353. cephes.hankel1e(1,1)
  354. def test_hankel2(self):
  355. cephes.hankel2(1,1)
  356. def test_hankel2e(self):
  357. cephes.hankel2e(1,1)
  358. def test_hyp1f1(self):
  359. assert_approx_equal(cephes.hyp1f1(1,1,1), exp(1.0))
  360. assert_approx_equal(cephes.hyp1f1(3,4,-6), 0.026056422099537251095)
  361. cephes.hyp1f1(1,1,1)
  362. def test_hyp2f1(self):
  363. assert_equal(cephes.hyp2f1(1,1,1,0),1.0)
  364. def test_i0(self):
  365. assert_equal(cephes.i0(0),1.0)
  366. def test_i0e(self):
  367. assert_equal(cephes.i0e(0),1.0)
  368. def test_i1(self):
  369. assert_equal(cephes.i1(0),0.0)
  370. def test_i1e(self):
  371. assert_equal(cephes.i1e(0),0.0)
  372. def test_it2i0k0(self):
  373. cephes.it2i0k0(1)
  374. def test_it2j0y0(self):
  375. cephes.it2j0y0(1)
  376. def test_it2struve0(self):
  377. cephes.it2struve0(1)
  378. def test_itairy(self):
  379. cephes.itairy(1)
  380. def test_iti0k0(self):
  381. assert_equal(cephes.iti0k0(0),(0.0,0.0))
  382. def test_itj0y0(self):
  383. assert_equal(cephes.itj0y0(0),(0.0,0.0))
  384. def test_itmodstruve0(self):
  385. assert_equal(cephes.itmodstruve0(0),0.0)
  386. def test_itstruve0(self):
  387. assert_equal(cephes.itstruve0(0),0.0)
  388. def test_iv(self):
  389. assert_equal(cephes.iv(1,0),0.0)
  390. def _check_ive(self):
  391. assert_equal(cephes.ive(1,0),0.0)
  392. def test_j0(self):
  393. assert_equal(cephes.j0(0),1.0)
  394. def test_j1(self):
  395. assert_equal(cephes.j1(0),0.0)
  396. def test_jn(self):
  397. assert_equal(cephes.jn(0,0),1.0)
  398. def test_jv(self):
  399. assert_equal(cephes.jv(0,0),1.0)
  400. def _check_jve(self):
  401. assert_equal(cephes.jve(0,0),1.0)
  402. def test_k0(self):
  403. cephes.k0(2)
  404. def test_k0e(self):
  405. cephes.k0e(2)
  406. def test_k1(self):
  407. cephes.k1(2)
  408. def test_k1e(self):
  409. cephes.k1e(2)
  410. def test_kei(self):
  411. cephes.kei(2)
  412. def test_keip(self):
  413. assert_equal(cephes.keip(0),0.0)
  414. def test_ker(self):
  415. cephes.ker(2)
  416. def test_kerp(self):
  417. cephes.kerp(2)
  418. def _check_kelvin(self):
  419. cephes.kelvin(2)
  420. def test_kn(self):
  421. cephes.kn(1,1)
  422. def test_kolmogi(self):
  423. assert_equal(cephes.kolmogi(1),0.0)
  424. assert_(np.isnan(cephes.kolmogi(np.nan)))
  425. def test_kolmogorov(self):
  426. assert_equal(cephes.kolmogorov(0), 1.0)
  427. def test_kolmogp(self):
  428. assert_equal(cephes._kolmogp(0), -0.0)
  429. def test_kolmogc(self):
  430. assert_equal(cephes._kolmogc(0), 0.0)
  431. def test_kolmogci(self):
  432. assert_equal(cephes._kolmogci(0), 0.0)
  433. assert_(np.isnan(cephes._kolmogci(np.nan)))
  434. def _check_kv(self):
  435. cephes.kv(1,1)
  436. def _check_kve(self):
  437. cephes.kve(1,1)
  438. def test_log1p(self):
  439. log1p = cephes.log1p
  440. assert_equal(log1p(0), 0.0)
  441. assert_equal(log1p(-1), -np.inf)
  442. assert_equal(log1p(-2), np.nan)
  443. assert_equal(log1p(np.inf), np.inf)
  444. def test_log1p_complex(self):
  445. log1p = cephes.log1p
  446. c = complex
  447. assert_equal(log1p(0 + 0j), 0 + 0j)
  448. assert_equal(log1p(c(-1, 0)), c(-np.inf, 0))
  449. with suppress_warnings() as sup:
  450. sup.filter(RuntimeWarning, "invalid value encountered in multiply")
  451. assert_allclose(log1p(c(1, np.inf)), c(np.inf, np.pi/2))
  452. assert_equal(log1p(c(1, np.nan)), c(np.nan, np.nan))
  453. assert_allclose(log1p(c(-np.inf, 1)), c(np.inf, np.pi))
  454. assert_equal(log1p(c(np.inf, 1)), c(np.inf, 0))
  455. assert_allclose(log1p(c(-np.inf, np.inf)), c(np.inf, 3*np.pi/4))
  456. assert_allclose(log1p(c(np.inf, np.inf)), c(np.inf, np.pi/4))
  457. assert_equal(log1p(c(np.inf, np.nan)), c(np.inf, np.nan))
  458. assert_equal(log1p(c(-np.inf, np.nan)), c(np.inf, np.nan))
  459. assert_equal(log1p(c(np.nan, np.inf)), c(np.inf, np.nan))
  460. assert_equal(log1p(c(np.nan, 1)), c(np.nan, np.nan))
  461. assert_equal(log1p(c(np.nan, np.nan)), c(np.nan, np.nan))
  462. def test_lpmv(self):
  463. assert_equal(cephes.lpmv(0,0,1),1.0)
  464. def test_mathieu_a(self):
  465. assert_equal(cephes.mathieu_a(1,0),1.0)
  466. def test_mathieu_b(self):
  467. assert_equal(cephes.mathieu_b(1,0),1.0)
  468. def test_mathieu_cem(self):
  469. assert_equal(cephes.mathieu_cem(1,0,0),(1.0,0.0))
  470. # Test AMS 20.2.27
  471. @np.vectorize
  472. def ce_smallq(m, q, z):
  473. z *= np.pi/180
  474. if m == 0:
  475. return 2**(-0.5) * (1 - .5*q*cos(2*z)) # + O(q^2)
  476. elif m == 1:
  477. return cos(z) - q/8 * cos(3*z) # + O(q^2)
  478. elif m == 2:
  479. return cos(2*z) - q*(cos(4*z)/12 - 1/4) # + O(q^2)
  480. else:
  481. return cos(m*z) - q*(cos((m+2)*z)/(4*(m+1)) - cos((m-2)*z)/(4*(m-1))) # + O(q^2)
  482. m = np.arange(0, 100)
  483. q = np.r_[0, np.logspace(-30, -9, 10)]
  484. assert_allclose(cephes.mathieu_cem(m[:,None], q[None,:], 0.123)[0],
  485. ce_smallq(m[:,None], q[None,:], 0.123),
  486. rtol=1e-14, atol=0)
  487. def test_mathieu_sem(self):
  488. assert_equal(cephes.mathieu_sem(1,0,0),(0.0,1.0))
  489. # Test AMS 20.2.27
  490. @np.vectorize
  491. def se_smallq(m, q, z):
  492. z *= np.pi/180
  493. if m == 1:
  494. return sin(z) - q/8 * sin(3*z) # + O(q^2)
  495. elif m == 2:
  496. return sin(2*z) - q*sin(4*z)/12 # + O(q^2)
  497. else:
  498. return sin(m*z) - q*(sin((m+2)*z)/(4*(m+1)) - sin((m-2)*z)/(4*(m-1))) # + O(q^2)
  499. m = np.arange(1, 100)
  500. q = np.r_[0, np.logspace(-30, -9, 10)]
  501. assert_allclose(cephes.mathieu_sem(m[:,None], q[None,:], 0.123)[0],
  502. se_smallq(m[:,None], q[None,:], 0.123),
  503. rtol=1e-14, atol=0)
  504. def test_mathieu_modcem1(self):
  505. assert_equal(cephes.mathieu_modcem1(1,0,0),(0.0,0.0))
  506. def test_mathieu_modcem2(self):
  507. cephes.mathieu_modcem2(1,1,1)
  508. # Test reflection relation AMS 20.6.19
  509. m = np.arange(0, 4)[:,None,None]
  510. q = np.r_[np.logspace(-2, 2, 10)][None,:,None]
  511. z = np.linspace(0, 1, 7)[None,None,:]
  512. y1 = cephes.mathieu_modcem2(m, q, -z)[0]
  513. fr = -cephes.mathieu_modcem2(m, q, 0)[0] / cephes.mathieu_modcem1(m, q, 0)[0]
  514. y2 = -cephes.mathieu_modcem2(m, q, z)[0] - 2*fr*cephes.mathieu_modcem1(m, q, z)[0]
  515. assert_allclose(y1, y2, rtol=1e-10)
  516. def test_mathieu_modsem1(self):
  517. assert_equal(cephes.mathieu_modsem1(1,0,0),(0.0,0.0))
  518. def test_mathieu_modsem2(self):
  519. cephes.mathieu_modsem2(1,1,1)
  520. # Test reflection relation AMS 20.6.20
  521. m = np.arange(1, 4)[:,None,None]
  522. q = np.r_[np.logspace(-2, 2, 10)][None,:,None]
  523. z = np.linspace(0, 1, 7)[None,None,:]
  524. y1 = cephes.mathieu_modsem2(m, q, -z)[0]
  525. fr = cephes.mathieu_modsem2(m, q, 0)[1] / cephes.mathieu_modsem1(m, q, 0)[1]
  526. y2 = cephes.mathieu_modsem2(m, q, z)[0] - 2*fr*cephes.mathieu_modsem1(m, q, z)[0]
  527. assert_allclose(y1, y2, rtol=1e-10)
  528. def test_mathieu_overflow(self):
  529. # Check that these return NaNs instead of causing a SEGV
  530. assert_equal(cephes.mathieu_cem(10000, 0, 1.3), (np.nan, np.nan))
  531. assert_equal(cephes.mathieu_sem(10000, 0, 1.3), (np.nan, np.nan))
  532. assert_equal(cephes.mathieu_cem(10000, 1.5, 1.3), (np.nan, np.nan))
  533. assert_equal(cephes.mathieu_sem(10000, 1.5, 1.3), (np.nan, np.nan))
  534. assert_equal(cephes.mathieu_modcem1(10000, 1.5, 1.3), (np.nan, np.nan))
  535. assert_equal(cephes.mathieu_modsem1(10000, 1.5, 1.3), (np.nan, np.nan))
  536. assert_equal(cephes.mathieu_modcem2(10000, 1.5, 1.3), (np.nan, np.nan))
  537. assert_equal(cephes.mathieu_modsem2(10000, 1.5, 1.3), (np.nan, np.nan))
  538. def test_mathieu_ticket_1847(self):
  539. # Regression test --- this call had some out-of-bounds access
  540. # and could return nan occasionally
  541. for k in range(60):
  542. v = cephes.mathieu_modsem2(2, 100, -1)
  543. # Values from ACM TOMS 804 (derivate by numerical differentiation)
  544. assert_allclose(v[0], 0.1431742913063671074347, rtol=1e-10)
  545. assert_allclose(v[1], 0.9017807375832909144719, rtol=1e-4)
  546. def test_modfresnelm(self):
  547. cephes.modfresnelm(0)
  548. def test_modfresnelp(self):
  549. cephes.modfresnelp(0)
  550. def _check_modstruve(self):
  551. assert_equal(cephes.modstruve(1,0),0.0)
  552. def test_nbdtr(self):
  553. assert_equal(cephes.nbdtr(1,1,1),1.0)
  554. def test_nbdtrc(self):
  555. assert_equal(cephes.nbdtrc(1,1,1),0.0)
  556. def test_nbdtri(self):
  557. assert_equal(cephes.nbdtri(1,1,1),1.0)
  558. def __check_nbdtrik(self):
  559. cephes.nbdtrik(1,.4,.5)
  560. def test_nbdtrin(self):
  561. assert_equal(cephes.nbdtrin(1,0,0),5.0)
  562. def test_ncfdtr(self):
  563. assert_equal(cephes.ncfdtr(1,1,1,0),0.0)
  564. def test_ncfdtri(self):
  565. assert_equal(cephes.ncfdtri(1, 1, 1, 0), 0.0)
  566. f = [0.5, 1, 1.5]
  567. p = cephes.ncfdtr(2, 3, 1.5, f)
  568. assert_allclose(cephes.ncfdtri(2, 3, 1.5, p), f)
  569. def test_ncfdtridfd(self):
  570. dfd = [1, 2, 3]
  571. p = cephes.ncfdtr(2, dfd, 0.25, 15)
  572. assert_allclose(cephes.ncfdtridfd(2, p, 0.25, 15), dfd)
  573. def test_ncfdtridfn(self):
  574. dfn = [0.1, 1, 2, 3, 1e4]
  575. p = cephes.ncfdtr(dfn, 2, 0.25, 15)
  576. assert_allclose(cephes.ncfdtridfn(p, 2, 0.25, 15), dfn, rtol=1e-5)
  577. def test_ncfdtrinc(self):
  578. nc = [0.5, 1.5, 2.0]
  579. p = cephes.ncfdtr(2, 3, nc, 15)
  580. assert_allclose(cephes.ncfdtrinc(2, 3, p, 15), nc)
  581. def test_nctdtr(self):
  582. assert_equal(cephes.nctdtr(1,0,0),0.5)
  583. assert_equal(cephes.nctdtr(9, 65536, 45), 0.0)
  584. assert_approx_equal(cephes.nctdtr(np.inf, 1., 1.), 0.5, 5)
  585. assert_(np.isnan(cephes.nctdtr(2., np.inf, 10.)))
  586. assert_approx_equal(cephes.nctdtr(2., 1., np.inf), 1.)
  587. assert_(np.isnan(cephes.nctdtr(np.nan, 1., 1.)))
  588. assert_(np.isnan(cephes.nctdtr(2., np.nan, 1.)))
  589. assert_(np.isnan(cephes.nctdtr(2., 1., np.nan)))
  590. def __check_nctdtridf(self):
  591. cephes.nctdtridf(1,0.5,0)
  592. def test_nctdtrinc(self):
  593. cephes.nctdtrinc(1,0,0)
  594. def test_nctdtrit(self):
  595. cephes.nctdtrit(.1,0.2,.5)
  596. def test_nrdtrimn(self):
  597. assert_approx_equal(cephes.nrdtrimn(0.5,1,1),1.0)
  598. def test_nrdtrisd(self):
  599. assert_allclose(cephes.nrdtrisd(0.5,0.5,0.5), 0.0,
  600. atol=0, rtol=0)
  601. def test_obl_ang1(self):
  602. cephes.obl_ang1(1,1,1,0)
  603. def test_obl_ang1_cv(self):
  604. result = cephes.obl_ang1_cv(1,1,1,1,0)
  605. assert_almost_equal(result[0],1.0)
  606. assert_almost_equal(result[1],0.0)
  607. def _check_obl_cv(self):
  608. assert_equal(cephes.obl_cv(1,1,0),2.0)
  609. def test_obl_rad1(self):
  610. cephes.obl_rad1(1,1,1,0)
  611. def test_obl_rad1_cv(self):
  612. cephes.obl_rad1_cv(1,1,1,1,0)
  613. def test_obl_rad2(self):
  614. cephes.obl_rad2(1,1,1,0)
  615. def test_obl_rad2_cv(self):
  616. cephes.obl_rad2_cv(1,1,1,1,0)
  617. def test_pbdv(self):
  618. assert_equal(cephes.pbdv(1,0),(0.0,1.0))
  619. def test_pbvv(self):
  620. cephes.pbvv(1,0)
  621. def test_pbwa(self):
  622. cephes.pbwa(1,0)
  623. def test_pdtr(self):
  624. val = cephes.pdtr(0, 1)
  625. assert_almost_equal(val, np.exp(-1))
  626. # Edge case: m = 0.
  627. val = cephes.pdtr([0, 1, 2], 0)
  628. assert_array_equal(val, [1, 1, 1])
  629. def test_pdtrc(self):
  630. val = cephes.pdtrc(0, 1)
  631. assert_almost_equal(val, 1 - np.exp(-1))
  632. # Edge case: m = 0.
  633. val = cephes.pdtrc([0, 1, 2], 0.0)
  634. assert_array_equal(val, [0, 0, 0])
  635. def test_pdtri(self):
  636. with suppress_warnings() as sup:
  637. sup.filter(RuntimeWarning, "floating point number truncated to an integer")
  638. cephes.pdtri(0.5,0.5)
  639. def test_pdtrik(self):
  640. k = cephes.pdtrik(0.5, 1)
  641. assert_almost_equal(cephes.gammaincc(k + 1, 1), 0.5)
  642. # Edge case: m = 0 or very small.
  643. k = cephes.pdtrik([[0], [0.25], [0.95]], [0, 1e-20, 1e-6])
  644. assert_array_equal(k, np.zeros((3, 3)))
  645. def test_pro_ang1(self):
  646. cephes.pro_ang1(1,1,1,0)
  647. def test_pro_ang1_cv(self):
  648. assert_array_almost_equal(cephes.pro_ang1_cv(1,1,1,1,0),
  649. array((1.0,0.0)))
  650. def _check_pro_cv(self):
  651. assert_equal(cephes.pro_cv(1,1,0),2.0)
  652. def test_pro_rad1(self):
  653. cephes.pro_rad1(1,1,1,0.1)
  654. def test_pro_rad1_cv(self):
  655. cephes.pro_rad1_cv(1,1,1,1,0)
  656. def test_pro_rad2(self):
  657. cephes.pro_rad2(1,1,1,0)
  658. def test_pro_rad2_cv(self):
  659. cephes.pro_rad2_cv(1,1,1,1,0)
  660. def test_psi(self):
  661. cephes.psi(1)
  662. def test_radian(self):
  663. assert_equal(cephes.radian(0,0,0),0)
  664. def test_rgamma(self):
  665. assert_equal(cephes.rgamma(1),1.0)
  666. def test_round(self):
  667. assert_equal(cephes.round(3.4),3.0)
  668. assert_equal(cephes.round(-3.4),-3.0)
  669. assert_equal(cephes.round(3.6),4.0)
  670. assert_equal(cephes.round(-3.6),-4.0)
  671. assert_equal(cephes.round(3.5),4.0)
  672. assert_equal(cephes.round(-3.5),-4.0)
  673. def test_shichi(self):
  674. cephes.shichi(1)
  675. def test_sici(self):
  676. cephes.sici(1)
  677. s, c = cephes.sici(np.inf)
  678. assert_almost_equal(s, np.pi * 0.5)
  679. assert_almost_equal(c, 0)
  680. s, c = cephes.sici(-np.inf)
  681. assert_almost_equal(s, -np.pi * 0.5)
  682. assert_(np.isnan(c), "cosine integral(-inf) is not nan")
  683. def test_sindg(self):
  684. assert_equal(cephes.sindg(90),1.0)
  685. def test_smirnov(self):
  686. assert_equal(cephes.smirnov(1,.1),0.9)
  687. assert_(np.isnan(cephes.smirnov(1,np.nan)))
  688. def test_smirnovp(self):
  689. assert_equal(cephes._smirnovp(1, .1), -1)
  690. assert_equal(cephes._smirnovp(2, 0.75), -2*(0.25)**(2-1))
  691. assert_equal(cephes._smirnovp(3, 0.75), -3*(0.25)**(3-1))
  692. assert_(np.isnan(cephes._smirnovp(1, np.nan)))
  693. def test_smirnovc(self):
  694. assert_equal(cephes._smirnovc(1,.1),0.1)
  695. assert_(np.isnan(cephes._smirnovc(1,np.nan)))
  696. x10 = np.linspace(0, 1, 11, endpoint=True)
  697. assert_almost_equal(cephes._smirnovc(3, x10), 1-cephes.smirnov(3, x10))
  698. x4 = np.linspace(0, 1, 5, endpoint=True)
  699. assert_almost_equal(cephes._smirnovc(4, x4), 1-cephes.smirnov(4, x4))
  700. def test_smirnovi(self):
  701. assert_almost_equal(cephes.smirnov(1,cephes.smirnovi(1,0.4)),0.4)
  702. assert_almost_equal(cephes.smirnov(1,cephes.smirnovi(1,0.6)),0.6)
  703. assert_(np.isnan(cephes.smirnovi(1,np.nan)))
  704. def test_smirnovci(self):
  705. assert_almost_equal(cephes._smirnovc(1,cephes._smirnovci(1,0.4)),0.4)
  706. assert_almost_equal(cephes._smirnovc(1,cephes._smirnovci(1,0.6)),0.6)
  707. assert_(np.isnan(cephes._smirnovci(1,np.nan)))
  708. def test_spence(self):
  709. assert_equal(cephes.spence(1),0.0)
  710. def test_stdtr(self):
  711. assert_equal(cephes.stdtr(1,0),0.5)
  712. assert_almost_equal(cephes.stdtr(1,1), 0.75)
  713. assert_almost_equal(cephes.stdtr(1,2), 0.852416382349)
  714. def test_stdtridf(self):
  715. cephes.stdtridf(0.7,1)
  716. def test_stdtrit(self):
  717. cephes.stdtrit(1,0.7)
  718. def test_struve(self):
  719. assert_equal(cephes.struve(0,0),0.0)
  720. def test_tandg(self):
  721. assert_equal(cephes.tandg(45),1.0)
  722. def test_tklmbda(self):
  723. assert_almost_equal(cephes.tklmbda(1,1),1.0)
  724. def test_y0(self):
  725. cephes.y0(1)
  726. def test_y1(self):
  727. cephes.y1(1)
  728. def test_yn(self):
  729. cephes.yn(1,1)
  730. def test_yv(self):
  731. cephes.yv(1,1)
  732. def _check_yve(self):
  733. cephes.yve(1,1)
  734. def test_wofz(self):
  735. z = [complex(624.2,-0.26123), complex(-0.4,3.), complex(0.6,2.),
  736. complex(-1.,1.), complex(-1.,-9.), complex(-1.,9.),
  737. complex(-0.0000000234545,1.1234), complex(-3.,5.1),
  738. complex(-53,30.1), complex(0.0,0.12345),
  739. complex(11,1), complex(-22,-2), complex(9,-28),
  740. complex(21,-33), complex(1e5,1e5), complex(1e14,1e14)
  741. ]
  742. w = [
  743. complex(-3.78270245518980507452677445620103199303131110e-7,
  744. 0.000903861276433172057331093754199933411710053155),
  745. complex(0.1764906227004816847297495349730234591778719532788,
  746. -0.02146550539468457616788719893991501311573031095617),
  747. complex(0.2410250715772692146133539023007113781272362309451,
  748. 0.06087579663428089745895459735240964093522265589350),
  749. complex(0.30474420525691259245713884106959496013413834051768,
  750. -0.20821893820283162728743734725471561394145872072738),
  751. complex(7.317131068972378096865595229600561710140617977e34,
  752. 8.321873499714402777186848353320412813066170427e34),
  753. complex(0.0615698507236323685519612934241429530190806818395,
  754. -0.00676005783716575013073036218018565206070072304635),
  755. complex(0.3960793007699874918961319170187598400134746631,
  756. -5.593152259116644920546186222529802777409274656e-9),
  757. complex(0.08217199226739447943295069917990417630675021771804,
  758. -0.04701291087643609891018366143118110965272615832184),
  759. complex(0.00457246000350281640952328010227885008541748668738,
  760. -0.00804900791411691821818731763401840373998654987934),
  761. complex(0.8746342859608052666092782112565360755791467973338452,
  762. 0.),
  763. complex(0.00468190164965444174367477874864366058339647648741,
  764. 0.0510735563901306197993676329845149741675029197050),
  765. complex(-0.0023193175200187620902125853834909543869428763219,
  766. -0.025460054739731556004902057663500272721780776336),
  767. complex(9.11463368405637174660562096516414499772662584e304,
  768. 3.97101807145263333769664875189354358563218932e305),
  769. complex(-4.4927207857715598976165541011143706155432296e281,
  770. -2.8019591213423077494444700357168707775769028e281),
  771. complex(2.820947917809305132678577516325951485807107151e-6,
  772. 2.820947917668257736791638444590253942253354058e-6),
  773. complex(2.82094791773878143474039725787438662716372268e-15,
  774. 2.82094791773878143474039725773333923127678361e-15)
  775. ]
  776. assert_func_equal(cephes.wofz, w, z, rtol=1e-13)
  777. class TestAiry:
  778. def test_airy(self):
  779. # This tests the airy function to ensure 8 place accuracy in computation
  780. x = special.airy(.99)
  781. assert_array_almost_equal(x,array([0.13689066,-0.16050153,1.19815925,0.92046818]),8)
  782. x = special.airy(.41)
  783. assert_array_almost_equal(x,array([0.25238916,-.23480512,0.80686202,0.51053919]),8)
  784. x = special.airy(-.36)
  785. assert_array_almost_equal(x,array([0.44508477,-0.23186773,0.44939534,0.48105354]),8)
  786. def test_airye(self):
  787. a = special.airye(0.01)
  788. b = special.airy(0.01)
  789. b1 = [None]*4
  790. for n in range(2):
  791. b1[n] = b[n]*exp(2.0/3.0*0.01*sqrt(0.01))
  792. for n in range(2,4):
  793. b1[n] = b[n]*exp(-abs(real(2.0/3.0*0.01*sqrt(0.01))))
  794. assert_array_almost_equal(a,b1,6)
  795. def test_bi_zeros(self):
  796. bi = special.bi_zeros(2)
  797. bia = (array([-1.17371322, -3.2710930]),
  798. array([-2.29443968, -4.07315509]),
  799. array([-0.45494438, 0.39652284]),
  800. array([0.60195789, -0.76031014]))
  801. assert_array_almost_equal(bi,bia,4)
  802. bi = special.bi_zeros(5)
  803. assert_array_almost_equal(bi[0],array([-1.173713222709127,
  804. -3.271093302836352,
  805. -4.830737841662016,
  806. -6.169852128310251,
  807. -7.376762079367764]),11)
  808. assert_array_almost_equal(bi[1],array([-2.294439682614122,
  809. -4.073155089071828,
  810. -5.512395729663599,
  811. -6.781294445990305,
  812. -7.940178689168587]),10)
  813. assert_array_almost_equal(bi[2],array([-0.454944383639657,
  814. 0.396522836094465,
  815. -0.367969161486959,
  816. 0.349499116831805,
  817. -0.336026240133662]),11)
  818. assert_array_almost_equal(bi[3],array([0.601957887976239,
  819. -0.760310141492801,
  820. 0.836991012619261,
  821. -0.88947990142654,
  822. 0.929983638568022]),10)
  823. def test_ai_zeros(self):
  824. ai = special.ai_zeros(1)
  825. assert_array_almost_equal(ai,(array([-2.33810741]),
  826. array([-1.01879297]),
  827. array([0.5357]),
  828. array([0.7012])),4)
  829. def test_ai_zeros_big(self):
  830. z, zp, ai_zpx, aip_zx = special.ai_zeros(50000)
  831. ai_z, aip_z, _, _ = special.airy(z)
  832. ai_zp, aip_zp, _, _ = special.airy(zp)
  833. ai_envelope = 1/abs(z)**(1./4)
  834. aip_envelope = abs(zp)**(1./4)
  835. # Check values
  836. assert_allclose(ai_zpx, ai_zp, rtol=1e-10)
  837. assert_allclose(aip_zx, aip_z, rtol=1e-10)
  838. # Check they are zeros
  839. assert_allclose(ai_z/ai_envelope, 0, atol=1e-10, rtol=0)
  840. assert_allclose(aip_zp/aip_envelope, 0, atol=1e-10, rtol=0)
  841. # Check first zeros, DLMF 9.9.1
  842. assert_allclose(z[:6],
  843. [-2.3381074105, -4.0879494441, -5.5205598281,
  844. -6.7867080901, -7.9441335871, -9.0226508533], rtol=1e-10)
  845. assert_allclose(zp[:6],
  846. [-1.0187929716, -3.2481975822, -4.8200992112,
  847. -6.1633073556, -7.3721772550, -8.4884867340], rtol=1e-10)
  848. def test_bi_zeros_big(self):
  849. z, zp, bi_zpx, bip_zx = special.bi_zeros(50000)
  850. _, _, bi_z, bip_z = special.airy(z)
  851. _, _, bi_zp, bip_zp = special.airy(zp)
  852. bi_envelope = 1/abs(z)**(1./4)
  853. bip_envelope = abs(zp)**(1./4)
  854. # Check values
  855. assert_allclose(bi_zpx, bi_zp, rtol=1e-10)
  856. assert_allclose(bip_zx, bip_z, rtol=1e-10)
  857. # Check they are zeros
  858. assert_allclose(bi_z/bi_envelope, 0, atol=1e-10, rtol=0)
  859. assert_allclose(bip_zp/bip_envelope, 0, atol=1e-10, rtol=0)
  860. # Check first zeros, DLMF 9.9.2
  861. assert_allclose(z[:6],
  862. [-1.1737132227, -3.2710933028, -4.8307378417,
  863. -6.1698521283, -7.3767620794, -8.4919488465], rtol=1e-10)
  864. assert_allclose(zp[:6],
  865. [-2.2944396826, -4.0731550891, -5.5123957297,
  866. -6.7812944460, -7.9401786892, -9.0195833588], rtol=1e-10)
  867. class TestAssocLaguerre:
  868. def test_assoc_laguerre(self):
  869. a1 = special.genlaguerre(11,1)
  870. a2 = special.assoc_laguerre(.2,11,1)
  871. assert_array_almost_equal(a2,a1(.2),8)
  872. a2 = special.assoc_laguerre(1,11,1)
  873. assert_array_almost_equal(a2,a1(1),8)
  874. class TestBesselpoly:
  875. def test_besselpoly(self):
  876. pass
  877. class TestKelvin:
  878. def test_bei(self):
  879. mbei = special.bei(2)
  880. assert_almost_equal(mbei, 0.9722916273066613,5) # this may not be exact
  881. def test_beip(self):
  882. mbeip = special.beip(2)
  883. assert_almost_equal(mbeip,0.91701361338403631,5) # this may not be exact
  884. def test_ber(self):
  885. mber = special.ber(2)
  886. assert_almost_equal(mber,0.75173418271380821,5) # this may not be exact
  887. def test_berp(self):
  888. mberp = special.berp(2)
  889. assert_almost_equal(mberp,-0.49306712470943909,5) # this may not be exact
  890. def test_bei_zeros(self):
  891. # Abramowitz & Stegun, Table 9.12
  892. bi = special.bei_zeros(5)
  893. assert_array_almost_equal(bi,array([5.02622,
  894. 9.45541,
  895. 13.89349,
  896. 18.33398,
  897. 22.77544]),4)
  898. def test_beip_zeros(self):
  899. bip = special.beip_zeros(5)
  900. assert_array_almost_equal(bip,array([3.772673304934953,
  901. 8.280987849760042,
  902. 12.742147523633703,
  903. 17.193431752512542,
  904. 21.641143941167325]),8)
  905. def test_ber_zeros(self):
  906. ber = special.ber_zeros(5)
  907. assert_array_almost_equal(ber,array([2.84892,
  908. 7.23883,
  909. 11.67396,
  910. 16.11356,
  911. 20.55463]),4)
  912. def test_berp_zeros(self):
  913. brp = special.berp_zeros(5)
  914. assert_array_almost_equal(brp,array([6.03871,
  915. 10.51364,
  916. 14.96844,
  917. 19.41758,
  918. 23.86430]),4)
  919. def test_kelvin(self):
  920. mkelv = special.kelvin(2)
  921. assert_array_almost_equal(mkelv,(special.ber(2) + special.bei(2)*1j,
  922. special.ker(2) + special.kei(2)*1j,
  923. special.berp(2) + special.beip(2)*1j,
  924. special.kerp(2) + special.keip(2)*1j),8)
  925. def test_kei(self):
  926. mkei = special.kei(2)
  927. assert_almost_equal(mkei,-0.20240006776470432,5)
  928. def test_keip(self):
  929. mkeip = special.keip(2)
  930. assert_almost_equal(mkeip,0.21980790991960536,5)
  931. def test_ker(self):
  932. mker = special.ker(2)
  933. assert_almost_equal(mker,-0.041664513991509472,5)
  934. def test_kerp(self):
  935. mkerp = special.kerp(2)
  936. assert_almost_equal(mkerp,-0.10660096588105264,5)
  937. def test_kei_zeros(self):
  938. kei = special.kei_zeros(5)
  939. assert_array_almost_equal(kei,array([3.91467,
  940. 8.34422,
  941. 12.78256,
  942. 17.22314,
  943. 21.66464]),4)
  944. def test_keip_zeros(self):
  945. keip = special.keip_zeros(5)
  946. assert_array_almost_equal(keip,array([4.93181,
  947. 9.40405,
  948. 13.85827,
  949. 18.30717,
  950. 22.75379]),4)
  951. # numbers come from 9.9 of A&S pg. 381
  952. def test_kelvin_zeros(self):
  953. tmp = special.kelvin_zeros(5)
  954. berz,beiz,kerz,keiz,berpz,beipz,kerpz,keipz = tmp
  955. assert_array_almost_equal(berz,array([2.84892,
  956. 7.23883,
  957. 11.67396,
  958. 16.11356,
  959. 20.55463]),4)
  960. assert_array_almost_equal(beiz,array([5.02622,
  961. 9.45541,
  962. 13.89349,
  963. 18.33398,
  964. 22.77544]),4)
  965. assert_array_almost_equal(kerz,array([1.71854,
  966. 6.12728,
  967. 10.56294,
  968. 15.00269,
  969. 19.44382]),4)
  970. assert_array_almost_equal(keiz,array([3.91467,
  971. 8.34422,
  972. 12.78256,
  973. 17.22314,
  974. 21.66464]),4)
  975. assert_array_almost_equal(berpz,array([6.03871,
  976. 10.51364,
  977. 14.96844,
  978. 19.41758,
  979. 23.86430]),4)
  980. assert_array_almost_equal(beipz,array([3.77267,
  981. # table from 1927 had 3.77320
  982. # but this is more accurate
  983. 8.28099,
  984. 12.74215,
  985. 17.19343,
  986. 21.64114]),4)
  987. assert_array_almost_equal(kerpz,array([2.66584,
  988. 7.17212,
  989. 11.63218,
  990. 16.08312,
  991. 20.53068]),4)
  992. assert_array_almost_equal(keipz,array([4.93181,
  993. 9.40405,
  994. 13.85827,
  995. 18.30717,
  996. 22.75379]),4)
  997. def test_ker_zeros(self):
  998. ker = special.ker_zeros(5)
  999. assert_array_almost_equal(ker,array([1.71854,
  1000. 6.12728,
  1001. 10.56294,
  1002. 15.00269,
  1003. 19.44381]),4)
  1004. def test_kerp_zeros(self):
  1005. kerp = special.kerp_zeros(5)
  1006. assert_array_almost_equal(kerp,array([2.66584,
  1007. 7.17212,
  1008. 11.63218,
  1009. 16.08312,
  1010. 20.53068]),4)
  1011. class TestBernoulli:
  1012. def test_bernoulli(self):
  1013. brn = special.bernoulli(5)
  1014. assert_array_almost_equal(brn,array([1.0000,
  1015. -0.5000,
  1016. 0.1667,
  1017. 0.0000,
  1018. -0.0333,
  1019. 0.0000]),4)
  1020. class TestBeta:
  1021. def test_beta(self):
  1022. bet = special.beta(2,4)
  1023. betg = (special.gamma(2)*special.gamma(4))/special.gamma(6)
  1024. assert_almost_equal(bet,betg,8)
  1025. def test_betaln(self):
  1026. betln = special.betaln(2,4)
  1027. bet = log(abs(special.beta(2,4)))
  1028. assert_almost_equal(betln,bet,8)
  1029. def test_betainc(self):
  1030. btinc = special.betainc(1,1,.2)
  1031. assert_almost_equal(btinc,0.2,8)
  1032. def test_betaincinv(self):
  1033. y = special.betaincinv(2,4,.5)
  1034. comp = special.betainc(2,4,y)
  1035. assert_almost_equal(comp,.5,5)
  1036. class TestCombinatorics:
  1037. def test_comb(self):
  1038. assert_array_almost_equal(special.comb([10, 10], [3, 4]), [120., 210.])
  1039. assert_almost_equal(special.comb(10, 3), 120.)
  1040. assert_equal(special.comb(10, 3, exact=True), 120)
  1041. assert_equal(special.comb(10, 3, exact=True, repetition=True), 220)
  1042. assert_allclose([special.comb(20, k, exact=True) for k in range(21)],
  1043. special.comb(20, list(range(21))), atol=1e-15)
  1044. ii = np.iinfo(int).max + 1
  1045. assert_equal(special.comb(ii, ii-1, exact=True), ii)
  1046. expected = 100891344545564193334812497256
  1047. assert special.comb(100, 50, exact=True) == expected
  1048. @pytest.mark.parametrize("repetition", [True, False])
  1049. @pytest.mark.parametrize("legacy", [True, False])
  1050. @pytest.mark.parametrize("k", [3.5, 3])
  1051. @pytest.mark.parametrize("N", [4.5, 4])
  1052. def test_comb_legacy(self, N, k, legacy, repetition):
  1053. # test is only relevant for exact=True
  1054. if legacy and (N != int(N) or k != int(k)):
  1055. with pytest.warns(
  1056. DeprecationWarning,
  1057. match=r"Non-integer arguments are currently being cast to",
  1058. ):
  1059. result = special.comb(N, k, exact=True, legacy=legacy,
  1060. repetition=repetition)
  1061. else:
  1062. result = special.comb(N, k, exact=True, legacy=legacy,
  1063. repetition=repetition)
  1064. if legacy:
  1065. # for exact=True and legacy=True, cast input arguments, else don't
  1066. if repetition:
  1067. # the casting in legacy mode happens AFTER transforming N & k,
  1068. # so rounding can change (e.g. both floats, but sum to int);
  1069. # hence we need to emulate the repetition-transformation here
  1070. N, k = int(N + k - 1), int(k)
  1071. repetition = False
  1072. else:
  1073. N, k = int(N), int(k)
  1074. # expected result is the same as with exact=False
  1075. expected = special.comb(N, k, legacy=legacy, repetition=repetition)
  1076. assert_equal(result, expected)
  1077. def test_comb_with_np_int64(self):
  1078. n = 70
  1079. k = 30
  1080. np_n = np.int64(n)
  1081. np_k = np.int64(k)
  1082. res_np = special.comb(np_n, np_k, exact=True)
  1083. res_py = special.comb(n, k, exact=True)
  1084. assert res_np == res_py
  1085. def test_comb_zeros(self):
  1086. assert_equal(special.comb(2, 3, exact=True), 0)
  1087. assert_equal(special.comb(-1, 3, exact=True), 0)
  1088. assert_equal(special.comb(2, -1, exact=True), 0)
  1089. assert_equal(special.comb(2, -1, exact=False), 0)
  1090. assert_array_almost_equal(special.comb([2, -1, 2, 10], [3, 3, -1, 3]),
  1091. [0., 0., 0., 120.])
  1092. def test_perm(self):
  1093. assert_array_almost_equal(special.perm([10, 10], [3, 4]), [720., 5040.])
  1094. assert_almost_equal(special.perm(10, 3), 720.)
  1095. assert_equal(special.perm(10, 3, exact=True), 720)
  1096. def test_perm_zeros(self):
  1097. assert_equal(special.perm(2, 3, exact=True), 0)
  1098. assert_equal(special.perm(-1, 3, exact=True), 0)
  1099. assert_equal(special.perm(2, -1, exact=True), 0)
  1100. assert_equal(special.perm(2, -1, exact=False), 0)
  1101. assert_array_almost_equal(special.perm([2, -1, 2, 10], [3, 3, -1, 3]),
  1102. [0., 0., 0., 720.])
  1103. class TestTrigonometric:
  1104. def test_cbrt(self):
  1105. cb = special.cbrt(27)
  1106. cbrl = 27**(1.0/3.0)
  1107. assert_approx_equal(cb,cbrl)
  1108. def test_cbrtmore(self):
  1109. cb1 = special.cbrt(27.9)
  1110. cbrl1 = 27.9**(1.0/3.0)
  1111. assert_almost_equal(cb1,cbrl1,8)
  1112. def test_cosdg(self):
  1113. cdg = special.cosdg(90)
  1114. cdgrl = cos(pi/2.0)
  1115. assert_almost_equal(cdg,cdgrl,8)
  1116. def test_cosdgmore(self):
  1117. cdgm = special.cosdg(30)
  1118. cdgmrl = cos(pi/6.0)
  1119. assert_almost_equal(cdgm,cdgmrl,8)
  1120. def test_cosm1(self):
  1121. cs = (special.cosm1(0),special.cosm1(.3),special.cosm1(pi/10))
  1122. csrl = (cos(0)-1,cos(.3)-1,cos(pi/10)-1)
  1123. assert_array_almost_equal(cs,csrl,8)
  1124. def test_cotdg(self):
  1125. ct = special.cotdg(30)
  1126. ctrl = tan(pi/6.0)**(-1)
  1127. assert_almost_equal(ct,ctrl,8)
  1128. def test_cotdgmore(self):
  1129. ct1 = special.cotdg(45)
  1130. ctrl1 = tan(pi/4.0)**(-1)
  1131. assert_almost_equal(ct1,ctrl1,8)
  1132. def test_specialpoints(self):
  1133. assert_almost_equal(special.cotdg(45), 1.0, 14)
  1134. assert_almost_equal(special.cotdg(-45), -1.0, 14)
  1135. assert_almost_equal(special.cotdg(90), 0.0, 14)
  1136. assert_almost_equal(special.cotdg(-90), 0.0, 14)
  1137. assert_almost_equal(special.cotdg(135), -1.0, 14)
  1138. assert_almost_equal(special.cotdg(-135), 1.0, 14)
  1139. assert_almost_equal(special.cotdg(225), 1.0, 14)
  1140. assert_almost_equal(special.cotdg(-225), -1.0, 14)
  1141. assert_almost_equal(special.cotdg(270), 0.0, 14)
  1142. assert_almost_equal(special.cotdg(-270), 0.0, 14)
  1143. assert_almost_equal(special.cotdg(315), -1.0, 14)
  1144. assert_almost_equal(special.cotdg(-315), 1.0, 14)
  1145. assert_almost_equal(special.cotdg(765), 1.0, 14)
  1146. def test_sinc(self):
  1147. # the sinc implementation and more extensive sinc tests are in numpy
  1148. assert_array_equal(special.sinc([0]), 1)
  1149. assert_equal(special.sinc(0.0), 1.0)
  1150. def test_sindg(self):
  1151. sn = special.sindg(90)
  1152. assert_equal(sn,1.0)
  1153. def test_sindgmore(self):
  1154. snm = special.sindg(30)
  1155. snmrl = sin(pi/6.0)
  1156. assert_almost_equal(snm,snmrl,8)
  1157. snm1 = special.sindg(45)
  1158. snmrl1 = sin(pi/4.0)
  1159. assert_almost_equal(snm1,snmrl1,8)
  1160. class TestTandg:
  1161. def test_tandg(self):
  1162. tn = special.tandg(30)
  1163. tnrl = tan(pi/6.0)
  1164. assert_almost_equal(tn,tnrl,8)
  1165. def test_tandgmore(self):
  1166. tnm = special.tandg(45)
  1167. tnmrl = tan(pi/4.0)
  1168. assert_almost_equal(tnm,tnmrl,8)
  1169. tnm1 = special.tandg(60)
  1170. tnmrl1 = tan(pi/3.0)
  1171. assert_almost_equal(tnm1,tnmrl1,8)
  1172. def test_specialpoints(self):
  1173. assert_almost_equal(special.tandg(0), 0.0, 14)
  1174. assert_almost_equal(special.tandg(45), 1.0, 14)
  1175. assert_almost_equal(special.tandg(-45), -1.0, 14)
  1176. assert_almost_equal(special.tandg(135), -1.0, 14)
  1177. assert_almost_equal(special.tandg(-135), 1.0, 14)
  1178. assert_almost_equal(special.tandg(180), 0.0, 14)
  1179. assert_almost_equal(special.tandg(-180), 0.0, 14)
  1180. assert_almost_equal(special.tandg(225), 1.0, 14)
  1181. assert_almost_equal(special.tandg(-225), -1.0, 14)
  1182. assert_almost_equal(special.tandg(315), -1.0, 14)
  1183. assert_almost_equal(special.tandg(-315), 1.0, 14)
  1184. class TestEllip:
  1185. def test_ellipj_nan(self):
  1186. """Regression test for #912."""
  1187. special.ellipj(0.5, np.nan)
  1188. def test_ellipj(self):
  1189. el = special.ellipj(0.2,0)
  1190. rel = [sin(0.2),cos(0.2),1.0,0.20]
  1191. assert_array_almost_equal(el,rel,13)
  1192. def test_ellipk(self):
  1193. elk = special.ellipk(.2)
  1194. assert_almost_equal(elk,1.659623598610528,11)
  1195. assert_equal(special.ellipkm1(0.0), np.inf)
  1196. assert_equal(special.ellipkm1(1.0), pi/2)
  1197. assert_equal(special.ellipkm1(np.inf), 0.0)
  1198. assert_equal(special.ellipkm1(np.nan), np.nan)
  1199. assert_equal(special.ellipkm1(-1), np.nan)
  1200. assert_allclose(special.ellipk(-10), 0.7908718902387385)
  1201. def test_ellipkinc(self):
  1202. elkinc = special.ellipkinc(pi/2,.2)
  1203. elk = special.ellipk(0.2)
  1204. assert_almost_equal(elkinc,elk,15)
  1205. alpha = 20*pi/180
  1206. phi = 45*pi/180
  1207. m = sin(alpha)**2
  1208. elkinc = special.ellipkinc(phi,m)
  1209. assert_almost_equal(elkinc,0.79398143,8)
  1210. # From pg. 614 of A & S
  1211. assert_equal(special.ellipkinc(pi/2, 0.0), pi/2)
  1212. assert_equal(special.ellipkinc(pi/2, 1.0), np.inf)
  1213. assert_equal(special.ellipkinc(pi/2, -np.inf), 0.0)
  1214. assert_equal(special.ellipkinc(pi/2, np.nan), np.nan)
  1215. assert_equal(special.ellipkinc(pi/2, 2), np.nan)
  1216. assert_equal(special.ellipkinc(0, 0.5), 0.0)
  1217. assert_equal(special.ellipkinc(np.inf, 0.5), np.inf)
  1218. assert_equal(special.ellipkinc(-np.inf, 0.5), -np.inf)
  1219. assert_equal(special.ellipkinc(np.inf, np.inf), np.nan)
  1220. assert_equal(special.ellipkinc(np.inf, -np.inf), np.nan)
  1221. assert_equal(special.ellipkinc(-np.inf, -np.inf), np.nan)
  1222. assert_equal(special.ellipkinc(-np.inf, np.inf), np.nan)
  1223. assert_equal(special.ellipkinc(np.nan, 0.5), np.nan)
  1224. assert_equal(special.ellipkinc(np.nan, np.nan), np.nan)
  1225. assert_allclose(special.ellipkinc(0.38974112035318718, 1), 0.4, rtol=1e-14)
  1226. assert_allclose(special.ellipkinc(1.5707, -10), 0.79084284661724946)
  1227. def test_ellipkinc_2(self):
  1228. # Regression test for gh-3550
  1229. # ellipkinc(phi, mbad) was NaN and mvals[2:6] were twice the correct value
  1230. mbad = 0.68359375000000011
  1231. phi = 0.9272952180016123
  1232. m = np.nextafter(mbad, 0)
  1233. mvals = []
  1234. for j in range(10):
  1235. mvals.append(m)
  1236. m = np.nextafter(m, 1)
  1237. f = special.ellipkinc(phi, mvals)
  1238. assert_array_almost_equal_nulp(f, np.full_like(f, 1.0259330100195334), 1)
  1239. # this bug also appears at phi + n * pi for at least small n
  1240. f1 = special.ellipkinc(phi + pi, mvals)
  1241. assert_array_almost_equal_nulp(f1, np.full_like(f1, 5.1296650500976675), 2)
  1242. def test_ellipkinc_singular(self):
  1243. # ellipkinc(phi, 1) has closed form and is finite only for phi in (-pi/2, pi/2)
  1244. xlog = np.logspace(-300, -17, 25)
  1245. xlin = np.linspace(1e-17, 0.1, 25)
  1246. xlin2 = np.linspace(0.1, pi/2, 25, endpoint=False)
  1247. assert_allclose(special.ellipkinc(xlog, 1), np.arcsinh(np.tan(xlog)), rtol=1e14)
  1248. assert_allclose(special.ellipkinc(xlin, 1), np.arcsinh(np.tan(xlin)), rtol=1e14)
  1249. assert_allclose(special.ellipkinc(xlin2, 1), np.arcsinh(np.tan(xlin2)), rtol=1e14)
  1250. assert_equal(special.ellipkinc(np.pi/2, 1), np.inf)
  1251. assert_allclose(special.ellipkinc(-xlog, 1), np.arcsinh(np.tan(-xlog)), rtol=1e14)
  1252. assert_allclose(special.ellipkinc(-xlin, 1), np.arcsinh(np.tan(-xlin)), rtol=1e14)
  1253. assert_allclose(special.ellipkinc(-xlin2, 1), np.arcsinh(np.tan(-xlin2)), rtol=1e14)
  1254. assert_equal(special.ellipkinc(-np.pi/2, 1), np.inf)
  1255. def test_ellipe(self):
  1256. ele = special.ellipe(.2)
  1257. assert_almost_equal(ele,1.4890350580958529,8)
  1258. assert_equal(special.ellipe(0.0), pi/2)
  1259. assert_equal(special.ellipe(1.0), 1.0)
  1260. assert_equal(special.ellipe(-np.inf), np.inf)
  1261. assert_equal(special.ellipe(np.nan), np.nan)
  1262. assert_equal(special.ellipe(2), np.nan)
  1263. assert_allclose(special.ellipe(-10), 3.6391380384177689)
  1264. def test_ellipeinc(self):
  1265. eleinc = special.ellipeinc(pi/2,.2)
  1266. ele = special.ellipe(0.2)
  1267. assert_almost_equal(eleinc,ele,14)
  1268. # pg 617 of A & S
  1269. alpha, phi = 52*pi/180,35*pi/180
  1270. m = sin(alpha)**2
  1271. eleinc = special.ellipeinc(phi,m)
  1272. assert_almost_equal(eleinc, 0.58823065, 8)
  1273. assert_equal(special.ellipeinc(pi/2, 0.0), pi/2)
  1274. assert_equal(special.ellipeinc(pi/2, 1.0), 1.0)
  1275. assert_equal(special.ellipeinc(pi/2, -np.inf), np.inf)
  1276. assert_equal(special.ellipeinc(pi/2, np.nan), np.nan)
  1277. assert_equal(special.ellipeinc(pi/2, 2), np.nan)
  1278. assert_equal(special.ellipeinc(0, 0.5), 0.0)
  1279. assert_equal(special.ellipeinc(np.inf, 0.5), np.inf)
  1280. assert_equal(special.ellipeinc(-np.inf, 0.5), -np.inf)
  1281. assert_equal(special.ellipeinc(np.inf, -np.inf), np.inf)
  1282. assert_equal(special.ellipeinc(-np.inf, -np.inf), -np.inf)
  1283. assert_equal(special.ellipeinc(np.inf, np.inf), np.nan)
  1284. assert_equal(special.ellipeinc(-np.inf, np.inf), np.nan)
  1285. assert_equal(special.ellipeinc(np.nan, 0.5), np.nan)
  1286. assert_equal(special.ellipeinc(np.nan, np.nan), np.nan)
  1287. assert_allclose(special.ellipeinc(1.5707, -10), 3.6388185585822876)
  1288. def test_ellipeinc_2(self):
  1289. # Regression test for gh-3550
  1290. # ellipeinc(phi, mbad) was NaN and mvals[2:6] were twice the correct value
  1291. mbad = 0.68359375000000011
  1292. phi = 0.9272952180016123
  1293. m = np.nextafter(mbad, 0)
  1294. mvals = []
  1295. for j in range(10):
  1296. mvals.append(m)
  1297. m = np.nextafter(m, 1)
  1298. f = special.ellipeinc(phi, mvals)
  1299. assert_array_almost_equal_nulp(f, np.full_like(f, 0.84442884574781019), 2)
  1300. # this bug also appears at phi + n * pi for at least small n
  1301. f1 = special.ellipeinc(phi + pi, mvals)
  1302. assert_array_almost_equal_nulp(f1, np.full_like(f1, 3.3471442287390509), 4)
  1303. class TestEllipCarlson(object):
  1304. """Test for Carlson elliptic integrals ellipr[cdfgj].
  1305. The special values used in these tests can be found in Sec. 3 of Carlson
  1306. (1994), https://arxiv.org/abs/math/9409227
  1307. """
  1308. def test_elliprc(self):
  1309. assert_allclose(elliprc(1, 1), 1)
  1310. assert elliprc(1, inf) == 0.0
  1311. assert isnan(elliprc(1, 0))
  1312. assert elliprc(1, complex(1, inf)) == 0.0
  1313. args = array([[0.0, 0.25],
  1314. [2.25, 2.0],
  1315. [0.0, 1.0j],
  1316. [-1.0j, 1.0j],
  1317. [0.25, -2.0],
  1318. [1.0j, -1.0]])
  1319. expected_results = array([np.pi,
  1320. np.log(2.0),
  1321. 1.1107207345396 * (1.0-1.0j),
  1322. 1.2260849569072-0.34471136988768j,
  1323. np.log(2.0) / 3.0,
  1324. 0.77778596920447+0.19832484993429j])
  1325. for i, arr in enumerate(args):
  1326. assert_allclose(elliprc(*arr), expected_results[i])
  1327. def test_elliprd(self):
  1328. assert_allclose(elliprd(1, 1, 1), 1)
  1329. assert_allclose(elliprd(0, 2, 1) / 3.0, 0.59907011736779610371)
  1330. assert elliprd(1, 1, inf) == 0.0
  1331. assert np.isinf(elliprd(1, 1, 0))
  1332. assert np.isinf(elliprd(1, 1, complex(0, 0)))
  1333. assert np.isinf(elliprd(0, 1, complex(0, 0)))
  1334. assert isnan(elliprd(1, 1, -np.finfo(np.double).tiny / 2.0))
  1335. assert isnan(elliprd(1, 1, complex(-1, 0)))
  1336. args = array([[0.0, 2.0, 1.0],
  1337. [2.0, 3.0, 4.0],
  1338. [1.0j, -1.0j, 2.0],
  1339. [0.0, 1.0j, -1.0j],
  1340. [0.0, -1.0+1.0j, 1.0j],
  1341. [-2.0-1.0j, -1.0j, -1.0+1.0j]])
  1342. expected_results = array([1.7972103521034,
  1343. 0.16510527294261,
  1344. 0.65933854154220,
  1345. 1.2708196271910+2.7811120159521j,
  1346. -1.8577235439239-0.96193450888839j,
  1347. 1.8249027393704-1.2218475784827j])
  1348. for i, arr in enumerate(args):
  1349. assert_allclose(elliprd(*arr), expected_results[i])
  1350. def test_elliprf(self):
  1351. assert_allclose(elliprf(1, 1, 1), 1)
  1352. assert_allclose(elliprf(0, 1, 2), 1.31102877714605990523)
  1353. assert elliprf(1, inf, 1) == 0.0
  1354. assert np.isinf(elliprf(0, 1, 0))
  1355. assert isnan(elliprf(1, 1, -1))
  1356. assert elliprf(complex(inf), 0, 1) == 0.0
  1357. assert isnan(elliprf(1, 1, complex(-inf, 1)))
  1358. args = array([[1.0, 2.0, 0.0],
  1359. [1.0j, -1.0j, 0.0],
  1360. [0.5, 1.0, 0.0],
  1361. [-1.0+1.0j, 1.0j, 0.0],
  1362. [2.0, 3.0, 4.0],
  1363. [1.0j, -1.0j, 2.0],
  1364. [-1.0+1.0j, 1.0j, 1.0-1.0j]])
  1365. expected_results = array([1.3110287771461,
  1366. 1.8540746773014,
  1367. 1.8540746773014,
  1368. 0.79612586584234-1.2138566698365j,
  1369. 0.58408284167715,
  1370. 1.0441445654064,
  1371. 0.93912050218619-0.53296252018635j])
  1372. for i, arr in enumerate(args):
  1373. assert_allclose(elliprf(*arr), expected_results[i])
  1374. def test_elliprg(self):
  1375. assert_allclose(elliprg(1, 1, 1), 1)
  1376. assert_allclose(elliprg(0, 0, 1), 0.5)
  1377. assert_allclose(elliprg(0, 0, 0), 0)
  1378. assert np.isinf(elliprg(1, inf, 1))
  1379. assert np.isinf(elliprg(complex(inf), 1, 1))
  1380. args = array([[0.0, 16.0, 16.0],
  1381. [2.0, 3.0, 4.0],
  1382. [0.0, 1.0j, -1.0j],
  1383. [-1.0+1.0j, 1.0j, 0.0],
  1384. [-1.0j, -1.0+1.0j, 1.0j],
  1385. [0.0, 0.0796, 4.0]])
  1386. expected_results = array([np.pi,
  1387. 1.7255030280692,
  1388. 0.42360654239699,
  1389. 0.44660591677018+0.70768352357515j,
  1390. 0.36023392184473+0.40348623401722j,
  1391. 1.0284758090288])
  1392. for i, arr in enumerate(args):
  1393. assert_allclose(elliprg(*arr), expected_results[i])
  1394. def test_elliprj(self):
  1395. assert_allclose(elliprj(1, 1, 1, 1), 1)
  1396. assert elliprj(1, 1, inf, 1) == 0.0
  1397. assert isnan(elliprj(1, 0, 0, 0))
  1398. assert isnan(elliprj(-1, 1, 1, 1))
  1399. assert elliprj(1, 1, 1, inf) == 0.0
  1400. args = array([[0.0, 1.0, 2.0, 3.0],
  1401. [2.0, 3.0, 4.0, 5.0],
  1402. [2.0, 3.0, 4.0, -1.0+1.0j],
  1403. [1.0j, -1.0j, 0.0, 2.0],
  1404. [-1.0+1.0j, -1.0-1.0j, 1.0, 2.0],
  1405. [1.0j, -1.0j, 0.0, 1.0-1.0j],
  1406. [-1.0+1.0j, -1.0-1.0j, 1.0, -3.0+1.0j],
  1407. [2.0, 3.0, 4.0, -0.5], # Cauchy principal value
  1408. [2.0, 3.0, 4.0, -5.0]]) # Cauchy principal value
  1409. expected_results = array([0.77688623778582,
  1410. 0.14297579667157,
  1411. 0.13613945827771-0.38207561624427j,
  1412. 1.6490011662711,
  1413. 0.94148358841220,
  1414. 1.8260115229009+1.2290661908643j,
  1415. -0.61127970812028-1.0684038390007j,
  1416. 0.24723819703052, # Cauchy principal value
  1417. -0.12711230042964]) # Caucny principal value
  1418. for i, arr in enumerate(args):
  1419. assert_allclose(elliprj(*arr), expected_results[i])
  1420. @pytest.mark.xfail(reason="Insufficient accuracy on 32-bit")
  1421. def test_elliprj_hard(self):
  1422. assert_allclose(elliprj(6.483625725195452e-08,
  1423. 1.1649136528196886e-27,
  1424. 3.6767340167168e+13,
  1425. 0.493704617023468),
  1426. 8.63426920644241857617477551054e-6,
  1427. rtol=5e-15, atol=1e-20)
  1428. assert_allclose(elliprj(14.375105857849121,
  1429. 9.993988969725365e-11,
  1430. 1.72844262269944e-26,
  1431. 5.898871222598245e-06),
  1432. 829774.1424801627252574054378691828,
  1433. rtol=5e-15, atol=1e-20)
  1434. class TestEllipLegendreCarlsonIdentities(object):
  1435. """Test identities expressing the Legendre elliptic integrals in terms
  1436. of Carlson's symmetric integrals. These identities can be found
  1437. in the DLMF https://dlmf.nist.gov/19.25#i .
  1438. """
  1439. def setup_class(self):
  1440. self.m_n1_1 = np.arange(-1., 1., 0.01)
  1441. # For double, this is -(2**1024)
  1442. self.max_neg = finfo(float_).min
  1443. # Lots of very negative numbers
  1444. self.very_neg_m = -1. * 2.**arange(-1 +
  1445. np.log2(-self.max_neg), 0.,
  1446. -1.)
  1447. self.ms_up_to_1 = np.concatenate(([self.max_neg],
  1448. self.very_neg_m,
  1449. self.m_n1_1))
  1450. def test_k(self):
  1451. """Test identity:
  1452. K(m) = R_F(0, 1-m, 1)
  1453. """
  1454. m = self.ms_up_to_1
  1455. assert_allclose(ellipk(m), elliprf(0., 1.-m, 1.))
  1456. def test_km1(self):
  1457. """Test identity:
  1458. K(m) = R_F(0, 1-m, 1)
  1459. But with the ellipkm1 function
  1460. """
  1461. # For double, this is 2**-1022
  1462. tiny = finfo(float_).tiny
  1463. # All these small powers of 2, up to 2**-1
  1464. m1 = tiny * 2.**arange(0., -np.log2(tiny))
  1465. assert_allclose(ellipkm1(m1), elliprf(0., m1, 1.))
  1466. def test_e(self):
  1467. """Test identity:
  1468. E(m) = 2*R_G(0, 1-k^2, 1)
  1469. """
  1470. m = self.ms_up_to_1
  1471. assert_allclose(ellipe(m), 2.*elliprg(0., 1.-m, 1.))
  1472. class TestErf:
  1473. def test_erf(self):
  1474. er = special.erf(.25)
  1475. assert_almost_equal(er,0.2763263902,8)
  1476. def test_erf_zeros(self):
  1477. erz = special.erf_zeros(5)
  1478. erzr = array([1.45061616+1.88094300j,
  1479. 2.24465928+2.61657514j,
  1480. 2.83974105+3.17562810j,
  1481. 3.33546074+3.64617438j,
  1482. 3.76900557+4.06069723j])
  1483. assert_array_almost_equal(erz,erzr,4)
  1484. def _check_variant_func(self, func, other_func, rtol, atol=0):
  1485. np.random.seed(1234)
  1486. n = 10000
  1487. x = np.random.pareto(0.02, n) * (2*np.random.randint(0, 2, n) - 1)
  1488. y = np.random.pareto(0.02, n) * (2*np.random.randint(0, 2, n) - 1)
  1489. z = x + 1j*y
  1490. with np.errstate(all='ignore'):
  1491. w = other_func(z)
  1492. w_real = other_func(x).real
  1493. mask = np.isfinite(w)
  1494. w = w[mask]
  1495. z = z[mask]
  1496. mask = np.isfinite(w_real)
  1497. w_real = w_real[mask]
  1498. x = x[mask]
  1499. # test both real and complex variants
  1500. assert_func_equal(func, w, z, rtol=rtol, atol=atol)
  1501. assert_func_equal(func, w_real, x, rtol=rtol, atol=atol)
  1502. def test_erfc_consistent(self):
  1503. self._check_variant_func(
  1504. cephes.erfc,
  1505. lambda z: 1 - cephes.erf(z),
  1506. rtol=1e-12,
  1507. atol=1e-14 # <- the test function loses precision
  1508. )
  1509. def test_erfcx_consistent(self):
  1510. self._check_variant_func(
  1511. cephes.erfcx,
  1512. lambda z: np.exp(z*z) * cephes.erfc(z),
  1513. rtol=1e-12
  1514. )
  1515. def test_erfi_consistent(self):
  1516. self._check_variant_func(
  1517. cephes.erfi,
  1518. lambda z: -1j * cephes.erf(1j*z),
  1519. rtol=1e-12
  1520. )
  1521. def test_dawsn_consistent(self):
  1522. self._check_variant_func(
  1523. cephes.dawsn,
  1524. lambda z: sqrt(pi)/2 * np.exp(-z*z) * cephes.erfi(z),
  1525. rtol=1e-12
  1526. )
  1527. def test_erf_nan_inf(self):
  1528. vals = [np.nan, -np.inf, np.inf]
  1529. expected = [np.nan, -1, 1]
  1530. assert_allclose(special.erf(vals), expected, rtol=1e-15)
  1531. def test_erfc_nan_inf(self):
  1532. vals = [np.nan, -np.inf, np.inf]
  1533. expected = [np.nan, 2, 0]
  1534. assert_allclose(special.erfc(vals), expected, rtol=1e-15)
  1535. def test_erfcx_nan_inf(self):
  1536. vals = [np.nan, -np.inf, np.inf]
  1537. expected = [np.nan, np.inf, 0]
  1538. assert_allclose(special.erfcx(vals), expected, rtol=1e-15)
  1539. def test_erfi_nan_inf(self):
  1540. vals = [np.nan, -np.inf, np.inf]
  1541. expected = [np.nan, -np.inf, np.inf]
  1542. assert_allclose(special.erfi(vals), expected, rtol=1e-15)
  1543. def test_dawsn_nan_inf(self):
  1544. vals = [np.nan, -np.inf, np.inf]
  1545. expected = [np.nan, -0.0, 0.0]
  1546. assert_allclose(special.dawsn(vals), expected, rtol=1e-15)
  1547. def test_wofz_nan_inf(self):
  1548. vals = [np.nan, -np.inf, np.inf]
  1549. expected = [np.nan + np.nan * 1.j, 0.-0.j, 0.+0.j]
  1550. assert_allclose(special.wofz(vals), expected, rtol=1e-15)
  1551. class TestEuler:
  1552. def test_euler(self):
  1553. eu0 = special.euler(0)
  1554. eu1 = special.euler(1)
  1555. eu2 = special.euler(2) # just checking segfaults
  1556. assert_allclose(eu0, [1], rtol=1e-15)
  1557. assert_allclose(eu1, [1, 0], rtol=1e-15)
  1558. assert_allclose(eu2, [1, 0, -1], rtol=1e-15)
  1559. eu24 = special.euler(24)
  1560. mathworld = [1,1,5,61,1385,50521,2702765,199360981,
  1561. 19391512145,2404879675441,
  1562. 370371188237525,69348874393137901,
  1563. 15514534163557086905]
  1564. correct = zeros((25,),'d')
  1565. for k in range(0,13):
  1566. if (k % 2):
  1567. correct[2*k] = -float(mathworld[k])
  1568. else:
  1569. correct[2*k] = float(mathworld[k])
  1570. with np.errstate(all='ignore'):
  1571. err = nan_to_num((eu24-correct)/correct)
  1572. errmax = max(err)
  1573. assert_almost_equal(errmax, 0.0, 14)
  1574. class TestExp:
  1575. def test_exp2(self):
  1576. ex = special.exp2(2)
  1577. exrl = 2**2
  1578. assert_equal(ex,exrl)
  1579. def test_exp2more(self):
  1580. exm = special.exp2(2.5)
  1581. exmrl = 2**(2.5)
  1582. assert_almost_equal(exm,exmrl,8)
  1583. def test_exp10(self):
  1584. ex = special.exp10(2)
  1585. exrl = 10**2
  1586. assert_approx_equal(ex,exrl)
  1587. def test_exp10more(self):
  1588. exm = special.exp10(2.5)
  1589. exmrl = 10**(2.5)
  1590. assert_almost_equal(exm,exmrl,8)
  1591. def test_expm1(self):
  1592. ex = (special.expm1(2),special.expm1(3),special.expm1(4))
  1593. exrl = (exp(2)-1,exp(3)-1,exp(4)-1)
  1594. assert_array_almost_equal(ex,exrl,8)
  1595. def test_expm1more(self):
  1596. ex1 = (special.expm1(2),special.expm1(2.1),special.expm1(2.2))
  1597. exrl1 = (exp(2)-1,exp(2.1)-1,exp(2.2)-1)
  1598. assert_array_almost_equal(ex1,exrl1,8)
  1599. class TestFactorialFunctions:
  1600. def test_factorial(self):
  1601. # Some known values, float math
  1602. assert_array_almost_equal(special.factorial(0), 1)
  1603. assert_array_almost_equal(special.factorial(1), 1)
  1604. assert_array_almost_equal(special.factorial(2), 2)
  1605. assert_array_almost_equal([6., 24., 120.],
  1606. special.factorial([3, 4, 5], exact=False))
  1607. assert_array_almost_equal(special.factorial([[5, 3], [4, 3]]),
  1608. [[120, 6], [24, 6]])
  1609. # Some known values, integer math
  1610. assert_equal(special.factorial(0, exact=True), 1)
  1611. assert_equal(special.factorial(1, exact=True), 1)
  1612. assert_equal(special.factorial(2, exact=True), 2)
  1613. assert_equal(special.factorial(5, exact=True), 120)
  1614. assert_equal(special.factorial(15, exact=True), 1307674368000)
  1615. # ndarray shape is maintained
  1616. assert_equal(special.factorial([7, 4, 15, 10], exact=True),
  1617. [5040, 24, 1307674368000, 3628800])
  1618. assert_equal(special.factorial([[5, 3], [4, 3]], True),
  1619. [[120, 6], [24, 6]])
  1620. # object arrays
  1621. assert_equal(special.factorial(np.arange(-3, 22), True),
  1622. special.factorial(np.arange(-3, 22), False))
  1623. # int64 array
  1624. assert_equal(special.factorial(np.arange(-3, 15), True),
  1625. special.factorial(np.arange(-3, 15), False))
  1626. # int32 array
  1627. assert_equal(special.factorial(np.arange(-3, 5), True),
  1628. special.factorial(np.arange(-3, 5), False))
  1629. # Consistent output for n < 0
  1630. for exact in (True, False):
  1631. assert_array_equal(0, special.factorial(-3, exact))
  1632. assert_array_equal([1, 2, 0, 0],
  1633. special.factorial([1, 2, -5, -4], exact))
  1634. for n in range(0, 22):
  1635. # Compare all with math.factorial
  1636. correct = math.factorial(n)
  1637. assert_array_equal(correct, special.factorial(n, True))
  1638. assert_array_equal(correct, special.factorial([n], True)[0])
  1639. assert_allclose(float(correct), special.factorial(n, False))
  1640. assert_allclose(float(correct), special.factorial([n], False)[0])
  1641. # Compare exact=True vs False, scalar vs array
  1642. assert_array_equal(special.factorial(n, True),
  1643. special.factorial(n, False))
  1644. assert_array_equal(special.factorial([n], True),
  1645. special.factorial([n], False))
  1646. @pytest.mark.parametrize('x, exact', [
  1647. (1, True),
  1648. (1, False),
  1649. (np.array(1), True),
  1650. (np.array(1), False),
  1651. ])
  1652. def test_factorial_0d_return_type(self, x, exact):
  1653. assert np.isscalar(special.factorial(x, exact=exact))
  1654. def test_factorial2(self):
  1655. assert_array_almost_equal([105., 384., 945.],
  1656. special.factorial2([7, 8, 9], exact=False))
  1657. assert_equal(special.factorial2(7, exact=True), 105)
  1658. def test_factorialk(self):
  1659. assert_equal(special.factorialk(5, 1, exact=True), 120)
  1660. assert_equal(special.factorialk(5, 3, exact=True), 10)
  1661. @pytest.mark.parametrize('x, exact', [
  1662. (np.nan, True),
  1663. (np.nan, False),
  1664. (np.array([np.nan]), True),
  1665. (np.array([np.nan]), False),
  1666. ])
  1667. def test_nan_inputs(self, x, exact):
  1668. result = special.factorial(x, exact=exact)
  1669. assert_(np.isnan(result))
  1670. # GH-13122: special.factorial() argument should be an array of integers.
  1671. # On Python 3.10, math.factorial() reject float.
  1672. # On Python 3.9, a DeprecationWarning is emitted.
  1673. # A numpy array casts all integers to float if the array contains a
  1674. # single NaN.
  1675. @pytest.mark.skipif(sys.version_info >= (3, 10),
  1676. reason="Python 3.10+ math.factorial() requires int")
  1677. def test_mixed_nan_inputs(self):
  1678. x = np.array([np.nan, 1, 2, 3, np.nan])
  1679. with suppress_warnings() as sup:
  1680. sup.filter(DeprecationWarning, "Using factorial\\(\\) with floats is deprecated")
  1681. result = special.factorial(x, exact=True)
  1682. assert_equal(np.array([np.nan, 1, 2, 6, np.nan]), result)
  1683. result = special.factorial(x, exact=False)
  1684. assert_equal(np.array([np.nan, 1, 2, 6, np.nan]), result)
  1685. class TestFresnel:
  1686. @pytest.mark.parametrize("z, s, c", [
  1687. # some positive value
  1688. (.5, 0.064732432859999287, 0.49234422587144644),
  1689. (.5 + .0j, 0.064732432859999287, 0.49234422587144644),
  1690. # negative half annulus
  1691. # https://github.com/scipy/scipy/issues/12309
  1692. # Reference values can be reproduced with
  1693. # https://www.wolframalpha.com/input/?i=FresnelS%5B-2.0+%2B+0.1i%5D
  1694. # https://www.wolframalpha.com/input/?i=FresnelC%5B-2.0+%2B+0.1i%5D
  1695. (
  1696. -2.0 + 0.1j,
  1697. -0.3109538687728942-0.0005870728836383176j,
  1698. -0.4879956866358554+0.10670801832903172j
  1699. ),
  1700. (
  1701. -0.1 - 1.5j,
  1702. -0.03918309471866977+0.7197508454568574j,
  1703. 0.09605692502968956-0.43625191013617465j
  1704. ),
  1705. # a different algorithm kicks in for "large" values, i.e., |z| >= 4.5,
  1706. # make sure to test both float and complex values; a different
  1707. # algorithm is used
  1708. (6.0, 0.44696076, 0.49953147),
  1709. (6.0 + 0.0j, 0.44696076, 0.49953147),
  1710. (6.0j, -0.44696076j, 0.49953147j),
  1711. (-6.0 + 0.0j, -0.44696076, -0.49953147),
  1712. (-6.0j, 0.44696076j, -0.49953147j),
  1713. # inf
  1714. (np.inf, 0.5, 0.5),
  1715. (-np.inf, -0.5, -0.5),
  1716. ])
  1717. def test_fresnel_values(self, z, s, c):
  1718. frs = array(special.fresnel(z))
  1719. assert_array_almost_equal(frs, array([s, c]), 8)
  1720. # values from pg 329 Table 7.11 of A & S
  1721. # slightly corrected in 4th decimal place
  1722. def test_fresnel_zeros(self):
  1723. szo, czo = special.fresnel_zeros(5)
  1724. assert_array_almost_equal(szo,
  1725. array([2.0093+0.2885j,
  1726. 2.8335+0.2443j,
  1727. 3.4675+0.2185j,
  1728. 4.0026+0.2009j,
  1729. 4.4742+0.1877j]),3)
  1730. assert_array_almost_equal(czo,
  1731. array([1.7437+0.3057j,
  1732. 2.6515+0.2529j,
  1733. 3.3204+0.2240j,
  1734. 3.8757+0.2047j,
  1735. 4.3611+0.1907j]),3)
  1736. vals1 = special.fresnel(szo)[0]
  1737. vals2 = special.fresnel(czo)[1]
  1738. assert_array_almost_equal(vals1,0,14)
  1739. assert_array_almost_equal(vals2,0,14)
  1740. def test_fresnelc_zeros(self):
  1741. szo, czo = special.fresnel_zeros(6)
  1742. frc = special.fresnelc_zeros(6)
  1743. assert_array_almost_equal(frc,czo,12)
  1744. def test_fresnels_zeros(self):
  1745. szo, czo = special.fresnel_zeros(5)
  1746. frs = special.fresnels_zeros(5)
  1747. assert_array_almost_equal(frs,szo,12)
  1748. class TestGamma:
  1749. def test_gamma(self):
  1750. gam = special.gamma(5)
  1751. assert_equal(gam,24.0)
  1752. def test_gammaln(self):
  1753. gamln = special.gammaln(3)
  1754. lngam = log(special.gamma(3))
  1755. assert_almost_equal(gamln,lngam,8)
  1756. def test_gammainccinv(self):
  1757. gccinv = special.gammainccinv(.5,.5)
  1758. gcinv = special.gammaincinv(.5,.5)
  1759. assert_almost_equal(gccinv,gcinv,8)
  1760. @with_special_errors
  1761. def test_gammaincinv(self):
  1762. y = special.gammaincinv(.4,.4)
  1763. x = special.gammainc(.4,y)
  1764. assert_almost_equal(x,0.4,1)
  1765. y = special.gammainc(10, 0.05)
  1766. x = special.gammaincinv(10, 2.5715803516000736e-20)
  1767. assert_almost_equal(0.05, x, decimal=10)
  1768. assert_almost_equal(y, 2.5715803516000736e-20, decimal=10)
  1769. x = special.gammaincinv(50, 8.20754777388471303050299243573393e-18)
  1770. assert_almost_equal(11.0, x, decimal=10)
  1771. @with_special_errors
  1772. def test_975(self):
  1773. # Regression test for ticket #975 -- switch point in algorithm
  1774. # check that things work OK at the point, immediately next floats
  1775. # around it, and a bit further away
  1776. pts = [0.25,
  1777. np.nextafter(0.25, 0), 0.25 - 1e-12,
  1778. np.nextafter(0.25, 1), 0.25 + 1e-12]
  1779. for xp in pts:
  1780. y = special.gammaincinv(.4, xp)
  1781. x = special.gammainc(0.4, y)
  1782. assert_allclose(x, xp, rtol=1e-12)
  1783. def test_rgamma(self):
  1784. rgam = special.rgamma(8)
  1785. rlgam = 1/special.gamma(8)
  1786. assert_almost_equal(rgam,rlgam,8)
  1787. def test_infinity(self):
  1788. assert_(np.isinf(special.gamma(-1)))
  1789. assert_equal(special.rgamma(-1), 0)
  1790. class TestHankel:
  1791. def test_negv1(self):
  1792. assert_almost_equal(special.hankel1(-3,2), -special.hankel1(3,2), 14)
  1793. def test_hankel1(self):
  1794. hank1 = special.hankel1(1,.1)
  1795. hankrl = (special.jv(1,.1) + special.yv(1,.1)*1j)
  1796. assert_almost_equal(hank1,hankrl,8)
  1797. def test_negv1e(self):
  1798. assert_almost_equal(special.hankel1e(-3,2), -special.hankel1e(3,2), 14)
  1799. def test_hankel1e(self):
  1800. hank1e = special.hankel1e(1,.1)
  1801. hankrle = special.hankel1(1,.1)*exp(-.1j)
  1802. assert_almost_equal(hank1e,hankrle,8)
  1803. def test_negv2(self):
  1804. assert_almost_equal(special.hankel2(-3,2), -special.hankel2(3,2), 14)
  1805. def test_hankel2(self):
  1806. hank2 = special.hankel2(1,.1)
  1807. hankrl2 = (special.jv(1,.1) - special.yv(1,.1)*1j)
  1808. assert_almost_equal(hank2,hankrl2,8)
  1809. def test_neg2e(self):
  1810. assert_almost_equal(special.hankel2e(-3,2), -special.hankel2e(3,2), 14)
  1811. def test_hankl2e(self):
  1812. hank2e = special.hankel2e(1,.1)
  1813. hankrl2e = special.hankel2e(1,.1)
  1814. assert_almost_equal(hank2e,hankrl2e,8)
  1815. class TestHyper:
  1816. def test_h1vp(self):
  1817. h1 = special.h1vp(1,.1)
  1818. h1real = (special.jvp(1,.1) + special.yvp(1,.1)*1j)
  1819. assert_almost_equal(h1,h1real,8)
  1820. def test_h2vp(self):
  1821. h2 = special.h2vp(1,.1)
  1822. h2real = (special.jvp(1,.1) - special.yvp(1,.1)*1j)
  1823. assert_almost_equal(h2,h2real,8)
  1824. def test_hyp0f1(self):
  1825. # scalar input
  1826. assert_allclose(special.hyp0f1(2.5, 0.5), 1.21482702689997, rtol=1e-12)
  1827. assert_allclose(special.hyp0f1(2.5, 0), 1.0, rtol=1e-15)
  1828. # float input, expected values match mpmath
  1829. x = special.hyp0f1(3.0, [-1.5, -1, 0, 1, 1.5])
  1830. expected = np.array([0.58493659229143, 0.70566805723127, 1.0,
  1831. 1.37789689539747, 1.60373685288480])
  1832. assert_allclose(x, expected, rtol=1e-12)
  1833. # complex input
  1834. x = special.hyp0f1(3.0, np.array([-1.5, -1, 0, 1, 1.5]) + 0.j)
  1835. assert_allclose(x, expected.astype(complex), rtol=1e-12)
  1836. # test broadcasting
  1837. x1 = [0.5, 1.5, 2.5]
  1838. x2 = [0, 1, 0.5]
  1839. x = special.hyp0f1(x1, x2)
  1840. expected = [1.0, 1.8134302039235093, 1.21482702689997]
  1841. assert_allclose(x, expected, rtol=1e-12)
  1842. x = special.hyp0f1(np.row_stack([x1] * 2), x2)
  1843. assert_allclose(x, np.row_stack([expected] * 2), rtol=1e-12)
  1844. assert_raises(ValueError, special.hyp0f1,
  1845. np.row_stack([x1] * 3), [0, 1])
  1846. def test_hyp0f1_gh5764(self):
  1847. # Just checks the point that failed; there's a more systematic
  1848. # test in test_mpmath
  1849. res = special.hyp0f1(0.8, 0.5 + 0.5*1J)
  1850. # The expected value was generated using mpmath
  1851. assert_almost_equal(res, 1.6139719776441115 + 1J*0.80893054061790665)
  1852. def test_hyp1f1(self):
  1853. hyp1 = special.hyp1f1(.1,.1,.3)
  1854. assert_almost_equal(hyp1, 1.3498588075760032,7)
  1855. # test contributed by Moritz Deger (2008-05-29)
  1856. # https://github.com/scipy/scipy/issues/1186 (Trac #659)
  1857. # reference data obtained from mathematica [ a, b, x, m(a,b,x)]:
  1858. # produced with test_hyp1f1.nb
  1859. ref_data = array([[-8.38132975e+00, -1.28436461e+01, -2.91081397e+01, 1.04178330e+04],
  1860. [2.91076882e+00, -6.35234333e+00, -1.27083993e+01, 6.68132725e+00],
  1861. [-1.42938258e+01, 1.80869131e-01, 1.90038728e+01, 1.01385897e+05],
  1862. [5.84069088e+00, 1.33187908e+01, 2.91290106e+01, 1.59469411e+08],
  1863. [-2.70433202e+01, -1.16274873e+01, -2.89582384e+01, 1.39900152e+24],
  1864. [4.26344966e+00, -2.32701773e+01, 1.91635759e+01, 6.13816915e+21],
  1865. [1.20514340e+01, -3.40260240e+00, 7.26832235e+00, 1.17696112e+13],
  1866. [2.77372955e+01, -1.99424687e+00, 3.61332246e+00, 3.07419615e+13],
  1867. [1.50310939e+01, -2.91198675e+01, -1.53581080e+01, -3.79166033e+02],
  1868. [1.43995827e+01, 9.84311196e+00, 1.93204553e+01, 2.55836264e+10],
  1869. [-4.08759686e+00, 1.34437025e+01, -1.42072843e+01, 1.70778449e+01],
  1870. [8.05595738e+00, -1.31019838e+01, 1.52180721e+01, 3.06233294e+21],
  1871. [1.81815804e+01, -1.42908793e+01, 9.57868793e+00, -2.84771348e+20],
  1872. [-2.49671396e+01, 1.25082843e+01, -1.71562286e+01, 2.36290426e+07],
  1873. [2.67277673e+01, 1.70315414e+01, 6.12701450e+00, 7.77917232e+03],
  1874. [2.49565476e+01, 2.91694684e+01, 6.29622660e+00, 2.35300027e+02],
  1875. [6.11924542e+00, -1.59943768e+00, 9.57009289e+00, 1.32906326e+11],
  1876. [-1.47863653e+01, 2.41691301e+01, -1.89981821e+01, 2.73064953e+03],
  1877. [2.24070483e+01, -2.93647433e+00, 8.19281432e+00, -6.42000372e+17],
  1878. [8.04042600e-01, 1.82710085e+01, -1.97814534e+01, 5.48372441e-01],
  1879. [1.39590390e+01, 1.97318686e+01, 2.37606635e+00, 5.51923681e+00],
  1880. [-4.66640483e+00, -2.00237930e+01, 7.40365095e+00, 4.50310752e+00],
  1881. [2.76821999e+01, -6.36563968e+00, 1.11533984e+01, -9.28725179e+23],
  1882. [-2.56764457e+01, 1.24544906e+00, 1.06407572e+01, 1.25922076e+01],
  1883. [3.20447808e+00, 1.30874383e+01, 2.26098014e+01, 2.03202059e+04],
  1884. [-1.24809647e+01, 4.15137113e+00, -2.92265700e+01, 2.39621411e+08],
  1885. [2.14778108e+01, -2.35162960e+00, -1.13758664e+01, 4.46882152e-01],
  1886. [-9.85469168e+00, -3.28157680e+00, 1.67447548e+01, -1.07342390e+07],
  1887. [1.08122310e+01, -2.47353236e+01, -1.15622349e+01, -2.91733796e+03],
  1888. [-2.67933347e+01, -3.39100709e+00, 2.56006986e+01, -5.29275382e+09],
  1889. [-8.60066776e+00, -8.02200924e+00, 1.07231926e+01, 1.33548320e+06],
  1890. [-1.01724238e-01, -1.18479709e+01, -2.55407104e+01, 1.55436570e+00],
  1891. [-3.93356771e+00, 2.11106818e+01, -2.57598485e+01, 2.13467840e+01],
  1892. [3.74750503e+00, 1.55687633e+01, -2.92841720e+01, 1.43873509e-02],
  1893. [6.99726781e+00, 2.69855571e+01, -1.63707771e+01, 3.08098673e-02],
  1894. [-2.31996011e+01, 3.47631054e+00, 9.75119815e-01, 1.79971073e-02],
  1895. [2.38951044e+01, -2.91460190e+01, -2.50774708e+00, 9.56934814e+00],
  1896. [1.52730825e+01, 5.77062507e+00, 1.21922003e+01, 1.32345307e+09],
  1897. [1.74673917e+01, 1.89723426e+01, 4.94903250e+00, 9.90859484e+01],
  1898. [1.88971241e+01, 2.86255413e+01, 5.52360109e-01, 1.44165360e+00],
  1899. [1.02002319e+01, -1.66855152e+01, -2.55426235e+01, 6.56481554e+02],
  1900. [-1.79474153e+01, 1.22210200e+01, -1.84058212e+01, 8.24041812e+05],
  1901. [-1.36147103e+01, 1.32365492e+00, -7.22375200e+00, 9.92446491e+05],
  1902. [7.57407832e+00, 2.59738234e+01, -1.34139168e+01, 3.64037761e-02],
  1903. [2.21110169e+00, 1.28012666e+01, 1.62529102e+01, 1.33433085e+02],
  1904. [-2.64297569e+01, -1.63176658e+01, -1.11642006e+01, -2.44797251e+13],
  1905. [-2.46622944e+01, -3.02147372e+00, 8.29159315e+00, -3.21799070e+05],
  1906. [-1.37215095e+01, -1.96680183e+01, 2.91940118e+01, 3.21457520e+12],
  1907. [-5.45566105e+00, 2.81292086e+01, 1.72548215e-01, 9.66973000e-01],
  1908. [-1.55751298e+00, -8.65703373e+00, 2.68622026e+01, -3.17190834e+16],
  1909. [2.45393609e+01, -2.70571903e+01, 1.96815505e+01, 1.80708004e+37],
  1910. [5.77482829e+00, 1.53203143e+01, 2.50534322e+01, 1.14304242e+06],
  1911. [-1.02626819e+01, 2.36887658e+01, -2.32152102e+01, 7.28965646e+02],
  1912. [-1.30833446e+00, -1.28310210e+01, 1.87275544e+01, -9.33487904e+12],
  1913. [5.83024676e+00, -1.49279672e+01, 2.44957538e+01, -7.61083070e+27],
  1914. [-2.03130747e+01, 2.59641715e+01, -2.06174328e+01, 4.54744859e+04],
  1915. [1.97684551e+01, -2.21410519e+01, -2.26728740e+01, 3.53113026e+06],
  1916. [2.73673444e+01, 2.64491725e+01, 1.57599882e+01, 1.07385118e+07],
  1917. [5.73287971e+00, 1.21111904e+01, 1.33080171e+01, 2.63220467e+03],
  1918. [-2.82751072e+01, 2.08605881e+01, 9.09838900e+00, -6.60957033e-07],
  1919. [1.87270691e+01, -1.74437016e+01, 1.52413599e+01, 6.59572851e+27],
  1920. [6.60681457e+00, -2.69449855e+00, 9.78972047e+00, -2.38587870e+12],
  1921. [1.20895561e+01, -2.51355765e+01, 2.30096101e+01, 7.58739886e+32],
  1922. [-2.44682278e+01, 2.10673441e+01, -1.36705538e+01, 4.54213550e+04],
  1923. [-4.50665152e+00, 3.72292059e+00, -4.83403707e+00, 2.68938214e+01],
  1924. [-7.46540049e+00, -1.08422222e+01, -1.72203805e+01, -2.09402162e+02],
  1925. [-2.00307551e+01, -7.50604431e+00, -2.78640020e+01, 4.15985444e+19],
  1926. [1.99890876e+01, 2.20677419e+01, -2.51301778e+01, 1.23840297e-09],
  1927. [2.03183823e+01, -7.66942559e+00, 2.10340070e+01, 1.46285095e+31],
  1928. [-2.90315825e+00, -2.55785967e+01, -9.58779316e+00, 2.65714264e-01],
  1929. [2.73960829e+01, -1.80097203e+01, -2.03070131e+00, 2.52908999e+02],
  1930. [-2.11708058e+01, -2.70304032e+01, 2.48257944e+01, 3.09027527e+08],
  1931. [2.21959758e+01, 4.00258675e+00, -1.62853977e+01, -9.16280090e-09],
  1932. [1.61661840e+01, -2.26845150e+01, 2.17226940e+01, -8.24774394e+33],
  1933. [-3.35030306e+00, 1.32670581e+00, 9.39711214e+00, -1.47303163e+01],
  1934. [7.23720726e+00, -2.29763909e+01, 2.34709682e+01, -9.20711735e+29],
  1935. [2.71013568e+01, 1.61951087e+01, -7.11388906e-01, 2.98750911e-01],
  1936. [8.40057933e+00, -7.49665220e+00, 2.95587388e+01, 6.59465635e+29],
  1937. [-1.51603423e+01, 1.94032322e+01, -7.60044357e+00, 1.05186941e+02],
  1938. [-8.83788031e+00, -2.72018313e+01, 1.88269907e+00, 1.81687019e+00],
  1939. [-1.87283712e+01, 5.87479570e+00, -1.91210203e+01, 2.52235612e+08],
  1940. [-5.61338513e-01, 2.69490237e+01, 1.16660111e-01, 9.97567783e-01],
  1941. [-5.44354025e+00, -1.26721408e+01, -4.66831036e+00, 1.06660735e-01],
  1942. [-2.18846497e+00, 2.33299566e+01, 9.62564397e+00, 3.03842061e-01],
  1943. [6.65661299e+00, -2.39048713e+01, 1.04191807e+01, 4.73700451e+13],
  1944. [-2.57298921e+01, -2.60811296e+01, 2.74398110e+01, -5.32566307e+11],
  1945. [-1.11431826e+01, -1.59420160e+01, -1.84880553e+01, -1.01514747e+02],
  1946. [6.50301931e+00, 2.59859051e+01, -2.33270137e+01, 1.22760500e-02],
  1947. [-1.94987891e+01, -2.62123262e+01, 3.90323225e+00, 1.71658894e+01],
  1948. [7.26164601e+00, -1.41469402e+01, 2.81499763e+01, -2.50068329e+31],
  1949. [-1.52424040e+01, 2.99719005e+01, -2.85753678e+01, 1.31906693e+04],
  1950. [5.24149291e+00, -1.72807223e+01, 2.22129493e+01, 2.50748475e+25],
  1951. [3.63207230e-01, -9.54120862e-02, -2.83874044e+01, 9.43854939e-01],
  1952. [-2.11326457e+00, -1.25707023e+01, 1.17172130e+00, 1.20812698e+00],
  1953. [2.48513582e+00, 1.03652647e+01, -1.84625148e+01, 6.47910997e-02],
  1954. [2.65395942e+01, 2.74794672e+01, 1.29413428e+01, 2.89306132e+05],
  1955. [-9.49445460e+00, 1.59930921e+01, -1.49596331e+01, 3.27574841e+02],
  1956. [-5.89173945e+00, 9.96742426e+00, 2.60318889e+01, -3.15842908e-01],
  1957. [-1.15387239e+01, -2.21433107e+01, -2.17686413e+01, 1.56724718e-01],
  1958. [-5.30592244e+00, -2.42752190e+01, 1.29734035e+00, 1.31985534e+00]])
  1959. for a,b,c,expected in ref_data:
  1960. result = special.hyp1f1(a,b,c)
  1961. assert_(abs(expected - result)/expected < 1e-4)
  1962. def test_hyp1f1_gh2957(self):
  1963. hyp1 = special.hyp1f1(0.5, 1.5, -709.7827128933)
  1964. hyp2 = special.hyp1f1(0.5, 1.5, -709.7827128934)
  1965. assert_almost_equal(hyp1, hyp2, 12)
  1966. def test_hyp1f1_gh2282(self):
  1967. hyp = special.hyp1f1(0.5, 1.5, -1000)
  1968. assert_almost_equal(hyp, 0.028024956081989643, 12)
  1969. def test_hyp2f1(self):
  1970. # a collection of special cases taken from AMS 55
  1971. values = [[0.5, 1, 1.5, 0.2**2, 0.5/0.2*log((1+0.2)/(1-0.2))],
  1972. [0.5, 1, 1.5, -0.2**2, 1./0.2*arctan(0.2)],
  1973. [1, 1, 2, 0.2, -1/0.2*log(1-0.2)],
  1974. [3, 3.5, 1.5, 0.2**2,
  1975. 0.5/0.2/(-5)*((1+0.2)**(-5)-(1-0.2)**(-5))],
  1976. [-3, 3, 0.5, sin(0.2)**2, cos(2*3*0.2)],
  1977. [3, 4, 8, 1, special.gamma(8)*special.gamma(8-4-3)/special.gamma(8-3)/special.gamma(8-4)],
  1978. [3, 2, 3-2+1, -1, 1./2**3*sqrt(pi) *
  1979. special.gamma(1+3-2)/special.gamma(1+0.5*3-2)/special.gamma(0.5+0.5*3)],
  1980. [5, 2, 5-2+1, -1, 1./2**5*sqrt(pi) *
  1981. special.gamma(1+5-2)/special.gamma(1+0.5*5-2)/special.gamma(0.5+0.5*5)],
  1982. [4, 0.5+4, 1.5-2*4, -1./3, (8./9)**(-2*4)*special.gamma(4./3) *
  1983. special.gamma(1.5-2*4)/special.gamma(3./2)/special.gamma(4./3-2*4)],
  1984. # and some others
  1985. # ticket #424
  1986. [1.5, -0.5, 1.0, -10.0, 4.1300097765277476484],
  1987. # negative integer a or b, with c-a-b integer and x > 0.9
  1988. [-2,3,1,0.95,0.715],
  1989. [2,-3,1,0.95,-0.007],
  1990. [-6,3,1,0.95,0.0000810625],
  1991. [2,-5,1,0.95,-0.000029375],
  1992. # huge negative integers
  1993. (10, -900, 10.5, 0.99, 1.91853705796607664803709475658e-24),
  1994. (10, -900, -10.5, 0.99, 3.54279200040355710199058559155e-18),
  1995. ]
  1996. for i, (a, b, c, x, v) in enumerate(values):
  1997. cv = special.hyp2f1(a, b, c, x)
  1998. assert_almost_equal(cv, v, 8, err_msg='test #%d' % i)
  1999. def test_hyperu(self):
  2000. val1 = special.hyperu(1,0.1,100)
  2001. assert_almost_equal(val1,0.0098153,7)
  2002. a,b = [0.3,0.6,1.2,-2.7],[1.5,3.2,-0.4,-3.2]
  2003. a,b = asarray(a), asarray(b)
  2004. z = 0.5
  2005. hypu = special.hyperu(a,b,z)
  2006. hprl = (pi/sin(pi*b))*(special.hyp1f1(a,b,z) /
  2007. (special.gamma(1+a-b)*special.gamma(b)) -
  2008. z**(1-b)*special.hyp1f1(1+a-b,2-b,z)
  2009. / (special.gamma(a)*special.gamma(2-b)))
  2010. assert_array_almost_equal(hypu,hprl,12)
  2011. def test_hyperu_gh2287(self):
  2012. assert_almost_equal(special.hyperu(1, 1.5, 20.2),
  2013. 0.048360918656699191, 12)
  2014. class TestBessel:
  2015. def test_itj0y0(self):
  2016. it0 = array(special.itj0y0(.2))
  2017. assert_array_almost_equal(it0,array([0.19933433254006822, -0.34570883800412566]),8)
  2018. def test_it2j0y0(self):
  2019. it2 = array(special.it2j0y0(.2))
  2020. assert_array_almost_equal(it2,array([0.0049937546274601858, -0.43423067011231614]),8)
  2021. def test_negv_iv(self):
  2022. assert_equal(special.iv(3,2), special.iv(-3,2))
  2023. def test_j0(self):
  2024. oz = special.j0(.1)
  2025. ozr = special.jn(0,.1)
  2026. assert_almost_equal(oz,ozr,8)
  2027. def test_j1(self):
  2028. o1 = special.j1(.1)
  2029. o1r = special.jn(1,.1)
  2030. assert_almost_equal(o1,o1r,8)
  2031. def test_jn(self):
  2032. jnnr = special.jn(1,.2)
  2033. assert_almost_equal(jnnr,0.099500832639235995,8)
  2034. def test_negv_jv(self):
  2035. assert_almost_equal(special.jv(-3,2), -special.jv(3,2), 14)
  2036. def test_jv(self):
  2037. values = [[0, 0.1, 0.99750156206604002],
  2038. [2./3, 1e-8, 0.3239028506761532e-5],
  2039. [2./3, 1e-10, 0.1503423854873779e-6],
  2040. [3.1, 1e-10, 0.1711956265409013e-32],
  2041. [2./3, 4.0, -0.2325440850267039],
  2042. ]
  2043. for i, (v, x, y) in enumerate(values):
  2044. yc = special.jv(v, x)
  2045. assert_almost_equal(yc, y, 8, err_msg='test #%d' % i)
  2046. def test_negv_jve(self):
  2047. assert_almost_equal(special.jve(-3,2), -special.jve(3,2), 14)
  2048. def test_jve(self):
  2049. jvexp = special.jve(1,.2)
  2050. assert_almost_equal(jvexp,0.099500832639235995,8)
  2051. jvexp1 = special.jve(1,.2+1j)
  2052. z = .2+1j
  2053. jvexpr = special.jv(1,z)*exp(-abs(z.imag))
  2054. assert_almost_equal(jvexp1,jvexpr,8)
  2055. def test_jn_zeros(self):
  2056. jn0 = special.jn_zeros(0,5)
  2057. jn1 = special.jn_zeros(1,5)
  2058. assert_array_almost_equal(jn0,array([2.4048255577,
  2059. 5.5200781103,
  2060. 8.6537279129,
  2061. 11.7915344391,
  2062. 14.9309177086]),4)
  2063. assert_array_almost_equal(jn1,array([3.83171,
  2064. 7.01559,
  2065. 10.17347,
  2066. 13.32369,
  2067. 16.47063]),4)
  2068. jn102 = special.jn_zeros(102,5)
  2069. assert_allclose(jn102, array([110.89174935992040343,
  2070. 117.83464175788308398,
  2071. 123.70194191713507279,
  2072. 129.02417238949092824,
  2073. 134.00114761868422559]), rtol=1e-13)
  2074. jn301 = special.jn_zeros(301,5)
  2075. assert_allclose(jn301, array([313.59097866698830153,
  2076. 323.21549776096288280,
  2077. 331.22338738656748796,
  2078. 338.39676338872084500,
  2079. 345.03284233056064157]), rtol=1e-13)
  2080. def test_jn_zeros_slow(self):
  2081. jn0 = special.jn_zeros(0, 300)
  2082. assert_allclose(jn0[260-1], 816.02884495068867280, rtol=1e-13)
  2083. assert_allclose(jn0[280-1], 878.86068707124422606, rtol=1e-13)
  2084. assert_allclose(jn0[300-1], 941.69253065317954064, rtol=1e-13)
  2085. jn10 = special.jn_zeros(10, 300)
  2086. assert_allclose(jn10[260-1], 831.67668514305631151, rtol=1e-13)
  2087. assert_allclose(jn10[280-1], 894.51275095371316931, rtol=1e-13)
  2088. assert_allclose(jn10[300-1], 957.34826370866539775, rtol=1e-13)
  2089. jn3010 = special.jn_zeros(3010,5)
  2090. assert_allclose(jn3010, array([3036.86590780927,
  2091. 3057.06598526482,
  2092. 3073.66360690272,
  2093. 3088.37736494778,
  2094. 3101.86438139042]), rtol=1e-8)
  2095. def test_jnjnp_zeros(self):
  2096. jn = special.jn
  2097. def jnp(n, x):
  2098. return (jn(n-1,x) - jn(n+1,x))/2
  2099. for nt in range(1, 30):
  2100. z, n, m, t = special.jnjnp_zeros(nt)
  2101. for zz, nn, tt in zip(z, n, t):
  2102. if tt == 0:
  2103. assert_allclose(jn(nn, zz), 0, atol=1e-6)
  2104. elif tt == 1:
  2105. assert_allclose(jnp(nn, zz), 0, atol=1e-6)
  2106. else:
  2107. raise AssertionError("Invalid t return for nt=%d" % nt)
  2108. def test_jnp_zeros(self):
  2109. jnp = special.jnp_zeros(1,5)
  2110. assert_array_almost_equal(jnp, array([1.84118,
  2111. 5.33144,
  2112. 8.53632,
  2113. 11.70600,
  2114. 14.86359]),4)
  2115. jnp = special.jnp_zeros(443,5)
  2116. assert_allclose(special.jvp(443, jnp), 0, atol=1e-15)
  2117. def test_jnyn_zeros(self):
  2118. jnz = special.jnyn_zeros(1,5)
  2119. assert_array_almost_equal(jnz,(array([3.83171,
  2120. 7.01559,
  2121. 10.17347,
  2122. 13.32369,
  2123. 16.47063]),
  2124. array([1.84118,
  2125. 5.33144,
  2126. 8.53632,
  2127. 11.70600,
  2128. 14.86359]),
  2129. array([2.19714,
  2130. 5.42968,
  2131. 8.59601,
  2132. 11.74915,
  2133. 14.89744]),
  2134. array([3.68302,
  2135. 6.94150,
  2136. 10.12340,
  2137. 13.28576,
  2138. 16.44006])),5)
  2139. def test_jvp(self):
  2140. jvprim = special.jvp(2,2)
  2141. jv0 = (special.jv(1,2)-special.jv(3,2))/2
  2142. assert_almost_equal(jvprim,jv0,10)
  2143. def test_k0(self):
  2144. ozk = special.k0(.1)
  2145. ozkr = special.kv(0,.1)
  2146. assert_almost_equal(ozk,ozkr,8)
  2147. def test_k0e(self):
  2148. ozke = special.k0e(.1)
  2149. ozker = special.kve(0,.1)
  2150. assert_almost_equal(ozke,ozker,8)
  2151. def test_k1(self):
  2152. o1k = special.k1(.1)
  2153. o1kr = special.kv(1,.1)
  2154. assert_almost_equal(o1k,o1kr,8)
  2155. def test_k1e(self):
  2156. o1ke = special.k1e(.1)
  2157. o1ker = special.kve(1,.1)
  2158. assert_almost_equal(o1ke,o1ker,8)
  2159. def test_jacobi(self):
  2160. a = 5*np.random.random() - 1
  2161. b = 5*np.random.random() - 1
  2162. P0 = special.jacobi(0,a,b)
  2163. P1 = special.jacobi(1,a,b)
  2164. P2 = special.jacobi(2,a,b)
  2165. P3 = special.jacobi(3,a,b)
  2166. assert_array_almost_equal(P0.c,[1],13)
  2167. assert_array_almost_equal(P1.c,array([a+b+2,a-b])/2.0,13)
  2168. cp = [(a+b+3)*(a+b+4), 4*(a+b+3)*(a+2), 4*(a+1)*(a+2)]
  2169. p2c = [cp[0],cp[1]-2*cp[0],cp[2]-cp[1]+cp[0]]
  2170. assert_array_almost_equal(P2.c,array(p2c)/8.0,13)
  2171. cp = [(a+b+4)*(a+b+5)*(a+b+6),6*(a+b+4)*(a+b+5)*(a+3),
  2172. 12*(a+b+4)*(a+2)*(a+3),8*(a+1)*(a+2)*(a+3)]
  2173. p3c = [cp[0],cp[1]-3*cp[0],cp[2]-2*cp[1]+3*cp[0],cp[3]-cp[2]+cp[1]-cp[0]]
  2174. assert_array_almost_equal(P3.c,array(p3c)/48.0,13)
  2175. def test_kn(self):
  2176. kn1 = special.kn(0,.2)
  2177. assert_almost_equal(kn1,1.7527038555281462,8)
  2178. def test_negv_kv(self):
  2179. assert_equal(special.kv(3.0, 2.2), special.kv(-3.0, 2.2))
  2180. def test_kv0(self):
  2181. kv0 = special.kv(0,.2)
  2182. assert_almost_equal(kv0, 1.7527038555281462, 10)
  2183. def test_kv1(self):
  2184. kv1 = special.kv(1,0.2)
  2185. assert_almost_equal(kv1, 4.775972543220472, 10)
  2186. def test_kv2(self):
  2187. kv2 = special.kv(2,0.2)
  2188. assert_almost_equal(kv2, 49.51242928773287, 10)
  2189. def test_kn_largeorder(self):
  2190. assert_allclose(special.kn(32, 1), 1.7516596664574289e+43)
  2191. def test_kv_largearg(self):
  2192. assert_equal(special.kv(0, 1e19), 0)
  2193. def test_negv_kve(self):
  2194. assert_equal(special.kve(3.0, 2.2), special.kve(-3.0, 2.2))
  2195. def test_kve(self):
  2196. kve1 = special.kve(0,.2)
  2197. kv1 = special.kv(0,.2)*exp(.2)
  2198. assert_almost_equal(kve1,kv1,8)
  2199. z = .2+1j
  2200. kve2 = special.kve(0,z)
  2201. kv2 = special.kv(0,z)*exp(z)
  2202. assert_almost_equal(kve2,kv2,8)
  2203. def test_kvp_v0n1(self):
  2204. z = 2.2
  2205. assert_almost_equal(-special.kv(1,z), special.kvp(0,z, n=1), 10)
  2206. def test_kvp_n1(self):
  2207. v = 3.
  2208. z = 2.2
  2209. xc = -special.kv(v+1,z) + v/z*special.kv(v,z)
  2210. x = special.kvp(v,z, n=1)
  2211. assert_almost_equal(xc, x, 10) # this function (kvp) is broken
  2212. def test_kvp_n2(self):
  2213. v = 3.
  2214. z = 2.2
  2215. xc = (z**2+v**2-v)/z**2 * special.kv(v,z) + special.kv(v+1,z)/z
  2216. x = special.kvp(v, z, n=2)
  2217. assert_almost_equal(xc, x, 10)
  2218. def test_y0(self):
  2219. oz = special.y0(.1)
  2220. ozr = special.yn(0,.1)
  2221. assert_almost_equal(oz,ozr,8)
  2222. def test_y1(self):
  2223. o1 = special.y1(.1)
  2224. o1r = special.yn(1,.1)
  2225. assert_almost_equal(o1,o1r,8)
  2226. def test_y0_zeros(self):
  2227. yo,ypo = special.y0_zeros(2)
  2228. zo,zpo = special.y0_zeros(2,complex=1)
  2229. all = r_[yo,zo]
  2230. allval = r_[ypo,zpo]
  2231. assert_array_almost_equal(abs(special.yv(0.0,all)),0.0,11)
  2232. assert_array_almost_equal(abs(special.yv(1,all)-allval),0.0,11)
  2233. def test_y1_zeros(self):
  2234. y1 = special.y1_zeros(1)
  2235. assert_array_almost_equal(y1,(array([2.19714]),array([0.52079])),5)
  2236. def test_y1p_zeros(self):
  2237. y1p = special.y1p_zeros(1,complex=1)
  2238. assert_array_almost_equal(y1p,(array([0.5768+0.904j]), array([-0.7635+0.5892j])),3)
  2239. def test_yn_zeros(self):
  2240. an = special.yn_zeros(4,2)
  2241. assert_array_almost_equal(an,array([5.64515, 9.36162]),5)
  2242. an = special.yn_zeros(443,5)
  2243. assert_allclose(an, [450.13573091578090314, 463.05692376675001542,
  2244. 472.80651546418663566, 481.27353184725625838,
  2245. 488.98055964441374646], rtol=1e-15)
  2246. def test_ynp_zeros(self):
  2247. ao = special.ynp_zeros(0,2)
  2248. assert_array_almost_equal(ao,array([2.19714133, 5.42968104]),6)
  2249. ao = special.ynp_zeros(43,5)
  2250. assert_allclose(special.yvp(43, ao), 0, atol=1e-15)
  2251. ao = special.ynp_zeros(443,5)
  2252. assert_allclose(special.yvp(443, ao), 0, atol=1e-9)
  2253. def test_ynp_zeros_large_order(self):
  2254. ao = special.ynp_zeros(443,5)
  2255. assert_allclose(special.yvp(443, ao), 0, atol=1e-14)
  2256. def test_yn(self):
  2257. yn2n = special.yn(1,.2)
  2258. assert_almost_equal(yn2n,-3.3238249881118471,8)
  2259. def test_negv_yv(self):
  2260. assert_almost_equal(special.yv(-3,2), -special.yv(3,2), 14)
  2261. def test_yv(self):
  2262. yv2 = special.yv(1,.2)
  2263. assert_almost_equal(yv2,-3.3238249881118471,8)
  2264. def test_negv_yve(self):
  2265. assert_almost_equal(special.yve(-3,2), -special.yve(3,2), 14)
  2266. def test_yve(self):
  2267. yve2 = special.yve(1,.2)
  2268. assert_almost_equal(yve2,-3.3238249881118471,8)
  2269. yve2r = special.yv(1,.2+1j)*exp(-1)
  2270. yve22 = special.yve(1,.2+1j)
  2271. assert_almost_equal(yve22,yve2r,8)
  2272. def test_yvp(self):
  2273. yvpr = (special.yv(1,.2) - special.yv(3,.2))/2.0
  2274. yvp1 = special.yvp(2,.2)
  2275. assert_array_almost_equal(yvp1,yvpr,10)
  2276. def _cephes_vs_amos_points(self):
  2277. """Yield points at which to compare Cephes implementation to AMOS"""
  2278. # check several points, including large-amplitude ones
  2279. v = [-120, -100.3, -20., -10., -1., -.5, 0., 1., 12.49, 120., 301]
  2280. z = [-1300, -11, -10, -1, 1., 10., 200.5, 401., 600.5, 700.6, 1300,
  2281. 10003]
  2282. yield from itertools.product(v, z)
  2283. # check half-integers; these are problematic points at least
  2284. # for cephes/iv
  2285. yield from itertools.product(0.5 + arange(-60, 60), [3.5])
  2286. def check_cephes_vs_amos(self, f1, f2, rtol=1e-11, atol=0, skip=None):
  2287. for v, z in self._cephes_vs_amos_points():
  2288. if skip is not None and skip(v, z):
  2289. continue
  2290. c1, c2, c3 = f1(v, z), f1(v,z+0j), f2(int(v), z)
  2291. if np.isinf(c1):
  2292. assert_(np.abs(c2) >= 1e300, (v, z))
  2293. elif np.isnan(c1):
  2294. assert_(c2.imag != 0, (v, z))
  2295. else:
  2296. assert_allclose(c1, c2, err_msg=(v, z), rtol=rtol, atol=atol)
  2297. if v == int(v):
  2298. assert_allclose(c3, c2, err_msg=(v, z),
  2299. rtol=rtol, atol=atol)
  2300. @pytest.mark.xfail(platform.machine() == 'ppc64le',
  2301. reason="fails on ppc64le")
  2302. def test_jv_cephes_vs_amos(self):
  2303. self.check_cephes_vs_amos(special.jv, special.jn, rtol=1e-10, atol=1e-305)
  2304. @pytest.mark.xfail(platform.machine() == 'ppc64le',
  2305. reason="fails on ppc64le")
  2306. def test_yv_cephes_vs_amos(self):
  2307. self.check_cephes_vs_amos(special.yv, special.yn, rtol=1e-11, atol=1e-305)
  2308. def test_yv_cephes_vs_amos_only_small_orders(self):
  2309. skipper = lambda v, z: (abs(v) > 50)
  2310. self.check_cephes_vs_amos(special.yv, special.yn, rtol=1e-11, atol=1e-305, skip=skipper)
  2311. def test_iv_cephes_vs_amos(self):
  2312. with np.errstate(all='ignore'):
  2313. self.check_cephes_vs_amos(special.iv, special.iv, rtol=5e-9, atol=1e-305)
  2314. @pytest.mark.slow
  2315. def test_iv_cephes_vs_amos_mass_test(self):
  2316. N = 1000000
  2317. np.random.seed(1)
  2318. v = np.random.pareto(0.5, N) * (-1)**np.random.randint(2, size=N)
  2319. x = np.random.pareto(0.2, N) * (-1)**np.random.randint(2, size=N)
  2320. imsk = (np.random.randint(8, size=N) == 0)
  2321. v[imsk] = v[imsk].astype(int)
  2322. with np.errstate(all='ignore'):
  2323. c1 = special.iv(v, x)
  2324. c2 = special.iv(v, x+0j)
  2325. # deal with differences in the inf and zero cutoffs
  2326. c1[abs(c1) > 1e300] = np.inf
  2327. c2[abs(c2) > 1e300] = np.inf
  2328. c1[abs(c1) < 1e-300] = 0
  2329. c2[abs(c2) < 1e-300] = 0
  2330. dc = abs(c1/c2 - 1)
  2331. dc[np.isnan(dc)] = 0
  2332. k = np.argmax(dc)
  2333. # Most error apparently comes from AMOS and not our implementation;
  2334. # there are some problems near integer orders there
  2335. assert_(dc[k] < 2e-7, (v[k], x[k], special.iv(v[k], x[k]), special.iv(v[k], x[k]+0j)))
  2336. def test_kv_cephes_vs_amos(self):
  2337. self.check_cephes_vs_amos(special.kv, special.kn, rtol=1e-9, atol=1e-305)
  2338. self.check_cephes_vs_amos(special.kv, special.kv, rtol=1e-9, atol=1e-305)
  2339. def test_ticket_623(self):
  2340. assert_allclose(special.jv(3, 4), 0.43017147387562193)
  2341. assert_allclose(special.jv(301, 1300), 0.0183487151115275)
  2342. assert_allclose(special.jv(301, 1296.0682), -0.0224174325312048)
  2343. def test_ticket_853(self):
  2344. """Negative-order Bessels"""
  2345. # cephes
  2346. assert_allclose(special.jv(-1, 1), -0.4400505857449335)
  2347. assert_allclose(special.jv(-2, 1), 0.1149034849319005)
  2348. assert_allclose(special.yv(-1, 1), 0.7812128213002887)
  2349. assert_allclose(special.yv(-2, 1), -1.650682606816255)
  2350. assert_allclose(special.iv(-1, 1), 0.5651591039924851)
  2351. assert_allclose(special.iv(-2, 1), 0.1357476697670383)
  2352. assert_allclose(special.kv(-1, 1), 0.6019072301972347)
  2353. assert_allclose(special.kv(-2, 1), 1.624838898635178)
  2354. assert_allclose(special.jv(-0.5, 1), 0.43109886801837607952)
  2355. assert_allclose(special.yv(-0.5, 1), 0.6713967071418031)
  2356. assert_allclose(special.iv(-0.5, 1), 1.231200214592967)
  2357. assert_allclose(special.kv(-0.5, 1), 0.4610685044478945)
  2358. # amos
  2359. assert_allclose(special.jv(-1, 1+0j), -0.4400505857449335)
  2360. assert_allclose(special.jv(-2, 1+0j), 0.1149034849319005)
  2361. assert_allclose(special.yv(-1, 1+0j), 0.7812128213002887)
  2362. assert_allclose(special.yv(-2, 1+0j), -1.650682606816255)
  2363. assert_allclose(special.iv(-1, 1+0j), 0.5651591039924851)
  2364. assert_allclose(special.iv(-2, 1+0j), 0.1357476697670383)
  2365. assert_allclose(special.kv(-1, 1+0j), 0.6019072301972347)
  2366. assert_allclose(special.kv(-2, 1+0j), 1.624838898635178)
  2367. assert_allclose(special.jv(-0.5, 1+0j), 0.43109886801837607952)
  2368. assert_allclose(special.jv(-0.5, 1+1j), 0.2628946385649065-0.827050182040562j)
  2369. assert_allclose(special.yv(-0.5, 1+0j), 0.6713967071418031)
  2370. assert_allclose(special.yv(-0.5, 1+1j), 0.967901282890131+0.0602046062142816j)
  2371. assert_allclose(special.iv(-0.5, 1+0j), 1.231200214592967)
  2372. assert_allclose(special.iv(-0.5, 1+1j), 0.77070737376928+0.39891821043561j)
  2373. assert_allclose(special.kv(-0.5, 1+0j), 0.4610685044478945)
  2374. assert_allclose(special.kv(-0.5, 1+1j), 0.06868578341999-0.38157825981268j)
  2375. assert_allclose(special.jve(-0.5,1+0.3j), special.jv(-0.5, 1+0.3j)*exp(-0.3))
  2376. assert_allclose(special.yve(-0.5,1+0.3j), special.yv(-0.5, 1+0.3j)*exp(-0.3))
  2377. assert_allclose(special.ive(-0.5,0.3+1j), special.iv(-0.5, 0.3+1j)*exp(-0.3))
  2378. assert_allclose(special.kve(-0.5,0.3+1j), special.kv(-0.5, 0.3+1j)*exp(0.3+1j))
  2379. assert_allclose(special.hankel1(-0.5, 1+1j), special.jv(-0.5, 1+1j) + 1j*special.yv(-0.5,1+1j))
  2380. assert_allclose(special.hankel2(-0.5, 1+1j), special.jv(-0.5, 1+1j) - 1j*special.yv(-0.5,1+1j))
  2381. def test_ticket_854(self):
  2382. """Real-valued Bessel domains"""
  2383. assert_(isnan(special.jv(0.5, -1)))
  2384. assert_(isnan(special.iv(0.5, -1)))
  2385. assert_(isnan(special.yv(0.5, -1)))
  2386. assert_(isnan(special.yv(1, -1)))
  2387. assert_(isnan(special.kv(0.5, -1)))
  2388. assert_(isnan(special.kv(1, -1)))
  2389. assert_(isnan(special.jve(0.5, -1)))
  2390. assert_(isnan(special.ive(0.5, -1)))
  2391. assert_(isnan(special.yve(0.5, -1)))
  2392. assert_(isnan(special.yve(1, -1)))
  2393. assert_(isnan(special.kve(0.5, -1)))
  2394. assert_(isnan(special.kve(1, -1)))
  2395. assert_(isnan(special.airye(-1)[0:2]).all(), special.airye(-1))
  2396. assert_(not isnan(special.airye(-1)[2:4]).any(), special.airye(-1))
  2397. def test_gh_7909(self):
  2398. assert_(special.kv(1.5, 0) == np.inf)
  2399. assert_(special.kve(1.5, 0) == np.inf)
  2400. def test_ticket_503(self):
  2401. """Real-valued Bessel I overflow"""
  2402. assert_allclose(special.iv(1, 700), 1.528500390233901e302)
  2403. assert_allclose(special.iv(1000, 1120), 1.301564549405821e301)
  2404. def test_iv_hyperg_poles(self):
  2405. assert_allclose(special.iv(-0.5, 1), 1.231200214592967)
  2406. def iv_series(self, v, z, n=200):
  2407. k = arange(0, n).astype(float_)
  2408. r = (v+2*k)*log(.5*z) - special.gammaln(k+1) - special.gammaln(v+k+1)
  2409. r[isnan(r)] = inf
  2410. r = exp(r)
  2411. err = abs(r).max() * finfo(float_).eps * n + abs(r[-1])*10
  2412. return r.sum(), err
  2413. def test_i0_series(self):
  2414. for z in [1., 10., 200.5]:
  2415. value, err = self.iv_series(0, z)
  2416. assert_allclose(special.i0(z), value, atol=err, err_msg=z)
  2417. def test_i1_series(self):
  2418. for z in [1., 10., 200.5]:
  2419. value, err = self.iv_series(1, z)
  2420. assert_allclose(special.i1(z), value, atol=err, err_msg=z)
  2421. def test_iv_series(self):
  2422. for v in [-20., -10., -1., 0., 1., 12.49, 120.]:
  2423. for z in [1., 10., 200.5, -1+2j]:
  2424. value, err = self.iv_series(v, z)
  2425. assert_allclose(special.iv(v, z), value, atol=err, err_msg=(v, z))
  2426. def test_i0(self):
  2427. values = [[0.0, 1.0],
  2428. [1e-10, 1.0],
  2429. [0.1, 0.9071009258],
  2430. [0.5, 0.6450352706],
  2431. [1.0, 0.4657596077],
  2432. [2.5, 0.2700464416],
  2433. [5.0, 0.1835408126],
  2434. [20.0, 0.0897803119],
  2435. ]
  2436. for i, (x, v) in enumerate(values):
  2437. cv = special.i0(x) * exp(-x)
  2438. assert_almost_equal(cv, v, 8, err_msg='test #%d' % i)
  2439. def test_i0e(self):
  2440. oize = special.i0e(.1)
  2441. oizer = special.ive(0,.1)
  2442. assert_almost_equal(oize,oizer,8)
  2443. def test_i1(self):
  2444. values = [[0.0, 0.0],
  2445. [1e-10, 0.4999999999500000e-10],
  2446. [0.1, 0.0452984468],
  2447. [0.5, 0.1564208032],
  2448. [1.0, 0.2079104154],
  2449. [5.0, 0.1639722669],
  2450. [20.0, 0.0875062222],
  2451. ]
  2452. for i, (x, v) in enumerate(values):
  2453. cv = special.i1(x) * exp(-x)
  2454. assert_almost_equal(cv, v, 8, err_msg='test #%d' % i)
  2455. def test_i1e(self):
  2456. oi1e = special.i1e(.1)
  2457. oi1er = special.ive(1,.1)
  2458. assert_almost_equal(oi1e,oi1er,8)
  2459. def test_iti0k0(self):
  2460. iti0 = array(special.iti0k0(5))
  2461. assert_array_almost_equal(iti0,array([31.848667776169801, 1.5673873907283657]),5)
  2462. def test_it2i0k0(self):
  2463. it2k = special.it2i0k0(.1)
  2464. assert_array_almost_equal(it2k,array([0.0012503906973464409, 3.3309450354686687]),6)
  2465. def test_iv(self):
  2466. iv1 = special.iv(0,.1)*exp(-.1)
  2467. assert_almost_equal(iv1,0.90710092578230106,10)
  2468. def test_negv_ive(self):
  2469. assert_equal(special.ive(3,2), special.ive(-3,2))
  2470. def test_ive(self):
  2471. ive1 = special.ive(0,.1)
  2472. iv1 = special.iv(0,.1)*exp(-.1)
  2473. assert_almost_equal(ive1,iv1,10)
  2474. def test_ivp0(self):
  2475. assert_almost_equal(special.iv(1,2), special.ivp(0,2), 10)
  2476. def test_ivp(self):
  2477. y = (special.iv(0,2) + special.iv(2,2))/2
  2478. x = special.ivp(1,2)
  2479. assert_almost_equal(x,y,10)
  2480. class TestLaguerre:
  2481. def test_laguerre(self):
  2482. lag0 = special.laguerre(0)
  2483. lag1 = special.laguerre(1)
  2484. lag2 = special.laguerre(2)
  2485. lag3 = special.laguerre(3)
  2486. lag4 = special.laguerre(4)
  2487. lag5 = special.laguerre(5)
  2488. assert_array_almost_equal(lag0.c,[1],13)
  2489. assert_array_almost_equal(lag1.c,[-1,1],13)
  2490. assert_array_almost_equal(lag2.c,array([1,-4,2])/2.0,13)
  2491. assert_array_almost_equal(lag3.c,array([-1,9,-18,6])/6.0,13)
  2492. assert_array_almost_equal(lag4.c,array([1,-16,72,-96,24])/24.0,13)
  2493. assert_array_almost_equal(lag5.c,array([-1,25,-200,600,-600,120])/120.0,13)
  2494. def test_genlaguerre(self):
  2495. k = 5*np.random.random() - 0.9
  2496. lag0 = special.genlaguerre(0,k)
  2497. lag1 = special.genlaguerre(1,k)
  2498. lag2 = special.genlaguerre(2,k)
  2499. lag3 = special.genlaguerre(3,k)
  2500. assert_equal(lag0.c,[1])
  2501. assert_equal(lag1.c,[-1,k+1])
  2502. assert_almost_equal(lag2.c,array([1,-2*(k+2),(k+1.)*(k+2.)])/2.0)
  2503. assert_almost_equal(lag3.c,array([-1,3*(k+3),-3*(k+2)*(k+3),(k+1)*(k+2)*(k+3)])/6.0)
  2504. # Base polynomials come from Abrahmowitz and Stegan
  2505. class TestLegendre:
  2506. def test_legendre(self):
  2507. leg0 = special.legendre(0)
  2508. leg1 = special.legendre(1)
  2509. leg2 = special.legendre(2)
  2510. leg3 = special.legendre(3)
  2511. leg4 = special.legendre(4)
  2512. leg5 = special.legendre(5)
  2513. assert_equal(leg0.c, [1])
  2514. assert_equal(leg1.c, [1,0])
  2515. assert_almost_equal(leg2.c, array([3,0,-1])/2.0, decimal=13)
  2516. assert_almost_equal(leg3.c, array([5,0,-3,0])/2.0)
  2517. assert_almost_equal(leg4.c, array([35,0,-30,0,3])/8.0)
  2518. assert_almost_equal(leg5.c, array([63,0,-70,0,15,0])/8.0)
  2519. class TestLambda:
  2520. def test_lmbda(self):
  2521. lam = special.lmbda(1,.1)
  2522. lamr = (array([special.jn(0,.1), 2*special.jn(1,.1)/.1]),
  2523. array([special.jvp(0,.1), -2*special.jv(1,.1)/.01 + 2*special.jvp(1,.1)/.1]))
  2524. assert_array_almost_equal(lam,lamr,8)
  2525. class TestLog1p:
  2526. def test_log1p(self):
  2527. l1p = (special.log1p(10), special.log1p(11), special.log1p(12))
  2528. l1prl = (log(11), log(12), log(13))
  2529. assert_array_almost_equal(l1p,l1prl,8)
  2530. def test_log1pmore(self):
  2531. l1pm = (special.log1p(1), special.log1p(1.1), special.log1p(1.2))
  2532. l1pmrl = (log(2),log(2.1),log(2.2))
  2533. assert_array_almost_equal(l1pm,l1pmrl,8)
  2534. class TestLegendreFunctions:
  2535. def test_clpmn(self):
  2536. z = 0.5+0.3j
  2537. clp = special.clpmn(2, 2, z, 3)
  2538. assert_array_almost_equal(clp,
  2539. (array([[1.0000, z, 0.5*(3*z*z-1)],
  2540. [0.0000, sqrt(z*z-1), 3*z*sqrt(z*z-1)],
  2541. [0.0000, 0.0000, 3*(z*z-1)]]),
  2542. array([[0.0000, 1.0000, 3*z],
  2543. [0.0000, z/sqrt(z*z-1), 3*(2*z*z-1)/sqrt(z*z-1)],
  2544. [0.0000, 0.0000, 6*z]])),
  2545. 7)
  2546. def test_clpmn_close_to_real_2(self):
  2547. eps = 1e-10
  2548. m = 1
  2549. n = 3
  2550. x = 0.5
  2551. clp_plus = special.clpmn(m, n, x+1j*eps, 2)[0][m, n]
  2552. clp_minus = special.clpmn(m, n, x-1j*eps, 2)[0][m, n]
  2553. assert_array_almost_equal(array([clp_plus, clp_minus]),
  2554. array([special.lpmv(m, n, x),
  2555. special.lpmv(m, n, x)]),
  2556. 7)
  2557. def test_clpmn_close_to_real_3(self):
  2558. eps = 1e-10
  2559. m = 1
  2560. n = 3
  2561. x = 0.5
  2562. clp_plus = special.clpmn(m, n, x+1j*eps, 3)[0][m, n]
  2563. clp_minus = special.clpmn(m, n, x-1j*eps, 3)[0][m, n]
  2564. assert_array_almost_equal(array([clp_plus, clp_minus]),
  2565. array([special.lpmv(m, n, x)*np.exp(-0.5j*m*np.pi),
  2566. special.lpmv(m, n, x)*np.exp(0.5j*m*np.pi)]),
  2567. 7)
  2568. def test_clpmn_across_unit_circle(self):
  2569. eps = 1e-7
  2570. m = 1
  2571. n = 1
  2572. x = 1j
  2573. for type in [2, 3]:
  2574. assert_almost_equal(special.clpmn(m, n, x+1j*eps, type)[0][m, n],
  2575. special.clpmn(m, n, x-1j*eps, type)[0][m, n], 6)
  2576. def test_inf(self):
  2577. for z in (1, -1):
  2578. for n in range(4):
  2579. for m in range(1, n):
  2580. lp = special.clpmn(m, n, z)
  2581. assert_(np.isinf(lp[1][1,1:]).all())
  2582. lp = special.lpmn(m, n, z)
  2583. assert_(np.isinf(lp[1][1,1:]).all())
  2584. def test_deriv_clpmn(self):
  2585. # data inside and outside of the unit circle
  2586. zvals = [0.5+0.5j, -0.5+0.5j, -0.5-0.5j, 0.5-0.5j,
  2587. 1+1j, -1+1j, -1-1j, 1-1j]
  2588. m = 2
  2589. n = 3
  2590. for type in [2, 3]:
  2591. for z in zvals:
  2592. for h in [1e-3, 1e-3j]:
  2593. approx_derivative = (special.clpmn(m, n, z+0.5*h, type)[0]
  2594. - special.clpmn(m, n, z-0.5*h, type)[0])/h
  2595. assert_allclose(special.clpmn(m, n, z, type)[1],
  2596. approx_derivative,
  2597. rtol=1e-4)
  2598. def test_lpmn(self):
  2599. lp = special.lpmn(0,2,.5)
  2600. assert_array_almost_equal(lp,(array([[1.00000,
  2601. 0.50000,
  2602. -0.12500]]),
  2603. array([[0.00000,
  2604. 1.00000,
  2605. 1.50000]])),4)
  2606. def test_lpn(self):
  2607. lpnf = special.lpn(2,.5)
  2608. assert_array_almost_equal(lpnf,(array([1.00000,
  2609. 0.50000,
  2610. -0.12500]),
  2611. array([0.00000,
  2612. 1.00000,
  2613. 1.50000])),4)
  2614. def test_lpmv(self):
  2615. lp = special.lpmv(0,2,.5)
  2616. assert_almost_equal(lp,-0.125,7)
  2617. lp = special.lpmv(0,40,.001)
  2618. assert_almost_equal(lp,0.1252678976534484,7)
  2619. # XXX: this is outside the domain of the current implementation,
  2620. # so ensure it returns a NaN rather than a wrong answer.
  2621. with np.errstate(all='ignore'):
  2622. lp = special.lpmv(-1,-1,.001)
  2623. assert_(lp != 0 or np.isnan(lp))
  2624. def test_lqmn(self):
  2625. lqmnf = special.lqmn(0,2,.5)
  2626. lqf = special.lqn(2,.5)
  2627. assert_array_almost_equal(lqmnf[0][0],lqf[0],4)
  2628. assert_array_almost_equal(lqmnf[1][0],lqf[1],4)
  2629. def test_lqmn_gt1(self):
  2630. """algorithm for real arguments changes at 1.0001
  2631. test against analytical result for m=2, n=1
  2632. """
  2633. x0 = 1.0001
  2634. delta = 0.00002
  2635. for x in (x0-delta, x0+delta):
  2636. lq = special.lqmn(2, 1, x)[0][-1, -1]
  2637. expected = 2/(x*x-1)
  2638. assert_almost_equal(lq, expected)
  2639. def test_lqmn_shape(self):
  2640. a, b = special.lqmn(4, 4, 1.1)
  2641. assert_equal(a.shape, (5, 5))
  2642. assert_equal(b.shape, (5, 5))
  2643. a, b = special.lqmn(4, 0, 1.1)
  2644. assert_equal(a.shape, (5, 1))
  2645. assert_equal(b.shape, (5, 1))
  2646. def test_lqn(self):
  2647. lqf = special.lqn(2,.5)
  2648. assert_array_almost_equal(lqf,(array([0.5493, -0.7253, -0.8187]),
  2649. array([1.3333, 1.216, -0.8427])),4)
  2650. class TestMathieu:
  2651. def test_mathieu_a(self):
  2652. pass
  2653. def test_mathieu_even_coef(self):
  2654. special.mathieu_even_coef(2,5)
  2655. # Q not defined broken and cannot figure out proper reporting order
  2656. def test_mathieu_odd_coef(self):
  2657. # same problem as above
  2658. pass
  2659. class TestFresnelIntegral:
  2660. def test_modfresnelp(self):
  2661. pass
  2662. def test_modfresnelm(self):
  2663. pass
  2664. class TestOblCvSeq:
  2665. def test_obl_cv_seq(self):
  2666. obl = special.obl_cv_seq(0,3,1)
  2667. assert_array_almost_equal(obl,array([-0.348602,
  2668. 1.393206,
  2669. 5.486800,
  2670. 11.492120]),5)
  2671. class TestParabolicCylinder:
  2672. def test_pbdn_seq(self):
  2673. pb = special.pbdn_seq(1,.1)
  2674. assert_array_almost_equal(pb,(array([0.9975,
  2675. 0.0998]),
  2676. array([-0.0499,
  2677. 0.9925])),4)
  2678. def test_pbdv(self):
  2679. special.pbdv(1,.2)
  2680. 1/2*(.2)*special.pbdv(1,.2)[0] - special.pbdv(0,.2)[0]
  2681. def test_pbdv_seq(self):
  2682. pbn = special.pbdn_seq(1,.1)
  2683. pbv = special.pbdv_seq(1,.1)
  2684. assert_array_almost_equal(pbv,(real(pbn[0]),real(pbn[1])),4)
  2685. def test_pbdv_points(self):
  2686. # simple case
  2687. eta = np.linspace(-10, 10, 5)
  2688. z = 2**(eta/2)*np.sqrt(np.pi)/special.gamma(.5-.5*eta)
  2689. assert_allclose(special.pbdv(eta, 0.)[0], z, rtol=1e-14, atol=1e-14)
  2690. # some points
  2691. assert_allclose(special.pbdv(10.34, 20.44)[0], 1.3731383034455e-32, rtol=1e-12)
  2692. assert_allclose(special.pbdv(-9.53, 3.44)[0], 3.166735001119246e-8, rtol=1e-12)
  2693. def test_pbdv_gradient(self):
  2694. x = np.linspace(-4, 4, 8)[:,None]
  2695. eta = np.linspace(-10, 10, 5)[None,:]
  2696. p = special.pbdv(eta, x)
  2697. eps = 1e-7 + 1e-7*abs(x)
  2698. dp = (special.pbdv(eta, x + eps)[0] - special.pbdv(eta, x - eps)[0]) / eps / 2.
  2699. assert_allclose(p[1], dp, rtol=1e-6, atol=1e-6)
  2700. def test_pbvv_gradient(self):
  2701. x = np.linspace(-4, 4, 8)[:,None]
  2702. eta = np.linspace(-10, 10, 5)[None,:]
  2703. p = special.pbvv(eta, x)
  2704. eps = 1e-7 + 1e-7*abs(x)
  2705. dp = (special.pbvv(eta, x + eps)[0] - special.pbvv(eta, x - eps)[0]) / eps / 2.
  2706. assert_allclose(p[1], dp, rtol=1e-6, atol=1e-6)
  2707. class TestPolygamma:
  2708. # from Table 6.2 (pg. 271) of A&S
  2709. def test_polygamma(self):
  2710. poly2 = special.polygamma(2,1)
  2711. poly3 = special.polygamma(3,1)
  2712. assert_almost_equal(poly2,-2.4041138063,10)
  2713. assert_almost_equal(poly3,6.4939394023,10)
  2714. # Test polygamma(0, x) == psi(x)
  2715. x = [2, 3, 1.1e14]
  2716. assert_almost_equal(special.polygamma(0, x), special.psi(x))
  2717. # Test broadcasting
  2718. n = [0, 1, 2]
  2719. x = [0.5, 1.5, 2.5]
  2720. expected = [-1.9635100260214238, 0.93480220054467933,
  2721. -0.23620405164172739]
  2722. assert_almost_equal(special.polygamma(n, x), expected)
  2723. expected = np.row_stack([expected]*2)
  2724. assert_almost_equal(special.polygamma(n, np.row_stack([x]*2)),
  2725. expected)
  2726. assert_almost_equal(special.polygamma(np.row_stack([n]*2), x),
  2727. expected)
  2728. class TestProCvSeq:
  2729. def test_pro_cv_seq(self):
  2730. prol = special.pro_cv_seq(0,3,1)
  2731. assert_array_almost_equal(prol,array([0.319000,
  2732. 2.593084,
  2733. 6.533471,
  2734. 12.514462]),5)
  2735. class TestPsi:
  2736. def test_psi(self):
  2737. ps = special.psi(1)
  2738. assert_almost_equal(ps,-0.57721566490153287,8)
  2739. class TestRadian:
  2740. def test_radian(self):
  2741. rad = special.radian(90,0,0)
  2742. assert_almost_equal(rad,pi/2.0,5)
  2743. def test_radianmore(self):
  2744. rad1 = special.radian(90,1,60)
  2745. assert_almost_equal(rad1,pi/2+0.0005816135199345904,5)
  2746. class TestRiccati:
  2747. def test_riccati_jn(self):
  2748. N, x = 2, 0.2
  2749. S = np.empty((N, N))
  2750. for n in range(N):
  2751. j = special.spherical_jn(n, x)
  2752. jp = special.spherical_jn(n, x, derivative=True)
  2753. S[0,n] = x*j
  2754. S[1,n] = x*jp + j
  2755. assert_array_almost_equal(S, special.riccati_jn(n, x), 8)
  2756. def test_riccati_yn(self):
  2757. N, x = 2, 0.2
  2758. C = np.empty((N, N))
  2759. for n in range(N):
  2760. y = special.spherical_yn(n, x)
  2761. yp = special.spherical_yn(n, x, derivative=True)
  2762. C[0,n] = x*y
  2763. C[1,n] = x*yp + y
  2764. assert_array_almost_equal(C, special.riccati_yn(n, x), 8)
  2765. class TestRound:
  2766. def test_round(self):
  2767. rnd = list(map(int,(special.round(10.1),special.round(10.4),special.round(10.5),special.round(10.6))))
  2768. # Note: According to the documentation, scipy.special.round is
  2769. # supposed to round to the nearest even number if the fractional
  2770. # part is exactly 0.5. On some platforms, this does not appear
  2771. # to work and thus this test may fail. However, this unit test is
  2772. # correctly written.
  2773. rndrl = (10,10,10,11)
  2774. assert_array_equal(rnd,rndrl)
  2775. def test_sph_harm():
  2776. # Tests derived from tables in
  2777. # https://en.wikipedia.org/wiki/Table_of_spherical_harmonics
  2778. sh = special.sph_harm
  2779. pi = np.pi
  2780. exp = np.exp
  2781. sqrt = np.sqrt
  2782. sin = np.sin
  2783. cos = np.cos
  2784. assert_array_almost_equal(sh(0,0,0,0),
  2785. 0.5/sqrt(pi))
  2786. assert_array_almost_equal(sh(-2,2,0.,pi/4),
  2787. 0.25*sqrt(15./(2.*pi)) *
  2788. (sin(pi/4))**2.)
  2789. assert_array_almost_equal(sh(-2,2,0.,pi/2),
  2790. 0.25*sqrt(15./(2.*pi)))
  2791. assert_array_almost_equal(sh(2,2,pi,pi/2),
  2792. 0.25*sqrt(15/(2.*pi)) *
  2793. exp(0+2.*pi*1j)*sin(pi/2.)**2.)
  2794. assert_array_almost_equal(sh(2,4,pi/4.,pi/3.),
  2795. (3./8.)*sqrt(5./(2.*pi)) *
  2796. exp(0+2.*pi/4.*1j) *
  2797. sin(pi/3.)**2. *
  2798. (7.*cos(pi/3.)**2.-1))
  2799. assert_array_almost_equal(sh(4,4,pi/8.,pi/6.),
  2800. (3./16.)*sqrt(35./(2.*pi)) *
  2801. exp(0+4.*pi/8.*1j)*sin(pi/6.)**4.)
  2802. def test_sph_harm_ufunc_loop_selection():
  2803. # see https://github.com/scipy/scipy/issues/4895
  2804. dt = np.dtype(np.complex128)
  2805. assert_equal(special.sph_harm(0, 0, 0, 0).dtype, dt)
  2806. assert_equal(special.sph_harm([0], 0, 0, 0).dtype, dt)
  2807. assert_equal(special.sph_harm(0, [0], 0, 0).dtype, dt)
  2808. assert_equal(special.sph_harm(0, 0, [0], 0).dtype, dt)
  2809. assert_equal(special.sph_harm(0, 0, 0, [0]).dtype, dt)
  2810. assert_equal(special.sph_harm([0], [0], [0], [0]).dtype, dt)
  2811. class TestStruve:
  2812. def _series(self, v, z, n=100):
  2813. """Compute Struve function & error estimate from its power series."""
  2814. k = arange(0, n)
  2815. r = (-1)**k * (.5*z)**(2*k+v+1)/special.gamma(k+1.5)/special.gamma(k+v+1.5)
  2816. err = abs(r).max() * finfo(float_).eps * n
  2817. return r.sum(), err
  2818. def test_vs_series(self):
  2819. """Check Struve function versus its power series"""
  2820. for v in [-20, -10, -7.99, -3.4, -1, 0, 1, 3.4, 12.49, 16]:
  2821. for z in [1, 10, 19, 21, 30]:
  2822. value, err = self._series(v, z)
  2823. assert_allclose(special.struve(v, z), value, rtol=0, atol=err), (v, z)
  2824. def test_some_values(self):
  2825. assert_allclose(special.struve(-7.99, 21), 0.0467547614113, rtol=1e-7)
  2826. assert_allclose(special.struve(-8.01, 21), 0.0398716951023, rtol=1e-8)
  2827. assert_allclose(special.struve(-3.0, 200), 0.0142134427432, rtol=1e-12)
  2828. assert_allclose(special.struve(-8.0, -41), 0.0192469727846, rtol=1e-11)
  2829. assert_equal(special.struve(-12, -41), -special.struve(-12, 41))
  2830. assert_equal(special.struve(+12, -41), -special.struve(+12, 41))
  2831. assert_equal(special.struve(-11, -41), +special.struve(-11, 41))
  2832. assert_equal(special.struve(+11, -41), +special.struve(+11, 41))
  2833. assert_(isnan(special.struve(-7.1, -1)))
  2834. assert_(isnan(special.struve(-10.1, -1)))
  2835. def test_regression_679(self):
  2836. """Regression test for #679"""
  2837. assert_allclose(special.struve(-1.0, 20 - 1e-8), special.struve(-1.0, 20 + 1e-8))
  2838. assert_allclose(special.struve(-2.0, 20 - 1e-8), special.struve(-2.0, 20 + 1e-8))
  2839. assert_allclose(special.struve(-4.3, 20 - 1e-8), special.struve(-4.3, 20 + 1e-8))
  2840. def test_chi2_smalldf():
  2841. assert_almost_equal(special.chdtr(0.6,3), 0.957890536704110)
  2842. def test_ch2_inf():
  2843. assert_equal(special.chdtr(0.7,np.inf), 1.0)
  2844. def test_chi2c_smalldf():
  2845. assert_almost_equal(special.chdtrc(0.6,3), 1-0.957890536704110)
  2846. def test_chi2_inv_smalldf():
  2847. assert_almost_equal(special.chdtri(0.6,1-0.957890536704110), 3)
  2848. def test_agm_simple():
  2849. rtol = 1e-13
  2850. # Gauss's constant
  2851. assert_allclose(1/special.agm(1, np.sqrt(2)), 0.834626841674073186,
  2852. rtol=rtol)
  2853. # These values were computed using Wolfram Alpha, with the
  2854. # function ArithmeticGeometricMean[a, b].
  2855. agm13 = 1.863616783244897
  2856. agm15 = 2.604008190530940
  2857. agm35 = 3.936235503649555
  2858. assert_allclose(special.agm([[1], [3]], [1, 3, 5]),
  2859. [[1, agm13, agm15],
  2860. [agm13, 3, agm35]], rtol=rtol)
  2861. # Computed by the iteration formula using mpmath,
  2862. # with mpmath.mp.prec = 1000:
  2863. agm12 = 1.4567910310469068
  2864. assert_allclose(special.agm(1, 2), agm12, rtol=rtol)
  2865. assert_allclose(special.agm(2, 1), agm12, rtol=rtol)
  2866. assert_allclose(special.agm(-1, -2), -agm12, rtol=rtol)
  2867. assert_allclose(special.agm(24, 6), 13.458171481725614, rtol=rtol)
  2868. assert_allclose(special.agm(13, 123456789.5), 11111458.498599306,
  2869. rtol=rtol)
  2870. assert_allclose(special.agm(1e30, 1), 2.229223055945383e+28, rtol=rtol)
  2871. assert_allclose(special.agm(1e-22, 1), 0.030182566420169886, rtol=rtol)
  2872. assert_allclose(special.agm(1e150, 1e180), 2.229223055945383e+178,
  2873. rtol=rtol)
  2874. assert_allclose(special.agm(1e180, 1e-150), 2.0634722510162677e+177,
  2875. rtol=rtol)
  2876. assert_allclose(special.agm(1e-150, 1e-170), 3.3112619670463756e-152,
  2877. rtol=rtol)
  2878. fi = np.finfo(1.0)
  2879. assert_allclose(special.agm(fi.tiny, fi.max), 1.9892072050015473e+305,
  2880. rtol=rtol)
  2881. assert_allclose(special.agm(0.75*fi.max, fi.max), 1.564904312298045e+308,
  2882. rtol=rtol)
  2883. assert_allclose(special.agm(fi.tiny, 3*fi.tiny), 4.1466849866735005e-308,
  2884. rtol=rtol)
  2885. # zero, nan and inf cases.
  2886. assert_equal(special.agm(0, 0), 0)
  2887. assert_equal(special.agm(99, 0), 0)
  2888. assert_equal(special.agm(-1, 10), np.nan)
  2889. assert_equal(special.agm(0, np.inf), np.nan)
  2890. assert_equal(special.agm(np.inf, 0), np.nan)
  2891. assert_equal(special.agm(0, -np.inf), np.nan)
  2892. assert_equal(special.agm(-np.inf, 0), np.nan)
  2893. assert_equal(special.agm(np.inf, -np.inf), np.nan)
  2894. assert_equal(special.agm(-np.inf, np.inf), np.nan)
  2895. assert_equal(special.agm(1, np.nan), np.nan)
  2896. assert_equal(special.agm(np.nan, -1), np.nan)
  2897. assert_equal(special.agm(1, np.inf), np.inf)
  2898. assert_equal(special.agm(np.inf, 1), np.inf)
  2899. assert_equal(special.agm(-1, -np.inf), -np.inf)
  2900. assert_equal(special.agm(-np.inf, -1), -np.inf)
  2901. def test_legacy():
  2902. # Legacy behavior: truncating arguments to integers
  2903. with suppress_warnings() as sup:
  2904. sup.filter(RuntimeWarning, "floating point number truncated to an integer")
  2905. assert_equal(special.expn(1, 0.3), special.expn(1.8, 0.3))
  2906. assert_equal(special.nbdtrc(1, 2, 0.3), special.nbdtrc(1.8, 2.8, 0.3))
  2907. assert_equal(special.nbdtr(1, 2, 0.3), special.nbdtr(1.8, 2.8, 0.3))
  2908. assert_equal(special.nbdtri(1, 2, 0.3), special.nbdtri(1.8, 2.8, 0.3))
  2909. assert_equal(special.pdtri(1, 0.3), special.pdtri(1.8, 0.3))
  2910. assert_equal(special.kn(1, 0.3), special.kn(1.8, 0.3))
  2911. assert_equal(special.yn(1, 0.3), special.yn(1.8, 0.3))
  2912. assert_equal(special.smirnov(1, 0.3), special.smirnov(1.8, 0.3))
  2913. assert_equal(special.smirnovi(1, 0.3), special.smirnovi(1.8, 0.3))
  2914. @with_special_errors
  2915. def test_error_raising():
  2916. assert_raises(special.SpecialFunctionError, special.iv, 1, 1e99j)
  2917. def test_xlogy():
  2918. def xfunc(x, y):
  2919. with np.errstate(invalid='ignore'):
  2920. if x == 0 and not np.isnan(y):
  2921. return x
  2922. else:
  2923. return x*np.log(y)
  2924. z1 = np.asarray([(0,0), (0, np.nan), (0, np.inf), (1.0, 2.0)], dtype=float)
  2925. z2 = np.r_[z1, [(0, 1j), (1, 1j)]]
  2926. w1 = np.vectorize(xfunc)(z1[:,0], z1[:,1])
  2927. assert_func_equal(special.xlogy, w1, z1, rtol=1e-13, atol=1e-13)
  2928. w2 = np.vectorize(xfunc)(z2[:,0], z2[:,1])
  2929. assert_func_equal(special.xlogy, w2, z2, rtol=1e-13, atol=1e-13)
  2930. def test_xlog1py():
  2931. def xfunc(x, y):
  2932. with np.errstate(invalid='ignore'):
  2933. if x == 0 and not np.isnan(y):
  2934. return x
  2935. else:
  2936. return x * np.log1p(y)
  2937. z1 = np.asarray([(0,0), (0, np.nan), (0, np.inf), (1.0, 2.0),
  2938. (1, 1e-30)], dtype=float)
  2939. w1 = np.vectorize(xfunc)(z1[:,0], z1[:,1])
  2940. assert_func_equal(special.xlog1py, w1, z1, rtol=1e-13, atol=1e-13)
  2941. def test_entr():
  2942. def xfunc(x):
  2943. if x < 0:
  2944. return -np.inf
  2945. else:
  2946. return -special.xlogy(x, x)
  2947. values = (0, 0.5, 1.0, np.inf)
  2948. signs = [-1, 1]
  2949. arr = []
  2950. for sgn, v in itertools.product(signs, values):
  2951. arr.append(sgn * v)
  2952. z = np.array(arr, dtype=float)
  2953. w = np.vectorize(xfunc, otypes=[np.float64])(z)
  2954. assert_func_equal(special.entr, w, z, rtol=1e-13, atol=1e-13)
  2955. def test_kl_div():
  2956. def xfunc(x, y):
  2957. if x < 0 or y < 0 or (y == 0 and x != 0):
  2958. # extension of natural domain to preserve convexity
  2959. return np.inf
  2960. elif np.isposinf(x) or np.isposinf(y):
  2961. # limits within the natural domain
  2962. return np.inf
  2963. elif x == 0:
  2964. return y
  2965. else:
  2966. return special.xlogy(x, x/y) - x + y
  2967. values = (0, 0.5, 1.0)
  2968. signs = [-1, 1]
  2969. arr = []
  2970. for sgna, va, sgnb, vb in itertools.product(signs, values, signs, values):
  2971. arr.append((sgna*va, sgnb*vb))
  2972. z = np.array(arr, dtype=float)
  2973. w = np.vectorize(xfunc, otypes=[np.float64])(z[:,0], z[:,1])
  2974. assert_func_equal(special.kl_div, w, z, rtol=1e-13, atol=1e-13)
  2975. def test_rel_entr():
  2976. def xfunc(x, y):
  2977. if x > 0 and y > 0:
  2978. return special.xlogy(x, x/y)
  2979. elif x == 0 and y >= 0:
  2980. return 0
  2981. else:
  2982. return np.inf
  2983. values = (0, 0.5, 1.0)
  2984. signs = [-1, 1]
  2985. arr = []
  2986. for sgna, va, sgnb, vb in itertools.product(signs, values, signs, values):
  2987. arr.append((sgna*va, sgnb*vb))
  2988. z = np.array(arr, dtype=float)
  2989. w = np.vectorize(xfunc, otypes=[np.float64])(z[:,0], z[:,1])
  2990. assert_func_equal(special.rel_entr, w, z, rtol=1e-13, atol=1e-13)
  2991. def test_huber():
  2992. assert_equal(special.huber(-1, 1.5), np.inf)
  2993. assert_allclose(special.huber(2, 1.5), 0.5 * np.square(1.5))
  2994. assert_allclose(special.huber(2, 2.5), 2 * (2.5 - 0.5 * 2))
  2995. def xfunc(delta, r):
  2996. if delta < 0:
  2997. return np.inf
  2998. elif np.abs(r) < delta:
  2999. return 0.5 * np.square(r)
  3000. else:
  3001. return delta * (np.abs(r) - 0.5 * delta)
  3002. z = np.random.randn(10, 2)
  3003. w = np.vectorize(xfunc, otypes=[np.float64])(z[:,0], z[:,1])
  3004. assert_func_equal(special.huber, w, z, rtol=1e-13, atol=1e-13)
  3005. def test_pseudo_huber():
  3006. def xfunc(delta, r):
  3007. if delta < 0:
  3008. return np.inf
  3009. elif (not delta) or (not r):
  3010. return 0
  3011. else:
  3012. return delta**2 * (np.sqrt(1 + (r/delta)**2) - 1)
  3013. z = np.array(np.random.randn(10, 2).tolist() + [[0, 0.5], [0.5, 0]])
  3014. w = np.vectorize(xfunc, otypes=[np.float64])(z[:,0], z[:,1])
  3015. assert_func_equal(special.pseudo_huber, w, z, rtol=1e-13, atol=1e-13)
  3016. def test_pseudo_huber_small_r():
  3017. delta = 1.0
  3018. r = 1e-18
  3019. y = special.pseudo_huber(delta, r)
  3020. # expected computed with mpmath:
  3021. # import mpmath
  3022. # mpmath.mp.dps = 200
  3023. # r = mpmath.mpf(1e-18)
  3024. # expected = float(mpmath.sqrt(1 + r**2) - 1)
  3025. expected = 5.0000000000000005e-37
  3026. assert_allclose(y, expected, rtol=1e-13)
  3027. def test_runtime_warning():
  3028. with pytest.warns(RuntimeWarning,
  3029. match=r'Too many predicted coefficients'):
  3030. mathieu_odd_coef(1000, 1000)
  3031. with pytest.warns(RuntimeWarning,
  3032. match=r'Too many predicted coefficients'):
  3033. mathieu_even_coef(1000, 1000)