function_docs.py 277 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201
  1. """
  2. Extended docstrings for functions.py
  3. """
  4. pi = r"""
  5. `\pi`, roughly equal to 3.141592654, represents the area of the unit
  6. circle, the half-period of trigonometric functions, and many other
  7. things in mathematics.
  8. Mpmath can evaluate `\pi` to arbitrary precision::
  9. >>> from mpmath import *
  10. >>> mp.dps = 50; mp.pretty = True
  11. >>> +pi
  12. 3.1415926535897932384626433832795028841971693993751
  13. This shows digits 99991-100000 of `\pi` (the last digit is actually
  14. a 4 when the decimal expansion is truncated, but here the nearest
  15. rounding is used)::
  16. >>> mp.dps = 100000
  17. >>> str(pi)[-10:]
  18. '5549362465'
  19. **Possible issues**
  20. :data:`pi` always rounds to the nearest floating-point
  21. number when used. This means that exact mathematical identities
  22. involving `\pi` will generally not be preserved in floating-point
  23. arithmetic. In particular, multiples of :data:`pi` (except for
  24. the trivial case ``0*pi``) are *not* the exact roots of
  25. :func:`~mpmath.sin`, but differ roughly by the current epsilon::
  26. >>> mp.dps = 15
  27. >>> sin(pi)
  28. 1.22464679914735e-16
  29. One solution is to use the :func:`~mpmath.sinpi` function instead::
  30. >>> sinpi(1)
  31. 0.0
  32. See the documentation of trigonometric functions for additional
  33. details.
  34. **References**
  35. * [BorweinBorwein]_
  36. """
  37. degree = r"""
  38. Represents one degree of angle, `1^{\circ} = \pi/180`, or
  39. about 0.01745329. This constant may be evaluated to arbitrary
  40. precision::
  41. >>> from mpmath import *
  42. >>> mp.dps = 50; mp.pretty = True
  43. >>> +degree
  44. 0.017453292519943295769236907684886127134428718885417
  45. The :data:`degree` object is convenient for conversion
  46. to radians::
  47. >>> sin(30 * degree)
  48. 0.5
  49. >>> asin(0.5) / degree
  50. 30.0
  51. """
  52. e = r"""
  53. The transcendental number `e` = 2.718281828... is the base of the
  54. natural logarithm (:func:`~mpmath.ln`) and of the exponential function
  55. (:func:`~mpmath.exp`).
  56. Mpmath can be evaluate `e` to arbitrary precision::
  57. >>> from mpmath import *
  58. >>> mp.dps = 50; mp.pretty = True
  59. >>> +e
  60. 2.7182818284590452353602874713526624977572470937
  61. This shows digits 99991-100000 of `e` (the last digit is actually
  62. a 5 when the decimal expansion is truncated, but here the nearest
  63. rounding is used)::
  64. >>> mp.dps = 100000
  65. >>> str(e)[-10:]
  66. '2100427166'
  67. **Possible issues**
  68. :data:`e` always rounds to the nearest floating-point number
  69. when used, and mathematical identities involving `e` may not
  70. hold in floating-point arithmetic. For example, ``ln(e)``
  71. might not evaluate exactly to 1.
  72. In particular, don't use ``e**x`` to compute the exponential
  73. function. Use ``exp(x)`` instead; this is both faster and more
  74. accurate.
  75. """
  76. phi = r"""
  77. Represents the golden ratio `\phi = (1+\sqrt 5)/2`,
  78. approximately equal to 1.6180339887. To high precision,
  79. its value is::
  80. >>> from mpmath import *
  81. >>> mp.dps = 50; mp.pretty = True
  82. >>> +phi
  83. 1.6180339887498948482045868343656381177203091798058
  84. Formulas for the golden ratio include the following::
  85. >>> (1+sqrt(5))/2
  86. 1.6180339887498948482045868343656381177203091798058
  87. >>> findroot(lambda x: x**2-x-1, 1)
  88. 1.6180339887498948482045868343656381177203091798058
  89. >>> limit(lambda n: fib(n+1)/fib(n), inf)
  90. 1.6180339887498948482045868343656381177203091798058
  91. """
  92. euler = r"""
  93. Euler's constant or the Euler-Mascheroni constant `\gamma`
  94. = 0.57721566... is a number of central importance to
  95. number theory and special functions. It is defined as the limit
  96. .. math ::
  97. \gamma = \lim_{n\to\infty} H_n - \log n
  98. where `H_n = 1 + \frac{1}{2} + \ldots + \frac{1}{n}` is a harmonic
  99. number (see :func:`~mpmath.harmonic`).
  100. Evaluation of `\gamma` is supported at arbitrary precision::
  101. >>> from mpmath import *
  102. >>> mp.dps = 50; mp.pretty = True
  103. >>> +euler
  104. 0.57721566490153286060651209008240243104215933593992
  105. We can also compute `\gamma` directly from the definition,
  106. although this is less efficient::
  107. >>> limit(lambda n: harmonic(n)-log(n), inf)
  108. 0.57721566490153286060651209008240243104215933593992
  109. This shows digits 9991-10000 of `\gamma` (the last digit is actually
  110. a 5 when the decimal expansion is truncated, but here the nearest
  111. rounding is used)::
  112. >>> mp.dps = 10000
  113. >>> str(euler)[-10:]
  114. '4679858166'
  115. Integrals, series, and representations for `\gamma` in terms of
  116. special functions include the following (there are many others)::
  117. >>> mp.dps = 25
  118. >>> -quad(lambda x: exp(-x)*log(x), [0,inf])
  119. 0.5772156649015328606065121
  120. >>> quad(lambda x,y: (x-1)/(1-x*y)/log(x*y), [0,1], [0,1])
  121. 0.5772156649015328606065121
  122. >>> nsum(lambda k: 1/k-log(1+1/k), [1,inf])
  123. 0.5772156649015328606065121
  124. >>> nsum(lambda k: (-1)**k*zeta(k)/k, [2,inf])
  125. 0.5772156649015328606065121
  126. >>> -diff(gamma, 1)
  127. 0.5772156649015328606065121
  128. >>> limit(lambda x: 1/x-gamma(x), 0)
  129. 0.5772156649015328606065121
  130. >>> limit(lambda x: zeta(x)-1/(x-1), 1)
  131. 0.5772156649015328606065121
  132. >>> (log(2*pi*nprod(lambda n:
  133. ... exp(-2+2/n)*(1+2/n)**n, [1,inf]))-3)/2
  134. 0.5772156649015328606065121
  135. For generalizations of the identities `\gamma = -\Gamma'(1)`
  136. and `\gamma = \lim_{x\to1} \zeta(x)-1/(x-1)`, see
  137. :func:`~mpmath.psi` and :func:`~mpmath.stieltjes` respectively.
  138. **References**
  139. * [BorweinBailey]_
  140. """
  141. catalan = r"""
  142. Catalan's constant `K` = 0.91596559... is given by the infinite
  143. series
  144. .. math ::
  145. K = \sum_{k=0}^{\infty} \frac{(-1)^k}{(2k+1)^2}.
  146. Mpmath can evaluate it to arbitrary precision::
  147. >>> from mpmath import *
  148. >>> mp.dps = 50; mp.pretty = True
  149. >>> +catalan
  150. 0.91596559417721901505460351493238411077414937428167
  151. One can also compute `K` directly from the definition, although
  152. this is significantly less efficient::
  153. >>> nsum(lambda k: (-1)**k/(2*k+1)**2, [0, inf])
  154. 0.91596559417721901505460351493238411077414937428167
  155. This shows digits 9991-10000 of `K` (the last digit is actually
  156. a 3 when the decimal expansion is truncated, but here the nearest
  157. rounding is used)::
  158. >>> mp.dps = 10000
  159. >>> str(catalan)[-10:]
  160. '9537871504'
  161. Catalan's constant has numerous integral representations::
  162. >>> mp.dps = 50
  163. >>> quad(lambda x: -log(x)/(1+x**2), [0, 1])
  164. 0.91596559417721901505460351493238411077414937428167
  165. >>> quad(lambda x: atan(x)/x, [0, 1])
  166. 0.91596559417721901505460351493238411077414937428167
  167. >>> quad(lambda x: ellipk(x**2)/2, [0, 1])
  168. 0.91596559417721901505460351493238411077414937428167
  169. >>> quad(lambda x,y: 1/(1+(x*y)**2), [0, 1], [0, 1])
  170. 0.91596559417721901505460351493238411077414937428167
  171. As well as series representations::
  172. >>> pi*log(sqrt(3)+2)/8 + 3*nsum(lambda n:
  173. ... (fac(n)/(2*n+1))**2/fac(2*n), [0, inf])/8
  174. 0.91596559417721901505460351493238411077414937428167
  175. >>> 1-nsum(lambda n: n*zeta(2*n+1)/16**n, [1,inf])
  176. 0.91596559417721901505460351493238411077414937428167
  177. """
  178. khinchin = r"""
  179. Khinchin's constant `K` = 2.68542... is a number that
  180. appears in the theory of continued fractions. Mpmath can evaluate
  181. it to arbitrary precision::
  182. >>> from mpmath import *
  183. >>> mp.dps = 50; mp.pretty = True
  184. >>> +khinchin
  185. 2.6854520010653064453097148354817956938203822939945
  186. An integral representation is::
  187. >>> I = quad(lambda x: log((1-x**2)/sincpi(x))/x/(1+x), [0, 1])
  188. >>> 2*exp(1/log(2)*I)
  189. 2.6854520010653064453097148354817956938203822939945
  190. The computation of ``khinchin`` is based on an efficient
  191. implementation of the following series::
  192. >>> f = lambda n: (zeta(2*n)-1)/n*sum((-1)**(k+1)/mpf(k)
  193. ... for k in range(1,2*int(n)))
  194. >>> exp(nsum(f, [1,inf])/log(2))
  195. 2.6854520010653064453097148354817956938203822939945
  196. """
  197. glaisher = r"""
  198. Glaisher's constant `A`, also known as the Glaisher-Kinkelin
  199. constant, is a number approximately equal to 1.282427129 that
  200. sometimes appears in formulas related to gamma and zeta functions.
  201. It is also related to the Barnes G-function (see :func:`~mpmath.barnesg`).
  202. The constant is defined as `A = \exp(1/12-\zeta'(-1))` where
  203. `\zeta'(s)` denotes the derivative of the Riemann zeta function
  204. (see :func:`~mpmath.zeta`).
  205. Mpmath can evaluate Glaisher's constant to arbitrary precision:
  206. >>> from mpmath import *
  207. >>> mp.dps = 50; mp.pretty = True
  208. >>> +glaisher
  209. 1.282427129100622636875342568869791727767688927325
  210. We can verify that the value computed by :data:`glaisher` is
  211. correct using mpmath's facilities for numerical
  212. differentiation and arbitrary evaluation of the zeta function:
  213. >>> exp(mpf(1)/12 - diff(zeta, -1))
  214. 1.282427129100622636875342568869791727767688927325
  215. Here is an example of an integral that can be evaluated in
  216. terms of Glaisher's constant:
  217. >>> mp.dps = 15
  218. >>> quad(lambda x: log(gamma(x)), [1, 1.5])
  219. -0.0428537406502909
  220. >>> -0.5 - 7*log(2)/24 + log(pi)/4 + 3*log(glaisher)/2
  221. -0.042853740650291
  222. Mpmath computes Glaisher's constant by applying Euler-Maclaurin
  223. summation to a slowly convergent series. The implementation is
  224. reasonably efficient up to about 10,000 digits. See the source
  225. code for additional details.
  226. References:
  227. http://mathworld.wolfram.com/Glaisher-KinkelinConstant.html
  228. """
  229. apery = r"""
  230. Represents Apery's constant, which is the irrational number
  231. approximately equal to 1.2020569 given by
  232. .. math ::
  233. \zeta(3) = \sum_{k=1}^\infty\frac{1}{k^3}.
  234. The calculation is based on an efficient hypergeometric
  235. series. To 50 decimal places, the value is given by::
  236. >>> from mpmath import *
  237. >>> mp.dps = 50; mp.pretty = True
  238. >>> +apery
  239. 1.2020569031595942853997381615114499907649862923405
  240. Other ways to evaluate Apery's constant using mpmath
  241. include::
  242. >>> zeta(3)
  243. 1.2020569031595942853997381615114499907649862923405
  244. >>> -psi(2,1)/2
  245. 1.2020569031595942853997381615114499907649862923405
  246. >>> 8*nsum(lambda k: 1/(2*k+1)**3, [0,inf])/7
  247. 1.2020569031595942853997381615114499907649862923405
  248. >>> f = lambda k: 2/k**3/(exp(2*pi*k)-1)
  249. >>> 7*pi**3/180 - nsum(f, [1,inf])
  250. 1.2020569031595942853997381615114499907649862923405
  251. This shows digits 9991-10000 of Apery's constant::
  252. >>> mp.dps = 10000
  253. >>> str(apery)[-10:]
  254. '3189504235'
  255. """
  256. mertens = r"""
  257. Represents the Mertens or Meissel-Mertens constant, which is the
  258. prime number analog of Euler's constant:
  259. .. math ::
  260. B_1 = \lim_{N\to\infty}
  261. \left(\sum_{p_k \le N} \frac{1}{p_k} - \log \log N \right)
  262. Here `p_k` denotes the `k`-th prime number. Other names for this
  263. constant include the Hadamard-de la Vallee-Poussin constant or
  264. the prime reciprocal constant.
  265. The following gives the Mertens constant to 50 digits::
  266. >>> from mpmath import *
  267. >>> mp.dps = 50; mp.pretty = True
  268. >>> +mertens
  269. 0.2614972128476427837554268386086958590515666482612
  270. References:
  271. http://mathworld.wolfram.com/MertensConstant.html
  272. """
  273. twinprime = r"""
  274. Represents the twin prime constant, which is the factor `C_2`
  275. featuring in the Hardy-Littlewood conjecture for the growth of the
  276. twin prime counting function,
  277. .. math ::
  278. \pi_2(n) \sim 2 C_2 \frac{n}{\log^2 n}.
  279. It is given by the product over primes
  280. .. math ::
  281. C_2 = \prod_{p\ge3} \frac{p(p-2)}{(p-1)^2} \approx 0.66016
  282. Computing `C_2` to 50 digits::
  283. >>> from mpmath import *
  284. >>> mp.dps = 50; mp.pretty = True
  285. >>> +twinprime
  286. 0.66016181584686957392781211001455577843262336028473
  287. References:
  288. http://mathworld.wolfram.com/TwinPrimesConstant.html
  289. """
  290. ln = r"""
  291. Computes the natural logarithm of `x`, `\ln x`.
  292. See :func:`~mpmath.log` for additional documentation."""
  293. sqrt = r"""
  294. ``sqrt(x)`` gives the principal square root of `x`, `\sqrt x`.
  295. For positive real numbers, the principal root is simply the
  296. positive square root. For arbitrary complex numbers, the principal
  297. square root is defined to satisfy `\sqrt x = \exp(\log(x)/2)`.
  298. The function thus has a branch cut along the negative half real axis.
  299. For all mpmath numbers ``x``, calling ``sqrt(x)`` is equivalent to
  300. performing ``x**0.5``.
  301. **Examples**
  302. Basic examples and limits::
  303. >>> from mpmath import *
  304. >>> mp.dps = 15; mp.pretty = True
  305. >>> sqrt(10)
  306. 3.16227766016838
  307. >>> sqrt(100)
  308. 10.0
  309. >>> sqrt(-4)
  310. (0.0 + 2.0j)
  311. >>> sqrt(1+1j)
  312. (1.09868411346781 + 0.455089860562227j)
  313. >>> sqrt(inf)
  314. +inf
  315. Square root evaluation is fast at huge precision::
  316. >>> mp.dps = 50000
  317. >>> a = sqrt(3)
  318. >>> str(a)[-10:]
  319. '9329332815'
  320. :func:`mpmath.iv.sqrt` supports interval arguments::
  321. >>> iv.dps = 15; iv.pretty = True
  322. >>> iv.sqrt([16,100])
  323. [4.0, 10.0]
  324. >>> iv.sqrt(2)
  325. [1.4142135623730949234, 1.4142135623730951455]
  326. >>> iv.sqrt(2) ** 2
  327. [1.9999999999999995559, 2.0000000000000004441]
  328. """
  329. cbrt = r"""
  330. ``cbrt(x)`` computes the cube root of `x`, `x^{1/3}`. This
  331. function is faster and more accurate than raising to a floating-point
  332. fraction::
  333. >>> from mpmath import *
  334. >>> mp.dps = 15; mp.pretty = False
  335. >>> 125**(mpf(1)/3)
  336. mpf('4.9999999999999991')
  337. >>> cbrt(125)
  338. mpf('5.0')
  339. Every nonzero complex number has three cube roots. This function
  340. returns the cube root defined by `\exp(\log(x)/3)` where the
  341. principal branch of the natural logarithm is used. Note that this
  342. does not give a real cube root for negative real numbers::
  343. >>> mp.pretty = True
  344. >>> cbrt(-1)
  345. (0.5 + 0.866025403784439j)
  346. """
  347. exp = r"""
  348. Computes the exponential function,
  349. .. math ::
  350. \exp(x) = e^x = \sum_{k=0}^{\infty} \frac{x^k}{k!}.
  351. For complex numbers, the exponential function also satisfies
  352. .. math ::
  353. \exp(x+yi) = e^x (\cos y + i \sin y).
  354. **Basic examples**
  355. Some values of the exponential function::
  356. >>> from mpmath import *
  357. >>> mp.dps = 25; mp.pretty = True
  358. >>> exp(0)
  359. 1.0
  360. >>> exp(1)
  361. 2.718281828459045235360287
  362. >>> exp(-1)
  363. 0.3678794411714423215955238
  364. >>> exp(inf)
  365. +inf
  366. >>> exp(-inf)
  367. 0.0
  368. Arguments can be arbitrarily large::
  369. >>> exp(10000)
  370. 8.806818225662921587261496e+4342
  371. >>> exp(-10000)
  372. 1.135483865314736098540939e-4343
  373. Evaluation is supported for interval arguments via
  374. :func:`mpmath.iv.exp`::
  375. >>> iv.dps = 25; iv.pretty = True
  376. >>> iv.exp([-inf,0])
  377. [0.0, 1.0]
  378. >>> iv.exp([0,1])
  379. [1.0, 2.71828182845904523536028749558]
  380. The exponential function can be evaluated efficiently to arbitrary
  381. precision::
  382. >>> mp.dps = 10000
  383. >>> exp(pi) #doctest: +ELLIPSIS
  384. 23.140692632779269005729...8984304016040616
  385. **Functional properties**
  386. Numerical verification of Euler's identity for the complex
  387. exponential function::
  388. >>> mp.dps = 15
  389. >>> exp(j*pi)+1
  390. (0.0 + 1.22464679914735e-16j)
  391. >>> chop(exp(j*pi)+1)
  392. 0.0
  393. This recovers the coefficients (reciprocal factorials) in the
  394. Maclaurin series expansion of exp::
  395. >>> nprint(taylor(exp, 0, 5))
  396. [1.0, 1.0, 0.5, 0.166667, 0.0416667, 0.00833333]
  397. The exponential function is its own derivative and antiderivative::
  398. >>> exp(pi)
  399. 23.1406926327793
  400. >>> diff(exp, pi)
  401. 23.1406926327793
  402. >>> quad(exp, [-inf, pi])
  403. 23.1406926327793
  404. The exponential function can be evaluated using various methods,
  405. including direct summation of the series, limits, and solving
  406. the defining differential equation::
  407. >>> nsum(lambda k: pi**k/fac(k), [0,inf])
  408. 23.1406926327793
  409. >>> limit(lambda k: (1+pi/k)**k, inf)
  410. 23.1406926327793
  411. >>> odefun(lambda t, x: x, 0, 1)(pi)
  412. 23.1406926327793
  413. """
  414. cosh = r"""
  415. Computes the hyperbolic cosine of `x`,
  416. `\cosh(x) = (e^x + e^{-x})/2`. Values and limits include::
  417. >>> from mpmath import *
  418. >>> mp.dps = 25; mp.pretty = True
  419. >>> cosh(0)
  420. 1.0
  421. >>> cosh(1)
  422. 1.543080634815243778477906
  423. >>> cosh(-inf), cosh(+inf)
  424. (+inf, +inf)
  425. The hyperbolic cosine is an even, convex function with
  426. a global minimum at `x = 0`, having a Maclaurin series
  427. that starts::
  428. >>> nprint(chop(taylor(cosh, 0, 5)))
  429. [1.0, 0.0, 0.5, 0.0, 0.0416667, 0.0]
  430. Generalized to complex numbers, the hyperbolic cosine is
  431. equivalent to a cosine with the argument rotated
  432. in the imaginary direction, or `\cosh x = \cos ix`::
  433. >>> cosh(2+3j)
  434. (-3.724545504915322565473971 + 0.5118225699873846088344638j)
  435. >>> cos(3-2j)
  436. (-3.724545504915322565473971 + 0.5118225699873846088344638j)
  437. """
  438. sinh = r"""
  439. Computes the hyperbolic sine of `x`,
  440. `\sinh(x) = (e^x - e^{-x})/2`. Values and limits include::
  441. >>> from mpmath import *
  442. >>> mp.dps = 25; mp.pretty = True
  443. >>> sinh(0)
  444. 0.0
  445. >>> sinh(1)
  446. 1.175201193643801456882382
  447. >>> sinh(-inf), sinh(+inf)
  448. (-inf, +inf)
  449. The hyperbolic sine is an odd function, with a Maclaurin
  450. series that starts::
  451. >>> nprint(chop(taylor(sinh, 0, 5)))
  452. [0.0, 1.0, 0.0, 0.166667, 0.0, 0.00833333]
  453. Generalized to complex numbers, the hyperbolic sine is
  454. essentially a sine with a rotation `i` applied to
  455. the argument; more precisely, `\sinh x = -i \sin ix`::
  456. >>> sinh(2+3j)
  457. (-3.590564589985779952012565 + 0.5309210862485198052670401j)
  458. >>> j*sin(3-2j)
  459. (-3.590564589985779952012565 + 0.5309210862485198052670401j)
  460. """
  461. tanh = r"""
  462. Computes the hyperbolic tangent of `x`,
  463. `\tanh(x) = \sinh(x)/\cosh(x)`. Values and limits include::
  464. >>> from mpmath import *
  465. >>> mp.dps = 25; mp.pretty = True
  466. >>> tanh(0)
  467. 0.0
  468. >>> tanh(1)
  469. 0.7615941559557648881194583
  470. >>> tanh(-inf), tanh(inf)
  471. (-1.0, 1.0)
  472. The hyperbolic tangent is an odd, sigmoidal function, similar
  473. to the inverse tangent and error function. Its Maclaurin
  474. series is::
  475. >>> nprint(chop(taylor(tanh, 0, 5)))
  476. [0.0, 1.0, 0.0, -0.333333, 0.0, 0.133333]
  477. Generalized to complex numbers, the hyperbolic tangent is
  478. essentially a tangent with a rotation `i` applied to
  479. the argument; more precisely, `\tanh x = -i \tan ix`::
  480. >>> tanh(2+3j)
  481. (0.9653858790221331242784803 - 0.009884375038322493720314034j)
  482. >>> j*tan(3-2j)
  483. (0.9653858790221331242784803 - 0.009884375038322493720314034j)
  484. """
  485. cos = r"""
  486. Computes the cosine of `x`, `\cos(x)`.
  487. >>> from mpmath import *
  488. >>> mp.dps = 25; mp.pretty = True
  489. >>> cos(pi/3)
  490. 0.5
  491. >>> cos(100000001)
  492. -0.9802850113244713353133243
  493. >>> cos(2+3j)
  494. (-4.189625690968807230132555 - 9.109227893755336597979197j)
  495. >>> cos(inf)
  496. nan
  497. >>> nprint(chop(taylor(cos, 0, 6)))
  498. [1.0, 0.0, -0.5, 0.0, 0.0416667, 0.0, -0.00138889]
  499. Intervals are supported via :func:`mpmath.iv.cos`::
  500. >>> iv.dps = 25; iv.pretty = True
  501. >>> iv.cos([0,1])
  502. [0.540302305868139717400936602301, 1.0]
  503. >>> iv.cos([0,2])
  504. [-0.41614683654714238699756823214, 1.0]
  505. """
  506. sin = r"""
  507. Computes the sine of `x`, `\sin(x)`.
  508. >>> from mpmath import *
  509. >>> mp.dps = 25; mp.pretty = True
  510. >>> sin(pi/3)
  511. 0.8660254037844386467637232
  512. >>> sin(100000001)
  513. 0.1975887055794968911438743
  514. >>> sin(2+3j)
  515. (9.1544991469114295734673 - 4.168906959966564350754813j)
  516. >>> sin(inf)
  517. nan
  518. >>> nprint(chop(taylor(sin, 0, 6)))
  519. [0.0, 1.0, 0.0, -0.166667, 0.0, 0.00833333, 0.0]
  520. Intervals are supported via :func:`mpmath.iv.sin`::
  521. >>> iv.dps = 25; iv.pretty = True
  522. >>> iv.sin([0,1])
  523. [0.0, 0.841470984807896506652502331201]
  524. >>> iv.sin([0,2])
  525. [0.0, 1.0]
  526. """
  527. tan = r"""
  528. Computes the tangent of `x`, `\tan(x) = \frac{\sin(x)}{\cos(x)}`.
  529. The tangent function is singular at `x = (n+1/2)\pi`, but
  530. ``tan(x)`` always returns a finite result since `(n+1/2)\pi`
  531. cannot be represented exactly using floating-point arithmetic.
  532. >>> from mpmath import *
  533. >>> mp.dps = 25; mp.pretty = True
  534. >>> tan(pi/3)
  535. 1.732050807568877293527446
  536. >>> tan(100000001)
  537. -0.2015625081449864533091058
  538. >>> tan(2+3j)
  539. (-0.003764025641504248292751221 + 1.003238627353609801446359j)
  540. >>> tan(inf)
  541. nan
  542. >>> nprint(chop(taylor(tan, 0, 6)))
  543. [0.0, 1.0, 0.0, 0.333333, 0.0, 0.133333, 0.0]
  544. Intervals are supported via :func:`mpmath.iv.tan`::
  545. >>> iv.dps = 25; iv.pretty = True
  546. >>> iv.tan([0,1])
  547. [0.0, 1.55740772465490223050697482944]
  548. >>> iv.tan([0,2]) # Interval includes a singularity
  549. [-inf, +inf]
  550. """
  551. sec = r"""
  552. Computes the secant of `x`, `\mathrm{sec}(x) = \frac{1}{\cos(x)}`.
  553. The secant function is singular at `x = (n+1/2)\pi`, but
  554. ``sec(x)`` always returns a finite result since `(n+1/2)\pi`
  555. cannot be represented exactly using floating-point arithmetic.
  556. >>> from mpmath import *
  557. >>> mp.dps = 25; mp.pretty = True
  558. >>> sec(pi/3)
  559. 2.0
  560. >>> sec(10000001)
  561. -1.184723164360392819100265
  562. >>> sec(2+3j)
  563. (-0.04167496441114427004834991 + 0.0906111371962375965296612j)
  564. >>> sec(inf)
  565. nan
  566. >>> nprint(chop(taylor(sec, 0, 6)))
  567. [1.0, 0.0, 0.5, 0.0, 0.208333, 0.0, 0.0847222]
  568. Intervals are supported via :func:`mpmath.iv.sec`::
  569. >>> iv.dps = 25; iv.pretty = True
  570. >>> iv.sec([0,1])
  571. [1.0, 1.85081571768092561791175326276]
  572. >>> iv.sec([0,2]) # Interval includes a singularity
  573. [-inf, +inf]
  574. """
  575. csc = r"""
  576. Computes the cosecant of `x`, `\mathrm{csc}(x) = \frac{1}{\sin(x)}`.
  577. This cosecant function is singular at `x = n \pi`, but with the
  578. exception of the point `x = 0`, ``csc(x)`` returns a finite result
  579. since `n \pi` cannot be represented exactly using floating-point
  580. arithmetic.
  581. >>> from mpmath import *
  582. >>> mp.dps = 25; mp.pretty = True
  583. >>> csc(pi/3)
  584. 1.154700538379251529018298
  585. >>> csc(10000001)
  586. -1.864910497503629858938891
  587. >>> csc(2+3j)
  588. (0.09047320975320743980579048 + 0.04120098628857412646300981j)
  589. >>> csc(inf)
  590. nan
  591. Intervals are supported via :func:`mpmath.iv.csc`::
  592. >>> iv.dps = 25; iv.pretty = True
  593. >>> iv.csc([0,1]) # Interval includes a singularity
  594. [1.18839510577812121626159943988, +inf]
  595. >>> iv.csc([0,2])
  596. [1.0, +inf]
  597. """
  598. cot = r"""
  599. Computes the cotangent of `x`,
  600. `\mathrm{cot}(x) = \frac{1}{\tan(x)} = \frac{\cos(x)}{\sin(x)}`.
  601. This cotangent function is singular at `x = n \pi`, but with the
  602. exception of the point `x = 0`, ``cot(x)`` returns a finite result
  603. since `n \pi` cannot be represented exactly using floating-point
  604. arithmetic.
  605. >>> from mpmath import *
  606. >>> mp.dps = 25; mp.pretty = True
  607. >>> cot(pi/3)
  608. 0.5773502691896257645091488
  609. >>> cot(10000001)
  610. 1.574131876209625656003562
  611. >>> cot(2+3j)
  612. (-0.003739710376336956660117409 - 0.9967577965693583104609688j)
  613. >>> cot(inf)
  614. nan
  615. Intervals are supported via :func:`mpmath.iv.cot`::
  616. >>> iv.dps = 25; iv.pretty = True
  617. >>> iv.cot([0,1]) # Interval includes a singularity
  618. [0.642092615934330703006419974862, +inf]
  619. >>> iv.cot([1,2])
  620. [-inf, +inf]
  621. """
  622. acos = r"""
  623. Computes the inverse cosine or arccosine of `x`, `\cos^{-1}(x)`.
  624. Since `-1 \le \cos(x) \le 1` for real `x`, the inverse
  625. cosine is real-valued only for `-1 \le x \le 1`. On this interval,
  626. :func:`~mpmath.acos` is defined to be a monotonically decreasing
  627. function assuming values between `+\pi` and `0`.
  628. Basic values are::
  629. >>> from mpmath import *
  630. >>> mp.dps = 25; mp.pretty = True
  631. >>> acos(-1)
  632. 3.141592653589793238462643
  633. >>> acos(0)
  634. 1.570796326794896619231322
  635. >>> acos(1)
  636. 0.0
  637. >>> nprint(chop(taylor(acos, 0, 6)))
  638. [1.5708, -1.0, 0.0, -0.166667, 0.0, -0.075, 0.0]
  639. :func:`~mpmath.acos` is defined so as to be a proper inverse function of
  640. `\cos(\theta)` for `0 \le \theta < \pi`.
  641. We have `\cos(\cos^{-1}(x)) = x` for all `x`, but
  642. `\cos^{-1}(\cos(x)) = x` only for `0 \le \Re[x] < \pi`::
  643. >>> for x in [1, 10, -1, 2+3j, 10+3j]:
  644. ... print("%s %s" % (cos(acos(x)), acos(cos(x))))
  645. ...
  646. 1.0 1.0
  647. (10.0 + 0.0j) 2.566370614359172953850574
  648. -1.0 1.0
  649. (2.0 + 3.0j) (2.0 + 3.0j)
  650. (10.0 + 3.0j) (2.566370614359172953850574 - 3.0j)
  651. The inverse cosine has two branch points: `x = \pm 1`. :func:`~mpmath.acos`
  652. places the branch cuts along the line segments `(-\infty, -1)` and
  653. `(+1, +\infty)`. In general,
  654. .. math ::
  655. \cos^{-1}(x) = \frac{\pi}{2} + i \log\left(ix + \sqrt{1-x^2} \right)
  656. where the principal-branch log and square root are implied.
  657. """
  658. asin = r"""
  659. Computes the inverse sine or arcsine of `x`, `\sin^{-1}(x)`.
  660. Since `-1 \le \sin(x) \le 1` for real `x`, the inverse
  661. sine is real-valued only for `-1 \le x \le 1`.
  662. On this interval, it is defined to be a monotonically increasing
  663. function assuming values between `-\pi/2` and `\pi/2`.
  664. Basic values are::
  665. >>> from mpmath import *
  666. >>> mp.dps = 25; mp.pretty = True
  667. >>> asin(-1)
  668. -1.570796326794896619231322
  669. >>> asin(0)
  670. 0.0
  671. >>> asin(1)
  672. 1.570796326794896619231322
  673. >>> nprint(chop(taylor(asin, 0, 6)))
  674. [0.0, 1.0, 0.0, 0.166667, 0.0, 0.075, 0.0]
  675. :func:`~mpmath.asin` is defined so as to be a proper inverse function of
  676. `\sin(\theta)` for `-\pi/2 < \theta < \pi/2`.
  677. We have `\sin(\sin^{-1}(x)) = x` for all `x`, but
  678. `\sin^{-1}(\sin(x)) = x` only for `-\pi/2 < \Re[x] < \pi/2`::
  679. >>> for x in [1, 10, -1, 1+3j, -2+3j]:
  680. ... print("%s %s" % (chop(sin(asin(x))), asin(sin(x))))
  681. ...
  682. 1.0 1.0
  683. 10.0 -0.5752220392306202846120698
  684. -1.0 -1.0
  685. (1.0 + 3.0j) (1.0 + 3.0j)
  686. (-2.0 + 3.0j) (-1.141592653589793238462643 - 3.0j)
  687. The inverse sine has two branch points: `x = \pm 1`. :func:`~mpmath.asin`
  688. places the branch cuts along the line segments `(-\infty, -1)` and
  689. `(+1, +\infty)`. In general,
  690. .. math ::
  691. \sin^{-1}(x) = -i \log\left(ix + \sqrt{1-x^2} \right)
  692. where the principal-branch log and square root are implied.
  693. """
  694. atan = r"""
  695. Computes the inverse tangent or arctangent of `x`, `\tan^{-1}(x)`.
  696. This is a real-valued function for all real `x`, with range
  697. `(-\pi/2, \pi/2)`.
  698. Basic values are::
  699. >>> from mpmath import *
  700. >>> mp.dps = 25; mp.pretty = True
  701. >>> atan(-inf)
  702. -1.570796326794896619231322
  703. >>> atan(-1)
  704. -0.7853981633974483096156609
  705. >>> atan(0)
  706. 0.0
  707. >>> atan(1)
  708. 0.7853981633974483096156609
  709. >>> atan(inf)
  710. 1.570796326794896619231322
  711. >>> nprint(chop(taylor(atan, 0, 6)))
  712. [0.0, 1.0, 0.0, -0.333333, 0.0, 0.2, 0.0]
  713. The inverse tangent is often used to compute angles. However,
  714. the atan2 function is often better for this as it preserves sign
  715. (see :func:`~mpmath.atan2`).
  716. :func:`~mpmath.atan` is defined so as to be a proper inverse function of
  717. `\tan(\theta)` for `-\pi/2 < \theta < \pi/2`.
  718. We have `\tan(\tan^{-1}(x)) = x` for all `x`, but
  719. `\tan^{-1}(\tan(x)) = x` only for `-\pi/2 < \Re[x] < \pi/2`::
  720. >>> mp.dps = 25
  721. >>> for x in [1, 10, -1, 1+3j, -2+3j]:
  722. ... print("%s %s" % (tan(atan(x)), atan(tan(x))))
  723. ...
  724. 1.0 1.0
  725. 10.0 0.5752220392306202846120698
  726. -1.0 -1.0
  727. (1.0 + 3.0j) (1.000000000000000000000001 + 3.0j)
  728. (-2.0 + 3.0j) (1.141592653589793238462644 + 3.0j)
  729. The inverse tangent has two branch points: `x = \pm i`. :func:`~mpmath.atan`
  730. places the branch cuts along the line segments `(-i \infty, -i)` and
  731. `(+i, +i \infty)`. In general,
  732. .. math ::
  733. \tan^{-1}(x) = \frac{i}{2}\left(\log(1-ix)-\log(1+ix)\right)
  734. where the principal-branch log is implied.
  735. """
  736. acot = r"""Computes the inverse cotangent of `x`,
  737. `\mathrm{cot}^{-1}(x) = \tan^{-1}(1/x)`."""
  738. asec = r"""Computes the inverse secant of `x`,
  739. `\mathrm{sec}^{-1}(x) = \cos^{-1}(1/x)`."""
  740. acsc = r"""Computes the inverse cosecant of `x`,
  741. `\mathrm{csc}^{-1}(x) = \sin^{-1}(1/x)`."""
  742. coth = r"""Computes the hyperbolic cotangent of `x`,
  743. `\mathrm{coth}(x) = \frac{\cosh(x)}{\sinh(x)}`.
  744. """
  745. sech = r"""Computes the hyperbolic secant of `x`,
  746. `\mathrm{sech}(x) = \frac{1}{\cosh(x)}`.
  747. """
  748. csch = r"""Computes the hyperbolic cosecant of `x`,
  749. `\mathrm{csch}(x) = \frac{1}{\sinh(x)}`.
  750. """
  751. acosh = r"""Computes the inverse hyperbolic cosine of `x`,
  752. `\mathrm{cosh}^{-1}(x) = \log(x+\sqrt{x+1}\sqrt{x-1})`.
  753. """
  754. asinh = r"""Computes the inverse hyperbolic sine of `x`,
  755. `\mathrm{sinh}^{-1}(x) = \log(x+\sqrt{1+x^2})`.
  756. """
  757. atanh = r"""Computes the inverse hyperbolic tangent of `x`,
  758. `\mathrm{tanh}^{-1}(x) = \frac{1}{2}\left(\log(1+x)-\log(1-x)\right)`.
  759. """
  760. acoth = r"""Computes the inverse hyperbolic cotangent of `x`,
  761. `\mathrm{coth}^{-1}(x) = \tanh^{-1}(1/x)`."""
  762. asech = r"""Computes the inverse hyperbolic secant of `x`,
  763. `\mathrm{sech}^{-1}(x) = \cosh^{-1}(1/x)`."""
  764. acsch = r"""Computes the inverse hyperbolic cosecant of `x`,
  765. `\mathrm{csch}^{-1}(x) = \sinh^{-1}(1/x)`."""
  766. sinpi = r"""
  767. Computes `\sin(\pi x)`, more accurately than the expression
  768. ``sin(pi*x)``::
  769. >>> from mpmath import *
  770. >>> mp.dps = 15; mp.pretty = True
  771. >>> sinpi(10**10), sin(pi*(10**10))
  772. (0.0, -2.23936276195592e-6)
  773. >>> sinpi(10**10+0.5), sin(pi*(10**10+0.5))
  774. (1.0, 0.999999999998721)
  775. """
  776. cospi = r"""
  777. Computes `\cos(\pi x)`, more accurately than the expression
  778. ``cos(pi*x)``::
  779. >>> from mpmath import *
  780. >>> mp.dps = 15; mp.pretty = True
  781. >>> cospi(10**10), cos(pi*(10**10))
  782. (1.0, 0.999999999997493)
  783. >>> cospi(10**10+0.5), cos(pi*(10**10+0.5))
  784. (0.0, 1.59960492420134e-6)
  785. """
  786. sinc = r"""
  787. ``sinc(x)`` computes the unnormalized sinc function, defined as
  788. .. math ::
  789. \mathrm{sinc}(x) = \begin{cases}
  790. \sin(x)/x, & \mbox{if } x \ne 0 \\
  791. 1, & \mbox{if } x = 0.
  792. \end{cases}
  793. See :func:`~mpmath.sincpi` for the normalized sinc function.
  794. Simple values and limits include::
  795. >>> from mpmath import *
  796. >>> mp.dps = 15; mp.pretty = True
  797. >>> sinc(0)
  798. 1.0
  799. >>> sinc(1)
  800. 0.841470984807897
  801. >>> sinc(inf)
  802. 0.0
  803. The integral of the sinc function is the sine integral Si::
  804. >>> quad(sinc, [0, 1])
  805. 0.946083070367183
  806. >>> si(1)
  807. 0.946083070367183
  808. """
  809. sincpi = r"""
  810. ``sincpi(x)`` computes the normalized sinc function, defined as
  811. .. math ::
  812. \mathrm{sinc}_{\pi}(x) = \begin{cases}
  813. \sin(\pi x)/(\pi x), & \mbox{if } x \ne 0 \\
  814. 1, & \mbox{if } x = 0.
  815. \end{cases}
  816. Equivalently, we have
  817. `\mathrm{sinc}_{\pi}(x) = \mathrm{sinc}(\pi x)`.
  818. The normalization entails that the function integrates
  819. to unity over the entire real line::
  820. >>> from mpmath import *
  821. >>> mp.dps = 15; mp.pretty = True
  822. >>> quadosc(sincpi, [-inf, inf], period=2.0)
  823. 1.0
  824. Like, :func:`~mpmath.sinpi`, :func:`~mpmath.sincpi` is evaluated accurately
  825. at its roots::
  826. >>> sincpi(10)
  827. 0.0
  828. """
  829. expj = r"""
  830. Convenience function for computing `e^{ix}`::
  831. >>> from mpmath import *
  832. >>> mp.dps = 25; mp.pretty = True
  833. >>> expj(0)
  834. (1.0 + 0.0j)
  835. >>> expj(-1)
  836. (0.5403023058681397174009366 - 0.8414709848078965066525023j)
  837. >>> expj(j)
  838. (0.3678794411714423215955238 + 0.0j)
  839. >>> expj(1+j)
  840. (0.1987661103464129406288032 + 0.3095598756531121984439128j)
  841. """
  842. expjpi = r"""
  843. Convenience function for computing `e^{i \pi x}`.
  844. Evaluation is accurate near zeros (see also :func:`~mpmath.cospi`,
  845. :func:`~mpmath.sinpi`)::
  846. >>> from mpmath import *
  847. >>> mp.dps = 25; mp.pretty = True
  848. >>> expjpi(0)
  849. (1.0 + 0.0j)
  850. >>> expjpi(1)
  851. (-1.0 + 0.0j)
  852. >>> expjpi(0.5)
  853. (0.0 + 1.0j)
  854. >>> expjpi(-1)
  855. (-1.0 + 0.0j)
  856. >>> expjpi(j)
  857. (0.04321391826377224977441774 + 0.0j)
  858. >>> expjpi(1+j)
  859. (-0.04321391826377224977441774 + 0.0j)
  860. """
  861. floor = r"""
  862. Computes the floor of `x`, `\lfloor x \rfloor`, defined as
  863. the largest integer less than or equal to `x`::
  864. >>> from mpmath import *
  865. >>> mp.pretty = False
  866. >>> floor(3.5)
  867. mpf('3.0')
  868. .. note ::
  869. :func:`~mpmath.floor`, :func:`~mpmath.ceil` and :func:`~mpmath.nint` return a
  870. floating-point number, not a Python ``int``. If `\lfloor x \rfloor` is
  871. too large to be represented exactly at the present working precision,
  872. the result will be rounded, not necessarily in the direction
  873. implied by the mathematical definition of the function.
  874. To avoid rounding, use *prec=0*::
  875. >>> mp.dps = 15
  876. >>> print(int(floor(10**30+1)))
  877. 1000000000000000019884624838656
  878. >>> print(int(floor(10**30+1, prec=0)))
  879. 1000000000000000000000000000001
  880. The floor function is defined for complex numbers and
  881. acts on the real and imaginary parts separately::
  882. >>> floor(3.25+4.75j)
  883. mpc(real='3.0', imag='4.0')
  884. """
  885. ceil = r"""
  886. Computes the ceiling of `x`, `\lceil x \rceil`, defined as
  887. the smallest integer greater than or equal to `x`::
  888. >>> from mpmath import *
  889. >>> mp.pretty = False
  890. >>> ceil(3.5)
  891. mpf('4.0')
  892. The ceiling function is defined for complex numbers and
  893. acts on the real and imaginary parts separately::
  894. >>> ceil(3.25+4.75j)
  895. mpc(real='4.0', imag='5.0')
  896. See notes about rounding for :func:`~mpmath.floor`.
  897. """
  898. nint = r"""
  899. Evaluates the nearest integer function, `\mathrm{nint}(x)`.
  900. This gives the nearest integer to `x`; on a tie, it
  901. gives the nearest even integer::
  902. >>> from mpmath import *
  903. >>> mp.pretty = False
  904. >>> nint(3.2)
  905. mpf('3.0')
  906. >>> nint(3.8)
  907. mpf('4.0')
  908. >>> nint(3.5)
  909. mpf('4.0')
  910. >>> nint(4.5)
  911. mpf('4.0')
  912. The nearest integer function is defined for complex numbers and
  913. acts on the real and imaginary parts separately::
  914. >>> nint(3.25+4.75j)
  915. mpc(real='3.0', imag='5.0')
  916. See notes about rounding for :func:`~mpmath.floor`.
  917. """
  918. frac = r"""
  919. Gives the fractional part of `x`, defined as
  920. `\mathrm{frac}(x) = x - \lfloor x \rfloor` (see :func:`~mpmath.floor`).
  921. In effect, this computes `x` modulo 1, or `x+n` where
  922. `n \in \mathbb{Z}` is such that `x+n \in [0,1)`::
  923. >>> from mpmath import *
  924. >>> mp.pretty = False
  925. >>> frac(1.25)
  926. mpf('0.25')
  927. >>> frac(3)
  928. mpf('0.0')
  929. >>> frac(-1.25)
  930. mpf('0.75')
  931. For a complex number, the fractional part function applies to
  932. the real and imaginary parts separately::
  933. >>> frac(2.25+3.75j)
  934. mpc(real='0.25', imag='0.75')
  935. Plotted, the fractional part function gives a sawtooth
  936. wave. The Fourier series coefficients have a simple
  937. form::
  938. >>> mp.dps = 15
  939. >>> nprint(fourier(lambda x: frac(x)-0.5, [0,1], 4))
  940. ([0.0, 0.0, 0.0, 0.0, 0.0], [0.0, -0.31831, -0.159155, -0.106103, -0.0795775])
  941. >>> nprint([-1/(pi*k) for k in range(1,5)])
  942. [-0.31831, -0.159155, -0.106103, -0.0795775]
  943. .. note::
  944. The fractional part is sometimes defined as a symmetric
  945. function, i.e. returning `-\mathrm{frac}(-x)` if `x < 0`.
  946. This convention is used, for instance, by Mathematica's
  947. ``FractionalPart``.
  948. """
  949. sign = r"""
  950. Returns the sign of `x`, defined as `\mathrm{sign}(x) = x / |x|`
  951. (with the special case `\mathrm{sign}(0) = 0`)::
  952. >>> from mpmath import *
  953. >>> mp.dps = 15; mp.pretty = False
  954. >>> sign(10)
  955. mpf('1.0')
  956. >>> sign(-10)
  957. mpf('-1.0')
  958. >>> sign(0)
  959. mpf('0.0')
  960. Note that the sign function is also defined for complex numbers,
  961. for which it gives the projection onto the unit circle::
  962. >>> mp.dps = 15; mp.pretty = True
  963. >>> sign(1+j)
  964. (0.707106781186547 + 0.707106781186547j)
  965. """
  966. arg = r"""
  967. Computes the complex argument (phase) of `x`, defined as the
  968. signed angle between the positive real axis and `x` in the
  969. complex plane::
  970. >>> from mpmath import *
  971. >>> mp.dps = 15; mp.pretty = True
  972. >>> arg(3)
  973. 0.0
  974. >>> arg(3+3j)
  975. 0.785398163397448
  976. >>> arg(3j)
  977. 1.5707963267949
  978. >>> arg(-3)
  979. 3.14159265358979
  980. >>> arg(-3j)
  981. -1.5707963267949
  982. The angle is defined to satisfy `-\pi < \arg(x) \le \pi` and
  983. with the sign convention that a nonnegative imaginary part
  984. results in a nonnegative argument.
  985. The value returned by :func:`~mpmath.arg` is an ``mpf`` instance.
  986. """
  987. fabs = r"""
  988. Returns the absolute value of `x`, `|x|`. Unlike :func:`abs`,
  989. :func:`~mpmath.fabs` converts non-mpmath numbers (such as ``int``)
  990. into mpmath numbers::
  991. >>> from mpmath import *
  992. >>> mp.dps = 15; mp.pretty = False
  993. >>> fabs(3)
  994. mpf('3.0')
  995. >>> fabs(-3)
  996. mpf('3.0')
  997. >>> fabs(3+4j)
  998. mpf('5.0')
  999. """
  1000. re = r"""
  1001. Returns the real part of `x`, `\Re(x)`. :func:`~mpmath.re`
  1002. converts a non-mpmath number to an mpmath number::
  1003. >>> from mpmath import *
  1004. >>> mp.dps = 15; mp.pretty = False
  1005. >>> re(3)
  1006. mpf('3.0')
  1007. >>> re(-1+4j)
  1008. mpf('-1.0')
  1009. """
  1010. im = r"""
  1011. Returns the imaginary part of `x`, `\Im(x)`. :func:`~mpmath.im`
  1012. converts a non-mpmath number to an mpmath number::
  1013. >>> from mpmath import *
  1014. >>> mp.dps = 15; mp.pretty = False
  1015. >>> im(3)
  1016. mpf('0.0')
  1017. >>> im(-1+4j)
  1018. mpf('4.0')
  1019. """
  1020. conj = r"""
  1021. Returns the complex conjugate of `x`, `\overline{x}`. Unlike
  1022. ``x.conjugate()``, :func:`~mpmath.im` converts `x` to a mpmath number::
  1023. >>> from mpmath import *
  1024. >>> mp.dps = 15; mp.pretty = False
  1025. >>> conj(3)
  1026. mpf('3.0')
  1027. >>> conj(-1+4j)
  1028. mpc(real='-1.0', imag='-4.0')
  1029. """
  1030. polar = r"""
  1031. Returns the polar representation of the complex number `z`
  1032. as a pair `(r, \phi)` such that `z = r e^{i \phi}`::
  1033. >>> from mpmath import *
  1034. >>> mp.dps = 15; mp.pretty = True
  1035. >>> polar(-2)
  1036. (2.0, 3.14159265358979)
  1037. >>> polar(3-4j)
  1038. (5.0, -0.927295218001612)
  1039. """
  1040. rect = r"""
  1041. Returns the complex number represented by polar
  1042. coordinates `(r, \phi)`::
  1043. >>> from mpmath import *
  1044. >>> mp.dps = 15; mp.pretty = True
  1045. >>> chop(rect(2, pi))
  1046. -2.0
  1047. >>> rect(sqrt(2), -pi/4)
  1048. (1.0 - 1.0j)
  1049. """
  1050. expm1 = r"""
  1051. Computes `e^x - 1`, accurately for small `x`.
  1052. Unlike the expression ``exp(x) - 1``, ``expm1(x)`` does not suffer from
  1053. potentially catastrophic cancellation::
  1054. >>> from mpmath import *
  1055. >>> mp.dps = 15; mp.pretty = True
  1056. >>> exp(1e-10)-1; print(expm1(1e-10))
  1057. 1.00000008274037e-10
  1058. 1.00000000005e-10
  1059. >>> exp(1e-20)-1; print(expm1(1e-20))
  1060. 0.0
  1061. 1.0e-20
  1062. >>> 1/(exp(1e-20)-1)
  1063. Traceback (most recent call last):
  1064. ...
  1065. ZeroDivisionError
  1066. >>> 1/expm1(1e-20)
  1067. 1.0e+20
  1068. Evaluation works for extremely tiny values::
  1069. >>> expm1(0)
  1070. 0.0
  1071. >>> expm1('1e-10000000')
  1072. 1.0e-10000000
  1073. """
  1074. log1p = r"""
  1075. Computes `\log(1+x)`, accurately for small `x`.
  1076. >>> from mpmath import *
  1077. >>> mp.dps = 15; mp.pretty = True
  1078. >>> log(1+1e-10); print(mp.log1p(1e-10))
  1079. 1.00000008269037e-10
  1080. 9.9999999995e-11
  1081. >>> mp.log1p(1e-100j)
  1082. (5.0e-201 + 1.0e-100j)
  1083. >>> mp.log1p(0)
  1084. 0.0
  1085. """
  1086. powm1 = r"""
  1087. Computes `x^y - 1`, accurately when `x^y` is very close to 1.
  1088. This avoids potentially catastrophic cancellation::
  1089. >>> from mpmath import *
  1090. >>> mp.dps = 15; mp.pretty = True
  1091. >>> power(0.99999995, 1e-10) - 1
  1092. 0.0
  1093. >>> powm1(0.99999995, 1e-10)
  1094. -5.00000012791934e-18
  1095. Powers exactly equal to 1, and only those powers, yield 0 exactly::
  1096. >>> powm1(-j, 4)
  1097. (0.0 + 0.0j)
  1098. >>> powm1(3, 0)
  1099. 0.0
  1100. >>> powm1(fadd(-1, 1e-100, exact=True), 4)
  1101. -4.0e-100
  1102. Evaluation works for extremely tiny `y`::
  1103. >>> powm1(2, '1e-100000')
  1104. 6.93147180559945e-100001
  1105. >>> powm1(j, '1e-1000')
  1106. (-1.23370055013617e-2000 + 1.5707963267949e-1000j)
  1107. """
  1108. root = r"""
  1109. ``root(z, n, k=0)`` computes an `n`-th root of `z`, i.e. returns a number
  1110. `r` that (up to possible approximation error) satisfies `r^n = z`.
  1111. (``nthroot`` is available as an alias for ``root``.)
  1112. Every complex number `z \ne 0` has `n` distinct `n`-th roots, which are
  1113. equidistant points on a circle with radius `|z|^{1/n}`, centered around the
  1114. origin. A specific root may be selected using the optional index
  1115. `k`. The roots are indexed counterclockwise, starting with `k = 0` for the root
  1116. closest to the positive real half-axis.
  1117. The `k = 0` root is the so-called principal `n`-th root, often denoted by
  1118. `\sqrt[n]{z}` or `z^{1/n}`, and also given by `\exp(\log(z) / n)`. If `z` is
  1119. a positive real number, the principal root is just the unique positive
  1120. `n`-th root of `z`. Under some circumstances, non-principal real roots exist:
  1121. for positive real `z`, `n` even, there is a negative root given by `k = n/2`;
  1122. for negative real `z`, `n` odd, there is a negative root given by `k = (n-1)/2`.
  1123. To obtain all roots with a simple expression, use
  1124. ``[root(z,n,k) for k in range(n)]``.
  1125. An important special case, ``root(1, n, k)`` returns the `k`-th `n`-th root of
  1126. unity, `\zeta_k = e^{2 \pi i k / n}`. Alternatively, :func:`~mpmath.unitroots`
  1127. provides a slightly more convenient way to obtain the roots of unity,
  1128. including the option to compute only the primitive roots of unity.
  1129. Both `k` and `n` should be integers; `k` outside of ``range(n)`` will be
  1130. reduced modulo `n`. If `n` is negative, `x^{-1/n} = 1/x^{1/n}` (or
  1131. the equivalent reciprocal for a non-principal root with `k \ne 0`) is computed.
  1132. :func:`~mpmath.root` is implemented to use Newton's method for small
  1133. `n`. At high precision, this makes `x^{1/n}` not much more
  1134. expensive than the regular exponentiation, `x^n`. For very large
  1135. `n`, :func:`~mpmath.nthroot` falls back to use the exponential function.
  1136. **Examples**
  1137. :func:`~mpmath.nthroot`/:func:`~mpmath.root` is faster and more accurate than raising to a
  1138. floating-point fraction::
  1139. >>> from mpmath import *
  1140. >>> mp.dps = 15; mp.pretty = False
  1141. >>> 16807 ** (mpf(1)/5)
  1142. mpf('7.0000000000000009')
  1143. >>> root(16807, 5)
  1144. mpf('7.0')
  1145. >>> nthroot(16807, 5) # Alias
  1146. mpf('7.0')
  1147. A high-precision root::
  1148. >>> mp.dps = 50; mp.pretty = True
  1149. >>> nthroot(10, 5)
  1150. 1.584893192461113485202101373391507013269442133825
  1151. >>> nthroot(10, 5) ** 5
  1152. 10.0
  1153. Computing principal and non-principal square and cube roots::
  1154. >>> mp.dps = 15
  1155. >>> root(10, 2)
  1156. 3.16227766016838
  1157. >>> root(10, 2, 1)
  1158. -3.16227766016838
  1159. >>> root(-10, 3)
  1160. (1.07721734501594 + 1.86579517236206j)
  1161. >>> root(-10, 3, 1)
  1162. -2.15443469003188
  1163. >>> root(-10, 3, 2)
  1164. (1.07721734501594 - 1.86579517236206j)
  1165. All the 7th roots of a complex number::
  1166. >>> for r in [root(3+4j, 7, k) for k in range(7)]:
  1167. ... print("%s %s" % (r, r**7))
  1168. ...
  1169. (1.24747270589553 + 0.166227124177353j) (3.0 + 4.0j)
  1170. (0.647824911301003 + 1.07895435170559j) (3.0 + 4.0j)
  1171. (-0.439648254723098 + 1.17920694574172j) (3.0 + 4.0j)
  1172. (-1.19605731775069 + 0.391492658196305j) (3.0 + 4.0j)
  1173. (-1.05181082538903 - 0.691023585965793j) (3.0 + 4.0j)
  1174. (-0.115529328478668 - 1.25318497558335j) (3.0 + 4.0j)
  1175. (0.907748109144957 - 0.871672518271819j) (3.0 + 4.0j)
  1176. Cube roots of unity::
  1177. >>> for k in range(3): print(root(1, 3, k))
  1178. ...
  1179. 1.0
  1180. (-0.5 + 0.866025403784439j)
  1181. (-0.5 - 0.866025403784439j)
  1182. Some exact high order roots::
  1183. >>> root(75**210, 105)
  1184. 5625.0
  1185. >>> root(1, 128, 96)
  1186. (0.0 - 1.0j)
  1187. >>> root(4**128, 128, 96)
  1188. (0.0 - 4.0j)
  1189. """
  1190. unitroots = r"""
  1191. ``unitroots(n)`` returns `\zeta_0, \zeta_1, \ldots, \zeta_{n-1}`,
  1192. all the distinct `n`-th roots of unity, as a list. If the option
  1193. *primitive=True* is passed, only the primitive roots are returned.
  1194. Every `n`-th root of unity satisfies `(\zeta_k)^n = 1`. There are `n` distinct
  1195. roots for each `n` (`\zeta_k` and `\zeta_j` are the same when
  1196. `k = j \pmod n`), which form a regular polygon with vertices on the unit
  1197. circle. They are ordered counterclockwise with increasing `k`, starting
  1198. with `\zeta_0 = 1`.
  1199. **Examples**
  1200. The roots of unity up to `n = 4`::
  1201. >>> from mpmath import *
  1202. >>> mp.dps = 15; mp.pretty = True
  1203. >>> nprint(unitroots(1))
  1204. [1.0]
  1205. >>> nprint(unitroots(2))
  1206. [1.0, -1.0]
  1207. >>> nprint(unitroots(3))
  1208. [1.0, (-0.5 + 0.866025j), (-0.5 - 0.866025j)]
  1209. >>> nprint(unitroots(4))
  1210. [1.0, (0.0 + 1.0j), -1.0, (0.0 - 1.0j)]
  1211. Roots of unity form a geometric series that sums to 0::
  1212. >>> mp.dps = 50
  1213. >>> chop(fsum(unitroots(25)))
  1214. 0.0
  1215. Primitive roots up to `n = 4`::
  1216. >>> mp.dps = 15
  1217. >>> nprint(unitroots(1, primitive=True))
  1218. [1.0]
  1219. >>> nprint(unitroots(2, primitive=True))
  1220. [-1.0]
  1221. >>> nprint(unitroots(3, primitive=True))
  1222. [(-0.5 + 0.866025j), (-0.5 - 0.866025j)]
  1223. >>> nprint(unitroots(4, primitive=True))
  1224. [(0.0 + 1.0j), (0.0 - 1.0j)]
  1225. There are only four primitive 12th roots::
  1226. >>> nprint(unitroots(12, primitive=True))
  1227. [(0.866025 + 0.5j), (-0.866025 + 0.5j), (-0.866025 - 0.5j), (0.866025 - 0.5j)]
  1228. The `n`-th roots of unity form a group, the cyclic group of order `n`.
  1229. Any primitive root `r` is a generator for this group, meaning that
  1230. `r^0, r^1, \ldots, r^{n-1}` gives the whole set of unit roots (in
  1231. some permuted order)::
  1232. >>> for r in unitroots(6): print(r)
  1233. ...
  1234. 1.0
  1235. (0.5 + 0.866025403784439j)
  1236. (-0.5 + 0.866025403784439j)
  1237. -1.0
  1238. (-0.5 - 0.866025403784439j)
  1239. (0.5 - 0.866025403784439j)
  1240. >>> r = unitroots(6, primitive=True)[1]
  1241. >>> for k in range(6): print(chop(r**k))
  1242. ...
  1243. 1.0
  1244. (0.5 - 0.866025403784439j)
  1245. (-0.5 - 0.866025403784439j)
  1246. -1.0
  1247. (-0.5 + 0.866025403784438j)
  1248. (0.5 + 0.866025403784438j)
  1249. The number of primitive roots equals the Euler totient function `\phi(n)`::
  1250. >>> [len(unitroots(n, primitive=True)) for n in range(1,20)]
  1251. [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 18]
  1252. """
  1253. log = r"""
  1254. Computes the base-`b` logarithm of `x`, `\log_b(x)`. If `b` is
  1255. unspecified, :func:`~mpmath.log` computes the natural (base `e`) logarithm
  1256. and is equivalent to :func:`~mpmath.ln`. In general, the base `b` logarithm
  1257. is defined in terms of the natural logarithm as
  1258. `\log_b(x) = \ln(x)/\ln(b)`.
  1259. By convention, we take `\log(0) = -\infty`.
  1260. The natural logarithm is real if `x > 0` and complex if `x < 0` or if
  1261. `x` is complex. The principal branch of the complex logarithm is
  1262. used, meaning that `\Im(\ln(x)) = -\pi < \arg(x) \le \pi`.
  1263. **Examples**
  1264. Some basic values and limits::
  1265. >>> from mpmath import *
  1266. >>> mp.dps = 15; mp.pretty = True
  1267. >>> log(1)
  1268. 0.0
  1269. >>> log(2)
  1270. 0.693147180559945
  1271. >>> log(1000,10)
  1272. 3.0
  1273. >>> log(4, 16)
  1274. 0.5
  1275. >>> log(j)
  1276. (0.0 + 1.5707963267949j)
  1277. >>> log(-1)
  1278. (0.0 + 3.14159265358979j)
  1279. >>> log(0)
  1280. -inf
  1281. >>> log(inf)
  1282. +inf
  1283. The natural logarithm is the antiderivative of `1/x`::
  1284. >>> quad(lambda x: 1/x, [1, 5])
  1285. 1.6094379124341
  1286. >>> log(5)
  1287. 1.6094379124341
  1288. >>> diff(log, 10)
  1289. 0.1
  1290. The Taylor series expansion of the natural logarithm around
  1291. `x = 1` has coefficients `(-1)^{n+1}/n`::
  1292. >>> nprint(taylor(log, 1, 7))
  1293. [0.0, 1.0, -0.5, 0.333333, -0.25, 0.2, -0.166667, 0.142857]
  1294. :func:`~mpmath.log` supports arbitrary precision evaluation::
  1295. >>> mp.dps = 50
  1296. >>> log(pi)
  1297. 1.1447298858494001741434273513530587116472948129153
  1298. >>> log(pi, pi**3)
  1299. 0.33333333333333333333333333333333333333333333333333
  1300. >>> mp.dps = 25
  1301. >>> log(3+4j)
  1302. (1.609437912434100374600759 + 0.9272952180016122324285125j)
  1303. """
  1304. log10 = r"""
  1305. Computes the base-10 logarithm of `x`, `\log_{10}(x)`. ``log10(x)``
  1306. is equivalent to ``log(x, 10)``.
  1307. """
  1308. fmod = r"""
  1309. Converts `x` and `y` to mpmath numbers and returns `x \mod y`.
  1310. For mpmath numbers, this is equivalent to ``x % y``.
  1311. >>> from mpmath import *
  1312. >>> mp.dps = 15; mp.pretty = True
  1313. >>> fmod(100, pi)
  1314. 2.61062773871641
  1315. You can use :func:`~mpmath.fmod` to compute fractional parts of numbers::
  1316. >>> fmod(10.25, 1)
  1317. 0.25
  1318. """
  1319. radians = r"""
  1320. Converts the degree angle `x` to radians::
  1321. >>> from mpmath import *
  1322. >>> mp.dps = 15; mp.pretty = True
  1323. >>> radians(60)
  1324. 1.0471975511966
  1325. """
  1326. degrees = r"""
  1327. Converts the radian angle `x` to a degree angle::
  1328. >>> from mpmath import *
  1329. >>> mp.dps = 15; mp.pretty = True
  1330. >>> degrees(pi/3)
  1331. 60.0
  1332. """
  1333. atan2 = r"""
  1334. Computes the two-argument arctangent, `\mathrm{atan2}(y, x)`,
  1335. giving the signed angle between the positive `x`-axis and the
  1336. point `(x, y)` in the 2D plane. This function is defined for
  1337. real `x` and `y` only.
  1338. The two-argument arctangent essentially computes
  1339. `\mathrm{atan}(y/x)`, but accounts for the signs of both
  1340. `x` and `y` to give the angle for the correct quadrant. The
  1341. following examples illustrate the difference::
  1342. >>> from mpmath import *
  1343. >>> mp.dps = 15; mp.pretty = True
  1344. >>> atan2(1,1), atan(1/1.)
  1345. (0.785398163397448, 0.785398163397448)
  1346. >>> atan2(1,-1), atan(1/-1.)
  1347. (2.35619449019234, -0.785398163397448)
  1348. >>> atan2(-1,1), atan(-1/1.)
  1349. (-0.785398163397448, -0.785398163397448)
  1350. >>> atan2(-1,-1), atan(-1/-1.)
  1351. (-2.35619449019234, 0.785398163397448)
  1352. The angle convention is the same as that used for the complex
  1353. argument; see :func:`~mpmath.arg`.
  1354. """
  1355. fibonacci = r"""
  1356. ``fibonacci(n)`` computes the `n`-th Fibonacci number, `F(n)`. The
  1357. Fibonacci numbers are defined by the recurrence `F(n) = F(n-1) + F(n-2)`
  1358. with the initial values `F(0) = 0`, `F(1) = 1`. :func:`~mpmath.fibonacci`
  1359. extends this definition to arbitrary real and complex arguments
  1360. using the formula
  1361. .. math ::
  1362. F(z) = \frac{\phi^z - \cos(\pi z) \phi^{-z}}{\sqrt 5}
  1363. where `\phi` is the golden ratio. :func:`~mpmath.fibonacci` also uses this
  1364. continuous formula to compute `F(n)` for extremely large `n`, where
  1365. calculating the exact integer would be wasteful.
  1366. For convenience, :func:`~mpmath.fib` is available as an alias for
  1367. :func:`~mpmath.fibonacci`.
  1368. **Basic examples**
  1369. Some small Fibonacci numbers are::
  1370. >>> from mpmath import *
  1371. >>> mp.dps = 15; mp.pretty = True
  1372. >>> for i in range(10):
  1373. ... print(fibonacci(i))
  1374. ...
  1375. 0.0
  1376. 1.0
  1377. 1.0
  1378. 2.0
  1379. 3.0
  1380. 5.0
  1381. 8.0
  1382. 13.0
  1383. 21.0
  1384. 34.0
  1385. >>> fibonacci(50)
  1386. 12586269025.0
  1387. The recurrence for `F(n)` extends backwards to negative `n`::
  1388. >>> for i in range(10):
  1389. ... print(fibonacci(-i))
  1390. ...
  1391. 0.0
  1392. 1.0
  1393. -1.0
  1394. 2.0
  1395. -3.0
  1396. 5.0
  1397. -8.0
  1398. 13.0
  1399. -21.0
  1400. 34.0
  1401. Large Fibonacci numbers will be computed approximately unless
  1402. the precision is set high enough::
  1403. >>> fib(200)
  1404. 2.8057117299251e+41
  1405. >>> mp.dps = 45
  1406. >>> fib(200)
  1407. 280571172992510140037611932413038677189525.0
  1408. :func:`~mpmath.fibonacci` can compute approximate Fibonacci numbers
  1409. of stupendous size::
  1410. >>> mp.dps = 15
  1411. >>> fibonacci(10**25)
  1412. 3.49052338550226e+2089876402499787337692720
  1413. **Real and complex arguments**
  1414. The extended Fibonacci function is an analytic function. The
  1415. property `F(z) = F(z-1) + F(z-2)` holds for arbitrary `z`::
  1416. >>> mp.dps = 15
  1417. >>> fib(pi)
  1418. 2.1170270579161
  1419. >>> fib(pi-1) + fib(pi-2)
  1420. 2.1170270579161
  1421. >>> fib(3+4j)
  1422. (-5248.51130728372 - 14195.962288353j)
  1423. >>> fib(2+4j) + fib(1+4j)
  1424. (-5248.51130728372 - 14195.962288353j)
  1425. The Fibonacci function has infinitely many roots on the
  1426. negative half-real axis. The first root is at 0, the second is
  1427. close to -0.18, and then there are infinitely many roots that
  1428. asymptotically approach `-n+1/2`::
  1429. >>> findroot(fib, -0.2)
  1430. -0.183802359692956
  1431. >>> findroot(fib, -2)
  1432. -1.57077646820395
  1433. >>> findroot(fib, -17)
  1434. -16.4999999596115
  1435. >>> findroot(fib, -24)
  1436. -23.5000000000479
  1437. **Mathematical relationships**
  1438. For large `n`, `F(n+1)/F(n)` approaches the golden ratio::
  1439. >>> mp.dps = 50
  1440. >>> fibonacci(101)/fibonacci(100)
  1441. 1.6180339887498948482045868343656381177203127439638
  1442. >>> +phi
  1443. 1.6180339887498948482045868343656381177203091798058
  1444. The sum of reciprocal Fibonacci numbers converges to an irrational
  1445. number for which no closed form expression is known::
  1446. >>> mp.dps = 15
  1447. >>> nsum(lambda n: 1/fib(n), [1, inf])
  1448. 3.35988566624318
  1449. Amazingly, however, the sum of odd-index reciprocal Fibonacci
  1450. numbers can be expressed in terms of a Jacobi theta function::
  1451. >>> nsum(lambda n: 1/fib(2*n+1), [0, inf])
  1452. 1.82451515740692
  1453. >>> sqrt(5)*jtheta(2,0,(3-sqrt(5))/2)**2/4
  1454. 1.82451515740692
  1455. Some related sums can be done in closed form::
  1456. >>> nsum(lambda k: 1/(1+fib(2*k+1)), [0, inf])
  1457. 1.11803398874989
  1458. >>> phi - 0.5
  1459. 1.11803398874989
  1460. >>> f = lambda k:(-1)**(k+1) / sum(fib(n)**2 for n in range(1,int(k+1)))
  1461. >>> nsum(f, [1, inf])
  1462. 0.618033988749895
  1463. >>> phi-1
  1464. 0.618033988749895
  1465. **References**
  1466. 1. http://mathworld.wolfram.com/FibonacciNumber.html
  1467. """
  1468. altzeta = r"""
  1469. Gives the Dirichlet eta function, `\eta(s)`, also known as the
  1470. alternating zeta function. This function is defined in analogy
  1471. with the Riemann zeta function as providing the sum of the
  1472. alternating series
  1473. .. math ::
  1474. \eta(s) = \sum_{k=0}^{\infty} \frac{(-1)^k}{k^s}
  1475. = 1-\frac{1}{2^s}+\frac{1}{3^s}-\frac{1}{4^s}+\ldots
  1476. The eta function, unlike the Riemann zeta function, is an entire
  1477. function, having a finite value for all complex `s`. The special case
  1478. `\eta(1) = \log(2)` gives the value of the alternating harmonic series.
  1479. The alternating zeta function may expressed using the Riemann zeta function
  1480. as `\eta(s) = (1 - 2^{1-s}) \zeta(s)`. It can also be expressed
  1481. in terms of the Hurwitz zeta function, for example using
  1482. :func:`~mpmath.dirichlet` (see documentation for that function).
  1483. **Examples**
  1484. Some special values are::
  1485. >>> from mpmath import *
  1486. >>> mp.dps = 15; mp.pretty = True
  1487. >>> altzeta(1)
  1488. 0.693147180559945
  1489. >>> altzeta(0)
  1490. 0.5
  1491. >>> altzeta(-1)
  1492. 0.25
  1493. >>> altzeta(-2)
  1494. 0.0
  1495. An example of a sum that can be computed more accurately and
  1496. efficiently via :func:`~mpmath.altzeta` than via numerical summation::
  1497. >>> sum(-(-1)**n / mpf(n)**2.5 for n in range(1, 100))
  1498. 0.867204951503984
  1499. >>> altzeta(2.5)
  1500. 0.867199889012184
  1501. At positive even integers, the Dirichlet eta function
  1502. evaluates to a rational multiple of a power of `\pi`::
  1503. >>> altzeta(2)
  1504. 0.822467033424113
  1505. >>> pi**2/12
  1506. 0.822467033424113
  1507. Like the Riemann zeta function, `\eta(s)`, approaches 1
  1508. as `s` approaches positive infinity, although it does
  1509. so from below rather than from above::
  1510. >>> altzeta(30)
  1511. 0.999999999068682
  1512. >>> altzeta(inf)
  1513. 1.0
  1514. >>> mp.pretty = False
  1515. >>> altzeta(1000, rounding='d')
  1516. mpf('0.99999999999999989')
  1517. >>> altzeta(1000, rounding='u')
  1518. mpf('1.0')
  1519. **References**
  1520. 1. http://mathworld.wolfram.com/DirichletEtaFunction.html
  1521. 2. http://en.wikipedia.org/wiki/Dirichlet_eta_function
  1522. """
  1523. factorial = r"""
  1524. Computes the factorial, `x!`. For integers `n \ge 0`, we have
  1525. `n! = 1 \cdot 2 \cdots (n-1) \cdot n` and more generally the factorial
  1526. is defined for real or complex `x` by `x! = \Gamma(x+1)`.
  1527. **Examples**
  1528. Basic values and limits::
  1529. >>> from mpmath import *
  1530. >>> mp.dps = 15; mp.pretty = True
  1531. >>> for k in range(6):
  1532. ... print("%s %s" % (k, fac(k)))
  1533. ...
  1534. 0 1.0
  1535. 1 1.0
  1536. 2 2.0
  1537. 3 6.0
  1538. 4 24.0
  1539. 5 120.0
  1540. >>> fac(inf)
  1541. +inf
  1542. >>> fac(0.5), sqrt(pi)/2
  1543. (0.886226925452758, 0.886226925452758)
  1544. For large positive `x`, `x!` can be approximated by
  1545. Stirling's formula::
  1546. >>> x = 10**10
  1547. >>> fac(x)
  1548. 2.32579620567308e+95657055186
  1549. >>> sqrt(2*pi*x)*(x/e)**x
  1550. 2.32579597597705e+95657055186
  1551. :func:`~mpmath.fac` supports evaluation for astronomically large values::
  1552. >>> fac(10**30)
  1553. 6.22311232304258e+29565705518096748172348871081098
  1554. Reciprocal factorials appear in the Taylor series of the
  1555. exponential function (among many other contexts)::
  1556. >>> nsum(lambda k: 1/fac(k), [0, inf]), exp(1)
  1557. (2.71828182845905, 2.71828182845905)
  1558. >>> nsum(lambda k: pi**k/fac(k), [0, inf]), exp(pi)
  1559. (23.1406926327793, 23.1406926327793)
  1560. """
  1561. gamma = r"""
  1562. Computes the gamma function, `\Gamma(x)`. The gamma function is a
  1563. shifted version of the ordinary factorial, satisfying
  1564. `\Gamma(n) = (n-1)!` for integers `n > 0`. More generally, it
  1565. is defined by
  1566. .. math ::
  1567. \Gamma(x) = \int_0^{\infty} t^{x-1} e^{-t}\, dt
  1568. for any real or complex `x` with `\Re(x) > 0` and for `\Re(x) < 0`
  1569. by analytic continuation.
  1570. **Examples**
  1571. Basic values and limits::
  1572. >>> from mpmath import *
  1573. >>> mp.dps = 15; mp.pretty = True
  1574. >>> for k in range(1, 6):
  1575. ... print("%s %s" % (k, gamma(k)))
  1576. ...
  1577. 1 1.0
  1578. 2 1.0
  1579. 3 2.0
  1580. 4 6.0
  1581. 5 24.0
  1582. >>> gamma(inf)
  1583. +inf
  1584. >>> gamma(0)
  1585. Traceback (most recent call last):
  1586. ...
  1587. ValueError: gamma function pole
  1588. The gamma function of a half-integer is a rational multiple of
  1589. `\sqrt{\pi}`::
  1590. >>> gamma(0.5), sqrt(pi)
  1591. (1.77245385090552, 1.77245385090552)
  1592. >>> gamma(1.5), sqrt(pi)/2
  1593. (0.886226925452758, 0.886226925452758)
  1594. We can check the integral definition::
  1595. >>> gamma(3.5)
  1596. 3.32335097044784
  1597. >>> quad(lambda t: t**2.5*exp(-t), [0,inf])
  1598. 3.32335097044784
  1599. :func:`~mpmath.gamma` supports arbitrary-precision evaluation and
  1600. complex arguments::
  1601. >>> mp.dps = 50
  1602. >>> gamma(sqrt(3))
  1603. 0.91510229697308632046045539308226554038315280564184
  1604. >>> mp.dps = 25
  1605. >>> gamma(2j)
  1606. (0.009902440080927490985955066 - 0.07595200133501806872408048j)
  1607. Arguments can also be large. Note that the gamma function grows
  1608. very quickly::
  1609. >>> mp.dps = 15
  1610. >>> gamma(10**20)
  1611. 1.9328495143101e+1956570551809674817225
  1612. **References**
  1613. * [Spouge]_
  1614. """
  1615. psi = r"""
  1616. Gives the polygamma function of order `m` of `z`, `\psi^{(m)}(z)`.
  1617. Special cases are known as the *digamma function* (`\psi^{(0)}(z)`),
  1618. the *trigamma function* (`\psi^{(1)}(z)`), etc. The polygamma
  1619. functions are defined as the logarithmic derivatives of the gamma
  1620. function:
  1621. .. math ::
  1622. \psi^{(m)}(z) = \left(\frac{d}{dz}\right)^{m+1} \log \Gamma(z)
  1623. In particular, `\psi^{(0)}(z) = \Gamma'(z)/\Gamma(z)`. In the
  1624. present implementation of :func:`~mpmath.psi`, the order `m` must be a
  1625. nonnegative integer, while the argument `z` may be an arbitrary
  1626. complex number (with exception for the polygamma function's poles
  1627. at `z = 0, -1, -2, \ldots`).
  1628. **Examples**
  1629. For various rational arguments, the polygamma function reduces to
  1630. a combination of standard mathematical constants::
  1631. >>> from mpmath import *
  1632. >>> mp.dps = 25; mp.pretty = True
  1633. >>> psi(0, 1), -euler
  1634. (-0.5772156649015328606065121, -0.5772156649015328606065121)
  1635. >>> psi(1, '1/4'), pi**2+8*catalan
  1636. (17.19732915450711073927132, 17.19732915450711073927132)
  1637. >>> psi(2, '1/2'), -14*apery
  1638. (-16.82879664423431999559633, -16.82879664423431999559633)
  1639. The polygamma functions are derivatives of each other::
  1640. >>> diff(lambda x: psi(3, x), pi), psi(4, pi)
  1641. (-0.1105749312578862734526952, -0.1105749312578862734526952)
  1642. >>> quad(lambda x: psi(4, x), [2, 3]), psi(3,3)-psi(3,2)
  1643. (-0.375, -0.375)
  1644. The digamma function diverges logarithmically as `z \to \infty`,
  1645. while higher orders tend to zero::
  1646. >>> psi(0,inf), psi(1,inf), psi(2,inf)
  1647. (+inf, 0.0, 0.0)
  1648. Evaluation for a complex argument::
  1649. >>> psi(2, -1-2j)
  1650. (0.03902435405364952654838445 + 0.1574325240413029954685366j)
  1651. Evaluation is supported for large orders `m` and/or large
  1652. arguments `z`::
  1653. >>> psi(3, 10**100)
  1654. 2.0e-300
  1655. >>> psi(250, 10**30+10**20*j)
  1656. (-1.293142504363642687204865e-7010 + 3.232856260909107391513108e-7018j)
  1657. **Application to infinite series**
  1658. Any infinite series where the summand is a rational function of
  1659. the index `k` can be evaluated in closed form in terms of polygamma
  1660. functions of the roots and poles of the summand::
  1661. >>> a = sqrt(2)
  1662. >>> b = sqrt(3)
  1663. >>> nsum(lambda k: 1/((k+a)**2*(k+b)), [0, inf])
  1664. 0.4049668927517857061917531
  1665. >>> (psi(0,a)-psi(0,b)-a*psi(1,a)+b*psi(1,a))/(a-b)**2
  1666. 0.4049668927517857061917531
  1667. This follows from the series representation (`m > 0`)
  1668. .. math ::
  1669. \psi^{(m)}(z) = (-1)^{m+1} m! \sum_{k=0}^{\infty}
  1670. \frac{1}{(z+k)^{m+1}}.
  1671. Since the roots of a polynomial may be complex, it is sometimes
  1672. necessary to use the complex polygamma function to evaluate
  1673. an entirely real-valued sum::
  1674. >>> nsum(lambda k: 1/(k**2-2*k+3), [0, inf])
  1675. 1.694361433907061256154665
  1676. >>> nprint(polyroots([1,-2,3]))
  1677. [(1.0 - 1.41421j), (1.0 + 1.41421j)]
  1678. >>> r1 = 1-sqrt(2)*j
  1679. >>> r2 = r1.conjugate()
  1680. >>> (psi(0,-r2)-psi(0,-r1))/(r1-r2)
  1681. (1.694361433907061256154665 + 0.0j)
  1682. """
  1683. digamma = r"""
  1684. Shortcut for ``psi(0,z)``.
  1685. """
  1686. harmonic = r"""
  1687. If `n` is an integer, ``harmonic(n)`` gives a floating-point
  1688. approximation of the `n`-th harmonic number `H(n)`, defined as
  1689. .. math ::
  1690. H(n) = 1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}
  1691. The first few harmonic numbers are::
  1692. >>> from mpmath import *
  1693. >>> mp.dps = 15; mp.pretty = True
  1694. >>> for n in range(8):
  1695. ... print("%s %s" % (n, harmonic(n)))
  1696. ...
  1697. 0 0.0
  1698. 1 1.0
  1699. 2 1.5
  1700. 3 1.83333333333333
  1701. 4 2.08333333333333
  1702. 5 2.28333333333333
  1703. 6 2.45
  1704. 7 2.59285714285714
  1705. The infinite harmonic series `1 + 1/2 + 1/3 + \ldots` diverges::
  1706. >>> harmonic(inf)
  1707. +inf
  1708. :func:`~mpmath.harmonic` is evaluated using the digamma function rather
  1709. than by summing the harmonic series term by term. It can therefore
  1710. be computed quickly for arbitrarily large `n`, and even for
  1711. nonintegral arguments::
  1712. >>> harmonic(10**100)
  1713. 230.835724964306
  1714. >>> harmonic(0.5)
  1715. 0.613705638880109
  1716. >>> harmonic(3+4j)
  1717. (2.24757548223494 + 0.850502209186044j)
  1718. :func:`~mpmath.harmonic` supports arbitrary precision evaluation::
  1719. >>> mp.dps = 50
  1720. >>> harmonic(11)
  1721. 3.0198773448773448773448773448773448773448773448773
  1722. >>> harmonic(pi)
  1723. 1.8727388590273302654363491032336134987519132374152
  1724. The harmonic series diverges, but at a glacial pace. It is possible
  1725. to calculate the exact number of terms required before the sum
  1726. exceeds a given amount, say 100::
  1727. >>> mp.dps = 50
  1728. >>> v = 10**findroot(lambda x: harmonic(10**x) - 100, 10)
  1729. >>> v
  1730. 15092688622113788323693563264538101449859496.864101
  1731. >>> v = int(ceil(v))
  1732. >>> print(v)
  1733. 15092688622113788323693563264538101449859497
  1734. >>> harmonic(v-1)
  1735. 99.999999999999999999999999999999999999999999942747
  1736. >>> harmonic(v)
  1737. 100.000000000000000000000000000000000000000000009
  1738. """
  1739. bernoulli = r"""
  1740. Computes the nth Bernoulli number, `B_n`, for any integer `n \ge 0`.
  1741. The Bernoulli numbers are rational numbers, but this function
  1742. returns a floating-point approximation. To obtain an exact
  1743. fraction, use :func:`~mpmath.bernfrac` instead.
  1744. **Examples**
  1745. Numerical values of the first few Bernoulli numbers::
  1746. >>> from mpmath import *
  1747. >>> mp.dps = 15; mp.pretty = True
  1748. >>> for n in range(15):
  1749. ... print("%s %s" % (n, bernoulli(n)))
  1750. ...
  1751. 0 1.0
  1752. 1 -0.5
  1753. 2 0.166666666666667
  1754. 3 0.0
  1755. 4 -0.0333333333333333
  1756. 5 0.0
  1757. 6 0.0238095238095238
  1758. 7 0.0
  1759. 8 -0.0333333333333333
  1760. 9 0.0
  1761. 10 0.0757575757575758
  1762. 11 0.0
  1763. 12 -0.253113553113553
  1764. 13 0.0
  1765. 14 1.16666666666667
  1766. Bernoulli numbers can be approximated with arbitrary precision::
  1767. >>> mp.dps = 50
  1768. >>> bernoulli(100)
  1769. -2.8382249570693706959264156336481764738284680928013e+78
  1770. Arbitrarily large `n` are supported::
  1771. >>> mp.dps = 15
  1772. >>> bernoulli(10**20 + 2)
  1773. 3.09136296657021e+1876752564973863312327
  1774. The Bernoulli numbers are related to the Riemann zeta function
  1775. at integer arguments::
  1776. >>> -bernoulli(8) * (2*pi)**8 / (2*fac(8))
  1777. 1.00407735619794
  1778. >>> zeta(8)
  1779. 1.00407735619794
  1780. **Algorithm**
  1781. For small `n` (`n < 3000`) :func:`~mpmath.bernoulli` uses a recurrence
  1782. formula due to Ramanujan. All results in this range are cached,
  1783. so sequential computation of small Bernoulli numbers is
  1784. guaranteed to be fast.
  1785. For larger `n`, `B_n` is evaluated in terms of the Riemann zeta
  1786. function.
  1787. """
  1788. stieltjes = r"""
  1789. For a nonnegative integer `n`, ``stieltjes(n)`` computes the
  1790. `n`-th Stieltjes constant `\gamma_n`, defined as the
  1791. `n`-th coefficient in the Laurent series expansion of the
  1792. Riemann zeta function around the pole at `s = 1`. That is,
  1793. we have:
  1794. .. math ::
  1795. \zeta(s) = \frac{1}{s-1} \sum_{n=0}^{\infty}
  1796. \frac{(-1)^n}{n!} \gamma_n (s-1)^n
  1797. More generally, ``stieltjes(n, a)`` gives the corresponding
  1798. coefficient `\gamma_n(a)` for the Hurwitz zeta function
  1799. `\zeta(s,a)` (with `\gamma_n = \gamma_n(1)`).
  1800. **Examples**
  1801. The zeroth Stieltjes constant is just Euler's constant `\gamma`::
  1802. >>> from mpmath import *
  1803. >>> mp.dps = 15; mp.pretty = True
  1804. >>> stieltjes(0)
  1805. 0.577215664901533
  1806. Some more values are::
  1807. >>> stieltjes(1)
  1808. -0.0728158454836767
  1809. >>> stieltjes(10)
  1810. 0.000205332814909065
  1811. >>> stieltjes(30)
  1812. 0.00355772885557316
  1813. >>> stieltjes(1000)
  1814. -1.57095384420474e+486
  1815. >>> stieltjes(2000)
  1816. 2.680424678918e+1109
  1817. >>> stieltjes(1, 2.5)
  1818. -0.23747539175716
  1819. An alternative way to compute `\gamma_1`::
  1820. >>> diff(extradps(15)(lambda x: 1/(x-1) - zeta(x)), 1)
  1821. -0.0728158454836767
  1822. :func:`~mpmath.stieltjes` supports arbitrary precision evaluation::
  1823. >>> mp.dps = 50
  1824. >>> stieltjes(2)
  1825. -0.0096903631928723184845303860352125293590658061013408
  1826. **Algorithm**
  1827. :func:`~mpmath.stieltjes` numerically evaluates the integral in
  1828. the following representation due to Ainsworth, Howell and
  1829. Coffey [1], [2]:
  1830. .. math ::
  1831. \gamma_n(a) = \frac{\log^n a}{2a} - \frac{\log^{n+1}(a)}{n+1} +
  1832. \frac{2}{a} \Re \int_0^{\infty}
  1833. \frac{(x/a-i)\log^n(a-ix)}{(1+x^2/a^2)(e^{2\pi x}-1)} dx.
  1834. For some reference values with `a = 1`, see e.g. [4].
  1835. **References**
  1836. 1. O. R. Ainsworth & L. W. Howell, "An integral representation of
  1837. the generalized Euler-Mascheroni constants", NASA Technical
  1838. Paper 2456 (1985),
  1839. http://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19850014994_1985014994.pdf
  1840. 2. M. W. Coffey, "The Stieltjes constants, their relation to the
  1841. `\eta_j` coefficients, and representation of the Hurwitz
  1842. zeta function", arXiv:0706.0343v1 http://arxiv.org/abs/0706.0343
  1843. 3. http://mathworld.wolfram.com/StieltjesConstants.html
  1844. 4. http://pi.lacim.uqam.ca/piDATA/stieltjesgamma.txt
  1845. """
  1846. gammaprod = r"""
  1847. Given iterables `a` and `b`, ``gammaprod(a, b)`` computes the
  1848. product / quotient of gamma functions:
  1849. .. math ::
  1850. \frac{\Gamma(a_0) \Gamma(a_1) \cdots \Gamma(a_p)}
  1851. {\Gamma(b_0) \Gamma(b_1) \cdots \Gamma(b_q)}
  1852. Unlike direct calls to :func:`~mpmath.gamma`, :func:`~mpmath.gammaprod` considers
  1853. the entire product as a limit and evaluates this limit properly if
  1854. any of the numerator or denominator arguments are nonpositive
  1855. integers such that poles of the gamma function are encountered.
  1856. That is, :func:`~mpmath.gammaprod` evaluates
  1857. .. math ::
  1858. \lim_{\epsilon \to 0}
  1859. \frac{\Gamma(a_0+\epsilon) \Gamma(a_1+\epsilon) \cdots
  1860. \Gamma(a_p+\epsilon)}
  1861. {\Gamma(b_0+\epsilon) \Gamma(b_1+\epsilon) \cdots
  1862. \Gamma(b_q+\epsilon)}
  1863. In particular:
  1864. * If there are equally many poles in the numerator and the
  1865. denominator, the limit is a rational number times the remaining,
  1866. regular part of the product.
  1867. * If there are more poles in the numerator, :func:`~mpmath.gammaprod`
  1868. returns ``+inf``.
  1869. * If there are more poles in the denominator, :func:`~mpmath.gammaprod`
  1870. returns 0.
  1871. **Examples**
  1872. The reciprocal gamma function `1/\Gamma(x)` evaluated at `x = 0`::
  1873. >>> from mpmath import *
  1874. >>> mp.dps = 15
  1875. >>> gammaprod([], [0])
  1876. 0.0
  1877. A limit::
  1878. >>> gammaprod([-4], [-3])
  1879. -0.25
  1880. >>> limit(lambda x: gamma(x-1)/gamma(x), -3, direction=1)
  1881. -0.25
  1882. >>> limit(lambda x: gamma(x-1)/gamma(x), -3, direction=-1)
  1883. -0.25
  1884. """
  1885. beta = r"""
  1886. Computes the beta function,
  1887. `B(x,y) = \Gamma(x) \Gamma(y) / \Gamma(x+y)`.
  1888. The beta function is also commonly defined by the integral
  1889. representation
  1890. .. math ::
  1891. B(x,y) = \int_0^1 t^{x-1} (1-t)^{y-1} \, dt
  1892. **Examples**
  1893. For integer and half-integer arguments where all three gamma
  1894. functions are finite, the beta function becomes either rational
  1895. number or a rational multiple of `\pi`::
  1896. >>> from mpmath import *
  1897. >>> mp.dps = 15; mp.pretty = True
  1898. >>> beta(5, 2)
  1899. 0.0333333333333333
  1900. >>> beta(1.5, 2)
  1901. 0.266666666666667
  1902. >>> 16*beta(2.5, 1.5)
  1903. 3.14159265358979
  1904. Where appropriate, :func:`~mpmath.beta` evaluates limits. A pole
  1905. of the beta function is taken to result in ``+inf``::
  1906. >>> beta(-0.5, 0.5)
  1907. 0.0
  1908. >>> beta(-3, 3)
  1909. -0.333333333333333
  1910. >>> beta(-2, 3)
  1911. +inf
  1912. >>> beta(inf, 1)
  1913. 0.0
  1914. >>> beta(inf, 0)
  1915. nan
  1916. :func:`~mpmath.beta` supports complex numbers and arbitrary precision
  1917. evaluation::
  1918. >>> beta(1, 2+j)
  1919. (0.4 - 0.2j)
  1920. >>> mp.dps = 25
  1921. >>> beta(j,0.5)
  1922. (1.079424249270925780135675 - 1.410032405664160838288752j)
  1923. >>> mp.dps = 50
  1924. >>> beta(pi, e)
  1925. 0.037890298781212201348153837138927165984170287886464
  1926. Various integrals can be computed by means of the
  1927. beta function::
  1928. >>> mp.dps = 15
  1929. >>> quad(lambda t: t**2.5*(1-t)**2, [0, 1])
  1930. 0.0230880230880231
  1931. >>> beta(3.5, 3)
  1932. 0.0230880230880231
  1933. >>> quad(lambda t: sin(t)**4 * sqrt(cos(t)), [0, pi/2])
  1934. 0.319504062596158
  1935. >>> beta(2.5, 0.75)/2
  1936. 0.319504062596158
  1937. """
  1938. betainc = r"""
  1939. ``betainc(a, b, x1=0, x2=1, regularized=False)`` gives the generalized
  1940. incomplete beta function,
  1941. .. math ::
  1942. I_{x_1}^{x_2}(a,b) = \int_{x_1}^{x_2} t^{a-1} (1-t)^{b-1} dt.
  1943. When `x_1 = 0, x_2 = 1`, this reduces to the ordinary (complete)
  1944. beta function `B(a,b)`; see :func:`~mpmath.beta`.
  1945. With the keyword argument ``regularized=True``, :func:`~mpmath.betainc`
  1946. computes the regularized incomplete beta function
  1947. `I_{x_1}^{x_2}(a,b) / B(a,b)`. This is the cumulative distribution of the
  1948. beta distribution with parameters `a`, `b`.
  1949. .. note :
  1950. Implementations of the incomplete beta function in some other
  1951. software uses a different argument order. For example, Mathematica uses the
  1952. reversed argument order ``Beta[x1,x2,a,b]``. For the equivalent of SciPy's
  1953. three-argument incomplete beta integral (implicitly with `x1 = 0`), use
  1954. ``betainc(a,b,0,x2,regularized=True)``.
  1955. **Examples**
  1956. Verifying that :func:`~mpmath.betainc` computes the integral in the
  1957. definition::
  1958. >>> from mpmath import *
  1959. >>> mp.dps = 25; mp.pretty = True
  1960. >>> x,y,a,b = 3, 4, 0, 6
  1961. >>> betainc(x, y, a, b)
  1962. -4010.4
  1963. >>> quad(lambda t: t**(x-1) * (1-t)**(y-1), [a, b])
  1964. -4010.4
  1965. The arguments may be arbitrary complex numbers::
  1966. >>> betainc(0.75, 1-4j, 0, 2+3j)
  1967. (0.2241657956955709603655887 + 0.3619619242700451992411724j)
  1968. With regularization::
  1969. >>> betainc(1, 2, 0, 0.25, regularized=True)
  1970. 0.4375
  1971. >>> betainc(pi, e, 0, 1, regularized=True) # Complete
  1972. 1.0
  1973. The beta integral satisfies some simple argument transformation
  1974. symmetries::
  1975. >>> mp.dps = 15
  1976. >>> betainc(2,3,4,5), -betainc(2,3,5,4), betainc(3,2,1-5,1-4)
  1977. (56.0833333333333, 56.0833333333333, 56.0833333333333)
  1978. The beta integral can often be evaluated analytically. For integer and
  1979. rational arguments, the incomplete beta function typically reduces to a
  1980. simple algebraic-logarithmic expression::
  1981. >>> mp.dps = 25
  1982. >>> identify(chop(betainc(0, 0, 3, 4)))
  1983. '-(log((9/8)))'
  1984. >>> identify(betainc(2, 3, 4, 5))
  1985. '(673/12)'
  1986. >>> identify(betainc(1.5, 1, 1, 2))
  1987. '((-12+sqrt(1152))/18)'
  1988. """
  1989. binomial = r"""
  1990. Computes the binomial coefficient
  1991. .. math ::
  1992. {n \choose k} = \frac{n!}{k!(n-k)!}.
  1993. The binomial coefficient gives the number of ways that `k` items
  1994. can be chosen from a set of `n` items. More generally, the binomial
  1995. coefficient is a well-defined function of arbitrary real or
  1996. complex `n` and `k`, via the gamma function.
  1997. **Examples**
  1998. Generate Pascal's triangle::
  1999. >>> from mpmath import *
  2000. >>> mp.dps = 15; mp.pretty = True
  2001. >>> for n in range(5):
  2002. ... nprint([binomial(n,k) for k in range(n+1)])
  2003. ...
  2004. [1.0]
  2005. [1.0, 1.0]
  2006. [1.0, 2.0, 1.0]
  2007. [1.0, 3.0, 3.0, 1.0]
  2008. [1.0, 4.0, 6.0, 4.0, 1.0]
  2009. There is 1 way to select 0 items from the empty set, and 0 ways to
  2010. select 1 item from the empty set::
  2011. >>> binomial(0, 0)
  2012. 1.0
  2013. >>> binomial(0, 1)
  2014. 0.0
  2015. :func:`~mpmath.binomial` supports large arguments::
  2016. >>> binomial(10**20, 10**20-5)
  2017. 8.33333333333333e+97
  2018. >>> binomial(10**20, 10**10)
  2019. 2.60784095465201e+104342944813
  2020. Nonintegral binomial coefficients find use in series
  2021. expansions::
  2022. >>> nprint(taylor(lambda x: (1+x)**0.25, 0, 4))
  2023. [1.0, 0.25, -0.09375, 0.0546875, -0.0375977]
  2024. >>> nprint([binomial(0.25, k) for k in range(5)])
  2025. [1.0, 0.25, -0.09375, 0.0546875, -0.0375977]
  2026. An integral representation::
  2027. >>> n, k = 5, 3
  2028. >>> f = lambda t: exp(-j*k*t)*(1+exp(j*t))**n
  2029. >>> chop(quad(f, [-pi,pi])/(2*pi))
  2030. 10.0
  2031. >>> binomial(n,k)
  2032. 10.0
  2033. """
  2034. rf = r"""
  2035. Computes the rising factorial or Pochhammer symbol,
  2036. .. math ::
  2037. x^{(n)} = x (x+1) \cdots (x+n-1) = \frac{\Gamma(x+n)}{\Gamma(x)}
  2038. where the rightmost expression is valid for nonintegral `n`.
  2039. **Examples**
  2040. For integral `n`, the rising factorial is a polynomial::
  2041. >>> from mpmath import *
  2042. >>> mp.dps = 15; mp.pretty = True
  2043. >>> for n in range(5):
  2044. ... nprint(taylor(lambda x: rf(x,n), 0, n))
  2045. ...
  2046. [1.0]
  2047. [0.0, 1.0]
  2048. [0.0, 1.0, 1.0]
  2049. [0.0, 2.0, 3.0, 1.0]
  2050. [0.0, 6.0, 11.0, 6.0, 1.0]
  2051. Evaluation is supported for arbitrary arguments::
  2052. >>> rf(2+3j, 5.5)
  2053. (-7202.03920483347 - 3777.58810701527j)
  2054. """
  2055. ff = r"""
  2056. Computes the falling factorial,
  2057. .. math ::
  2058. (x)_n = x (x-1) \cdots (x-n+1) = \frac{\Gamma(x+1)}{\Gamma(x-n+1)}
  2059. where the rightmost expression is valid for nonintegral `n`.
  2060. **Examples**
  2061. For integral `n`, the falling factorial is a polynomial::
  2062. >>> from mpmath import *
  2063. >>> mp.dps = 15; mp.pretty = True
  2064. >>> for n in range(5):
  2065. ... nprint(taylor(lambda x: ff(x,n), 0, n))
  2066. ...
  2067. [1.0]
  2068. [0.0, 1.0]
  2069. [0.0, -1.0, 1.0]
  2070. [0.0, 2.0, -3.0, 1.0]
  2071. [0.0, -6.0, 11.0, -6.0, 1.0]
  2072. Evaluation is supported for arbitrary arguments::
  2073. >>> ff(2+3j, 5.5)
  2074. (-720.41085888203 + 316.101124983878j)
  2075. """
  2076. fac2 = r"""
  2077. Computes the double factorial `x!!`, defined for integers
  2078. `x > 0` by
  2079. .. math ::
  2080. x!! = \begin{cases}
  2081. 1 \cdot 3 \cdots (x-2) \cdot x & x \;\mathrm{odd} \\
  2082. 2 \cdot 4 \cdots (x-2) \cdot x & x \;\mathrm{even}
  2083. \end{cases}
  2084. and more generally by [1]
  2085. .. math ::
  2086. x!! = 2^{x/2} \left(\frac{\pi}{2}\right)^{(\cos(\pi x)-1)/4}
  2087. \Gamma\left(\frac{x}{2}+1\right).
  2088. **Examples**
  2089. The integer sequence of double factorials begins::
  2090. >>> from mpmath import *
  2091. >>> mp.dps = 15; mp.pretty = True
  2092. >>> nprint([fac2(n) for n in range(10)])
  2093. [1.0, 1.0, 2.0, 3.0, 8.0, 15.0, 48.0, 105.0, 384.0, 945.0]
  2094. For large `x`, double factorials follow a Stirling-like asymptotic
  2095. approximation::
  2096. >>> x = mpf(10000)
  2097. >>> fac2(x)
  2098. 5.97272691416282e+17830
  2099. >>> sqrt(pi)*x**((x+1)/2)*exp(-x/2)
  2100. 5.97262736954392e+17830
  2101. The recurrence formula `x!! = x (x-2)!!` can be reversed to
  2102. define the double factorial of negative odd integers (but
  2103. not negative even integers)::
  2104. >>> fac2(-1), fac2(-3), fac2(-5), fac2(-7)
  2105. (1.0, -1.0, 0.333333333333333, -0.0666666666666667)
  2106. >>> fac2(-2)
  2107. Traceback (most recent call last):
  2108. ...
  2109. ValueError: gamma function pole
  2110. With the exception of the poles at negative even integers,
  2111. :func:`~mpmath.fac2` supports evaluation for arbitrary complex arguments.
  2112. The recurrence formula is valid generally::
  2113. >>> fac2(pi+2j)
  2114. (-1.3697207890154e-12 + 3.93665300979176e-12j)
  2115. >>> (pi+2j)*fac2(pi-2+2j)
  2116. (-1.3697207890154e-12 + 3.93665300979176e-12j)
  2117. Double factorials should not be confused with nested factorials,
  2118. which are immensely larger::
  2119. >>> fac(fac(20))
  2120. 5.13805976125208e+43675043585825292774
  2121. >>> fac2(20)
  2122. 3715891200.0
  2123. Double factorials appear, among other things, in series expansions
  2124. of Gaussian functions and the error function. Infinite series
  2125. include::
  2126. >>> nsum(lambda k: 1/fac2(k), [0, inf])
  2127. 3.05940740534258
  2128. >>> sqrt(e)*(1+sqrt(pi/2)*erf(sqrt(2)/2))
  2129. 3.05940740534258
  2130. >>> nsum(lambda k: 2**k/fac2(2*k-1), [1, inf])
  2131. 4.06015693855741
  2132. >>> e * erf(1) * sqrt(pi)
  2133. 4.06015693855741
  2134. A beautiful Ramanujan sum::
  2135. >>> nsum(lambda k: (-1)**k*(fac2(2*k-1)/fac2(2*k))**3, [0,inf])
  2136. 0.90917279454693
  2137. >>> (gamma('9/8')/gamma('5/4')/gamma('7/8'))**2
  2138. 0.90917279454693
  2139. **References**
  2140. 1. http://functions.wolfram.com/GammaBetaErf/Factorial2/27/01/0002/
  2141. 2. http://mathworld.wolfram.com/DoubleFactorial.html
  2142. """
  2143. hyper = r"""
  2144. Evaluates the generalized hypergeometric function
  2145. .. math ::
  2146. \,_pF_q(a_1,\ldots,a_p; b_1,\ldots,b_q; z) =
  2147. \sum_{n=0}^\infty \frac{(a_1)_n (a_2)_n \ldots (a_p)_n}
  2148. {(b_1)_n(b_2)_n\ldots(b_q)_n} \frac{z^n}{n!}
  2149. where `(x)_n` denotes the rising factorial (see :func:`~mpmath.rf`).
  2150. The parameters lists ``a_s`` and ``b_s`` may contain integers,
  2151. real numbers, complex numbers, as well as exact fractions given in
  2152. the form of tuples `(p, q)`. :func:`~mpmath.hyper` is optimized to handle
  2153. integers and fractions more efficiently than arbitrary
  2154. floating-point parameters (since rational parameters are by
  2155. far the most common).
  2156. **Examples**
  2157. Verifying that :func:`~mpmath.hyper` gives the sum in the definition, by
  2158. comparison with :func:`~mpmath.nsum`::
  2159. >>> from mpmath import *
  2160. >>> mp.dps = 25; mp.pretty = True
  2161. >>> a,b,c,d = 2,3,4,5
  2162. >>> x = 0.25
  2163. >>> hyper([a,b],[c,d],x)
  2164. 1.078903941164934876086237
  2165. >>> fn = lambda n: rf(a,n)*rf(b,n)/rf(c,n)/rf(d,n)*x**n/fac(n)
  2166. >>> nsum(fn, [0, inf])
  2167. 1.078903941164934876086237
  2168. The parameters can be any combination of integers, fractions,
  2169. floats and complex numbers::
  2170. >>> a, b, c, d, e = 1, (-1,2), pi, 3+4j, (2,3)
  2171. >>> x = 0.2j
  2172. >>> hyper([a,b],[c,d,e],x)
  2173. (0.9923571616434024810831887 - 0.005753848733883879742993122j)
  2174. >>> b, e = -0.5, mpf(2)/3
  2175. >>> fn = lambda n: rf(a,n)*rf(b,n)/rf(c,n)/rf(d,n)/rf(e,n)*x**n/fac(n)
  2176. >>> nsum(fn, [0, inf])
  2177. (0.9923571616434024810831887 - 0.005753848733883879742993122j)
  2178. The `\,_0F_0` and `\,_1F_0` series are just elementary functions::
  2179. >>> a, z = sqrt(2), +pi
  2180. >>> hyper([],[],z)
  2181. 23.14069263277926900572909
  2182. >>> exp(z)
  2183. 23.14069263277926900572909
  2184. >>> hyper([a],[],z)
  2185. (-0.09069132879922920160334114 + 0.3283224323946162083579656j)
  2186. >>> (1-z)**(-a)
  2187. (-0.09069132879922920160334114 + 0.3283224323946162083579656j)
  2188. If any `a_k` coefficient is a nonpositive integer, the series terminates
  2189. into a finite polynomial::
  2190. >>> hyper([1,1,1,-3],[2,5],1)
  2191. 0.7904761904761904761904762
  2192. >>> identify(_)
  2193. '(83/105)'
  2194. If any `b_k` is a nonpositive integer, the function is undefined (unless the
  2195. series terminates before the division by zero occurs)::
  2196. >>> hyper([1,1,1,-3],[-2,5],1)
  2197. Traceback (most recent call last):
  2198. ...
  2199. ZeroDivisionError: pole in hypergeometric series
  2200. >>> hyper([1,1,1,-1],[-2,5],1)
  2201. 1.1
  2202. Except for polynomial cases, the radius of convergence `R` of the hypergeometric
  2203. series is either `R = \infty` (if `p \le q`), `R = 1` (if `p = q+1`), or
  2204. `R = 0` (if `p > q+1`).
  2205. The analytic continuations of the functions with `p = q+1`, i.e. `\,_2F_1`,
  2206. `\,_3F_2`, `\,_4F_3`, etc, are all implemented and therefore these functions
  2207. can be evaluated for `|z| \ge 1`. The shortcuts :func:`~mpmath.hyp2f1`, :func:`~mpmath.hyp3f2`
  2208. are available to handle the most common cases (see their documentation),
  2209. but functions of higher degree are also supported via :func:`~mpmath.hyper`::
  2210. >>> hyper([1,2,3,4], [5,6,7], 1) # 4F3 at finite-valued branch point
  2211. 1.141783505526870731311423
  2212. >>> hyper([4,5,6,7], [1,2,3], 1) # 4F3 at pole
  2213. +inf
  2214. >>> hyper([1,2,3,4,5], [6,7,8,9], 10) # 5F4
  2215. (1.543998916527972259717257 - 0.5876309929580408028816365j)
  2216. >>> hyper([1,2,3,4,5,6], [7,8,9,10,11], 1j) # 6F5
  2217. (0.9996565821853579063502466 + 0.0129721075905630604445669j)
  2218. Near `z = 1` with noninteger parameters::
  2219. >>> hyper(['1/3',1,'3/2',2], ['1/5','11/6','41/8'], 1)
  2220. 2.219433352235586121250027
  2221. >>> hyper(['1/3',1,'3/2',2], ['1/5','11/6','5/4'], 1)
  2222. +inf
  2223. >>> eps1 = extradps(6)(lambda: 1 - mpf('1e-6'))()
  2224. >>> hyper(['1/3',1,'3/2',2], ['1/5','11/6','5/4'], eps1)
  2225. 2923978034.412973409330956
  2226. Please note that, as currently implemented, evaluation of `\,_pF_{p-1}`
  2227. with `p \ge 3` may be slow or inaccurate when `|z-1|` is small,
  2228. for some parameter values.
  2229. Evaluation may be aborted if convergence appears to be too slow.
  2230. The optional ``maxterms`` (limiting the number of series terms) and ``maxprec``
  2231. (limiting the internal precision) keyword arguments can be used
  2232. to control evaluation::
  2233. >>> hyper([1,2,3], [4,5,6], 10000) # doctest: +IGNORE_EXCEPTION_DETAIL
  2234. Traceback (most recent call last):
  2235. ...
  2236. NoConvergence: Hypergeometric series converges too slowly. Try increasing maxterms.
  2237. >>> hyper([1,2,3], [4,5,6], 10000, maxterms=10**6)
  2238. 7.622806053177969474396918e+4310
  2239. Additional options include ``force_series`` (which forces direct use of
  2240. a hypergeometric series even if another evaluation method might work better)
  2241. and ``asymp_tol`` which controls the target tolerance for using
  2242. asymptotic series.
  2243. When `p > q+1`, ``hyper`` computes the (iterated) Borel sum of the divergent
  2244. series. For `\,_2F_0` the Borel sum has an analytic solution and can be
  2245. computed efficiently (see :func:`~mpmath.hyp2f0`). For higher degrees, the functions
  2246. is evaluated first by attempting to sum it directly as an asymptotic
  2247. series (this only works for tiny `|z|`), and then by evaluating the Borel
  2248. regularized sum using numerical integration. Except for
  2249. special parameter combinations, this can be extremely slow.
  2250. >>> hyper([1,1], [], 0.5) # regularization of 2F0
  2251. (1.340965419580146562086448 + 0.8503366631752726568782447j)
  2252. >>> hyper([1,1,1,1], [1], 0.5) # regularization of 4F1
  2253. (1.108287213689475145830699 + 0.5327107430640678181200491j)
  2254. With the following magnitude of argument, the asymptotic series for `\,_3F_1`
  2255. gives only a few digits. Using Borel summation, ``hyper`` can produce
  2256. a value with full accuracy::
  2257. >>> mp.dps = 15
  2258. >>> hyper([2,0.5,4], [5.25], '0.08', force_series=True) # doctest: +IGNORE_EXCEPTION_DETAIL
  2259. Traceback (most recent call last):
  2260. ...
  2261. NoConvergence: Hypergeometric series converges too slowly. Try increasing maxterms.
  2262. >>> hyper([2,0.5,4], [5.25], '0.08', asymp_tol=1e-4)
  2263. 1.0725535790737
  2264. >>> hyper([2,0.5,4], [5.25], '0.08')
  2265. (1.07269542893559 + 5.54668863216891e-5j)
  2266. >>> hyper([2,0.5,4], [5.25], '-0.08', asymp_tol=1e-4)
  2267. 0.946344925484879
  2268. >>> hyper([2,0.5,4], [5.25], '-0.08')
  2269. 0.946312503737771
  2270. >>> mp.dps = 25
  2271. >>> hyper([2,0.5,4], [5.25], '-0.08')
  2272. 0.9463125037377662296700858
  2273. Note that with the positive `z` value, there is a complex part in the
  2274. correct result, which falls below the tolerance of the asymptotic series.
  2275. By default, a parameter that appears in both ``a_s`` and ``b_s`` will be removed
  2276. unless it is a nonpositive integer. This generally speeds up evaluation
  2277. by producing a hypergeometric function of lower order.
  2278. This optimization can be disabled by passing ``eliminate=False``.
  2279. >>> hyper([1,2,3], [4,5,3], 10000)
  2280. 1.268943190440206905892212e+4321
  2281. >>> hyper([1,2,3], [4,5,3], 10000, eliminate=False) # doctest: +IGNORE_EXCEPTION_DETAIL
  2282. Traceback (most recent call last):
  2283. ...
  2284. NoConvergence: Hypergeometric series converges too slowly. Try increasing maxterms.
  2285. >>> hyper([1,2,3], [4,5,3], 10000, eliminate=False, maxterms=10**6)
  2286. 1.268943190440206905892212e+4321
  2287. If a nonpositive integer `-n` appears in both ``a_s`` and ``b_s``, this parameter
  2288. cannot be unambiguously removed since it creates a term 0 / 0.
  2289. In this case the hypergeometric series is understood to terminate before
  2290. the division by zero occurs. This convention is consistent with Mathematica.
  2291. An alternative convention of eliminating the parameters can be toggled
  2292. with ``eliminate_all=True``:
  2293. >>> hyper([2,-1], [-1], 3)
  2294. 7.0
  2295. >>> hyper([2,-1], [-1], 3, eliminate_all=True)
  2296. 0.25
  2297. >>> hyper([2], [], 3)
  2298. 0.25
  2299. """
  2300. hypercomb = r"""
  2301. Computes a weighted combination of hypergeometric functions
  2302. .. math ::
  2303. \sum_{r=1}^N \left[ \prod_{k=1}^{l_r} {w_{r,k}}^{c_{r,k}}
  2304. \frac{\prod_{k=1}^{m_r} \Gamma(\alpha_{r,k})}{\prod_{k=1}^{n_r}
  2305. \Gamma(\beta_{r,k})}
  2306. \,_{p_r}F_{q_r}(a_{r,1},\ldots,a_{r,p}; b_{r,1},
  2307. \ldots, b_{r,q}; z_r)\right].
  2308. Typically the parameters are linear combinations of a small set of base
  2309. parameters; :func:`~mpmath.hypercomb` permits computing a correct value in
  2310. the case that some of the `\alpha`, `\beta`, `b` turn out to be
  2311. nonpositive integers, or if division by zero occurs for some `w^c`,
  2312. assuming that there are opposing singularities that cancel out.
  2313. The limit is computed by evaluating the function with the base
  2314. parameters perturbed, at a higher working precision.
  2315. The first argument should be a function that takes the perturbable
  2316. base parameters ``params`` as input and returns `N` tuples
  2317. ``(w, c, alpha, beta, a, b, z)``, where the coefficients ``w``, ``c``,
  2318. gamma factors ``alpha``, ``beta``, and hypergeometric coefficients
  2319. ``a``, ``b`` each should be lists of numbers, and ``z`` should be a single
  2320. number.
  2321. **Examples**
  2322. The following evaluates
  2323. .. math ::
  2324. (a-1) \frac{\Gamma(a-3)}{\Gamma(a-4)} \,_1F_1(a,a-1,z) = e^z(a-4)(a+z-1)
  2325. with `a=1, z=3`. There is a zero factor, two gamma function poles, and
  2326. the 1F1 function is singular; all singularities cancel out to give a finite
  2327. value::
  2328. >>> from mpmath import *
  2329. >>> mp.dps = 15; mp.pretty = True
  2330. >>> hypercomb(lambda a: [([a-1],[1],[a-3],[a-4],[a],[a-1],3)], [1])
  2331. -180.769832308689
  2332. >>> -9*exp(3)
  2333. -180.769832308689
  2334. """
  2335. hyp0f1 = r"""
  2336. Gives the hypergeometric function `\,_0F_1`, sometimes known as the
  2337. confluent limit function, defined as
  2338. .. math ::
  2339. \,_0F_1(a,z) = \sum_{k=0}^{\infty} \frac{1}{(a)_k} \frac{z^k}{k!}.
  2340. This function satisfies the differential equation `z f''(z) + a f'(z) = f(z)`,
  2341. and is related to the Bessel function of the first kind (see :func:`~mpmath.besselj`).
  2342. ``hyp0f1(a,z)`` is equivalent to ``hyper([],[a],z)``; see documentation for
  2343. :func:`~mpmath.hyper` for more information.
  2344. **Examples**
  2345. Evaluation for arbitrary arguments::
  2346. >>> from mpmath import *
  2347. >>> mp.dps = 25; mp.pretty = True
  2348. >>> hyp0f1(2, 0.25)
  2349. 1.130318207984970054415392
  2350. >>> hyp0f1((1,2), 1234567)
  2351. 6.27287187546220705604627e+964
  2352. >>> hyp0f1(3+4j, 1000000j)
  2353. (3.905169561300910030267132e+606 + 3.807708544441684513934213e+606j)
  2354. Evaluation is supported for arbitrarily large values of `z`,
  2355. using asymptotic expansions::
  2356. >>> hyp0f1(1, 10**50)
  2357. 2.131705322874965310390701e+8685889638065036553022565
  2358. >>> hyp0f1(1, -10**50)
  2359. 1.115945364792025420300208e-13
  2360. Verifying the differential equation::
  2361. >>> a = 2.5
  2362. >>> f = lambda z: hyp0f1(a,z)
  2363. >>> for z in [0, 10, 3+4j]:
  2364. ... chop(z*diff(f,z,2) + a*diff(f,z) - f(z))
  2365. ...
  2366. 0.0
  2367. 0.0
  2368. 0.0
  2369. """
  2370. hyp1f1 = r"""
  2371. Gives the confluent hypergeometric function of the first kind,
  2372. .. math ::
  2373. \,_1F_1(a,b,z) = \sum_{k=0}^{\infty} \frac{(a)_k}{(b)_k} \frac{z^k}{k!},
  2374. also known as Kummer's function and sometimes denoted by `M(a,b,z)`. This
  2375. function gives one solution to the confluent (Kummer's) differential equation
  2376. .. math ::
  2377. z f''(z) + (b-z) f'(z) - af(z) = 0.
  2378. A second solution is given by the `U` function; see :func:`~mpmath.hyperu`.
  2379. Solutions are also given in an alternate form by the Whittaker
  2380. functions (:func:`~mpmath.whitm`, :func:`~mpmath.whitw`).
  2381. ``hyp1f1(a,b,z)`` is equivalent
  2382. to ``hyper([a],[b],z)``; see documentation for :func:`~mpmath.hyper` for more
  2383. information.
  2384. **Examples**
  2385. Evaluation for real and complex values of the argument `z`, with
  2386. fixed parameters `a = 2, b = -1/3`::
  2387. >>> from mpmath import *
  2388. >>> mp.dps = 25; mp.pretty = True
  2389. >>> hyp1f1(2, (-1,3), 3.25)
  2390. -2815.956856924817275640248
  2391. >>> hyp1f1(2, (-1,3), -3.25)
  2392. -1.145036502407444445553107
  2393. >>> hyp1f1(2, (-1,3), 1000)
  2394. -8.021799872770764149793693e+441
  2395. >>> hyp1f1(2, (-1,3), -1000)
  2396. 0.000003131987633006813594535331
  2397. >>> hyp1f1(2, (-1,3), 100+100j)
  2398. (-3.189190365227034385898282e+48 - 1.106169926814270418999315e+49j)
  2399. Parameters may be complex::
  2400. >>> hyp1f1(2+3j, -1+j, 10j)
  2401. (261.8977905181045142673351 + 160.8930312845682213562172j)
  2402. Arbitrarily large values of `z` are supported::
  2403. >>> hyp1f1(3, 4, 10**20)
  2404. 3.890569218254486878220752e+43429448190325182745
  2405. >>> hyp1f1(3, 4, -10**20)
  2406. 6.0e-60
  2407. >>> hyp1f1(3, 4, 10**20*j)
  2408. (-1.935753855797342532571597e-20 - 2.291911213325184901239155e-20j)
  2409. Verifying the differential equation::
  2410. >>> a, b = 1.5, 2
  2411. >>> f = lambda z: hyp1f1(a,b,z)
  2412. >>> for z in [0, -10, 3, 3+4j]:
  2413. ... chop(z*diff(f,z,2) + (b-z)*diff(f,z) - a*f(z))
  2414. ...
  2415. 0.0
  2416. 0.0
  2417. 0.0
  2418. 0.0
  2419. An integral representation::
  2420. >>> a, b = 1.5, 3
  2421. >>> z = 1.5
  2422. >>> hyp1f1(a,b,z)
  2423. 2.269381460919952778587441
  2424. >>> g = lambda t: exp(z*t)*t**(a-1)*(1-t)**(b-a-1)
  2425. >>> gammaprod([b],[a,b-a])*quad(g, [0,1])
  2426. 2.269381460919952778587441
  2427. """
  2428. hyp1f2 = r"""
  2429. Gives the hypergeometric function `\,_1F_2(a_1,a_2;b_1,b_2; z)`.
  2430. The call ``hyp1f2(a1,b1,b2,z)`` is equivalent to
  2431. ``hyper([a1],[b1,b2],z)``.
  2432. Evaluation works for complex and arbitrarily large arguments::
  2433. >>> from mpmath import *
  2434. >>> mp.dps = 25; mp.pretty = True
  2435. >>> a, b, c = 1.5, (-1,3), 2.25
  2436. >>> hyp1f2(a, b, c, 10**20)
  2437. -1.159388148811981535941434e+8685889639
  2438. >>> hyp1f2(a, b, c, -10**20)
  2439. -12.60262607892655945795907
  2440. >>> hyp1f2(a, b, c, 10**20*j)
  2441. (4.237220401382240876065501e+6141851464 - 2.950930337531768015892987e+6141851464j)
  2442. >>> hyp1f2(2+3j, -2j, 0.5j, 10-20j)
  2443. (135881.9905586966432662004 - 86681.95885418079535738828j)
  2444. """
  2445. hyp2f2 = r"""
  2446. Gives the hypergeometric function `\,_2F_2(a_1,a_2;b_1,b_2; z)`.
  2447. The call ``hyp2f2(a1,a2,b1,b2,z)`` is equivalent to
  2448. ``hyper([a1,a2],[b1,b2],z)``.
  2449. Evaluation works for complex and arbitrarily large arguments::
  2450. >>> from mpmath import *
  2451. >>> mp.dps = 25; mp.pretty = True
  2452. >>> a, b, c, d = 1.5, (-1,3), 2.25, 4
  2453. >>> hyp2f2(a, b, c, d, 10**20)
  2454. -5.275758229007902299823821e+43429448190325182663
  2455. >>> hyp2f2(a, b, c, d, -10**20)
  2456. 2561445.079983207701073448
  2457. >>> hyp2f2(a, b, c, d, 10**20*j)
  2458. (2218276.509664121194836667 - 1280722.539991603850462856j)
  2459. >>> hyp2f2(2+3j, -2j, 0.5j, 4j, 10-20j)
  2460. (80500.68321405666957342788 - 20346.82752982813540993502j)
  2461. """
  2462. hyp2f3 = r"""
  2463. Gives the hypergeometric function `\,_2F_3(a_1,a_2;b_1,b_2,b_3; z)`.
  2464. The call ``hyp2f3(a1,a2,b1,b2,b3,z)`` is equivalent to
  2465. ``hyper([a1,a2],[b1,b2,b3],z)``.
  2466. Evaluation works for arbitrarily large arguments::
  2467. >>> from mpmath import *
  2468. >>> mp.dps = 25; mp.pretty = True
  2469. >>> a1,a2,b1,b2,b3 = 1.5, (-1,3), 2.25, 4, (1,5)
  2470. >>> hyp2f3(a1,a2,b1,b2,b3,10**20)
  2471. -4.169178177065714963568963e+8685889590
  2472. >>> hyp2f3(a1,a2,b1,b2,b3,-10**20)
  2473. 7064472.587757755088178629
  2474. >>> hyp2f3(a1,a2,b1,b2,b3,10**20*j)
  2475. (-5.163368465314934589818543e+6141851415 + 1.783578125755972803440364e+6141851416j)
  2476. >>> hyp2f3(2+3j, -2j, 0.5j, 4j, -1-j, 10-20j)
  2477. (-2280.938956687033150740228 + 13620.97336609573659199632j)
  2478. >>> hyp2f3(2+3j, -2j, 0.5j, 4j, -1-j, 10000000-20000000j)
  2479. (4.849835186175096516193e+3504 - 3.365981529122220091353633e+3504j)
  2480. """
  2481. hyp2f1 = r"""
  2482. Gives the Gauss hypergeometric function `\,_2F_1` (often simply referred to as
  2483. *the* hypergeometric function), defined for `|z| < 1` as
  2484. .. math ::
  2485. \,_2F_1(a,b,c,z) = \sum_{k=0}^{\infty}
  2486. \frac{(a)_k (b)_k}{(c)_k} \frac{z^k}{k!}.
  2487. and for `|z| \ge 1` by analytic continuation, with a branch cut on `(1, \infty)`
  2488. when necessary.
  2489. Special cases of this function include many of the orthogonal polynomials as
  2490. well as the incomplete beta function and other functions. Properties of the
  2491. Gauss hypergeometric function are documented comprehensively in many references,
  2492. for example Abramowitz & Stegun, section 15.
  2493. The implementation supports the analytic continuation as well as evaluation
  2494. close to the unit circle where `|z| \approx 1`. The syntax ``hyp2f1(a,b,c,z)``
  2495. is equivalent to ``hyper([a,b],[c],z)``.
  2496. **Examples**
  2497. Evaluation with `z` inside, outside and on the unit circle, for
  2498. fixed parameters::
  2499. >>> from mpmath import *
  2500. >>> mp.dps = 25; mp.pretty = True
  2501. >>> hyp2f1(2, (1,2), 4, 0.75)
  2502. 1.303703703703703703703704
  2503. >>> hyp2f1(2, (1,2), 4, -1.75)
  2504. 0.7431290566046919177853916
  2505. >>> hyp2f1(2, (1,2), 4, 1.75)
  2506. (1.418075801749271137026239 - 1.114976146679907015775102j)
  2507. >>> hyp2f1(2, (1,2), 4, 1)
  2508. 1.6
  2509. >>> hyp2f1(2, (1,2), 4, -1)
  2510. 0.8235498012182875315037882
  2511. >>> hyp2f1(2, (1,2), 4, j)
  2512. (0.9144026291433065674259078 + 0.2050415770437884900574923j)
  2513. >>> hyp2f1(2, (1,2), 4, 2+j)
  2514. (0.9274013540258103029011549 + 0.7455257875808100868984496j)
  2515. >>> hyp2f1(2, (1,2), 4, 0.25j)
  2516. (0.9931169055799728251931672 + 0.06154836525312066938147793j)
  2517. Evaluation with complex parameter values::
  2518. >>> hyp2f1(1+j, 0.75, 10j, 1+5j)
  2519. (0.8834833319713479923389638 + 0.7053886880648105068343509j)
  2520. Evaluation with `z = 1`::
  2521. >>> hyp2f1(-2.5, 3.5, 1.5, 1)
  2522. 0.0
  2523. >>> hyp2f1(-2.5, 3, 4, 1)
  2524. 0.06926406926406926406926407
  2525. >>> hyp2f1(2, 3, 4, 1)
  2526. +inf
  2527. Evaluation for huge arguments::
  2528. >>> hyp2f1((-1,3), 1.75, 4, '1e100')
  2529. (7.883714220959876246415651e+32 + 1.365499358305579597618785e+33j)
  2530. >>> hyp2f1((-1,3), 1.75, 4, '1e1000000')
  2531. (7.883714220959876246415651e+333332 + 1.365499358305579597618785e+333333j)
  2532. >>> hyp2f1((-1,3), 1.75, 4, '1e1000000j')
  2533. (1.365499358305579597618785e+333333 - 7.883714220959876246415651e+333332j)
  2534. An integral representation::
  2535. >>> a,b,c,z = -0.5, 1, 2.5, 0.25
  2536. >>> g = lambda t: t**(b-1) * (1-t)**(c-b-1) * (1-t*z)**(-a)
  2537. >>> gammaprod([c],[b,c-b]) * quad(g, [0,1])
  2538. 0.9480458814362824478852618
  2539. >>> hyp2f1(a,b,c,z)
  2540. 0.9480458814362824478852618
  2541. Verifying the hypergeometric differential equation::
  2542. >>> f = lambda z: hyp2f1(a,b,c,z)
  2543. >>> chop(z*(1-z)*diff(f,z,2) + (c-(a+b+1)*z)*diff(f,z) - a*b*f(z))
  2544. 0.0
  2545. """
  2546. hyp3f2 = r"""
  2547. Gives the generalized hypergeometric function `\,_3F_2`, defined for `|z| < 1`
  2548. as
  2549. .. math ::
  2550. \,_3F_2(a_1,a_2,a_3,b_1,b_2,z) = \sum_{k=0}^{\infty}
  2551. \frac{(a_1)_k (a_2)_k (a_3)_k}{(b_1)_k (b_2)_k} \frac{z^k}{k!}.
  2552. and for `|z| \ge 1` by analytic continuation. The analytic structure of this
  2553. function is similar to that of `\,_2F_1`, generally with a singularity at
  2554. `z = 1` and a branch cut on `(1, \infty)`.
  2555. Evaluation is supported inside, on, and outside
  2556. the circle of convergence `|z| = 1`::
  2557. >>> from mpmath import *
  2558. >>> mp.dps = 25; mp.pretty = True
  2559. >>> hyp3f2(1,2,3,4,5,0.25)
  2560. 1.083533123380934241548707
  2561. >>> hyp3f2(1,2+2j,3,4,5,-10+10j)
  2562. (0.1574651066006004632914361 - 0.03194209021885226400892963j)
  2563. >>> hyp3f2(1,2,3,4,5,-10)
  2564. 0.3071141169208772603266489
  2565. >>> hyp3f2(1,2,3,4,5,10)
  2566. (-0.4857045320523947050581423 - 0.5988311440454888436888028j)
  2567. >>> hyp3f2(0.25,1,1,2,1.5,1)
  2568. 1.157370995096772047567631
  2569. >>> (8-pi-2*ln2)/3
  2570. 1.157370995096772047567631
  2571. >>> hyp3f2(1+j,0.5j,2,1,-2j,-1)
  2572. (1.74518490615029486475959 + 0.1454701525056682297614029j)
  2573. >>> hyp3f2(1+j,0.5j,2,1,-2j,sqrt(j))
  2574. (0.9829816481834277511138055 - 0.4059040020276937085081127j)
  2575. >>> hyp3f2(-3,2,1,-5,4,1)
  2576. 1.41
  2577. >>> hyp3f2(-3,2,1,-5,4,2)
  2578. 2.12
  2579. Evaluation very close to the unit circle::
  2580. >>> hyp3f2(1,2,3,4,5,'1.0001')
  2581. (1.564877796743282766872279 - 3.76821518787438186031973e-11j)
  2582. >>> hyp3f2(1,2,3,4,5,'1+0.0001j')
  2583. (1.564747153061671573212831 + 0.0001305757570366084557648482j)
  2584. >>> hyp3f2(1,2,3,4,5,'0.9999')
  2585. 1.564616644881686134983664
  2586. >>> hyp3f2(1,2,3,4,5,'-0.9999')
  2587. 0.7823896253461678060196207
  2588. .. note ::
  2589. Evaluation for `|z-1|` small can currently be inaccurate or slow
  2590. for some parameter combinations.
  2591. For various parameter combinations, `\,_3F_2` admits representation in terms
  2592. of hypergeometric functions of lower degree, or in terms of
  2593. simpler functions::
  2594. >>> for a, b, z in [(1,2,-1), (2,0.5,1)]:
  2595. ... hyp2f1(a,b,a+b+0.5,z)**2
  2596. ... hyp3f2(2*a,a+b,2*b,a+b+0.5,2*a+2*b,z)
  2597. ...
  2598. 0.4246104461966439006086308
  2599. 0.4246104461966439006086308
  2600. 7.111111111111111111111111
  2601. 7.111111111111111111111111
  2602. >>> z = 2+3j
  2603. >>> hyp3f2(0.5,1,1.5,2,2,z)
  2604. (0.7621440939243342419729144 + 0.4249117735058037649915723j)
  2605. >>> 4*(pi-2*ellipe(z))/(pi*z)
  2606. (0.7621440939243342419729144 + 0.4249117735058037649915723j)
  2607. """
  2608. hyperu = r"""
  2609. Gives the Tricomi confluent hypergeometric function `U`, also known as
  2610. the Kummer or confluent hypergeometric function of the second kind. This
  2611. function gives a second linearly independent solution to the confluent
  2612. hypergeometric differential equation (the first is provided by `\,_1F_1` --
  2613. see :func:`~mpmath.hyp1f1`).
  2614. **Examples**
  2615. Evaluation for arbitrary complex arguments::
  2616. >>> from mpmath import *
  2617. >>> mp.dps = 25; mp.pretty = True
  2618. >>> hyperu(2,3,4)
  2619. 0.0625
  2620. >>> hyperu(0.25, 5, 1000)
  2621. 0.1779949416140579573763523
  2622. >>> hyperu(0.25, 5, -1000)
  2623. (0.1256256609322773150118907 - 0.1256256609322773150118907j)
  2624. The `U` function may be singular at `z = 0`::
  2625. >>> hyperu(1.5, 2, 0)
  2626. +inf
  2627. >>> hyperu(1.5, -2, 0)
  2628. 0.1719434921288400112603671
  2629. Verifying the differential equation::
  2630. >>> a, b = 1.5, 2
  2631. >>> f = lambda z: hyperu(a,b,z)
  2632. >>> for z in [-10, 3, 3+4j]:
  2633. ... chop(z*diff(f,z,2) + (b-z)*diff(f,z) - a*f(z))
  2634. ...
  2635. 0.0
  2636. 0.0
  2637. 0.0
  2638. An integral representation::
  2639. >>> a,b,z = 2, 3.5, 4.25
  2640. >>> hyperu(a,b,z)
  2641. 0.06674960718150520648014567
  2642. >>> quad(lambda t: exp(-z*t)*t**(a-1)*(1+t)**(b-a-1),[0,inf]) / gamma(a)
  2643. 0.06674960718150520648014567
  2644. [1] http://people.math.sfu.ca/~cbm/aands/page_504.htm
  2645. """
  2646. hyp2f0 = r"""
  2647. Gives the hypergeometric function `\,_2F_0`, defined formally by the
  2648. series
  2649. .. math ::
  2650. \,_2F_0(a,b;;z) = \sum_{n=0}^{\infty} (a)_n (b)_n \frac{z^n}{n!}.
  2651. This series usually does not converge. For small enough `z`, it can be viewed
  2652. as an asymptotic series that may be summed directly with an appropriate
  2653. truncation. When this is not the case, :func:`~mpmath.hyp2f0` gives a regularized sum,
  2654. or equivalently, it uses a representation in terms of the
  2655. hypergeometric U function [1]. The series also converges when either `a` or `b`
  2656. is a nonpositive integer, as it then terminates into a polynomial
  2657. after `-a` or `-b` terms.
  2658. **Examples**
  2659. Evaluation is supported for arbitrary complex arguments::
  2660. >>> from mpmath import *
  2661. >>> mp.dps = 25; mp.pretty = True
  2662. >>> hyp2f0((2,3), 1.25, -100)
  2663. 0.07095851870980052763312791
  2664. >>> hyp2f0((2,3), 1.25, 100)
  2665. (-0.03254379032170590665041131 + 0.07269254613282301012735797j)
  2666. >>> hyp2f0(-0.75, 1-j, 4j)
  2667. (-0.3579987031082732264862155 - 3.052951783922142735255881j)
  2668. Even with real arguments, the regularized value of 2F0 is often complex-valued,
  2669. but the imaginary part decreases exponentially as `z \to 0`. In the following
  2670. example, the first call uses complex evaluation while the second has a small
  2671. enough `z` to evaluate using the direct series and thus the returned value
  2672. is strictly real (this should be taken to indicate that the imaginary
  2673. part is less than ``eps``)::
  2674. >>> mp.dps = 15
  2675. >>> hyp2f0(1.5, 0.5, 0.05)
  2676. (1.04166637647907 + 8.34584913683906e-8j)
  2677. >>> hyp2f0(1.5, 0.5, 0.0005)
  2678. 1.00037535207621
  2679. The imaginary part can be retrieved by increasing the working precision::
  2680. >>> mp.dps = 80
  2681. >>> nprint(hyp2f0(1.5, 0.5, 0.009).imag)
  2682. 1.23828e-46
  2683. In the polynomial case (the series terminating), 2F0 can evaluate exactly::
  2684. >>> mp.dps = 15
  2685. >>> hyp2f0(-6,-6,2)
  2686. 291793.0
  2687. >>> identify(hyp2f0(-2,1,0.25))
  2688. '(5/8)'
  2689. The coefficients of the polynomials can be recovered using Taylor expansion::
  2690. >>> nprint(taylor(lambda x: hyp2f0(-3,0.5,x), 0, 10))
  2691. [1.0, -1.5, 2.25, -1.875, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  2692. >>> nprint(taylor(lambda x: hyp2f0(-4,0.5,x), 0, 10))
  2693. [1.0, -2.0, 4.5, -7.5, 6.5625, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  2694. [1] http://people.math.sfu.ca/~cbm/aands/page_504.htm
  2695. """
  2696. gammainc = r"""
  2697. ``gammainc(z, a=0, b=inf)`` computes the (generalized) incomplete
  2698. gamma function with integration limits `[a, b]`:
  2699. .. math ::
  2700. \Gamma(z,a,b) = \int_a^b t^{z-1} e^{-t} \, dt
  2701. The generalized incomplete gamma function reduces to the
  2702. following special cases when one or both endpoints are fixed:
  2703. * `\Gamma(z,0,\infty)` is the standard ("complete")
  2704. gamma function, `\Gamma(z)` (available directly
  2705. as the mpmath function :func:`~mpmath.gamma`)
  2706. * `\Gamma(z,a,\infty)` is the "upper" incomplete gamma
  2707. function, `\Gamma(z,a)`
  2708. * `\Gamma(z,0,b)` is the "lower" incomplete gamma
  2709. function, `\gamma(z,b)`.
  2710. Of course, we have
  2711. `\Gamma(z,0,x) + \Gamma(z,x,\infty) = \Gamma(z)`
  2712. for all `z` and `x`.
  2713. Note however that some authors reverse the order of the
  2714. arguments when defining the lower and upper incomplete
  2715. gamma function, so one should be careful to get the correct
  2716. definition.
  2717. If also given the keyword argument ``regularized=True``,
  2718. :func:`~mpmath.gammainc` computes the "regularized" incomplete gamma
  2719. function
  2720. .. math ::
  2721. P(z,a,b) = \frac{\Gamma(z,a,b)}{\Gamma(z)}.
  2722. **Examples**
  2723. We can compare with numerical quadrature to verify that
  2724. :func:`~mpmath.gammainc` computes the integral in the definition::
  2725. >>> from mpmath import *
  2726. >>> mp.dps = 25; mp.pretty = True
  2727. >>> gammainc(2+3j, 4, 10)
  2728. (0.00977212668627705160602312 - 0.0770637306312989892451977j)
  2729. >>> quad(lambda t: t**(2+3j-1) * exp(-t), [4, 10])
  2730. (0.00977212668627705160602312 - 0.0770637306312989892451977j)
  2731. Argument symmetries follow directly from the integral definition::
  2732. >>> gammainc(3, 4, 5) + gammainc(3, 5, 4)
  2733. 0.0
  2734. >>> gammainc(3,0,2) + gammainc(3,2,4); gammainc(3,0,4)
  2735. 1.523793388892911312363331
  2736. 1.523793388892911312363331
  2737. >>> findroot(lambda z: gammainc(2,z,3), 1)
  2738. 3.0
  2739. Evaluation for arbitrarily large arguments::
  2740. >>> gammainc(10, 100)
  2741. 4.083660630910611272288592e-26
  2742. >>> gammainc(10, 10000000000000000)
  2743. 5.290402449901174752972486e-4342944819032375
  2744. >>> gammainc(3+4j, 1000000+1000000j)
  2745. (-1.257913707524362408877881e-434284 + 2.556691003883483531962095e-434284j)
  2746. Evaluation of a generalized incomplete gamma function automatically chooses
  2747. the representation that gives a more accurate result, depending on which
  2748. parameter is larger::
  2749. >>> gammainc(10000000, 3) - gammainc(10000000, 2) # Bad
  2750. 0.0
  2751. >>> gammainc(10000000, 2, 3) # Good
  2752. 1.755146243738946045873491e+4771204
  2753. >>> gammainc(2, 0, 100000001) - gammainc(2, 0, 100000000) # Bad
  2754. 0.0
  2755. >>> gammainc(2, 100000000, 100000001) # Good
  2756. 4.078258353474186729184421e-43429441
  2757. The incomplete gamma functions satisfy simple recurrence
  2758. relations::
  2759. >>> mp.dps = 25
  2760. >>> z, a = mpf(3.5), mpf(2)
  2761. >>> gammainc(z+1, a); z*gammainc(z,a) + a**z*exp(-a)
  2762. 10.60130296933533459267329
  2763. 10.60130296933533459267329
  2764. >>> gammainc(z+1,0,a); z*gammainc(z,0,a) - a**z*exp(-a)
  2765. 1.030425427232114336470932
  2766. 1.030425427232114336470932
  2767. Evaluation at integers and poles::
  2768. >>> gammainc(-3, -4, -5)
  2769. (-0.2214577048967798566234192 + 0.0j)
  2770. >>> gammainc(-3, 0, 5)
  2771. +inf
  2772. If `z` is an integer, the recurrence reduces the incomplete gamma
  2773. function to `P(a) \exp(-a) + Q(b) \exp(-b)` where `P` and
  2774. `Q` are polynomials::
  2775. >>> gammainc(1, 2); exp(-2)
  2776. 0.1353352832366126918939995
  2777. 0.1353352832366126918939995
  2778. >>> mp.dps = 50
  2779. >>> identify(gammainc(6, 1, 2), ['exp(-1)', 'exp(-2)'])
  2780. '(326*exp(-1) + (-872)*exp(-2))'
  2781. The incomplete gamma functions reduce to functions such as
  2782. the exponential integral Ei and the error function for special
  2783. arguments::
  2784. >>> mp.dps = 25
  2785. >>> gammainc(0, 4); -ei(-4)
  2786. 0.00377935240984890647887486
  2787. 0.00377935240984890647887486
  2788. >>> gammainc(0.5, 0, 2); sqrt(pi)*erf(sqrt(2))
  2789. 1.691806732945198336509541
  2790. 1.691806732945198336509541
  2791. """
  2792. erf = r"""
  2793. Computes the error function, `\mathrm{erf}(x)`. The error
  2794. function is the normalized antiderivative of the Gaussian function
  2795. `\exp(-t^2)`. More precisely,
  2796. .. math::
  2797. \mathrm{erf}(x) = \frac{2}{\sqrt \pi} \int_0^x \exp(-t^2) \,dt
  2798. **Basic examples**
  2799. Simple values and limits include::
  2800. >>> from mpmath import *
  2801. >>> mp.dps = 15; mp.pretty = True
  2802. >>> erf(0)
  2803. 0.0
  2804. >>> erf(1)
  2805. 0.842700792949715
  2806. >>> erf(-1)
  2807. -0.842700792949715
  2808. >>> erf(inf)
  2809. 1.0
  2810. >>> erf(-inf)
  2811. -1.0
  2812. For large real `x`, `\mathrm{erf}(x)` approaches 1 very
  2813. rapidly::
  2814. >>> erf(3)
  2815. 0.999977909503001
  2816. >>> erf(5)
  2817. 0.999999999998463
  2818. The error function is an odd function::
  2819. >>> nprint(chop(taylor(erf, 0, 5)))
  2820. [0.0, 1.12838, 0.0, -0.376126, 0.0, 0.112838]
  2821. :func:`~mpmath.erf` implements arbitrary-precision evaluation and
  2822. supports complex numbers::
  2823. >>> mp.dps = 50
  2824. >>> erf(0.5)
  2825. 0.52049987781304653768274665389196452873645157575796
  2826. >>> mp.dps = 25
  2827. >>> erf(1+j)
  2828. (1.316151281697947644880271 + 0.1904534692378346862841089j)
  2829. Evaluation is supported for large arguments::
  2830. >>> mp.dps = 25
  2831. >>> erf('1e1000')
  2832. 1.0
  2833. >>> erf('-1e1000')
  2834. -1.0
  2835. >>> erf('1e-1000')
  2836. 1.128379167095512573896159e-1000
  2837. >>> erf('1e7j')
  2838. (0.0 + 8.593897639029319267398803e+43429448190317j)
  2839. >>> erf('1e7+1e7j')
  2840. (0.9999999858172446172631323 + 3.728805278735270407053139e-8j)
  2841. **Related functions**
  2842. See also :func:`~mpmath.erfc`, which is more accurate for large `x`,
  2843. and :func:`~mpmath.erfi` which gives the antiderivative of
  2844. `\exp(t^2)`.
  2845. The Fresnel integrals :func:`~mpmath.fresnels` and :func:`~mpmath.fresnelc`
  2846. are also related to the error function.
  2847. """
  2848. erfc = r"""
  2849. Computes the complementary error function,
  2850. `\mathrm{erfc}(x) = 1-\mathrm{erf}(x)`.
  2851. This function avoids cancellation that occurs when naively
  2852. computing the complementary error function as ``1-erf(x)``::
  2853. >>> from mpmath import *
  2854. >>> mp.dps = 15; mp.pretty = True
  2855. >>> 1 - erf(10)
  2856. 0.0
  2857. >>> erfc(10)
  2858. 2.08848758376254e-45
  2859. :func:`~mpmath.erfc` works accurately even for ludicrously large
  2860. arguments::
  2861. >>> erfc(10**10)
  2862. 4.3504398860243e-43429448190325182776
  2863. Complex arguments are supported::
  2864. >>> erfc(500+50j)
  2865. (1.19739830969552e-107492 + 1.46072418957528e-107491j)
  2866. """
  2867. erfi = r"""
  2868. Computes the imaginary error function, `\mathrm{erfi}(x)`.
  2869. The imaginary error function is defined in analogy with the
  2870. error function, but with a positive sign in the integrand:
  2871. .. math ::
  2872. \mathrm{erfi}(x) = \frac{2}{\sqrt \pi} \int_0^x \exp(t^2) \,dt
  2873. Whereas the error function rapidly converges to 1 as `x` grows,
  2874. the imaginary error function rapidly diverges to infinity.
  2875. The functions are related as
  2876. `\mathrm{erfi}(x) = -i\,\mathrm{erf}(ix)` for all complex
  2877. numbers `x`.
  2878. **Examples**
  2879. Basic values and limits::
  2880. >>> from mpmath import *
  2881. >>> mp.dps = 15; mp.pretty = True
  2882. >>> erfi(0)
  2883. 0.0
  2884. >>> erfi(1)
  2885. 1.65042575879754
  2886. >>> erfi(-1)
  2887. -1.65042575879754
  2888. >>> erfi(inf)
  2889. +inf
  2890. >>> erfi(-inf)
  2891. -inf
  2892. Note the symmetry between erf and erfi::
  2893. >>> erfi(3j)
  2894. (0.0 + 0.999977909503001j)
  2895. >>> erf(3)
  2896. 0.999977909503001
  2897. >>> erf(1+2j)
  2898. (-0.536643565778565 - 5.04914370344703j)
  2899. >>> erfi(2+1j)
  2900. (-5.04914370344703 - 0.536643565778565j)
  2901. Large arguments are supported::
  2902. >>> erfi(1000)
  2903. 1.71130938718796e+434291
  2904. >>> erfi(10**10)
  2905. 7.3167287567024e+43429448190325182754
  2906. >>> erfi(-10**10)
  2907. -7.3167287567024e+43429448190325182754
  2908. >>> erfi(1000-500j)
  2909. (2.49895233563961e+325717 + 2.6846779342253e+325717j)
  2910. >>> erfi(100000j)
  2911. (0.0 + 1.0j)
  2912. >>> erfi(-100000j)
  2913. (0.0 - 1.0j)
  2914. """
  2915. erfinv = r"""
  2916. Computes the inverse error function, satisfying
  2917. .. math ::
  2918. \mathrm{erf}(\mathrm{erfinv}(x)) =
  2919. \mathrm{erfinv}(\mathrm{erf}(x)) = x.
  2920. This function is defined only for `-1 \le x \le 1`.
  2921. **Examples**
  2922. Special values include::
  2923. >>> from mpmath import *
  2924. >>> mp.dps = 15; mp.pretty = True
  2925. >>> erfinv(0)
  2926. 0.0
  2927. >>> erfinv(1)
  2928. +inf
  2929. >>> erfinv(-1)
  2930. -inf
  2931. The domain is limited to the standard interval::
  2932. >>> erfinv(2)
  2933. Traceback (most recent call last):
  2934. ...
  2935. ValueError: erfinv(x) is defined only for -1 <= x <= 1
  2936. It is simple to check that :func:`~mpmath.erfinv` computes inverse values of
  2937. :func:`~mpmath.erf` as promised::
  2938. >>> erf(erfinv(0.75))
  2939. 0.75
  2940. >>> erf(erfinv(-0.995))
  2941. -0.995
  2942. :func:`~mpmath.erfinv` supports arbitrary-precision evaluation::
  2943. >>> mp.dps = 50
  2944. >>> x = erf(2)
  2945. >>> x
  2946. 0.99532226501895273416206925636725292861089179704006
  2947. >>> erfinv(x)
  2948. 2.0
  2949. A definite integral involving the inverse error function::
  2950. >>> mp.dps = 15
  2951. >>> quad(erfinv, [0, 1])
  2952. 0.564189583547756
  2953. >>> 1/sqrt(pi)
  2954. 0.564189583547756
  2955. The inverse error function can be used to generate random numbers
  2956. with a Gaussian distribution (although this is a relatively
  2957. inefficient algorithm)::
  2958. >>> nprint([erfinv(2*rand()-1) for n in range(6)]) # doctest: +SKIP
  2959. [-0.586747, 1.10233, -0.376796, 0.926037, -0.708142, -0.732012]
  2960. """
  2961. npdf = r"""
  2962. ``npdf(x, mu=0, sigma=1)`` evaluates the probability density
  2963. function of a normal distribution with mean value `\mu`
  2964. and variance `\sigma^2`.
  2965. Elementary properties of the probability distribution can
  2966. be verified using numerical integration::
  2967. >>> from mpmath import *
  2968. >>> mp.dps = 15; mp.pretty = True
  2969. >>> quad(npdf, [-inf, inf])
  2970. 1.0
  2971. >>> quad(lambda x: npdf(x, 3), [3, inf])
  2972. 0.5
  2973. >>> quad(lambda x: npdf(x, 3, 2), [3, inf])
  2974. 0.5
  2975. See also :func:`~mpmath.ncdf`, which gives the cumulative
  2976. distribution.
  2977. """
  2978. ncdf = r"""
  2979. ``ncdf(x, mu=0, sigma=1)`` evaluates the cumulative distribution
  2980. function of a normal distribution with mean value `\mu`
  2981. and variance `\sigma^2`.
  2982. See also :func:`~mpmath.npdf`, which gives the probability density.
  2983. Elementary properties include::
  2984. >>> from mpmath import *
  2985. >>> mp.dps = 15; mp.pretty = True
  2986. >>> ncdf(pi, mu=pi)
  2987. 0.5
  2988. >>> ncdf(-inf)
  2989. 0.0
  2990. >>> ncdf(+inf)
  2991. 1.0
  2992. The cumulative distribution is the integral of the density
  2993. function having identical mu and sigma::
  2994. >>> mp.dps = 15
  2995. >>> diff(ncdf, 2)
  2996. 0.053990966513188
  2997. >>> npdf(2)
  2998. 0.053990966513188
  2999. >>> diff(lambda x: ncdf(x, 1, 0.5), 0)
  3000. 0.107981933026376
  3001. >>> npdf(0, 1, 0.5)
  3002. 0.107981933026376
  3003. """
  3004. expint = r"""
  3005. :func:`~mpmath.expint(n,z)` gives the generalized exponential integral
  3006. or En-function,
  3007. .. math ::
  3008. \mathrm{E}_n(z) = \int_1^{\infty} \frac{e^{-zt}}{t^n} dt,
  3009. where `n` and `z` may both be complex numbers. The case with `n = 1` is
  3010. also given by :func:`~mpmath.e1`.
  3011. **Examples**
  3012. Evaluation at real and complex arguments::
  3013. >>> from mpmath import *
  3014. >>> mp.dps = 25; mp.pretty = True
  3015. >>> expint(1, 6.25)
  3016. 0.0002704758872637179088496194
  3017. >>> expint(-3, 2+3j)
  3018. (0.00299658467335472929656159 + 0.06100816202125885450319632j)
  3019. >>> expint(2+3j, 4-5j)
  3020. (0.001803529474663565056945248 - 0.002235061547756185403349091j)
  3021. At negative integer values of `n`, `E_n(z)` reduces to a
  3022. rational-exponential function::
  3023. >>> f = lambda n, z: fac(n)*sum(z**k/fac(k-1) for k in range(1,n+2))/\
  3024. ... exp(z)/z**(n+2)
  3025. >>> n = 3
  3026. >>> z = 1/pi
  3027. >>> expint(-n,z)
  3028. 584.2604820613019908668219
  3029. >>> f(n,z)
  3030. 584.2604820613019908668219
  3031. >>> n = 5
  3032. >>> expint(-n,z)
  3033. 115366.5762594725451811138
  3034. >>> f(n,z)
  3035. 115366.5762594725451811138
  3036. """
  3037. e1 = r"""
  3038. Computes the exponential integral `\mathrm{E}_1(z)`, given by
  3039. .. math ::
  3040. \mathrm{E}_1(z) = \int_z^{\infty} \frac{e^{-t}}{t} dt.
  3041. This is equivalent to :func:`~mpmath.expint` with `n = 1`.
  3042. **Examples**
  3043. Two ways to evaluate this function::
  3044. >>> from mpmath import *
  3045. >>> mp.dps = 25; mp.pretty = True
  3046. >>> e1(6.25)
  3047. 0.0002704758872637179088496194
  3048. >>> expint(1,6.25)
  3049. 0.0002704758872637179088496194
  3050. The E1-function is essentially the same as the Ei-function (:func:`~mpmath.ei`)
  3051. with negated argument, except for an imaginary branch cut term::
  3052. >>> e1(2.5)
  3053. 0.02491491787026973549562801
  3054. >>> -ei(-2.5)
  3055. 0.02491491787026973549562801
  3056. >>> e1(-2.5)
  3057. (-7.073765894578600711923552 - 3.141592653589793238462643j)
  3058. >>> -ei(2.5)
  3059. -7.073765894578600711923552
  3060. """
  3061. ei = r"""
  3062. Computes the exponential integral or Ei-function, `\mathrm{Ei}(x)`.
  3063. The exponential integral is defined as
  3064. .. math ::
  3065. \mathrm{Ei}(x) = \int_{-\infty\,}^x \frac{e^t}{t} \, dt.
  3066. When the integration range includes `t = 0`, the exponential
  3067. integral is interpreted as providing the Cauchy principal value.
  3068. For real `x`, the Ei-function behaves roughly like
  3069. `\mathrm{Ei}(x) \approx \exp(x) + \log(|x|)`.
  3070. The Ei-function is related to the more general family of exponential
  3071. integral functions denoted by `E_n`, which are available as :func:`~mpmath.expint`.
  3072. **Basic examples**
  3073. Some basic values and limits are::
  3074. >>> from mpmath import *
  3075. >>> mp.dps = 15; mp.pretty = True
  3076. >>> ei(0)
  3077. -inf
  3078. >>> ei(1)
  3079. 1.89511781635594
  3080. >>> ei(inf)
  3081. +inf
  3082. >>> ei(-inf)
  3083. 0.0
  3084. For `x < 0`, the defining integral can be evaluated
  3085. numerically as a reference::
  3086. >>> ei(-4)
  3087. -0.00377935240984891
  3088. >>> quad(lambda t: exp(t)/t, [-inf, -4])
  3089. -0.00377935240984891
  3090. :func:`~mpmath.ei` supports complex arguments and arbitrary
  3091. precision evaluation::
  3092. >>> mp.dps = 50
  3093. >>> ei(pi)
  3094. 10.928374389331410348638445906907535171566338835056
  3095. >>> mp.dps = 25
  3096. >>> ei(3+4j)
  3097. (-4.154091651642689822535359 + 4.294418620024357476985535j)
  3098. **Related functions**
  3099. The exponential integral is closely related to the logarithmic
  3100. integral. See :func:`~mpmath.li` for additional information.
  3101. The exponential integral is related to the hyperbolic
  3102. and trigonometric integrals (see :func:`~mpmath.chi`, :func:`~mpmath.shi`,
  3103. :func:`~mpmath.ci`, :func:`~mpmath.si`) similarly to how the ordinary
  3104. exponential function is related to the hyperbolic and
  3105. trigonometric functions::
  3106. >>> mp.dps = 15
  3107. >>> ei(3)
  3108. 9.93383257062542
  3109. >>> chi(3) + shi(3)
  3110. 9.93383257062542
  3111. >>> chop(ci(3j) - j*si(3j) - pi*j/2)
  3112. 9.93383257062542
  3113. Beware that logarithmic corrections, as in the last example
  3114. above, are required to obtain the correct branch in general.
  3115. For details, see [1].
  3116. The exponential integral is also a special case of the
  3117. hypergeometric function `\,_2F_2`::
  3118. >>> z = 0.6
  3119. >>> z*hyper([1,1],[2,2],z) + (ln(z)-ln(1/z))/2 + euler
  3120. 0.769881289937359
  3121. >>> ei(z)
  3122. 0.769881289937359
  3123. **References**
  3124. 1. Relations between Ei and other functions:
  3125. http://functions.wolfram.com/GammaBetaErf/ExpIntegralEi/27/01/
  3126. 2. Abramowitz & Stegun, section 5:
  3127. http://people.math.sfu.ca/~cbm/aands/page_228.htm
  3128. 3. Asymptotic expansion for Ei:
  3129. http://mathworld.wolfram.com/En-Function.html
  3130. """
  3131. li = r"""
  3132. Computes the logarithmic integral or li-function
  3133. `\mathrm{li}(x)`, defined by
  3134. .. math ::
  3135. \mathrm{li}(x) = \int_0^x \frac{1}{\log t} \, dt
  3136. The logarithmic integral has a singularity at `x = 1`.
  3137. Alternatively, ``li(x, offset=True)`` computes the offset
  3138. logarithmic integral (used in number theory)
  3139. .. math ::
  3140. \mathrm{Li}(x) = \int_2^x \frac{1}{\log t} \, dt.
  3141. These two functions are related via the simple identity
  3142. `\mathrm{Li}(x) = \mathrm{li}(x) - \mathrm{li}(2)`.
  3143. The logarithmic integral should also not be confused with
  3144. the polylogarithm (also denoted by Li), which is implemented
  3145. as :func:`~mpmath.polylog`.
  3146. **Examples**
  3147. Some basic values and limits::
  3148. >>> from mpmath import *
  3149. >>> mp.dps = 30; mp.pretty = True
  3150. >>> li(0)
  3151. 0.0
  3152. >>> li(1)
  3153. -inf
  3154. >>> li(1)
  3155. -inf
  3156. >>> li(2)
  3157. 1.04516378011749278484458888919
  3158. >>> findroot(li, 2)
  3159. 1.45136923488338105028396848589
  3160. >>> li(inf)
  3161. +inf
  3162. >>> li(2, offset=True)
  3163. 0.0
  3164. >>> li(1, offset=True)
  3165. -inf
  3166. >>> li(0, offset=True)
  3167. -1.04516378011749278484458888919
  3168. >>> li(10, offset=True)
  3169. 5.12043572466980515267839286347
  3170. The logarithmic integral can be evaluated for arbitrary
  3171. complex arguments::
  3172. >>> mp.dps = 20
  3173. >>> li(3+4j)
  3174. (3.1343755504645775265 + 2.6769247817778742392j)
  3175. The logarithmic integral is related to the exponential integral::
  3176. >>> ei(log(3))
  3177. 2.1635885946671919729
  3178. >>> li(3)
  3179. 2.1635885946671919729
  3180. The logarithmic integral grows like `O(x/\log(x))`::
  3181. >>> mp.dps = 15
  3182. >>> x = 10**100
  3183. >>> x/log(x)
  3184. 4.34294481903252e+97
  3185. >>> li(x)
  3186. 4.3619719871407e+97
  3187. The prime number theorem states that the number of primes less
  3188. than `x` is asymptotic to `\mathrm{Li}(x)` (equivalently
  3189. `\mathrm{li}(x)`). For example, it is known that there are
  3190. exactly 1,925,320,391,606,803,968,923 prime numbers less than
  3191. `10^{23}` [1]. The logarithmic integral provides a very
  3192. accurate estimate::
  3193. >>> li(10**23, offset=True)
  3194. 1.92532039161405e+21
  3195. A definite integral is::
  3196. >>> quad(li, [0, 1])
  3197. -0.693147180559945
  3198. >>> -ln(2)
  3199. -0.693147180559945
  3200. **References**
  3201. 1. http://mathworld.wolfram.com/PrimeCountingFunction.html
  3202. 2. http://mathworld.wolfram.com/LogarithmicIntegral.html
  3203. """
  3204. ci = r"""
  3205. Computes the cosine integral,
  3206. .. math ::
  3207. \mathrm{Ci}(x) = -\int_x^{\infty} \frac{\cos t}{t}\,dt
  3208. = \gamma + \log x + \int_0^x \frac{\cos t - 1}{t}\,dt
  3209. **Examples**
  3210. Some values and limits::
  3211. >>> from mpmath import *
  3212. >>> mp.dps = 25; mp.pretty = True
  3213. >>> ci(0)
  3214. -inf
  3215. >>> ci(1)
  3216. 0.3374039229009681346626462
  3217. >>> ci(pi)
  3218. 0.07366791204642548599010096
  3219. >>> ci(inf)
  3220. 0.0
  3221. >>> ci(-inf)
  3222. (0.0 + 3.141592653589793238462643j)
  3223. >>> ci(2+3j)
  3224. (1.408292501520849518759125 - 2.983617742029605093121118j)
  3225. The cosine integral behaves roughly like the sinc function
  3226. (see :func:`~mpmath.sinc`) for large real `x`::
  3227. >>> ci(10**10)
  3228. -4.875060251748226537857298e-11
  3229. >>> sinc(10**10)
  3230. -4.875060250875106915277943e-11
  3231. >>> chop(limit(ci, inf))
  3232. 0.0
  3233. It has infinitely many roots on the positive real axis::
  3234. >>> findroot(ci, 1)
  3235. 0.6165054856207162337971104
  3236. >>> findroot(ci, 2)
  3237. 3.384180422551186426397851
  3238. Evaluation is supported for `z` anywhere in the complex plane::
  3239. >>> ci(10**6*(1+j))
  3240. (4.449410587611035724984376e+434287 + 9.75744874290013526417059e+434287j)
  3241. We can evaluate the defining integral as a reference::
  3242. >>> mp.dps = 15
  3243. >>> -quadosc(lambda t: cos(t)/t, [5, inf], omega=1)
  3244. -0.190029749656644
  3245. >>> ci(5)
  3246. -0.190029749656644
  3247. Some infinite series can be evaluated using the
  3248. cosine integral::
  3249. >>> nsum(lambda k: (-1)**k/(fac(2*k)*(2*k)), [1,inf])
  3250. -0.239811742000565
  3251. >>> ci(1) - euler
  3252. -0.239811742000565
  3253. """
  3254. si = r"""
  3255. Computes the sine integral,
  3256. .. math ::
  3257. \mathrm{Si}(x) = \int_0^x \frac{\sin t}{t}\,dt.
  3258. The sine integral is thus the antiderivative of the sinc
  3259. function (see :func:`~mpmath.sinc`).
  3260. **Examples**
  3261. Some values and limits::
  3262. >>> from mpmath import *
  3263. >>> mp.dps = 25; mp.pretty = True
  3264. >>> si(0)
  3265. 0.0
  3266. >>> si(1)
  3267. 0.9460830703671830149413533
  3268. >>> si(-1)
  3269. -0.9460830703671830149413533
  3270. >>> si(pi)
  3271. 1.851937051982466170361053
  3272. >>> si(inf)
  3273. 1.570796326794896619231322
  3274. >>> si(-inf)
  3275. -1.570796326794896619231322
  3276. >>> si(2+3j)
  3277. (4.547513889562289219853204 + 1.399196580646054789459839j)
  3278. The sine integral approaches `\pi/2` for large real `x`::
  3279. >>> si(10**10)
  3280. 1.570796326707584656968511
  3281. >>> pi/2
  3282. 1.570796326794896619231322
  3283. Evaluation is supported for `z` anywhere in the complex plane::
  3284. >>> si(10**6*(1+j))
  3285. (-9.75744874290013526417059e+434287 + 4.449410587611035724984376e+434287j)
  3286. We can evaluate the defining integral as a reference::
  3287. >>> mp.dps = 15
  3288. >>> quad(sinc, [0, 5])
  3289. 1.54993124494467
  3290. >>> si(5)
  3291. 1.54993124494467
  3292. Some infinite series can be evaluated using the
  3293. sine integral::
  3294. >>> nsum(lambda k: (-1)**k/(fac(2*k+1)*(2*k+1)), [0,inf])
  3295. 0.946083070367183
  3296. >>> si(1)
  3297. 0.946083070367183
  3298. """
  3299. chi = r"""
  3300. Computes the hyperbolic cosine integral, defined
  3301. in analogy with the cosine integral (see :func:`~mpmath.ci`) as
  3302. .. math ::
  3303. \mathrm{Chi}(x) = -\int_x^{\infty} \frac{\cosh t}{t}\,dt
  3304. = \gamma + \log x + \int_0^x \frac{\cosh t - 1}{t}\,dt
  3305. Some values and limits::
  3306. >>> from mpmath import *
  3307. >>> mp.dps = 25; mp.pretty = True
  3308. >>> chi(0)
  3309. -inf
  3310. >>> chi(1)
  3311. 0.8378669409802082408946786
  3312. >>> chi(inf)
  3313. +inf
  3314. >>> findroot(chi, 0.5)
  3315. 0.5238225713898644064509583
  3316. >>> chi(2+3j)
  3317. (-0.1683628683277204662429321 + 2.625115880451325002151688j)
  3318. Evaluation is supported for `z` anywhere in the complex plane::
  3319. >>> chi(10**6*(1+j))
  3320. (4.449410587611035724984376e+434287 - 9.75744874290013526417059e+434287j)
  3321. """
  3322. shi = r"""
  3323. Computes the hyperbolic sine integral, defined
  3324. in analogy with the sine integral (see :func:`~mpmath.si`) as
  3325. .. math ::
  3326. \mathrm{Shi}(x) = \int_0^x \frac{\sinh t}{t}\,dt.
  3327. Some values and limits::
  3328. >>> from mpmath import *
  3329. >>> mp.dps = 25; mp.pretty = True
  3330. >>> shi(0)
  3331. 0.0
  3332. >>> shi(1)
  3333. 1.057250875375728514571842
  3334. >>> shi(-1)
  3335. -1.057250875375728514571842
  3336. >>> shi(inf)
  3337. +inf
  3338. >>> shi(2+3j)
  3339. (-0.1931890762719198291678095 + 2.645432555362369624818525j)
  3340. Evaluation is supported for `z` anywhere in the complex plane::
  3341. >>> shi(10**6*(1+j))
  3342. (4.449410587611035724984376e+434287 - 9.75744874290013526417059e+434287j)
  3343. """
  3344. fresnels = r"""
  3345. Computes the Fresnel sine integral
  3346. .. math ::
  3347. S(x) = \int_0^x \sin\left(\frac{\pi t^2}{2}\right) \,dt
  3348. Note that some sources define this function
  3349. without the normalization factor `\pi/2`.
  3350. **Examples**
  3351. Some basic values and limits::
  3352. >>> from mpmath import *
  3353. >>> mp.dps = 25; mp.pretty = True
  3354. >>> fresnels(0)
  3355. 0.0
  3356. >>> fresnels(inf)
  3357. 0.5
  3358. >>> fresnels(-inf)
  3359. -0.5
  3360. >>> fresnels(1)
  3361. 0.4382591473903547660767567
  3362. >>> fresnels(1+2j)
  3363. (36.72546488399143842838788 + 15.58775110440458732748279j)
  3364. Comparing with the definition::
  3365. >>> fresnels(3)
  3366. 0.4963129989673750360976123
  3367. >>> quad(lambda t: sin(pi*t**2/2), [0,3])
  3368. 0.4963129989673750360976123
  3369. """
  3370. fresnelc = r"""
  3371. Computes the Fresnel cosine integral
  3372. .. math ::
  3373. C(x) = \int_0^x \cos\left(\frac{\pi t^2}{2}\right) \,dt
  3374. Note that some sources define this function
  3375. without the normalization factor `\pi/2`.
  3376. **Examples**
  3377. Some basic values and limits::
  3378. >>> from mpmath import *
  3379. >>> mp.dps = 25; mp.pretty = True
  3380. >>> fresnelc(0)
  3381. 0.0
  3382. >>> fresnelc(inf)
  3383. 0.5
  3384. >>> fresnelc(-inf)
  3385. -0.5
  3386. >>> fresnelc(1)
  3387. 0.7798934003768228294742064
  3388. >>> fresnelc(1+2j)
  3389. (16.08787137412548041729489 - 36.22568799288165021578758j)
  3390. Comparing with the definition::
  3391. >>> fresnelc(3)
  3392. 0.6057207892976856295561611
  3393. >>> quad(lambda t: cos(pi*t**2/2), [0,3])
  3394. 0.6057207892976856295561611
  3395. """
  3396. airyai = r"""
  3397. Computes the Airy function `\operatorname{Ai}(z)`, which is
  3398. the solution of the Airy differential equation `f''(z) - z f(z) = 0`
  3399. with initial conditions
  3400. .. math ::
  3401. \operatorname{Ai}(0) =
  3402. \frac{1}{3^{2/3}\Gamma\left(\frac{2}{3}\right)}
  3403. \operatorname{Ai}'(0) =
  3404. -\frac{1}{3^{1/3}\Gamma\left(\frac{1}{3}\right)}.
  3405. Other common ways of defining the Ai-function include
  3406. integrals such as
  3407. .. math ::
  3408. \operatorname{Ai}(x) = \frac{1}{\pi}
  3409. \int_0^{\infty} \cos\left(\frac{1}{3}t^3+xt\right) dt
  3410. \qquad x \in \mathbb{R}
  3411. \operatorname{Ai}(z) = \frac{\sqrt{3}}{2\pi}
  3412. \int_0^{\infty}
  3413. \exp\left(-\frac{t^3}{3}-\frac{z^3}{3t^3}\right) dt.
  3414. The Ai-function is an entire function with a turning point,
  3415. behaving roughly like a slowly decaying sine wave for `z < 0` and
  3416. like a rapidly decreasing exponential for `z > 0`.
  3417. A second solution of the Airy differential equation
  3418. is given by `\operatorname{Bi}(z)` (see :func:`~mpmath.airybi`).
  3419. Optionally, with *derivative=alpha*, :func:`airyai` can compute the
  3420. `\alpha`-th order fractional derivative with respect to `z`.
  3421. For `\alpha = n = 1,2,3,\ldots` this gives the derivative
  3422. `\operatorname{Ai}^{(n)}(z)`, and for `\alpha = -n = -1,-2,-3,\ldots`
  3423. this gives the `n`-fold iterated integral
  3424. .. math ::
  3425. f_0(z) = \operatorname{Ai}(z)
  3426. f_n(z) = \int_0^z f_{n-1}(t) dt.
  3427. The Ai-function has infinitely many zeros, all located along the
  3428. negative half of the real axis. They can be computed with
  3429. :func:`~mpmath.airyaizero`.
  3430. **Plots**
  3431. .. literalinclude :: /plots/ai.py
  3432. .. image :: /plots/ai.png
  3433. .. literalinclude :: /plots/ai_c.py
  3434. .. image :: /plots/ai_c.png
  3435. **Basic examples**
  3436. Limits and values include::
  3437. >>> from mpmath import *
  3438. >>> mp.dps = 25; mp.pretty = True
  3439. >>> airyai(0); 1/(power(3,'2/3')*gamma('2/3'))
  3440. 0.3550280538878172392600632
  3441. 0.3550280538878172392600632
  3442. >>> airyai(1)
  3443. 0.1352924163128814155241474
  3444. >>> airyai(-1)
  3445. 0.5355608832923521187995166
  3446. >>> airyai(inf); airyai(-inf)
  3447. 0.0
  3448. 0.0
  3449. Evaluation is supported for large magnitudes of the argument::
  3450. >>> airyai(-100)
  3451. 0.1767533932395528780908311
  3452. >>> airyai(100)
  3453. 2.634482152088184489550553e-291
  3454. >>> airyai(50+50j)
  3455. (-5.31790195707456404099817e-68 - 1.163588003770709748720107e-67j)
  3456. >>> airyai(-50+50j)
  3457. (1.041242537363167632587245e+158 + 3.347525544923600321838281e+157j)
  3458. Huge arguments are also fine::
  3459. >>> airyai(10**10)
  3460. 1.162235978298741779953693e-289529654602171
  3461. >>> airyai(-10**10)
  3462. 0.0001736206448152818510510181
  3463. >>> w = airyai(10**10*(1+j))
  3464. >>> w.real
  3465. 5.711508683721355528322567e-186339621747698
  3466. >>> w.imag
  3467. 1.867245506962312577848166e-186339621747697
  3468. The first root of the Ai-function is::
  3469. >>> findroot(airyai, -2)
  3470. -2.338107410459767038489197
  3471. >>> airyaizero(1)
  3472. -2.338107410459767038489197
  3473. **Properties and relations**
  3474. Verifying the Airy differential equation::
  3475. >>> for z in [-3.4, 0, 2.5, 1+2j]:
  3476. ... chop(airyai(z,2) - z*airyai(z))
  3477. ...
  3478. 0.0
  3479. 0.0
  3480. 0.0
  3481. 0.0
  3482. The first few terms of the Taylor series expansion around `z = 0`
  3483. (every third term is zero)::
  3484. >>> nprint(taylor(airyai, 0, 5))
  3485. [0.355028, -0.258819, 0.0, 0.0591713, -0.0215683, 0.0]
  3486. The Airy functions satisfy the Wronskian relation
  3487. `\operatorname{Ai}(z) \operatorname{Bi}'(z) -
  3488. \operatorname{Ai}'(z) \operatorname{Bi}(z) = 1/\pi`::
  3489. >>> z = -0.5
  3490. >>> airyai(z)*airybi(z,1) - airyai(z,1)*airybi(z)
  3491. 0.3183098861837906715377675
  3492. >>> 1/pi
  3493. 0.3183098861837906715377675
  3494. The Airy functions can be expressed in terms of Bessel
  3495. functions of order `\pm 1/3`. For `\Re[z] \le 0`, we have::
  3496. >>> z = -3
  3497. >>> airyai(z)
  3498. -0.3788142936776580743472439
  3499. >>> y = 2*power(-z,'3/2')/3
  3500. >>> (sqrt(-z) * (besselj('1/3',y) + besselj('-1/3',y)))/3
  3501. -0.3788142936776580743472439
  3502. **Derivatives and integrals**
  3503. Derivatives of the Ai-function (directly and using :func:`~mpmath.diff`)::
  3504. >>> airyai(-3,1); diff(airyai,-3)
  3505. 0.3145837692165988136507873
  3506. 0.3145837692165988136507873
  3507. >>> airyai(-3,2); diff(airyai,-3,2)
  3508. 1.136442881032974223041732
  3509. 1.136442881032974223041732
  3510. >>> airyai(1000,1); diff(airyai,1000)
  3511. -2.943133917910336090459748e-9156
  3512. -2.943133917910336090459748e-9156
  3513. Several derivatives at `z = 0`::
  3514. >>> airyai(0,0); airyai(0,1); airyai(0,2)
  3515. 0.3550280538878172392600632
  3516. -0.2588194037928067984051836
  3517. 0.0
  3518. >>> airyai(0,3); airyai(0,4); airyai(0,5)
  3519. 0.3550280538878172392600632
  3520. -0.5176388075856135968103671
  3521. 0.0
  3522. >>> airyai(0,15); airyai(0,16); airyai(0,17)
  3523. 1292.30211615165475090663
  3524. -3188.655054727379756351861
  3525. 0.0
  3526. The integral of the Ai-function::
  3527. >>> airyai(3,-1); quad(airyai, [0,3])
  3528. 0.3299203760070217725002701
  3529. 0.3299203760070217725002701
  3530. >>> airyai(-10,-1); quad(airyai, [0,-10])
  3531. -0.765698403134212917425148
  3532. -0.765698403134212917425148
  3533. Integrals of high or fractional order::
  3534. >>> airyai(-2,0.5); differint(airyai,-2,0.5,0)
  3535. (0.0 + 0.2453596101351438273844725j)
  3536. (0.0 + 0.2453596101351438273844725j)
  3537. >>> airyai(-2,-4); differint(airyai,-2,-4,0)
  3538. 0.2939176441636809580339365
  3539. 0.2939176441636809580339365
  3540. >>> airyai(0,-1); airyai(0,-2); airyai(0,-3)
  3541. 0.0
  3542. 0.0
  3543. 0.0
  3544. Integrals of the Ai-function can be evaluated at limit points::
  3545. >>> airyai(-1000000,-1); airyai(-inf,-1)
  3546. -0.6666843728311539978751512
  3547. -0.6666666666666666666666667
  3548. >>> airyai(10,-1); airyai(+inf,-1)
  3549. 0.3333333332991690159427932
  3550. 0.3333333333333333333333333
  3551. >>> airyai(+inf,-2); airyai(+inf,-3)
  3552. +inf
  3553. +inf
  3554. >>> airyai(-1000000,-2); airyai(-inf,-2)
  3555. 666666.4078472650651209742
  3556. +inf
  3557. >>> airyai(-1000000,-3); airyai(-inf,-3)
  3558. -333333074513.7520264995733
  3559. -inf
  3560. **References**
  3561. 1. [DLMF]_ Chapter 9: Airy and Related Functions
  3562. 2. [WolframFunctions]_ section: Bessel-Type Functions
  3563. """
  3564. airybi = r"""
  3565. Computes the Airy function `\operatorname{Bi}(z)`, which is
  3566. the solution of the Airy differential equation `f''(z) - z f(z) = 0`
  3567. with initial conditions
  3568. .. math ::
  3569. \operatorname{Bi}(0) =
  3570. \frac{1}{3^{1/6}\Gamma\left(\frac{2}{3}\right)}
  3571. \operatorname{Bi}'(0) =
  3572. \frac{3^{1/6}}{\Gamma\left(\frac{1}{3}\right)}.
  3573. Like the Ai-function (see :func:`~mpmath.airyai`), the Bi-function
  3574. is oscillatory for `z < 0`, but it grows rather than decreases
  3575. for `z > 0`.
  3576. Optionally, as for :func:`~mpmath.airyai`, derivatives, integrals
  3577. and fractional derivatives can be computed with the *derivative*
  3578. parameter.
  3579. The Bi-function has infinitely many zeros along the negative
  3580. half-axis, as well as complex zeros, which can all be computed
  3581. with :func:`~mpmath.airybizero`.
  3582. **Plots**
  3583. .. literalinclude :: /plots/bi.py
  3584. .. image :: /plots/bi.png
  3585. .. literalinclude :: /plots/bi_c.py
  3586. .. image :: /plots/bi_c.png
  3587. **Basic examples**
  3588. Limits and values include::
  3589. >>> from mpmath import *
  3590. >>> mp.dps = 25; mp.pretty = True
  3591. >>> airybi(0); 1/(power(3,'1/6')*gamma('2/3'))
  3592. 0.6149266274460007351509224
  3593. 0.6149266274460007351509224
  3594. >>> airybi(1)
  3595. 1.207423594952871259436379
  3596. >>> airybi(-1)
  3597. 0.10399738949694461188869
  3598. >>> airybi(inf); airybi(-inf)
  3599. +inf
  3600. 0.0
  3601. Evaluation is supported for large magnitudes of the argument::
  3602. >>> airybi(-100)
  3603. 0.02427388768016013160566747
  3604. >>> airybi(100)
  3605. 6.041223996670201399005265e+288
  3606. >>> airybi(50+50j)
  3607. (-5.322076267321435669290334e+63 + 1.478450291165243789749427e+65j)
  3608. >>> airybi(-50+50j)
  3609. (-3.347525544923600321838281e+157 + 1.041242537363167632587245e+158j)
  3610. Huge arguments::
  3611. >>> airybi(10**10)
  3612. 1.369385787943539818688433e+289529654602165
  3613. >>> airybi(-10**10)
  3614. 0.001775656141692932747610973
  3615. >>> w = airybi(10**10*(1+j))
  3616. >>> w.real
  3617. -6.559955931096196875845858e+186339621747689
  3618. >>> w.imag
  3619. -6.822462726981357180929024e+186339621747690
  3620. The first real root of the Bi-function is::
  3621. >>> findroot(airybi, -1); airybizero(1)
  3622. -1.17371322270912792491998
  3623. -1.17371322270912792491998
  3624. **Properties and relations**
  3625. Verifying the Airy differential equation::
  3626. >>> for z in [-3.4, 0, 2.5, 1+2j]:
  3627. ... chop(airybi(z,2) - z*airybi(z))
  3628. ...
  3629. 0.0
  3630. 0.0
  3631. 0.0
  3632. 0.0
  3633. The first few terms of the Taylor series expansion around `z = 0`
  3634. (every third term is zero)::
  3635. >>> nprint(taylor(airybi, 0, 5))
  3636. [0.614927, 0.448288, 0.0, 0.102488, 0.0373574, 0.0]
  3637. The Airy functions can be expressed in terms of Bessel
  3638. functions of order `\pm 1/3`. For `\Re[z] \le 0`, we have::
  3639. >>> z = -3
  3640. >>> airybi(z)
  3641. -0.1982896263749265432206449
  3642. >>> p = 2*power(-z,'3/2')/3
  3643. >>> sqrt(-mpf(z)/3)*(besselj('-1/3',p) - besselj('1/3',p))
  3644. -0.1982896263749265432206449
  3645. **Derivatives and integrals**
  3646. Derivatives of the Bi-function (directly and using :func:`~mpmath.diff`)::
  3647. >>> airybi(-3,1); diff(airybi,-3)
  3648. -0.675611222685258537668032
  3649. -0.675611222685258537668032
  3650. >>> airybi(-3,2); diff(airybi,-3,2)
  3651. 0.5948688791247796296619346
  3652. 0.5948688791247796296619346
  3653. >>> airybi(1000,1); diff(airybi,1000)
  3654. 1.710055114624614989262335e+9156
  3655. 1.710055114624614989262335e+9156
  3656. Several derivatives at `z = 0`::
  3657. >>> airybi(0,0); airybi(0,1); airybi(0,2)
  3658. 0.6149266274460007351509224
  3659. 0.4482883573538263579148237
  3660. 0.0
  3661. >>> airybi(0,3); airybi(0,4); airybi(0,5)
  3662. 0.6149266274460007351509224
  3663. 0.8965767147076527158296474
  3664. 0.0
  3665. >>> airybi(0,15); airybi(0,16); airybi(0,17)
  3666. 2238.332923903442675949357
  3667. 5522.912562599140729510628
  3668. 0.0
  3669. The integral of the Bi-function::
  3670. >>> airybi(3,-1); quad(airybi, [0,3])
  3671. 10.06200303130620056316655
  3672. 10.06200303130620056316655
  3673. >>> airybi(-10,-1); quad(airybi, [0,-10])
  3674. -0.01504042480614002045135483
  3675. -0.01504042480614002045135483
  3676. Integrals of high or fractional order::
  3677. >>> airybi(-2,0.5); differint(airybi, -2, 0.5, 0)
  3678. (0.0 + 0.5019859055341699223453257j)
  3679. (0.0 + 0.5019859055341699223453257j)
  3680. >>> airybi(-2,-4); differint(airybi,-2,-4,0)
  3681. 0.2809314599922447252139092
  3682. 0.2809314599922447252139092
  3683. >>> airybi(0,-1); airybi(0,-2); airybi(0,-3)
  3684. 0.0
  3685. 0.0
  3686. 0.0
  3687. Integrals of the Bi-function can be evaluated at limit points::
  3688. >>> airybi(-1000000,-1); airybi(-inf,-1)
  3689. 0.000002191261128063434047966873
  3690. 0.0
  3691. >>> airybi(10,-1); airybi(+inf,-1)
  3692. 147809803.1074067161675853
  3693. +inf
  3694. >>> airybi(+inf,-2); airybi(+inf,-3)
  3695. +inf
  3696. +inf
  3697. >>> airybi(-1000000,-2); airybi(-inf,-2)
  3698. 0.4482883750599908479851085
  3699. 0.4482883573538263579148237
  3700. >>> gamma('2/3')*power(3,'2/3')/(2*pi)
  3701. 0.4482883573538263579148237
  3702. >>> airybi(-100000,-3); airybi(-inf,-3)
  3703. -44828.52827206932872493133
  3704. -inf
  3705. >>> airybi(-100000,-4); airybi(-inf,-4)
  3706. 2241411040.437759489540248
  3707. +inf
  3708. """
  3709. airyaizero = r"""
  3710. Gives the `k`-th zero of the Airy Ai-function,
  3711. i.e. the `k`-th number `a_k` ordered by magnitude for which
  3712. `\operatorname{Ai}(a_k) = 0`.
  3713. Optionally, with *derivative=1*, the corresponding
  3714. zero `a'_k` of the derivative function, i.e.
  3715. `\operatorname{Ai}'(a'_k) = 0`, is computed.
  3716. **Examples**
  3717. Some values of `a_k`::
  3718. >>> from mpmath import *
  3719. >>> mp.dps = 25; mp.pretty = True
  3720. >>> airyaizero(1)
  3721. -2.338107410459767038489197
  3722. >>> airyaizero(2)
  3723. -4.087949444130970616636989
  3724. >>> airyaizero(3)
  3725. -5.520559828095551059129856
  3726. >>> airyaizero(1000)
  3727. -281.0315196125215528353364
  3728. Some values of `a'_k`::
  3729. >>> airyaizero(1,1)
  3730. -1.018792971647471089017325
  3731. >>> airyaizero(2,1)
  3732. -3.248197582179836537875424
  3733. >>> airyaizero(3,1)
  3734. -4.820099211178735639400616
  3735. >>> airyaizero(1000,1)
  3736. -280.9378080358935070607097
  3737. Verification::
  3738. >>> chop(airyai(airyaizero(1)))
  3739. 0.0
  3740. >>> chop(airyai(airyaizero(1,1),1))
  3741. 0.0
  3742. """
  3743. airybizero = r"""
  3744. With *complex=False*, gives the `k`-th real zero of the Airy Bi-function,
  3745. i.e. the `k`-th number `b_k` ordered by magnitude for which
  3746. `\operatorname{Bi}(b_k) = 0`.
  3747. With *complex=True*, gives the `k`-th complex zero in the upper
  3748. half plane `\beta_k`. Also the conjugate `\overline{\beta_k}`
  3749. is a zero.
  3750. Optionally, with *derivative=1*, the corresponding
  3751. zero `b'_k` or `\beta'_k` of the derivative function, i.e.
  3752. `\operatorname{Bi}'(b'_k) = 0` or `\operatorname{Bi}'(\beta'_k) = 0`,
  3753. is computed.
  3754. **Examples**
  3755. Some values of `b_k`::
  3756. >>> from mpmath import *
  3757. >>> mp.dps = 25; mp.pretty = True
  3758. >>> airybizero(1)
  3759. -1.17371322270912792491998
  3760. >>> airybizero(2)
  3761. -3.271093302836352715680228
  3762. >>> airybizero(3)
  3763. -4.830737841662015932667709
  3764. >>> airybizero(1000)
  3765. -280.9378112034152401578834
  3766. Some values of `b_k`::
  3767. >>> airybizero(1,1)
  3768. -2.294439682614123246622459
  3769. >>> airybizero(2,1)
  3770. -4.073155089071828215552369
  3771. >>> airybizero(3,1)
  3772. -5.512395729663599496259593
  3773. >>> airybizero(1000,1)
  3774. -281.0315164471118527161362
  3775. Some values of `\beta_k`::
  3776. >>> airybizero(1,complex=True)
  3777. (0.9775448867316206859469927 + 2.141290706038744575749139j)
  3778. >>> airybizero(2,complex=True)
  3779. (1.896775013895336346627217 + 3.627291764358919410440499j)
  3780. >>> airybizero(3,complex=True)
  3781. (2.633157739354946595708019 + 4.855468179979844983174628j)
  3782. >>> airybizero(1000,complex=True)
  3783. (140.4978560578493018899793 + 243.3907724215792121244867j)
  3784. Some values of `\beta'_k`::
  3785. >>> airybizero(1,1,complex=True)
  3786. (0.2149470745374305676088329 + 1.100600143302797880647194j)
  3787. >>> airybizero(2,1,complex=True)
  3788. (1.458168309223507392028211 + 2.912249367458445419235083j)
  3789. >>> airybizero(3,1,complex=True)
  3790. (2.273760763013482299792362 + 4.254528549217097862167015j)
  3791. >>> airybizero(1000,1,complex=True)
  3792. (140.4509972835270559730423 + 243.3096175398562811896208j)
  3793. Verification::
  3794. >>> chop(airybi(airybizero(1)))
  3795. 0.0
  3796. >>> chop(airybi(airybizero(1,1),1))
  3797. 0.0
  3798. >>> u = airybizero(1,complex=True)
  3799. >>> chop(airybi(u))
  3800. 0.0
  3801. >>> chop(airybi(conj(u)))
  3802. 0.0
  3803. The complex zeros (in the upper and lower half-planes respectively)
  3804. asymptotically approach the rays `z = R \exp(\pm i \pi /3)`::
  3805. >>> arg(airybizero(1,complex=True))
  3806. 1.142532510286334022305364
  3807. >>> arg(airybizero(1000,complex=True))
  3808. 1.047271114786212061583917
  3809. >>> arg(airybizero(1000000,complex=True))
  3810. 1.047197624741816183341355
  3811. >>> pi/3
  3812. 1.047197551196597746154214
  3813. """
  3814. ellipk = r"""
  3815. Evaluates the complete elliptic integral of the first kind,
  3816. `K(m)`, defined by
  3817. .. math ::
  3818. K(m) = \int_0^{\pi/2} \frac{dt}{\sqrt{1-m \sin^2 t}} \, = \,
  3819. \frac{\pi}{2} \,_2F_1\left(\frac{1}{2}, \frac{1}{2}, 1, m\right).
  3820. Note that the argument is the parameter `m = k^2`,
  3821. not the modulus `k` which is sometimes used.
  3822. **Plots**
  3823. .. literalinclude :: /plots/ellipk.py
  3824. .. image :: /plots/ellipk.png
  3825. **Examples**
  3826. Values and limits include::
  3827. >>> from mpmath import *
  3828. >>> mp.dps = 25; mp.pretty = True
  3829. >>> ellipk(0)
  3830. 1.570796326794896619231322
  3831. >>> ellipk(inf)
  3832. (0.0 + 0.0j)
  3833. >>> ellipk(-inf)
  3834. 0.0
  3835. >>> ellipk(1)
  3836. +inf
  3837. >>> ellipk(-1)
  3838. 1.31102877714605990523242
  3839. >>> ellipk(2)
  3840. (1.31102877714605990523242 - 1.31102877714605990523242j)
  3841. Verifying the defining integral and hypergeometric
  3842. representation::
  3843. >>> ellipk(0.5)
  3844. 1.85407467730137191843385
  3845. >>> quad(lambda t: (1-0.5*sin(t)**2)**-0.5, [0, pi/2])
  3846. 1.85407467730137191843385
  3847. >>> pi/2*hyp2f1(0.5,0.5,1,0.5)
  3848. 1.85407467730137191843385
  3849. Evaluation is supported for arbitrary complex `m`::
  3850. >>> ellipk(3+4j)
  3851. (0.9111955638049650086562171 + 0.6313342832413452438845091j)
  3852. A definite integral::
  3853. >>> quad(ellipk, [0, 1])
  3854. 2.0
  3855. """
  3856. agm = r"""
  3857. ``agm(a, b)`` computes the arithmetic-geometric mean of `a` and
  3858. `b`, defined as the limit of the following iteration:
  3859. .. math ::
  3860. a_0 = a
  3861. b_0 = b
  3862. a_{n+1} = \frac{a_n+b_n}{2}
  3863. b_{n+1} = \sqrt{a_n b_n}
  3864. This function can be called with a single argument, computing
  3865. `\mathrm{agm}(a,1) = \mathrm{agm}(1,a)`.
  3866. **Examples**
  3867. It is a well-known theorem that the geometric mean of
  3868. two distinct positive numbers is less than the arithmetic
  3869. mean. It follows that the arithmetic-geometric mean lies
  3870. between the two means::
  3871. >>> from mpmath import *
  3872. >>> mp.dps = 15; mp.pretty = True
  3873. >>> a = mpf(3)
  3874. >>> b = mpf(4)
  3875. >>> sqrt(a*b)
  3876. 3.46410161513775
  3877. >>> agm(a,b)
  3878. 3.48202767635957
  3879. >>> (a+b)/2
  3880. 3.5
  3881. The arithmetic-geometric mean is scale-invariant::
  3882. >>> agm(10*e, 10*pi)
  3883. 29.261085515723
  3884. >>> 10*agm(e, pi)
  3885. 29.261085515723
  3886. As an order-of-magnitude estimate, `\mathrm{agm}(1,x) \approx x`
  3887. for large `x`::
  3888. >>> agm(10**10)
  3889. 643448704.760133
  3890. >>> agm(10**50)
  3891. 1.34814309345871e+48
  3892. For tiny `x`, `\mathrm{agm}(1,x) \approx -\pi/(2 \log(x/4))`::
  3893. >>> agm('0.01')
  3894. 0.262166887202249
  3895. >>> -pi/2/log('0.0025')
  3896. 0.262172347753122
  3897. The arithmetic-geometric mean can also be computed for complex
  3898. numbers::
  3899. >>> agm(3, 2+j)
  3900. (2.51055133276184 + 0.547394054060638j)
  3901. The AGM iteration converges very quickly (each step doubles
  3902. the number of correct digits), so :func:`~mpmath.agm` supports efficient
  3903. high-precision evaluation::
  3904. >>> mp.dps = 10000
  3905. >>> a = agm(1,2)
  3906. >>> str(a)[-10:]
  3907. '1679581912'
  3908. **Mathematical relations**
  3909. The arithmetic-geometric mean may be used to evaluate the
  3910. following two parametric definite integrals:
  3911. .. math ::
  3912. I_1 = \int_0^{\infty}
  3913. \frac{1}{\sqrt{(x^2+a^2)(x^2+b^2)}} \,dx
  3914. I_2 = \int_0^{\pi/2}
  3915. \frac{1}{\sqrt{a^2 \cos^2(x) + b^2 \sin^2(x)}} \,dx
  3916. We have::
  3917. >>> mp.dps = 15
  3918. >>> a = 3
  3919. >>> b = 4
  3920. >>> f1 = lambda x: ((x**2+a**2)*(x**2+b**2))**-0.5
  3921. >>> f2 = lambda x: ((a*cos(x))**2 + (b*sin(x))**2)**-0.5
  3922. >>> quad(f1, [0, inf])
  3923. 0.451115405388492
  3924. >>> quad(f2, [0, pi/2])
  3925. 0.451115405388492
  3926. >>> pi/(2*agm(a,b))
  3927. 0.451115405388492
  3928. A formula for `\Gamma(1/4)`::
  3929. >>> gamma(0.25)
  3930. 3.62560990822191
  3931. >>> sqrt(2*sqrt(2*pi**3)/agm(1,sqrt(2)))
  3932. 3.62560990822191
  3933. **Possible issues**
  3934. The branch cut chosen for complex `a` and `b` is somewhat
  3935. arbitrary.
  3936. """
  3937. gegenbauer = r"""
  3938. Evaluates the Gegenbauer polynomial, or ultraspherical polynomial,
  3939. .. math ::
  3940. C_n^{(a)}(z) = {n+2a-1 \choose n} \,_2F_1\left(-n, n+2a;
  3941. a+\frac{1}{2}; \frac{1}{2}(1-z)\right).
  3942. When `n` is a nonnegative integer, this formula gives a polynomial
  3943. in `z` of degree `n`, but all parameters are permitted to be
  3944. complex numbers. With `a = 1/2`, the Gegenbauer polynomial
  3945. reduces to a Legendre polynomial.
  3946. **Examples**
  3947. Evaluation for arbitrary arguments::
  3948. >>> from mpmath import *
  3949. >>> mp.dps = 25; mp.pretty = True
  3950. >>> gegenbauer(3, 0.5, -10)
  3951. -2485.0
  3952. >>> gegenbauer(1000, 10, 100)
  3953. 3.012757178975667428359374e+2322
  3954. >>> gegenbauer(2+3j, -0.75, -1000j)
  3955. (-5038991.358609026523401901 + 9414549.285447104177860806j)
  3956. Evaluation at negative integer orders::
  3957. >>> gegenbauer(-4, 2, 1.75)
  3958. -1.0
  3959. >>> gegenbauer(-4, 3, 1.75)
  3960. 0.0
  3961. >>> gegenbauer(-4, 2j, 1.75)
  3962. 0.0
  3963. >>> gegenbauer(-7, 0.5, 3)
  3964. 8989.0
  3965. The Gegenbauer polynomials solve the differential equation::
  3966. >>> n, a = 4.5, 1+2j
  3967. >>> f = lambda z: gegenbauer(n, a, z)
  3968. >>> for z in [0, 0.75, -0.5j]:
  3969. ... chop((1-z**2)*diff(f,z,2) - (2*a+1)*z*diff(f,z) + n*(n+2*a)*f(z))
  3970. ...
  3971. 0.0
  3972. 0.0
  3973. 0.0
  3974. The Gegenbauer polynomials have generating function
  3975. `(1-2zt+t^2)^{-a}`::
  3976. >>> a, z = 2.5, 1
  3977. >>> taylor(lambda t: (1-2*z*t+t**2)**(-a), 0, 3)
  3978. [1.0, 5.0, 15.0, 35.0]
  3979. >>> [gegenbauer(n,a,z) for n in range(4)]
  3980. [1.0, 5.0, 15.0, 35.0]
  3981. The Gegenbauer polynomials are orthogonal on `[-1, 1]` with respect
  3982. to the weight `(1-z^2)^{a-\frac{1}{2}}`::
  3983. >>> a, n, m = 2.5, 4, 5
  3984. >>> Cn = lambda z: gegenbauer(n, a, z, zeroprec=1000)
  3985. >>> Cm = lambda z: gegenbauer(m, a, z, zeroprec=1000)
  3986. >>> chop(quad(lambda z: Cn(z)*Cm(z)*(1-z**2)*(a-0.5), [-1, 1]))
  3987. 0.0
  3988. """
  3989. laguerre = r"""
  3990. Gives the generalized (associated) Laguerre polynomial, defined by
  3991. .. math ::
  3992. L_n^a(z) = \frac{\Gamma(n+b+1)}{\Gamma(b+1) \Gamma(n+1)}
  3993. \,_1F_1(-n, a+1, z).
  3994. With `a = 0` and `n` a nonnegative integer, this reduces to an ordinary
  3995. Laguerre polynomial, the sequence of which begins
  3996. `L_0(z) = 1, L_1(z) = 1-z, L_2(z) = z^2-2z+1, \ldots`.
  3997. The Laguerre polynomials are orthogonal with respect to the weight
  3998. `z^a e^{-z}` on `[0, \infty)`.
  3999. **Plots**
  4000. .. literalinclude :: /plots/laguerre.py
  4001. .. image :: /plots/laguerre.png
  4002. **Examples**
  4003. Evaluation for arbitrary arguments::
  4004. >>> from mpmath import *
  4005. >>> mp.dps = 25; mp.pretty = True
  4006. >>> laguerre(5, 0, 0.25)
  4007. 0.03726399739583333333333333
  4008. >>> laguerre(1+j, 0.5, 2+3j)
  4009. (4.474921610704496808379097 - 11.02058050372068958069241j)
  4010. >>> laguerre(2, 0, 10000)
  4011. 49980001.0
  4012. >>> laguerre(2.5, 0, 10000)
  4013. -9.327764910194842158583189e+4328
  4014. The first few Laguerre polynomials, normalized to have integer
  4015. coefficients::
  4016. >>> for n in range(7):
  4017. ... chop(taylor(lambda z: fac(n)*laguerre(n, 0, z), 0, n))
  4018. ...
  4019. [1.0]
  4020. [1.0, -1.0]
  4021. [2.0, -4.0, 1.0]
  4022. [6.0, -18.0, 9.0, -1.0]
  4023. [24.0, -96.0, 72.0, -16.0, 1.0]
  4024. [120.0, -600.0, 600.0, -200.0, 25.0, -1.0]
  4025. [720.0, -4320.0, 5400.0, -2400.0, 450.0, -36.0, 1.0]
  4026. Verifying orthogonality::
  4027. >>> Lm = lambda t: laguerre(m,a,t)
  4028. >>> Ln = lambda t: laguerre(n,a,t)
  4029. >>> a, n, m = 2.5, 2, 3
  4030. >>> chop(quad(lambda t: exp(-t)*t**a*Lm(t)*Ln(t), [0,inf]))
  4031. 0.0
  4032. """
  4033. hermite = r"""
  4034. Evaluates the Hermite polynomial `H_n(z)`, which may be defined using
  4035. the recurrence
  4036. .. math ::
  4037. H_0(z) = 1
  4038. H_1(z) = 2z
  4039. H_{n+1} = 2z H_n(z) - 2n H_{n-1}(z).
  4040. The Hermite polynomials are orthogonal on `(-\infty, \infty)` with
  4041. respect to the weight `e^{-z^2}`. More generally, allowing arbitrary complex
  4042. values of `n`, the Hermite function `H_n(z)` is defined as
  4043. .. math ::
  4044. H_n(z) = (2z)^n \,_2F_0\left(-\frac{n}{2}, \frac{1-n}{2},
  4045. -\frac{1}{z^2}\right)
  4046. for `\Re{z} > 0`, or generally
  4047. .. math ::
  4048. H_n(z) = 2^n \sqrt{\pi} \left(
  4049. \frac{1}{\Gamma\left(\frac{1-n}{2}\right)}
  4050. \,_1F_1\left(-\frac{n}{2}, \frac{1}{2}, z^2\right) -
  4051. \frac{2z}{\Gamma\left(-\frac{n}{2}\right)}
  4052. \,_1F_1\left(\frac{1-n}{2}, \frac{3}{2}, z^2\right)
  4053. \right).
  4054. **Plots**
  4055. .. literalinclude :: /plots/hermite.py
  4056. .. image :: /plots/hermite.png
  4057. **Examples**
  4058. Evaluation for arbitrary arguments::
  4059. >>> from mpmath import *
  4060. >>> mp.dps = 25; mp.pretty = True
  4061. >>> hermite(0, 10)
  4062. 1.0
  4063. >>> hermite(1, 10); hermite(2, 10)
  4064. 20.0
  4065. 398.0
  4066. >>> hermite(10000, 2)
  4067. 4.950440066552087387515653e+19334
  4068. >>> hermite(3, -10**8)
  4069. -7999999999999998800000000.0
  4070. >>> hermite(-3, -10**8)
  4071. 1.675159751729877682920301e+4342944819032534
  4072. >>> hermite(2+3j, -1+2j)
  4073. (-0.07652130602993513389421901 - 0.1084662449961914580276007j)
  4074. Coefficients of the first few Hermite polynomials are::
  4075. >>> for n in range(7):
  4076. ... chop(taylor(lambda z: hermite(n, z), 0, n))
  4077. ...
  4078. [1.0]
  4079. [0.0, 2.0]
  4080. [-2.0, 0.0, 4.0]
  4081. [0.0, -12.0, 0.0, 8.0]
  4082. [12.0, 0.0, -48.0, 0.0, 16.0]
  4083. [0.0, 120.0, 0.0, -160.0, 0.0, 32.0]
  4084. [-120.0, 0.0, 720.0, 0.0, -480.0, 0.0, 64.0]
  4085. Values at `z = 0`::
  4086. >>> for n in range(-5, 9):
  4087. ... hermite(n, 0)
  4088. ...
  4089. 0.02769459142039868792653387
  4090. 0.08333333333333333333333333
  4091. 0.2215567313631895034122709
  4092. 0.5
  4093. 0.8862269254527580136490837
  4094. 1.0
  4095. 0.0
  4096. -2.0
  4097. 0.0
  4098. 12.0
  4099. 0.0
  4100. -120.0
  4101. 0.0
  4102. 1680.0
  4103. Hermite functions satisfy the differential equation::
  4104. >>> n = 4
  4105. >>> f = lambda z: hermite(n, z)
  4106. >>> z = 1.5
  4107. >>> chop(diff(f,z,2) - 2*z*diff(f,z) + 2*n*f(z))
  4108. 0.0
  4109. Verifying orthogonality::
  4110. >>> chop(quad(lambda t: hermite(2,t)*hermite(4,t)*exp(-t**2), [-inf,inf]))
  4111. 0.0
  4112. """
  4113. jacobi = r"""
  4114. ``jacobi(n, a, b, x)`` evaluates the Jacobi polynomial
  4115. `P_n^{(a,b)}(x)`. The Jacobi polynomials are a special
  4116. case of the hypergeometric function `\,_2F_1` given by:
  4117. .. math ::
  4118. P_n^{(a,b)}(x) = {n+a \choose n}
  4119. \,_2F_1\left(-n,1+a+b+n,a+1,\frac{1-x}{2}\right).
  4120. Note that this definition generalizes to nonintegral values
  4121. of `n`. When `n` is an integer, the hypergeometric series
  4122. terminates after a finite number of terms, giving
  4123. a polynomial in `x`.
  4124. **Evaluation of Jacobi polynomials**
  4125. A special evaluation is `P_n^{(a,b)}(1) = {n+a \choose n}`::
  4126. >>> from mpmath import *
  4127. >>> mp.dps = 15; mp.pretty = True
  4128. >>> jacobi(4, 0.5, 0.25, 1)
  4129. 2.4609375
  4130. >>> binomial(4+0.5, 4)
  4131. 2.4609375
  4132. A Jacobi polynomial of degree `n` is equal to its
  4133. Taylor polynomial of degree `n`. The explicit
  4134. coefficients of Jacobi polynomials can therefore
  4135. be recovered easily using :func:`~mpmath.taylor`::
  4136. >>> for n in range(5):
  4137. ... nprint(taylor(lambda x: jacobi(n,1,2,x), 0, n))
  4138. ...
  4139. [1.0]
  4140. [-0.5, 2.5]
  4141. [-0.75, -1.5, 5.25]
  4142. [0.5, -3.5, -3.5, 10.5]
  4143. [0.625, 2.5, -11.25, -7.5, 20.625]
  4144. For nonintegral `n`, the Jacobi "polynomial" is no longer
  4145. a polynomial::
  4146. >>> nprint(taylor(lambda x: jacobi(0.5,1,2,x), 0, 4))
  4147. [0.309983, 1.84119, -1.26933, 1.26699, -1.34808]
  4148. **Orthogonality**
  4149. The Jacobi polynomials are orthogonal on the interval
  4150. `[-1, 1]` with respect to the weight function
  4151. `w(x) = (1-x)^a (1+x)^b`. That is,
  4152. `w(x) P_n^{(a,b)}(x) P_m^{(a,b)}(x)` integrates to
  4153. zero if `m \ne n` and to a nonzero number if `m = n`.
  4154. The orthogonality is easy to verify using numerical
  4155. quadrature::
  4156. >>> P = jacobi
  4157. >>> f = lambda x: (1-x)**a * (1+x)**b * P(m,a,b,x) * P(n,a,b,x)
  4158. >>> a = 2
  4159. >>> b = 3
  4160. >>> m, n = 3, 4
  4161. >>> chop(quad(f, [-1, 1]), 1)
  4162. 0.0
  4163. >>> m, n = 4, 4
  4164. >>> quad(f, [-1, 1])
  4165. 1.9047619047619
  4166. **Differential equation**
  4167. The Jacobi polynomials are solutions of the differential
  4168. equation
  4169. .. math ::
  4170. (1-x^2) y'' + (b-a-(a+b+2)x) y' + n (n+a+b+1) y = 0.
  4171. We can verify that :func:`~mpmath.jacobi` approximately satisfies
  4172. this equation::
  4173. >>> from mpmath import *
  4174. >>> mp.dps = 15
  4175. >>> a = 2.5
  4176. >>> b = 4
  4177. >>> n = 3
  4178. >>> y = lambda x: jacobi(n,a,b,x)
  4179. >>> x = pi
  4180. >>> A0 = n*(n+a+b+1)*y(x)
  4181. >>> A1 = (b-a-(a+b+2)*x)*diff(y,x)
  4182. >>> A2 = (1-x**2)*diff(y,x,2)
  4183. >>> nprint(A2 + A1 + A0, 1)
  4184. 4.0e-12
  4185. The difference of order `10^{-12}` is as close to zero as
  4186. it could be at 15-digit working precision, since the terms
  4187. are large::
  4188. >>> A0, A1, A2
  4189. (26560.2328981879, -21503.7641037294, -5056.46879445852)
  4190. """
  4191. legendre = r"""
  4192. ``legendre(n, x)`` evaluates the Legendre polynomial `P_n(x)`.
  4193. The Legendre polynomials are given by the formula
  4194. .. math ::
  4195. P_n(x) = \frac{1}{2^n n!} \frac{d^n}{dx^n} (x^2 -1)^n.
  4196. Alternatively, they can be computed recursively using
  4197. .. math ::
  4198. P_0(x) = 1
  4199. P_1(x) = x
  4200. (n+1) P_{n+1}(x) = (2n+1) x P_n(x) - n P_{n-1}(x).
  4201. A third definition is in terms of the hypergeometric function
  4202. `\,_2F_1`, whereby they can be generalized to arbitrary `n`:
  4203. .. math ::
  4204. P_n(x) = \,_2F_1\left(-n, n+1, 1, \frac{1-x}{2}\right)
  4205. **Plots**
  4206. .. literalinclude :: /plots/legendre.py
  4207. .. image :: /plots/legendre.png
  4208. **Basic evaluation**
  4209. The Legendre polynomials assume fixed values at the points
  4210. `x = -1` and `x = 1`::
  4211. >>> from mpmath import *
  4212. >>> mp.dps = 15; mp.pretty = True
  4213. >>> nprint([legendre(n, 1) for n in range(6)])
  4214. [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
  4215. >>> nprint([legendre(n, -1) for n in range(6)])
  4216. [1.0, -1.0, 1.0, -1.0, 1.0, -1.0]
  4217. The coefficients of Legendre polynomials can be recovered
  4218. using degree-`n` Taylor expansion::
  4219. >>> for n in range(5):
  4220. ... nprint(chop(taylor(lambda x: legendre(n, x), 0, n)))
  4221. ...
  4222. [1.0]
  4223. [0.0, 1.0]
  4224. [-0.5, 0.0, 1.5]
  4225. [0.0, -1.5, 0.0, 2.5]
  4226. [0.375, 0.0, -3.75, 0.0, 4.375]
  4227. The roots of Legendre polynomials are located symmetrically
  4228. on the interval `[-1, 1]`::
  4229. >>> for n in range(5):
  4230. ... nprint(polyroots(taylor(lambda x: legendre(n, x), 0, n)[::-1]))
  4231. ...
  4232. []
  4233. [0.0]
  4234. [-0.57735, 0.57735]
  4235. [-0.774597, 0.0, 0.774597]
  4236. [-0.861136, -0.339981, 0.339981, 0.861136]
  4237. An example of an evaluation for arbitrary `n`::
  4238. >>> legendre(0.75, 2+4j)
  4239. (1.94952805264875 + 2.1071073099422j)
  4240. **Orthogonality**
  4241. The Legendre polynomials are orthogonal on `[-1, 1]` with respect
  4242. to the trivial weight `w(x) = 1`. That is, `P_m(x) P_n(x)`
  4243. integrates to zero if `m \ne n` and to `2/(2n+1)` if `m = n`::
  4244. >>> m, n = 3, 4
  4245. >>> quad(lambda x: legendre(m,x)*legendre(n,x), [-1, 1])
  4246. 0.0
  4247. >>> m, n = 4, 4
  4248. >>> quad(lambda x: legendre(m,x)*legendre(n,x), [-1, 1])
  4249. 0.222222222222222
  4250. **Differential equation**
  4251. The Legendre polynomials satisfy the differential equation
  4252. .. math ::
  4253. ((1-x^2) y')' + n(n+1) y' = 0.
  4254. We can verify this numerically::
  4255. >>> n = 3.6
  4256. >>> x = 0.73
  4257. >>> P = legendre
  4258. >>> A = diff(lambda t: (1-t**2)*diff(lambda u: P(n,u), t), x)
  4259. >>> B = n*(n+1)*P(n,x)
  4260. >>> nprint(A+B,1)
  4261. 9.0e-16
  4262. """
  4263. legenp = r"""
  4264. Calculates the (associated) Legendre function of the first kind of
  4265. degree *n* and order *m*, `P_n^m(z)`. Taking `m = 0` gives the ordinary
  4266. Legendre function of the first kind, `P_n(z)`. The parameters may be
  4267. complex numbers.
  4268. In terms of the Gauss hypergeometric function, the (associated) Legendre
  4269. function is defined as
  4270. .. math ::
  4271. P_n^m(z) = \frac{1}{\Gamma(1-m)} \frac{(1+z)^{m/2}}{(1-z)^{m/2}}
  4272. \,_2F_1\left(-n, n+1, 1-m, \frac{1-z}{2}\right).
  4273. With *type=3* instead of *type=2*, the alternative
  4274. definition
  4275. .. math ::
  4276. \hat{P}_n^m(z) = \frac{1}{\Gamma(1-m)} \frac{(z+1)^{m/2}}{(z-1)^{m/2}}
  4277. \,_2F_1\left(-n, n+1, 1-m, \frac{1-z}{2}\right).
  4278. is used. These functions correspond respectively to ``LegendreP[n,m,2,z]``
  4279. and ``LegendreP[n,m,3,z]`` in Mathematica.
  4280. The general solution of the (associated) Legendre differential equation
  4281. .. math ::
  4282. (1-z^2) f''(z) - 2zf'(z) + \left(n(n+1)-\frac{m^2}{1-z^2}\right)f(z) = 0
  4283. is given by `C_1 P_n^m(z) + C_2 Q_n^m(z)` for arbitrary constants
  4284. `C_1`, `C_2`, where `Q_n^m(z)` is a Legendre function of the
  4285. second kind as implemented by :func:`~mpmath.legenq`.
  4286. **Examples**
  4287. Evaluation for arbitrary parameters and arguments::
  4288. >>> from mpmath import *
  4289. >>> mp.dps = 25; mp.pretty = True
  4290. >>> legenp(2, 0, 10); legendre(2, 10)
  4291. 149.5
  4292. 149.5
  4293. >>> legenp(-2, 0.5, 2.5)
  4294. (1.972260393822275434196053 - 1.972260393822275434196053j)
  4295. >>> legenp(2+3j, 1-j, -0.5+4j)
  4296. (-3.335677248386698208736542 - 5.663270217461022307645625j)
  4297. >>> chop(legenp(3, 2, -1.5, type=2))
  4298. 28.125
  4299. >>> chop(legenp(3, 2, -1.5, type=3))
  4300. -28.125
  4301. Verifying the associated Legendre differential equation::
  4302. >>> n, m = 2, -0.5
  4303. >>> C1, C2 = 1, -3
  4304. >>> f = lambda z: C1*legenp(n,m,z) + C2*legenq(n,m,z)
  4305. >>> deq = lambda z: (1-z**2)*diff(f,z,2) - 2*z*diff(f,z) + \
  4306. ... (n*(n+1)-m**2/(1-z**2))*f(z)
  4307. >>> for z in [0, 2, -1.5, 0.5+2j]:
  4308. ... chop(deq(mpmathify(z)))
  4309. ...
  4310. 0.0
  4311. 0.0
  4312. 0.0
  4313. 0.0
  4314. """
  4315. legenq = r"""
  4316. Calculates the (associated) Legendre function of the second kind of
  4317. degree *n* and order *m*, `Q_n^m(z)`. Taking `m = 0` gives the ordinary
  4318. Legendre function of the second kind, `Q_n(z)`. The parameters may be
  4319. complex numbers.
  4320. The Legendre functions of the second kind give a second set of
  4321. solutions to the (associated) Legendre differential equation.
  4322. (See :func:`~mpmath.legenp`.)
  4323. Unlike the Legendre functions of the first kind, they are not
  4324. polynomials of `z` for integer `n`, `m` but rational or logarithmic
  4325. functions with poles at `z = \pm 1`.
  4326. There are various ways to define Legendre functions of
  4327. the second kind, giving rise to different complex structure.
  4328. A version can be selected using the *type* keyword argument.
  4329. The *type=2* and *type=3* functions are given respectively by
  4330. .. math ::
  4331. Q_n^m(z) = \frac{\pi}{2 \sin(\pi m)}
  4332. \left( \cos(\pi m) P_n^m(z) -
  4333. \frac{\Gamma(1+m+n)}{\Gamma(1-m+n)} P_n^{-m}(z)\right)
  4334. \hat{Q}_n^m(z) = \frac{\pi}{2 \sin(\pi m)} e^{\pi i m}
  4335. \left( \hat{P}_n^m(z) -
  4336. \frac{\Gamma(1+m+n)}{\Gamma(1-m+n)} \hat{P}_n^{-m}(z)\right)
  4337. where `P` and `\hat{P}` are the *type=2* and *type=3* Legendre functions
  4338. of the first kind. The formulas above should be understood as limits
  4339. when `m` is an integer.
  4340. These functions correspond to ``LegendreQ[n,m,2,z]`` (or ``LegendreQ[n,m,z]``)
  4341. and ``LegendreQ[n,m,3,z]`` in Mathematica. The *type=3* function
  4342. is essentially the same as the function defined in
  4343. Abramowitz & Stegun (eq. 8.1.3) but with `(z+1)^{m/2}(z-1)^{m/2}` instead
  4344. of `(z^2-1)^{m/2}`, giving slightly different branches.
  4345. **Examples**
  4346. Evaluation for arbitrary parameters and arguments::
  4347. >>> from mpmath import *
  4348. >>> mp.dps = 25; mp.pretty = True
  4349. >>> legenq(2, 0, 0.5)
  4350. -0.8186632680417568557122028
  4351. >>> legenq(-1.5, -2, 2.5)
  4352. (0.6655964618250228714288277 + 0.3937692045497259717762649j)
  4353. >>> legenq(2-j, 3+4j, -6+5j)
  4354. (-10001.95256487468541686564 - 6011.691337610097577791134j)
  4355. Different versions of the function::
  4356. >>> legenq(2, 1, 0.5)
  4357. 0.7298060598018049369381857
  4358. >>> legenq(2, 1, 1.5)
  4359. (-7.902916572420817192300921 + 0.1998650072605976600724502j)
  4360. >>> legenq(2, 1, 0.5, type=3)
  4361. (2.040524284763495081918338 - 0.7298060598018049369381857j)
  4362. >>> chop(legenq(2, 1, 1.5, type=3))
  4363. -0.1998650072605976600724502
  4364. """
  4365. chebyt = r"""
  4366. ``chebyt(n, x)`` evaluates the Chebyshev polynomial of the first
  4367. kind `T_n(x)`, defined by the identity
  4368. .. math ::
  4369. T_n(\cos x) = \cos(n x).
  4370. The Chebyshev polynomials of the first kind are a special
  4371. case of the Jacobi polynomials, and by extension of the
  4372. hypergeometric function `\,_2F_1`. They can thus also be
  4373. evaluated for nonintegral `n`.
  4374. **Plots**
  4375. .. literalinclude :: /plots/chebyt.py
  4376. .. image :: /plots/chebyt.png
  4377. **Basic evaluation**
  4378. The coefficients of the `n`-th polynomial can be recovered
  4379. using using degree-`n` Taylor expansion::
  4380. >>> from mpmath import *
  4381. >>> mp.dps = 15; mp.pretty = True
  4382. >>> for n in range(5):
  4383. ... nprint(chop(taylor(lambda x: chebyt(n, x), 0, n)))
  4384. ...
  4385. [1.0]
  4386. [0.0, 1.0]
  4387. [-1.0, 0.0, 2.0]
  4388. [0.0, -3.0, 0.0, 4.0]
  4389. [1.0, 0.0, -8.0, 0.0, 8.0]
  4390. **Orthogonality**
  4391. The Chebyshev polynomials of the first kind are orthogonal
  4392. on the interval `[-1, 1]` with respect to the weight
  4393. function `w(x) = 1/\sqrt{1-x^2}`::
  4394. >>> f = lambda x: chebyt(m,x)*chebyt(n,x)/sqrt(1-x**2)
  4395. >>> m, n = 3, 4
  4396. >>> nprint(quad(f, [-1, 1]),1)
  4397. 0.0
  4398. >>> m, n = 4, 4
  4399. >>> quad(f, [-1, 1])
  4400. 1.57079632596448
  4401. """
  4402. chebyu = r"""
  4403. ``chebyu(n, x)`` evaluates the Chebyshev polynomial of the second
  4404. kind `U_n(x)`, defined by the identity
  4405. .. math ::
  4406. U_n(\cos x) = \frac{\sin((n+1)x)}{\sin(x)}.
  4407. The Chebyshev polynomials of the second kind are a special
  4408. case of the Jacobi polynomials, and by extension of the
  4409. hypergeometric function `\,_2F_1`. They can thus also be
  4410. evaluated for nonintegral `n`.
  4411. **Plots**
  4412. .. literalinclude :: /plots/chebyu.py
  4413. .. image :: /plots/chebyu.png
  4414. **Basic evaluation**
  4415. The coefficients of the `n`-th polynomial can be recovered
  4416. using using degree-`n` Taylor expansion::
  4417. >>> from mpmath import *
  4418. >>> mp.dps = 15; mp.pretty = True
  4419. >>> for n in range(5):
  4420. ... nprint(chop(taylor(lambda x: chebyu(n, x), 0, n)))
  4421. ...
  4422. [1.0]
  4423. [0.0, 2.0]
  4424. [-1.0, 0.0, 4.0]
  4425. [0.0, -4.0, 0.0, 8.0]
  4426. [1.0, 0.0, -12.0, 0.0, 16.0]
  4427. **Orthogonality**
  4428. The Chebyshev polynomials of the second kind are orthogonal
  4429. on the interval `[-1, 1]` with respect to the weight
  4430. function `w(x) = \sqrt{1-x^2}`::
  4431. >>> f = lambda x: chebyu(m,x)*chebyu(n,x)*sqrt(1-x**2)
  4432. >>> m, n = 3, 4
  4433. >>> quad(f, [-1, 1])
  4434. 0.0
  4435. >>> m, n = 4, 4
  4436. >>> quad(f, [-1, 1])
  4437. 1.5707963267949
  4438. """
  4439. besselj = r"""
  4440. ``besselj(n, x, derivative=0)`` gives the Bessel function of the first kind
  4441. `J_n(x)`. Bessel functions of the first kind are defined as
  4442. solutions of the differential equation
  4443. .. math ::
  4444. x^2 y'' + x y' + (x^2 - n^2) y = 0
  4445. which appears, among other things, when solving the radial
  4446. part of Laplace's equation in cylindrical coordinates. This
  4447. equation has two solutions for given `n`, where the
  4448. `J_n`-function is the solution that is nonsingular at `x = 0`.
  4449. For positive integer `n`, `J_n(x)` behaves roughly like a sine
  4450. (odd `n`) or cosine (even `n`) multiplied by a magnitude factor
  4451. that decays slowly as `x \to \pm\infty`.
  4452. Generally, `J_n` is a special case of the hypergeometric
  4453. function `\,_0F_1`:
  4454. .. math ::
  4455. J_n(x) = \frac{x^n}{2^n \Gamma(n+1)}
  4456. \,_0F_1\left(n+1,-\frac{x^2}{4}\right)
  4457. With *derivative* = `m \ne 0`, the `m`-th derivative
  4458. .. math ::
  4459. \frac{d^m}{dx^m} J_n(x)
  4460. is computed.
  4461. **Plots**
  4462. .. literalinclude :: /plots/besselj.py
  4463. .. image :: /plots/besselj.png
  4464. .. literalinclude :: /plots/besselj_c.py
  4465. .. image :: /plots/besselj_c.png
  4466. **Examples**
  4467. Evaluation is supported for arbitrary arguments, and at
  4468. arbitrary precision::
  4469. >>> from mpmath import *
  4470. >>> mp.dps = 15; mp.pretty = True
  4471. >>> besselj(2, 1000)
  4472. -0.024777229528606
  4473. >>> besselj(4, 0.75)
  4474. 0.000801070086542314
  4475. >>> besselj(2, 1000j)
  4476. (-2.48071721019185e+432 + 6.41567059811949e-437j)
  4477. >>> mp.dps = 25
  4478. >>> besselj(0.75j, 3+4j)
  4479. (-2.778118364828153309919653 - 1.5863603889018621585533j)
  4480. >>> mp.dps = 50
  4481. >>> besselj(1, pi)
  4482. 0.28461534317975275734531059968613140570981118184947
  4483. Arguments may be large::
  4484. >>> mp.dps = 25
  4485. >>> besselj(0, 10000)
  4486. -0.007096160353388801477265164
  4487. >>> besselj(0, 10**10)
  4488. 0.000002175591750246891726859055
  4489. >>> besselj(2, 10**100)
  4490. 7.337048736538615712436929e-51
  4491. >>> besselj(2, 10**5*j)
  4492. (-3.540725411970948860173735e+43426 + 4.4949812409615803110051e-43433j)
  4493. The Bessel functions of the first kind satisfy simple
  4494. symmetries around `x = 0`::
  4495. >>> mp.dps = 15
  4496. >>> nprint([besselj(n,0) for n in range(5)])
  4497. [1.0, 0.0, 0.0, 0.0, 0.0]
  4498. >>> nprint([besselj(n,pi) for n in range(5)])
  4499. [-0.304242, 0.284615, 0.485434, 0.333458, 0.151425]
  4500. >>> nprint([besselj(n,-pi) for n in range(5)])
  4501. [-0.304242, -0.284615, 0.485434, -0.333458, 0.151425]
  4502. Roots of Bessel functions are often used::
  4503. >>> nprint([findroot(j0, k) for k in [2, 5, 8, 11, 14]])
  4504. [2.40483, 5.52008, 8.65373, 11.7915, 14.9309]
  4505. >>> nprint([findroot(j1, k) for k in [3, 7, 10, 13, 16]])
  4506. [3.83171, 7.01559, 10.1735, 13.3237, 16.4706]
  4507. The roots are not periodic, but the distance between successive
  4508. roots asymptotically approaches `2 \pi`. Bessel functions of
  4509. the first kind have the following normalization::
  4510. >>> quadosc(j0, [0, inf], period=2*pi)
  4511. 1.0
  4512. >>> quadosc(j1, [0, inf], period=2*pi)
  4513. 1.0
  4514. For `n = 1/2` or `n = -1/2`, the Bessel function reduces to a
  4515. trigonometric function::
  4516. >>> x = 10
  4517. >>> besselj(0.5, x), sqrt(2/(pi*x))*sin(x)
  4518. (-0.13726373575505, -0.13726373575505)
  4519. >>> besselj(-0.5, x), sqrt(2/(pi*x))*cos(x)
  4520. (-0.211708866331398, -0.211708866331398)
  4521. Derivatives of any order can be computed (negative orders
  4522. correspond to integration)::
  4523. >>> mp.dps = 25
  4524. >>> besselj(0, 7.5, 1)
  4525. -0.1352484275797055051822405
  4526. >>> diff(lambda x: besselj(0,x), 7.5)
  4527. -0.1352484275797055051822405
  4528. >>> besselj(0, 7.5, 10)
  4529. -0.1377811164763244890135677
  4530. >>> diff(lambda x: besselj(0,x), 7.5, 10)
  4531. -0.1377811164763244890135677
  4532. >>> besselj(0,7.5,-1) - besselj(0,3.5,-1)
  4533. -0.1241343240399987693521378
  4534. >>> quad(j0, [3.5, 7.5])
  4535. -0.1241343240399987693521378
  4536. Differentiation with a noninteger order gives the fractional derivative
  4537. in the sense of the Riemann-Liouville differintegral, as computed by
  4538. :func:`~mpmath.differint`::
  4539. >>> mp.dps = 15
  4540. >>> besselj(1, 3.5, 0.75)
  4541. -0.385977722939384
  4542. >>> differint(lambda x: besselj(1, x), 3.5, 0.75)
  4543. -0.385977722939384
  4544. """
  4545. besseli = r"""
  4546. ``besseli(n, x, derivative=0)`` gives the modified Bessel function of the
  4547. first kind,
  4548. .. math ::
  4549. I_n(x) = i^{-n} J_n(ix).
  4550. With *derivative* = `m \ne 0`, the `m`-th derivative
  4551. .. math ::
  4552. \frac{d^m}{dx^m} I_n(x)
  4553. is computed.
  4554. **Plots**
  4555. .. literalinclude :: /plots/besseli.py
  4556. .. image :: /plots/besseli.png
  4557. .. literalinclude :: /plots/besseli_c.py
  4558. .. image :: /plots/besseli_c.png
  4559. **Examples**
  4560. Some values of `I_n(x)`::
  4561. >>> from mpmath import *
  4562. >>> mp.dps = 25; mp.pretty = True
  4563. >>> besseli(0,0)
  4564. 1.0
  4565. >>> besseli(1,0)
  4566. 0.0
  4567. >>> besseli(0,1)
  4568. 1.266065877752008335598245
  4569. >>> besseli(3.5, 2+3j)
  4570. (-0.2904369752642538144289025 - 0.4469098397654815837307006j)
  4571. Arguments may be large::
  4572. >>> besseli(2, 1000)
  4573. 2.480717210191852440616782e+432
  4574. >>> besseli(2, 10**10)
  4575. 4.299602851624027900335391e+4342944813
  4576. >>> besseli(2, 6000+10000j)
  4577. (-2.114650753239580827144204e+2603 + 4.385040221241629041351886e+2602j)
  4578. For integers `n`, the following integral representation holds::
  4579. >>> mp.dps = 15
  4580. >>> n = 3
  4581. >>> x = 2.3
  4582. >>> quad(lambda t: exp(x*cos(t))*cos(n*t), [0,pi])/pi
  4583. 0.349223221159309
  4584. >>> besseli(n,x)
  4585. 0.349223221159309
  4586. Derivatives and antiderivatives of any order can be computed::
  4587. >>> mp.dps = 25
  4588. >>> besseli(2, 7.5, 1)
  4589. 195.8229038931399062565883
  4590. >>> diff(lambda x: besseli(2,x), 7.5)
  4591. 195.8229038931399062565883
  4592. >>> besseli(2, 7.5, 10)
  4593. 153.3296508971734525525176
  4594. >>> diff(lambda x: besseli(2,x), 7.5, 10)
  4595. 153.3296508971734525525176
  4596. >>> besseli(2,7.5,-1) - besseli(2,3.5,-1)
  4597. 202.5043900051930141956876
  4598. >>> quad(lambda x: besseli(2,x), [3.5, 7.5])
  4599. 202.5043900051930141956876
  4600. """
  4601. bessely = r"""
  4602. ``bessely(n, x, derivative=0)`` gives the Bessel function of the second kind,
  4603. .. math ::
  4604. Y_n(x) = \frac{J_n(x) \cos(\pi n) - J_{-n}(x)}{\sin(\pi n)}.
  4605. For `n` an integer, this formula should be understood as a
  4606. limit. With *derivative* = `m \ne 0`, the `m`-th derivative
  4607. .. math ::
  4608. \frac{d^m}{dx^m} Y_n(x)
  4609. is computed.
  4610. **Plots**
  4611. .. literalinclude :: /plots/bessely.py
  4612. .. image :: /plots/bessely.png
  4613. .. literalinclude :: /plots/bessely_c.py
  4614. .. image :: /plots/bessely_c.png
  4615. **Examples**
  4616. Some values of `Y_n(x)`::
  4617. >>> from mpmath import *
  4618. >>> mp.dps = 25; mp.pretty = True
  4619. >>> bessely(0,0), bessely(1,0), bessely(2,0)
  4620. (-inf, -inf, -inf)
  4621. >>> bessely(1, pi)
  4622. 0.3588729167767189594679827
  4623. >>> bessely(0.5, 3+4j)
  4624. (9.242861436961450520325216 - 3.085042824915332562522402j)
  4625. Arguments may be large::
  4626. >>> bessely(0, 10000)
  4627. 0.00364780555898660588668872
  4628. >>> bessely(2.5, 10**50)
  4629. -4.8952500412050989295774e-26
  4630. >>> bessely(2.5, -10**50)
  4631. (0.0 + 4.8952500412050989295774e-26j)
  4632. Derivatives and antiderivatives of any order can be computed::
  4633. >>> bessely(2, 3.5, 1)
  4634. 0.3842618820422660066089231
  4635. >>> diff(lambda x: bessely(2, x), 3.5)
  4636. 0.3842618820422660066089231
  4637. >>> bessely(0.5, 3.5, 1)
  4638. -0.2066598304156764337900417
  4639. >>> diff(lambda x: bessely(0.5, x), 3.5)
  4640. -0.2066598304156764337900417
  4641. >>> diff(lambda x: bessely(2, x), 0.5, 10)
  4642. -208173867409.5547350101511
  4643. >>> bessely(2, 0.5, 10)
  4644. -208173867409.5547350101511
  4645. >>> bessely(2, 100.5, 100)
  4646. 0.02668487547301372334849043
  4647. >>> quad(lambda x: bessely(2,x), [1,3])
  4648. -1.377046859093181969213262
  4649. >>> bessely(2,3,-1) - bessely(2,1,-1)
  4650. -1.377046859093181969213262
  4651. """
  4652. besselk = r"""
  4653. ``besselk(n, x)`` gives the modified Bessel function of the
  4654. second kind,
  4655. .. math ::
  4656. K_n(x) = \frac{\pi}{2} \frac{I_{-n}(x)-I_{n}(x)}{\sin(\pi n)}
  4657. For `n` an integer, this formula should be understood as a
  4658. limit.
  4659. **Plots**
  4660. .. literalinclude :: /plots/besselk.py
  4661. .. image :: /plots/besselk.png
  4662. .. literalinclude :: /plots/besselk_c.py
  4663. .. image :: /plots/besselk_c.png
  4664. **Examples**
  4665. Evaluation is supported for arbitrary complex arguments::
  4666. >>> from mpmath import *
  4667. >>> mp.dps = 25; mp.pretty = True
  4668. >>> besselk(0,1)
  4669. 0.4210244382407083333356274
  4670. >>> besselk(0, -1)
  4671. (0.4210244382407083333356274 - 3.97746326050642263725661j)
  4672. >>> besselk(3.5, 2+3j)
  4673. (-0.02090732889633760668464128 + 0.2464022641351420167819697j)
  4674. >>> besselk(2+3j, 0.5)
  4675. (0.9615816021726349402626083 + 0.1918250181801757416908224j)
  4676. Arguments may be large::
  4677. >>> besselk(0, 100)
  4678. 4.656628229175902018939005e-45
  4679. >>> besselk(1, 10**6)
  4680. 4.131967049321725588398296e-434298
  4681. >>> besselk(1, 10**6*j)
  4682. (0.001140348428252385844876706 - 0.0005200017201681152909000961j)
  4683. >>> besselk(4.5, fmul(10**50, j, exact=True))
  4684. (1.561034538142413947789221e-26 + 1.243554598118700063281496e-25j)
  4685. The point `x = 0` is a singularity (logarithmic if `n = 0`)::
  4686. >>> besselk(0,0)
  4687. +inf
  4688. >>> besselk(1,0)
  4689. +inf
  4690. >>> for n in range(-4, 5):
  4691. ... print(besselk(n, '1e-1000'))
  4692. ...
  4693. 4.8e+4001
  4694. 8.0e+3000
  4695. 2.0e+2000
  4696. 1.0e+1000
  4697. 2302.701024509704096466802
  4698. 1.0e+1000
  4699. 2.0e+2000
  4700. 8.0e+3000
  4701. 4.8e+4001
  4702. """
  4703. hankel1 = r"""
  4704. ``hankel1(n,x)`` computes the Hankel function of the first kind,
  4705. which is the complex combination of Bessel functions given by
  4706. .. math ::
  4707. H_n^{(1)}(x) = J_n(x) + i Y_n(x).
  4708. **Plots**
  4709. .. literalinclude :: /plots/hankel1.py
  4710. .. image :: /plots/hankel1.png
  4711. .. literalinclude :: /plots/hankel1_c.py
  4712. .. image :: /plots/hankel1_c.png
  4713. **Examples**
  4714. The Hankel function is generally complex-valued::
  4715. >>> from mpmath import *
  4716. >>> mp.dps = 25; mp.pretty = True
  4717. >>> hankel1(2, pi)
  4718. (0.4854339326315091097054957 - 0.0999007139290278787734903j)
  4719. >>> hankel1(3.5, pi)
  4720. (0.2340002029630507922628888 - 0.6419643823412927142424049j)
  4721. """
  4722. hankel2 = r"""
  4723. ``hankel2(n,x)`` computes the Hankel function of the second kind,
  4724. which is the complex combination of Bessel functions given by
  4725. .. math ::
  4726. H_n^{(2)}(x) = J_n(x) - i Y_n(x).
  4727. **Plots**
  4728. .. literalinclude :: /plots/hankel2.py
  4729. .. image :: /plots/hankel2.png
  4730. .. literalinclude :: /plots/hankel2_c.py
  4731. .. image :: /plots/hankel2_c.png
  4732. **Examples**
  4733. The Hankel function is generally complex-valued::
  4734. >>> from mpmath import *
  4735. >>> mp.dps = 25; mp.pretty = True
  4736. >>> hankel2(2, pi)
  4737. (0.4854339326315091097054957 + 0.0999007139290278787734903j)
  4738. >>> hankel2(3.5, pi)
  4739. (0.2340002029630507922628888 + 0.6419643823412927142424049j)
  4740. """
  4741. lambertw = r"""
  4742. The Lambert W function `W(z)` is defined as the inverse function
  4743. of `w \exp(w)`. In other words, the value of `W(z)` is such that
  4744. `z = W(z) \exp(W(z))` for any complex number `z`.
  4745. The Lambert W function is a multivalued function with infinitely
  4746. many branches `W_k(z)`, indexed by `k \in \mathbb{Z}`. Each branch
  4747. gives a different solution `w` of the equation `z = w \exp(w)`.
  4748. All branches are supported by :func:`~mpmath.lambertw`:
  4749. * ``lambertw(z)`` gives the principal solution (branch 0)
  4750. * ``lambertw(z, k)`` gives the solution on branch `k`
  4751. The Lambert W function has two partially real branches: the
  4752. principal branch (`k = 0`) is real for real `z > -1/e`, and the
  4753. `k = -1` branch is real for `-1/e < z < 0`. All branches except
  4754. `k = 0` have a logarithmic singularity at `z = 0`.
  4755. The definition, implementation and choice of branches
  4756. is based on [Corless]_.
  4757. **Plots**
  4758. .. literalinclude :: /plots/lambertw.py
  4759. .. image :: /plots/lambertw.png
  4760. .. literalinclude :: /plots/lambertw_c.py
  4761. .. image :: /plots/lambertw_c.png
  4762. **Basic examples**
  4763. The Lambert W function is the inverse of `w \exp(w)`::
  4764. >>> from mpmath import *
  4765. >>> mp.dps = 25; mp.pretty = True
  4766. >>> w = lambertw(1)
  4767. >>> w
  4768. 0.5671432904097838729999687
  4769. >>> w*exp(w)
  4770. 1.0
  4771. Any branch gives a valid inverse::
  4772. >>> w = lambertw(1, k=3)
  4773. >>> w
  4774. (-2.853581755409037807206819 + 17.11353553941214591260783j)
  4775. >>> w = lambertw(1, k=25)
  4776. >>> w
  4777. (-5.047020464221569709378686 + 155.4763860949415867162066j)
  4778. >>> chop(w*exp(w))
  4779. 1.0
  4780. **Applications to equation-solving**
  4781. The Lambert W function may be used to solve various kinds of
  4782. equations, such as finding the value of the infinite power
  4783. tower `z^{z^{z^{\ldots}}}`::
  4784. >>> def tower(z, n):
  4785. ... if n == 0:
  4786. ... return z
  4787. ... return z ** tower(z, n-1)
  4788. ...
  4789. >>> tower(mpf(0.5), 100)
  4790. 0.6411857445049859844862005
  4791. >>> -lambertw(-log(0.5))/log(0.5)
  4792. 0.6411857445049859844862005
  4793. **Properties**
  4794. The Lambert W function grows roughly like the natural logarithm
  4795. for large arguments::
  4796. >>> lambertw(1000); log(1000)
  4797. 5.249602852401596227126056
  4798. 6.907755278982137052053974
  4799. >>> lambertw(10**100); log(10**100)
  4800. 224.8431064451185015393731
  4801. 230.2585092994045684017991
  4802. The principal branch of the Lambert W function has a rational
  4803. Taylor series expansion around `z = 0`::
  4804. >>> nprint(taylor(lambertw, 0, 6), 10)
  4805. [0.0, 1.0, -1.0, 1.5, -2.666666667, 5.208333333, -10.8]
  4806. Some special values and limits are::
  4807. >>> lambertw(0)
  4808. 0.0
  4809. >>> lambertw(1)
  4810. 0.5671432904097838729999687
  4811. >>> lambertw(e)
  4812. 1.0
  4813. >>> lambertw(inf)
  4814. +inf
  4815. >>> lambertw(0, k=-1)
  4816. -inf
  4817. >>> lambertw(0, k=3)
  4818. -inf
  4819. >>> lambertw(inf, k=2)
  4820. (+inf + 12.56637061435917295385057j)
  4821. >>> lambertw(inf, k=3)
  4822. (+inf + 18.84955592153875943077586j)
  4823. >>> lambertw(-inf, k=3)
  4824. (+inf + 21.9911485751285526692385j)
  4825. The `k = 0` and `k = -1` branches join at `z = -1/e` where
  4826. `W(z) = -1` for both branches. Since `-1/e` can only be represented
  4827. approximately with binary floating-point numbers, evaluating the
  4828. Lambert W function at this point only gives `-1` approximately::
  4829. >>> lambertw(-1/e, 0)
  4830. -0.9999999999998371330228251
  4831. >>> lambertw(-1/e, -1)
  4832. -1.000000000000162866977175
  4833. If `-1/e` happens to round in the negative direction, there might be
  4834. a small imaginary part::
  4835. >>> mp.dps = 15
  4836. >>> lambertw(-1/e)
  4837. (-1.0 + 8.22007971483662e-9j)
  4838. >>> lambertw(-1/e+eps)
  4839. -0.999999966242188
  4840. **References**
  4841. 1. [Corless]_
  4842. """
  4843. barnesg = r"""
  4844. Evaluates the Barnes G-function, which generalizes the
  4845. superfactorial (:func:`~mpmath.superfac`) and by extension also the
  4846. hyperfactorial (:func:`~mpmath.hyperfac`) to the complex numbers
  4847. in an analogous way to how the gamma function generalizes
  4848. the ordinary factorial.
  4849. The Barnes G-function may be defined in terms of a Weierstrass
  4850. product:
  4851. .. math ::
  4852. G(z+1) = (2\pi)^{z/2} e^{-[z(z+1)+\gamma z^2]/2}
  4853. \prod_{n=1}^\infty
  4854. \left[\left(1+\frac{z}{n}\right)^ne^{-z+z^2/(2n)}\right]
  4855. For positive integers `n`, we have have relation to superfactorials
  4856. `G(n) = \mathrm{sf}(n-2) = 0! \cdot 1! \cdots (n-2)!`.
  4857. **Examples**
  4858. Some elementary values and limits of the Barnes G-function::
  4859. >>> from mpmath import *
  4860. >>> mp.dps = 15; mp.pretty = True
  4861. >>> barnesg(1), barnesg(2), barnesg(3)
  4862. (1.0, 1.0, 1.0)
  4863. >>> barnesg(4)
  4864. 2.0
  4865. >>> barnesg(5)
  4866. 12.0
  4867. >>> barnesg(6)
  4868. 288.0
  4869. >>> barnesg(7)
  4870. 34560.0
  4871. >>> barnesg(8)
  4872. 24883200.0
  4873. >>> barnesg(inf)
  4874. +inf
  4875. >>> barnesg(0), barnesg(-1), barnesg(-2)
  4876. (0.0, 0.0, 0.0)
  4877. Closed-form values are known for some rational arguments::
  4878. >>> barnesg('1/2')
  4879. 0.603244281209446
  4880. >>> sqrt(exp(0.25+log(2)/12)/sqrt(pi)/glaisher**3)
  4881. 0.603244281209446
  4882. >>> barnesg('1/4')
  4883. 0.29375596533861
  4884. >>> nthroot(exp('3/8')/exp(catalan/pi)/
  4885. ... gamma(0.25)**3/sqrt(glaisher)**9, 4)
  4886. 0.29375596533861
  4887. The Barnes G-function satisfies the functional equation
  4888. `G(z+1) = \Gamma(z) G(z)`::
  4889. >>> z = pi
  4890. >>> barnesg(z+1)
  4891. 2.39292119327948
  4892. >>> gamma(z)*barnesg(z)
  4893. 2.39292119327948
  4894. The asymptotic growth rate of the Barnes G-function is related to
  4895. the Glaisher-Kinkelin constant::
  4896. >>> limit(lambda n: barnesg(n+1)/(n**(n**2/2-mpf(1)/12)*
  4897. ... (2*pi)**(n/2)*exp(-3*n**2/4)), inf)
  4898. 0.847536694177301
  4899. >>> exp('1/12')/glaisher
  4900. 0.847536694177301
  4901. The Barnes G-function can be differentiated in closed form::
  4902. >>> z = 3
  4903. >>> diff(barnesg, z)
  4904. 0.264507203401607
  4905. >>> barnesg(z)*((z-1)*psi(0,z)-z+(log(2*pi)+1)/2)
  4906. 0.264507203401607
  4907. Evaluation is supported for arbitrary arguments and at arbitrary
  4908. precision::
  4909. >>> barnesg(6.5)
  4910. 2548.7457695685
  4911. >>> barnesg(-pi)
  4912. 0.00535976768353037
  4913. >>> barnesg(3+4j)
  4914. (-0.000676375932234244 - 4.42236140124728e-5j)
  4915. >>> mp.dps = 50
  4916. >>> barnesg(1/sqrt(2))
  4917. 0.81305501090451340843586085064413533788206204124732
  4918. >>> q = barnesg(10j)
  4919. >>> q.real
  4920. 0.000000000021852360840356557241543036724799812371995850552234
  4921. >>> q.imag
  4922. -0.00000000000070035335320062304849020654215545839053210041457588
  4923. >>> mp.dps = 15
  4924. >>> barnesg(100)
  4925. 3.10361006263698e+6626
  4926. >>> barnesg(-101)
  4927. 0.0
  4928. >>> barnesg(-10.5)
  4929. 5.94463017605008e+25
  4930. >>> barnesg(-10000.5)
  4931. -6.14322868174828e+167480422
  4932. >>> barnesg(1000j)
  4933. (5.21133054865546e-1173597 + 4.27461836811016e-1173597j)
  4934. >>> barnesg(-1000+1000j)
  4935. (2.43114569750291e+1026623 + 2.24851410674842e+1026623j)
  4936. **References**
  4937. 1. Whittaker & Watson, *A Course of Modern Analysis*,
  4938. Cambridge University Press, 4th edition (1927), p.264
  4939. 2. http://en.wikipedia.org/wiki/Barnes_G-function
  4940. 3. http://mathworld.wolfram.com/BarnesG-Function.html
  4941. """
  4942. superfac = r"""
  4943. Computes the superfactorial, defined as the product of
  4944. consecutive factorials
  4945. .. math ::
  4946. \mathrm{sf}(n) = \prod_{k=1}^n k!
  4947. For general complex `z`, `\mathrm{sf}(z)` is defined
  4948. in terms of the Barnes G-function (see :func:`~mpmath.barnesg`).
  4949. **Examples**
  4950. The first few superfactorials are (OEIS A000178)::
  4951. >>> from mpmath import *
  4952. >>> mp.dps = 15; mp.pretty = True
  4953. >>> for n in range(10):
  4954. ... print("%s %s" % (n, superfac(n)))
  4955. ...
  4956. 0 1.0
  4957. 1 1.0
  4958. 2 2.0
  4959. 3 12.0
  4960. 4 288.0
  4961. 5 34560.0
  4962. 6 24883200.0
  4963. 7 125411328000.0
  4964. 8 5.05658474496e+15
  4965. 9 1.83493347225108e+21
  4966. Superfactorials grow very rapidly::
  4967. >>> superfac(1000)
  4968. 3.24570818422368e+1177245
  4969. >>> superfac(10**10)
  4970. 2.61398543581249e+467427913956904067453
  4971. Evaluation is supported for arbitrary arguments::
  4972. >>> mp.dps = 25
  4973. >>> superfac(pi)
  4974. 17.20051550121297985285333
  4975. >>> superfac(2+3j)
  4976. (-0.005915485633199789627466468 + 0.008156449464604044948738263j)
  4977. >>> diff(superfac, 1)
  4978. 0.2645072034016070205673056
  4979. **References**
  4980. 1. http://oeis.org/A000178
  4981. """
  4982. hyperfac = r"""
  4983. Computes the hyperfactorial, defined for integers as the product
  4984. .. math ::
  4985. H(n) = \prod_{k=1}^n k^k.
  4986. The hyperfactorial satisfies the recurrence formula `H(z) = z^z H(z-1)`.
  4987. It can be defined more generally in terms of the Barnes G-function (see
  4988. :func:`~mpmath.barnesg`) and the gamma function by the formula
  4989. .. math ::
  4990. H(z) = \frac{\Gamma(z+1)^z}{G(z)}.
  4991. The extension to complex numbers can also be done via
  4992. the integral representation
  4993. .. math ::
  4994. H(z) = (2\pi)^{-z/2} \exp \left[
  4995. {z+1 \choose 2} + \int_0^z \log(t!)\,dt
  4996. \right].
  4997. **Examples**
  4998. The rapidly-growing sequence of hyperfactorials begins
  4999. (OEIS A002109)::
  5000. >>> from mpmath import *
  5001. >>> mp.dps = 15; mp.pretty = True
  5002. >>> for n in range(10):
  5003. ... print("%s %s" % (n, hyperfac(n)))
  5004. ...
  5005. 0 1.0
  5006. 1 1.0
  5007. 2 4.0
  5008. 3 108.0
  5009. 4 27648.0
  5010. 5 86400000.0
  5011. 6 4031078400000.0
  5012. 7 3.3197663987712e+18
  5013. 8 5.56964379417266e+25
  5014. 9 2.15779412229419e+34
  5015. Some even larger hyperfactorials are::
  5016. >>> hyperfac(1000)
  5017. 5.46458120882585e+1392926
  5018. >>> hyperfac(10**10)
  5019. 4.60408207642219e+489142638002418704309
  5020. The hyperfactorial can be evaluated for arbitrary arguments::
  5021. >>> hyperfac(0.5)
  5022. 0.880449235173423
  5023. >>> diff(hyperfac, 1)
  5024. 0.581061466795327
  5025. >>> hyperfac(pi)
  5026. 205.211134637462
  5027. >>> hyperfac(-10+1j)
  5028. (3.01144471378225e+46 - 2.45285242480185e+46j)
  5029. The recurrence property of the hyperfactorial holds
  5030. generally::
  5031. >>> z = 3-4*j
  5032. >>> hyperfac(z)
  5033. (-4.49795891462086e-7 - 6.33262283196162e-7j)
  5034. >>> z**z * hyperfac(z-1)
  5035. (-4.49795891462086e-7 - 6.33262283196162e-7j)
  5036. >>> z = mpf(-0.6)
  5037. >>> chop(z**z * hyperfac(z-1))
  5038. 1.28170142849352
  5039. >>> hyperfac(z)
  5040. 1.28170142849352
  5041. The hyperfactorial may also be computed using the integral
  5042. definition::
  5043. >>> z = 2.5
  5044. >>> hyperfac(z)
  5045. 15.9842119922237
  5046. >>> (2*pi)**(-z/2)*exp(binomial(z+1,2) +
  5047. ... quad(lambda t: loggamma(t+1), [0, z]))
  5048. 15.9842119922237
  5049. :func:`~mpmath.hyperfac` supports arbitrary-precision evaluation::
  5050. >>> mp.dps = 50
  5051. >>> hyperfac(10)
  5052. 215779412229418562091680268288000000000000000.0
  5053. >>> hyperfac(1/sqrt(2))
  5054. 0.89404818005227001975423476035729076375705084390942
  5055. **References**
  5056. 1. http://oeis.org/A002109
  5057. 2. http://mathworld.wolfram.com/Hyperfactorial.html
  5058. """
  5059. rgamma = r"""
  5060. Computes the reciprocal of the gamma function, `1/\Gamma(z)`. This
  5061. function evaluates to zero at the poles
  5062. of the gamma function, `z = 0, -1, -2, \ldots`.
  5063. **Examples**
  5064. Basic examples::
  5065. >>> from mpmath import *
  5066. >>> mp.dps = 25; mp.pretty = True
  5067. >>> rgamma(1)
  5068. 1.0
  5069. >>> rgamma(4)
  5070. 0.1666666666666666666666667
  5071. >>> rgamma(0); rgamma(-1)
  5072. 0.0
  5073. 0.0
  5074. >>> rgamma(1000)
  5075. 2.485168143266784862783596e-2565
  5076. >>> rgamma(inf)
  5077. 0.0
  5078. A definite integral that can be evaluated in terms of elementary
  5079. integrals::
  5080. >>> quad(rgamma, [0,inf])
  5081. 2.807770242028519365221501
  5082. >>> e + quad(lambda t: exp(-t)/(pi**2+log(t)**2), [0,inf])
  5083. 2.807770242028519365221501
  5084. """
  5085. loggamma = r"""
  5086. Computes the principal branch of the log-gamma function,
  5087. `\ln \Gamma(z)`. Unlike `\ln(\Gamma(z))`, which has infinitely many
  5088. complex branch cuts, the principal log-gamma function only has a single
  5089. branch cut along the negative half-axis. The principal branch
  5090. continuously matches the asymptotic Stirling expansion
  5091. .. math ::
  5092. \ln \Gamma(z) \sim \frac{\ln(2 \pi)}{2} +
  5093. \left(z-\frac{1}{2}\right) \ln(z) - z + O(z^{-1}).
  5094. The real parts of both functions agree, but their imaginary
  5095. parts generally differ by `2 n \pi` for some `n \in \mathbb{Z}`.
  5096. They coincide for `z \in \mathbb{R}, z > 0`.
  5097. Computationally, it is advantageous to use :func:`~mpmath.loggamma`
  5098. instead of :func:`~mpmath.gamma` for extremely large arguments.
  5099. **Examples**
  5100. Comparing with `\ln(\Gamma(z))`::
  5101. >>> from mpmath import *
  5102. >>> mp.dps = 25; mp.pretty = True
  5103. >>> loggamma('13.2'); log(gamma('13.2'))
  5104. 20.49400419456603678498394
  5105. 20.49400419456603678498394
  5106. >>> loggamma(3+4j)
  5107. (-1.756626784603784110530604 + 4.742664438034657928194889j)
  5108. >>> log(gamma(3+4j))
  5109. (-1.756626784603784110530604 - 1.540520869144928548730397j)
  5110. >>> log(gamma(3+4j)) + 2*pi*j
  5111. (-1.756626784603784110530604 + 4.742664438034657928194889j)
  5112. Note the imaginary parts for negative arguments::
  5113. >>> loggamma(-0.5); loggamma(-1.5); loggamma(-2.5)
  5114. (1.265512123484645396488946 - 3.141592653589793238462643j)
  5115. (0.8600470153764810145109327 - 6.283185307179586476925287j)
  5116. (-0.05624371649767405067259453 - 9.42477796076937971538793j)
  5117. Some special values::
  5118. >>> loggamma(1); loggamma(2)
  5119. 0.0
  5120. 0.0
  5121. >>> loggamma(3); +ln2
  5122. 0.6931471805599453094172321
  5123. 0.6931471805599453094172321
  5124. >>> loggamma(3.5); log(15*sqrt(pi)/8)
  5125. 1.200973602347074224816022
  5126. 1.200973602347074224816022
  5127. >>> loggamma(inf)
  5128. +inf
  5129. Huge arguments are permitted::
  5130. >>> loggamma('1e30')
  5131. 6.807755278982137052053974e+31
  5132. >>> loggamma('1e300')
  5133. 6.897755278982137052053974e+302
  5134. >>> loggamma('1e3000')
  5135. 6.906755278982137052053974e+3003
  5136. >>> loggamma('1e100000000000000000000')
  5137. 2.302585092994045684007991e+100000000000000000020
  5138. >>> loggamma('1e30j')
  5139. (-1.570796326794896619231322e+30 + 6.807755278982137052053974e+31j)
  5140. >>> loggamma('1e300j')
  5141. (-1.570796326794896619231322e+300 + 6.897755278982137052053974e+302j)
  5142. >>> loggamma('1e3000j')
  5143. (-1.570796326794896619231322e+3000 + 6.906755278982137052053974e+3003j)
  5144. The log-gamma function can be integrated analytically
  5145. on any interval of unit length::
  5146. >>> z = 0
  5147. >>> quad(loggamma, [z,z+1]); log(2*pi)/2
  5148. 0.9189385332046727417803297
  5149. 0.9189385332046727417803297
  5150. >>> z = 3+4j
  5151. >>> quad(loggamma, [z,z+1]); (log(z)-1)*z + log(2*pi)/2
  5152. (-0.9619286014994750641314421 + 5.219637303741238195688575j)
  5153. (-0.9619286014994750641314421 + 5.219637303741238195688575j)
  5154. The derivatives of the log-gamma function are given by the
  5155. polygamma function (:func:`~mpmath.psi`)::
  5156. >>> diff(loggamma, -4+3j); psi(0, -4+3j)
  5157. (1.688493531222971393607153 + 2.554898911356806978892748j)
  5158. (1.688493531222971393607153 + 2.554898911356806978892748j)
  5159. >>> diff(loggamma, -4+3j, 2); psi(1, -4+3j)
  5160. (-0.1539414829219882371561038 - 0.1020485197430267719746479j)
  5161. (-0.1539414829219882371561038 - 0.1020485197430267719746479j)
  5162. The log-gamma function satisfies an additive form of the
  5163. recurrence relation for the ordinary gamma function::
  5164. >>> z = 2+3j
  5165. >>> loggamma(z); loggamma(z+1) - log(z)
  5166. (-2.092851753092733349564189 + 2.302396543466867626153708j)
  5167. (-2.092851753092733349564189 + 2.302396543466867626153708j)
  5168. """
  5169. siegeltheta = r"""
  5170. Computes the Riemann-Siegel theta function,
  5171. .. math ::
  5172. \theta(t) = \frac{
  5173. \log\Gamma\left(\frac{1+2it}{4}\right) -
  5174. \log\Gamma\left(\frac{1-2it}{4}\right)
  5175. }{2i} - \frac{\log \pi}{2} t.
  5176. The Riemann-Siegel theta function is important in
  5177. providing the phase factor for the Z-function
  5178. (see :func:`~mpmath.siegelz`). Evaluation is supported for real and
  5179. complex arguments::
  5180. >>> from mpmath import *
  5181. >>> mp.dps = 25; mp.pretty = True
  5182. >>> siegeltheta(0)
  5183. 0.0
  5184. >>> siegeltheta(inf)
  5185. +inf
  5186. >>> siegeltheta(-inf)
  5187. -inf
  5188. >>> siegeltheta(1)
  5189. -1.767547952812290388302216
  5190. >>> siegeltheta(10+0.25j)
  5191. (-3.068638039426838572528867 + 0.05804937947429712998395177j)
  5192. Arbitrary derivatives may be computed with derivative = k
  5193. >>> siegeltheta(1234, derivative=2)
  5194. 0.0004051864079114053109473741
  5195. >>> diff(siegeltheta, 1234, n=2)
  5196. 0.0004051864079114053109473741
  5197. The Riemann-Siegel theta function has odd symmetry around `t = 0`,
  5198. two local extreme points and three real roots including 0 (located
  5199. symmetrically)::
  5200. >>> nprint(chop(taylor(siegeltheta, 0, 5)))
  5201. [0.0, -2.68609, 0.0, 2.69433, 0.0, -6.40218]
  5202. >>> findroot(diffun(siegeltheta), 7)
  5203. 6.28983598883690277966509
  5204. >>> findroot(siegeltheta, 20)
  5205. 17.84559954041086081682634
  5206. For large `t`, there is a famous asymptotic formula
  5207. for `\theta(t)`, to first order given by::
  5208. >>> t = mpf(10**6)
  5209. >>> siegeltheta(t)
  5210. 5488816.353078403444882823
  5211. >>> -t*log(2*pi/t)/2-t/2
  5212. 5488816.745777464310273645
  5213. """
  5214. grampoint = r"""
  5215. Gives the `n`-th Gram point `g_n`, defined as the solution
  5216. to the equation `\theta(g_n) = \pi n` where `\theta(t)`
  5217. is the Riemann-Siegel theta function (:func:`~mpmath.siegeltheta`).
  5218. The first few Gram points are::
  5219. >>> from mpmath import *
  5220. >>> mp.dps = 25; mp.pretty = True
  5221. >>> grampoint(0)
  5222. 17.84559954041086081682634
  5223. >>> grampoint(1)
  5224. 23.17028270124630927899664
  5225. >>> grampoint(2)
  5226. 27.67018221781633796093849
  5227. >>> grampoint(3)
  5228. 31.71797995476405317955149
  5229. Checking the definition::
  5230. >>> siegeltheta(grampoint(3))
  5231. 9.42477796076937971538793
  5232. >>> 3*pi
  5233. 9.42477796076937971538793
  5234. A large Gram point::
  5235. >>> grampoint(10**10)
  5236. 3293531632.728335454561153
  5237. Gram points are useful when studying the Z-function
  5238. (:func:`~mpmath.siegelz`). See the documentation of that function
  5239. for additional examples.
  5240. :func:`~mpmath.grampoint` can solve the defining equation for
  5241. nonintegral `n`. There is a fixed point where `g(x) = x`::
  5242. >>> findroot(lambda x: grampoint(x) - x, 10000)
  5243. 9146.698193171459265866198
  5244. **References**
  5245. 1. http://mathworld.wolfram.com/GramPoint.html
  5246. """
  5247. siegelz = r"""
  5248. Computes the Z-function, also known as the Riemann-Siegel Z function,
  5249. .. math ::
  5250. Z(t) = e^{i \theta(t)} \zeta(1/2+it)
  5251. where `\zeta(s)` is the Riemann zeta function (:func:`~mpmath.zeta`)
  5252. and where `\theta(t)` denotes the Riemann-Siegel theta function
  5253. (see :func:`~mpmath.siegeltheta`).
  5254. Evaluation is supported for real and complex arguments::
  5255. >>> from mpmath import *
  5256. >>> mp.dps = 25; mp.pretty = True
  5257. >>> siegelz(1)
  5258. -0.7363054628673177346778998
  5259. >>> siegelz(3+4j)
  5260. (-0.1852895764366314976003936 - 0.2773099198055652246992479j)
  5261. The first four derivatives are supported, using the
  5262. optional *derivative* keyword argument::
  5263. >>> siegelz(1234567, derivative=3)
  5264. 56.89689348495089294249178
  5265. >>> diff(siegelz, 1234567, n=3)
  5266. 56.89689348495089294249178
  5267. The Z-function has a Maclaurin expansion::
  5268. >>> nprint(chop(taylor(siegelz, 0, 4)))
  5269. [-1.46035, 0.0, 2.73588, 0.0, -8.39357]
  5270. The Z-function `Z(t)` is equal to `\pm |\zeta(s)|` on the
  5271. critical line `s = 1/2+it` (i.e. for real arguments `t`
  5272. to `Z`). Its zeros coincide with those of the Riemann zeta
  5273. function::
  5274. >>> findroot(siegelz, 14)
  5275. 14.13472514173469379045725
  5276. >>> findroot(siegelz, 20)
  5277. 21.02203963877155499262848
  5278. >>> findroot(zeta, 0.5+14j)
  5279. (0.5 + 14.13472514173469379045725j)
  5280. >>> findroot(zeta, 0.5+20j)
  5281. (0.5 + 21.02203963877155499262848j)
  5282. Since the Z-function is real-valued on the critical line
  5283. (and unlike `|\zeta(s)|` analytic), it is useful for
  5284. investigating the zeros of the Riemann zeta function.
  5285. For example, one can use a root-finding algorithm based
  5286. on sign changes::
  5287. >>> findroot(siegelz, [100, 200], solver='bisect')
  5288. 176.4414342977104188888926
  5289. To locate roots, Gram points `g_n` which can be computed
  5290. by :func:`~mpmath.grampoint` are useful. If `(-1)^n Z(g_n)` is
  5291. positive for two consecutive `n`, then `Z(t)` must have
  5292. a zero between those points::
  5293. >>> g10 = grampoint(10)
  5294. >>> g11 = grampoint(11)
  5295. >>> (-1)**10 * siegelz(g10) > 0
  5296. True
  5297. >>> (-1)**11 * siegelz(g11) > 0
  5298. True
  5299. >>> findroot(siegelz, [g10, g11], solver='bisect')
  5300. 56.44624769706339480436776
  5301. >>> g10, g11
  5302. (54.67523744685325626632663, 57.54516517954725443703014)
  5303. """
  5304. riemannr = r"""
  5305. Evaluates the Riemann R function, a smooth approximation of the
  5306. prime counting function `\pi(x)` (see :func:`~mpmath.primepi`). The Riemann
  5307. R function gives a fast numerical approximation useful e.g. to
  5308. roughly estimate the number of primes in a given interval.
  5309. The Riemann R function is computed using the rapidly convergent Gram
  5310. series,
  5311. .. math ::
  5312. R(x) = 1 + \sum_{k=1}^{\infty}
  5313. \frac{\log^k x}{k k! \zeta(k+1)}.
  5314. From the Gram series, one sees that the Riemann R function is a
  5315. well-defined analytic function (except for a branch cut along
  5316. the negative real half-axis); it can be evaluated for arbitrary
  5317. real or complex arguments.
  5318. The Riemann R function gives a very accurate approximation
  5319. of the prime counting function. For example, it is wrong by at
  5320. most 2 for `x < 1000`, and for `x = 10^9` differs from the exact
  5321. value of `\pi(x)` by 79, or less than two parts in a million.
  5322. It is about 10 times more accurate than the logarithmic integral
  5323. estimate (see :func:`~mpmath.li`), which however is even faster to evaluate.
  5324. It is orders of magnitude more accurate than the extremely
  5325. fast `x/\log x` estimate.
  5326. **Examples**
  5327. For small arguments, the Riemann R function almost exactly
  5328. gives the prime counting function if rounded to the nearest
  5329. integer::
  5330. >>> from mpmath import *
  5331. >>> mp.dps = 15; mp.pretty = True
  5332. >>> primepi(50), riemannr(50)
  5333. (15, 14.9757023241462)
  5334. >>> max(abs(primepi(n)-int(round(riemannr(n)))) for n in range(100))
  5335. 1
  5336. >>> max(abs(primepi(n)-int(round(riemannr(n)))) for n in range(300))
  5337. 2
  5338. The Riemann R function can be evaluated for arguments far too large
  5339. for exact determination of `\pi(x)` to be computationally
  5340. feasible with any presently known algorithm::
  5341. >>> riemannr(10**30)
  5342. 1.46923988977204e+28
  5343. >>> riemannr(10**100)
  5344. 4.3619719871407e+97
  5345. >>> riemannr(10**1000)
  5346. 4.3448325764012e+996
  5347. A comparison of the Riemann R function and logarithmic integral estimates
  5348. for `\pi(x)` using exact values of `\pi(10^n)` up to `n = 9`.
  5349. The fractional error is shown in parentheses::
  5350. >>> exact = [4,25,168,1229,9592,78498,664579,5761455,50847534]
  5351. >>> for n, p in enumerate(exact):
  5352. ... n += 1
  5353. ... r, l = riemannr(10**n), li(10**n)
  5354. ... rerr, lerr = nstr((r-p)/p,3), nstr((l-p)/p,3)
  5355. ... print("%i %i %s(%s) %s(%s)" % (n, p, r, rerr, l, lerr))
  5356. ...
  5357. 1 4 4.56458314100509(0.141) 6.1655995047873(0.541)
  5358. 2 25 25.6616332669242(0.0265) 30.1261415840796(0.205)
  5359. 3 168 168.359446281167(0.00214) 177.609657990152(0.0572)
  5360. 4 1229 1226.93121834343(-0.00168) 1246.13721589939(0.0139)
  5361. 5 9592 9587.43173884197(-0.000476) 9629.8090010508(0.00394)
  5362. 6 78498 78527.3994291277(0.000375) 78627.5491594622(0.00165)
  5363. 7 664579 664667.447564748(0.000133) 664918.405048569(0.000511)
  5364. 8 5761455 5761551.86732017(1.68e-5) 5762209.37544803(0.000131)
  5365. 9 50847534 50847455.4277214(-1.55e-6) 50849234.9570018(3.35e-5)
  5366. The derivative of the Riemann R function gives the approximate
  5367. probability for a number of magnitude `x` to be prime::
  5368. >>> diff(riemannr, 1000)
  5369. 0.141903028110784
  5370. >>> mpf(primepi(1050) - primepi(950)) / 100
  5371. 0.15
  5372. Evaluation is supported for arbitrary arguments and at arbitrary
  5373. precision::
  5374. >>> mp.dps = 30
  5375. >>> riemannr(7.5)
  5376. 3.72934743264966261918857135136
  5377. >>> riemannr(-4+2j)
  5378. (-0.551002208155486427591793957644 + 2.16966398138119450043195899746j)
  5379. """
  5380. primepi = r"""
  5381. Evaluates the prime counting function, `\pi(x)`, which gives
  5382. the number of primes less than or equal to `x`. The argument
  5383. `x` may be fractional.
  5384. The prime counting function is very expensive to evaluate
  5385. precisely for large `x`, and the present implementation is
  5386. not optimized in any way. For numerical approximation of the
  5387. prime counting function, it is better to use :func:`~mpmath.primepi2`
  5388. or :func:`~mpmath.riemannr`.
  5389. Some values of the prime counting function::
  5390. >>> from mpmath import *
  5391. >>> [primepi(k) for k in range(20)]
  5392. [0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8]
  5393. >>> primepi(3.5)
  5394. 2
  5395. >>> primepi(100000)
  5396. 9592
  5397. """
  5398. primepi2 = r"""
  5399. Returns an interval (as an ``mpi`` instance) providing bounds
  5400. for the value of the prime counting function `\pi(x)`. For small
  5401. `x`, :func:`~mpmath.primepi2` returns an exact interval based on
  5402. the output of :func:`~mpmath.primepi`. For `x > 2656`, a loose interval
  5403. based on Schoenfeld's inequality
  5404. .. math ::
  5405. |\pi(x) - \mathrm{li}(x)| < \frac{\sqrt x \log x}{8 \pi}
  5406. is returned. This estimate is rigorous assuming the truth of
  5407. the Riemann hypothesis, and can be computed very quickly.
  5408. **Examples**
  5409. Exact values of the prime counting function for small `x`::
  5410. >>> from mpmath import *
  5411. >>> mp.dps = 15; mp.pretty = True
  5412. >>> iv.dps = 15; iv.pretty = True
  5413. >>> primepi2(10)
  5414. [4.0, 4.0]
  5415. >>> primepi2(100)
  5416. [25.0, 25.0]
  5417. >>> primepi2(1000)
  5418. [168.0, 168.0]
  5419. Loose intervals are generated for moderately large `x`:
  5420. >>> primepi2(10000), primepi(10000)
  5421. ([1209.0, 1283.0], 1229)
  5422. >>> primepi2(50000), primepi(50000)
  5423. ([5070.0, 5263.0], 5133)
  5424. As `x` increases, the absolute error gets worse while the relative
  5425. error improves. The exact value of `\pi(10^{23})` is
  5426. 1925320391606803968923, and :func:`~mpmath.primepi2` gives 9 significant
  5427. digits::
  5428. >>> p = primepi2(10**23)
  5429. >>> p
  5430. [1.9253203909477020467e+21, 1.925320392280406229e+21]
  5431. >>> mpf(p.delta) / mpf(p.a)
  5432. 6.9219865355293e-10
  5433. A more precise, nonrigorous estimate for `\pi(x)` can be
  5434. obtained using the Riemann R function (:func:`~mpmath.riemannr`).
  5435. For large enough `x`, the value returned by :func:`~mpmath.primepi2`
  5436. essentially amounts to a small perturbation of the value returned by
  5437. :func:`~mpmath.riemannr`::
  5438. >>> primepi2(10**100)
  5439. [4.3619719871407024816e+97, 4.3619719871407032404e+97]
  5440. >>> riemannr(10**100)
  5441. 4.3619719871407e+97
  5442. """
  5443. primezeta = r"""
  5444. Computes the prime zeta function, which is defined
  5445. in analogy with the Riemann zeta function (:func:`~mpmath.zeta`)
  5446. as
  5447. .. math ::
  5448. P(s) = \sum_p \frac{1}{p^s}
  5449. where the sum is taken over all prime numbers `p`. Although
  5450. this sum only converges for `\mathrm{Re}(s) > 1`, the
  5451. function is defined by analytic continuation in the
  5452. half-plane `\mathrm{Re}(s) > 0`.
  5453. **Examples**
  5454. Arbitrary-precision evaluation for real and complex arguments is
  5455. supported::
  5456. >>> from mpmath import *
  5457. >>> mp.dps = 30; mp.pretty = True
  5458. >>> primezeta(2)
  5459. 0.452247420041065498506543364832
  5460. >>> primezeta(pi)
  5461. 0.15483752698840284272036497397
  5462. >>> mp.dps = 50
  5463. >>> primezeta(3)
  5464. 0.17476263929944353642311331466570670097541212192615
  5465. >>> mp.dps = 20
  5466. >>> primezeta(3+4j)
  5467. (-0.12085382601645763295 - 0.013370403397787023602j)
  5468. The prime zeta function has a logarithmic pole at `s = 1`,
  5469. with residue equal to the difference of the Mertens and
  5470. Euler constants::
  5471. >>> primezeta(1)
  5472. +inf
  5473. >>> extradps(25)(lambda x: primezeta(1+x)+log(x))(+eps)
  5474. -0.31571845205389007685
  5475. >>> mertens-euler
  5476. -0.31571845205389007685
  5477. The analytic continuation to `0 < \mathrm{Re}(s) \le 1`
  5478. is implemented. In this strip the function exhibits
  5479. very complex behavior; on the unit interval, it has poles at
  5480. `1/n` for every squarefree integer `n`::
  5481. >>> primezeta(0.5) # Pole at s = 1/2
  5482. (-inf + 3.1415926535897932385j)
  5483. >>> primezeta(0.25)
  5484. (-1.0416106801757269036 + 0.52359877559829887308j)
  5485. >>> primezeta(0.5+10j)
  5486. (0.54892423556409790529 + 0.45626803423487934264j)
  5487. Although evaluation works in principle for any `\mathrm{Re}(s) > 0`,
  5488. it should be noted that the evaluation time increases exponentially
  5489. as `s` approaches the imaginary axis.
  5490. For large `\mathrm{Re}(s)`, `P(s)` is asymptotic to `2^{-s}`::
  5491. >>> primezeta(inf)
  5492. 0.0
  5493. >>> primezeta(10), mpf(2)**-10
  5494. (0.00099360357443698021786, 0.0009765625)
  5495. >>> primezeta(1000)
  5496. 9.3326361850321887899e-302
  5497. >>> primezeta(1000+1000j)
  5498. (-3.8565440833654995949e-302 - 8.4985390447553234305e-302j)
  5499. **References**
  5500. Carl-Erik Froberg, "On the prime zeta function",
  5501. BIT 8 (1968), pp. 187-202.
  5502. """
  5503. bernpoly = r"""
  5504. Evaluates the Bernoulli polynomial `B_n(z)`.
  5505. The first few Bernoulli polynomials are::
  5506. >>> from mpmath import *
  5507. >>> mp.dps = 15; mp.pretty = True
  5508. >>> for n in range(6):
  5509. ... nprint(chop(taylor(lambda x: bernpoly(n,x), 0, n)))
  5510. ...
  5511. [1.0]
  5512. [-0.5, 1.0]
  5513. [0.166667, -1.0, 1.0]
  5514. [0.0, 0.5, -1.5, 1.0]
  5515. [-0.0333333, 0.0, 1.0, -2.0, 1.0]
  5516. [0.0, -0.166667, 0.0, 1.66667, -2.5, 1.0]
  5517. At `z = 0`, the Bernoulli polynomial evaluates to a
  5518. Bernoulli number (see :func:`~mpmath.bernoulli`)::
  5519. >>> bernpoly(12, 0), bernoulli(12)
  5520. (-0.253113553113553, -0.253113553113553)
  5521. >>> bernpoly(13, 0), bernoulli(13)
  5522. (0.0, 0.0)
  5523. Evaluation is accurate for large `n` and small `z`::
  5524. >>> mp.dps = 25
  5525. >>> bernpoly(100, 0.5)
  5526. 2.838224957069370695926416e+78
  5527. >>> bernpoly(1000, 10.5)
  5528. 5.318704469415522036482914e+1769
  5529. """
  5530. polylog = r"""
  5531. Computes the polylogarithm, defined by the sum
  5532. .. math ::
  5533. \mathrm{Li}_s(z) = \sum_{k=1}^{\infty} \frac{z^k}{k^s}.
  5534. This series is convergent only for `|z| < 1`, so elsewhere
  5535. the analytic continuation is implied.
  5536. The polylogarithm should not be confused with the logarithmic
  5537. integral (also denoted by Li or li), which is implemented
  5538. as :func:`~mpmath.li`.
  5539. **Examples**
  5540. The polylogarithm satisfies a huge number of functional identities.
  5541. A sample of polylogarithm evaluations is shown below::
  5542. >>> from mpmath import *
  5543. >>> mp.dps = 15; mp.pretty = True
  5544. >>> polylog(1,0.5), log(2)
  5545. (0.693147180559945, 0.693147180559945)
  5546. >>> polylog(2,0.5), (pi**2-6*log(2)**2)/12
  5547. (0.582240526465012, 0.582240526465012)
  5548. >>> polylog(2,-phi), -log(phi)**2-pi**2/10
  5549. (-1.21852526068613, -1.21852526068613)
  5550. >>> polylog(3,0.5), 7*zeta(3)/8-pi**2*log(2)/12+log(2)**3/6
  5551. (0.53721319360804, 0.53721319360804)
  5552. :func:`~mpmath.polylog` can evaluate the analytic continuation of the
  5553. polylogarithm when `s` is an integer::
  5554. >>> polylog(2, 10)
  5555. (0.536301287357863 - 7.23378441241546j)
  5556. >>> polylog(2, -10)
  5557. -4.1982778868581
  5558. >>> polylog(2, 10j)
  5559. (-3.05968879432873 + 3.71678149306807j)
  5560. >>> polylog(-2, 10)
  5561. -0.150891632373114
  5562. >>> polylog(-2, -10)
  5563. 0.067618332081142
  5564. >>> polylog(-2, 10j)
  5565. (0.0384353698579347 + 0.0912451798066779j)
  5566. Some more examples, with arguments on the unit circle (note that
  5567. the series definition cannot be used for computation here)::
  5568. >>> polylog(2,j)
  5569. (-0.205616758356028 + 0.915965594177219j)
  5570. >>> j*catalan-pi**2/48
  5571. (-0.205616758356028 + 0.915965594177219j)
  5572. >>> polylog(3,exp(2*pi*j/3))
  5573. (-0.534247512515375 + 0.765587078525922j)
  5574. >>> -4*zeta(3)/9 + 2*j*pi**3/81
  5575. (-0.534247512515375 + 0.765587078525921j)
  5576. Polylogarithms of different order are related by integration
  5577. and differentiation::
  5578. >>> s, z = 3, 0.5
  5579. >>> polylog(s+1, z)
  5580. 0.517479061673899
  5581. >>> quad(lambda t: polylog(s,t)/t, [0, z])
  5582. 0.517479061673899
  5583. >>> z*diff(lambda t: polylog(s+2,t), z)
  5584. 0.517479061673899
  5585. Taylor series expansions around `z = 0` are::
  5586. >>> for n in range(-3, 4):
  5587. ... nprint(taylor(lambda x: polylog(n,x), 0, 5))
  5588. ...
  5589. [0.0, 1.0, 8.0, 27.0, 64.0, 125.0]
  5590. [0.0, 1.0, 4.0, 9.0, 16.0, 25.0]
  5591. [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
  5592. [0.0, 1.0, 1.0, 1.0, 1.0, 1.0]
  5593. [0.0, 1.0, 0.5, 0.333333, 0.25, 0.2]
  5594. [0.0, 1.0, 0.25, 0.111111, 0.0625, 0.04]
  5595. [0.0, 1.0, 0.125, 0.037037, 0.015625, 0.008]
  5596. The series defining the polylogarithm is simultaneously
  5597. a Taylor series and an L-series. For certain values of `z`, the
  5598. polylogarithm reduces to a pure zeta function::
  5599. >>> polylog(pi, 1), zeta(pi)
  5600. (1.17624173838258, 1.17624173838258)
  5601. >>> polylog(pi, -1), -altzeta(pi)
  5602. (-0.909670702980385, -0.909670702980385)
  5603. Evaluation for arbitrary, nonintegral `s` is supported
  5604. for `z` within the unit circle:
  5605. >>> polylog(3+4j, 0.25)
  5606. (0.24258605789446 - 0.00222938275488344j)
  5607. >>> nsum(lambda k: 0.25**k / k**(3+4j), [1,inf])
  5608. (0.24258605789446 - 0.00222938275488344j)
  5609. It is also supported outside of the unit circle::
  5610. >>> polylog(1+j, 20+40j)
  5611. (-7.1421172179728 - 3.92726697721369j)
  5612. >>> polylog(1+j, 200+400j)
  5613. (-5.41934747194626 - 9.94037752563927j)
  5614. **References**
  5615. 1. Richard Crandall, "Note on fast polylogarithm computation"
  5616. http://www.reed.edu/physics/faculty/crandall/papers/Polylog.pdf
  5617. 2. http://en.wikipedia.org/wiki/Polylogarithm
  5618. 3. http://mathworld.wolfram.com/Polylogarithm.html
  5619. """
  5620. bell = r"""
  5621. For `n` a nonnegative integer, ``bell(n,x)`` evaluates the Bell
  5622. polynomial `B_n(x)`, the first few of which are
  5623. .. math ::
  5624. B_0(x) = 1
  5625. B_1(x) = x
  5626. B_2(x) = x^2+x
  5627. B_3(x) = x^3+3x^2+x
  5628. If `x = 1` or :func:`~mpmath.bell` is called with only one argument, it
  5629. gives the `n`-th Bell number `B_n`, which is the number of
  5630. partitions of a set with `n` elements. By setting the precision to
  5631. at least `\log_{10} B_n` digits, :func:`~mpmath.bell` provides fast
  5632. calculation of exact Bell numbers.
  5633. In general, :func:`~mpmath.bell` computes
  5634. .. math ::
  5635. B_n(x) = e^{-x} \left(\mathrm{sinc}(\pi n) + E_n(x)\right)
  5636. where `E_n(x)` is the generalized exponential function implemented
  5637. by :func:`~mpmath.polyexp`. This is an extension of Dobinski's formula [1],
  5638. where the modification is the sinc term ensuring that `B_n(x)` is
  5639. continuous in `n`; :func:`~mpmath.bell` can thus be evaluated,
  5640. differentiated, etc for arbitrary complex arguments.
  5641. **Examples**
  5642. Simple evaluations::
  5643. >>> from mpmath import *
  5644. >>> mp.dps = 25; mp.pretty = True
  5645. >>> bell(0, 2.5)
  5646. 1.0
  5647. >>> bell(1, 2.5)
  5648. 2.5
  5649. >>> bell(2, 2.5)
  5650. 8.75
  5651. Evaluation for arbitrary complex arguments::
  5652. >>> bell(5.75+1j, 2-3j)
  5653. (-10767.71345136587098445143 - 15449.55065599872579097221j)
  5654. The first few Bell polynomials::
  5655. >>> for k in range(7):
  5656. ... nprint(taylor(lambda x: bell(k,x), 0, k))
  5657. ...
  5658. [1.0]
  5659. [0.0, 1.0]
  5660. [0.0, 1.0, 1.0]
  5661. [0.0, 1.0, 3.0, 1.0]
  5662. [0.0, 1.0, 7.0, 6.0, 1.0]
  5663. [0.0, 1.0, 15.0, 25.0, 10.0, 1.0]
  5664. [0.0, 1.0, 31.0, 90.0, 65.0, 15.0, 1.0]
  5665. The first few Bell numbers and complementary Bell numbers::
  5666. >>> [int(bell(k)) for k in range(10)]
  5667. [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147]
  5668. >>> [int(bell(k,-1)) for k in range(10)]
  5669. [1, -1, 0, 1, 1, -2, -9, -9, 50, 267]
  5670. Large Bell numbers::
  5671. >>> mp.dps = 50
  5672. >>> bell(50)
  5673. 185724268771078270438257767181908917499221852770.0
  5674. >>> bell(50,-1)
  5675. -29113173035759403920216141265491160286912.0
  5676. Some even larger values::
  5677. >>> mp.dps = 25
  5678. >>> bell(1000,-1)
  5679. -1.237132026969293954162816e+1869
  5680. >>> bell(1000)
  5681. 2.989901335682408421480422e+1927
  5682. >>> bell(1000,2)
  5683. 6.591553486811969380442171e+1987
  5684. >>> bell(1000,100.5)
  5685. 9.101014101401543575679639e+2529
  5686. A determinant identity satisfied by Bell numbers::
  5687. >>> mp.dps = 15
  5688. >>> N = 8
  5689. >>> det([[bell(k+j) for j in range(N)] for k in range(N)])
  5690. 125411328000.0
  5691. >>> superfac(N-1)
  5692. 125411328000.0
  5693. **References**
  5694. 1. http://mathworld.wolfram.com/DobinskisFormula.html
  5695. """
  5696. polyexp = r"""
  5697. Evaluates the polyexponential function, defined for arbitrary
  5698. complex `s`, `z` by the series
  5699. .. math ::
  5700. E_s(z) = \sum_{k=1}^{\infty} \frac{k^s}{k!} z^k.
  5701. `E_s(z)` is constructed from the exponential function analogously
  5702. to how the polylogarithm is constructed from the ordinary
  5703. logarithm; as a function of `s` (with `z` fixed), `E_s` is an L-series
  5704. It is an entire function of both `s` and `z`.
  5705. The polyexponential function provides a generalization of the
  5706. Bell polynomials `B_n(x)` (see :func:`~mpmath.bell`) to noninteger orders `n`.
  5707. In terms of the Bell polynomials,
  5708. .. math ::
  5709. E_s(z) = e^z B_s(z) - \mathrm{sinc}(\pi s).
  5710. Note that `B_n(x)` and `e^{-x} E_n(x)` are identical if `n`
  5711. is a nonzero integer, but not otherwise. In particular, they differ
  5712. at `n = 0`.
  5713. **Examples**
  5714. Evaluating a series::
  5715. >>> from mpmath import *
  5716. >>> mp.dps = 25; mp.pretty = True
  5717. >>> nsum(lambda k: sqrt(k)/fac(k), [1,inf])
  5718. 2.101755547733791780315904
  5719. >>> polyexp(0.5,1)
  5720. 2.101755547733791780315904
  5721. Evaluation for arbitrary arguments::
  5722. >>> polyexp(-3-4j, 2.5+2j)
  5723. (2.351660261190434618268706 + 1.202966666673054671364215j)
  5724. Evaluation is accurate for tiny function values::
  5725. >>> polyexp(4, -100)
  5726. 3.499471750566824369520223e-36
  5727. If `n` is a nonpositive integer, `E_n` reduces to a special
  5728. instance of the hypergeometric function `\,_pF_q`::
  5729. >>> n = 3
  5730. >>> x = pi
  5731. >>> polyexp(-n,x)
  5732. 4.042192318847986561771779
  5733. >>> x*hyper([1]*(n+1), [2]*(n+1), x)
  5734. 4.042192318847986561771779
  5735. """
  5736. cyclotomic = r"""
  5737. Evaluates the cyclotomic polynomial `\Phi_n(x)`, defined by
  5738. .. math ::
  5739. \Phi_n(x) = \prod_{\zeta} (x - \zeta)
  5740. where `\zeta` ranges over all primitive `n`-th roots of unity
  5741. (see :func:`~mpmath.unitroots`). An equivalent representation, used
  5742. for computation, is
  5743. .. math ::
  5744. \Phi_n(x) = \prod_{d\mid n}(x^d-1)^{\mu(n/d)} = \Phi_n(x)
  5745. where `\mu(m)` denotes the Moebius function. The cyclotomic
  5746. polynomials are integer polynomials, the first of which can be
  5747. written explicitly as
  5748. .. math ::
  5749. \Phi_0(x) = 1
  5750. \Phi_1(x) = x - 1
  5751. \Phi_2(x) = x + 1
  5752. \Phi_3(x) = x^3 + x^2 + 1
  5753. \Phi_4(x) = x^2 + 1
  5754. \Phi_5(x) = x^4 + x^3 + x^2 + x + 1
  5755. \Phi_6(x) = x^2 - x + 1
  5756. **Examples**
  5757. The coefficients of low-order cyclotomic polynomials can be recovered
  5758. using Taylor expansion::
  5759. >>> from mpmath import *
  5760. >>> mp.dps = 15; mp.pretty = True
  5761. >>> for n in range(9):
  5762. ... p = chop(taylor(lambda x: cyclotomic(n,x), 0, 10))
  5763. ... print("%s %s" % (n, nstr(p[:10+1-p[::-1].index(1)])))
  5764. ...
  5765. 0 [1.0]
  5766. 1 [-1.0, 1.0]
  5767. 2 [1.0, 1.0]
  5768. 3 [1.0, 1.0, 1.0]
  5769. 4 [1.0, 0.0, 1.0]
  5770. 5 [1.0, 1.0, 1.0, 1.0, 1.0]
  5771. 6 [1.0, -1.0, 1.0]
  5772. 7 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
  5773. 8 [1.0, 0.0, 0.0, 0.0, 1.0]
  5774. The definition as a product over primitive roots may be checked
  5775. by computing the product explicitly (for a real argument, this
  5776. method will generally introduce numerical noise in the imaginary
  5777. part)::
  5778. >>> mp.dps = 25
  5779. >>> z = 3+4j
  5780. >>> cyclotomic(10, z)
  5781. (-419.0 - 360.0j)
  5782. >>> fprod(z-r for r in unitroots(10, primitive=True))
  5783. (-419.0 - 360.0j)
  5784. >>> z = 3
  5785. >>> cyclotomic(10, z)
  5786. 61.0
  5787. >>> fprod(z-r for r in unitroots(10, primitive=True))
  5788. (61.0 - 3.146045605088568607055454e-25j)
  5789. Up to permutation, the roots of a given cyclotomic polynomial
  5790. can be checked to agree with the list of primitive roots::
  5791. >>> p = taylor(lambda x: cyclotomic(6,x), 0, 6)[:3]
  5792. >>> for r in polyroots(p[::-1]):
  5793. ... print(r)
  5794. ...
  5795. (0.5 - 0.8660254037844386467637232j)
  5796. (0.5 + 0.8660254037844386467637232j)
  5797. >>>
  5798. >>> for r in unitroots(6, primitive=True):
  5799. ... print(r)
  5800. ...
  5801. (0.5 + 0.8660254037844386467637232j)
  5802. (0.5 - 0.8660254037844386467637232j)
  5803. """
  5804. meijerg = r"""
  5805. Evaluates the Meijer G-function, defined as
  5806. .. math ::
  5807. G^{m,n}_{p,q} \left( \left. \begin{matrix}
  5808. a_1, \dots, a_n ; a_{n+1} \dots a_p \\
  5809. b_1, \dots, b_m ; b_{m+1} \dots b_q
  5810. \end{matrix}\; \right| \; z ; r \right) =
  5811. \frac{1}{2 \pi i} \int_L
  5812. \frac{\prod_{j=1}^m \Gamma(b_j+s) \prod_{j=1}^n\Gamma(1-a_j-s)}
  5813. {\prod_{j=n+1}^{p}\Gamma(a_j+s) \prod_{j=m+1}^q \Gamma(1-b_j-s)}
  5814. z^{-s/r} ds
  5815. for an appropriate choice of the contour `L` (see references).
  5816. There are `p` elements `a_j`.
  5817. The argument *a_s* should be a pair of lists, the first containing the
  5818. `n` elements `a_1, \ldots, a_n` and the second containing
  5819. the `p-n` elements `a_{n+1}, \ldots a_p`.
  5820. There are `q` elements `b_j`.
  5821. The argument *b_s* should be a pair of lists, the first containing the
  5822. `m` elements `b_1, \ldots, b_m` and the second containing
  5823. the `q-m` elements `b_{m+1}, \ldots b_q`.
  5824. The implicit tuple `(m, n, p, q)` constitutes the order or degree of the
  5825. Meijer G-function, and is determined by the lengths of the coefficient
  5826. vectors. Confusingly, the indices in this tuple appear in a different order
  5827. from the coefficients, but this notation is standard. The many examples
  5828. given below should hopefully clear up any potential confusion.
  5829. **Algorithm**
  5830. The Meijer G-function is evaluated as a combination of hypergeometric series.
  5831. There are two versions of the function, which can be selected with
  5832. the optional *series* argument.
  5833. *series=1* uses a sum of `m` `\,_pF_{q-1}` functions of `z`
  5834. *series=2* uses a sum of `n` `\,_qF_{p-1}` functions of `1/z`
  5835. The default series is chosen based on the degree and `|z|` in order
  5836. to be consistent with Mathematica's. This definition of the Meijer G-function
  5837. has a discontinuity at `|z| = 1` for some orders, which can
  5838. be avoided by explicitly specifying a series.
  5839. Keyword arguments are forwarded to :func:`~mpmath.hypercomb`.
  5840. **Examples**
  5841. Many standard functions are special cases of the Meijer G-function
  5842. (possibly rescaled and/or with branch cut corrections). We define
  5843. some test parameters::
  5844. >>> from mpmath import *
  5845. >>> mp.dps = 25; mp.pretty = True
  5846. >>> a = mpf(0.75)
  5847. >>> b = mpf(1.5)
  5848. >>> z = mpf(2.25)
  5849. The exponential function:
  5850. `e^z = G^{1,0}_{0,1} \left( \left. \begin{matrix} - \\ 0 \end{matrix} \;
  5851. \right| \; -z \right)`
  5852. >>> meijerg([[],[]], [[0],[]], -z)
  5853. 9.487735836358525720550369
  5854. >>> exp(z)
  5855. 9.487735836358525720550369
  5856. The natural logarithm:
  5857. `\log(1+z) = G^{1,2}_{2,2} \left( \left. \begin{matrix} 1, 1 \\ 1, 0
  5858. \end{matrix} \; \right| \; -z \right)`
  5859. >>> meijerg([[1,1],[]], [[1],[0]], z)
  5860. 1.178654996341646117219023
  5861. >>> log(1+z)
  5862. 1.178654996341646117219023
  5863. A rational function:
  5864. `\frac{z}{z+1} = G^{1,2}_{2,2} \left( \left. \begin{matrix} 1, 1 \\ 1, 1
  5865. \end{matrix} \; \right| \; z \right)`
  5866. >>> meijerg([[1,1],[]], [[1],[1]], z)
  5867. 0.6923076923076923076923077
  5868. >>> z/(z+1)
  5869. 0.6923076923076923076923077
  5870. The sine and cosine functions:
  5871. `\frac{1}{\sqrt \pi} \sin(2 \sqrt z) = G^{1,0}_{0,2} \left( \left. \begin{matrix}
  5872. - \\ \frac{1}{2}, 0 \end{matrix} \; \right| \; z \right)`
  5873. `\frac{1}{\sqrt \pi} \cos(2 \sqrt z) = G^{1,0}_{0,2} \left( \left. \begin{matrix}
  5874. - \\ 0, \frac{1}{2} \end{matrix} \; \right| \; z \right)`
  5875. >>> meijerg([[],[]], [[0.5],[0]], (z/2)**2)
  5876. 0.4389807929218676682296453
  5877. >>> sin(z)/sqrt(pi)
  5878. 0.4389807929218676682296453
  5879. >>> meijerg([[],[]], [[0],[0.5]], (z/2)**2)
  5880. -0.3544090145996275423331762
  5881. >>> cos(z)/sqrt(pi)
  5882. -0.3544090145996275423331762
  5883. Bessel functions:
  5884. `J_a(2 \sqrt z) = G^{1,0}_{0,2} \left( \left.
  5885. \begin{matrix} - \\ \frac{a}{2}, -\frac{a}{2}
  5886. \end{matrix} \; \right| \; z \right)`
  5887. `Y_a(2 \sqrt z) = G^{2,0}_{1,3} \left( \left.
  5888. \begin{matrix} \frac{-a-1}{2} \\ \frac{a}{2}, -\frac{a}{2}, \frac{-a-1}{2}
  5889. \end{matrix} \; \right| \; z \right)`
  5890. `(-z)^{a/2} z^{-a/2} I_a(2 \sqrt z) = G^{1,0}_{0,2} \left( \left.
  5891. \begin{matrix} - \\ \frac{a}{2}, -\frac{a}{2}
  5892. \end{matrix} \; \right| \; -z \right)`
  5893. `2 K_a(2 \sqrt z) = G^{2,0}_{0,2} \left( \left.
  5894. \begin{matrix} - \\ \frac{a}{2}, -\frac{a}{2}
  5895. \end{matrix} \; \right| \; z \right)`
  5896. As the example with the Bessel *I* function shows, a branch
  5897. factor is required for some arguments when inverting the square root.
  5898. >>> meijerg([[],[]], [[a/2],[-a/2]], (z/2)**2)
  5899. 0.5059425789597154858527264
  5900. >>> besselj(a,z)
  5901. 0.5059425789597154858527264
  5902. >>> meijerg([[],[(-a-1)/2]], [[a/2,-a/2],[(-a-1)/2]], (z/2)**2)
  5903. 0.1853868950066556941442559
  5904. >>> bessely(a, z)
  5905. 0.1853868950066556941442559
  5906. >>> meijerg([[],[]], [[a/2],[-a/2]], -(z/2)**2)
  5907. (0.8685913322427653875717476 + 2.096964974460199200551738j)
  5908. >>> (-z)**(a/2) / z**(a/2) * besseli(a, z)
  5909. (0.8685913322427653875717476 + 2.096964974460199200551738j)
  5910. >>> 0.5*meijerg([[],[]], [[a/2,-a/2],[]], (z/2)**2)
  5911. 0.09334163695597828403796071
  5912. >>> besselk(a,z)
  5913. 0.09334163695597828403796071
  5914. Error functions:
  5915. `\sqrt{\pi} z^{2(a-1)} \mathrm{erfc}(z) = G^{2,0}_{1,2} \left( \left.
  5916. \begin{matrix} a \\ a-1, a-\frac{1}{2}
  5917. \end{matrix} \; \right| \; z, \frac{1}{2} \right)`
  5918. >>> meijerg([[],[a]], [[a-1,a-0.5],[]], z, 0.5)
  5919. 0.00172839843123091957468712
  5920. >>> sqrt(pi) * z**(2*a-2) * erfc(z)
  5921. 0.00172839843123091957468712
  5922. A Meijer G-function of higher degree, (1,1,2,3):
  5923. >>> meijerg([[a],[b]], [[a],[b,a-1]], z)
  5924. 1.55984467443050210115617
  5925. >>> sin((b-a)*pi)/pi*(exp(z)-1)*z**(a-1)
  5926. 1.55984467443050210115617
  5927. A Meijer G-function of still higher degree, (4,1,2,4), that can
  5928. be expanded as a messy combination of exponential integrals:
  5929. >>> meijerg([[a],[2*b-a]], [[b,a,b-0.5,-1-a+2*b],[]], z)
  5930. 0.3323667133658557271898061
  5931. >>> chop(4**(a-b+1)*sqrt(pi)*gamma(2*b-2*a)*z**a*\
  5932. ... expint(2*b-2*a, -2*sqrt(-z))*expint(2*b-2*a, 2*sqrt(-z)))
  5933. 0.3323667133658557271898061
  5934. In the following case, different series give different values::
  5935. >>> chop(meijerg([[1],[0.25]],[[3],[0.5]],-2))
  5936. -0.06417628097442437076207337
  5937. >>> meijerg([[1],[0.25]],[[3],[0.5]],-2,series=1)
  5938. 0.1428699426155117511873047
  5939. >>> chop(meijerg([[1],[0.25]],[[3],[0.5]],-2,series=2))
  5940. -0.06417628097442437076207337
  5941. **References**
  5942. 1. http://en.wikipedia.org/wiki/Meijer_G-function
  5943. 2. http://mathworld.wolfram.com/MeijerG-Function.html
  5944. 3. http://functions.wolfram.com/HypergeometricFunctions/MeijerG/
  5945. 4. http://functions.wolfram.com/HypergeometricFunctions/MeijerG1/
  5946. """
  5947. clsin = r"""
  5948. Computes the Clausen sine function, defined formally by the series
  5949. .. math ::
  5950. \mathrm{Cl}_s(z) = \sum_{k=1}^{\infty} \frac{\sin(kz)}{k^s}.
  5951. The special case `\mathrm{Cl}_2(z)` (i.e. ``clsin(2,z)``) is the classical
  5952. "Clausen function". More generally, the Clausen function is defined for
  5953. complex `s` and `z`, even when the series does not converge. The
  5954. Clausen function is related to the polylogarithm (:func:`~mpmath.polylog`) as
  5955. .. math ::
  5956. \mathrm{Cl}_s(z) = \frac{1}{2i}\left(\mathrm{Li}_s\left(e^{iz}\right) -
  5957. \mathrm{Li}_s\left(e^{-iz}\right)\right)
  5958. = \mathrm{Im}\left[\mathrm{Li}_s(e^{iz})\right] \quad (s, z \in \mathbb{R}),
  5959. and this representation can be taken to provide the analytic continuation of the
  5960. series. The complementary function :func:`~mpmath.clcos` gives the corresponding
  5961. cosine sum.
  5962. **Examples**
  5963. Evaluation for arbitrarily chosen `s` and `z`::
  5964. >>> from mpmath import *
  5965. >>> mp.dps = 25; mp.pretty = True
  5966. >>> s, z = 3, 4
  5967. >>> clsin(s, z); nsum(lambda k: sin(z*k)/k**s, [1,inf])
  5968. -0.6533010136329338746275795
  5969. -0.6533010136329338746275795
  5970. Using `z + \pi` instead of `z` gives an alternating series::
  5971. >>> clsin(s, z+pi)
  5972. 0.8860032351260589402871624
  5973. >>> nsum(lambda k: (-1)**k*sin(z*k)/k**s, [1,inf])
  5974. 0.8860032351260589402871624
  5975. With `s = 1`, the sum can be expressed in closed form
  5976. using elementary functions::
  5977. >>> z = 1 + sqrt(3)
  5978. >>> clsin(1, z)
  5979. 0.2047709230104579724675985
  5980. >>> chop((log(1-exp(-j*z)) - log(1-exp(j*z)))/(2*j))
  5981. 0.2047709230104579724675985
  5982. >>> nsum(lambda k: sin(k*z)/k, [1,inf])
  5983. 0.2047709230104579724675985
  5984. The classical Clausen function `\mathrm{Cl}_2(\theta)` gives the
  5985. value of the integral `\int_0^{\theta} -\ln(2\sin(x/2)) dx` for
  5986. `0 < \theta < 2 \pi`::
  5987. >>> cl2 = lambda t: clsin(2, t)
  5988. >>> cl2(3.5)
  5989. -0.2465045302347694216534255
  5990. >>> -quad(lambda x: ln(2*sin(0.5*x)), [0, 3.5])
  5991. -0.2465045302347694216534255
  5992. This function is symmetric about `\theta = \pi` with zeros and extreme
  5993. points::
  5994. >>> cl2(0); cl2(pi/3); chop(cl2(pi)); cl2(5*pi/3); chop(cl2(2*pi))
  5995. 0.0
  5996. 1.014941606409653625021203
  5997. 0.0
  5998. -1.014941606409653625021203
  5999. 0.0
  6000. Catalan's constant is a special value::
  6001. >>> cl2(pi/2)
  6002. 0.9159655941772190150546035
  6003. >>> +catalan
  6004. 0.9159655941772190150546035
  6005. The Clausen sine function can be expressed in closed form when
  6006. `s` is an odd integer (becoming zero when `s` < 0)::
  6007. >>> z = 1 + sqrt(2)
  6008. >>> clsin(1, z); (pi-z)/2
  6009. 0.3636895456083490948304773
  6010. 0.3636895456083490948304773
  6011. >>> clsin(3, z); pi**2/6*z - pi*z**2/4 + z**3/12
  6012. 0.5661751584451144991707161
  6013. 0.5661751584451144991707161
  6014. >>> clsin(-1, z)
  6015. 0.0
  6016. >>> clsin(-3, z)
  6017. 0.0
  6018. It can also be expressed in closed form for even integer `s \le 0`,
  6019. providing a finite sum for series such as
  6020. `\sin(z) + \sin(2z) + \sin(3z) + \ldots`::
  6021. >>> z = 1 + sqrt(2)
  6022. >>> clsin(0, z)
  6023. 0.1903105029507513881275865
  6024. >>> cot(z/2)/2
  6025. 0.1903105029507513881275865
  6026. >>> clsin(-2, z)
  6027. -0.1089406163841548817581392
  6028. >>> -cot(z/2)*csc(z/2)**2/4
  6029. -0.1089406163841548817581392
  6030. Call with ``pi=True`` to multiply `z` by `\pi` exactly::
  6031. >>> clsin(3, 3*pi)
  6032. -8.892316224968072424732898e-26
  6033. >>> clsin(3, 3, pi=True)
  6034. 0.0
  6035. Evaluation for complex `s`, `z` in a nonconvergent case::
  6036. >>> s, z = -1-j, 1+2j
  6037. >>> clsin(s, z)
  6038. (-0.593079480117379002516034 + 0.9038644233367868273362446j)
  6039. >>> extraprec(20)(nsum)(lambda k: sin(k*z)/k**s, [1,inf])
  6040. (-0.593079480117379002516034 + 0.9038644233367868273362446j)
  6041. """
  6042. clcos = r"""
  6043. Computes the Clausen cosine function, defined formally by the series
  6044. .. math ::
  6045. \mathrm{\widetilde{Cl}}_s(z) = \sum_{k=1}^{\infty} \frac{\cos(kz)}{k^s}.
  6046. This function is complementary to the Clausen sine function
  6047. :func:`~mpmath.clsin`. In terms of the polylogarithm,
  6048. .. math ::
  6049. \mathrm{\widetilde{Cl}}_s(z) =
  6050. \frac{1}{2}\left(\mathrm{Li}_s\left(e^{iz}\right) +
  6051. \mathrm{Li}_s\left(e^{-iz}\right)\right)
  6052. = \mathrm{Re}\left[\mathrm{Li}_s(e^{iz})\right] \quad (s, z \in \mathbb{R}).
  6053. **Examples**
  6054. Evaluation for arbitrarily chosen `s` and `z`::
  6055. >>> from mpmath import *
  6056. >>> mp.dps = 25; mp.pretty = True
  6057. >>> s, z = 3, 4
  6058. >>> clcos(s, z); nsum(lambda k: cos(z*k)/k**s, [1,inf])
  6059. -0.6518926267198991308332759
  6060. -0.6518926267198991308332759
  6061. Using `z + \pi` instead of `z` gives an alternating series::
  6062. >>> s, z = 3, 0.5
  6063. >>> clcos(s, z+pi)
  6064. -0.8155530586502260817855618
  6065. >>> nsum(lambda k: (-1)**k*cos(z*k)/k**s, [1,inf])
  6066. -0.8155530586502260817855618
  6067. With `s = 1`, the sum can be expressed in closed form
  6068. using elementary functions::
  6069. >>> z = 1 + sqrt(3)
  6070. >>> clcos(1, z)
  6071. -0.6720334373369714849797918
  6072. >>> chop(-0.5*(log(1-exp(j*z))+log(1-exp(-j*z))))
  6073. -0.6720334373369714849797918
  6074. >>> -log(abs(2*sin(0.5*z))) # Equivalent to above when z is real
  6075. -0.6720334373369714849797918
  6076. >>> nsum(lambda k: cos(k*z)/k, [1,inf])
  6077. -0.6720334373369714849797918
  6078. It can also be expressed in closed form when `s` is an even integer.
  6079. For example,
  6080. >>> clcos(2,z)
  6081. -0.7805359025135583118863007
  6082. >>> pi**2/6 - pi*z/2 + z**2/4
  6083. -0.7805359025135583118863007
  6084. The case `s = 0` gives the renormalized sum of
  6085. `\cos(z) + \cos(2z) + \cos(3z) + \ldots` (which happens to be the same for
  6086. any value of `z`)::
  6087. >>> clcos(0, z)
  6088. -0.5
  6089. >>> nsum(lambda k: cos(k*z), [1,inf])
  6090. -0.5
  6091. Also the sums
  6092. .. math ::
  6093. \cos(z) + 2\cos(2z) + 3\cos(3z) + \ldots
  6094. and
  6095. .. math ::
  6096. \cos(z) + 2^n \cos(2z) + 3^n \cos(3z) + \ldots
  6097. for higher integer powers `n = -s` can be done in closed form. They are zero
  6098. when `n` is positive and even (`s` negative and even)::
  6099. >>> clcos(-1, z); 1/(2*cos(z)-2)
  6100. -0.2607829375240542480694126
  6101. -0.2607829375240542480694126
  6102. >>> clcos(-3, z); (2+cos(z))*csc(z/2)**4/8
  6103. 0.1472635054979944390848006
  6104. 0.1472635054979944390848006
  6105. >>> clcos(-2, z); clcos(-4, z); clcos(-6, z)
  6106. 0.0
  6107. 0.0
  6108. 0.0
  6109. With `z = \pi`, the series reduces to that of the Riemann zeta function
  6110. (more generally, if `z = p \pi/q`, it is a finite sum over Hurwitz zeta
  6111. function values)::
  6112. >>> clcos(2.5, 0); zeta(2.5)
  6113. 1.34148725725091717975677
  6114. 1.34148725725091717975677
  6115. >>> clcos(2.5, pi); -altzeta(2.5)
  6116. -0.8671998890121841381913472
  6117. -0.8671998890121841381913472
  6118. Call with ``pi=True`` to multiply `z` by `\pi` exactly::
  6119. >>> clcos(-3, 2*pi)
  6120. 2.997921055881167659267063e+102
  6121. >>> clcos(-3, 2, pi=True)
  6122. 0.008333333333333333333333333
  6123. Evaluation for complex `s`, `z` in a nonconvergent case::
  6124. >>> s, z = -1-j, 1+2j
  6125. >>> clcos(s, z)
  6126. (0.9407430121562251476136807 + 0.715826296033590204557054j)
  6127. >>> extraprec(20)(nsum)(lambda k: cos(k*z)/k**s, [1,inf])
  6128. (0.9407430121562251476136807 + 0.715826296033590204557054j)
  6129. """
  6130. whitm = r"""
  6131. Evaluates the Whittaker function `M(k,m,z)`, which gives a solution
  6132. to the Whittaker differential equation
  6133. .. math ::
  6134. \frac{d^2f}{dz^2} + \left(-\frac{1}{4}+\frac{k}{z}+
  6135. \frac{(\frac{1}{4}-m^2)}{z^2}\right) f = 0.
  6136. A second solution is given by :func:`~mpmath.whitw`.
  6137. The Whittaker functions are defined in Abramowitz & Stegun, section 13.1.
  6138. They are alternate forms of the confluent hypergeometric functions
  6139. `\,_1F_1` and `U`:
  6140. .. math ::
  6141. M(k,m,z) = e^{-\frac{1}{2}z} z^{\frac{1}{2}+m}
  6142. \,_1F_1(\tfrac{1}{2}+m-k, 1+2m, z)
  6143. W(k,m,z) = e^{-\frac{1}{2}z} z^{\frac{1}{2}+m}
  6144. U(\tfrac{1}{2}+m-k, 1+2m, z).
  6145. **Examples**
  6146. Evaluation for arbitrary real and complex arguments is supported::
  6147. >>> from mpmath import *
  6148. >>> mp.dps = 25; mp.pretty = True
  6149. >>> whitm(1, 1, 1)
  6150. 0.7302596799460411820509668
  6151. >>> whitm(1, 1, -1)
  6152. (0.0 - 1.417977827655098025684246j)
  6153. >>> whitm(j, j/2, 2+3j)
  6154. (3.245477713363581112736478 - 0.822879187542699127327782j)
  6155. >>> whitm(2, 3, 100000)
  6156. 4.303985255686378497193063e+21707
  6157. Evaluation at zero::
  6158. >>> whitm(1,-1,0); whitm(1,-0.5,0); whitm(1,0,0)
  6159. +inf
  6160. nan
  6161. 0.0
  6162. We can verify that :func:`~mpmath.whitm` numerically satisfies the
  6163. differential equation for arbitrarily chosen values::
  6164. >>> k = mpf(0.25)
  6165. >>> m = mpf(1.5)
  6166. >>> f = lambda z: whitm(k,m,z)
  6167. >>> for z in [-1, 2.5, 3, 1+2j]:
  6168. ... chop(diff(f,z,2) + (-0.25 + k/z + (0.25-m**2)/z**2)*f(z))
  6169. ...
  6170. 0.0
  6171. 0.0
  6172. 0.0
  6173. 0.0
  6174. An integral involving both :func:`~mpmath.whitm` and :func:`~mpmath.whitw`,
  6175. verifying evaluation along the real axis::
  6176. >>> quad(lambda x: exp(-x)*whitm(3,2,x)*whitw(1,-2,x), [0,inf])
  6177. 3.438869842576800225207341
  6178. >>> 128/(21*sqrt(pi))
  6179. 3.438869842576800225207341
  6180. """
  6181. whitw = r"""
  6182. Evaluates the Whittaker function `W(k,m,z)`, which gives a second
  6183. solution to the Whittaker differential equation. (See :func:`~mpmath.whitm`.)
  6184. **Examples**
  6185. Evaluation for arbitrary real and complex arguments is supported::
  6186. >>> from mpmath import *
  6187. >>> mp.dps = 25; mp.pretty = True
  6188. >>> whitw(1, 1, 1)
  6189. 1.19532063107581155661012
  6190. >>> whitw(1, 1, -1)
  6191. (-0.9424875979222187313924639 - 0.2607738054097702293308689j)
  6192. >>> whitw(j, j/2, 2+3j)
  6193. (0.1782899315111033879430369 - 0.01609578360403649340169406j)
  6194. >>> whitw(2, 3, 100000)
  6195. 1.887705114889527446891274e-21705
  6196. >>> whitw(-1, -1, 100)
  6197. 1.905250692824046162462058e-24
  6198. Evaluation at zero::
  6199. >>> for m in [-1, -0.5, 0, 0.5, 1]:
  6200. ... whitw(1, m, 0)
  6201. ...
  6202. +inf
  6203. nan
  6204. 0.0
  6205. nan
  6206. +inf
  6207. We can verify that :func:`~mpmath.whitw` numerically satisfies the
  6208. differential equation for arbitrarily chosen values::
  6209. >>> k = mpf(0.25)
  6210. >>> m = mpf(1.5)
  6211. >>> f = lambda z: whitw(k,m,z)
  6212. >>> for z in [-1, 2.5, 3, 1+2j]:
  6213. ... chop(diff(f,z,2) + (-0.25 + k/z + (0.25-m**2)/z**2)*f(z))
  6214. ...
  6215. 0.0
  6216. 0.0
  6217. 0.0
  6218. 0.0
  6219. """
  6220. ber = r"""
  6221. Computes the Kelvin function ber, which for real arguments gives the real part
  6222. of the Bessel J function of a rotated argument
  6223. .. math ::
  6224. J_n\left(x e^{3\pi i/4}\right) = \mathrm{ber}_n(x) + i \mathrm{bei}_n(x).
  6225. The imaginary part is given by :func:`~mpmath.bei`.
  6226. **Plots**
  6227. .. literalinclude :: /plots/ber.py
  6228. .. image :: /plots/ber.png
  6229. **Examples**
  6230. Verifying the defining relation::
  6231. >>> from mpmath import *
  6232. >>> mp.dps = 25; mp.pretty = True
  6233. >>> n, x = 2, 3.5
  6234. >>> ber(n,x)
  6235. 1.442338852571888752631129
  6236. >>> bei(n,x)
  6237. -0.948359035324558320217678
  6238. >>> besselj(n, x*root(1,8,3))
  6239. (1.442338852571888752631129 - 0.948359035324558320217678j)
  6240. The ber and bei functions are also defined by analytic continuation
  6241. for complex arguments::
  6242. >>> ber(1+j, 2+3j)
  6243. (4.675445984756614424069563 - 15.84901771719130765656316j)
  6244. >>> bei(1+j, 2+3j)
  6245. (15.83886679193707699364398 + 4.684053288183046528703611j)
  6246. """
  6247. bei = r"""
  6248. Computes the Kelvin function bei, which for real arguments gives the
  6249. imaginary part of the Bessel J function of a rotated argument.
  6250. See :func:`~mpmath.ber`.
  6251. """
  6252. ker = r"""
  6253. Computes the Kelvin function ker, which for real arguments gives the real part
  6254. of the (rescaled) Bessel K function of a rotated argument
  6255. .. math ::
  6256. e^{-\pi i/2} K_n\left(x e^{3\pi i/4}\right) = \mathrm{ker}_n(x) + i \mathrm{kei}_n(x).
  6257. The imaginary part is given by :func:`~mpmath.kei`.
  6258. **Plots**
  6259. .. literalinclude :: /plots/ker.py
  6260. .. image :: /plots/ker.png
  6261. **Examples**
  6262. Verifying the defining relation::
  6263. >>> from mpmath import *
  6264. >>> mp.dps = 25; mp.pretty = True
  6265. >>> n, x = 2, 4.5
  6266. >>> ker(n,x)
  6267. 0.02542895201906369640249801
  6268. >>> kei(n,x)
  6269. -0.02074960467222823237055351
  6270. >>> exp(-n*pi*j/2) * besselk(n, x*root(1,8,1))
  6271. (0.02542895201906369640249801 - 0.02074960467222823237055351j)
  6272. The ker and kei functions are also defined by analytic continuation
  6273. for complex arguments::
  6274. >>> ker(1+j, 3+4j)
  6275. (1.586084268115490421090533 - 2.939717517906339193598719j)
  6276. >>> kei(1+j, 3+4j)
  6277. (-2.940403256319453402690132 - 1.585621643835618941044855j)
  6278. """
  6279. kei = r"""
  6280. Computes the Kelvin function kei, which for real arguments gives the
  6281. imaginary part of the (rescaled) Bessel K function of a rotated argument.
  6282. See :func:`~mpmath.ker`.
  6283. """
  6284. struveh = r"""
  6285. Gives the Struve function
  6286. .. math ::
  6287. \,\mathbf{H}_n(z) =
  6288. \sum_{k=0}^\infty \frac{(-1)^k}{\Gamma(k+\frac{3}{2})
  6289. \Gamma(k+n+\frac{3}{2})} {\left({\frac{z}{2}}\right)}^{2k+n+1}
  6290. which is a solution to the Struve differential equation
  6291. .. math ::
  6292. z^2 f''(z) + z f'(z) + (z^2-n^2) f(z) = \frac{2 z^{n+1}}{\pi (2n-1)!!}.
  6293. **Examples**
  6294. Evaluation for arbitrary real and complex arguments::
  6295. >>> from mpmath import *
  6296. >>> mp.dps = 25; mp.pretty = True
  6297. >>> struveh(0, 3.5)
  6298. 0.3608207733778295024977797
  6299. >>> struveh(-1, 10)
  6300. -0.255212719726956768034732
  6301. >>> struveh(1, -100.5)
  6302. 0.5819566816797362287502246
  6303. >>> struveh(2.5, 10000000000000)
  6304. 3153915652525200060.308937
  6305. >>> struveh(2.5, -10000000000000)
  6306. (0.0 - 3153915652525200060.308937j)
  6307. >>> struveh(1+j, 1000000+4000000j)
  6308. (-3.066421087689197632388731e+1737173 - 1.596619701076529803290973e+1737173j)
  6309. A Struve function of half-integer order is elementary; for example:
  6310. >>> z = 3
  6311. >>> struveh(0.5, 3)
  6312. 0.9167076867564138178671595
  6313. >>> sqrt(2/(pi*z))*(1-cos(z))
  6314. 0.9167076867564138178671595
  6315. Numerically verifying the differential equation::
  6316. >>> z = mpf(4.5)
  6317. >>> n = 3
  6318. >>> f = lambda z: struveh(n,z)
  6319. >>> lhs = z**2*diff(f,z,2) + z*diff(f,z) + (z**2-n**2)*f(z)
  6320. >>> rhs = 2*z**(n+1)/fac2(2*n-1)/pi
  6321. >>> lhs
  6322. 17.40359302709875496632744
  6323. >>> rhs
  6324. 17.40359302709875496632744
  6325. """
  6326. struvel = r"""
  6327. Gives the modified Struve function
  6328. .. math ::
  6329. \,\mathbf{L}_n(z) = -i e^{-n\pi i/2} \mathbf{H}_n(i z)
  6330. which solves to the modified Struve differential equation
  6331. .. math ::
  6332. z^2 f''(z) + z f'(z) - (z^2+n^2) f(z) = \frac{2 z^{n+1}}{\pi (2n-1)!!}.
  6333. **Examples**
  6334. Evaluation for arbitrary real and complex arguments::
  6335. >>> from mpmath import *
  6336. >>> mp.dps = 25; mp.pretty = True
  6337. >>> struvel(0, 3.5)
  6338. 7.180846515103737996249972
  6339. >>> struvel(-1, 10)
  6340. 2670.994904980850550721511
  6341. >>> struvel(1, -100.5)
  6342. 1.757089288053346261497686e+42
  6343. >>> struvel(2.5, 10000000000000)
  6344. 4.160893281017115450519948e+4342944819025
  6345. >>> struvel(2.5, -10000000000000)
  6346. (0.0 - 4.160893281017115450519948e+4342944819025j)
  6347. >>> struvel(1+j, 700j)
  6348. (-0.1721150049480079451246076 + 0.1240770953126831093464055j)
  6349. >>> struvel(1+j, 1000000+4000000j)
  6350. (-2.973341637511505389128708e+434290 - 5.164633059729968297147448e+434290j)
  6351. Numerically verifying the differential equation::
  6352. >>> z = mpf(3.5)
  6353. >>> n = 3
  6354. >>> f = lambda z: struvel(n,z)
  6355. >>> lhs = z**2*diff(f,z,2) + z*diff(f,z) - (z**2+n**2)*f(z)
  6356. >>> rhs = 2*z**(n+1)/fac2(2*n-1)/pi
  6357. >>> lhs
  6358. 6.368850306060678353018165
  6359. >>> rhs
  6360. 6.368850306060678353018165
  6361. """
  6362. appellf1 = r"""
  6363. Gives the Appell F1 hypergeometric function of two variables,
  6364. .. math ::
  6365. F_1(a,b_1,b_2,c,x,y) = \sum_{m=0}^{\infty} \sum_{n=0}^{\infty}
  6366. \frac{(a)_{m+n} (b_1)_m (b_2)_n}{(c)_{m+n}}
  6367. \frac{x^m y^n}{m! n!}.
  6368. This series is only generally convergent when `|x| < 1` and `|y| < 1`,
  6369. although :func:`~mpmath.appellf1` can evaluate an analytic continuation
  6370. with respecto to either variable, and sometimes both.
  6371. **Examples**
  6372. Evaluation is supported for real and complex parameters::
  6373. >>> from mpmath import *
  6374. >>> mp.dps = 25; mp.pretty = True
  6375. >>> appellf1(1,0,0.5,1,0.5,0.25)
  6376. 1.154700538379251529018298
  6377. >>> appellf1(1,1+j,0.5,1,0.5,0.5j)
  6378. (1.138403860350148085179415 + 1.510544741058517621110615j)
  6379. For some integer parameters, the F1 series reduces to a polynomial::
  6380. >>> appellf1(2,-4,-3,1,2,5)
  6381. -816.0
  6382. >>> appellf1(-5,1,2,1,4,5)
  6383. -20528.0
  6384. The analytic continuation with respect to either `x` or `y`,
  6385. and sometimes with respect to both, can be evaluated::
  6386. >>> appellf1(2,3,4,5,100,0.5)
  6387. (0.0006231042714165329279738662 + 0.0000005769149277148425774499857j)
  6388. >>> appellf1('1.1', '0.3', '0.2+2j', '0.4', '0.2', 1.5+3j)
  6389. (-0.1782604566893954897128702 + 0.002472407104546216117161499j)
  6390. >>> appellf1(1,2,3,4,10,12)
  6391. -0.07122993830066776374929313
  6392. For certain arguments, F1 reduces to an ordinary hypergeometric function::
  6393. >>> appellf1(1,2,3,5,0.5,0.25)
  6394. 1.547902270302684019335555
  6395. >>> 4*hyp2f1(1,2,5,'1/3')/3
  6396. 1.547902270302684019335555
  6397. >>> appellf1(1,2,3,4,0,1.5)
  6398. (-1.717202506168937502740238 - 2.792526803190927323077905j)
  6399. >>> hyp2f1(1,3,4,1.5)
  6400. (-1.717202506168937502740238 - 2.792526803190927323077905j)
  6401. The F1 function satisfies a system of partial differential equations::
  6402. >>> a,b1,b2,c,x,y = map(mpf, [1,0.5,0.25,1.125,0.25,-0.25])
  6403. >>> F = lambda x,y: appellf1(a,b1,b2,c,x,y)
  6404. >>> chop(x*(1-x)*diff(F,(x,y),(2,0)) +
  6405. ... y*(1-x)*diff(F,(x,y),(1,1)) +
  6406. ... (c-(a+b1+1)*x)*diff(F,(x,y),(1,0)) -
  6407. ... b1*y*diff(F,(x,y),(0,1)) -
  6408. ... a*b1*F(x,y))
  6409. 0.0
  6410. >>>
  6411. >>> chop(y*(1-y)*diff(F,(x,y),(0,2)) +
  6412. ... x*(1-y)*diff(F,(x,y),(1,1)) +
  6413. ... (c-(a+b2+1)*y)*diff(F,(x,y),(0,1)) -
  6414. ... b2*x*diff(F,(x,y),(1,0)) -
  6415. ... a*b2*F(x,y))
  6416. 0.0
  6417. The Appell F1 function allows for closed-form evaluation of various
  6418. integrals, such as any integral of the form
  6419. `\int x^r (x+a)^p (x+b)^q dx`::
  6420. >>> def integral(a,b,p,q,r,x1,x2):
  6421. ... a,b,p,q,r,x1,x2 = map(mpmathify, [a,b,p,q,r,x1,x2])
  6422. ... f = lambda x: x**r * (x+a)**p * (x+b)**q
  6423. ... def F(x):
  6424. ... v = x**(r+1)/(r+1) * (a+x)**p * (b+x)**q
  6425. ... v *= (1+x/a)**(-p)
  6426. ... v *= (1+x/b)**(-q)
  6427. ... v *= appellf1(r+1,-p,-q,2+r,-x/a,-x/b)
  6428. ... return v
  6429. ... print("Num. quad: %s" % quad(f, [x1,x2]))
  6430. ... print("Appell F1: %s" % (F(x2)-F(x1)))
  6431. ...
  6432. >>> integral('1/5','4/3','-2','3','1/2',0,1)
  6433. Num. quad: 9.073335358785776206576981
  6434. Appell F1: 9.073335358785776206576981
  6435. >>> integral('3/2','4/3','-2','3','1/2',0,1)
  6436. Num. quad: 1.092829171999626454344678
  6437. Appell F1: 1.092829171999626454344678
  6438. >>> integral('3/2','4/3','-2','3','1/2',12,25)
  6439. Num. quad: 1106.323225040235116498927
  6440. Appell F1: 1106.323225040235116498927
  6441. Also incomplete elliptic integrals fall into this category [1]::
  6442. >>> def E(z, m):
  6443. ... if (pi/2).ae(z):
  6444. ... return ellipe(m)
  6445. ... return 2*round(re(z)/pi)*ellipe(m) + mpf(-1)**round(re(z)/pi)*\
  6446. ... sin(z)*appellf1(0.5,0.5,-0.5,1.5,sin(z)**2,m*sin(z)**2)
  6447. ...
  6448. >>> z, m = 1, 0.5
  6449. >>> E(z,m); quad(lambda t: sqrt(1-m*sin(t)**2), [0,pi/4,3*pi/4,z])
  6450. 0.9273298836244400669659042
  6451. 0.9273298836244400669659042
  6452. >>> z, m = 3, 2
  6453. >>> E(z,m); quad(lambda t: sqrt(1-m*sin(t)**2), [0,pi/4,3*pi/4,z])
  6454. (1.057495752337234229715836 + 1.198140234735592207439922j)
  6455. (1.057495752337234229715836 + 1.198140234735592207439922j)
  6456. **References**
  6457. 1. [WolframFunctions]_ http://functions.wolfram.com/EllipticIntegrals/EllipticE2/26/01/
  6458. 2. [SrivastavaKarlsson]_
  6459. 3. [CabralRosetti]_
  6460. 4. [Vidunas]_
  6461. 5. [Slater]_
  6462. """
  6463. angerj = r"""
  6464. Gives the Anger function
  6465. .. math ::
  6466. \mathbf{J}_{\nu}(z) = \frac{1}{\pi}
  6467. \int_0^{\pi} \cos(\nu t - z \sin t) dt
  6468. which is an entire function of both the parameter `\nu` and
  6469. the argument `z`. It solves the inhomogeneous Bessel differential
  6470. equation
  6471. .. math ::
  6472. f''(z) + \frac{1}{z}f'(z) + \left(1-\frac{\nu^2}{z^2}\right) f(z)
  6473. = \frac{(z-\nu)}{\pi z^2} \sin(\pi \nu).
  6474. **Examples**
  6475. Evaluation for real and complex parameter and argument::
  6476. >>> from mpmath import *
  6477. >>> mp.dps = 25; mp.pretty = True
  6478. >>> angerj(2,3)
  6479. 0.4860912605858910769078311
  6480. >>> angerj(-3+4j, 2+5j)
  6481. (-5033.358320403384472395612 + 585.8011892476145118551756j)
  6482. >>> angerj(3.25, 1e6j)
  6483. (4.630743639715893346570743e+434290 - 1.117960409887505906848456e+434291j)
  6484. >>> angerj(-1.5, 1e6)
  6485. 0.0002795719747073879393087011
  6486. The Anger function coincides with the Bessel J-function when `\nu`
  6487. is an integer::
  6488. >>> angerj(1,3); besselj(1,3)
  6489. 0.3390589585259364589255146
  6490. 0.3390589585259364589255146
  6491. >>> angerj(1.5,3); besselj(1.5,3)
  6492. 0.4088969848691080859328847
  6493. 0.4777182150870917715515015
  6494. Verifying the differential equation::
  6495. >>> v,z = mpf(2.25), 0.75
  6496. >>> f = lambda z: angerj(v,z)
  6497. >>> diff(f,z,2) + diff(f,z)/z + (1-(v/z)**2)*f(z)
  6498. -0.6002108774380707130367995
  6499. >>> (z-v)/(pi*z**2) * sinpi(v)
  6500. -0.6002108774380707130367995
  6501. Verifying the integral representation::
  6502. >>> angerj(v,z)
  6503. 0.1145380759919333180900501
  6504. >>> quad(lambda t: cos(v*t-z*sin(t))/pi, [0,pi])
  6505. 0.1145380759919333180900501
  6506. **References**
  6507. 1. [DLMF]_ section 11.10: Anger-Weber Functions
  6508. """
  6509. webere = r"""
  6510. Gives the Weber function
  6511. .. math ::
  6512. \mathbf{E}_{\nu}(z) = \frac{1}{\pi}
  6513. \int_0^{\pi} \sin(\nu t - z \sin t) dt
  6514. which is an entire function of both the parameter `\nu` and
  6515. the argument `z`. It solves the inhomogeneous Bessel differential
  6516. equation
  6517. .. math ::
  6518. f''(z) + \frac{1}{z}f'(z) + \left(1-\frac{\nu^2}{z^2}\right) f(z)
  6519. = -\frac{1}{\pi z^2} (z+\nu+(z-\nu)\cos(\pi \nu)).
  6520. **Examples**
  6521. Evaluation for real and complex parameter and argument::
  6522. >>> from mpmath import *
  6523. >>> mp.dps = 25; mp.pretty = True
  6524. >>> webere(2,3)
  6525. -0.1057668973099018425662646
  6526. >>> webere(-3+4j, 2+5j)
  6527. (-585.8081418209852019290498 - 5033.314488899926921597203j)
  6528. >>> webere(3.25, 1e6j)
  6529. (-1.117960409887505906848456e+434291 - 4.630743639715893346570743e+434290j)
  6530. >>> webere(3.25, 1e6)
  6531. -0.00002812518265894315604914453
  6532. Up to addition of a rational function of `z`, the Weber function coincides
  6533. with the Struve H-function when `\nu` is an integer::
  6534. >>> webere(1,3); 2/pi-struveh(1,3)
  6535. -0.3834897968188690177372881
  6536. -0.3834897968188690177372881
  6537. >>> webere(5,3); 26/(35*pi)-struveh(5,3)
  6538. 0.2009680659308154011878075
  6539. 0.2009680659308154011878075
  6540. Verifying the differential equation::
  6541. >>> v,z = mpf(2.25), 0.75
  6542. >>> f = lambda z: webere(v,z)
  6543. >>> diff(f,z,2) + diff(f,z)/z + (1-(v/z)**2)*f(z)
  6544. -1.097441848875479535164627
  6545. >>> -(z+v+(z-v)*cospi(v))/(pi*z**2)
  6546. -1.097441848875479535164627
  6547. Verifying the integral representation::
  6548. >>> webere(v,z)
  6549. 0.1486507351534283744485421
  6550. >>> quad(lambda t: sin(v*t-z*sin(t))/pi, [0,pi])
  6551. 0.1486507351534283744485421
  6552. **References**
  6553. 1. [DLMF]_ section 11.10: Anger-Weber Functions
  6554. """
  6555. lommels1 = r"""
  6556. Gives the Lommel function `s_{\mu,\nu}` or `s^{(1)}_{\mu,\nu}`
  6557. .. math ::
  6558. s_{\mu,\nu}(z) = \frac{z^{\mu+1}}{(\mu-\nu+1)(\mu+\nu+1)}
  6559. \,_1F_2\left(1; \frac{\mu-\nu+3}{2}, \frac{\mu+\nu+3}{2};
  6560. -\frac{z^2}{4} \right)
  6561. which solves the inhomogeneous Bessel equation
  6562. .. math ::
  6563. z^2 f''(z) + z f'(z) + (z^2-\nu^2) f(z) = z^{\mu+1}.
  6564. A second solution is given by :func:`~mpmath.lommels2`.
  6565. **Plots**
  6566. .. literalinclude :: /plots/lommels1.py
  6567. .. image :: /plots/lommels1.png
  6568. **Examples**
  6569. An integral representation::
  6570. >>> from mpmath import *
  6571. >>> mp.dps = 25; mp.pretty = True
  6572. >>> u,v,z = 0.25, 0.125, mpf(0.75)
  6573. >>> lommels1(u,v,z)
  6574. 0.4276243877565150372999126
  6575. >>> (bessely(v,z)*quad(lambda t: t**u*besselj(v,t), [0,z]) - \
  6576. ... besselj(v,z)*quad(lambda t: t**u*bessely(v,t), [0,z]))*(pi/2)
  6577. 0.4276243877565150372999126
  6578. A special value::
  6579. >>> lommels1(v,v,z)
  6580. 0.5461221367746048054932553
  6581. >>> gamma(v+0.5)*sqrt(pi)*power(2,v-1)*struveh(v,z)
  6582. 0.5461221367746048054932553
  6583. Verifying the differential equation::
  6584. >>> f = lambda z: lommels1(u,v,z)
  6585. >>> z**2*diff(f,z,2) + z*diff(f,z) + (z**2-v**2)*f(z)
  6586. 0.6979536443265746992059141
  6587. >>> z**(u+1)
  6588. 0.6979536443265746992059141
  6589. **References**
  6590. 1. [GradshteynRyzhik]_
  6591. 2. [Weisstein]_ http://mathworld.wolfram.com/LommelFunction.html
  6592. """
  6593. lommels2 = r"""
  6594. Gives the second Lommel function `S_{\mu,\nu}` or `s^{(2)}_{\mu,\nu}`
  6595. .. math ::
  6596. S_{\mu,\nu}(z) = s_{\mu,\nu}(z) + 2^{\mu-1}
  6597. \Gamma\left(\tfrac{1}{2}(\mu-\nu+1)\right)
  6598. \Gamma\left(\tfrac{1}{2}(\mu+\nu+1)\right) \times
  6599. \left[\sin(\tfrac{1}{2}(\mu-\nu)\pi) J_{\nu}(z) -
  6600. \cos(\tfrac{1}{2}(\mu-\nu)\pi) Y_{\nu}(z)
  6601. \right]
  6602. which solves the same differential equation as
  6603. :func:`~mpmath.lommels1`.
  6604. **Plots**
  6605. .. literalinclude :: /plots/lommels2.py
  6606. .. image :: /plots/lommels2.png
  6607. **Examples**
  6608. For large `|z|`, `S_{\mu,\nu} \sim z^{\mu-1}`::
  6609. >>> from mpmath import *
  6610. >>> mp.dps = 25; mp.pretty = True
  6611. >>> lommels2(10,2,30000)
  6612. 1.968299831601008419949804e+40
  6613. >>> power(30000,9)
  6614. 1.9683e+40
  6615. A special value::
  6616. >>> u,v,z = 0.5, 0.125, mpf(0.75)
  6617. >>> lommels2(v,v,z)
  6618. 0.9589683199624672099969765
  6619. >>> (struveh(v,z)-bessely(v,z))*power(2,v-1)*sqrt(pi)*gamma(v+0.5)
  6620. 0.9589683199624672099969765
  6621. Verifying the differential equation::
  6622. >>> f = lambda z: lommels2(u,v,z)
  6623. >>> z**2*diff(f,z,2) + z*diff(f,z) + (z**2-v**2)*f(z)
  6624. 0.6495190528383289850727924
  6625. >>> z**(u+1)
  6626. 0.6495190528383289850727924
  6627. **References**
  6628. 1. [GradshteynRyzhik]_
  6629. 2. [Weisstein]_ http://mathworld.wolfram.com/LommelFunction.html
  6630. """
  6631. appellf2 = r"""
  6632. Gives the Appell F2 hypergeometric function of two variables
  6633. .. math ::
  6634. F_2(a,b_1,b_2,c_1,c_2,x,y) = \sum_{m=0}^{\infty} \sum_{n=0}^{\infty}
  6635. \frac{(a)_{m+n} (b_1)_m (b_2)_n}{(c_1)_m (c_2)_n}
  6636. \frac{x^m y^n}{m! n!}.
  6637. The series is generally absolutely convergent for `|x| + |y| < 1`.
  6638. **Examples**
  6639. Evaluation for real and complex arguments::
  6640. >>> from mpmath import *
  6641. >>> mp.dps = 25; mp.pretty = True
  6642. >>> appellf2(1,2,3,4,5,0.25,0.125)
  6643. 1.257417193533135344785602
  6644. >>> appellf2(1,-3,-4,2,3,2,3)
  6645. -42.8
  6646. >>> appellf2(0.5,0.25,-0.25,2,3,0.25j,0.25)
  6647. (0.9880539519421899867041719 + 0.01497616165031102661476978j)
  6648. >>> chop(appellf2(1,1+j,1-j,3j,-3j,0.25,0.25))
  6649. 1.201311219287411337955192
  6650. >>> appellf2(1,1,1,4,6,0.125,16)
  6651. (-0.09455532250274744282125152 - 0.7647282253046207836769297j)
  6652. A transformation formula::
  6653. >>> a,b1,b2,c1,c2,x,y = map(mpf, [1,2,0.5,0.25,1.625,-0.125,0.125])
  6654. >>> appellf2(a,b1,b2,c1,c2,x,y)
  6655. 0.2299211717841180783309688
  6656. >>> (1-x)**(-a)*appellf2(a,c1-b1,b2,c1,c2,x/(x-1),y/(1-x))
  6657. 0.2299211717841180783309688
  6658. A system of partial differential equations satisfied by F2::
  6659. >>> a,b1,b2,c1,c2,x,y = map(mpf, [1,0.5,0.25,1.125,1.5,0.0625,-0.0625])
  6660. >>> F = lambda x,y: appellf2(a,b1,b2,c1,c2,x,y)
  6661. >>> chop(x*(1-x)*diff(F,(x,y),(2,0)) -
  6662. ... x*y*diff(F,(x,y),(1,1)) +
  6663. ... (c1-(a+b1+1)*x)*diff(F,(x,y),(1,0)) -
  6664. ... b1*y*diff(F,(x,y),(0,1)) -
  6665. ... a*b1*F(x,y))
  6666. 0.0
  6667. >>> chop(y*(1-y)*diff(F,(x,y),(0,2)) -
  6668. ... x*y*diff(F,(x,y),(1,1)) +
  6669. ... (c2-(a+b2+1)*y)*diff(F,(x,y),(0,1)) -
  6670. ... b2*x*diff(F,(x,y),(1,0)) -
  6671. ... a*b2*F(x,y))
  6672. 0.0
  6673. **References**
  6674. See references for :func:`~mpmath.appellf1`.
  6675. """
  6676. appellf3 = r"""
  6677. Gives the Appell F3 hypergeometric function of two variables
  6678. .. math ::
  6679. F_3(a_1,a_2,b_1,b_2,c,x,y) = \sum_{m=0}^{\infty} \sum_{n=0}^{\infty}
  6680. \frac{(a_1)_m (a_2)_n (b_1)_m (b_2)_n}{(c)_{m+n}}
  6681. \frac{x^m y^n}{m! n!}.
  6682. The series is generally absolutely convergent for `|x| < 1, |y| < 1`.
  6683. **Examples**
  6684. Evaluation for various parameters and variables::
  6685. >>> from mpmath import *
  6686. >>> mp.dps = 25; mp.pretty = True
  6687. >>> appellf3(1,2,3,4,5,0.5,0.25)
  6688. 2.221557778107438938158705
  6689. >>> appellf3(1,2,3,4,5,6,0); hyp2f1(1,3,5,6)
  6690. (-0.5189554589089861284537389 - 0.1454441043328607980769742j)
  6691. (-0.5189554589089861284537389 - 0.1454441043328607980769742j)
  6692. >>> appellf3(1,-2,-3,1,1,4,6)
  6693. -17.4
  6694. >>> appellf3(1,2,-3,1,1,4,6)
  6695. (17.7876136773677356641825 + 19.54768762233649126154534j)
  6696. >>> appellf3(1,2,-3,1,1,6,4)
  6697. (85.02054175067929402953645 + 148.4402528821177305173599j)
  6698. >>> chop(appellf3(1+j,2,1-j,2,3,0.25,0.25))
  6699. 1.719992169545200286696007
  6700. Many transformations and evaluations for special combinations
  6701. of the parameters are possible, e.g.:
  6702. >>> a,b,c,x,y = map(mpf, [0.5,0.25,0.125,0.125,-0.125])
  6703. >>> appellf3(a,c-a,b,c-b,c,x,y)
  6704. 1.093432340896087107444363
  6705. >>> (1-y)**(a+b-c)*hyp2f1(a,b,c,x+y-x*y)
  6706. 1.093432340896087107444363
  6707. >>> x**2*appellf3(1,1,1,1,3,x,-x)
  6708. 0.01568646277445385390945083
  6709. >>> polylog(2,x**2)
  6710. 0.01568646277445385390945083
  6711. >>> a1,a2,b1,b2,c,x = map(mpf, [0.5,0.25,0.125,0.5,4.25,0.125])
  6712. >>> appellf3(a1,a2,b1,b2,c,x,1)
  6713. 1.03947361709111140096947
  6714. >>> gammaprod([c,c-a2-b2],[c-a2,c-b2])*hyp3f2(a1,b1,c-a2-b2,c-a2,c-b2,x)
  6715. 1.03947361709111140096947
  6716. The Appell F3 function satisfies a pair of partial
  6717. differential equations::
  6718. >>> a1,a2,b1,b2,c,x,y = map(mpf, [0.5,0.25,0.125,0.5,0.625,0.0625,-0.0625])
  6719. >>> F = lambda x,y: appellf3(a1,a2,b1,b2,c,x,y)
  6720. >>> chop(x*(1-x)*diff(F,(x,y),(2,0)) +
  6721. ... y*diff(F,(x,y),(1,1)) +
  6722. ... (c-(a1+b1+1)*x)*diff(F,(x,y),(1,0)) -
  6723. ... a1*b1*F(x,y))
  6724. 0.0
  6725. >>> chop(y*(1-y)*diff(F,(x,y),(0,2)) +
  6726. ... x*diff(F,(x,y),(1,1)) +
  6727. ... (c-(a2+b2+1)*y)*diff(F,(x,y),(0,1)) -
  6728. ... a2*b2*F(x,y))
  6729. 0.0
  6730. **References**
  6731. See references for :func:`~mpmath.appellf1`.
  6732. """
  6733. appellf4 = r"""
  6734. Gives the Appell F4 hypergeometric function of two variables
  6735. .. math ::
  6736. F_4(a,b,c_1,c_2,x,y) = \sum_{m=0}^{\infty} \sum_{n=0}^{\infty}
  6737. \frac{(a)_{m+n} (b)_{m+n}}{(c_1)_m (c_2)_n}
  6738. \frac{x^m y^n}{m! n!}.
  6739. The series is generally absolutely convergent for
  6740. `\sqrt{|x|} + \sqrt{|y|} < 1`.
  6741. **Examples**
  6742. Evaluation for various parameters and arguments::
  6743. >>> from mpmath import *
  6744. >>> mp.dps = 25; mp.pretty = True
  6745. >>> appellf4(1,1,2,2,0.25,0.125)
  6746. 1.286182069079718313546608
  6747. >>> appellf4(-2,-3,4,5,4,5)
  6748. 34.8
  6749. >>> appellf4(5,4,2,3,0.25j,-0.125j)
  6750. (-0.2585967215437846642163352 + 2.436102233553582711818743j)
  6751. Reduction to `\,_2F_1` in a special case::
  6752. >>> a,b,c,x,y = map(mpf, [0.5,0.25,0.125,0.125,-0.125])
  6753. >>> appellf4(a,b,c,a+b-c+1,x*(1-y),y*(1-x))
  6754. 1.129143488466850868248364
  6755. >>> hyp2f1(a,b,c,x)*hyp2f1(a,b,a+b-c+1,y)
  6756. 1.129143488466850868248364
  6757. A system of partial differential equations satisfied by F4::
  6758. >>> a,b,c1,c2,x,y = map(mpf, [1,0.5,0.25,1.125,0.0625,-0.0625])
  6759. >>> F = lambda x,y: appellf4(a,b,c1,c2,x,y)
  6760. >>> chop(x*(1-x)*diff(F,(x,y),(2,0)) -
  6761. ... y**2*diff(F,(x,y),(0,2)) -
  6762. ... 2*x*y*diff(F,(x,y),(1,1)) +
  6763. ... (c1-(a+b+1)*x)*diff(F,(x,y),(1,0)) -
  6764. ... ((a+b+1)*y)*diff(F,(x,y),(0,1)) -
  6765. ... a*b*F(x,y))
  6766. 0.0
  6767. >>> chop(y*(1-y)*diff(F,(x,y),(0,2)) -
  6768. ... x**2*diff(F,(x,y),(2,0)) -
  6769. ... 2*x*y*diff(F,(x,y),(1,1)) +
  6770. ... (c2-(a+b+1)*y)*diff(F,(x,y),(0,1)) -
  6771. ... ((a+b+1)*x)*diff(F,(x,y),(1,0)) -
  6772. ... a*b*F(x,y))
  6773. 0.0
  6774. **References**
  6775. See references for :func:`~mpmath.appellf1`.
  6776. """
  6777. zeta = r"""
  6778. Computes the Riemann zeta function
  6779. .. math ::
  6780. \zeta(s) = 1+\frac{1}{2^s}+\frac{1}{3^s}+\frac{1}{4^s}+\ldots
  6781. or, with `a \ne 1`, the more general Hurwitz zeta function
  6782. .. math ::
  6783. \zeta(s,a) = \sum_{k=0}^\infty \frac{1}{(a+k)^s}.
  6784. Optionally, ``zeta(s, a, n)`` computes the `n`-th derivative with
  6785. respect to `s`,
  6786. .. math ::
  6787. \zeta^{(n)}(s,a) = (-1)^n \sum_{k=0}^\infty \frac{\log^n(a+k)}{(a+k)^s}.
  6788. Although these series only converge for `\Re(s) > 1`, the Riemann and Hurwitz
  6789. zeta functions are defined through analytic continuation for arbitrary
  6790. complex `s \ne 1` (`s = 1` is a pole).
  6791. The implementation uses three algorithms: the Borwein algorithm for
  6792. the Riemann zeta function when `s` is close to the real line;
  6793. the Riemann-Siegel formula for the Riemann zeta function when `s` is
  6794. large imaginary, and Euler-Maclaurin summation in all other cases.
  6795. The reflection formula for `\Re(s) < 0` is implemented in some cases.
  6796. The algorithm can be chosen with ``method = 'borwein'``,
  6797. ``method='riemann-siegel'`` or ``method = 'euler-maclaurin'``.
  6798. The parameter `a` is usually a rational number `a = p/q`, and may be specified
  6799. as such by passing an integer tuple `(p, q)`. Evaluation is supported for
  6800. arbitrary complex `a`, but may be slow and/or inaccurate when `\Re(s) < 0` for
  6801. nonrational `a` or when computing derivatives.
  6802. **Examples**
  6803. Some values of the Riemann zeta function::
  6804. >>> from mpmath import *
  6805. >>> mp.dps = 25; mp.pretty = True
  6806. >>> zeta(2); pi**2 / 6
  6807. 1.644934066848226436472415
  6808. 1.644934066848226436472415
  6809. >>> zeta(0)
  6810. -0.5
  6811. >>> zeta(-1)
  6812. -0.08333333333333333333333333
  6813. >>> zeta(-2)
  6814. 0.0
  6815. For large positive `s`, `\zeta(s)` rapidly approaches 1::
  6816. >>> zeta(50)
  6817. 1.000000000000000888178421
  6818. >>> zeta(100)
  6819. 1.0
  6820. >>> zeta(inf)
  6821. 1.0
  6822. >>> 1-sum((zeta(k)-1)/k for k in range(2,85)); +euler
  6823. 0.5772156649015328606065121
  6824. 0.5772156649015328606065121
  6825. >>> nsum(lambda k: zeta(k)-1, [2, inf])
  6826. 1.0
  6827. Evaluation is supported for complex `s` and `a`:
  6828. >>> zeta(-3+4j)
  6829. (-0.03373057338827757067584698 + 0.2774499251557093745297677j)
  6830. >>> zeta(2+3j, -1+j)
  6831. (389.6841230140842816370741 + 295.2674610150305334025962j)
  6832. The Riemann zeta function has so-called nontrivial zeros on
  6833. the critical line `s = 1/2 + it`::
  6834. >>> findroot(zeta, 0.5+14j); zetazero(1)
  6835. (0.5 + 14.13472514173469379045725j)
  6836. (0.5 + 14.13472514173469379045725j)
  6837. >>> findroot(zeta, 0.5+21j); zetazero(2)
  6838. (0.5 + 21.02203963877155499262848j)
  6839. (0.5 + 21.02203963877155499262848j)
  6840. >>> findroot(zeta, 0.5+25j); zetazero(3)
  6841. (0.5 + 25.01085758014568876321379j)
  6842. (0.5 + 25.01085758014568876321379j)
  6843. >>> chop(zeta(zetazero(10)))
  6844. 0.0
  6845. Evaluation on and near the critical line is supported for large
  6846. heights `t` by means of the Riemann-Siegel formula (currently
  6847. for `a = 1`, `n \le 4`)::
  6848. >>> zeta(0.5+100000j)
  6849. (1.073032014857753132114076 + 5.780848544363503984261041j)
  6850. >>> zeta(0.75+1000000j)
  6851. (0.9535316058375145020351559 + 0.9525945894834273060175651j)
  6852. >>> zeta(0.5+10000000j)
  6853. (11.45804061057709254500227 - 8.643437226836021723818215j)
  6854. >>> zeta(0.5+100000000j, derivative=1)
  6855. (51.12433106710194942681869 + 43.87221167872304520599418j)
  6856. >>> zeta(0.5+100000000j, derivative=2)
  6857. (-444.2760822795430400549229 - 896.3789978119185981665403j)
  6858. >>> zeta(0.5+100000000j, derivative=3)
  6859. (3230.72682687670422215339 + 14374.36950073615897616781j)
  6860. >>> zeta(0.5+100000000j, derivative=4)
  6861. (-11967.35573095046402130602 - 218945.7817789262839266148j)
  6862. >>> zeta(1+10000000j) # off the line
  6863. (2.859846483332530337008882 + 0.491808047480981808903986j)
  6864. >>> zeta(1+10000000j, derivative=1)
  6865. (-4.333835494679647915673205 - 0.08405337962602933636096103j)
  6866. >>> zeta(1+10000000j, derivative=4)
  6867. (453.2764822702057701894278 - 581.963625832768189140995j)
  6868. For investigation of the zeta function zeros, the Riemann-Siegel
  6869. Z-function is often more convenient than working with the Riemann
  6870. zeta function directly (see :func:`~mpmath.siegelz`).
  6871. Some values of the Hurwitz zeta function::
  6872. >>> zeta(2, 3); -5./4 + pi**2/6
  6873. 0.3949340668482264364724152
  6874. 0.3949340668482264364724152
  6875. >>> zeta(2, (3,4)); pi**2 - 8*catalan
  6876. 2.541879647671606498397663
  6877. 2.541879647671606498397663
  6878. For positive integer values of `s`, the Hurwitz zeta function is
  6879. equivalent to a polygamma function (except for a normalizing factor)::
  6880. >>> zeta(4, (1,5)); psi(3, '1/5')/6
  6881. 625.5408324774542966919938
  6882. 625.5408324774542966919938
  6883. Evaluation of derivatives::
  6884. >>> zeta(0, 3+4j, 1); loggamma(3+4j) - ln(2*pi)/2
  6885. (-2.675565317808456852310934 + 4.742664438034657928194889j)
  6886. (-2.675565317808456852310934 + 4.742664438034657928194889j)
  6887. >>> zeta(2, 1, 20)
  6888. 2432902008176640000.000242
  6889. >>> zeta(3+4j, 5.5+2j, 4)
  6890. (-0.140075548947797130681075 - 0.3109263360275413251313634j)
  6891. >>> zeta(0.5+100000j, 1, 4)
  6892. (-10407.16081931495861539236 + 13777.78669862804508537384j)
  6893. >>> zeta(-100+0.5j, (1,3), derivative=4)
  6894. (4.007180821099823942702249e+79 + 4.916117957092593868321778e+78j)
  6895. Generating a Taylor series at `s = 2` using derivatives::
  6896. >>> for k in range(11): print("%s * (s-2)^%i" % (zeta(2,1,k)/fac(k), k))
  6897. ...
  6898. 1.644934066848226436472415 * (s-2)^0
  6899. -0.9375482543158437537025741 * (s-2)^1
  6900. 0.9946401171494505117104293 * (s-2)^2
  6901. -1.000024300473840810940657 * (s-2)^3
  6902. 1.000061933072352565457512 * (s-2)^4
  6903. -1.000006869443931806408941 * (s-2)^5
  6904. 1.000000173233769531820592 * (s-2)^6
  6905. -0.9999999569989868493432399 * (s-2)^7
  6906. 0.9999999937218844508684206 * (s-2)^8
  6907. -0.9999999996355013916608284 * (s-2)^9
  6908. 1.000000000004610645020747 * (s-2)^10
  6909. Evaluation at zero and for negative integer `s`::
  6910. >>> zeta(0, 10)
  6911. -9.5
  6912. >>> zeta(-2, (2,3)); mpf(1)/81
  6913. 0.01234567901234567901234568
  6914. 0.01234567901234567901234568
  6915. >>> zeta(-3+4j, (5,4))
  6916. (0.2899236037682695182085988 + 0.06561206166091757973112783j)
  6917. >>> zeta(-3.25, 1/pi)
  6918. -0.0005117269627574430494396877
  6919. >>> zeta(-3.5, pi, 1)
  6920. 11.156360390440003294709
  6921. >>> zeta(-100.5, (8,3))
  6922. -4.68162300487989766727122e+77
  6923. >>> zeta(-10.5, (-8,3))
  6924. (-0.01521913704446246609237979 + 29907.72510874248161608216j)
  6925. >>> zeta(-1000.5, (-8,3))
  6926. (1.031911949062334538202567e+1770 + 1.519555750556794218804724e+426j)
  6927. >>> zeta(-1+j, 3+4j)
  6928. (-16.32988355630802510888631 - 22.17706465801374033261383j)
  6929. >>> zeta(-1+j, 3+4j, 2)
  6930. (32.48985276392056641594055 - 51.11604466157397267043655j)
  6931. >>> diff(lambda s: zeta(s, 3+4j), -1+j, 2)
  6932. (32.48985276392056641594055 - 51.11604466157397267043655j)
  6933. **References**
  6934. 1. http://mathworld.wolfram.com/RiemannZetaFunction.html
  6935. 2. http://mathworld.wolfram.com/HurwitzZetaFunction.html
  6936. 3. [BorweinZeta]_
  6937. """
  6938. dirichlet = r"""
  6939. Evaluates the Dirichlet L-function
  6940. .. math ::
  6941. L(s,\chi) = \sum_{k=1}^\infty \frac{\chi(k)}{k^s}.
  6942. where `\chi` is a periodic sequence of length `q` which should be supplied
  6943. in the form of a list `[\chi(0), \chi(1), \ldots, \chi(q-1)]`.
  6944. Strictly, `\chi` should be a Dirichlet character, but any periodic
  6945. sequence will work.
  6946. For example, ``dirichlet(s, [1])`` gives the ordinary
  6947. Riemann zeta function and ``dirichlet(s, [-1,1])`` gives
  6948. the alternating zeta function (Dirichlet eta function).
  6949. Also the derivative with respect to `s` (currently only a first
  6950. derivative) can be evaluated.
  6951. **Examples**
  6952. The ordinary Riemann zeta function::
  6953. >>> from mpmath import *
  6954. >>> mp.dps = 25; mp.pretty = True
  6955. >>> dirichlet(3, [1]); zeta(3)
  6956. 1.202056903159594285399738
  6957. 1.202056903159594285399738
  6958. >>> dirichlet(1, [1])
  6959. +inf
  6960. The alternating zeta function::
  6961. >>> dirichlet(1, [-1,1]); ln(2)
  6962. 0.6931471805599453094172321
  6963. 0.6931471805599453094172321
  6964. The following defines the Dirichlet beta function
  6965. `\beta(s) = \sum_{k=0}^\infty \frac{(-1)^k}{(2k+1)^s}` and verifies
  6966. several values of this function::
  6967. >>> B = lambda s, d=0: dirichlet(s, [0, 1, 0, -1], d)
  6968. >>> B(0); 1./2
  6969. 0.5
  6970. 0.5
  6971. >>> B(1); pi/4
  6972. 0.7853981633974483096156609
  6973. 0.7853981633974483096156609
  6974. >>> B(2); +catalan
  6975. 0.9159655941772190150546035
  6976. 0.9159655941772190150546035
  6977. >>> B(2,1); diff(B, 2)
  6978. 0.08158073611659279510291217
  6979. 0.08158073611659279510291217
  6980. >>> B(-1,1); 2*catalan/pi
  6981. 0.5831218080616375602767689
  6982. 0.5831218080616375602767689
  6983. >>> B(0,1); log(gamma(0.25)**2/(2*pi*sqrt(2)))
  6984. 0.3915943927068367764719453
  6985. 0.3915943927068367764719454
  6986. >>> B(1,1); 0.25*pi*(euler+2*ln2+3*ln(pi)-4*ln(gamma(0.25)))
  6987. 0.1929013167969124293631898
  6988. 0.1929013167969124293631898
  6989. A custom L-series of period 3::
  6990. >>> dirichlet(2, [2,0,1])
  6991. 0.7059715047839078092146831
  6992. >>> 2*nsum(lambda k: (3*k)**-2, [1,inf]) + \
  6993. ... nsum(lambda k: (3*k+2)**-2, [0,inf])
  6994. 0.7059715047839078092146831
  6995. """
  6996. coulombf = r"""
  6997. Calculates the regular Coulomb wave function
  6998. .. math ::
  6999. F_l(\eta,z) = C_l(\eta) z^{l+1} e^{-iz} \,_1F_1(l+1-i\eta, 2l+2, 2iz)
  7000. where the normalization constant `C_l(\eta)` is as calculated by
  7001. :func:`~mpmath.coulombc`. This function solves the differential equation
  7002. .. math ::
  7003. f''(z) + \left(1-\frac{2\eta}{z}-\frac{l(l+1)}{z^2}\right) f(z) = 0.
  7004. A second linearly independent solution is given by the irregular
  7005. Coulomb wave function `G_l(\eta,z)` (see :func:`~mpmath.coulombg`)
  7006. and thus the general solution is
  7007. `f(z) = C_1 F_l(\eta,z) + C_2 G_l(\eta,z)` for arbitrary
  7008. constants `C_1`, `C_2`.
  7009. Physically, the Coulomb wave functions give the radial solution
  7010. to the Schrodinger equation for a point particle in a `1/z` potential; `z` is
  7011. then the radius and `l`, `\eta` are quantum numbers.
  7012. The Coulomb wave functions with real parameters are defined
  7013. in Abramowitz & Stegun, section 14. However, all parameters are permitted
  7014. to be complex in this implementation (see references).
  7015. **Plots**
  7016. .. literalinclude :: /plots/coulombf.py
  7017. .. image :: /plots/coulombf.png
  7018. .. literalinclude :: /plots/coulombf_c.py
  7019. .. image :: /plots/coulombf_c.png
  7020. **Examples**
  7021. Evaluation is supported for arbitrary magnitudes of `z`::
  7022. >>> from mpmath import *
  7023. >>> mp.dps = 25; mp.pretty = True
  7024. >>> coulombf(2, 1.5, 3.5)
  7025. 0.4080998961088761187426445
  7026. >>> coulombf(-2, 1.5, 3.5)
  7027. 0.7103040849492536747533465
  7028. >>> coulombf(2, 1.5, '1e-10')
  7029. 4.143324917492256448770769e-33
  7030. >>> coulombf(2, 1.5, 1000)
  7031. 0.4482623140325567050716179
  7032. >>> coulombf(2, 1.5, 10**10)
  7033. -0.066804196437694360046619
  7034. Verifying the differential equation::
  7035. >>> l, eta, z = 2, 3, mpf(2.75)
  7036. >>> A, B = 1, 2
  7037. >>> f = lambda z: A*coulombf(l,eta,z) + B*coulombg(l,eta,z)
  7038. >>> chop(diff(f,z,2) + (1-2*eta/z - l*(l+1)/z**2)*f(z))
  7039. 0.0
  7040. A Wronskian relation satisfied by the Coulomb wave functions::
  7041. >>> l = 2
  7042. >>> eta = 1.5
  7043. >>> F = lambda z: coulombf(l,eta,z)
  7044. >>> G = lambda z: coulombg(l,eta,z)
  7045. >>> for z in [3.5, -1, 2+3j]:
  7046. ... chop(diff(F,z)*G(z) - F(z)*diff(G,z))
  7047. ...
  7048. 1.0
  7049. 1.0
  7050. 1.0
  7051. Another Wronskian relation::
  7052. >>> F = coulombf
  7053. >>> G = coulombg
  7054. >>> for z in [3.5, -1, 2+3j]:
  7055. ... chop(F(l-1,eta,z)*G(l,eta,z)-F(l,eta,z)*G(l-1,eta,z) - l/sqrt(l**2+eta**2))
  7056. ...
  7057. 0.0
  7058. 0.0
  7059. 0.0
  7060. An integral identity connecting the regular and irregular wave functions::
  7061. >>> l, eta, z = 4+j, 2-j, 5+2j
  7062. >>> coulombf(l,eta,z) + j*coulombg(l,eta,z)
  7063. (0.7997977752284033239714479 + 0.9294486669502295512503127j)
  7064. >>> g = lambda t: exp(-t)*t**(l-j*eta)*(t+2*j*z)**(l+j*eta)
  7065. >>> j*exp(-j*z)*z**(-l)/fac(2*l+1)/coulombc(l,eta)*quad(g, [0,inf])
  7066. (0.7997977752284033239714479 + 0.9294486669502295512503127j)
  7067. Some test case with complex parameters, taken from Michel [2]::
  7068. >>> mp.dps = 15
  7069. >>> coulombf(1+0.1j, 50+50j, 100.156)
  7070. (-1.02107292320897e+15 - 2.83675545731519e+15j)
  7071. >>> coulombg(1+0.1j, 50+50j, 100.156)
  7072. (2.83675545731519e+15 - 1.02107292320897e+15j)
  7073. >>> coulombf(1e-5j, 10+1e-5j, 0.1+1e-6j)
  7074. (4.30566371247811e-14 - 9.03347835361657e-19j)
  7075. >>> coulombg(1e-5j, 10+1e-5j, 0.1+1e-6j)
  7076. (778709182061.134 + 18418936.2660553j)
  7077. The following reproduces a table in Abramowitz & Stegun, at twice
  7078. the precision::
  7079. >>> mp.dps = 10
  7080. >>> eta = 2; z = 5
  7081. >>> for l in [5, 4, 3, 2, 1, 0]:
  7082. ... print("%s %s %s" % (l, coulombf(l,eta,z),
  7083. ... diff(lambda z: coulombf(l,eta,z), z)))
  7084. ...
  7085. 5 0.09079533488 0.1042553261
  7086. 4 0.2148205331 0.2029591779
  7087. 3 0.4313159311 0.320534053
  7088. 2 0.7212774133 0.3952408216
  7089. 1 0.9935056752 0.3708676452
  7090. 0 1.143337392 0.2937960375
  7091. **References**
  7092. 1. I.J. Thompson & A.R. Barnett, "Coulomb and Bessel Functions of Complex
  7093. Arguments and Order", J. Comp. Phys., vol 64, no. 2, June 1986.
  7094. 2. N. Michel, "Precise Coulomb wave functions for a wide range of
  7095. complex `l`, `\eta` and `z`", http://arxiv.org/abs/physics/0702051v1
  7096. """
  7097. coulombg = r"""
  7098. Calculates the irregular Coulomb wave function
  7099. .. math ::
  7100. G_l(\eta,z) = \frac{F_l(\eta,z) \cos(\chi) - F_{-l-1}(\eta,z)}{\sin(\chi)}
  7101. where `\chi = \sigma_l - \sigma_{-l-1} - (l+1/2) \pi`
  7102. and `\sigma_l(\eta) = (\ln \Gamma(1+l+i\eta)-\ln \Gamma(1+l-i\eta))/(2i)`.
  7103. See :func:`~mpmath.coulombf` for additional information.
  7104. **Plots**
  7105. .. literalinclude :: /plots/coulombg.py
  7106. .. image :: /plots/coulombg.png
  7107. .. literalinclude :: /plots/coulombg_c.py
  7108. .. image :: /plots/coulombg_c.png
  7109. **Examples**
  7110. Evaluation is supported for arbitrary magnitudes of `z`::
  7111. >>> from mpmath import *
  7112. >>> mp.dps = 25; mp.pretty = True
  7113. >>> coulombg(-2, 1.5, 3.5)
  7114. 1.380011900612186346255524
  7115. >>> coulombg(2, 1.5, 3.5)
  7116. 1.919153700722748795245926
  7117. >>> coulombg(-2, 1.5, '1e-10')
  7118. 201126715824.7329115106793
  7119. >>> coulombg(-2, 1.5, 1000)
  7120. 0.1802071520691149410425512
  7121. >>> coulombg(-2, 1.5, 10**10)
  7122. 0.652103020061678070929794
  7123. The following reproduces a table in Abramowitz & Stegun,
  7124. at twice the precision::
  7125. >>> mp.dps = 10
  7126. >>> eta = 2; z = 5
  7127. >>> for l in [1, 2, 3, 4, 5]:
  7128. ... print("%s %s %s" % (l, coulombg(l,eta,z),
  7129. ... -diff(lambda z: coulombg(l,eta,z), z)))
  7130. ...
  7131. 1 1.08148276 0.6028279961
  7132. 2 1.496877075 0.5661803178
  7133. 3 2.048694714 0.7959909551
  7134. 4 3.09408669 1.731802374
  7135. 5 5.629840456 4.549343289
  7136. Evaluation close to the singularity at `z = 0`::
  7137. >>> mp.dps = 15
  7138. >>> coulombg(0,10,1)
  7139. 3088184933.67358
  7140. >>> coulombg(0,10,'1e-10')
  7141. 5554866000719.8
  7142. >>> coulombg(0,10,'1e-100')
  7143. 5554866221524.1
  7144. Evaluation with a half-integer value for `l`::
  7145. >>> coulombg(1.5, 1, 10)
  7146. 0.852320038297334
  7147. """
  7148. coulombc = r"""
  7149. Gives the normalizing Gamow constant for Coulomb wave functions,
  7150. .. math ::
  7151. C_l(\eta) = 2^l \exp\left(-\pi \eta/2 + [\ln \Gamma(1+l+i\eta) +
  7152. \ln \Gamma(1+l-i\eta)]/2 - \ln \Gamma(2l+2)\right),
  7153. where the log gamma function with continuous imaginary part
  7154. away from the negative half axis (see :func:`~mpmath.loggamma`) is implied.
  7155. This function is used internally for the calculation of
  7156. Coulomb wave functions, and automatically cached to make multiple
  7157. evaluations with fixed `l`, `\eta` fast.
  7158. """
  7159. ellipfun = r"""
  7160. Computes any of the Jacobi elliptic functions, defined
  7161. in terms of Jacobi theta functions as
  7162. .. math ::
  7163. \mathrm{sn}(u,m) = \frac{\vartheta_3(0,q)}{\vartheta_2(0,q)}
  7164. \frac{\vartheta_1(t,q)}{\vartheta_4(t,q)}
  7165. \mathrm{cn}(u,m) = \frac{\vartheta_4(0,q)}{\vartheta_2(0,q)}
  7166. \frac{\vartheta_2(t,q)}{\vartheta_4(t,q)}
  7167. \mathrm{dn}(u,m) = \frac{\vartheta_4(0,q)}{\vartheta_3(0,q)}
  7168. \frac{\vartheta_3(t,q)}{\vartheta_4(t,q)},
  7169. or more generally computes a ratio of two such functions. Here
  7170. `t = u/\vartheta_3(0,q)^2`, and `q = q(m)` denotes the nome (see
  7171. :func:`~mpmath.nome`). Optionally, you can specify the nome directly
  7172. instead of `m` by passing ``q=<value>``, or you can directly
  7173. specify the elliptic parameter `k` with ``k=<value>``.
  7174. The first argument should be a two-character string specifying the
  7175. function using any combination of ``'s'``, ``'c'``, ``'d'``, ``'n'``. These
  7176. letters respectively denote the basic functions
  7177. `\mathrm{sn}(u,m)`, `\mathrm{cn}(u,m)`, `\mathrm{dn}(u,m)`, and `1`.
  7178. The identifier specifies the ratio of two such functions.
  7179. For example, ``'ns'`` identifies the function
  7180. .. math ::
  7181. \mathrm{ns}(u,m) = \frac{1}{\mathrm{sn}(u,m)}
  7182. and ``'cd'`` identifies the function
  7183. .. math ::
  7184. \mathrm{cd}(u,m) = \frac{\mathrm{cn}(u,m)}{\mathrm{dn}(u,m)}.
  7185. If called with only the first argument, a function object
  7186. evaluating the chosen function for given arguments is returned.
  7187. **Examples**
  7188. Basic evaluation::
  7189. >>> from mpmath import *
  7190. >>> mp.dps = 25; mp.pretty = True
  7191. >>> ellipfun('cd', 3.5, 0.5)
  7192. -0.9891101840595543931308394
  7193. >>> ellipfun('cd', 3.5, q=0.25)
  7194. 0.07111979240214668158441418
  7195. The sn-function is doubly periodic in the complex plane with periods
  7196. `4 K(m)` and `2 i K(1-m)` (see :func:`~mpmath.ellipk`)::
  7197. >>> sn = ellipfun('sn')
  7198. >>> sn(2, 0.25)
  7199. 0.9628981775982774425751399
  7200. >>> sn(2+4*ellipk(0.25), 0.25)
  7201. 0.9628981775982774425751399
  7202. >>> chop(sn(2+2*j*ellipk(1-0.25), 0.25))
  7203. 0.9628981775982774425751399
  7204. The cn-function is doubly periodic with periods `4 K(m)` and `2 K(m) + 2 i K(1-m)`::
  7205. >>> cn = ellipfun('cn')
  7206. >>> cn(2, 0.25)
  7207. -0.2698649654510865792581416
  7208. >>> cn(2+4*ellipk(0.25), 0.25)
  7209. -0.2698649654510865792581416
  7210. >>> chop(cn(2+2*ellipk(0.25)+2*j*ellipk(1-0.25), 0.25))
  7211. -0.2698649654510865792581416
  7212. The dn-function is doubly periodic with periods `2 K(m)` and `4 i K(1-m)`::
  7213. >>> dn = ellipfun('dn')
  7214. >>> dn(2, 0.25)
  7215. 0.8764740583123262286931578
  7216. >>> dn(2+2*ellipk(0.25), 0.25)
  7217. 0.8764740583123262286931578
  7218. >>> chop(dn(2+4*j*ellipk(1-0.25), 0.25))
  7219. 0.8764740583123262286931578
  7220. """
  7221. jtheta = r"""
  7222. Computes the Jacobi theta function `\vartheta_n(z, q)`, where
  7223. `n = 1, 2, 3, 4`, defined by the infinite series:
  7224. .. math ::
  7225. \vartheta_1(z,q) = 2 q^{1/4} \sum_{n=0}^{\infty}
  7226. (-1)^n q^{n^2+n\,} \sin((2n+1)z)
  7227. \vartheta_2(z,q) = 2 q^{1/4} \sum_{n=0}^{\infty}
  7228. q^{n^{2\,} + n} \cos((2n+1)z)
  7229. \vartheta_3(z,q) = 1 + 2 \sum_{n=1}^{\infty}
  7230. q^{n^2\,} \cos(2 n z)
  7231. \vartheta_4(z,q) = 1 + 2 \sum_{n=1}^{\infty}
  7232. (-q)^{n^2\,} \cos(2 n z)
  7233. The theta functions are functions of two variables:
  7234. * `z` is the *argument*, an arbitrary real or complex number
  7235. * `q` is the *nome*, which must be a real or complex number
  7236. in the unit disk (i.e. `|q| < 1`). For `|q| \ll 1`, the
  7237. series converge very quickly, so the Jacobi theta functions
  7238. can efficiently be evaluated to high precision.
  7239. The compact notations `\vartheta_n(q) = \vartheta_n(0,q)`
  7240. and `\vartheta_n = \vartheta_n(0,q)` are also frequently
  7241. encountered. Finally, Jacobi theta functions are frequently
  7242. considered as functions of the half-period ratio `\tau`
  7243. and then usually denoted by `\vartheta_n(z|\tau)`.
  7244. Optionally, ``jtheta(n, z, q, derivative=d)`` with `d > 0` computes
  7245. a `d`-th derivative with respect to `z`.
  7246. **Examples and basic properties**
  7247. Considered as functions of `z`, the Jacobi theta functions may be
  7248. viewed as generalizations of the ordinary trigonometric functions
  7249. cos and sin. They are periodic functions::
  7250. >>> from mpmath import *
  7251. >>> mp.dps = 25; mp.pretty = True
  7252. >>> jtheta(1, 0.25, '0.2')
  7253. 0.2945120798627300045053104
  7254. >>> jtheta(1, 0.25 + 2*pi, '0.2')
  7255. 0.2945120798627300045053104
  7256. Indeed, the series defining the theta functions are essentially
  7257. trigonometric Fourier series. The coefficients can be retrieved
  7258. using :func:`~mpmath.fourier`::
  7259. >>> mp.dps = 10
  7260. >>> nprint(fourier(lambda x: jtheta(2, x, 0.5), [-pi, pi], 4))
  7261. ([0.0, 1.68179, 0.0, 0.420448, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0])
  7262. The Jacobi theta functions are also so-called quasiperiodic
  7263. functions of `z` and `\tau`, meaning that for fixed `\tau`,
  7264. `\vartheta_n(z, q)` and `\vartheta_n(z+\pi \tau, q)` are the same
  7265. except for an exponential factor::
  7266. >>> mp.dps = 25
  7267. >>> tau = 3*j/10
  7268. >>> q = exp(pi*j*tau)
  7269. >>> z = 10
  7270. >>> jtheta(4, z+tau*pi, q)
  7271. (-0.682420280786034687520568 + 1.526683999721399103332021j)
  7272. >>> -exp(-2*j*z)/q * jtheta(4, z, q)
  7273. (-0.682420280786034687520568 + 1.526683999721399103332021j)
  7274. The Jacobi theta functions satisfy a huge number of other
  7275. functional equations, such as the following identity (valid for
  7276. any `q`)::
  7277. >>> q = mpf(3)/10
  7278. >>> jtheta(3,0,q)**4
  7279. 6.823744089352763305137427
  7280. >>> jtheta(2,0,q)**4 + jtheta(4,0,q)**4
  7281. 6.823744089352763305137427
  7282. Extensive listings of identities satisfied by the Jacobi theta
  7283. functions can be found in standard reference works.
  7284. The Jacobi theta functions are related to the gamma function
  7285. for special arguments::
  7286. >>> jtheta(3, 0, exp(-pi))
  7287. 1.086434811213308014575316
  7288. >>> pi**(1/4.) / gamma(3/4.)
  7289. 1.086434811213308014575316
  7290. :func:`~mpmath.jtheta` supports arbitrary precision evaluation and complex
  7291. arguments::
  7292. >>> mp.dps = 50
  7293. >>> jtheta(4, sqrt(2), 0.5)
  7294. 2.0549510717571539127004115835148878097035750653737
  7295. >>> mp.dps = 25
  7296. >>> jtheta(4, 1+2j, (1+j)/5)
  7297. (7.180331760146805926356634 - 1.634292858119162417301683j)
  7298. Evaluation of derivatives::
  7299. >>> mp.dps = 25
  7300. >>> jtheta(1, 7, 0.25, 1); diff(lambda z: jtheta(1, z, 0.25), 7)
  7301. 1.209857192844475388637236
  7302. 1.209857192844475388637236
  7303. >>> jtheta(1, 7, 0.25, 2); diff(lambda z: jtheta(1, z, 0.25), 7, 2)
  7304. -0.2598718791650217206533052
  7305. -0.2598718791650217206533052
  7306. >>> jtheta(2, 7, 0.25, 1); diff(lambda z: jtheta(2, z, 0.25), 7)
  7307. -1.150231437070259644461474
  7308. -1.150231437070259644461474
  7309. >>> jtheta(2, 7, 0.25, 2); diff(lambda z: jtheta(2, z, 0.25), 7, 2)
  7310. -0.6226636990043777445898114
  7311. -0.6226636990043777445898114
  7312. >>> jtheta(3, 7, 0.25, 1); diff(lambda z: jtheta(3, z, 0.25), 7)
  7313. -0.9990312046096634316587882
  7314. -0.9990312046096634316587882
  7315. >>> jtheta(3, 7, 0.25, 2); diff(lambda z: jtheta(3, z, 0.25), 7, 2)
  7316. -0.1530388693066334936151174
  7317. -0.1530388693066334936151174
  7318. >>> jtheta(4, 7, 0.25, 1); diff(lambda z: jtheta(4, z, 0.25), 7)
  7319. 0.9820995967262793943571139
  7320. 0.9820995967262793943571139
  7321. >>> jtheta(4, 7, 0.25, 2); diff(lambda z: jtheta(4, z, 0.25), 7, 2)
  7322. 0.3936902850291437081667755
  7323. 0.3936902850291437081667755
  7324. **Possible issues**
  7325. For `|q| \ge 1` or `\Im(\tau) \le 0`, :func:`~mpmath.jtheta` raises
  7326. ``ValueError``. This exception is also raised for `|q|` extremely
  7327. close to 1 (or equivalently `\tau` very close to 0), since the
  7328. series would converge too slowly::
  7329. >>> jtheta(1, 10, 0.99999999 * exp(0.5*j))
  7330. Traceback (most recent call last):
  7331. ...
  7332. ValueError: abs(q) > THETA_Q_LIM = 1.000000
  7333. """
  7334. eulernum = r"""
  7335. Gives the `n`-th Euler number, defined as the `n`-th derivative of
  7336. `\mathrm{sech}(t) = 1/\cosh(t)` evaluated at `t = 0`. Equivalently, the
  7337. Euler numbers give the coefficients of the Taylor series
  7338. .. math ::
  7339. \mathrm{sech}(t) = \sum_{n=0}^{\infty} \frac{E_n}{n!} t^n.
  7340. The Euler numbers are closely related to Bernoulli numbers
  7341. and Bernoulli polynomials. They can also be evaluated in terms of
  7342. Euler polynomials (see :func:`~mpmath.eulerpoly`) as `E_n = 2^n E_n(1/2)`.
  7343. **Examples**
  7344. Computing the first few Euler numbers and verifying that they
  7345. agree with the Taylor series::
  7346. >>> from mpmath import *
  7347. >>> mp.dps = 25; mp.pretty = True
  7348. >>> [eulernum(n) for n in range(11)]
  7349. [1.0, 0.0, -1.0, 0.0, 5.0, 0.0, -61.0, 0.0, 1385.0, 0.0, -50521.0]
  7350. >>> chop(diffs(sech, 0, 10))
  7351. [1.0, 0.0, -1.0, 0.0, 5.0, 0.0, -61.0, 0.0, 1385.0, 0.0, -50521.0]
  7352. Euler numbers grow very rapidly. :func:`~mpmath.eulernum` efficiently
  7353. computes numerical approximations for large indices::
  7354. >>> eulernum(50)
  7355. -6.053285248188621896314384e+54
  7356. >>> eulernum(1000)
  7357. 3.887561841253070615257336e+2371
  7358. >>> eulernum(10**20)
  7359. 4.346791453661149089338186e+1936958564106659551331
  7360. Comparing with an asymptotic formula for the Euler numbers::
  7361. >>> n = 10**5
  7362. >>> (-1)**(n//2) * 8 * sqrt(n/(2*pi)) * (2*n/(pi*e))**n
  7363. 3.69919063017432362805663e+436961
  7364. >>> eulernum(n)
  7365. 3.699193712834466537941283e+436961
  7366. Pass ``exact=True`` to obtain exact values of Euler numbers as integers::
  7367. >>> print(eulernum(50, exact=True))
  7368. -6053285248188621896314383785111649088103498225146815121
  7369. >>> print(eulernum(200, exact=True) % 10**10)
  7370. 1925859625
  7371. >>> eulernum(1001, exact=True)
  7372. 0
  7373. """
  7374. eulerpoly = r"""
  7375. Evaluates the Euler polynomial `E_n(z)`, defined by the generating function
  7376. representation
  7377. .. math ::
  7378. \frac{2e^{zt}}{e^t+1} = \sum_{n=0}^\infty E_n(z) \frac{t^n}{n!}.
  7379. The Euler polynomials may also be represented in terms of
  7380. Bernoulli polynomials (see :func:`~mpmath.bernpoly`) using various formulas, for
  7381. example
  7382. .. math ::
  7383. E_n(z) = \frac{2}{n+1} \left(
  7384. B_n(z)-2^{n+1}B_n\left(\frac{z}{2}\right)
  7385. \right).
  7386. Special values include the Euler numbers `E_n = 2^n E_n(1/2)` (see
  7387. :func:`~mpmath.eulernum`).
  7388. **Examples**
  7389. Computing the coefficients of the first few Euler polynomials::
  7390. >>> from mpmath import *
  7391. >>> mp.dps = 25; mp.pretty = True
  7392. >>> for n in range(6):
  7393. ... chop(taylor(lambda z: eulerpoly(n,z), 0, n))
  7394. ...
  7395. [1.0]
  7396. [-0.5, 1.0]
  7397. [0.0, -1.0, 1.0]
  7398. [0.25, 0.0, -1.5, 1.0]
  7399. [0.0, 1.0, 0.0, -2.0, 1.0]
  7400. [-0.5, 0.0, 2.5, 0.0, -2.5, 1.0]
  7401. Evaluation for arbitrary `z`::
  7402. >>> eulerpoly(2,3)
  7403. 6.0
  7404. >>> eulerpoly(5,4)
  7405. 423.5
  7406. >>> eulerpoly(35, 11111111112)
  7407. 3.994957561486776072734601e+351
  7408. >>> eulerpoly(4, 10+20j)
  7409. (-47990.0 - 235980.0j)
  7410. >>> eulerpoly(2, '-3.5e-5')
  7411. 0.000035001225
  7412. >>> eulerpoly(3, 0.5)
  7413. 0.0
  7414. >>> eulerpoly(55, -10**80)
  7415. -1.0e+4400
  7416. >>> eulerpoly(5, -inf)
  7417. -inf
  7418. >>> eulerpoly(6, -inf)
  7419. +inf
  7420. Computing Euler numbers::
  7421. >>> 2**26 * eulerpoly(26,0.5)
  7422. -4087072509293123892361.0
  7423. >>> eulernum(26)
  7424. -4087072509293123892361.0
  7425. Evaluation is accurate for large `n` and small `z`::
  7426. >>> eulerpoly(100, 0.5)
  7427. 2.29047999988194114177943e+108
  7428. >>> eulerpoly(1000, 10.5)
  7429. 3.628120031122876847764566e+2070
  7430. >>> eulerpoly(10000, 10.5)
  7431. 1.149364285543783412210773e+30688
  7432. """
  7433. spherharm = r"""
  7434. Evaluates the spherical harmonic `Y_l^m(\theta,\phi)`,
  7435. .. math ::
  7436. Y_l^m(\theta,\phi) = \sqrt{\frac{2l+1}{4\pi}\frac{(l-m)!}{(l+m)!}}
  7437. P_l^m(\cos \theta) e^{i m \phi}
  7438. where `P_l^m` is an associated Legendre function (see :func:`~mpmath.legenp`).
  7439. Here `\theta \in [0, \pi]` denotes the polar coordinate (ranging
  7440. from the north pole to the south pole) and `\phi \in [0, 2 \pi]` denotes the
  7441. azimuthal coordinate on a sphere. Care should be used since many different
  7442. conventions for spherical coordinate variables are used.
  7443. Usually spherical harmonics are considered for `l \in \mathbb{N}`,
  7444. `m \in \mathbb{Z}`, `|m| \le l`. More generally, `l,m,\theta,\phi`
  7445. are permitted to be complex numbers.
  7446. .. note ::
  7447. :func:`~mpmath.spherharm` returns a complex number, even if the value is
  7448. purely real.
  7449. **Plots**
  7450. .. literalinclude :: /plots/spherharm40.py
  7451. `Y_{4,0}`:
  7452. .. image :: /plots/spherharm40.png
  7453. `Y_{4,1}`:
  7454. .. image :: /plots/spherharm41.png
  7455. `Y_{4,2}`:
  7456. .. image :: /plots/spherharm42.png
  7457. `Y_{4,3}`:
  7458. .. image :: /plots/spherharm43.png
  7459. `Y_{4,4}`:
  7460. .. image :: /plots/spherharm44.png
  7461. **Examples**
  7462. Some low-order spherical harmonics with reference values::
  7463. >>> from mpmath import *
  7464. >>> mp.dps = 25; mp.pretty = True
  7465. >>> theta = pi/4
  7466. >>> phi = pi/3
  7467. >>> spherharm(0,0,theta,phi); 0.5*sqrt(1/pi)*expj(0)
  7468. (0.2820947917738781434740397 + 0.0j)
  7469. (0.2820947917738781434740397 + 0.0j)
  7470. >>> spherharm(1,-1,theta,phi); 0.5*sqrt(3/(2*pi))*expj(-phi)*sin(theta)
  7471. (0.1221506279757299803965962 - 0.2115710938304086076055298j)
  7472. (0.1221506279757299803965962 - 0.2115710938304086076055298j)
  7473. >>> spherharm(1,0,theta,phi); 0.5*sqrt(3/pi)*cos(theta)*expj(0)
  7474. (0.3454941494713354792652446 + 0.0j)
  7475. (0.3454941494713354792652446 + 0.0j)
  7476. >>> spherharm(1,1,theta,phi); -0.5*sqrt(3/(2*pi))*expj(phi)*sin(theta)
  7477. (-0.1221506279757299803965962 - 0.2115710938304086076055298j)
  7478. (-0.1221506279757299803965962 - 0.2115710938304086076055298j)
  7479. With the normalization convention used, the spherical harmonics are orthonormal
  7480. on the unit sphere::
  7481. >>> sphere = [0,pi], [0,2*pi]
  7482. >>> dS = lambda t,p: fp.sin(t) # differential element
  7483. >>> Y1 = lambda t,p: fp.spherharm(l1,m1,t,p)
  7484. >>> Y2 = lambda t,p: fp.conj(fp.spherharm(l2,m2,t,p))
  7485. >>> l1 = l2 = 3; m1 = m2 = 2
  7486. >>> fp.chop(fp.quad(lambda t,p: Y1(t,p)*Y2(t,p)*dS(t,p), *sphere))
  7487. 1.0000000000000007
  7488. >>> m2 = 1 # m1 != m2
  7489. >>> print(fp.chop(fp.quad(lambda t,p: Y1(t,p)*Y2(t,p)*dS(t,p), *sphere)))
  7490. 0.0
  7491. Evaluation is accurate for large orders::
  7492. >>> spherharm(1000,750,0.5,0.25)
  7493. (3.776445785304252879026585e-102 - 5.82441278771834794493484e-102j)
  7494. Evaluation works with complex parameter values::
  7495. >>> spherharm(1+j, 2j, 2+3j, -0.5j)
  7496. (64.44922331113759992154992 + 1981.693919841408089681743j)
  7497. """
  7498. scorergi = r"""
  7499. Evaluates the Scorer function
  7500. .. math ::
  7501. \operatorname{Gi}(z) =
  7502. \operatorname{Ai}(z) \int_0^z \operatorname{Bi}(t) dt +
  7503. \operatorname{Bi}(z) \int_z^{\infty} \operatorname{Ai}(t) dt
  7504. which gives a particular solution to the inhomogeneous Airy
  7505. differential equation `f''(z) - z f(z) = 1/\pi`. Another
  7506. particular solution is given by the Scorer Hi-function
  7507. (:func:`~mpmath.scorerhi`). The two functions are related as
  7508. `\operatorname{Gi}(z) + \operatorname{Hi}(z) = \operatorname{Bi}(z)`.
  7509. **Plots**
  7510. .. literalinclude :: /plots/gi.py
  7511. .. image :: /plots/gi.png
  7512. .. literalinclude :: /plots/gi_c.py
  7513. .. image :: /plots/gi_c.png
  7514. **Examples**
  7515. Some values and limits::
  7516. >>> from mpmath import *
  7517. >>> mp.dps = 25; mp.pretty = True
  7518. >>> scorergi(0); 1/(power(3,'7/6')*gamma('2/3'))
  7519. 0.2049755424820002450503075
  7520. 0.2049755424820002450503075
  7521. >>> diff(scorergi, 0); 1/(power(3,'5/6')*gamma('1/3'))
  7522. 0.1494294524512754526382746
  7523. 0.1494294524512754526382746
  7524. >>> scorergi(+inf); scorergi(-inf)
  7525. 0.0
  7526. 0.0
  7527. >>> scorergi(1)
  7528. 0.2352184398104379375986902
  7529. >>> scorergi(-1)
  7530. -0.1166722172960152826494198
  7531. Evaluation for large arguments::
  7532. >>> scorergi(10)
  7533. 0.03189600510067958798062034
  7534. >>> scorergi(100)
  7535. 0.003183105228162961476590531
  7536. >>> scorergi(1000000)
  7537. 0.0000003183098861837906721743873
  7538. >>> 1/(pi*1000000)
  7539. 0.0000003183098861837906715377675
  7540. >>> scorergi(-1000)
  7541. -0.08358288400262780392338014
  7542. >>> scorergi(-100000)
  7543. 0.02886866118619660226809581
  7544. >>> scorergi(50+10j)
  7545. (0.0061214102799778578790984 - 0.001224335676457532180747917j)
  7546. >>> scorergi(-50-10j)
  7547. (5.236047850352252236372551e+29 - 3.08254224233701381482228e+29j)
  7548. >>> scorergi(100000j)
  7549. (-8.806659285336231052679025e+6474077 + 8.684731303500835514850962e+6474077j)
  7550. Verifying the connection between Gi and Hi::
  7551. >>> z = 0.25
  7552. >>> scorergi(z) + scorerhi(z)
  7553. 0.7287469039362150078694543
  7554. >>> airybi(z)
  7555. 0.7287469039362150078694543
  7556. Verifying the differential equation::
  7557. >>> for z in [-3.4, 0, 2.5, 1+2j]:
  7558. ... chop(diff(scorergi,z,2) - z*scorergi(z))
  7559. ...
  7560. -0.3183098861837906715377675
  7561. -0.3183098861837906715377675
  7562. -0.3183098861837906715377675
  7563. -0.3183098861837906715377675
  7564. Verifying the integral representation::
  7565. >>> z = 0.5
  7566. >>> scorergi(z)
  7567. 0.2447210432765581976910539
  7568. >>> Ai,Bi = airyai,airybi
  7569. >>> Bi(z)*(Ai(inf,-1)-Ai(z,-1)) + Ai(z)*(Bi(z,-1)-Bi(0,-1))
  7570. 0.2447210432765581976910539
  7571. **References**
  7572. 1. [DLMF]_ section 9.12: Scorer Functions
  7573. """
  7574. scorerhi = r"""
  7575. Evaluates the second Scorer function
  7576. .. math ::
  7577. \operatorname{Hi}(z) =
  7578. \operatorname{Bi}(z) \int_{-\infty}^z \operatorname{Ai}(t) dt -
  7579. \operatorname{Ai}(z) \int_{-\infty}^z \operatorname{Bi}(t) dt
  7580. which gives a particular solution to the inhomogeneous Airy
  7581. differential equation `f''(z) - z f(z) = 1/\pi`. See also
  7582. :func:`~mpmath.scorergi`.
  7583. **Plots**
  7584. .. literalinclude :: /plots/hi.py
  7585. .. image :: /plots/hi.png
  7586. .. literalinclude :: /plots/hi_c.py
  7587. .. image :: /plots/hi_c.png
  7588. **Examples**
  7589. Some values and limits::
  7590. >>> from mpmath import *
  7591. >>> mp.dps = 25; mp.pretty = True
  7592. >>> scorerhi(0); 2/(power(3,'7/6')*gamma('2/3'))
  7593. 0.4099510849640004901006149
  7594. 0.4099510849640004901006149
  7595. >>> diff(scorerhi,0); 2/(power(3,'5/6')*gamma('1/3'))
  7596. 0.2988589049025509052765491
  7597. 0.2988589049025509052765491
  7598. >>> scorerhi(+inf); scorerhi(-inf)
  7599. +inf
  7600. 0.0
  7601. >>> scorerhi(1)
  7602. 0.9722051551424333218376886
  7603. >>> scorerhi(-1)
  7604. 0.2206696067929598945381098
  7605. Evaluation for large arguments::
  7606. >>> scorerhi(10)
  7607. 455641153.5163291358991077
  7608. >>> scorerhi(100)
  7609. 6.041223996670201399005265e+288
  7610. >>> scorerhi(1000000)
  7611. 7.138269638197858094311122e+289529652
  7612. >>> scorerhi(-10)
  7613. 0.0317685352825022727415011
  7614. >>> scorerhi(-100)
  7615. 0.003183092495767499864680483
  7616. >>> scorerhi(100j)
  7617. (-6.366197716545672122983857e-9 + 0.003183098861710582761688475j)
  7618. >>> scorerhi(50+50j)
  7619. (-5.322076267321435669290334e+63 + 1.478450291165243789749427e+65j)
  7620. >>> scorerhi(-1000-1000j)
  7621. (0.0001591549432510502796565538 - 0.000159154943091895334973109j)
  7622. Verifying the differential equation::
  7623. >>> for z in [-3.4, 0, 2, 1+2j]:
  7624. ... chop(diff(scorerhi,z,2) - z*scorerhi(z))
  7625. ...
  7626. 0.3183098861837906715377675
  7627. 0.3183098861837906715377675
  7628. 0.3183098861837906715377675
  7629. 0.3183098861837906715377675
  7630. Verifying the integral representation::
  7631. >>> z = 0.5
  7632. >>> scorerhi(z)
  7633. 0.6095559998265972956089949
  7634. >>> Ai,Bi = airyai,airybi
  7635. >>> Bi(z)*(Ai(z,-1)-Ai(-inf,-1)) - Ai(z)*(Bi(z,-1)-Bi(-inf,-1))
  7636. 0.6095559998265972956089949
  7637. """
  7638. stirling1 = r"""
  7639. Gives the Stirling number of the first kind `s(n,k)`, defined by
  7640. .. math ::
  7641. x(x-1)(x-2)\cdots(x-n+1) = \sum_{k=0}^n s(n,k) x^k.
  7642. The value is computed using an integer recurrence. The implementation
  7643. is not optimized for approximating large values quickly.
  7644. **Examples**
  7645. Comparing with the generating function::
  7646. >>> from mpmath import *
  7647. >>> mp.dps = 25; mp.pretty = True
  7648. >>> taylor(lambda x: ff(x, 5), 0, 5)
  7649. [0.0, 24.0, -50.0, 35.0, -10.0, 1.0]
  7650. >>> [stirling1(5, k) for k in range(6)]
  7651. [0.0, 24.0, -50.0, 35.0, -10.0, 1.0]
  7652. Recurrence relation::
  7653. >>> n, k = 5, 3
  7654. >>> stirling1(n+1,k) + n*stirling1(n,k) - stirling1(n,k-1)
  7655. 0.0
  7656. The matrices of Stirling numbers of first and second kind are inverses
  7657. of each other::
  7658. >>> A = matrix(5, 5); B = matrix(5, 5)
  7659. >>> for n in range(5):
  7660. ... for k in range(5):
  7661. ... A[n,k] = stirling1(n,k)
  7662. ... B[n,k] = stirling2(n,k)
  7663. ...
  7664. >>> A * B
  7665. [1.0 0.0 0.0 0.0 0.0]
  7666. [0.0 1.0 0.0 0.0 0.0]
  7667. [0.0 0.0 1.0 0.0 0.0]
  7668. [0.0 0.0 0.0 1.0 0.0]
  7669. [0.0 0.0 0.0 0.0 1.0]
  7670. Pass ``exact=True`` to obtain exact values of Stirling numbers as integers::
  7671. >>> stirling1(42, 5)
  7672. -2.864498971768501633736628e+50
  7673. >>> print(stirling1(42, 5, exact=True))
  7674. -286449897176850163373662803014001546235808317440000
  7675. """
  7676. stirling2 = r"""
  7677. Gives the Stirling number of the second kind `S(n,k)`, defined by
  7678. .. math ::
  7679. x^n = \sum_{k=0}^n S(n,k) x(x-1)(x-2)\cdots(x-k+1)
  7680. The value is computed using integer arithmetic to evaluate a power sum.
  7681. The implementation is not optimized for approximating large values quickly.
  7682. **Examples**
  7683. Comparing with the generating function::
  7684. >>> from mpmath import *
  7685. >>> mp.dps = 25; mp.pretty = True
  7686. >>> taylor(lambda x: sum(stirling2(5,k) * ff(x,k) for k in range(6)), 0, 5)
  7687. [0.0, 0.0, 0.0, 0.0, 0.0, 1.0]
  7688. Recurrence relation::
  7689. >>> n, k = 5, 3
  7690. >>> stirling2(n+1,k) - k*stirling2(n,k) - stirling2(n,k-1)
  7691. 0.0
  7692. Pass ``exact=True`` to obtain exact values of Stirling numbers as integers::
  7693. >>> stirling2(52, 10)
  7694. 2.641822121003543906807485e+45
  7695. >>> print(stirling2(52, 10, exact=True))
  7696. 2641822121003543906807485307053638921722527655
  7697. """
  7698. squarew = r"""
  7699. Computes the square wave function using the definition:
  7700. .. math::
  7701. x(t) = A(-1)^{\left\lfloor{2t / P}\right\rfloor}
  7702. where `P` is the period of the wave and `A` is the amplitude.
  7703. **Examples**
  7704. Square wave with period = 2, amplitude = 1 ::
  7705. >>> from mpmath import *
  7706. >>> mp.dps = 25; mp.pretty = True
  7707. >>> squarew(0,1,2)
  7708. 1.0
  7709. >>> squarew(0.5,1,2)
  7710. 1.0
  7711. >>> squarew(1,1,2)
  7712. -1.0
  7713. >>> squarew(1.5,1,2)
  7714. -1.0
  7715. >>> squarew(2,1,2)
  7716. 1.0
  7717. """
  7718. trianglew = r"""
  7719. Computes the triangle wave function using the definition:
  7720. .. math::
  7721. x(t) = 2A\left(\frac{1}{2}-\left|1-2 \operatorname{frac}\left(\frac{x}{P}+\frac{1}{4}\right)\right|\right)
  7722. where :math:`\operatorname{frac}\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`
  7723. , `P` is the period of the wave, and `A` is the amplitude.
  7724. **Examples**
  7725. Triangle wave with period = 2, amplitude = 1 ::
  7726. >>> from mpmath import *
  7727. >>> mp.dps = 25; mp.pretty = True
  7728. >>> trianglew(0,1,2)
  7729. 0.0
  7730. >>> trianglew(0.25,1,2)
  7731. 0.5
  7732. >>> trianglew(0.5,1,2)
  7733. 1.0
  7734. >>> trianglew(1,1,2)
  7735. 0.0
  7736. >>> trianglew(1.5,1,2)
  7737. -1.0
  7738. >>> trianglew(2,1,2)
  7739. 0.0
  7740. """
  7741. sawtoothw = r"""
  7742. Computes the sawtooth wave function using the definition:
  7743. .. math::
  7744. x(t) = A\operatorname{frac}\left(\frac{t}{T}\right)
  7745. where :math:`\operatorname{frac}\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`,
  7746. `P` is the period of the wave, and `A` is the amplitude.
  7747. **Examples**
  7748. Sawtooth wave with period = 2, amplitude = 1 ::
  7749. >>> from mpmath import *
  7750. >>> mp.dps = 25; mp.pretty = True
  7751. >>> sawtoothw(0,1,2)
  7752. 0.0
  7753. >>> sawtoothw(0.5,1,2)
  7754. 0.25
  7755. >>> sawtoothw(1,1,2)
  7756. 0.5
  7757. >>> sawtoothw(1.5,1,2)
  7758. 0.75
  7759. >>> sawtoothw(2,1,2)
  7760. 0.0
  7761. """
  7762. unit_triangle = r"""
  7763. Computes the unit triangle using the definition:
  7764. .. math::
  7765. x(t) = A(-\left| t \right| + 1)
  7766. where `A` is the amplitude.
  7767. **Examples**
  7768. Unit triangle with amplitude = 1 ::
  7769. >>> from mpmath import *
  7770. >>> mp.dps = 25; mp.pretty = True
  7771. >>> unit_triangle(-1,1)
  7772. 0.0
  7773. >>> unit_triangle(-0.5,1)
  7774. 0.5
  7775. >>> unit_triangle(0,1)
  7776. 1.0
  7777. >>> unit_triangle(0.5,1)
  7778. 0.5
  7779. >>> unit_triangle(1,1)
  7780. 0.0
  7781. """
  7782. sigmoid = r"""
  7783. Computes the sigmoid function using the definition:
  7784. .. math::
  7785. x(t) = \frac{A}{1 + e^{-t}}
  7786. where `A` is the amplitude.
  7787. **Examples**
  7788. Sigmoid function with amplitude = 1 ::
  7789. >>> from mpmath import *
  7790. >>> mp.dps = 25; mp.pretty = True
  7791. >>> sigmoid(-1,1)
  7792. 0.2689414213699951207488408
  7793. >>> sigmoid(-0.5,1)
  7794. 0.3775406687981454353610994
  7795. >>> sigmoid(0,1)
  7796. 0.5
  7797. >>> sigmoid(0.5,1)
  7798. 0.6224593312018545646389006
  7799. >>> sigmoid(1,1)
  7800. 0.7310585786300048792511592
  7801. """