libmut.c 75 KB

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