demo_ode.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #include <setjmp.h>
  23. #include <ode/ode.h>
  24. #ifdef _MSC_VER
  25. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  26. #endif
  27. //****************************************************************************
  28. // matrix accessors
  29. #define _A(i,j) A[(i)*4+(j)]
  30. #define _I(i,j) I[(i)*4+(j)]
  31. #define _R(i,j) R[(i)*4+(j)]
  32. //****************************************************************************
  33. // tolerances
  34. #ifdef dDOUBLE
  35. const double tol = 1e-10;
  36. #endif
  37. #ifdef dSINGLE
  38. const double tol = 1e-5;
  39. #endif
  40. //****************************************************************************
  41. // misc messages and error handling
  42. #ifdef __GNUC__
  43. #define HEADER printf ("%s()\n", __FUNCTION__);
  44. #else
  45. #define HEADER printf ("%s:%d\n",__FILE__,__LINE__);
  46. #endif
  47. static jmp_buf jump_buffer;
  48. void myMessageFunction (int num, const char *msg, va_list ap)
  49. {
  50. printf ("(Message %d: ",num);
  51. vprintf (msg,ap);
  52. printf (")");
  53. dSetMessageHandler (0);
  54. longjmp (jump_buffer,1);
  55. }
  56. #define TRAP_MESSAGE(do,ifnomsg,ifmsg) \
  57. dSetMessageHandler (&myMessageFunction); \
  58. if (setjmp (jump_buffer)) { \
  59. dSetMessageHandler (0); \
  60. ifmsg ; \
  61. } \
  62. else { \
  63. dSetMessageHandler (&myMessageFunction); \
  64. do ; \
  65. ifnomsg ; \
  66. } \
  67. dSetMessageHandler (0);
  68. //****************************************************************************
  69. // utility stuff
  70. // compare two numbers, within a threshhold, return 1 if approx equal
  71. int cmp (dReal a, dReal b)
  72. {
  73. return (fabs(a-b) < tol);
  74. }
  75. //****************************************************************************
  76. // matrix utility stuff
  77. // compare a 3x3 matrix with the identity
  78. int cmpIdentityMat3 (dMatrix3 A)
  79. {
  80. return
  81. (cmp(_A(0,0),1.0) && cmp(_A(0,1),0.0) && cmp(_A(0,2),0.0) &&
  82. cmp(_A(1,0),0.0) && cmp(_A(1,1),1.0) && cmp(_A(1,2),0.0) &&
  83. cmp(_A(2,0),0.0) && cmp(_A(2,1),0.0) && cmp(_A(2,2),1.0));
  84. }
  85. // transpose a 3x3 matrix in-line
  86. void transpose3x3 (dMatrix3 A)
  87. {
  88. dReal tmp;
  89. tmp=A[4]; A[4]=A[1]; A[1]=tmp;
  90. tmp=A[8]; A[8]=A[2]; A[2]=tmp;
  91. tmp=A[9]; A[9]=A[6]; A[6]=tmp;
  92. }
  93. //****************************************************************************
  94. // test miscellaneous math functions
  95. void testRandomNumberGenerator()
  96. {
  97. HEADER;
  98. if (dTestRand()) printf ("\tpassed\n");
  99. else printf ("\tFAILED\n");
  100. }
  101. void testInfinity()
  102. {
  103. HEADER;
  104. if (1e10 < dInfinity && -1e10 > -dInfinity && -dInfinity < dInfinity)
  105. printf ("\tpassed\n");
  106. else printf ("\tFAILED\n");
  107. }
  108. void testPad()
  109. {
  110. HEADER;
  111. char s[100];
  112. s[0]=0;
  113. for (int i=0; i<=16; i++) sprintf (s+strlen(s),"%d ",dPAD(i));
  114. printf ("\t%s\n", strcmp(s,"0 1 4 4 4 8 8 8 8 12 12 12 12 16 16 16 16 ") ?
  115. "FAILED" : "passed");
  116. }
  117. void testCrossProduct()
  118. {
  119. HEADER;
  120. dVector3 a1,a2,b,c;
  121. dMatrix3 B;
  122. dMakeRandomVector (b,3,1.0);
  123. dMakeRandomVector (c,3,1.0);
  124. dCROSS (a1,=,b,c);
  125. dSetZero (B,12);
  126. dCROSSMAT (B,b,4,+,-);
  127. dMultiply0 (a2,B,c,3,3,1);
  128. dReal diff = dMaxDifference(a1,a2,3,1);
  129. printf ("\t%s\n", diff > tol ? "FAILED" : "passed");
  130. }
  131. void testSetZero()
  132. {
  133. HEADER;
  134. dReal a[100];
  135. dMakeRandomVector (a,100,1.0);
  136. dSetZero (a,100);
  137. for (int i=0; i<100; i++) if (a[i] != 0.0) {
  138. printf ("\tFAILED\n");
  139. return;
  140. }
  141. printf ("\tpassed\n");
  142. }
  143. void testNormalize3()
  144. {
  145. HEADER;
  146. int i,j,bad=0;
  147. dVector3 n1,n2;
  148. for (i=0; i<1000; i++) {
  149. dMakeRandomVector (n1,3,1.0);
  150. for (j=0; j<3; j++) n2[j]=n1[j];
  151. dNormalize3 (n2);
  152. if (dFabs(dDOT(n2,n2) - 1.0) > tol) bad |= 1;
  153. if (dFabs(n2[0]/n1[0] - n2[1]/n1[1]) > tol) bad |= 2;
  154. if (dFabs(n2[0]/n1[0] - n2[2]/n1[2]) > tol) bad |= 4;
  155. if (dFabs(n2[1]/n1[1] - n2[2]/n1[2]) > tol) bad |= 8;
  156. if (dFabs(dDOT(n2,n1) - dSqrt(dDOT(n1,n1))) > tol) bad |= 16;
  157. if (bad) {
  158. printf ("\tFAILED (code=%x)\n",bad);
  159. return;
  160. }
  161. }
  162. printf ("\tpassed\n");
  163. }
  164. /*
  165. void testReorthonormalize()
  166. {
  167. HEADER;
  168. dMatrix3 R,I;
  169. dMakeRandomMatrix (R,3,3,1.0);
  170. for (int i=0; i<30; i++) dReorthonormalize (R);
  171. dMultiply2 (I,R,R,3,3,3);
  172. printf ("\t%s\n",cmpIdentityMat3 (I) ? "passed" : "FAILED");
  173. }
  174. */
  175. void testPlaneSpace()
  176. {
  177. HEADER;
  178. dVector3 n,p,q;
  179. int bad = 0;
  180. for (int i=0; i<1000; i++) {
  181. dMakeRandomVector (n,3,1.0);
  182. dNormalize3 (n);
  183. dPlaneSpace (n,p,q);
  184. if (fabs(dDOT(n,p)) > tol) bad = 1;
  185. if (fabs(dDOT(n,q)) > tol) bad = 1;
  186. if (fabs(dDOT(p,q)) > tol) bad = 1;
  187. if (fabs(dDOT(p,p)-1) > tol) bad = 1;
  188. if (fabs(dDOT(q,q)-1) > tol) bad = 1;
  189. }
  190. printf ("\t%s\n", bad ? "FAILED" : "passed");
  191. }
  192. //****************************************************************************
  193. // test matrix functions
  194. #define MSIZE 21
  195. #define MSIZE4 24 // MSIZE rounded up to 4
  196. void testMatrixMultiply()
  197. {
  198. // A is 2x3, B is 3x4, B2 is B except stored columnwise, C is 2x4
  199. dReal A[8],B[12],A2[12],B2[16],C[8];
  200. int i;
  201. HEADER;
  202. dSetZero (A,8);
  203. for (i=0; i<3; i++) A[i] = i+2;
  204. for (i=0; i<3; i++) A[i+4] = i+3+2;
  205. for (i=0; i<12; i++) B[i] = i+8;
  206. dSetZero (A2,12);
  207. for (i=0; i<6; i++) A2[i+2*(i/2)] = A[i+i/3];
  208. dSetZero (B2,16);
  209. for (i=0; i<12; i++) B2[i+i/3] = B[i];
  210. dMultiply0 (C,A,B,2,3,4);
  211. if (C[0] != 116 || C[1] != 125 || C[2] != 134 || C[3] != 143 ||
  212. C[4] != 224 || C[5] != 242 || C[6] != 260 || C[7] != 278)
  213. printf ("\tFAILED (1)\n"); else printf ("\tpassed (1)\n");
  214. dMultiply1 (C,A2,B,2,3,4);
  215. if (C[0] != 160 || C[1] != 172 || C[2] != 184 || C[3] != 196 ||
  216. C[4] != 196 || C[5] != 211 || C[6] != 226 || C[7] != 241)
  217. printf ("\tFAILED (2)\n"); else printf ("\tpassed (2)\n");
  218. dMultiply2 (C,A,B2,2,3,4);
  219. if (C[0] != 83 || C[1] != 110 || C[2] != 137 || C[3] != 164 ||
  220. C[4] != 164 || C[5] != 218 || C[6] != 272 || C[7] != 326)
  221. printf ("\tFAILED (3)\n"); else printf ("\tpassed (3)\n");
  222. }
  223. void testSmallMatrixMultiply()
  224. {
  225. dMatrix3 A,B,C,A2;
  226. dVector3 a,a2,x;
  227. HEADER;
  228. dMakeRandomMatrix (A,3,3,1.0);
  229. dMakeRandomMatrix (B,3,3,1.0);
  230. dMakeRandomMatrix (C,3,3,1.0);
  231. dMakeRandomMatrix (x,3,1,1.0);
  232. // dMULTIPLY0_331()
  233. dMULTIPLY0_331 (a,B,x);
  234. dMultiply0 (a2,B,x,3,3,1);
  235. printf ("\t%s (1)\n",(dMaxDifference (a,a2,3,1) > tol) ? "FAILED" :
  236. "passed");
  237. // dMULTIPLY1_331()
  238. dMULTIPLY1_331 (a,B,x);
  239. dMultiply1 (a2,B,x,3,3,1);
  240. printf ("\t%s (2)\n",(dMaxDifference (a,a2,3,1) > tol) ? "FAILED" :
  241. "passed");
  242. // dMULTIPLY0_133
  243. dMULTIPLY0_133 (a,x,B);
  244. dMultiply0 (a2,x,B,1,3,3);
  245. printf ("\t%s (3)\n",(dMaxDifference (a,a2,1,3) > tol) ? "FAILED" :
  246. "passed");
  247. // dMULTIPLY0_333()
  248. dMULTIPLY0_333 (A,B,C);
  249. dMultiply0 (A2,B,C,3,3,3);
  250. printf ("\t%s (4)\n",(dMaxDifference (A,A2,3,3) > tol) ? "FAILED" :
  251. "passed");
  252. // dMULTIPLY1_333()
  253. dMULTIPLY1_333 (A,B,C);
  254. dMultiply1 (A2,B,C,3,3,3);
  255. printf ("\t%s (5)\n",(dMaxDifference (A,A2,3,3) > tol) ? "FAILED" :
  256. "passed");
  257. // dMULTIPLY2_333()
  258. dMULTIPLY2_333 (A,B,C);
  259. dMultiply2 (A2,B,C,3,3,3);
  260. printf ("\t%s (6)\n",(dMaxDifference (A,A2,3,3) > tol) ? "FAILED" :
  261. "passed");
  262. }
  263. void testCholeskyFactorization()
  264. {
  265. dReal A[MSIZE4*MSIZE], B[MSIZE4*MSIZE], C[MSIZE4*MSIZE], diff;
  266. HEADER;
  267. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  268. dMultiply2 (B,A,A,MSIZE,MSIZE,MSIZE);
  269. memcpy (A,B,MSIZE4*MSIZE*sizeof(dReal));
  270. if (dFactorCholesky (B,MSIZE)) printf ("\tpassed (1)\n");
  271. else printf ("\tFAILED (1)\n");
  272. dClearUpperTriangle (B,MSIZE);
  273. dMultiply2 (C,B,B,MSIZE,MSIZE,MSIZE);
  274. diff = dMaxDifference(A,C,MSIZE,MSIZE);
  275. printf ("\tmaximum difference = %.6e - %s (2)\n",diff,
  276. diff > tol ? "FAILED" : "passed");
  277. }
  278. void testCholeskySolve()
  279. {
  280. dReal A[MSIZE4*MSIZE], L[MSIZE4*MSIZE], b[MSIZE],x[MSIZE],btest[MSIZE],diff;
  281. HEADER;
  282. // get A,L = PD matrix
  283. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  284. dMultiply2 (L,A,A,MSIZE,MSIZE,MSIZE);
  285. memcpy (A,L,MSIZE4*MSIZE*sizeof(dReal));
  286. // get b,x = right hand side
  287. dMakeRandomMatrix (b,MSIZE,1,1.0);
  288. memcpy (x,b,MSIZE*sizeof(dReal));
  289. // factor L
  290. if (dFactorCholesky (L,MSIZE)) printf ("\tpassed (1)\n");
  291. else printf ("\tFAILED (1)\n");
  292. dClearUpperTriangle (L,MSIZE);
  293. // solve A*x = b
  294. dSolveCholesky (L,x,MSIZE);
  295. // compute A*x and compare it with b
  296. dMultiply2 (btest,A,x,MSIZE,MSIZE,1);
  297. diff = dMaxDifference(b,btest,MSIZE,1);
  298. printf ("\tmaximum difference = %.6e - %s (2)\n",diff,
  299. diff > tol ? "FAILED" : "passed");
  300. }
  301. void testInvertPDMatrix()
  302. {
  303. int i,j,ok;
  304. dReal A[MSIZE4*MSIZE], Ainv[MSIZE4*MSIZE], I[MSIZE4*MSIZE];
  305. HEADER;
  306. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  307. dMultiply2 (Ainv,A,A,MSIZE,MSIZE,MSIZE);
  308. memcpy (A,Ainv,MSIZE4*MSIZE*sizeof(dReal));
  309. dSetZero (Ainv,MSIZE4*MSIZE);
  310. if (dInvertPDMatrix (A,Ainv,MSIZE))
  311. printf ("\tpassed (1)\n"); else printf ("\tFAILED (1)\n");
  312. dMultiply0 (I,A,Ainv,MSIZE,MSIZE,MSIZE);
  313. // compare with identity
  314. ok = 1;
  315. for (i=0; i<MSIZE; i++) {
  316. for (j=0; j<MSIZE; j++) {
  317. if (i != j) if (cmp (I[i*MSIZE4+j],0.0)==0) ok = 0;
  318. }
  319. }
  320. for (i=0; i<MSIZE; i++) {
  321. if (cmp (I[i*MSIZE4+i],1.0)==0) ok = 0;
  322. }
  323. if (ok) printf ("\tpassed (2)\n"); else printf ("\tFAILED (2)\n");
  324. }
  325. void testIsPositiveDefinite()
  326. {
  327. dReal A[MSIZE4*MSIZE], B[MSIZE4*MSIZE];
  328. HEADER;
  329. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  330. dMultiply2 (B,A,A,MSIZE,MSIZE,MSIZE);
  331. printf ("\t%s\n",dIsPositiveDefinite(A,MSIZE) ? "FAILED (1)":"passed (1)");
  332. printf ("\t%s\n",dIsPositiveDefinite(B,MSIZE) ? "passed (2)":"FAILED (2)");
  333. }
  334. void testFastLDLTFactorization()
  335. {
  336. int i,j;
  337. dReal A[MSIZE4*MSIZE], L[MSIZE4*MSIZE], DL[MSIZE4*MSIZE],
  338. ATEST[MSIZE4*MSIZE], d[MSIZE], diff;
  339. HEADER;
  340. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  341. dMultiply2 (L,A,A,MSIZE,MSIZE,MSIZE);
  342. memcpy (A,L,MSIZE4*MSIZE*sizeof(dReal));
  343. dFactorLDLT (L,d,MSIZE,MSIZE4);
  344. dClearUpperTriangle (L,MSIZE);
  345. for (i=0; i<MSIZE; i++) L[i*MSIZE4+i] = 1.0;
  346. dSetZero (DL,MSIZE4*MSIZE);
  347. for (i=0; i<MSIZE; i++) {
  348. for (j=0; j<MSIZE; j++) DL[i*MSIZE4+j] = L[i*MSIZE4+j] / d[j];
  349. }
  350. dMultiply2 (ATEST,L,DL,MSIZE,MSIZE,MSIZE);
  351. diff = dMaxDifference(A,ATEST,MSIZE,MSIZE);
  352. printf ("\tmaximum difference = %.6e - %s\n",diff,
  353. diff > tol ? "FAILED" : "passed");
  354. }
  355. void testSolveLDLT()
  356. {
  357. dReal A[MSIZE4*MSIZE], L[MSIZE4*MSIZE], d[MSIZE], x[MSIZE],
  358. b[MSIZE], btest[MSIZE], diff;
  359. HEADER;
  360. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  361. dMultiply2 (L,A,A,MSIZE,MSIZE,MSIZE);
  362. memcpy (A,L,MSIZE4*MSIZE*sizeof(dReal));
  363. dMakeRandomMatrix (b,MSIZE,1,1.0);
  364. memcpy (x,b,MSIZE*sizeof(dReal));
  365. dFactorLDLT (L,d,MSIZE,MSIZE4);
  366. dSolveLDLT (L,d,x,MSIZE,MSIZE4);
  367. dMultiply2 (btest,A,x,MSIZE,MSIZE,1);
  368. diff = dMaxDifference(b,btest,MSIZE,1);
  369. printf ("\tmaximum difference = %.6e - %s\n",diff,
  370. diff > tol ? "FAILED" : "passed");
  371. }
  372. void testLDLTAddTL()
  373. {
  374. int i,j;
  375. dReal A[MSIZE4*MSIZE], L[MSIZE4*MSIZE], d[MSIZE], a[MSIZE],
  376. DL[MSIZE4*MSIZE], ATEST[MSIZE4*MSIZE], diff;
  377. HEADER;
  378. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  379. dMultiply2 (L,A,A,MSIZE,MSIZE,MSIZE);
  380. memcpy (A,L,MSIZE4*MSIZE*sizeof(dReal));
  381. dFactorLDLT (L,d,MSIZE,MSIZE4);
  382. // delete first row and column of factorization
  383. for (i=0; i<MSIZE; i++) a[i] = -A[i*MSIZE4];
  384. a[0] += 1;
  385. dLDLTAddTL (L,d,a,MSIZE,MSIZE4);
  386. for (i=1; i<MSIZE; i++) L[i*MSIZE4] = 0;
  387. d[0] = 1;
  388. // get modified L*D*L'
  389. dClearUpperTriangle (L,MSIZE);
  390. for (i=0; i<MSIZE; i++) L[i*MSIZE4+i] = 1.0;
  391. dSetZero (DL,MSIZE4*MSIZE);
  392. for (i=0; i<MSIZE; i++) {
  393. for (j=0; j<MSIZE; j++) DL[i*MSIZE4+j] = L[i*MSIZE4+j] / d[j];
  394. }
  395. dMultiply2 (ATEST,L,DL,MSIZE,MSIZE,MSIZE);
  396. // compare it to A with its first row/column removed
  397. for (i=1; i<MSIZE; i++) A[i*MSIZE4] = A[i] = 0;
  398. A[0] = 1;
  399. diff = dMaxDifference(A,ATEST,MSIZE,MSIZE);
  400. printf ("\tmaximum difference = %.6e - %s\n",diff,
  401. diff > tol ? "FAILED" : "passed");
  402. }
  403. void testLDLTRemove()
  404. {
  405. int i,j,r,p[MSIZE];
  406. dReal A[MSIZE4*MSIZE], L[MSIZE4*MSIZE], d[MSIZE],
  407. L2[MSIZE4*MSIZE], d2[MSIZE], DL2[MSIZE4*MSIZE],
  408. Atest1[MSIZE4*MSIZE], Atest2[MSIZE4*MSIZE], diff, maxdiff;
  409. HEADER;
  410. // make array of A row pointers
  411. dReal *Arows[MSIZE];
  412. for (i=0; i<MSIZE; i++) Arows[i] = A+i*MSIZE4;
  413. // fill permutation vector
  414. for (i=0; i<MSIZE; i++) p[i]=i;
  415. dMakeRandomMatrix (A,MSIZE,MSIZE,1.0);
  416. dMultiply2 (L,A,A,MSIZE,MSIZE,MSIZE);
  417. memcpy (A,L,MSIZE4*MSIZE*sizeof(dReal));
  418. dFactorLDLT (L,d,MSIZE,MSIZE4);
  419. maxdiff = 1e10;
  420. for (r=0; r<MSIZE; r++) {
  421. // get Atest1 = A with row/column r removed
  422. memcpy (Atest1,A,MSIZE4*MSIZE*sizeof(dReal));
  423. dRemoveRowCol (Atest1,MSIZE,MSIZE4,r);
  424. // test that the row/column removal worked
  425. int bad = 0;
  426. for (i=0; i<MSIZE; i++) {
  427. for (j=0; j<MSIZE; j++) {
  428. if (i != r && j != r) {
  429. int ii = i;
  430. int jj = j;
  431. if (ii >= r) ii--;
  432. if (jj >= r) jj--;
  433. if (A[i*MSIZE4+j] != Atest1[ii*MSIZE4+jj]) bad = 1;
  434. }
  435. }
  436. }
  437. if (bad) printf ("\trow/col removal FAILED for row %d\n",r);
  438. // zero out last row/column of Atest1
  439. for (i=0; i<MSIZE; i++) {
  440. Atest1[(MSIZE-1)*MSIZE4+i] = 0;
  441. Atest1[i*MSIZE4+MSIZE-1] = 0;
  442. }
  443. // get L2*D2*L2' = adjusted factorization to remove that row
  444. memcpy (L2,L,MSIZE4*MSIZE*sizeof(dReal));
  445. memcpy (d2,d,MSIZE*sizeof(dReal));
  446. dLDLTRemove (/*A*/ Arows,p,L2,d2,MSIZE,MSIZE,r,MSIZE4);
  447. // get Atest2 = L2*D2*L2'
  448. dClearUpperTriangle (L2,MSIZE);
  449. for (i=0; i<(MSIZE-1); i++) L2[i*MSIZE4+i] = 1.0;
  450. for (i=0; i<MSIZE; i++) L2[(MSIZE-1)*MSIZE4+i] = 0;
  451. d2[MSIZE-1] = 1;
  452. dSetZero (DL2,MSIZE4*MSIZE);
  453. for (i=0; i<(MSIZE-1); i++) {
  454. for (j=0; j<MSIZE-1; j++) DL2[i*MSIZE4+j] = L2[i*MSIZE4+j] / d2[j];
  455. }
  456. dMultiply2 (Atest2,L2,DL2,MSIZE,MSIZE,MSIZE);
  457. diff = dMaxDifference(Atest1,Atest2,MSIZE,MSIZE);
  458. if (diff < maxdiff) maxdiff = diff;
  459. /*
  460. dPrintMatrix (Atest1,MSIZE,MSIZE);
  461. printf ("\n");
  462. dPrintMatrix (Atest2,MSIZE,MSIZE);
  463. printf ("\n");
  464. */
  465. }
  466. printf ("\tmaximum difference = %.6e - %s\n",maxdiff,
  467. maxdiff > tol ? "FAILED" : "passed");
  468. }
  469. //****************************************************************************
  470. // test mass stuff
  471. #define NUMP 10 // number of particles
  472. void printMassParams (dMass *m)
  473. {
  474. printf ("mass = %.4f\n",m->mass);
  475. printf ("com = (%.4f,%.4f,%.4f)\n",m->c[0],m->c[1],m->c[2]);
  476. printf ("I = [ %10.4f %10.4f %10.4f ]\n"
  477. " [ %10.4f %10.4f %10.4f ]\n"
  478. " [ %10.4f %10.4f %10.4f ]\n",
  479. m->_I(0,0),m->_I(0,1),m->_I(0,2),
  480. m->_I(1,0),m->_I(1,1),m->_I(1,2),
  481. m->_I(2,0),m->_I(2,1),m->_I(2,2));
  482. }
  483. void compareMassParams (dMass *m1, dMass *m2, const char *msg)
  484. {
  485. int i,j,ok = 1;
  486. if (!(cmp(m1->mass,m2->mass) && cmp(m1->c[0],m2->c[0]) &&
  487. cmp(m1->c[1],m2->c[1]) && cmp(m1->c[2],m2->c[2])))
  488. ok = 0;
  489. for (i=0; i<3; i++) for (j=0; j<3; j++)
  490. if (cmp (m1->_I(i,j),m2->_I(i,j))==0) ok = 0;
  491. if (ok) printf ("\tpassed (%s)\n",msg); else printf ("\tFAILED (%s)\n",msg);
  492. }
  493. // compute the mass parameters of a particle set
  494. void computeMassParams (dMass *m, dReal q[NUMP][3], dReal pm[NUMP])
  495. {
  496. int i,j;
  497. dMassSetZero (m);
  498. for (i=0; i<NUMP; i++) {
  499. m->mass += pm[i];
  500. for (j=0; j<3; j++) m->c[j] += pm[i]*q[i][j];
  501. m->_I(0,0) += pm[i]*(q[i][1]*q[i][1] + q[i][2]*q[i][2]);
  502. m->_I(1,1) += pm[i]*(q[i][0]*q[i][0] + q[i][2]*q[i][2]);
  503. m->_I(2,2) += pm[i]*(q[i][0]*q[i][0] + q[i][1]*q[i][1]);
  504. m->_I(0,1) -= pm[i]*(q[i][0]*q[i][1]);
  505. m->_I(0,2) -= pm[i]*(q[i][0]*q[i][2]);
  506. m->_I(1,2) -= pm[i]*(q[i][1]*q[i][2]);
  507. }
  508. for (j=0; j<3; j++) m->c[j] /= m->mass;
  509. m->_I(1,0) = m->_I(0,1);
  510. m->_I(2,0) = m->_I(0,2);
  511. m->_I(2,1) = m->_I(1,2);
  512. }
  513. void testMassFunctions()
  514. {
  515. dMass m;
  516. int i,j;
  517. dReal q[NUMP][3]; // particle positions
  518. dReal pm[NUMP]; // particle masses
  519. dMass m1,m2;
  520. dMatrix3 R;
  521. HEADER;
  522. printf ("\t");
  523. dMassSetZero (&m);
  524. TRAP_MESSAGE (dMassSetParameters (&m,10, 0,0,0, 1,2,3, 4,5,6),
  525. printf (" FAILED (1)\n"), printf (" passed (1)\n"));
  526. printf ("\t");
  527. dMassSetZero (&m);
  528. TRAP_MESSAGE (dMassSetParameters (&m,10, 0.1,0.2,0.15, 3,5,14, 3.1,3.2,4),
  529. printf ("passed (2)\n") , printf (" FAILED (2)\n"));
  530. if (m.mass==10 && m.c[0]==REAL(0.1) && m.c[1]==REAL(0.2) &&
  531. m.c[2]==REAL(0.15) && m._I(0,0)==3 && m._I(1,1)==5 && m._I(2,2)==14 &&
  532. m._I(0,1)==REAL(3.1) && m._I(0,2)==REAL(3.2) && m._I(1,2)==4 &&
  533. m._I(1,0)==REAL(3.1) && m._I(2,0)==REAL(3.2) && m._I(2,1)==4)
  534. printf ("\tpassed (3)\n"); else printf ("\tFAILED (3)\n");
  535. dMassSetZero (&m);
  536. dMassSetSphere (&m,1.4, 0.86);
  537. if (cmp(m.mass,3.73002719949386) && m.c[0]==0 && m.c[1]==0 && m.c[2]==0 &&
  538. cmp(m._I(0,0),1.10349124669826) &&
  539. cmp(m._I(1,1),1.10349124669826) &&
  540. cmp(m._I(2,2),1.10349124669826) &&
  541. m._I(0,1)==0 && m._I(0,2)==0 && m._I(1,2)==0 &&
  542. m._I(1,0)==0 && m._I(2,0)==0 && m._I(2,1)==0)
  543. printf ("\tpassed (4)\n"); else printf ("\tFAILED (4)\n");
  544. dMassSetZero (&m);
  545. dMassSetCapsule (&m,1.3,1,0.76,1.53);
  546. if (cmp(m.mass,5.99961928996029) && m.c[0]==0 && m.c[1]==0 && m.c[2]==0 &&
  547. cmp(m._I(0,0),1.59461986077384) &&
  548. cmp(m._I(1,1),4.21878433864904) &&
  549. cmp(m._I(2,2),4.21878433864904) &&
  550. m._I(0,1)==0 && m._I(0,2)==0 && m._I(1,2)==0 &&
  551. m._I(1,0)==0 && m._I(2,0)==0 && m._I(2,1)==0)
  552. printf ("\tpassed (5)\n"); else printf ("\tFAILED (5)\n");
  553. dMassSetZero (&m);
  554. dMassSetBox (&m,0.27,3,4,5);
  555. if (cmp(m.mass,16.2) && m.c[0]==0 && m.c[1]==0 && m.c[2]==0 &&
  556. cmp(m._I(0,0),55.35) && cmp(m._I(1,1),45.9) && cmp(m._I(2,2),33.75) &&
  557. m._I(0,1)==0 && m._I(0,2)==0 && m._I(1,2)==0 &&
  558. m._I(1,0)==0 && m._I(2,0)==0 && m._I(2,1)==0)
  559. printf ("\tpassed (6)\n"); else printf ("\tFAILED (6)\n");
  560. // test dMassAdjust?
  561. // make random particles and compute the mass, COM and inertia, then
  562. // translate and repeat.
  563. for (i=0; i<NUMP; i++) {
  564. pm[i] = dRandReal()+0.5;
  565. for (j=0; j<3; j++) {
  566. q[i][j] = 2.0*(dRandReal()-0.5);
  567. }
  568. }
  569. computeMassParams (&m1,q,pm);
  570. memcpy (&m2,&m1,sizeof(dMass));
  571. dMassTranslate (&m2,1,2,-3);
  572. for (i=0; i<NUMP; i++) {
  573. q[i][0] += 1;
  574. q[i][1] += 2;
  575. q[i][2] -= 3;
  576. }
  577. computeMassParams (&m1,q,pm);
  578. compareMassParams (&m1,&m2,"7");
  579. // rotate the masses
  580. _R(0,0) = -0.87919618797635;
  581. _R(0,1) = 0.15278881840384;
  582. _R(0,2) = -0.45129772879842;
  583. _R(1,0) = -0.47307856232664;
  584. _R(1,1) = -0.39258064912909;
  585. _R(1,2) = 0.78871864932708;
  586. _R(2,0) = -0.05666336483842;
  587. _R(2,1) = 0.90693771059546;
  588. _R(2,2) = 0.41743652473765;
  589. dMassRotate (&m2,R);
  590. for (i=0; i<NUMP; i++) {
  591. dReal a[3];
  592. dMultiply0 (a,&_R(0,0),&q[i][0],3,3,1);
  593. q[i][0] = a[0];
  594. q[i][1] = a[1];
  595. q[i][2] = a[2];
  596. }
  597. computeMassParams (&m1,q,pm);
  598. compareMassParams (&m1,&m2,"8");
  599. }
  600. //****************************************************************************
  601. // test rotation stuff
  602. void makeRandomRotation (dMatrix3 R)
  603. {
  604. dReal *u1 = R, *u2=R+4, *u3=R+8;
  605. dMakeRandomVector (u1,3,1.0);
  606. dNormalize3 (u1);
  607. dMakeRandomVector (u2,3,1.0);
  608. dReal d = dDOT(u1,u2);
  609. u2[0] -= d*u1[0];
  610. u2[1] -= d*u1[1];
  611. u2[2] -= d*u1[2];
  612. dNormalize3 (u2);
  613. dCROSS (u3,=,u1,u2);
  614. }
  615. void testRtoQandQtoR()
  616. {
  617. HEADER;
  618. dMatrix3 R,I,R2;
  619. dQuaternion q;
  620. int i;
  621. // test makeRandomRotation()
  622. makeRandomRotation (R);
  623. dMultiply2 (I,R,R,3,3,3);
  624. printf ("\tmakeRandomRotation() - %s (1)\n",
  625. cmpIdentityMat3(I) ? "passed" : "FAILED");
  626. // test QtoR() on random normalized quaternions
  627. int ok = 1;
  628. for (i=0; i<100; i++) {
  629. dMakeRandomVector (q,4,1.0);
  630. dNormalize4 (q);
  631. dQtoR (q,R);
  632. dMultiply2 (I,R,R,3,3,3);
  633. if (cmpIdentityMat3(I)==0) ok = 0;
  634. }
  635. printf ("\tQtoR() orthonormality %s (2)\n", ok ? "passed" : "FAILED");
  636. // test R -> Q -> R works
  637. dReal maxdiff=0;
  638. for (i=0; i<100; i++) {
  639. makeRandomRotation (R);
  640. dRtoQ (R,q);
  641. dQtoR (q,R2);
  642. dReal diff = dMaxDifference (R,R2,3,3);
  643. if (diff > maxdiff) maxdiff = diff;
  644. }
  645. printf ("\tmaximum difference = %e - %s (3)\n",maxdiff,
  646. (maxdiff > tol) ? "FAILED" : "passed");
  647. }
  648. void testQuaternionMultiply()
  649. {
  650. HEADER;
  651. dMatrix3 RA,RB,RC,Rtest;
  652. dQuaternion qa,qb,qc;
  653. dReal diff,maxdiff=0;
  654. for (int i=0; i<100; i++) {
  655. makeRandomRotation (RB);
  656. makeRandomRotation (RC);
  657. dRtoQ (RB,qb);
  658. dRtoQ (RC,qc);
  659. dMultiply0 (RA,RB,RC,3,3,3);
  660. dQMultiply0 (qa,qb,qc);
  661. dQtoR (qa,Rtest);
  662. diff = dMaxDifference (Rtest,RA,3,3);
  663. if (diff > maxdiff) maxdiff = diff;
  664. dMultiply1 (RA,RB,RC,3,3,3);
  665. dQMultiply1 (qa,qb,qc);
  666. dQtoR (qa,Rtest);
  667. diff = dMaxDifference (Rtest,RA,3,3);
  668. if (diff > maxdiff) maxdiff = diff;
  669. dMultiply2 (RA,RB,RC,3,3,3);
  670. dQMultiply2 (qa,qb,qc);
  671. dQtoR (qa,Rtest);
  672. diff = dMaxDifference (Rtest,RA,3,3);
  673. if (diff > maxdiff) maxdiff = diff;
  674. dMultiply0 (RA,RC,RB,3,3,3);
  675. transpose3x3 (RA);
  676. dQMultiply3 (qa,qb,qc);
  677. dQtoR (qa,Rtest);
  678. diff = dMaxDifference (Rtest,RA,3,3);
  679. if (diff > maxdiff) maxdiff = diff;
  680. }
  681. printf ("\tmaximum difference = %e - %s\n",maxdiff,
  682. (maxdiff > tol) ? "FAILED" : "passed");
  683. }
  684. void testRotationFunctions()
  685. {
  686. dMatrix3 R1;
  687. HEADER;
  688. printf ("\tdRSetIdentity - ");
  689. dMakeRandomMatrix (R1,3,3,1.0);
  690. dRSetIdentity (R1);
  691. if (cmpIdentityMat3(R1)) printf ("passed\n"); else printf ("FAILED\n");
  692. printf ("\tdRFromAxisAndAngle - ");
  693. printf ("\n");
  694. printf ("\tdRFromEulerAngles - ");
  695. printf ("\n");
  696. printf ("\tdRFrom2Axes - ");
  697. printf ("\n");
  698. }
  699. //****************************************************************************
  700. #include "../src/array.h"
  701. #include "../src/array.cpp"
  702. // matrix header on the stack
  703. class dMatrixComparison {
  704. struct dMatInfo;
  705. dArray<dMatInfo*> mat;
  706. int afterfirst,index;
  707. public:
  708. dMatrixComparison();
  709. ~dMatrixComparison();
  710. dReal nextMatrix (dReal *A, int n, int m, int lower_tri, const char *name, ...);
  711. // add a new n*m matrix A to the sequence. the name of the matrix is given
  712. // by the printf-style arguments (name,...). if this is the first sequence
  713. // then this object will simply record the matrices and return 0.
  714. // if this the second or subsequent sequence then this object will compare
  715. // the matrices with the first sequence, and report any differences.
  716. // the matrix error will be returned. if `lower_tri' is 1 then only the
  717. // lower triangle of the matrix (including the diagonal) will be compared
  718. // (the matrix must be square).
  719. void end();
  720. // end a sequence.
  721. void reset();
  722. // restarts the object, so the next sequence will be the first sequence.
  723. void dump();
  724. // print out info about all the matrices in the sequence
  725. };
  726. struct dMatrixComparison::dMatInfo {
  727. int n,m; // size of matrix
  728. char name[128]; // name of the matrix
  729. dReal *data; // matrix data
  730. int size; // size of `data'
  731. };
  732. dMatrixComparison::dMatrixComparison()
  733. {
  734. afterfirst = 0;
  735. index = 0;
  736. }
  737. dMatrixComparison::~dMatrixComparison()
  738. {
  739. reset();
  740. }
  741. dReal dMatrixComparison::nextMatrix (dReal *A, int n, int m, int lower_tri,
  742. const char *name, ...)
  743. {
  744. if (A==0 || n < 1 || m < 1 || name==0) dDebug (0,"bad args to nextMatrix");
  745. int num = n*dPAD(m);
  746. if (afterfirst==0) {
  747. dMatInfo *mi = (dMatInfo*) dAlloc (sizeof(dMatInfo));
  748. mi->n = n;
  749. mi->m = m;
  750. mi->size = num * sizeof(dReal);
  751. mi->data = (dReal*) dAlloc (mi->size);
  752. memcpy (mi->data,A,mi->size);
  753. va_list ap;
  754. va_start (ap,name);
  755. vsprintf (mi->name,name,ap);
  756. if (strlen(mi->name) >= sizeof (mi->name)) dDebug (0,"name too long");
  757. mat.push (mi);
  758. return 0;
  759. }
  760. else {
  761. if (lower_tri && n != m)
  762. dDebug (0,"dMatrixComparison, lower triangular matrix must be square");
  763. if (index >= mat.size()) dDebug (0,"dMatrixComparison, too many matrices");
  764. dMatInfo *mp = mat[index];
  765. index++;
  766. dMatInfo mi;
  767. va_list ap;
  768. va_start (ap,name);
  769. vsprintf (mi.name,name,ap);
  770. if (strlen(mi.name) >= sizeof (mi.name)) dDebug (0,"name too long");
  771. if (strcmp(mp->name,mi.name) != 0)
  772. dDebug (0,"dMatrixComparison, name mismatch (\"%s\" and \"%s\")",
  773. mp->name,mi.name);
  774. if (mp->n != n || mp->m != m)
  775. dDebug (0,"dMatrixComparison, size mismatch (%dx%d and %dx%d)",
  776. mp->n,mp->m,n,m);
  777. dReal maxdiff;
  778. if (lower_tri) {
  779. maxdiff = dMaxDifferenceLowerTriangle (A,mp->data,n);
  780. }
  781. else {
  782. maxdiff = dMaxDifference (A,mp->data,n,m);
  783. }
  784. if (maxdiff > tol)
  785. dDebug (0,"dMatrixComparison, matrix error (size=%dx%d, name=\"%s\", "
  786. "error=%.4e)",n,m,mi.name,maxdiff);
  787. return maxdiff;
  788. }
  789. }
  790. void dMatrixComparison::end()
  791. {
  792. if (mat.size() <= 0) dDebug (0,"no matrices in sequence");
  793. afterfirst = 1;
  794. index = 0;
  795. }
  796. void dMatrixComparison::reset()
  797. {
  798. for (int i=0; i<mat.size(); i++) {
  799. dFree (mat[i]->data,mat[i]->size);
  800. dFree (mat[i],sizeof(dMatInfo));
  801. }
  802. mat.setSize (0);
  803. afterfirst = 0;
  804. index = 0;
  805. }
  806. void dMatrixComparison::dump()
  807. {
  808. for (int i=0; i<mat.size(); i++)
  809. printf ("%d: %s (%dx%d)\n",i,mat[i]->name,mat[i]->n,mat[i]->m);
  810. }
  811. //****************************************************************************
  812. // unit test
  813. #include <setjmp.h>
  814. // static jmp_buf jump_buffer;
  815. static void myDebug (int num, const char *msg, va_list ap)
  816. {
  817. // printf ("(Error %d: ",num);
  818. // vprintf (msg,ap);
  819. // printf (")\n");
  820. longjmp (jump_buffer,1);
  821. }
  822. extern "C" void dTestMatrixComparison()
  823. {
  824. volatile int i;
  825. printf ("dTestMatrixComparison()\n");
  826. dMessageFunction *orig_debug = dGetDebugHandler();
  827. dMatrixComparison mc;
  828. dReal A[50*50];
  829. // make first sequence
  830. unsigned long seed = dRandGetSeed();
  831. for (i=1; i<49; i++) {
  832. dMakeRandomMatrix (A,i,i+1,1.0);
  833. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  834. }
  835. mc.end();
  836. //mc.dump();
  837. // test identical sequence
  838. dSetDebugHandler (&myDebug);
  839. dRandSetSeed (seed);
  840. if (setjmp (jump_buffer)) {
  841. printf ("\tFAILED (1)\n");
  842. }
  843. else {
  844. for (i=1; i<49; i++) {
  845. dMakeRandomMatrix (A,i,i+1,1.0);
  846. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  847. }
  848. mc.end();
  849. printf ("\tpassed (1)\n");
  850. }
  851. dSetDebugHandler (orig_debug);
  852. // test broken sequences (with matrix error)
  853. dRandSetSeed (seed);
  854. volatile int passcount = 0;
  855. for (i=1; i<49; i++) {
  856. if (setjmp (jump_buffer)) {
  857. passcount++;
  858. }
  859. else {
  860. dSetDebugHandler (&myDebug);
  861. dMakeRandomMatrix (A,i,i+1,1.0);
  862. A[(i-1)*dPAD(i+1)+i] += REAL(0.01);
  863. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  864. dSetDebugHandler (orig_debug);
  865. }
  866. }
  867. mc.end();
  868. printf ("\t%s (2)\n",(passcount == 48) ? "passed" : "FAILED");
  869. // test broken sequences (with name error)
  870. dRandSetSeed (seed);
  871. passcount = 0;
  872. for (i=1; i<49; i++) {
  873. if (setjmp (jump_buffer)) {
  874. passcount++;
  875. }
  876. else {
  877. dSetDebugHandler (&myDebug);
  878. dMakeRandomMatrix (A,i,i+1,1.0);
  879. mc.nextMatrix (A,i,i+1,0,"B%d",i);
  880. dSetDebugHandler (orig_debug);
  881. }
  882. }
  883. mc.end();
  884. printf ("\t%s (3)\n",(passcount == 48) ? "passed" : "FAILED");
  885. // test identical sequence again
  886. dSetDebugHandler (&myDebug);
  887. dRandSetSeed (seed);
  888. if (setjmp (jump_buffer)) {
  889. printf ("\tFAILED (4)\n");
  890. }
  891. else {
  892. for (i=1; i<49; i++) {
  893. dMakeRandomMatrix (A,i,i+1,1.0);
  894. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  895. }
  896. mc.end();
  897. printf ("\tpassed (4)\n");
  898. }
  899. dSetDebugHandler (orig_debug);
  900. }
  901. //****************************************************************************
  902. // internal unit tests
  903. extern "C" void dTestDataStructures();
  904. extern "C" void dTestMatrixComparison();
  905. extern "C" void dTestSolveLCP();
  906. int main()
  907. {
  908. dInitODE();
  909. testRandomNumberGenerator();
  910. testInfinity();
  911. testPad();
  912. testCrossProduct();
  913. testSetZero();
  914. testNormalize3();
  915. //testReorthonormalize(); ... not any more
  916. testPlaneSpace();
  917. testMatrixMultiply();
  918. testSmallMatrixMultiply();
  919. testCholeskyFactorization();
  920. testCholeskySolve();
  921. testInvertPDMatrix();
  922. testIsPositiveDefinite();
  923. testFastLDLTFactorization();
  924. testSolveLDLT();
  925. testLDLTAddTL();
  926. testLDLTRemove();
  927. testMassFunctions();
  928. testRtoQandQtoR();
  929. testQuaternionMultiply();
  930. testRotationFunctions();
  931. dTestMatrixComparison();
  932. dTestSolveLCP();
  933. // dTestDataStructures();
  934. dCloseODE();
  935. return 0;
  936. }