function [ ampl, theta ] = r8_aimp ( x )

%*****************************************************************************80
%
%% r8_aimp() evaluates the modulus and phase of the Airy function.
%
%  Description:
%
%    This function must only be called when X <= -1.0.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 September 2011
%
%  Author:
%
%    Original FORTRAN77 version by Wayne Fullerton.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Wayne Fullerton,
%    Portable Special Function Routines,
%    in Portability of Numerical Software,
%    edited by Wayne Cowell,
%    Lecture Notes in Computer Science, Volume 57,
%    Springer 1977,
%    ISBN: 978-3-540-08446-4,
%    LC: QA297.W65.
%
%  Input:
%
%    real X, the argument.
%
%  Output:
%
%    real AMPL, PHI, the modulus and phase of the
%    Airy function.
%
  persistent am20cs
  persistent am21cs
  persistent am22cs
  persistent ath0cs
  persistent ath1cs
  persistent ath2cs
  persistent nam20
  persistent nam21
  persistent nam22
  persistent nath0
  persistent nath1
  persistent nath2
  persistent xsml

  if ( isempty ( nam20 ) )

    am20cs = [ ...
      +0.108716749086561856615730588125E-01, ...
      +0.369489228982663555091728665146E-03, ...
      +0.440680100484689563667507001327E-05, ...
      +0.143686762361911153929183952833E-06, ...
      +0.824275552390078308670628855353E-08, ...
      +0.684426758893661606173927278180E-09, ...
      +0.739566697282739287731004740213E-10, ...
      +0.974595633696825017638702600847E-11, ...
      +0.150076885829405775650973119497E-11, ...
      +0.262147910221527634206252854802E-12, ...
      +0.508354111376487180357278966914E-13, ...
      +0.107684753358811440492985997070E-13, ...
      +0.246091286618433429335914062617E-14, ...
      +0.600786380358656418436110373550E-15, ...
      +0.155449156102388071150651388384E-15, ...
      +0.423535125035576604426382780182E-16, ...
      +0.120862166289299840154401109189E-16, ...
      +0.359609651214658240861499706423E-17, ...
      +0.111134218386395638261774604677E-17, ...
      +0.355559532432366609893680289225E-18, ...
      +0.117433021600139309998766947387E-18, ...
      +0.399397454661077561389162200966E-19, ...
      +0.139576671528916310425606325640E-19, ...
      +0.500240055309236041393459280716E-20, ...
      +0.183552760958132679184834866457E-20, ...
      +0.688490998179202743197790112404E-21, ...
      +0.263631035611417012359996885105E-21, ...
      +0.102924890237338360287153563785E-21, ...
      +0.409246966671594885489762960571E-22, ...
      +0.165558573406734651039727903828E-22, ...
      +0.680797467063033356116599685727E-23, ...
      +0.284326559934079832419751134476E-23, ...
      +0.120507398348965255097287818819E-23, ...
      +0.517961243287505217976613610424E-24, ...
      +0.225622613427562816303268640887E-24, ...
      +0.995418801147745168832117078246E-25, ...
      +0.444551696397342424308280582053E-25, ...
      +0.200865195461501101425916097338E-25, ...
      +0.917786344151775165973885645402E-26, ...
      +0.423872958105589240661672197948E-26, ...
      +0.197789272007846092370846251490E-26, ...
      +0.932116351284620665680435253373E-27, ...
      +0.443482133249918099955611379722E-27, ...
      +0.212945672365573895594589552837E-27, ...
      +0.103158569651075977552209344907E-27, ...
      +0.504023773022591199157904590029E-28, ...
      +0.248301304570155945304046541005E-28, ...
      +0.123301783128562196054198238560E-28, ...
      +0.617033449920521746121976730507E-29, ...
      +0.311092617415918897233869792213E-29, ...
      +0.157983085201706173015269071503E-29, ...
      +0.807931987538283607678121339092E-30, ...
      +0.415997394138667562722951360052E-30, ...
      +0.215610934097716900471935862504E-30, ...
      +0.112468857265869178296752823613E-30, ...
      +0.590331560632838091123040811797E-31, ...
      +0.311735667692928562046280505333E-31 ]';
    am21cs = [ ...
      +0.592790266721309588375717482814E-02, ...
      +0.200569405393165186428695217690E-02, ...
      +0.911081850262275893553072526291E-04, ...
      +0.849894306372047155633172107475E-05, ...
      +0.113297908976913076637929215494E-05, ...
      +0.187517946100666496180950627804E-06, ...
      +0.359306519018245832699035211192E-07, ...
      +0.765757714071683864039093517470E-08, ...
      +0.176999967168039173925953460744E-08, ...
      +0.436259555654598932720546585535E-09, ...
      +0.113291641337853230035520085219E-09, ...
      +0.307257690982419244137868398126E-10, ...
      +0.864482416482201075541200465766E-11, ...
      +0.251015250060924402115104562212E-11, ...
      +0.749102496764440371601802227751E-12, ...
      +0.228996928487994073089565214432E-12, ...
      +0.715113658927987694949327491175E-13, ...
      +0.227607924959566841946395165061E-13, ...
      +0.736942142760886513969953227782E-14, ...
      +0.242328675267827490463991742006E-14, ...
      +0.808153774548239869283406558403E-15, ...
      +0.273008079804356086659174563386E-15, ...
      +0.933236070891385318473519474326E-16, ...
      +0.322508099681084622213867546973E-16, ...
      +0.112581932346444541217757573416E-16, ...
      +0.396699463986938821660259459530E-17, ...
      +0.141006567944319504660865034527E-17, ...
      +0.505302086537851213375537393032E-18, ...
      +0.182461523215945141197999102789E-18, ...
      +0.663584568262130466928029121642E-19, ...
      +0.242963731631276179741747455826E-19, ...
      +0.895238915123687802013669922963E-20, ...
      +0.331845289350050791260229250755E-20, ...
      +0.123706196188658315384437905922E-20, ...
      +0.463636677012390840306767734243E-21, ...
      +0.174653135947764475469758765989E-21, ...
      +0.661116810234991176307910643111E-22, ...
      +0.251409918994072486176125666459E-22, ...
      +0.960274995571732568694034386998E-23, ...
      +0.368324952289296395686436898078E-23, ...
      +0.141843138269159136145535939553E-23, ...
      +0.548342674276935830106345800990E-24, ...
      +0.212761054623118806650372562616E-24, ...
      +0.828443700849418591487734760953E-25, ...
      +0.323670563926127001421028600927E-25, ...
      +0.126868882963286057355055062493E-25, ...
      +0.498843818992121626935068934362E-26, ...
      +0.196734584467649390967119381790E-26, ...
      +0.778135971020326957713212064836E-27, ...
      +0.308633941498911152919192968451E-27, ...
      +0.122744647045453119789338037234E-27, ...
      +0.489431279134292205885241216204E-28, ...
      +0.195646879802909821175925099724E-28, ...
      +0.783988952922426171166311492266E-29, ...
      +0.314896914002484223748298978099E-29, ...
      +0.126769763137250681307067842559E-29, ...
      +0.511470691906900141641632107724E-30, ...
      +0.206801709795538770250900316706E-30, ...
      +0.837891344768519001325996867583E-31, ...
      +0.340168991971489802052339079577E-31 ]';
    am22cs = [ ...
      -0.156284448062534112753545828583E-01, ...
      +0.778336445239681307018943100334E-02, ...
      +0.867057770477189528406072812110E-03, ...
      +0.156966273156113719469953482266E-03, ...
      +0.356396257143286511324100666302E-04, ...
      +0.924598335425043154495080090994E-05, ...
      +0.262110161850422389523194982066E-05, ...
      +0.791882216516012561489469982263E-06, ...
      +0.251041527921011847803162690862E-06, ...
      +0.826522320665407734472997712940E-07, ...
      +0.280571166281305264396384290014E-07, ...
      +0.976821090484680786674631273890E-08, ...
      +0.347407923227710343287279035573E-08, ...
      +0.125828132169836914219092738164E-08, ...
      +0.462988260641895264497330784625E-09, ...
      +0.172728258813604072468143128696E-09, ...
      +0.652319200131154135148574124970E-10, ...
      +0.249047168520982056019881087112E-10, ...
      +0.960156820553765948078189890126E-11, ...
      +0.373448002067726856974776596757E-11, ...
      +0.146417565032053391722216189678E-11, ...
      +0.578265471168512825475827881553E-12, ...
      +0.229915407244706118560254184494E-12, ...
      +0.919780711231997257150883662365E-13, ...
      +0.370060068813090065807504045556E-13, ...
      +0.149675761698672987823326345205E-13, ...
      +0.608361194938461148720451399443E-14, ...
      +0.248404087115121397635425326873E-14, ...
      +0.101862476526769080727914465339E-14, ...
      +0.419383856352753989429640310957E-15, ...
      +0.173318901762930756149702493501E-15, ...
      +0.718821902388508517820445406811E-16, ...
      +0.299123633598403607712470896113E-16, ...
      +0.124868990433238627855713110880E-16, ...
      +0.522829344609483661928651193632E-17, ...
      +0.219532961724713396595998454359E-17, ...
      +0.924298325229777281154410024332E-18, ...
      +0.390157708236091407825543197309E-18, ...
      +0.165093892693863707213759030367E-18, ...
      +0.700221815715994367565716554487E-19, ...
      +0.297651833616786915573214963506E-19, ...
      +0.126796539086902072571134261229E-19, ...
      +0.541243400697077628687581725061E-20, ...
      +0.231487350218155252296382133283E-20, ...
      +0.991920288386566563462623851167E-21, ...
      +0.425803015323732357158897608174E-21, ...
      +0.183101842973024501678402003088E-21, ...
      +0.788678712311075375564526811022E-22, ...
      +0.340254607386229874956582997235E-22, ...
      +0.147020881405712530791860892535E-22, ...
      +0.636211018324916957733348071767E-23, ...
      +0.275707050680980721919395987768E-23, ...
      +0.119645858090104071356261780457E-23, ...
      +0.519912545729242147981768210567E-24, ...
      +0.226217674847104475260575286850E-24, ...
      +0.985526113754431819448565068283E-25, ...
      +0.429870630332508717223681286187E-25, ...
      +0.187723641661580639829657670189E-25, ...
      +0.820721941772842137268801052115E-26, ...
      +0.359214665604615507812767944463E-26, ...
      +0.157390594612773315611458940587E-26, ...
      +0.690329781039333834965319153586E-27, ...
      +0.303092079078968534607859331415E-27, ...
      +0.133204934160481219185689121944E-27, ...
      +0.585978836851523490117937981442E-28, ...
      +0.258016868489487806338425080457E-28, ...
      +0.113712433637283667223632182863E-28, ...
      +0.501592557226068509236430548549E-29, ...
      +0.221445829395509373322569708484E-29, ...
      +0.978470283886507289984691416411E-30, ...
      +0.432695414934180170112000952983E-30, ...
      +0.191497288193994570612929860440E-30, ...
      +0.848164622402392354171298331562E-31, ...
      +0.375947065173955919947455052934E-31 ]';
    ath0cs = [ ...
      -0.8172601764161634499840208700543E-01, ...
      -0.8004012824788273287596481113068E-03, ...
      -0.3186525268782113203795553628242E-05, ...
      -0.6688388266477509330741698865033E-07, ...
      -0.2931759284994564516506822463184E-08, ...
      -0.2011263760883621669049030307186E-09, ...
      -0.1877522678055973426074008166652E-10, ...
      -0.2199637137704601251899002199848E-11, ...
      -0.3071616682592272449025746605586E-12, ...
      -0.4936140553673418361025600985389E-13, ...
      -0.8902833722583660416935236969866E-14, ...
      -0.1768987764615272613656814199467E-14, ...
      -0.3817868689032277014678199609600E-15, ...
      -0.8851159014819947594156286509984E-16, ...
      -0.2184818181414365953149677679568E-16, ...
      -0.5700849046986452380599442295119E-17, ...
      -0.1563121122177875392516031795495E-17, ...
      -0.4481437996768995067906688776353E-18, ...
      -0.1337794883736188022044566044098E-18, ...
      -0.4143340036874114453776852445442E-19, ...
      -0.1327263385718805025080481164652E-19, ...
      -0.4385728589128440522215756835955E-20, ...
      -0.1491360695952818067686201743956E-20, ...
      -0.5208104738630711377154238188773E-21, ...
      -0.1864382222390498923872526604979E-21, ...
      -0.6830263751167969012975435381881E-22, ...
      -0.2557117058029329629296207591347E-22, ...
      -0.9770158640254300218246907254046E-23, ...
      -0.3805161433416679084068428254886E-23, ...
      -0.1509022750737054063493926482995E-23, ...
      -0.6087551341242424929005568014525E-24, ...
      -0.2495879513809711495425982124058E-24, ...
      -0.1039157654581920948909588084274E-24, ...
      -0.4390235913976846536974594969051E-25, ...
      -0.1880790678447990211675826820582E-25, ...
      -0.8165070764199462948863022205753E-26, ...
      -0.3589944503749750514266435585041E-26, ...
      -0.1597658126632132872981291608708E-26, ...
      -0.7193250175703823969113802835305E-27, ...
      -0.3274943012727856506209351132721E-27, ...
      -0.1507042445783690665816975047272E-27, ...
      -0.7006624198319904717843967949140E-28, ...
      -0.3289907402983718226528815678356E-28, ...
      -0.1559518084365146526445322711496E-28, ...
      -0.7460690508208254582833851119721E-29, ...
      -0.3600877034824662020563277249431E-29, ...
      -0.1752851437473772257350402219197E-29, ...
      -0.8603275775188512909623778628724E-30, ...
      -0.4256432603226946534668039480105E-30, ...
      -0.2122161865044262927723650698206E-30, ...
      -0.1065996156704879052472060798561E-30, ...
      -0.5393568608816949116410688086892E-31, ...
      -0.2748174851043954822278496517870E-31 ]';
    ath1cs = [ ...
      -0.6972849916208883845888148415037E-01, ...
      -0.5108722790650044987073448077961E-02, ...
      -0.8644335996989755094525334749512E-04, ...
      -0.5604720044235263542188698916125E-05, ...
      -0.6045735125623897409156376640077E-06, ...
      -0.8639802632488334393219721138499E-07, ...
      -0.1480809484309927157147782480780E-07, ...
      -0.2885809334577236039999449908712E-08, ...
      -0.6191631975665699609309191231800E-09, ...
      -0.1431992808860957830931365259879E-09, ...
      -0.3518141102137214721504616874321E-10, ...
      -0.9084761919955078290070339808051E-11, ...
      -0.2446171672688598449343283664767E-11, ...
      -0.6826083203213446240828996710264E-12, ...
      -0.1964579931194940171278546257802E-12, ...
      -0.5808933227139693164009191265856E-13, ...
      -0.1759042249527441992795400959024E-13, ...
      -0.5440902932714896613632538945319E-14, ...
      -0.1715247407486806802622358519451E-14, ...
      -0.5500929233576991546871101847161E-15, ...
      -0.1791878287739317259495152638754E-15, ...
      -0.5920372520086694197778411062231E-16, ...
      -0.1981713027876483962470972206590E-16, ...
      -0.6713232347016352262049984343790E-17, ...
      -0.2299450243658281116122358619832E-17, ...
      -0.7957300928236376595304637145634E-18, ...
      -0.2779994027291784157172290233739E-18, ...
      -0.9798924361326985224406795480814E-19, ...
      -0.3482717006061574386702645565849E-19, ...
      -0.1247489122558599057173300058084E-19, ...
      -0.4501210041478228113487751824452E-20, ...
      -0.1635346244013352135596114164667E-20, ...
      -0.5980102897780336268098762265941E-21, ...
      -0.2200246286286123454028196295475E-21, ...
      -0.8142463073515085897408205291519E-22, ...
      -0.3029924773660042537432330709674E-22, ...
      -0.1133390098574623537722943969689E-22, ...
      -0.4260766024749295719283049889791E-23, ...
      -0.1609363396278189718797500634453E-23, ...
      -0.6106377190825026293045330444287E-24, ...
      -0.2326954318021694061836577887573E-24, ...
      -0.8903987877472252604474129558186E-25, ...
      -0.3420558530005675024117914752341E-25, ...
      -0.1319026715257272659017212100607E-25, ...
      -0.5104899493612043091316191177386E-26, ...
      -0.1982599478474547451242444663466E-26, ...
      -0.7725702356880830535636111851519E-27, ...
      -0.3020234733664680100815776863573E-27, ...
      -0.1184379739074169993712946380800E-27, ...
      -0.4658430227922308520573252840106E-28, ...
      -0.1837554188100384647157502006613E-28, ...
      -0.7268566894427990953321876684800E-29, ...
      -0.2882863120391468135527089875626E-29, ...
      -0.1146374629459906350417591664639E-29, ...
      -0.4570031437748533058179991688533E-30, ...
      -0.1826276602045346104809934028799E-30, ...
      -0.7315349993385250469111066350933E-31, ...
      -0.2936925599971429781637815773866E-31 ]';
    ath2cs = [ ...
      +0.4405273458718778997061127057775E-02, ...
      -0.3042919452318454608483844239873E-01, ...
      -0.1385653283771793791602692842653E-02, ...
      -0.1804443908954952302670486910952E-03, ...
      -0.3380847108327308671057465323618E-04, ...
      -0.7678183535229023055257676817765E-05, ...
      -0.1967839443716035324690935417077E-05, ...
      -0.5483727115877700361586143659281E-06, ...
      -0.1625461550532612452712696212258E-06, ...
      -0.5053049981268895015277637842078E-07, ...
      -0.1631580701124066881183851715617E-07, ...
      -0.5434204112348517507963436694817E-08, ...
      -0.1857398556409900325763850109630E-08, ...
      -0.6489512033326108816213513640676E-09, ...
      -0.2310594885800944720482995987079E-09, ...
      -0.8363282183204411682819329546745E-10, ...
      -0.3071196844890191462660661303891E-10, ...
      -0.1142367142432716819409514579892E-10, ...
      -0.4298116066345803065822470108971E-11, ...
      -0.1633898699596715440601646086632E-11, ...
      -0.6269328620016619432123443754076E-12, ...
      -0.2426052694816257357356159203991E-12, ...
      -0.9461198321624039090742527765052E-13, ...
      -0.3716060313411504806847798281269E-13, ...
      -0.1469155684097526763170138810309E-13, ...
      -0.5843694726140911944556401363094E-14, ...
      -0.2337502595591951298832675034934E-14, ...
      -0.9399231371171435401160167358411E-15, ...
      -0.3798014669372894500076335263715E-15, ...
      -0.1541731043984972524883443681775E-15, ...
      -0.6285287079535307162925662365202E-16, ...
      -0.2572731812811455424755383992774E-16, ...
      -0.1057098119354017809340974866555E-16, ...
      -0.4359080267402696966695992699964E-17, ...
      -0.1803634315959978013953176945540E-17, ...
      -0.7486838064380536821719431676914E-18, ...
      -0.3117261367347604656799597209985E-18, ...
      -0.1301687980927700734792871620696E-18, ...
      -0.5450527587519522468973883909909E-19, ...
      -0.2288293490114231872268635931903E-19, ...
      -0.9631059503829538655655060440088E-20, ...
      -0.4063281001524614089092195416434E-20, ...
      -0.1718203980908026763900413858510E-20, ...
      -0.7281574619892536367415322473328E-21, ...
      -0.3092352652680643127960680345790E-21, ...
      -0.1315917855965440490383417023254E-21, ...
      -0.5610606786087055512664907412668E-22, ...
      -0.2396621894086355206020304337895E-22, ...
      -0.1025574332390581200832954423924E-22, ...
      -0.4396264138143656476403607323663E-23, ...
      -0.1887652998372577373342508719450E-23, ...
      -0.8118140359576807603579433230445E-24, ...
      -0.3496734274366286856375952089214E-24, ...
      -0.1508402925156873215171751475867E-24, ...
      -0.6516268284778671059787773834341E-25, ...
      -0.2818945797529207424505942114583E-25, ...
      -0.1221127596512262744598094464505E-25, ...
      -0.5296674341169867168620011705073E-26, ...
      -0.2300359270773673431358870971744E-26, ...
      -0.1000279482355367494781220348930E-26, ...
      -0.4354760404180879394806893162179E-27, ...
      -0.1898056134741477522515482827030E-27, ...
      -0.8282111868712974697554009309315E-28, ...
      -0.3617815493066569006586213484374E-28, ...
      -0.1582018896178003654858941843636E-28, ...
      -0.6925068597802270011772820383247E-29, ...
      -0.3034390239778629128908629727335E-29, ...
      -0.1330889568166725224761977446509E-29, ...
      -0.5842848522173090120487606971706E-30, ...
      -0.2567488423238302631121274357678E-30, ...
      -0.1129232322268882185791505819151E-30, ...
      -0.4970947029753336916550570105023E-31 ]';

    eta = 0.1 * r8_mach ( 3 );
    nam20 = r8_inits ( am20cs, 57, eta );
    nath0 = r8_inits ( ath0cs, 53, eta );
    nam21 = r8_inits ( am21cs, 60, eta );
    nath1 = r8_inits ( ath1cs, 58, eta );
    nam22 = r8_inits ( am22cs, 74, eta );
    nath2 = r8_inits ( ath2cs, 72, eta );
    xsml = - ( 128.0 / r8_mach ( 3 ) )^0.3333;

  end

  if ( x <= xsml )
    z = 1.0;
    ampl = 0.3125 + r8_csevl ( z, am20cs, nam20 );
    theta = - 0.625 + r8_csevl ( z, ath0cs, nath0 );
  elseif ( x < - 4.0 )
    z = 128.0 / x / x / x + 1.0;
    ampl = 0.3125 + r8_csevl ( z, am20cs, nam20 );
    theta = - 0.625 + r8_csevl ( z, ath0cs, nath0 );
  elseif ( x < - 2.0 )
    z = ( 128.0 / x / x / x + 9.0 ) / 7.0;
    ampl = 0.3125 + r8_csevl ( z, am21cs, nam21 );
    theta = - 0.625 + r8_csevl ( z, ath1cs, nath1 );
  elseif ( x <= - 1.0 )
    z = ( 16.0 / x / x / x + 9.0 ) / 7.0;
    ampl = 0.3125 + r8_csevl ( z, am22cs, nam22 );
    theta = - 0.625 + r8_csevl ( z, ath2cs, nath2 );
  else
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8_AIMP - Fatal error!\n' );
    fprintf ( 1, '  -1.0 < X.\n' );
    error ( 'R8_AIMP - Fatal error!' )
  end

  sqrtx = sqrt ( - x );
  ampl = sqrt ( ampl / sqrtx );
  theta = 0.25 * pi - x * sqrtx * theta;

  return
end
