libmut_win.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208
  1. /*
  2. * Copyright 1996-2006 Catherine Loader.
  3. */
  4. #include "mex.h"
  5. /*
  6. * Copyright 1996-2006 Catherine Loader.
  7. */
  8. #include <math.h>
  9. #include "mut.h"
  10. /* stirlerr(n) = log(n!) - log( sqrt(2*pi*n)*(n/e)^n ) */
  11. #define S0 0.083333333333333333333 /* 1/12 */
  12. #define S1 0.00277777777777777777778 /* 1/360 */
  13. #define S2 0.00079365079365079365079365 /* 1/1260 */
  14. #define S3 0.000595238095238095238095238 /* 1/1680 */
  15. #define S4 0.0008417508417508417508417508 /* 1/1188 */
  16. /*
  17. error for 0, 0.5, 1.0, 1.5, ..., 14.5, 15.0.
  18. */
  19. static double sferr_halves[31] = {
  20. 0.0, /* n=0 - wrong, place holder only */
  21. 0.1534264097200273452913848, /* 0.5 */
  22. 0.0810614667953272582196702, /* 1.0 */
  23. 0.0548141210519176538961390, /* 1.5 */
  24. 0.0413406959554092940938221, /* 2.0 */
  25. 0.03316287351993628748511048, /* 2.5 */
  26. 0.02767792568499833914878929, /* 3.0 */
  27. 0.02374616365629749597132920, /* 3.5 */
  28. 0.02079067210376509311152277, /* 4.0 */
  29. 0.01848845053267318523077934, /* 4.5 */
  30. 0.01664469118982119216319487, /* 5.0 */
  31. 0.01513497322191737887351255, /* 5.5 */
  32. 0.01387612882307074799874573, /* 6.0 */
  33. 0.01281046524292022692424986, /* 6.5 */
  34. 0.01189670994589177009505572, /* 7.0 */
  35. 0.01110455975820691732662991, /* 7.5 */
  36. 0.010411265261972096497478567, /* 8.0 */
  37. 0.009799416126158803298389475, /* 8.5 */
  38. 0.009255462182712732917728637, /* 9.0 */
  39. 0.008768700134139385462952823, /* 9.5 */
  40. 0.008330563433362871256469318, /* 10.0 */
  41. 0.007934114564314020547248100, /* 10.5 */
  42. 0.007573675487951840794972024, /* 11.0 */
  43. 0.007244554301320383179543912, /* 11.5 */
  44. 0.006942840107209529865664152, /* 12.0 */
  45. 0.006665247032707682442354394, /* 12.5 */
  46. 0.006408994188004207068439631, /* 13.0 */
  47. 0.006171712263039457647532867, /* 13.5 */
  48. 0.005951370112758847735624416, /* 14.0 */
  49. 0.005746216513010115682023589, /* 14.5 */
  50. 0.005554733551962801371038690 /* 15.0 */
  51. };
  52. double stirlerr(n)
  53. double n;
  54. { double nn;
  55. if (n<15.0)
  56. { nn = 2.0*n;
  57. if (nn==(int)nn) return(sferr_halves[(int)nn]);
  58. return(mut_lgamma(n+1.0) - (n+0.5)*log((double)n)+n - HF_LG_PIx2);
  59. }
  60. nn = (double)n;
  61. nn = nn*nn;
  62. if (n>500) return((S0-S1/nn)/n);
  63. if (n>80) return((S0-(S1-S2/nn)/nn)/n);
  64. if (n>35) return((S0-(S1-(S2-S3/nn)/nn)/nn)/n);
  65. return((S0-(S1-(S2-(S3-S4/nn)/nn)/nn)/nn)/n);
  66. }
  67. double bd0(x,np)
  68. double x, np;
  69. { double ej, s, s1, v;
  70. int j;
  71. if (fabs(x-np)<0.1*(x+np))
  72. {
  73. s = (x-np)*(x-np)/(x+np);
  74. v = (x-np)/(x+np);
  75. ej = 2*x*v; v = v*v;
  76. for (j=1; ;++j)
  77. { ej *= v;
  78. s1 = s+ej/((j<<1)+1);
  79. if (s1==s) return(s1);
  80. s = s1;
  81. }
  82. }
  83. return(x*log(x/np)+np-x);
  84. }
  85. /*
  86. Raw binomial probability calculation.
  87. (1) This has both p and q arguments, when one may be represented
  88. more accurately than the other (in particular, in df()).
  89. (2) This should NOT check that inputs x and n are integers. This
  90. should be done in the calling function, where necessary.
  91. (3) Does not check for 0<=p<=1 and 0<=q<=1 or NaN's. Do this in
  92. the calling function.
  93. */
  94. double dbinom_raw(x,n,p,q,give_log)
  95. double x, n, p, q;
  96. int give_log;
  97. { double f, lc;
  98. if (p==0.0) return((x==0) ? D_1 : D_0);
  99. if (q==0.0) return((x==n) ? D_1 : D_0);
  100. if (x==0)
  101. { lc = (p<0.1) ? -bd0(n,n*q) - n*p : n*log(q);
  102. return( DEXP(lc) );
  103. }
  104. if (x==n)
  105. { lc = (q<0.1) ? -bd0(n,n*p) - n*q : n*log(p);
  106. return( DEXP(lc) );
  107. }
  108. if ((x<0) | (x>n)) return( D_0 );
  109. lc = stirlerr(n) - stirlerr(x) - stirlerr(n-x)
  110. - bd0(x,n*p) - bd0(n-x,n*q);
  111. f = (PIx2*x*(n-x))/n;
  112. return( FEXP(f,lc) );
  113. }
  114. double dbinom(x,n,p,give_log)
  115. int x, n;
  116. double p;
  117. int give_log;
  118. {
  119. if ((p<0) | (p>1) | (n<0)) return(INVALID_PARAMS);
  120. if (x<0) return( D_0 );
  121. return( dbinom_raw((double)x,(double)n,p,1-p,give_log) );
  122. }
  123. /*
  124. Poisson probability lb^x exp(-lb) / x!.
  125. I don't check that x is an integer, since other functions
  126. that call dpois_raw() (i.e. dgamma) may use a fractional
  127. x argument.
  128. */
  129. double dpois_raw(x,lambda,give_log)
  130. int give_log;
  131. double x, lambda;
  132. {
  133. if (lambda==0) return( (x==0) ? D_1 : D_0 );
  134. if (x==0) return( DEXP(-lambda) );
  135. if (x<0) return( D_0 );
  136. return(FEXP( PIx2*x, -stirlerr(x)-bd0(x,lambda) ));
  137. }
  138. double dpois(x,lambda,give_log)
  139. int x, give_log;
  140. double lambda;
  141. {
  142. if (lambda<0) return(INVALID_PARAMS);
  143. if (x<0) return( D_0 );
  144. return( dpois_raw((double)x,lambda,give_log) );
  145. }
  146. double dbeta(x,a,b,give_log)
  147. double x, a, b;
  148. int give_log;
  149. { double f, p;
  150. if ((a<=0) | (b<=0)) return(INVALID_PARAMS);
  151. if ((x<=0) | (x>=1)) return(D_0);
  152. if (a<1)
  153. { if (b<1) /* a<1, b<1 */
  154. { f = a*b/((a+b)*x*(1-x));
  155. p = dbinom_raw(a,a+b,x,1-x,give_log);
  156. }
  157. else /* a<1, b>=1 */
  158. { f = a/x;
  159. p = dbinom_raw(a,a+b-1,x,1-x,give_log);
  160. }
  161. }
  162. else
  163. { if (b<1) /* a>=1, b<1 */
  164. { f = b/(1-x);
  165. p = dbinom_raw(a-1,a+b-1,x,1-x,give_log);
  166. }
  167. else /* a>=1, b>=1 */
  168. { f = a+b-1;
  169. p = dbinom_raw(a-1,(a-1)+(b-1),x,1-x,give_log);
  170. }
  171. }
  172. return( (give_log) ? p + log(f) : p*f );
  173. }
  174. /*
  175. * To evaluate the F density, write it as a Binomial probability
  176. * with p = x*m/(n+x*m). For m>=2, use the simplest conversion.
  177. * For m<2, (m-2)/2<0 so the conversion will not work, and we must use
  178. * a second conversion. Note the division by p; this seems unavoidable
  179. * for m < 2, since the F density has a singularity as x (or p) -> 0.
  180. */
  181. double df(x,m,n,give_log)
  182. double x, m, n;
  183. int give_log;
  184. { double p, q, f, dens;
  185. if ((m<=0) | (n<=0)) return(INVALID_PARAMS);
  186. if (x <= 0.0) return(D_0);
  187. f = 1.0/(n+x*m);
  188. q = n*f;
  189. p = x*m*f;
  190. if (m>=2)
  191. { f = m*q/2;
  192. dens = dbinom_raw((m-2)/2.0, (m+n-2)/2.0, p, q, give_log);
  193. }
  194. else
  195. { f = m*m*q / (2*p*(m+n));
  196. dens = dbinom_raw(m/2.0, (m+n)/2.0, p, q, give_log);
  197. }
  198. return((give_log) ? log(f)+dens : f*dens);
  199. }
  200. /*
  201. * Gamma density,
  202. * lb^r x^{r-1} exp(-lb*x)
  203. * p(x;r,lb) = -----------------------
  204. * (r-1)!
  205. *
  206. * If USE_SCALE is defined below, the lb argument will be interpreted
  207. * as a scale parameter (i.e. replace lb by 1/lb above). Otherwise,
  208. * it is interpreted as a rate parameter, as above.
  209. */
  210. /* #define USE_SCALE */
  211. double dgamma(x,r,lambda,give_log)
  212. int give_log;
  213. double x, r, lambda;
  214. { double pr;
  215. if ((r<=0) | (lambda<0)) return(INVALID_PARAMS);
  216. if (x<=0.0) return( D_0 );
  217. #ifdef USE_SCALE
  218. lambda = 1.0/lambda;
  219. #endif
  220. if (r<1)
  221. { pr = dpois_raw(r,lambda*x,give_log);
  222. return( (give_log) ? pr + log(r/x) : pr*r/x );
  223. }
  224. pr = dpois_raw(r-1.0,lambda*x,give_log);
  225. return( (give_log) ? pr + log(lambda) : lambda*pr);
  226. }
  227. double dchisq(x, df, give_log)
  228. double x, df;
  229. int give_log;
  230. {
  231. return(dgamma(x, df/2.0,
  232. 0.5
  233. ,give_log));
  234. /*
  235. #ifdef USE_SCALE
  236. 2.0
  237. #else
  238. 0.5
  239. #endif
  240. ,give_log));
  241. */
  242. }
  243. /*
  244. * Given a sequence of r successes and b failures, we sample n (\le b+r)
  245. * items without replacement. The hypergeometric probability is the
  246. * probability of x successes:
  247. *
  248. * dbinom(x,r,p) * dbinom(n-x,b,p)
  249. * p(x;r,b,n) = ---------------------------------
  250. * dbinom(n,r+b,p)
  251. *
  252. * for any p. For numerical stability, we take p=n/(r+b); with this choice,
  253. * the denominator is not exponentially small.
  254. */
  255. double dhyper(x,r,b,n,give_log)
  256. int x, r, b, n, give_log;
  257. { double p, q, p1, p2, p3;
  258. if ((r<0) | (b<0) | (n<0) | (n>r+b))
  259. return( INVALID_PARAMS );
  260. if (x<0) return(D_0);
  261. if (n==0) return((x==0) ? D_1 : D_0);
  262. p = ((double)n)/((double)(r+b));
  263. q = ((double)(r+b-n))/((double)(r+b));
  264. p1 = dbinom_raw((double)x,(double)r,p,q,give_log);
  265. p2 = dbinom_raw((double)(n-x),(double)b,p,q,give_log);
  266. p3 = dbinom_raw((double)n,(double)(r+b),p,q,give_log);
  267. return( (give_log) ? p1 + p2 - p3 : p1*p2/p3 );
  268. }
  269. /*
  270. probability of x failures before the nth success.
  271. */
  272. double dnbinom(x,n,p,give_log)
  273. double n, p;
  274. int x, give_log;
  275. { double prob, f;
  276. if ((p<0) | (p>1) | (n<=0)) return(INVALID_PARAMS);
  277. if (x<0) return( D_0 );
  278. prob = dbinom_raw(n,x+n,p,1-p,give_log);
  279. f = n/(n+x);
  280. return((give_log) ? log(f) + prob : f*prob);
  281. }
  282. double dt(x, df, give_log)
  283. double x, df;
  284. int give_log;
  285. { double t, u, f;
  286. if (df<=0.0) return(INVALID_PARAMS);
  287. /*
  288. exp(t) = Gamma((df+1)/2) /{ sqrt(df/2) * Gamma(df/2) }
  289. = sqrt(df/2) / ((df+1)/2) * Gamma((df+3)/2) / Gamma((df+2)/2).
  290. This form leads to a computation that should be stable for all
  291. values of df, including df -> 0 and df -> infinity.
  292. */
  293. t = -bd0(df/2.0,(df+1)/2.0) + stirlerr((df+1)/2.0) - stirlerr(df/2.0);
  294. if (x*x>df)
  295. u = log( 1+ x*x/df ) * df/2;
  296. else
  297. u = -bd0(df/2.0,(df+x*x)/2.0) + x*x/2.0;
  298. f = PIx2*(1+x*x/df);
  299. return( FEXP(f,t-u) );
  300. }
  301. /*
  302. * Copyright 1996-2006 Catherine Loader.
  303. */
  304. /*
  305. * Provides mut_erf() and mut_erfc() functions. Also mut_pnorm().
  306. * Had too many problems with erf()'s built into math libraries
  307. * (when they existed). Hence the need to write my own...
  308. *
  309. * Algorithm from Walter Kr\"{a}mer, Frithjof Blomquist.
  310. * "Algorithms with Guaranteed Error Bounds for the Error Function
  311. * and Complementary Error Function"
  312. * Preprint 2000/2, Bergische Universt\"{a}t GH Wuppertal
  313. * http://www.math.uni-wuppertal.de/wrswt/preprints/prep_00_2.pdf
  314. *
  315. * Coded by Catherine Loader, September 2006.
  316. */
  317. #include "mut.h"
  318. #define drand48() (rand())
  319. double erf1(double x) /* erf; 0 < x < 0.65) */
  320. { double p[5] = {1.12837916709551256e0, /* 2/sqrt(pi) */
  321. 1.35894887627277916e-1,
  322. 4.03259488531795274e-2,
  323. 1.20339380863079457e-3,
  324. 6.49254556481904354e-5};
  325. double q[5] = {1.00000000000000000e0,
  326. 4.53767041780002545e-1,
  327. 8.69936222615385890e-2,
  328. 8.49717371168693357e-3,
  329. 3.64915280629351082e-4};
  330. double x2, p4, q4;
  331. x2 = x*x;
  332. p4 = p[0] + p[1]*x2 + p[2]*x2*x2 + p[3]*x2*x2*x2 + p[4]*x2*x2*x2*x2;
  333. q4 = q[0] + q[1]*x2 + q[2]*x2*x2 + q[3]*x2*x2*x2 + q[4]*x2*x2*x2*x2;
  334. return(x*p4/q4);
  335. }
  336. double erf2(double x) /* erfc; 0.65 <= x < 2.2 */
  337. { double p[6] = {9.99999992049799098e-1,
  338. 1.33154163936765307e0,
  339. 8.78115804155881782e-1,
  340. 3.31899559578213215e-1,
  341. 7.14193832506776067e-2,
  342. 7.06940843763253131e-3};
  343. double q[7] = {1.00000000000000000e0,
  344. 2.45992070144245533e0,
  345. 2.65383972869775752e0,
  346. 1.61876655543871376e0,
  347. 5.94651311286481502e-1,
  348. 1.26579413030177940e-1,
  349. 1.25304936549413393e-2};
  350. double x2, p5, q6;
  351. x2 = x*x;
  352. p5 = p[0] + p[1]*x + p[2]*x2 + p[3]*x2*x + p[4]*x2*x2 + p[5]*x2*x2*x;
  353. q6 = q[0] + q[1]*x + q[2]*x2 + q[3]*x2*x + q[4]*x2*x2 + q[5]*x2*x2*x + q[6]*x2*x2*x2;
  354. return( exp(-x2)*p5/q6 );
  355. }
  356. double erf3(double x) /* erfc; 2.2 < x <= 6 */
  357. { double p[6] = {9.99921140009714409e-1,
  358. 1.62356584489366647e0,
  359. 1.26739901455873222e0,
  360. 5.81528574177741135e-1,
  361. 1.57289620742838702e-1,
  362. 2.25716982919217555e-2};
  363. double q[7] = {1.00000000000000000e0,
  364. 2.75143870676376208e0,
  365. 3.37367334657284535e0,
  366. 2.38574194785344389e0,
  367. 1.05074004614827206e0,
  368. 2.78788439273628983e-1,
  369. 4.00072964526861362e-2};
  370. double x2, p5, q6;
  371. x2 = x*x;
  372. p5 = p[0] + p[1]*x + p[2]*x2 + p[3]*x2*x + p[4]*x2*x2 + p[5]*x2*x2*x;
  373. q6 = q[0] + q[1]*x + q[2]*x2 + q[3]*x2*x + q[4]*x2*x2 + q[5]*x2*x2*x + q[6]*x2*x2*x2;
  374. return( exp(-x2)*p5/q6 );
  375. }
  376. double erf4(double x) /* erfc; x > 6.0 */
  377. { double p[5] = {5.64189583547756078e-1,
  378. 8.80253746105525775e0,
  379. 3.84683103716117320e1,
  380. 4.77209965874436377e1,
  381. 8.08040729052301677e0};
  382. double q[5] = {1.00000000000000000e0,
  383. 1.61020914205869003e1,
  384. 7.54843505665954743e1,
  385. 1.12123870801026015e2,
  386. 3.73997570145040850e1};
  387. double x2, p4, q4;
  388. if (x>26.5432) return(0.0);
  389. x2 = 1.0/(x*x);
  390. p4 = p[0] + p[1]*x2 + p[2]*x2*x2 + p[3]*x2*x2*x2 + p[4]*x2*x2*x2*x2;
  391. q4 = q[0] + q[1]*x2 + q[2]*x2*x2 + q[3]*x2*x2*x2 + q[4]*x2*x2*x2*x2;
  392. return(exp(-x*x)*p4/(x*q4));
  393. }
  394. double mut_erfc(double x)
  395. { if (x<0.0) return(2.0-mut_erfc(-x));
  396. if (x==0.0) return(1.0);
  397. if (x<0.65) return(1.0-erf1(x));
  398. if (x<2.2) return(erf2(x));
  399. if (x<6.0) return(erf3(x));
  400. return(erf4(x));
  401. }
  402. double mut_erf(double x)
  403. {
  404. if (x<0.0) return(-mut_erf(-x));
  405. if (x==0.0) return(0.0);
  406. if (x<0.65) return(erf1(x));
  407. if (x<2.2) return(1.0-erf2(x));
  408. if (x<6.0) return(1.0-erf3(x));
  409. return(1.0-erf4(x));
  410. }
  411. double mut_pnorm(double x)
  412. { if (x<0.0) return(mut_erfc(-x/SQRT2)/2);
  413. return((1.0+mut_erf(x/SQRT2))/2);
  414. }
  415. /*
  416. * Copyright 1996-2006 Catherine Loader.
  417. */
  418. #include "mut.h"
  419. static double lookup_gamma[21] = {
  420. 0.0, /* place filler */
  421. 0.572364942924699971, /* log(G(0.5)) = log(sqrt(pi)) */
  422. 0.000000000000000000, /* log(G(1)) = log(0!) */
  423. -0.120782237635245301, /* log(G(3/2)) = log(sqrt(pi)/2)) */
  424. 0.000000000000000000, /* log(G(2)) = log(1!) */
  425. 0.284682870472919181, /* log(G(5/2)) = log(3sqrt(pi)/4) */
  426. 0.693147180559945286, /* log(G(3)) = log(2!) */
  427. 1.200973602347074287, /* etc */
  428. 1.791759469228054957,
  429. 2.453736570842442344,
  430. 3.178053830347945752,
  431. 3.957813967618716511,
  432. 4.787491742782045812,
  433. 5.662562059857141783,
  434. 6.579251212010101213,
  435. 7.534364236758732680,
  436. 8.525161361065414667,
  437. 9.549267257300996903,
  438. 10.604602902745250859,
  439. 11.689333420797268559,
  440. 12.801827480081469091 };
  441. /*
  442. * coefs are B(2n)/(2n(2n-1)) 2n(2n-1) =
  443. * 2n B(2n) 2n(2n-1) coef
  444. * 2 1/6 2 1/12
  445. * 4 -1/30 12 -1/360
  446. * 6 1/42 30 1/1260
  447. * 8 -1/30 56 -1/1680
  448. * 10 5/66 90 1/1188
  449. * 12 -691/2730 132 691/360360
  450. */
  451. double mut_lgamma(double x)
  452. { double f, z, x2, se;
  453. int ix;
  454. /* lookup table for common values.
  455. */
  456. ix = (int)(2*x);
  457. if (((ix>=1) & (ix<=20)) && (ix==2*x)) return(lookup_gamma[ix]);
  458. f = 1.0;
  459. while (x <= 15)
  460. { f *= x;
  461. x += 1.0;
  462. }
  463. x2 = 1.0/(x*x);
  464. z = (x-0.5)*log(x) - x + HF_LG_PIx2;
  465. se = (13860 - x2*(462 - x2*(132 - x2*(99 - 140*x2))))/(166320*x);
  466. return(z + se - log(f));
  467. }
  468. double mut_lgammai(int i) /* log(Gamma(i/2)) for integer i */
  469. { if (i>20) return(mut_lgamma(i/2.0));
  470. return(lookup_gamma[i]);
  471. }
  472. /*
  473. * Copyright 1996-2006 Catherine Loader.
  474. */
  475. /*
  476. * A is a n*p matrix, find the cholesky decomposition
  477. * of the first p rows. In most applications, will want n=p.
  478. *
  479. * chol_dec(A,n,p) computes the decomoposition R'R=A.
  480. * (note that R is stored in the input A).
  481. * chol_solve(A,v,n,p) computes (R'R)^{-1}v
  482. * chol_hsolve(A,v,n,p) computes (R')^{-1}v
  483. * chol_isolve(A,v,n,p) computes (R)^{-1}v
  484. * chol_qf(A,v,n,p) computes ||(R')^{-1}v||^2.
  485. * chol_mult(A,v,n,p) computes (R'R)v
  486. *
  487. * The solve functions assume that A is already decomposed.
  488. * chol_solve(A,v,n,p) is equivalent to applying chol_hsolve()
  489. * and chol_isolve() in sequence.
  490. */
  491. #include <math.h>
  492. #include "mut.h"
  493. void chol_dec(A,n,p)
  494. double *A;
  495. int n, p;
  496. { int i, j, k;
  497. for (j=0; j<p; j++)
  498. { k = n*j+j;
  499. for (i=0; i<j; i++) A[k] -= A[n*j+i]*A[n*j+i];
  500. if (A[k]<=0)
  501. { for (i=j; i<p; i++) A[n*i+j] = 0.0; }
  502. else
  503. { A[k] = sqrt(A[k]);
  504. for (i=j+1; i<p; i++)
  505. { for (k=0; k<j; k++)
  506. A[n*i+j] -= A[n*i+k]*A[n*j+k];
  507. A[n*i+j] /= A[n*j+j];
  508. }
  509. }
  510. }
  511. for (j=0; j<p; j++)
  512. for (i=j+1; i<p; i++) A[n*j+i] = 0.0;
  513. }
  514. int chol_solve(A,v,n,p)
  515. double *A, *v;
  516. int n, p;
  517. { int i, j;
  518. for (i=0; i<p; i++)
  519. { for (j=0; j<i; j++) v[i] -= A[i*n+j]*v[j];
  520. v[i] /= A[i*n+i];
  521. }
  522. for (i=p-1; i>=0; i--)
  523. { for (j=i+1; j<p; j++) v[i] -= A[j*n+i]*v[j];
  524. v[i] /= A[i*n+i];
  525. }
  526. return(p);
  527. }
  528. int chol_hsolve(A,v,n,p)
  529. double *A, *v;
  530. int n, p;
  531. { int i, j;
  532. for (i=0; i<p; i++)
  533. { for (j=0; j<i; j++) v[i] -= A[i*n+j]*v[j];
  534. v[i] /= A[i*n+i];
  535. }
  536. return(p);
  537. }
  538. int chol_isolve(A,v,n,p)
  539. double *A, *v;
  540. int n, p;
  541. { int i, j;
  542. for (i=p-1; i>=0; i--)
  543. { for (j=i+1; j<p; j++) v[i] -= A[j*n+i]*v[j];
  544. v[i] /= A[i*n+i];
  545. }
  546. return(p);
  547. }
  548. double chol_qf(A,v,n,p)
  549. double *A, *v;
  550. int n, p;
  551. { int i, j;
  552. double sum;
  553. sum = 0.0;
  554. for (i=0; i<p; i++)
  555. { for (j=0; j<i; j++) v[i] -= A[i*n+j]*v[j];
  556. v[i] /= A[i*n+i];
  557. sum += v[i]*v[i];
  558. }
  559. return(sum);
  560. }
  561. int chol_mult(A,v,n,p)
  562. double *A, *v;
  563. int n, p;
  564. { int i, j;
  565. double sum;
  566. for (i=0; i<p; i++)
  567. { sum = 0;
  568. for (j=i; j<p; j++) sum += A[j*n+i]*v[j];
  569. v[i] = sum;
  570. }
  571. for (i=p-1; i>=0; i--)
  572. { sum = 0;
  573. for (j=0; j<=i; j++) sum += A[i*n+j]*v[j];
  574. v[i] = sum;
  575. }
  576. return(1);
  577. }
  578. /*
  579. * Copyright 1996-2006 Catherine Loader.
  580. */
  581. #include <stdio.h>
  582. #include <math.h>
  583. #include "mut.h"
  584. #define E_MAXIT 20
  585. #define E_TOL 1.0e-10
  586. #define SQR(x) ((x)*(x))
  587. double e_tol(D,p)
  588. double *D;
  589. int p;
  590. { double mx;
  591. int i;
  592. if (E_TOL <= 0.0) return(0.0);
  593. mx = D[0];
  594. for (i=1; i<p; i++) if (D[i*(p+1)]>mx) mx = D[i*(p+1)];
  595. return(E_TOL*mx);
  596. }
  597. void eig_dec(X,P,d)
  598. double *X, *P;
  599. int d;
  600. { int i, j, k, iter, ms;
  601. double c, s, r, u, v;
  602. for (i=0; i<d; i++)
  603. for (j=0; j<d; j++) P[i*d+j] = (i==j);
  604. for (iter=0; iter<E_MAXIT; iter++)
  605. { ms = 0;
  606. for (i=0; i<d; i++)
  607. for (j=i+1; j<d; j++)
  608. if (SQR(X[i*d+j]) > 1.0e-15*fabs(X[i*d+i]*X[j*d+j]))
  609. { c = (X[j*d+j]-X[i*d+i])/2;
  610. s = -X[i*d+j];
  611. r = sqrt(c*c+s*s);
  612. c /= r;
  613. s = sqrt((1-c)/2)*(2*(s>0)-1);
  614. c = sqrt((1+c)/2);
  615. for (k=0; k<d; k++)
  616. { u = X[i*d+k]; v = X[j*d+k];
  617. X[i*d+k] = u*c+v*s;
  618. X[j*d+k] = v*c-u*s;
  619. }
  620. for (k=0; k<d; k++)
  621. { u = X[k*d+i]; v = X[k*d+j];
  622. X[k*d+i] = u*c+v*s;
  623. X[k*d+j] = v*c-u*s;
  624. }
  625. X[i*d+j] = X[j*d+i] = 0.0;
  626. for (k=0; k<d; k++)
  627. { u = P[k*d+i]; v = P[k*d+j];
  628. P[k*d+i] = u*c+v*s;
  629. P[k*d+j] = v*c-u*s;
  630. }
  631. ms = 1;
  632. }
  633. if (ms==0) return;
  634. }
  635. mut_printf("eig_dec not converged\n");
  636. }
  637. int eig_solve(J,x)
  638. jacobian *J;
  639. double *x;
  640. { int d, i, j, rank;
  641. double *D, *P, *Q, *w;
  642. double tol;
  643. D = J->Z;
  644. P = Q = J->Q;
  645. d = J->p;
  646. w = J->wk;
  647. tol = e_tol(D,d);
  648. rank = 0;
  649. for (i=0; i<d; i++)
  650. { w[i] = 0.0;
  651. for (j=0; j<d; j++) w[i] += P[j*d+i]*x[j];
  652. }
  653. for (i=0; i<d; i++)
  654. if (D[i*d+i]>tol)
  655. { w[i] /= D[i*(d+1)];
  656. rank++;
  657. }
  658. for (i=0; i<d; i++)
  659. { x[i] = 0.0;
  660. for (j=0; j<d; j++) x[i] += Q[i*d+j]*w[j];
  661. }
  662. return(rank);
  663. }
  664. int eig_hsolve(J,v)
  665. jacobian *J;
  666. double *v;
  667. { int i, j, p, rank;
  668. double *D, *Q, *w;
  669. double tol;
  670. D = J->Z;
  671. Q = J->Q;
  672. p = J->p;
  673. w = J->wk;
  674. tol = e_tol(D,p);
  675. rank = 0;
  676. for (i=0; i<p; i++)
  677. { w[i] = 0.0;
  678. for (j=0; j<p; j++) w[i] += Q[j*p+i]*v[j];
  679. }
  680. for (i=0; i<p; i++)
  681. { if (D[i*p+i]>tol)
  682. { v[i] = w[i]/sqrt(D[i*(p+1)]);
  683. rank++;
  684. }
  685. else v[i] = 0.0;
  686. }
  687. return(rank);
  688. }
  689. int eig_isolve(J,v)
  690. jacobian *J;
  691. double *v;
  692. { int i, j, p, rank;
  693. double *D, *Q, *w;
  694. double tol;
  695. D = J->Z;
  696. Q = J->Q;
  697. p = J->p;
  698. w = J->wk;
  699. tol = e_tol(D,p);
  700. rank = 0;
  701. for (i=0; i<p; i++)
  702. { if (D[i*p+i]>tol)
  703. { v[i] = w[i]/sqrt(D[i*(p+1)]);
  704. rank++;
  705. }
  706. else v[i] = 0.0;
  707. }
  708. for (i=0; i<p; i++)
  709. { w[i] = 0.0;
  710. for (j=0; j<p; j++) w[i] += Q[i*p+j]*v[j];
  711. }
  712. return(rank);
  713. }
  714. double eig_qf(J,v)
  715. jacobian *J;
  716. double *v;
  717. { int i, j, p;
  718. double sum, tol;
  719. p = J->p;
  720. sum = 0.0;
  721. tol = e_tol(J->Z,p);
  722. for (i=0; i<p; i++)
  723. if (J->Z[i*p+i]>tol)
  724. { J->wk[i] = 0.0;
  725. for (j=0; j<p; j++) J->wk[i] += J->Q[j*p+i]*v[j];
  726. sum += J->wk[i]*J->wk[i]/J->Z[i*p+i];
  727. }
  728. return(sum);
  729. }
  730. /*
  731. * Copyright 1996-2006 Catherine Loader.
  732. */
  733. /*
  734. * Integrate a function f over a circle or disc.
  735. */
  736. #include "mut.h"
  737. void setM(M,r,s,c,b)
  738. double *M, r, s, c;
  739. int b;
  740. { M[0] =-r*s; M[1] = r*c;
  741. M[2] = b*c; M[3] = b*s;
  742. M[4] =-r*c; M[5] = -s;
  743. M[6] = -s; M[7] = 0.0;
  744. M[8] =-r*s; M[9] = c;
  745. M[10]= c; M[11]= 0.0;
  746. }
  747. void integ_circ(f,r,orig,res,mint,b)
  748. int (*f)(), mint, b;
  749. double r, *orig, *res;
  750. { double y, x[2], theta, tres[MXRESULT], M[12], c, s;
  751. int i, j, nr;
  752. y = 0;
  753. for (i=0; i<mint; i++)
  754. { theta = 2*PI*(double)i/(double)mint;
  755. c = cos(theta); s = sin(theta);
  756. x[0] = orig[0]+r*c;
  757. x[1] = orig[1]+r*s;
  758. if (b!=0)
  759. { M[0] =-r*s; M[1] = r*c;
  760. M[2] = b*c; M[3] = b*s;
  761. M[4] =-r*c; M[5] = -s;
  762. M[6] = -s; M[7] = 0.0;
  763. M[8] =-r*s; M[9] = c;
  764. M[10]= c; M[11]= 0.0;
  765. }
  766. nr = f(x,2,tres,M);
  767. if (i==0) setzero(res,nr);
  768. for (j=0; j<nr; j++) res[j] += tres[j];
  769. }
  770. y = 2 * PI * ((b==0)?r:1.0) / mint;
  771. for (j=0; j<nr; j++) res[j] *= y;
  772. }
  773. void integ_disc(f,fb,fl,res,resb,mg)
  774. int (*f)(), (*fb)(), *mg;
  775. double *fl, *res, *resb;
  776. { double x[2], y, r, tres[MXRESULT], *orig, rmin, rmax, theta, c, s, M[12];
  777. int ct, ctb, i, j, k, nr, nrb, w;
  778. orig = &fl[2];
  779. rmax = fl[1];
  780. rmin = fl[0];
  781. y = 0.0;
  782. ct = ctb = 0;
  783. for (j=0; j<mg[1]; j++)
  784. { theta = 2*PI*(double)j/(double)mg[1];
  785. c = cos(theta); s = sin(theta);
  786. for (i= (rmin>0) ? 0 : 1; i<=mg[0]; i++)
  787. { r = rmin + (rmax-rmin)*i/mg[0];
  788. w = (2+2*(i&1)-(i==0)-(i==mg[0]));
  789. x[0] = orig[0] + r*c;
  790. x[1] = orig[1] + r*s;
  791. nr = f(x,2,tres,NULL);
  792. if (ct==0) setzero(res,nr);
  793. for (k=0; k<nr; k++) res[k] += w*r*tres[k];
  794. ct++;
  795. if (((i==0) | (i==mg[0])) && (fb!=NULL))
  796. { setM(M,r,s,c,1-2*(i==0));
  797. nrb = fb(x,2,tres,M);
  798. if (ctb==0) setzero(resb,nrb);
  799. ctb++;
  800. for (k=0; k<nrb; k++) resb[k] += tres[k];
  801. }
  802. }
  803. }
  804. /* for (i= (rmin>0) ? 0 : 1; i<=mg[0]; i++)
  805. {
  806. r = rmin + (rmax-rmin)*i/mg[0];
  807. w = (2+2*(i&1)-(i==0)-(i==mg[0]));
  808. for (j=0; j<mg[1]; j++)
  809. { theta = 2*PI*(double)j/(double)mg[1];
  810. c = cos(theta); s = sin(theta);
  811. x[0] = orig[0] + r*c;
  812. x[1] = orig[1] + r*s;
  813. nr = f(x,2,tres,NULL);
  814. if (ct==0) setzero(res,nr);
  815. ct++;
  816. for (k=0; k<nr; k++) res[k] += w*r*tres[k];
  817. if (((i==0) | (i==mg[0])) && (fb!=NULL))
  818. { setM(M,r,s,c,1-2*(i==0));
  819. nrb = fb(x,2,tres,M);
  820. if (ctb==0) setzero(resb,nrb);
  821. ctb++;
  822. for (k=0; k<nrb; k++) resb[k] += tres[k];
  823. }
  824. }
  825. } */
  826. for (j=0; j<nr; j++) res[j] *= 2*PI*(rmax-rmin)/(3*mg[0]*mg[1]);
  827. for (j=0; j<nrb; j++) resb[j] *= 2*PI/mg[1];
  828. }
  829. /*
  830. * Copyright 1996-2006 Catherine Loader.
  831. */
  832. /*
  833. * Multivariate integration of a vector-valued function
  834. * using Monte-Carlo method.
  835. *
  836. * uses drand48() random number generator. Does not seed.
  837. */
  838. #include <stdlib.h>
  839. #include "mut.h"
  840. extern void setzero();
  841. static double M[(1+MXIDIM)*MXIDIM*MXIDIM];
  842. void monte(f,ll,ur,d,res,n)
  843. int (*f)(), d, n;
  844. double *ll, *ur, *res;
  845. {
  846. int i, j, nr;
  847. #ifdef WINDOWS
  848. mut_printf("Sorry, Monte-Carlo Integration not enabled.\n");
  849. for (i=0; i<n; i++) res[i] = 0.0;
  850. #else
  851. double z, x[MXIDIM], tres[MXRESULT];
  852. srand(234L);
  853. for (i=0; i<n; i++)
  854. { for (j=0; j<d; j++) x[j] = ll[j] + (ur[j]-ll[j])*drand48();
  855. nr = f(x,d,tres,NULL);
  856. if (i==0) setzero(res,nr);
  857. for (j=0; j<nr; j++) res[j] += tres[j];
  858. }
  859. z = 1;
  860. for (i=0; i<d; i++) z *= (ur[i]-ll[i]);
  861. for (i=0; i<nr; i++) res[i] *= z/n;
  862. #endif
  863. }
  864. /*
  865. * Copyright 1996-2006 Catherine Loader.
  866. */
  867. /*
  868. * Multivariate integration of a vector-valued function
  869. * using Simpson's rule.
  870. */
  871. #include <math.h>
  872. #include "mut.h"
  873. extern void setzero();
  874. static double M[(1+MXIDIM)*MXIDIM*MXIDIM];
  875. /* third order corners */
  876. void simp3(fd,x,d,resd,delta,wt,i0,i1,mg,ct,res2,index)
  877. int (*fd)(), d, wt, i0, i1, *mg, ct, *index;
  878. double *x, *resd, *delta, *res2;
  879. { int k, l, m, nrd;
  880. double zb;
  881. for (k=i1+1; k<d; k++) if ((index[k]==0) | (index[k]==mg[k]))
  882. {
  883. setzero(M,d*d);
  884. m = 0; zb = 1.0;
  885. for (l=0; l<d; l++)
  886. if ((l!=i0) & (l!=i1) & (l!=k))
  887. { M[m*d+l] = 1.0;
  888. m++;
  889. zb *= delta[l];
  890. }
  891. M[(d-3)*d+i0] = (index[i0]==0) ? -1 : 1;
  892. M[(d-2)*d+i1] = (index[i1]==0) ? -1 : 1;
  893. M[(d-1)*d+k] = (index[k]==0) ? -1 : 1;
  894. nrd = fd(x,d,res2,M);
  895. if ((ct==0) & (i0==0) & (i1==1) & (k==2)) setzero(resd,nrd);
  896. for (l=0; l<nrd; l++)
  897. resd[l] += wt*zb*res2[l];
  898. }
  899. }
  900. /* second order corners */
  901. void simp2(fc,fd,x,d,resc,resd,delta,wt,i0,mg,ct,res2,index)
  902. int (*fc)(), (*fd)(), d, wt, i0, *mg, ct, *index;
  903. double *x, *resc, *resd, *delta, *res2;
  904. { int j, k, l, nrc;
  905. double zb;
  906. for (j=i0+1; j<d; j++) if ((index[j]==0) | (index[j]==mg[j]))
  907. { setzero(M,d*d);
  908. l = 0; zb = 1;
  909. for (k=0; k<d; k++) if ((k!=i0) & (k!=j))
  910. { M[l*d+k] = 1.0;
  911. l++;
  912. zb *= delta[k];
  913. }
  914. M[(d-2)*d+i0] = (index[i0]==0) ? -1 : 1;
  915. M[(d-1)*d+j] = (index[j]==0) ? -1 : 1;
  916. nrc = fc(x,d,res2,M);
  917. if ((ct==0) & (i0==0) & (j==1)) setzero(resc,nrc);
  918. for (k=0; k<nrc; k++) resc[k] += wt*zb*res2[k];
  919. if (fd!=NULL)
  920. simp3(fd,x,d,resd,delta,wt,i0,j,mg,ct,res2,index);
  921. }
  922. }
  923. /* first order boundary */
  924. void simp1(fb,fc,fd,x,d,resb,resc,resd,delta,wt,mg,ct,res2,index)
  925. int (*fb)(), (*fc)(), (*fd)(), d, wt, *mg, ct, *index;
  926. double *x, *resb, *resc, *resd, *delta, *res2;
  927. { int i, j, k, nrb;
  928. double zb;
  929. for (i=0; i<d; i++) if ((index[i]==0) | (index[i]==mg[i]))
  930. { setzero(M,(1+d)*d*d);
  931. k = 0;
  932. for (j=0; j<d; j++) if (j!=i)
  933. { M[k*d+j] = 1;
  934. k++;
  935. }
  936. M[(d-1)*d+i] = (index[i]==0) ? -1 : 1;
  937. nrb = fb(x,d,res2,M);
  938. zb = 1;
  939. for (j=0; j<d; j++) if (i!=j) zb *= delta[j];
  940. if ((ct==0) && (i==0))
  941. for (j=0; j<nrb; j++) resb[j] = 0.0;
  942. for (j=0; j<nrb; j++) resb[j] += wt*zb*res2[j];
  943. if (fc!=NULL)
  944. simp2(fc,fd,x,d,resc,resd,delta,wt,i,mg,ct,res2,index);
  945. }
  946. }
  947. void simpson4(f,fb,fc,fd,ll,ur,d,res,resb,resc,resd,mg,res2)
  948. int (*f)(), (*fb)(), (*fc)(), (*fd)(), d, *mg;
  949. double *ll, *ur, *res, *resb, *resc, *resd, *res2;
  950. { int ct, i, j, nr, wt, index[MXIDIM];
  951. double x[MXIDIM], delta[MXIDIM], z;
  952. for (i=0; i<d; i++)
  953. { index[i] = 0;
  954. x[i] = ll[i];
  955. if (mg[i]&1) mg[i]++;
  956. delta[i] = (ur[i]-ll[i])/(3*mg[i]);
  957. }
  958. ct = 0;
  959. while(1)
  960. { wt = 1;
  961. for (i=0; i<d; i++)
  962. wt *= (4-2*(index[i]%2==0)-(index[i]==0)-(index[i]==mg[i]));
  963. nr = f(x,d,res2,NULL);
  964. if (ct==0) setzero(res,nr);
  965. for (i=0; i<nr; i++) res[i] += wt*res2[i];
  966. if (fb!=NULL)
  967. simp1(fb,fc,fd,x,d,resb,resc,resd,delta,wt,mg,ct,res2,index);
  968. /* compute next grid point */
  969. for (i=0; i<d; i++)
  970. { index[i]++;
  971. if (index[i]>mg[i])
  972. { index[i] = 0;
  973. x[i] = ll[i];
  974. if (i==d-1) /* done */
  975. { z = 1.0;
  976. for (j=0; j<d; j++) z *= delta[j];
  977. for (j=0; j<nr; j++) res[j] *= z;
  978. return;
  979. }
  980. }
  981. else
  982. { x[i] = ll[i] + 3*delta[i]*index[i];
  983. i = d;
  984. }
  985. }
  986. ct++;
  987. }
  988. }
  989. void simpsonm(f,ll,ur,d,res,mg,res2)
  990. int (*f)(), d, *mg;
  991. double *ll, *ur, *res, *res2;
  992. { simpson4(f,NULL,NULL,NULL,ll,ur,d,res,NULL,NULL,NULL,mg,res2);
  993. }
  994. double simpson(f,l0,l1,m)
  995. double (*f)(), l0, l1;
  996. int m;
  997. { double x, sum;
  998. int i;
  999. sum = 0;
  1000. for (i=0; i<=m; i++)
  1001. { x = ((m-i)*l0 + i*l1)/m;
  1002. sum += (2+2*(i&1)-(i==0)-(i==m)) * f(x);
  1003. }
  1004. return( (l1-l0) * sum / (3*m) );
  1005. }
  1006. /*
  1007. * Copyright 1996-2006 Catherine Loader.
  1008. */
  1009. #include "mut.h"
  1010. static double *res, *resb, *orig, rmin, rmax;
  1011. static int ct0;
  1012. void sphM(M,r,u)
  1013. double *M, r, *u;
  1014. { double h, u1[3], u2[3];
  1015. /* set the orthogonal unit vectors. */
  1016. h = sqrt(u[0]*u[0]+u[1]*u[1]);
  1017. if (h<=0)
  1018. { u1[0] = u2[1] = 1.0;
  1019. u1[1] = u1[2] = u2[0] = u2[2] = 0.0;
  1020. }
  1021. else
  1022. { u1[0] = u[1]/h; u1[1] = -u[0]/h; u1[2] = 0.0;
  1023. u2[0] = u[2]*u[0]/h; u2[1] = u[2]*u[1]/h; u2[2] = -h;
  1024. }
  1025. /* parameterize the sphere as r(cos(t)cos(v)u + sin(t)u1 + cos(t)sin(v)u2).
  1026. * first layer of M is (dx/dt, dx/dv, dx/dr) at t=v=0.
  1027. */
  1028. M[0] = r*u1[0]; M[1] = r*u1[1]; M[2] = r*u1[2];
  1029. M[3] = r*u2[0]; M[4] = r*u2[1]; M[5] = r*u2[2];
  1030. M[6] = u[0]; M[7] = u[1]; M[8] = u[2];
  1031. /* next layers are second derivative matrix of components of x(r,t,v).
  1032. * d^2x/dt^2 = d^2x/dv^2 = -ru; d^2x/dtdv = 0;
  1033. * d^2x/drdt = u1; d^2x/drdv = u2; d^2x/dr^2 = 0.
  1034. */
  1035. M[9] = M[13] = -r*u[0];
  1036. M[11]= M[15] = u1[0];
  1037. M[14]= M[16] = u2[0];
  1038. M[10]= M[12] = M[17] = 0.0;
  1039. M[18]= M[22] = -r*u[1];
  1040. M[20]= M[24] = u1[1];
  1041. M[23]= M[25] = u2[1];
  1042. M[19]= M[21] = M[26] = 0.0;
  1043. M[27]= M[31] = -r*u[1];
  1044. M[29]= M[33] = u1[1];
  1045. M[32]= M[34] = u2[1];
  1046. M[28]= M[30] = M[35] = 0.0;
  1047. }
  1048. double ip3(a,b)
  1049. double *a, *b;
  1050. { return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
  1051. }
  1052. void rn3(a)
  1053. double *a;
  1054. { double s;
  1055. s = sqrt(ip3(a,a));
  1056. a[0] /= s; a[1] /= s; a[2] /= s;
  1057. }
  1058. double sptarea(a,b,c)
  1059. double *a, *b, *c;
  1060. { double ea, eb, ec, yab, yac, ybc, sab, sac, sbc;
  1061. double ab[3], ac[3], bc[3], x1[3], x2[3];
  1062. ab[0] = a[0]-b[0]; ab[1] = a[1]-b[1]; ab[2] = a[2]-b[2];
  1063. ac[0] = a[0]-c[0]; ac[1] = a[1]-c[1]; ac[2] = a[2]-c[2];
  1064. bc[0] = b[0]-c[0]; bc[1] = b[1]-c[1]; bc[2] = b[2]-c[2];
  1065. yab = ip3(ab,a); yac = ip3(ac,a); ybc = ip3(bc,b);
  1066. x1[0] = ab[0] - yab*a[0]; x2[0] = ac[0] - yac*a[0];
  1067. x1[1] = ab[1] - yab*a[1]; x2[1] = ac[1] - yac*a[1];
  1068. x1[2] = ab[2] - yab*a[2]; x2[2] = ac[2] - yac*a[2];
  1069. sab = ip3(x1,x1); sac = ip3(x2,x2);
  1070. ea = acos(ip3(x1,x2)/sqrt(sab*sac));
  1071. x1[0] = ab[0] + yab*b[0]; x2[0] = bc[0] - ybc*b[0];
  1072. x1[1] = ab[1] + yab*b[1]; x2[1] = bc[1] - ybc*b[1];
  1073. x1[2] = ab[2] + yab*b[2]; x2[2] = bc[2] - ybc*b[2];
  1074. sbc = ip3(x2,x2);
  1075. eb = acos(ip3(x1,x2)/sqrt(sab*sbc));
  1076. x1[0] = ac[0] + yac*c[0]; x2[0] = bc[0] + ybc*c[0];
  1077. x1[1] = ac[1] + yac*c[1]; x2[1] = bc[1] + ybc*c[1];
  1078. x1[2] = ac[2] + yac*c[2]; x2[2] = bc[2] + ybc*c[2];
  1079. ec = acos(ip3(x1,x2)/sqrt(sac*sbc));
  1080. /*
  1081. * Euler's formula is a+b+c-PI, except I've cheated...
  1082. * a=ea, c=ec, b=PI-eb, which is more stable.
  1083. */
  1084. return(ea+ec-eb);
  1085. }
  1086. void li(x,f,fb,mint,ar)
  1087. double *x, ar;
  1088. int (*f)(), (*fb)(), mint;
  1089. { int i, j, nr, nrb, ct1, w;
  1090. double u[3], r, M[36];
  1091. double sres[MXRESULT], tres[MXRESULT];
  1092. /* divide mint by 2, and force to even (Simpson's rule...)
  1093. * to make comparable with rectangular interpretation of mint
  1094. */
  1095. mint <<= 1;
  1096. if (mint&1) mint++;
  1097. ct1 = 0;
  1098. for (i= (rmin==0) ? 1 : 0; i<=mint; i++)
  1099. {
  1100. r = rmin + (rmax-rmin)*i/mint;
  1101. w = 2+2*(i&1)-(i==0)-(i==mint);
  1102. u[0] = orig[0]+x[0]*r;
  1103. u[1] = orig[1]+x[1]*r;
  1104. u[2] = orig[2]+x[2]*r;
  1105. nr = f(u,3,tres,NULL);
  1106. if (ct1==0) setzero(sres,nr);
  1107. for (j=0; j<nr; j++)
  1108. sres[j] += w*r*r*tres[j];
  1109. ct1++;
  1110. if ((fb!=NULL) && (i==mint)) /* boundary */
  1111. { sphM(M,rmax,x);
  1112. nrb = fb(u,3,tres,M);
  1113. if (ct0==0) for (j=0; j<nrb; j++) resb[j] = 0.0;
  1114. for (j=0; j<nrb; j++)
  1115. resb[j] += tres[j]*ar;
  1116. }
  1117. }
  1118. if (ct0==0) for (j=0; j<nr; j++) res[j] = 0.0;
  1119. ct0++;
  1120. for (j=0; j<nr; j++)
  1121. res[j] += sres[j] * ar * (rmax-rmin)/(3*mint);
  1122. }
  1123. void sphint(f,fb,a,b,c,lev,mint,cent)
  1124. double *a, *b, *c;
  1125. int (*f)(), (*fb)(), lev, mint, cent;
  1126. { double x[3], ab[3], ac[3], bc[3], ar;
  1127. int i;
  1128. if (lev>1)
  1129. { ab[0] = a[0]+b[0]; ab[1] = a[1]+b[1]; ab[2] = a[2]+b[2]; rn3(ab);
  1130. ac[0] = a[0]+c[0]; ac[1] = a[1]+c[1]; ac[2] = a[2]+c[2]; rn3(ac);
  1131. bc[0] = b[0]+c[0]; bc[1] = b[1]+c[1]; bc[2] = b[2]+c[2]; rn3(bc);
  1132. lev >>= 1;
  1133. if (cent==0)
  1134. { sphint(f,fb,a,ab,ac,lev,mint,1);
  1135. sphint(f,fb,ab,bc,ac,lev,mint,0);
  1136. }
  1137. else
  1138. { sphint(f,fb,a,ab,ac,lev,mint,1);
  1139. sphint(f,fb,b,ab,bc,lev,mint,1);
  1140. sphint(f,fb,c,ac,bc,lev,mint,1);
  1141. sphint(f,fb,ab,bc,ac,lev,mint,1);
  1142. }
  1143. return;
  1144. }
  1145. x[0] = a[0]+b[0]+c[0];
  1146. x[1] = a[1]+b[1]+c[1];
  1147. x[2] = a[2]+b[2]+c[2];
  1148. rn3(x);
  1149. ar = sptarea(a,b,c);
  1150. for (i=0; i<8; i++)
  1151. { if (i>0)
  1152. { x[0] = -x[0];
  1153. if (i%2 == 0) x[1] = -x[1];
  1154. if (i==4) x[2] = -x[2];
  1155. }
  1156. switch(cent)
  1157. { case 2: /* the reflection and its 120', 240' rotations */
  1158. ab[0] = x[0]; ab[1] = x[2]; ab[2] = x[1]; li(ab,f,fb,mint,ar);
  1159. ab[0] = x[2]; ab[1] = x[1]; ab[2] = x[0]; li(ab,f,fb,mint,ar);
  1160. ab[0] = x[1]; ab[1] = x[0]; ab[2] = x[2]; li(ab,f,fb,mint,ar);
  1161. case 1: /* and the 120' and 240' rotations */
  1162. ab[0] = x[1]; ab[1] = x[2]; ab[2] = x[0]; li(ab,f,fb,mint,ar);
  1163. ac[0] = x[2]; ac[1] = x[0]; ac[2] = x[1]; li(ac,f,fb,mint,ar);
  1164. case 0: /* and the triangle itself. */
  1165. li( x,f,fb,mint,ar);
  1166. }
  1167. }
  1168. }
  1169. void integ_sphere(f,fb,fl,Res,Resb,mg)
  1170. double *fl, *Res, *Resb;
  1171. int (*f)(), (*fb)(), *mg;
  1172. { double a[3], b[3], c[3];
  1173. a[0] = 1; a[1] = a[2] = 0;
  1174. b[1] = 1; b[0] = b[2] = 0;
  1175. c[2] = 1; c[0] = c[1] = 0;
  1176. res = Res;
  1177. resb=Resb;
  1178. orig = &fl[2];
  1179. rmin = fl[0];
  1180. rmax = fl[1];
  1181. ct0 = 0;
  1182. sphint(f,fb,a,b,c,mg[1],mg[0],0);
  1183. }
  1184. /*
  1185. * Copyright 1996-2006 Catherine Loader.
  1186. */
  1187. /*
  1188. * solving symmetric equations using the jacobian structure. Currently, three
  1189. * methods can be used: cholesky decomposition, eigenvalues, eigenvalues on
  1190. * the correlation matrix.
  1191. *
  1192. * jacob_dec(J,meth) decompose the matrix, meth=JAC_CHOL, JAC_EIG, JAC_EIGD
  1193. * jacob_solve(J,v) J^{-1}v
  1194. * jacob_hsolve(J,v) (R')^{-1/2}v
  1195. * jacob_isolve(J,v) (R)^{-1/2}v
  1196. * jacob_qf(J,v) v' J^{-1} v
  1197. * jacob_mult(J,v) (R'R) v (pres. CHOL only)
  1198. * where for each decomposition, R'R=J, although the different decomp's will
  1199. * produce different R's.
  1200. *
  1201. * To set up the J matrix:
  1202. * first, allocate storage: jac_alloc(J,p,wk)
  1203. * where p=dimension of matrix, wk is a numeric vector of length
  1204. * jac_reqd(p) (or NULL, to allocate automatically).
  1205. * now, copy the numeric values to J->Z (numeric vector with length p*p).
  1206. * (or, just set J->Z to point to the data vector. But remember this
  1207. * will be overwritten by the decomposition).
  1208. * finally, set:
  1209. * J->st=JAC_RAW;
  1210. * J->p = p;
  1211. *
  1212. * now, call jac_dec(J,meth) (optional) and the solve functions as required.
  1213. *
  1214. */
  1215. #include "math.h"
  1216. #include "mut.h"
  1217. #define DEF_METH JAC_EIGD
  1218. int jac_reqd(int p) { return(2*p*(p+1)); }
  1219. double *jac_alloc(J,p,wk)
  1220. jacobian *J;
  1221. int p;
  1222. double *wk;
  1223. { if (wk==NULL)
  1224. wk = (double *)calloc(2*p*(p+1),sizeof(double));
  1225. if ( wk == NULL ) {
  1226. printf("Problem allocating memory for wk\n");fflush(stdout);
  1227. }
  1228. J->Z = wk; wk += p*p;
  1229. J->Q = wk; wk += p*p;
  1230. J->wk= wk; wk += p;
  1231. J->dg= wk; wk += p;
  1232. return(wk);
  1233. }
  1234. void jacob_dec(J, meth)
  1235. jacobian *J;
  1236. int meth;
  1237. { int i, j, p;
  1238. if (J->st != JAC_RAW) return;
  1239. J->sm = J->st = meth;
  1240. switch(meth)
  1241. { case JAC_EIG:
  1242. eig_dec(J->Z,J->Q,J->p);
  1243. return;
  1244. case JAC_EIGD:
  1245. p = J->p;
  1246. for (i=0; i<p; i++)
  1247. J->dg[i] = (J->Z[i*(p+1)]<=0) ? 0.0 : 1/sqrt(J->Z[i*(p+1)]);
  1248. for (i=0; i<p; i++)
  1249. for (j=0; j<p; j++)
  1250. J->Z[i*p+j] *= J->dg[i]*J->dg[j];
  1251. eig_dec(J->Z,J->Q,J->p);
  1252. J->st = JAC_EIGD;
  1253. return;
  1254. case JAC_CHOL:
  1255. chol_dec(J->Z,J->p,J->p);
  1256. return;
  1257. default: mut_printf("jacob_dec: unknown method %d",meth);
  1258. }
  1259. }
  1260. int jacob_solve(J,v) /* (X^T W X)^{-1} v */
  1261. jacobian *J;
  1262. double *v;
  1263. { int i, rank;
  1264. if (J->st == JAC_RAW) jacob_dec(J,DEF_METH);
  1265. switch(J->st)
  1266. { case JAC_EIG:
  1267. return(eig_solve(J,v));
  1268. case JAC_EIGD:
  1269. for (i=0; i<J->p; i++) v[i] *= J->dg[i];
  1270. rank = eig_solve(J,v);
  1271. for (i=0; i<J->p; i++) v[i] *= J->dg[i];
  1272. return(rank);
  1273. case JAC_CHOL:
  1274. return(chol_solve(J->Z,v,J->p,J->p));
  1275. }
  1276. mut_printf("jacob_solve: unknown method %d",J->st);
  1277. return(0);
  1278. }
  1279. int jacob_hsolve(J,v) /* J^{-1/2} v */
  1280. jacobian *J;
  1281. double *v;
  1282. { int i;
  1283. if (J->st == JAC_RAW) jacob_dec(J,DEF_METH);
  1284. switch(J->st)
  1285. { case JAC_EIG:
  1286. return(eig_hsolve(J,v));
  1287. case JAC_EIGD: /* eigenvalues on corr matrix */
  1288. for (i=0; i<J->p; i++) v[i] *= J->dg[i];
  1289. return(eig_hsolve(J,v));
  1290. case JAC_CHOL:
  1291. return(chol_hsolve(J->Z,v,J->p,J->p));
  1292. }
  1293. mut_printf("jacob_hsolve: unknown method %d\n",J->st);
  1294. return(0);
  1295. }
  1296. int jacob_isolve(J,v) /* J^{-1/2} v */
  1297. jacobian *J;
  1298. double *v;
  1299. { int i, r;
  1300. if (J->st == JAC_RAW) jacob_dec(J,DEF_METH);
  1301. switch(J->st)
  1302. { case JAC_EIG:
  1303. return(eig_isolve(J,v));
  1304. case JAC_EIGD: /* eigenvalues on corr matrix */
  1305. r = eig_isolve(J,v);
  1306. for (i=0; i<J->p; i++) v[i] *= J->dg[i];
  1307. return(r);
  1308. case JAC_CHOL:
  1309. return(chol_isolve(J->Z,v,J->p,J->p));
  1310. }
  1311. mut_printf("jacob_hsolve: unknown method %d\n",J->st);
  1312. return(0);
  1313. }
  1314. double jacob_qf(J,v) /* vT J^{-1} v */
  1315. jacobian *J;
  1316. double *v;
  1317. { int i;
  1318. if (J->st == JAC_RAW) jacob_dec(J,DEF_METH);
  1319. switch (J->st)
  1320. { case JAC_EIG:
  1321. return(eig_qf(J,v));
  1322. case JAC_EIGD:
  1323. for (i=0; i<J->p; i++) v[i] *= J->dg[i];
  1324. return(eig_qf(J,v));
  1325. case JAC_CHOL:
  1326. return(chol_qf(J->Z,v,J->p,J->p));
  1327. default:
  1328. mut_printf("jacob_qf: invalid method\n");
  1329. return(0.0);
  1330. }
  1331. }
  1332. int jacob_mult(J,v) /* J v */
  1333. jacobian *J;
  1334. double *v;
  1335. {
  1336. if (J->st == JAC_RAW) jacob_dec(J,DEF_METH);
  1337. switch (J->st)
  1338. { case JAC_CHOL:
  1339. return(chol_mult(J->Z,v,J->p,J->p));
  1340. default:
  1341. mut_printf("jacob_mult: invalid method\n");
  1342. return(0);
  1343. }
  1344. }
  1345. /*
  1346. * Copyright 1996-2006 Catherine Loader.
  1347. */
  1348. /*
  1349. * Routines for maximization of a one dimensional function f()
  1350. * over an interval [xlo,xhi]. In all cases. the flag argument
  1351. * controls the return:
  1352. * flag='x', the maximizer xmax is returned.
  1353. * otherwise, maximum f(xmax) is returned.
  1354. *
  1355. * max_grid(f,xlo,xhi,n,flag)
  1356. * grid maximization of f() over [xlo,xhi] with n intervals.
  1357. *
  1358. * max_golden(f,xlo,xhi,n,tol,err,flag)
  1359. * golden section maximization.
  1360. * If n>2, an initial grid search is performed with n intervals
  1361. * (this helps deal with local maxima).
  1362. * convergence criterion is |x-xmax| < tol.
  1363. * err is an error flag.
  1364. * if flag='x', return value is xmax.
  1365. * otherwise, return value is f(xmax).
  1366. *
  1367. * max_quad(f,xlo,xhi,n,tol,err,flag)
  1368. * quadratic maximization.
  1369. *
  1370. * max_nr()
  1371. * newton-raphson, handles multivariate case.
  1372. *
  1373. * TODO: additional error checking, non-convergence stop.
  1374. */
  1375. #include <math.h>
  1376. #include "mut.h"
  1377. #define max_val(a,b) ((flag=='x') ? a : b)
  1378. double max_grid(f,xlo,xhi,n,flag)
  1379. double (*f)(), xlo, xhi;
  1380. int n;
  1381. char flag;
  1382. { int i, mi;
  1383. double x, y, mx, my;
  1384. for (i=0; i<=n; i++)
  1385. { x = xlo + (xhi-xlo)*i/n;
  1386. y = f(x);
  1387. if ((i==0) || (y>my))
  1388. { mx = x;
  1389. my = y;
  1390. mi = i;
  1391. }
  1392. }
  1393. if (mi==0) return(max_val(xlo,my));
  1394. if (mi==n) return(max_val(xhi,my));
  1395. return(max_val(mx,my));
  1396. }
  1397. double max_golden(f,xlo,xhi,n,tol,err,flag)
  1398. double (*f)(), xhi, xlo, tol;
  1399. int n, *err;
  1400. char flag;
  1401. { double dlt, x0, x1, x2, x3, y0, y1, y2, y3;
  1402. *err = 0;
  1403. if (n>2)
  1404. { dlt = (xhi-xlo)/n;
  1405. x0 = max_grid(f,xlo,xhi,n,'x');
  1406. if (xlo<x0) xlo = x0-dlt;
  1407. if (xhi>x0) xhi = x0+dlt;
  1408. }
  1409. x0 = xlo; y0 = f(xlo);
  1410. x3 = xhi; y3 = f(xhi);
  1411. x1 = gold_rat*x0 + (1-gold_rat)*x3; y1 = f(x1);
  1412. x2 = gold_rat*x3 + (1-gold_rat)*x0; y2 = f(x2);
  1413. while (fabs(x3-x0)>tol)
  1414. { if ((y1>=y0) && (y1>=y2))
  1415. { x3 = x2; y3 = y2;
  1416. x2 = x1; y2 = y1;
  1417. x1 = gold_rat*x0 + (1-gold_rat)*x3; y1 = f(x1);
  1418. }
  1419. else if ((y2>=y3) && (y2>=y1))
  1420. { x0 = x1; y0 = y1;
  1421. x1 = x2; y1 = y2;
  1422. x2 = gold_rat*x3 + (1-gold_rat)*x0; y2 = f(x2);
  1423. }
  1424. else
  1425. { if (y3>y0) { x0 = x2; y0 = y2; }
  1426. else { x3 = x1; y3 = y1; }
  1427. x1 = gold_rat*x0 + (1-gold_rat)*x3; y1 = f(x1);
  1428. x2 = gold_rat*x3 + (1-gold_rat)*x0; y2 = f(x2);
  1429. }
  1430. }
  1431. if (y0>=y1) return(max_val(x0,y0));
  1432. if (y3>=y2) return(max_val(x3,y3));
  1433. return((y1>y2) ? max_val(x1,y1) : max_val(x2,y2));
  1434. }
  1435. double max_quad(f,xlo,xhi,n,tol,err,flag)
  1436. double (*f)(), xhi, xlo, tol;
  1437. int n, *err;
  1438. char flag;
  1439. { double x0, x1, x2, xnew, y0, y1, y2, ynew, a, b;
  1440. *err = 0;
  1441. if (n>2)
  1442. { x0 = max_grid(f,xlo,xhi,n,'x');
  1443. if (xlo<x0) xlo = x0-1.0/n;
  1444. if (xhi>x0) xhi = x0+1.0/n;
  1445. }
  1446. x0 = xlo; y0 = f(x0);
  1447. x2 = xhi; y2 = f(x2);
  1448. x1 = (x0+x2)/2; y1 = f(x1);
  1449. while (x2-x0>tol)
  1450. {
  1451. /* first, check (y0,y1,y2) is a peak. If not,
  1452. * next interval is the halve with larger of (y0,y2).
  1453. */
  1454. if ((y0>y1) | (y2>y1))
  1455. {
  1456. if (y0>y2) { x2 = x1; y2 = y1; }
  1457. else { x0 = x1; y0 = y1; }
  1458. x1 = (x0+x2)/2;
  1459. y1 = f(x1);
  1460. }
  1461. else /* peak */
  1462. { a = (y1-y0)*(x2-x1) + (y1-y2)*(x1-x0);
  1463. b = ((y1-y0)*(x2-x1)*(x2+x1) + (y1-y2)*(x1-x0)*(x1+x0))/2;
  1464. /* quadratic maximizer is b/a. But first check if a's too
  1465. * small, since we may be close to constant.
  1466. */
  1467. if ((a<=0) | (b<x0*a) | (b>x2*a))
  1468. { /* split the larger halve */
  1469. xnew = ((x2-x1) > (x1-x0)) ? (x1+x2)/2 : (x0+x1)/2;
  1470. }
  1471. else
  1472. { xnew = b/a;
  1473. if (10*xnew < (9*x0+x1)) xnew = (9*x0+x1)/10;
  1474. if (10*xnew > (9*x2+x1)) xnew = (9*x2+x1)/10;
  1475. if (fabs(xnew-x1) < 0.001*(x2-x0))
  1476. {
  1477. if ((x2-x1) > (x1-x0))
  1478. xnew = (99*x1+x2)/100;
  1479. else
  1480. xnew = (99*x1+x0)/100;
  1481. }
  1482. }
  1483. ynew = f(xnew);
  1484. if (xnew>x1)
  1485. { if (ynew >= y1) { x0 = x1; y0 = y1; x1 = xnew; y1 = ynew; }
  1486. else { x2 = xnew; y2 = ynew; }
  1487. }
  1488. else
  1489. { if (ynew >= y1) { x2 = x1; y2 = y1; x1 = xnew; y1 = ynew; }
  1490. else { x0 = xnew; y0 = ynew; }
  1491. }
  1492. }
  1493. }
  1494. return(max_val(x1,y1));
  1495. }
  1496. double max_nr(F, coef, old_coef, f1, delta, J, p, maxit, tol, err)
  1497. double *coef, *old_coef, *f1, *delta, tol;
  1498. int (*F)(), p, maxit, *err;
  1499. jacobian *J;
  1500. { double old_f, f, lambda;
  1501. int i, j, fr;
  1502. double nc, nd, cut;
  1503. int rank;
  1504. *err = NR_OK;
  1505. J->p = p;
  1506. fr = F(coef, &f, f1, J->Z); J->st = JAC_RAW;
  1507. for (i=0; i<maxit; i++)
  1508. { memcpy(old_coef,coef,p*sizeof(double));
  1509. old_f = f;
  1510. rank = jacob_solve(J,f1);
  1511. memcpy(delta,f1,p*sizeof(double));
  1512. if (rank==0) /* NR won't move! */
  1513. delta[0] = -f/f1[0];
  1514. lambda = 1.0;
  1515. nc = innerprod(old_coef,old_coef,p);
  1516. nd = innerprod(delta, delta, p);
  1517. cut = sqrt(nc/nd);
  1518. if (cut>1.0) cut = 1.0;
  1519. cut *= 0.0001;
  1520. do
  1521. { for (j=0; j<p; j++) coef[j] = old_coef[j] + lambda*delta[j];
  1522. f = old_f - 1.0;
  1523. fr = F(coef, &f, f1, J->Z); J->st = JAC_RAW;
  1524. if (fr==NR_BREAK) return(old_f);
  1525. lambda = (fr==NR_REDUCE) ? lambda/2 : lambda/10.0;
  1526. } while ((lambda>cut) & (f <= old_f - 1.0e-3));
  1527. if (f < old_f - 1.0e-3)
  1528. { *err = NR_NDIV;
  1529. return(f);
  1530. }
  1531. if (fr==NR_REDUCE) return(f);
  1532. if (fabs(f-old_f) < tol) return(f);
  1533. }
  1534. *err = NR_NCON;
  1535. return(f);
  1536. }
  1537. /*
  1538. * Copyright 1996-2006 Catherine Loader.
  1539. */
  1540. #include <math.h>
  1541. #include "mut.h"
  1542. /* qr decomposition of X (n*p organized by column).
  1543. * Take w for the ride, if not NULL.
  1544. */
  1545. void qr(X,n,p,w)
  1546. double *X, *w;
  1547. int n, p;
  1548. { int i, j, k, mi;
  1549. double c, s, mx, nx, t;
  1550. for (j=0; j<p; j++)
  1551. { mi = j;
  1552. mx = fabs(X[(n+1)*j]);
  1553. nx = mx*mx;
  1554. /* find the largest remaining element in j'th column, row mi.
  1555. * flip that row with row j.
  1556. */
  1557. for (i=j+1; i<n; i++)
  1558. { nx += X[j*n+i]*X[j*n+i];
  1559. if (fabs(X[j*n+i])>mx)
  1560. { mi = i;
  1561. mx = fabs(X[j*n+i]);
  1562. }
  1563. }
  1564. for (i=j; i<p; i++)
  1565. { t = X[i*n+j];
  1566. X[i*n+j] = X[i*n+mi];
  1567. X[i*n+mi] = t;
  1568. }
  1569. if (w!=NULL) { t = w[j]; w[j] = w[mi]; w[mi] = t; }
  1570. /* want the diag. element -ve, so we do the `good' Householder reflect.
  1571. */
  1572. if (X[(n+1)*j]>0)
  1573. { for (i=j; i<p; i++) X[i*n+j] = -X[i*n+j];
  1574. if (w!=NULL) w[j] = -w[j];
  1575. }
  1576. nx = sqrt(nx);
  1577. c = nx*(nx-X[(n+1)*j]);
  1578. if (c!=0)
  1579. { for (i=j+1; i<p; i++)
  1580. { s = 0;
  1581. for (k=j; k<n; k++)
  1582. s += X[i*n+k]*X[j*n+k];
  1583. s = (s-nx*X[i*n+j])/c;
  1584. for (k=j; k<n; k++)
  1585. X[i*n+k] -= s*X[j*n+k];
  1586. X[i*n+j] += s*nx;
  1587. }
  1588. if (w != NULL)
  1589. { s = 0;
  1590. for (k=j; k<n; k++)
  1591. s += w[k]*X[n*j+k];
  1592. s = (s-nx*w[j])/c;
  1593. for (k=j; k<n; k++)
  1594. w[k] -= s*X[n*j+k];
  1595. w[j] += s*nx;
  1596. }
  1597. X[j*n+j] = nx;
  1598. }
  1599. }
  1600. }
  1601. void qrinvx(R,x,n,p)
  1602. double *R, *x;
  1603. int n, p;
  1604. { int i, j;
  1605. for (i=p-1; i>=0; i--)
  1606. { for (j=i+1; j<p; j++) x[i] -= R[j*n+i]*x[j];
  1607. x[i] /= R[i*n+i];
  1608. }
  1609. }
  1610. void qrtinvx(R,x,n,p)
  1611. double *R, *x;
  1612. int n, p;
  1613. { int i, j;
  1614. for (i=0; i<p; i++)
  1615. { for (j=0; j<i; j++) x[i] -= R[i*n+j]*x[j];
  1616. x[i] /= R[i*n+i];
  1617. }
  1618. }
  1619. void qrsolv(R,x,n,p)
  1620. double *R, *x;
  1621. int n, p;
  1622. { qrtinvx(R,x,n,p);
  1623. qrinvx(R,x,n,p);
  1624. }
  1625. /*
  1626. * Copyright 1996-2006 Catherine Loader.
  1627. */
  1628. /*
  1629. * solve f(x)=c by various methods, with varying stability etc...
  1630. * xlo and xhi should be initial bounds for the solution.
  1631. * convergence criterion is |f(x)-c| < tol.
  1632. *
  1633. * double solve_bisect(f,c,xmin,xmax,tol,bd_flag,err)
  1634. * double solve_secant(f,c,xmin,xmax,tol,bd_flag,err)
  1635. * Bisection and secant methods for solving of f(x)=c.
  1636. * xmin and xmax are starting values and bound for solution.
  1637. * tol = convergence criterion, |f(x)-c| < tol.
  1638. * bd_flag = if (xmin,xmax) doesn't bound a solution, what action to take?
  1639. * BDF_NONE returns error.
  1640. * BDF_EXPRIGHT increases xmax.
  1641. * BDF_EXPLEFT decreases xmin.
  1642. * err = error flag.
  1643. * The (xmin,xmax) bound is not formally necessary for the secant method.
  1644. * But having such a bound vastly improves stability; the code performs
  1645. * a bisection step whenever the iterations run outside the bounds.
  1646. *
  1647. * double solve_nr(f,f1,c,x0,tol,err)
  1648. * Newton-Raphson solution of f(x)=c.
  1649. * f1 = f'(x).
  1650. * x0 = starting value.
  1651. * tol = convergence criteria, |f(x)-c| < tol.
  1652. * err = error flag.
  1653. * No stability checks at present.
  1654. *
  1655. * double solve_fp(f,x0,tol)
  1656. * fixed-point iteration to solve f(x)=x.
  1657. * x0 = starting value.
  1658. * tol = convergence criteria, stops when |f(x)-x| < tol.
  1659. * Convergence requires |f'(x)|<1 in neighborhood of true solution;
  1660. * f'(x) \approx 0 gives the fastest convergence.
  1661. * No stability checks at present.
  1662. *
  1663. * TODO: additional error checking, non-convergence stop.
  1664. */
  1665. #include <math.h>
  1666. #include "mut.h"
  1667. typedef struct {
  1668. double xmin, xmax, x0, x1;
  1669. double ymin, ymax, y0, y1;
  1670. } solvest;
  1671. int step_expand(f,c,sv,bd_flag)
  1672. double (*f)(), c;
  1673. solvest *sv;
  1674. int bd_flag;
  1675. { double x, y;
  1676. if (sv->ymin*sv->ymax <= 0.0) return(0);
  1677. if (bd_flag == BDF_NONE)
  1678. { mut_printf("invalid bracket\n");
  1679. return(1); /* error */
  1680. }
  1681. if (bd_flag == BDF_EXPRIGHT)
  1682. { while (sv->ymin*sv->ymax > 0)
  1683. { x = sv->xmax + 2*(sv->xmax-sv->xmin);
  1684. y = f(x) - c;
  1685. sv->xmin = sv->xmax; sv->xmax = x;
  1686. sv->ymin = sv->ymax; sv->ymax = y;
  1687. }
  1688. return(0);
  1689. }
  1690. if (bd_flag == BDF_EXPLEFT)
  1691. { while (sv->ymin*sv->ymax > 0)
  1692. { x = sv->xmin - 2*(sv->xmax-sv->xmin);
  1693. y = f(x) - c;
  1694. sv->xmax = sv->xmin; sv->xmin = x;
  1695. sv->ymax = sv->ymin; sv->ymin = y;
  1696. }
  1697. return(0);
  1698. }
  1699. mut_printf("step_expand: unknown bd_flag %d.\n",bd_flag);
  1700. return(1);
  1701. }
  1702. int step_addin(sv,x,y)
  1703. solvest *sv;
  1704. double x, y;
  1705. { sv->x1 = sv->x0; sv->x0 = x;
  1706. sv->y1 = sv->y0; sv->y0 = y;
  1707. if (y*sv->ymin > 0)
  1708. { sv->xmin = x;
  1709. sv->ymin = y;
  1710. return(0);
  1711. }
  1712. if (y*sv->ymax > 0)
  1713. { sv->xmax = x;
  1714. sv->ymax = y;
  1715. return(0);
  1716. }
  1717. if (y==0)
  1718. { sv->xmin = sv->xmax = x;
  1719. sv->ymin = sv->ymax = 0;
  1720. return(0);
  1721. }
  1722. return(1);
  1723. }
  1724. int step_bisect(f,c,sv)
  1725. double (*f)(), c;
  1726. solvest *sv;
  1727. { double x, y;
  1728. x = sv->x0 = (sv->xmin + sv->xmax)/2;
  1729. y = sv->y0 = f(x)-c;
  1730. return(step_addin(sv,x,y));
  1731. }
  1732. double solve_bisect(f,c,xmin,xmax,tol,bd_flag,err)
  1733. double (*f)(), c, xmin, xmax, tol;
  1734. int bd_flag, *err;
  1735. { solvest sv;
  1736. int z;
  1737. *err = 0;
  1738. sv.xmin = xmin; sv.ymin = f(xmin)-c;
  1739. sv.xmax = xmax; sv.ymax = f(xmax)-c;
  1740. *err = step_expand(f,c,&sv,bd_flag);
  1741. if (*err>0) return(sv.xmin);
  1742. while(1) /* infinite loop if f is discontinuous */
  1743. { z = step_bisect(f,c,&sv);
  1744. if (z)
  1745. { *err = 1;
  1746. return(sv.x0);
  1747. }
  1748. if (fabs(sv.y0)<tol) return(sv.x0);
  1749. }
  1750. }
  1751. int step_secant(f,c,sv)
  1752. double (*f)(), c;
  1753. solvest *sv;
  1754. { double x, y;
  1755. if (sv->y0==sv->y1) return(step_bisect(f,c,sv));
  1756. x = sv->x0 + (sv->x1-sv->x0)*sv->y0/(sv->y0-sv->y1);
  1757. if ((x<=sv->xmin) | (x>=sv->xmax)) return(step_bisect(f,c,sv));
  1758. y = f(x)-c;
  1759. return(step_addin(sv,x,y));
  1760. }
  1761. double solve_secant(f,c,xmin,xmax,tol,bd_flag,err)
  1762. double (*f)(), c, xmin, xmax, tol;
  1763. int bd_flag, *err;
  1764. { solvest sv;
  1765. int z;
  1766. *err = 0;
  1767. sv.xmin = xmin; sv.ymin = f(xmin)-c;
  1768. sv.xmax = xmax; sv.ymax = f(xmax)-c;
  1769. *err = step_expand(f,c,&sv,bd_flag);
  1770. if (*err>0) return(sv.xmin);
  1771. sv.x0 = sv.xmin; sv.y0 = sv.ymin;
  1772. sv.x1 = sv.xmax; sv.y1 = sv.ymax;
  1773. while(1) /* infinite loop if f is discontinuous */
  1774. { z = step_secant(f,c,&sv);
  1775. if (z)
  1776. { *err = 1;
  1777. return(sv.x0);
  1778. }
  1779. if (fabs(sv.y0)<tol) return(sv.x0);
  1780. }
  1781. }
  1782. double solve_nr(f,f1,c,x0,tol,err)
  1783. double (*f)(), (*f1)(), c, x0, tol;
  1784. int *err;
  1785. { double y;
  1786. do
  1787. { y = f(x0)-c;
  1788. x0 -= y/f1(x0);
  1789. } while (fabs(y)>tol);
  1790. return(x0);
  1791. }
  1792. double solve_fp(f,x0,tol,maxit)
  1793. double (*f)(), x0, tol;
  1794. int maxit;
  1795. { double x1;
  1796. int i;
  1797. for (i=0; i<maxit; i++)
  1798. { x1 = f(x0);
  1799. if (fabs(x1-x0)<tol) return(x1);
  1800. x0 = x1;
  1801. }
  1802. return(x1); /* although it hasn't converged */
  1803. }
  1804. /*
  1805. * Copyright 1996-2006 Catherine Loader.
  1806. */
  1807. #include "mut.h"
  1808. void svd(x,p,q,d,mxit) /* svd of square matrix */
  1809. double *x, *p, *q;
  1810. int d, mxit;
  1811. { int i, j, k, iter, ms, zer;
  1812. double r, u, v, cp, cm, sp, sm, c1, c2, s1, s2, mx;
  1813. for (i=0; i<d; i++)
  1814. for (j=0; j<d; j++) p[i*d+j] = q[i*d+j] = (i==j);
  1815. for (iter=0; iter<mxit; iter++)
  1816. { ms = 0;
  1817. for (i=0; i<d; i++)
  1818. for (j=i+1; j<d; j++)
  1819. { s1 = fabs(x[i*d+j]);
  1820. s2 = fabs(x[j*d+i]);
  1821. mx = (s1>s2) ? s1 : s2;
  1822. zer = 1;
  1823. if (mx*mx>1.0e-15*fabs(x[i*d+i]*x[j*d+j]))
  1824. { if (fabs(x[i*(d+1)])<fabs(x[j*(d+1)]))
  1825. { for (k=0; k<d; k++)
  1826. { u = x[i*d+k]; x[i*d+k] = x[j*d+k]; x[j*d+k] = u;
  1827. u = p[k*d+i]; p[k*d+i] = p[k*d+j]; p[k*d+j] = u;
  1828. }
  1829. for (k=0; k<d; k++)
  1830. { u = x[k*d+i]; x[k*d+i] = x[k*d+j]; x[k*d+j] = u;
  1831. u = q[k*d+i]; q[k*d+i] = q[k*d+j]; q[k*d+j] = u;
  1832. }
  1833. }
  1834. cp = x[i*(d+1)]+x[j*(d+1)];
  1835. sp = x[j*d+i]-x[i*d+j];
  1836. r = sqrt(cp*cp+sp*sp);
  1837. if (r>0) { cp /= r; sp /= r; }
  1838. else { cp = 1.0; zer = 0;}
  1839. cm = x[i*(d+1)]-x[j*(d+1)];
  1840. sm = x[i*d+j]+x[j*d+i];
  1841. r = sqrt(cm*cm+sm*sm);
  1842. if (r>0) { cm /= r; sm /= r; }
  1843. else { cm = 1.0; zer = 0;}
  1844. c1 = cm+cp;
  1845. s1 = sm+sp;
  1846. r = sqrt(c1*c1+s1*s1);
  1847. if (r>0) { c1 /= r; s1 /= r; }
  1848. else { c1 = 1.0; zer = 0;}
  1849. if (fabs(s1)>ms) ms = fabs(s1);
  1850. c2 = cm+cp;
  1851. s2 = sp-sm;
  1852. r = sqrt(c2*c2+s2*s2);
  1853. if (r>0) { c2 /= r; s2 /= r; }
  1854. else { c2 = 1.0; zer = 0;}
  1855. for (k=0; k<d; k++)
  1856. { u = x[i*d+k]; v = x[j*d+k];
  1857. x[i*d+k] = c1*u+s1*v;
  1858. x[j*d+k] = c1*v-s1*u;
  1859. u = p[k*d+i]; v = p[k*d+j];
  1860. p[k*d+i] = c1*u+s1*v;
  1861. p[k*d+j] = c1*v-s1*u;
  1862. }
  1863. for (k=0; k<d; k++)
  1864. { u = x[k*d+i]; v = x[k*d+j];
  1865. x[k*d+i] = c2*u-s2*v;
  1866. x[k*d+j] = s2*u+c2*v;
  1867. u = q[k*d+i]; v = q[k*d+j];
  1868. q[k*d+i] = c2*u-s2*v;
  1869. q[k*d+j] = s2*u+c2*v;
  1870. }
  1871. if (zer) x[i*d+j] = x[j*d+i] = 0.0;
  1872. ms = 1;
  1873. }
  1874. }
  1875. if (ms==0) iter=mxit+10;
  1876. }
  1877. if (iter==mxit) mut_printf("Warning: svd not converged.\n");
  1878. for (i=0; i<d; i++)
  1879. if (x[i*d+i]<0)
  1880. { x[i*d+i] = -x[i*d+i];
  1881. for (j=0; j<d; j++) p[j*d+i] = -p[j*d+i];
  1882. }
  1883. }
  1884. int svdsolve(x,w,P,D,Q,d,tol) /* original X = PDQ^T; comp. QD^{-1}P^T x */
  1885. double *x, *w, *P, *D, *Q, tol;
  1886. int d;
  1887. { int i, j, rank;
  1888. double mx;
  1889. if (tol>0)
  1890. { mx = D[0];
  1891. for (i=1; i<d; i++) if (D[i*(d+1)]>mx) mx = D[i*(d+1)];
  1892. tol *= mx;
  1893. }
  1894. rank = 0;
  1895. for (i=0; i<d; i++)
  1896. { w[i] = 0.0;
  1897. for (j=0; j<d; j++) w[i] += P[j*d+i]*x[j];
  1898. }
  1899. for (i=0; i<d; i++)
  1900. if (D[i*d+i]>tol)
  1901. { w[i] /= D[i*(d+1)];
  1902. rank++;
  1903. }
  1904. for (i=0; i<d; i++)
  1905. { x[i] = 0.0;
  1906. for (j=0; j<d; j++) x[i] += Q[i*d+j]*w[j];
  1907. }
  1908. return(rank);
  1909. }
  1910. void hsvdsolve(x,w,P,D,Q,d,tol) /* original X = PDQ^T; comp. D^{-1/2}P^T x */
  1911. double *x, *w, *P, *D, *Q, tol;
  1912. int d;
  1913. { int i, j;
  1914. double mx;
  1915. if (tol>0)
  1916. { mx = D[0];
  1917. for (i=1; i<d; i++) if (D[i*(d+1)]>mx) mx = D[i*(d+1)];
  1918. tol *= mx;
  1919. }
  1920. for (i=0; i<d; i++)
  1921. { w[i] = 0.0;
  1922. for (j=0; j<d; j++) w[i] += P[j*d+i]*x[j];
  1923. }
  1924. for (i=0; i<d; i++) if (D[i*d+i]>tol) w[i] /= sqrt(D[i*(d+1)]);
  1925. for (i=0; i<d; i++) x[i] = w[i];
  1926. }
  1927. /*
  1928. * Copyright 1996-2006 Catherine Loader.
  1929. */
  1930. /*
  1931. * Includes some miscellaneous vector functions:
  1932. * setzero(v,p) sets all elements of v to 0.
  1933. * unitvec(x,k,p) sets x to k'th unit vector e_k.
  1934. * innerprod(v1,v2,p) inner product.
  1935. * addouter(A,v1,v2,p,c) A <- A + c * v_1 v2^T
  1936. * multmatscal(A,z,n) A <- A*z
  1937. * matrixmultiply(A,B,C,m,n,p) C(m*p) <- A(m*n) * B(n*p)
  1938. * transpose(x,m,n) inline transpose
  1939. * m_trace(x,n) trace
  1940. * vecsum(x,n) sum elements.
  1941. */
  1942. #include "mut.h"
  1943. void setzero(v,p)
  1944. double *v;
  1945. int p;
  1946. { int i;
  1947. for (i=0; i<p; i++) v[i] = 0.0;
  1948. }
  1949. void unitvec(x,k,p)
  1950. double *x;
  1951. int k, p;
  1952. { setzero(x,p);
  1953. x[k] = 1.0;
  1954. }
  1955. double innerprod(v1,v2,p)
  1956. double *v1, *v2;
  1957. int p;
  1958. { int i;
  1959. double s;
  1960. s = 0;
  1961. for (i=0; i<p; i++) s += v1[i]*v2[i];
  1962. return(s);
  1963. }
  1964. void addouter(A,v1,v2,p,c)
  1965. double *A, *v1, *v2, c;
  1966. int p;
  1967. { int i, j;
  1968. for (i=0; i<p; i++)
  1969. for (j=0; j<p; j++)
  1970. A[i*p+j] += c*v1[i]*v2[j];
  1971. }
  1972. void multmatscal(A,z,n)
  1973. double *A, z;
  1974. int n;
  1975. { int i;
  1976. for (i=0; i<n; i++) A[i] *= z;
  1977. }
  1978. /* matrix multiply A (m*n) times B (n*p).
  1979. * store in C (m*p).
  1980. * all matrices stored by column.
  1981. */
  1982. void matrixmultiply(A,B,C,m,n,p)
  1983. double *A, *B, *C;
  1984. int m, n, p;
  1985. { int i, j, k, ij;
  1986. for (i=0; i<m; i++)
  1987. for (j=0; j<p; j++)
  1988. { ij = j*m+i;
  1989. C[ij] = 0.0;
  1990. for (k=0; k<n; k++)
  1991. C[ij] += A[k*m+i] * B[j*n+k];
  1992. }
  1993. }
  1994. /*
  1995. * transpose() transposes an m*n matrix in place.
  1996. * At input, the matrix has n rows, m columns and
  1997. * x[0..n-1] is the is the first column.
  1998. * At output, the matrix has m rows, n columns and
  1999. * x[0..m-1] is the first column.
  2000. */
  2001. void transpose(x,m,n)
  2002. double *x;
  2003. int m, n;
  2004. { int t0, t, ti, tj;
  2005. double z;
  2006. for (t0=1; t0<m*n-2; t0++)
  2007. { ti = t0%m; tj = t0/m;
  2008. do
  2009. { t = ti*n+tj;
  2010. ti= t%m;
  2011. tj= t/m;
  2012. } while (t<t0);
  2013. z = x[t];
  2014. x[t] = x[t0];
  2015. x[t0] = z;
  2016. }
  2017. }
  2018. /* trace of an n*n square matrix. */
  2019. double m_trace(x,n)
  2020. double *x;
  2021. int n;
  2022. { int i;
  2023. double sum;
  2024. sum = 0;
  2025. for (i=0; i<n; i++)
  2026. sum += x[i*(n+1)];
  2027. return(sum);
  2028. }
  2029. double vecsum(v,n)
  2030. double *v;
  2031. int n;
  2032. { int i;
  2033. double sum;
  2034. sum = 0.0;
  2035. for (i=0; i<n; i++) sum += v[i];
  2036. return(sum);
  2037. }
  2038. /*
  2039. * Copyright 1996-2006 Catherine Loader.
  2040. */
  2041. /*
  2042. miscellaneous functions that may not be defined in the math
  2043. libraries. The implementations are crude.
  2044. mut_daws(x) -- dawson's function
  2045. mut_exp(x) -- exp(x), but it won't overflow.
  2046. where required, these must be #define'd in header files.
  2047. also includes
  2048. ptail(x) -- exp(x*x/2)*int_{-\infty}^x exp(-u^2/2)du for x < -6.
  2049. logit(x) -- logistic function.
  2050. expit(x) -- inverse of logit.
  2051. factorial(n)-- factorial
  2052. */
  2053. #include "mut.h"
  2054. double mut_exp(x)
  2055. double x;
  2056. { if (x>700.0) return(1.014232054735004e+304);
  2057. return(exp(x));
  2058. }
  2059. double mut_daws(x)
  2060. double x;
  2061. { static double val[] = {
  2062. 0, 0.24485619356002, 0.46034428261948, 0.62399959848185, 0.72477845900708,
  2063. 0.76388186132749, 0.75213621001998, 0.70541701910853, 0.63998807456541,
  2064. 0.56917098836654, 0.50187821196415, 0.44274283060424, 0.39316687916687,
  2065. 0.35260646480842, 0.31964847250685, 0.29271122077502, 0.27039629581340,
  2066. 0.25160207761769, 0.23551176224443, 0.22153505358518, 0.20924575719548,
  2067. 0.19833146819662, 0.18855782729305, 0.17974461154688, 0.17175005072385 };
  2068. double h, f0, f1, f2, y, z, xx;
  2069. int j, m;
  2070. if (x<0) return(-mut_daws(-x));
  2071. if (x>6)
  2072. { /* Tail series: 1/x + 1/x^3 + 1.3/x^5 + 1.3.5/x^7 + ... */
  2073. y = z = 1/x;
  2074. j = 0;
  2075. while (((f0=(2*j+1)/(x*x))<1) && (y>1.0e-10*z))
  2076. { y *= f0;
  2077. z += y;
  2078. j++;
  2079. }
  2080. return(z);
  2081. }
  2082. m = (int) (4*x);
  2083. h = x-0.25*m;
  2084. if (h>0.125)
  2085. { m++;
  2086. h = h-0.25;
  2087. }
  2088. xx = 0.25*m;
  2089. f0 = val[m];
  2090. f1 = 1-xx*f0;
  2091. z = f0+h*f1;
  2092. y = h;
  2093. j = 2;
  2094. while (fabs(y)>z*1.0e-10)
  2095. { f2 = -(j-1)*f0-xx*f1;
  2096. y *= h/j;
  2097. z += y*f2;
  2098. f0 = f1; f1 = f2;
  2099. j++;
  2100. }
  2101. return(z);
  2102. }
  2103. double ptail(x) /* exp(x*x/2)*int_{-\infty}^x exp(-u^2/2)du for x < -6 */
  2104. double x;
  2105. { double y, z, f0;
  2106. int j;
  2107. y = z = -1.0/x;
  2108. j = 0;
  2109. while ((fabs(f0= -(2*j+1)/(x*x))<1) && (fabs(y)>1.0e-10*z))
  2110. { y *= f0;
  2111. z += y;
  2112. j++;
  2113. }
  2114. return(z);
  2115. }
  2116. double logit(x)
  2117. double x;
  2118. { return(log(x/(1-x)));
  2119. }
  2120. double expit(x)
  2121. double x;
  2122. { double u;
  2123. if (x<0)
  2124. { u = exp(x);
  2125. return(u/(1+u));
  2126. }
  2127. return(1/(1+exp(-x)));
  2128. }
  2129. int factorial(n)
  2130. int n;
  2131. { if (n<=1) return(1.0);
  2132. return(n*factorial(n-1));
  2133. }
  2134. /*
  2135. * Copyright 1996-2006 Catherine Loader.
  2136. */
  2137. /*
  2138. * Constrained maximization of a bivariate function.
  2139. * maxbvgrid(f,x,ll,ur,m0,m1)
  2140. * maximizes over a grid of m0*m1 points. Returns the maximum,
  2141. * and the maximizer through the array x. Usually this is a starter,
  2142. * to choose between local maxima, followed by other routines to refine.
  2143. *
  2144. * maxbvstep(f,x,ymax,h,ll,ur,err)
  2145. * essentially multivariate bisection. A 3x3 grid of points is
  2146. * built around the starting value (x,ymax). This grid is moved
  2147. * around (step size h[0] and h[1] in the two dimensions) until
  2148. * the maximum is in the middle. Then, the step size is halved.
  2149. * Usually, this will be called in a loop.
  2150. * The error flag is set if the maximum can't be centered in a
  2151. * reasonable number of steps.
  2152. *
  2153. * maxbv(f,x,h,ll,ur,m0,m1,tol)
  2154. * combines the two previous functions. It begins with a grid search
  2155. * (if m0>0 and m1>0), followed by refinement. Refines until both h
  2156. * components are < tol.
  2157. */
  2158. #include "mut.h"
  2159. #define max(a,b) ((a)>(b) ? (a) : (b))
  2160. #define min(a,b) ((a)<(b) ? (a) : (b))
  2161. double maxbvgrid(f,x,ll,ur,m0,m1,con)
  2162. double (*f)(), *x, *ll, *ur;
  2163. int m0, m1, *con;
  2164. { int i, j, im, jm;
  2165. double y, ymax;
  2166. im = -1;
  2167. for (i=0; i<=m0; i++)
  2168. { x[0] = ((m0-i)*ll[0] + i*ur[0])/m0;
  2169. for (j=0; j<=m1; j++)
  2170. { x[1] = ((m1-j)*ll[1] + j*ur[1])/m1;
  2171. y = f(x);
  2172. if ((im==-1) || (y>ymax))
  2173. { im = i; jm = j;
  2174. ymax = y;
  2175. }
  2176. }
  2177. }
  2178. x[0] = ((m0-im)*ll[0] + im*ur[0])/m0;
  2179. x[1] = ((m1-jm)*ll[1] + jm*ur[1])/m1;
  2180. con[0] = (im==m0)-(im==0);
  2181. con[1] = (jm==m1)-(jm==0);
  2182. return(ymax);
  2183. }
  2184. double maxbvstep(f,x,ymax,h,ll,ur,err,con)
  2185. double (*f)(), *x, ymax, *h, *ll, *ur;
  2186. int *err, *con;
  2187. { int i, j, ij, imax, steps, cts[2];
  2188. double newx, X[9][2], y[9];
  2189. imax =4; y[4] = ymax;
  2190. for (i=(con[0]==-1)-1; i<2-(con[0]==1); i++)
  2191. for (j=(con[1]==-1)-1; j<2-(con[1]==1); j++)
  2192. { ij = 3*i+j+4;
  2193. X[ij][0] = x[0]+i*h[0];
  2194. if (X[ij][0] < ll[0]+0.001*h[0]) X[ij][0] = ll[0];
  2195. if (X[ij][0] > ur[0]-0.001*h[0]) X[ij][0] = ur[0];
  2196. X[ij][1] = x[1]+j*h[1];
  2197. if (X[ij][1] < ll[1]+0.001*h[1]) X[ij][1] = ll[1];
  2198. if (X[ij][1] > ur[1]-0.001*h[1]) X[ij][1] = ur[1];
  2199. if (ij != 4)
  2200. { y[ij] = f(X[ij]);
  2201. if (y[ij]>ymax) { imax = ij; ymax = y[ij]; }
  2202. }
  2203. }
  2204. steps = 0;
  2205. cts[0] = cts[1] = 0;
  2206. while ((steps<20) && (imax != 4))
  2207. { steps++;
  2208. if ((con[0]>-1) && ((imax/3)==0)) /* shift right */
  2209. {
  2210. cts[0]--;
  2211. for (i=8; i>2; i--)
  2212. { X[i][0] = X[i-3][0]; y[i] = y[i-3];
  2213. }
  2214. imax = imax+3;
  2215. if (X[imax][0]==ll[0])
  2216. con[0] = -1;
  2217. else
  2218. { newx = X[imax][0]-h[0];
  2219. if (newx < ll[0]+0.001*h[0]) newx = ll[0];
  2220. for (i=(con[1]==-1); i<3-(con[1]==1); i++)
  2221. { X[i][0] = newx;
  2222. y[i] = f(X[i]);
  2223. if (y[i]>ymax) { ymax = y[i]; imax = i; }
  2224. }
  2225. con[0] = 0;
  2226. }
  2227. }
  2228. if ((con[0]<1) && ((imax/3)==2)) /* shift left */
  2229. {
  2230. cts[0]++;
  2231. for (i=0; i<6; i++)
  2232. { X[i][0] = X[i+3][0]; y[i] = y[i+3];
  2233. }
  2234. imax = imax-3;
  2235. if (X[imax][0]==ur[0])
  2236. con[0] = 1;
  2237. else
  2238. { newx = X[imax][0]+h[0];
  2239. if (newx > ur[0]-0.001*h[0]) newx = ur[0];
  2240. for (i=6+(con[1]==-1); i<9-(con[1]==1); i++)
  2241. { X[i][0] = newx;
  2242. y[i] = f(X[i]);
  2243. if (y[i]>ymax) { ymax = y[i]; imax = i; }
  2244. }
  2245. con[0] = 0;
  2246. }
  2247. }
  2248. if ((con[1]>-1) && ((imax%3)==0)) /* shift up */
  2249. {
  2250. cts[1]--;
  2251. for (i=9; i>0; i--) if (i%3 > 0)
  2252. { X[i][1] = X[i-1][1]; y[i] = y[i-1];
  2253. }
  2254. imax = imax+1;
  2255. if (X[imax][1]==ll[1])
  2256. con[1] = -1;
  2257. else
  2258. { newx = X[imax][1]-h[1];
  2259. if (newx < ll[1]+0.001*h[1]) newx = ll[1];
  2260. for (i=3*(con[0]==-1); i<7-(con[0]==1); i=i+3)
  2261. { X[i][1] = newx;
  2262. y[i] = f(X[i]);
  2263. if (y[i]>ymax) { ymax = y[i]; imax = i; }
  2264. }
  2265. con[1] = 0;
  2266. }
  2267. }
  2268. if ((con[1]<1) && ((imax%3)==2)) /* shift down */
  2269. {
  2270. cts[1]++;
  2271. for (i=0; i<9; i++) if (i%3 < 2)
  2272. { X[i][1] = X[i+1][1]; y[i] = y[i+1];
  2273. }
  2274. imax = imax-1;
  2275. if (X[imax][1]==ur[1])
  2276. con[1] = 1;
  2277. else
  2278. { newx = X[imax][1]+h[1];
  2279. if (newx > ur[1]-0.001*h[1]) newx = ur[1];
  2280. for (i=2+3*(con[0]==-1); i<9-(con[0]==1); i=i+3)
  2281. { X[i][1] = newx;
  2282. y[i] = f(X[i]);
  2283. if (y[i]>ymax) { ymax = y[i]; imax = i; }
  2284. }
  2285. con[1] = 0;
  2286. }
  2287. }
  2288. /* if we've taken 3 steps in one direction, try increasing the
  2289. * corresponding h.
  2290. */
  2291. if ((cts[0]==-2) | (cts[0]==2))
  2292. { h[0] = 2*h[0]; cts[0] = 0; }
  2293. if ((cts[1]==-2) | (cts[1]==2))
  2294. { h[1] = 2*h[1]; cts[1] = 0; }
  2295. }
  2296. if (steps==40)
  2297. *err = 1;
  2298. else
  2299. {
  2300. h[0] /= 2.0; h[1] /= 2.0;
  2301. *err = 0;
  2302. }
  2303. x[0] = X[imax][0];
  2304. x[1] = X[imax][1];
  2305. return(y[imax]);
  2306. }
  2307. #define BQMmaxp 5
  2308. int boxquadmin(J,b0,p,x0,ll,ur)
  2309. jacobian *J;
  2310. double *b0, *x0, *ll, *ur;
  2311. int p;
  2312. { double b[BQMmaxp], x[BQMmaxp], L[BQMmaxp*BQMmaxp], C[BQMmaxp*BQMmaxp], d[BQMmaxp];
  2313. double f, fmin;
  2314. int i, imin, m, con[BQMmaxp], rlx;
  2315. if (p>BQMmaxp) mut_printf("boxquadmin: maxp is 5.\n");
  2316. if (J->st != JAC_RAW) mut_printf("boxquadmin: must start with JAC_RAW.\n");
  2317. m = 0;
  2318. setzero(L,p*p);
  2319. setzero(x,p);
  2320. memcpy(C,J->Z,p*p*sizeof(double));
  2321. for (i=0; i<p; i++) con[i] = 0;
  2322. do
  2323. {
  2324. /* first, keep minimizing and add constraints, one at a time.
  2325. */
  2326. do
  2327. {
  2328. matrixmultiply(C,x,b,p,p,1);
  2329. for (i=0; i<p; i++) b[i] += b0[i];
  2330. conquadmin(J,b,p,L,d,m);
  2331. /* if C matrix is not pd, don't even bother.
  2332. * this relies on having used cholesky dec.
  2333. */
  2334. if ((J->Z[0]==0.0) | (J->Z[3]==0.0)) return(1);
  2335. fmin = 1.0;
  2336. for (i=0; i<p; i++) if (con[i]==0)
  2337. { f = 1.0;
  2338. if (x0[i]+x[i]+b[i] < ll[i]) f = (ll[i]-x[i]-x0[i])/b[i];
  2339. if (x0[i]+x[i]+b[i] > ur[i]) f = (ur[i]-x[i]-x0[i])/b[i];
  2340. if (f<fmin) fmin = f;
  2341. imin = i;
  2342. }
  2343. for (i=0; i<p; i++) x[i] += fmin*b[i];
  2344. if (fmin<1.0)
  2345. { L[m*p+imin] = 1;
  2346. m++;
  2347. con[imin] = (b[imin]<0) ? -1 : 1;
  2348. }
  2349. } while ((fmin < 1.0) & (m<p));
  2350. /* now, can I relax any constraints?
  2351. * compute slopes at current point. Can relax if:
  2352. * slope is -ve on a lower boundary.
  2353. * slope is +ve on an upper boundary.
  2354. */
  2355. rlx = 0;
  2356. if (m>0)
  2357. { matrixmultiply(C,x,b,p,p,1);
  2358. for (i=0; i<p; i++) b[i] += b0[i];
  2359. for (i=0; i<p; i++)
  2360. { if ((con[i]==-1)&& (b[i]<0)) { con[i] = 0; rlx = 1; }
  2361. if ((con[i]==1) && (b[i]>0)) { con[i] = 0; rlx = 1; }
  2362. }
  2363. if (rlx) /* reconstruct the constraint matrix */
  2364. { setzero(L,p*p); m = 0;
  2365. for (i=0; i<p; i++) if (con[i] != 0)
  2366. { L[m*p+i] = 1;
  2367. m++;
  2368. }
  2369. }
  2370. }
  2371. } while (rlx);
  2372. memcpy(b0,x,p*sizeof(double)); /* this is how far we should move from x0 */
  2373. return(0);
  2374. }
  2375. double maxquadstep(f,x,ymax,h,ll,ur,err,con)
  2376. double (*f)(), *x, ymax, *h, *ll, *ur;
  2377. int *err, *con;
  2378. { jacobian J;
  2379. double b[2], c[2], d, jwork[12];
  2380. double x0, x1, y0, y1, ym, h0, xl[2], xu[2], xi[2];
  2381. int i, m;
  2382. *err = 0;
  2383. /* first, can we relax any of the initial constraints?
  2384. * if so, just do one step away from the boundary, and
  2385. * return for restart.
  2386. */
  2387. for (i=0; i<2; i++)
  2388. if (con[i] != 0)
  2389. { xi[0] = x[0]; xi[1] = x[1];
  2390. xi[i] = x[i]-con[i]*h[i];
  2391. y0 = f(xi);
  2392. if (y0>ymax)
  2393. { memcpy(x,xi,2*sizeof(double));
  2394. con[i] = 0;
  2395. return(y0);
  2396. }
  2397. }
  2398. /* now, all initial constraints remain active.
  2399. */
  2400. m = 9;
  2401. for (i=0; i<2; i++) if (con[i]==0)
  2402. { m /= 3;
  2403. xl[0] = x[0]; xl[1] = x[1];
  2404. xl[i] = max(x[i]-h[i],ll[i]); y0 = f(xl);
  2405. x0 = xl[i]-x[i]; y0 -= ymax;
  2406. xu[0] = x[0]; xu[1] = x[1];
  2407. xu[i] = min(x[i]+h[i],ur[i]); y1 = f(xu);
  2408. x1 = xu[i]-x[i]; y1 -= ymax;
  2409. if (x0*x1*(x1-x0)==0) { *err = 1; return(0.0); }
  2410. b[i] = (x0*x0*y1-x1*x1*y0)/(x0*x1*(x0-x1));
  2411. c[i] = 2*(x0*y1-x1*y0)/(x0*x1*(x1-x0));
  2412. if (c[i] >= 0.0) { *err = 1; return(0.0); }
  2413. xi[i] = (b[i]<0) ? xl[i] : xu[i];
  2414. }
  2415. else { c[i] = -1.0; b[i] = 0.0; } /* enforce initial constraints */
  2416. if ((con[0]==0) && (con[1]==0))
  2417. { x0 = xi[0]-x[0];
  2418. x1 = xi[1]-x[1];
  2419. ym = f(xi) - ymax - b[0]*x0 - c[0]*x0*x0/2 - b[1]*x1 - c[1]*x1*x1/2;
  2420. d = ym/(x0*x1);
  2421. }
  2422. else d = 0.0;
  2423. /* now, maximize the quadratic.
  2424. * y[4] + b0*x0 + b1*x1 + 0.5(c0*x0*x0 + c1*x1*x1 + 2*d*x0*x1)
  2425. * -ve everything, to call quadmin.
  2426. */
  2427. jac_alloc(&J,2,jwork);
  2428. J.Z[0] = -c[0];
  2429. J.Z[1] = J.Z[2] = -d;
  2430. J.Z[3] = -c[1];
  2431. J.st = JAC_RAW;
  2432. J.p = 2;
  2433. b[0] = -b[0]; b[1] = -b[1];
  2434. *err = boxquadmin(&J,b,2,x,ll,ur);
  2435. if (*err) return(ymax);
  2436. /* test to see if this step successfully increases...
  2437. */
  2438. for (i=0; i<2; i++)
  2439. { xi[i] = x[i]+b[i];
  2440. if (xi[i]<ll[i]+1e-8*h[i]) xi[i] = ll[i];
  2441. if (xi[i]>ur[i]-1e-8*h[i]) xi[i] = ur[i];
  2442. }
  2443. y1 = f(xi);
  2444. if (y1 < ymax) /* no increase */
  2445. { *err = 1;
  2446. return(ymax);
  2447. }
  2448. /* wonderful. update x, h, with the restriction that h can't decrease
  2449. * by a factor over 10, or increase by over 2.
  2450. */
  2451. for (i=0; i<2; i++)
  2452. { x[i] = xi[i];
  2453. if (x[i]==ll[i]) con[i] = -1;
  2454. if (x[i]==ur[i]) con[i] = 1;
  2455. h0 = fabs(b[i]);
  2456. h0 = min(h0,2*h[i]);
  2457. h0 = max(h0,h[i]/10);
  2458. h[i] = min(h0,(ur[i]-ll[i])/2.0);
  2459. }
  2460. return(y1);
  2461. }
  2462. double maxbv(f,x,h,ll,ur,m0,m1,tol)
  2463. double (*f)(), *x, *h, *ll, *ur, tol;
  2464. int m0, m1;
  2465. { double ymax;
  2466. int err, con[2];
  2467. con[0] = con[1] = 0;
  2468. if ((m0>0) & (m1>0))
  2469. {
  2470. ymax = maxbvgrid(f,x,ll,ur,m0,m1,con);
  2471. h[0] = (ur[0]-ll[0])/(2*m0);
  2472. h[1] = (ur[1]-ll[1])/(2*m1);
  2473. }
  2474. else
  2475. { x[0] = (ll[0]+ur[0])/2;
  2476. x[1] = (ll[1]+ur[1])/2;
  2477. h[0] = (ur[0]-ll[0])/2;
  2478. h[1] = (ur[1]-ll[1])/2;
  2479. ymax = f(x);
  2480. }
  2481. while ((h[0]>tol) | (h[1]>tol))
  2482. { ymax = maxbvstep(f,x,ymax,h,ll,ur,&err,con);
  2483. if (err) mut_printf("maxbvstep failure\n");
  2484. }
  2485. return(ymax);
  2486. }
  2487. double maxbvq(f,x,h,ll,ur,m0,m1,tol)
  2488. double (*f)(), *x, *h, *ll, *ur, tol;
  2489. int m0, m1;
  2490. { double ymax;
  2491. int err, con[2];
  2492. con[0] = con[1] = 0;
  2493. if ((m0>0) & (m1>0))
  2494. {
  2495. ymax = maxbvgrid(f,x,ll,ur,m0,m1,con);
  2496. h[0] = (ur[0]-ll[0])/(2*m0);
  2497. h[1] = (ur[1]-ll[1])/(2*m1);
  2498. }
  2499. else
  2500. { x[0] = (ll[0]+ur[0])/2;
  2501. x[1] = (ll[1]+ur[1])/2;
  2502. h[0] = (ur[0]-ll[0])/2;
  2503. h[1] = (ur[1]-ll[1])/2;
  2504. ymax = f(x);
  2505. }
  2506. while ((h[0]>tol) | (h[1]>tol))
  2507. { /* first, try a quadratric step */
  2508. ymax = maxquadstep(f,x,ymax,h,ll,ur,&err,con);
  2509. /* if the quadratic step fails, move the grid around */
  2510. if (err)
  2511. {
  2512. ymax = maxbvstep(f,x,ymax,h,ll,ur,&err,con);
  2513. if (err)
  2514. { mut_printf("maxbvstep failure\n");
  2515. return(ymax);
  2516. }
  2517. }
  2518. }
  2519. return(ymax);
  2520. }
  2521. /*
  2522. * Copyright 1996-2006 Catherine Loader.
  2523. */
  2524. #include "mut.h"
  2525. prf mut_printf = (prf)printf;
  2526. void mut_redirect(newprf)
  2527. prf newprf;
  2528. { mut_printf = newprf;
  2529. }
  2530. /*
  2531. * Copyright 1996-2006 Catherine Loader.
  2532. */
  2533. /*
  2534. * function to find order of observations in an array.
  2535. *
  2536. * mut_order(x,ind,i0,i1)
  2537. * x array to find order of.
  2538. * ind integer array of indexes.
  2539. * i0,i1 (integers) range to order.
  2540. *
  2541. * at output, ind[i0...i1] are permuted so that
  2542. * x[ind[i0]] <= x[ind[i0+1]] <= ... <= x[ind[i1]].
  2543. * (with ties, output order of corresponding indices is arbitrary).
  2544. * The array x is unchanged.
  2545. *
  2546. * Typically, if x has length n, then i0=0, i1=n-1 and
  2547. * ind is (any permutation of) 0...n-1.
  2548. */
  2549. #include "mut.h"
  2550. double med3(x0,x1,x2)
  2551. double x0, x1, x2;
  2552. { if (x0<x1)
  2553. { if (x2<x0) return(x0);
  2554. if (x1<x2) return(x1);
  2555. return(x2);
  2556. }
  2557. /* x1 < x0 */
  2558. if (x2<x1) return(x1);
  2559. if (x0<x2) return(x0);
  2560. return(x2);
  2561. }
  2562. void mut_order(x,ind,i0,i1)
  2563. double *x;
  2564. int *ind, i0, i1;
  2565. { double piv;
  2566. int i, l, r, z;
  2567. if (i1<=i0) return;
  2568. piv = med3(x[ind[i0]],x[ind[i1]],x[ind[(i0+i1)/2]]);
  2569. l = i0; r = i0-1;
  2570. /* at each stage,
  2571. * x[i0..l-1] < piv
  2572. * x[l..r] = piv
  2573. * x[r+1..i-1] > piv
  2574. * then, decide where to put x[i].
  2575. */
  2576. for (i=i0; i<=i1; i++)
  2577. { if (x[ind[i]]==piv)
  2578. { r++;
  2579. z = ind[i]; ind[i] = ind[r]; ind[r] = z;
  2580. }
  2581. else if (x[ind[i]]<piv)
  2582. { r++;
  2583. z = ind[i]; ind[i] = ind[r]; ind[r] = ind[l]; ind[l] = z;
  2584. l++;
  2585. }
  2586. }
  2587. if (l>i0) mut_order(x,ind,i0,l-1);
  2588. if (r<i1) mut_order(x,ind,r+1,i1);
  2589. }
  2590. /*
  2591. * Copyright 1996-2006 Catherine Loader.
  2592. */
  2593. #include "mut.h"
  2594. #define LOG_2 0.6931471805599453094172321214581765680755
  2595. #define IBETA_LARGE 1.0e30
  2596. #define IBETA_SMALL 1.0e-30
  2597. #define IGAMMA_LARGE 1.0e30
  2598. #define DOUBLE_EP 2.2204460492503131E-16
  2599. double ibeta(x, a, b)
  2600. double x, a, b;
  2601. { int flipped = 0, i, k, count;
  2602. double I = 0, temp, pn[6], ak, bk, next, prev, factor, val;
  2603. if (x <= 0) return(0);
  2604. if (x >= 1) return(1);
  2605. /* use ibeta(x,a,b) = 1-ibeta(1-x,b,z) */
  2606. if ((a+b+1)*x > (a+1))
  2607. { flipped = 1;
  2608. temp = a;
  2609. a = b;
  2610. b = temp;
  2611. x = 1 - x;
  2612. }
  2613. pn[0] = 0.0;
  2614. pn[2] = pn[3] = pn[1] = 1.0;
  2615. count = 1;
  2616. val = x/(1.0-x);
  2617. bk = 1.0;
  2618. next = 1.0;
  2619. do
  2620. { count++;
  2621. k = count/2;
  2622. prev = next;
  2623. if (count%2 == 0)
  2624. ak = -((a+k-1.0)*(b-k)*val)/((a+2.0*k-2.0)*(a+2.0*k-1.0));
  2625. else
  2626. ak = ((a+b+k-1.0)*k*val)/((a+2.0*k)*(a+2.0*k-1.0));
  2627. pn[4] = bk*pn[2] + ak*pn[0];
  2628. pn[5] = bk*pn[3] + ak*pn[1];
  2629. next = pn[4] / pn[5];
  2630. for (i=0; i<=3; i++)
  2631. pn[i] = pn[i+2];
  2632. if (fabs(pn[4]) >= IBETA_LARGE)
  2633. for (i=0; i<=3; i++)
  2634. pn[i] /= IBETA_LARGE;
  2635. if (fabs(pn[4]) <= IBETA_SMALL)
  2636. for (i=0; i<=3; i++)
  2637. pn[i] /= IBETA_SMALL;
  2638. } while (fabs(next-prev) > DOUBLE_EP*prev);
  2639. /* factor = a*log(x) + (b-1)*log(1-x);
  2640. factor -= mut_lgamma(a+1) + mut_lgamma(b) - mut_lgamma(a+b); */
  2641. factor = dbeta(x,a,b,1) + log(x/a);
  2642. I = exp(factor) * next;
  2643. return(flipped ? 1-I : I);
  2644. }
  2645. /*
  2646. * Incomplete gamma function.
  2647. * int_0^x u^{df-1} e^{-u} du / Gamma(df).
  2648. */
  2649. double igamma(x, df)
  2650. double x, df;
  2651. { double factor, term, gintegral, pn[6], rn, ak, bk;
  2652. int i, count, k;
  2653. if (x <= 0.0) return(0.0);
  2654. if (df < 1.0)
  2655. return( dgamma(x,df+1.0,1.0,0) + igamma(x,df+1.0) );
  2656. factor = x * dgamma(x,df,1.0,0);
  2657. /* factor = exp(df*log(x) - x - lgamma(df)); */
  2658. if (x > 1.0 && x >= df)
  2659. {
  2660. pn[0] = 0.0;
  2661. pn[2] = pn[1] = 1.0;
  2662. pn[3] = x;
  2663. count = 1;
  2664. rn = 1.0 / x;
  2665. do
  2666. { count++;
  2667. k = count / 2;
  2668. gintegral = rn;
  2669. if (count%2 == 0)
  2670. { bk = 1.0;
  2671. ak = (double)k - df;
  2672. } else
  2673. { bk = x;
  2674. ak = (double)k;
  2675. }
  2676. pn[4] = bk*pn[2] + ak*pn[0];
  2677. pn[5] = bk*pn[3] + ak*pn[1];
  2678. rn = pn[4] / pn[5];
  2679. for (i=0; i<4; i++)
  2680. pn[i] = pn[i+2];
  2681. if (pn[4] > IGAMMA_LARGE)
  2682. for (i=0; i<4; i++)
  2683. pn[i] /= IGAMMA_LARGE;
  2684. } while (fabs(gintegral-rn) > DOUBLE_EP*rn);
  2685. gintegral = 1.0 - factor*rn;
  2686. }
  2687. else
  2688. { /* For x<df, use the series
  2689. * dpois(df,x)*( 1 + x/(df+1) + x^2/((df+1)(df+2)) + ... )
  2690. * This could be slow if df large and x/df is close to 1.
  2691. */
  2692. gintegral = term = 1.0;
  2693. rn = df;
  2694. do
  2695. { rn += 1.0;
  2696. term *= x/rn;
  2697. gintegral += term;
  2698. } while (term > DOUBLE_EP*gintegral);
  2699. gintegral *= factor/df;
  2700. }
  2701. return(gintegral);
  2702. }
  2703. double pf(q, df1, df2)
  2704. double q, df1, df2;
  2705. { return(ibeta(q*df1/(df2+q*df1), df1/2, df2/2));
  2706. }
  2707. /*
  2708. * Copyright 1996-2006 Catherine Loader.
  2709. */
  2710. #include "mut.h"
  2711. #include <string.h>
  2712. /* quadmin: minimize the quadratic,
  2713. * 2<x,b> + x^T A x.
  2714. * x = -A^{-1} b.
  2715. *
  2716. * conquadmin: min. subject to L'x = d (m constraints)
  2717. * x = -A^{-1}(b+Ly) (y = Lagrange multiplier)
  2718. * y = -(L'A^{-1}L)^{-1} (L'A^{-1}b)
  2719. * x = -A^{-1}b + A^{-1}L (L'A^{-1}L)^{-1} [(L'A^{-1})b + d]
  2720. * (non-zero d to be added!!)
  2721. *
  2722. * qprogmin: min. subject to L'x >= 0.
  2723. */
  2724. void quadmin(J,b,p)
  2725. jacobian *J;
  2726. double *b;
  2727. int p;
  2728. { int i;
  2729. jacob_dec(J,JAC_CHOL);
  2730. i = jacob_solve(J,b);
  2731. if (i<p) mut_printf("quadmin singular %2d %2d\n",i,p);
  2732. for (i=0; i<p; i++) b[i] = -b[i];
  2733. }
  2734. /* project vector a (length n) onto
  2735. * columns of X (n rows, m columns, organized by column).
  2736. * store result in y.
  2737. */
  2738. #define pmaxm 10
  2739. #define pmaxn 100
  2740. void project(a,X,y,n,m)
  2741. double *a, *X, *y;
  2742. int n, m;
  2743. { double xta[pmaxm], R[pmaxn*pmaxm];
  2744. int i;
  2745. if (n>pmaxn) mut_printf("project: n too large\n");
  2746. if (m>pmaxm) mut_printf("project: m too large\n");
  2747. for (i=0; i<m; i++) xta[i] = innerprod(a,&X[i*n],n);
  2748. memcpy(R,X,m*n*sizeof(double));
  2749. qr(R,n,m,NULL);
  2750. qrsolv(R,xta,n,m);
  2751. matrixmultiply(X,xta,y,n,m,1);
  2752. }
  2753. void resproj(a,X,y,n,m)
  2754. double *a, *X, *y;
  2755. int n, m;
  2756. { int i;
  2757. project(a,X,y,n,m);
  2758. for (i=0; i<n; i++) y[i] = a[i]-y[i];
  2759. }
  2760. /* x = -A^{-1}b + A^{-1}L (L'A^{-1}L)^{-1} [(L'A^{-1})b + d] */
  2761. void conquadmin(J,b,n,L,d,m)
  2762. jacobian *J;
  2763. double *b, *L, *d;
  2764. int m, n;
  2765. { double bp[10], L0[100];
  2766. int i, j;
  2767. if (n>10) mut_printf("conquadmin: max. n is 10.\n");
  2768. memcpy(L0,L,n*m*sizeof(double));
  2769. jacob_dec(J,JAC_CHOL);
  2770. for (i=0; i<m; i++) jacob_hsolve(J,&L[i*n]);
  2771. jacob_hsolve(J,b);
  2772. resproj(b,L,bp,n,m);
  2773. jacob_isolve(J,bp);
  2774. for (i=0; i<n; i++) b[i] = -bp[i];
  2775. qr(L,n,m,NULL);
  2776. qrsolv(L,d,n,m);
  2777. for (i=0; i<n; i++)
  2778. { bp[i] = 0;
  2779. for (j=0; j<m; j++) bp[i] += L0[j*n+i]*d[j];
  2780. }
  2781. jacob_solve(J,bp);
  2782. for (i=0; i<n; i++) b[i] += bp[i];
  2783. }
  2784. void qactivemin(J,b,n,L,d,m,ac)
  2785. jacobian *J;
  2786. double *b, *L, *d;
  2787. int m, n, *ac;
  2788. { int i, nac;
  2789. double M[100], dd[10];
  2790. nac = 0;
  2791. for (i=0; i<m; i++) if (ac[i]>0)
  2792. { memcpy(&M[nac*n],&L[i*n],n*sizeof(double));
  2793. dd[nac] = d[i];
  2794. nac++;
  2795. }
  2796. conquadmin(J,b,n,M,dd,nac);
  2797. }
  2798. /* return 1 for full step; 0 if new constraint imposed. */
  2799. int movefrom(x0,x,n,L,d,m,ac)
  2800. double *x0, *x, *L, *d;
  2801. int n, m, *ac;
  2802. { int i, imin;
  2803. double c0, c1, lb, lmin;
  2804. lmin = 1.0;
  2805. for (i=0; i<m; i++) if (ac[i]==0)
  2806. { c1 = innerprod(&L[i*n],x,n)-d[i];
  2807. if (c1<0.0)
  2808. { c0 = innerprod(&L[i*n],x0,n)-d[i];
  2809. if (c0<0.0)
  2810. { if (c1<c0) { lmin = 0.0; imin = 1; }
  2811. }
  2812. else
  2813. { lb = c0/(c0-c1);
  2814. if (lb<lmin) { lmin = lb; imin = i; }
  2815. }
  2816. }
  2817. }
  2818. for (i=0; i<n; i++)
  2819. x0[i] = lmin*x[i]+(1-lmin)*x0[i];
  2820. if (lmin==1.0) return(1);
  2821. ac[imin] = 1;
  2822. return(0);
  2823. }
  2824. int qstep(J,b,x0,n,L,d,m,ac,deac)
  2825. jacobian *J;
  2826. double *b, *x0, *L, *d;
  2827. int m, n, *ac, deac;
  2828. { double x[10];
  2829. int i;
  2830. if (m>10) mut_printf("qstep: too many constraints.\n");
  2831. if (deac)
  2832. { for (i=0; i<m; i++) if (ac[i]==1)
  2833. { ac[i] = 0;
  2834. memcpy(x,b,n*sizeof(double));
  2835. qactivemin(J,x,n,L,d,m,ac);
  2836. if (innerprod(&L[i*n],x,n)>d[i]) /* deactivate this constraint; should rem. */
  2837. i = m+10;
  2838. else
  2839. ac[i] = 1;
  2840. }
  2841. if (i==m) return(0); /* no deactivation possible */
  2842. }
  2843. do
  2844. { if (!deac)
  2845. { memcpy(x,b,n*sizeof(double));
  2846. qactivemin(J,x,n,L,d,m,ac);
  2847. }
  2848. i = movefrom(x0,x,n,L,d,m,ac);
  2849. deac = 0;
  2850. } while (i==0);
  2851. return(1);
  2852. }
  2853. /*
  2854. * x0 is starting value; should satisfy constraints.
  2855. * L is n*m constraint matrix.
  2856. * ac is active constraint vector:
  2857. * ac[i]=0, inactive.
  2858. * ac[i]=1, active, but can be deactivated.
  2859. * ac[i]=2, active, cannot be deactivated.
  2860. */
  2861. void qprogmin(J,b,x0,n,L,d,m,ac)
  2862. jacobian *J;
  2863. double *b, *x0, *L, *d;
  2864. int m, n, *ac;
  2865. { int i;
  2866. for (i=0; i<m; i++) if (ac[i]==0)
  2867. { if (innerprod(&L[i*n],x0,n) < d[i]) ac[i] = 1; }
  2868. jacob_dec(J,JAC_CHOL);
  2869. qstep(J,b,x0,n,L,d,m,ac,0);
  2870. while (qstep(J,b,x0,n,L,d,m,ac,1));
  2871. }
  2872. void qpm(A,b,x0,n,L,d,m,ac)
  2873. double *A, *b, *x0, *L, *d;
  2874. int *n, *m, *ac;
  2875. { jacobian J;
  2876. double wk[1000];
  2877. jac_alloc(&J,*n,wk);
  2878. memcpy(J.Z,A,(*n)*(*n)*sizeof(double));
  2879. J.p = *n;
  2880. J.st = JAC_RAW;
  2881. qprogmin(&J,b,x0,*n,L,d,*m,ac);
  2882. }