source:
issm/oecreview/Archive/20545-21336/ISSM-20728-20729.diff@
21337
Last change on this file since 21337 was 21337, checked in by , 8 years ago | |
---|---|
File size: 4.4 KB |
-
../trunk-jpl/src/c/shared/Matrix/MatrixUtils.cpp
21 21 /*TripleMultiply Perform triple matrix product a*b*c+d.*/ 22 22 23 23 int idima,idimb,idimc,idimd; 24 IssmDouble dtemp_static[600]; 24 IssmDouble dtemp_static[600]; 25 IssmDouble* dtemp_dynamic = NULL; 26 IssmDouble* dtemp = NULL; 25 27 _assert_(a && b && c && d); 26 28 27 29 /* set up dimensions for triple product */ … … 51 53 if (ncolc != idimc) _error_("Matrix B and C inner vectors not equal size."); 52 54 idimd=nrowc; 53 55 } 56 57 /*Depending on the size of incoming matrices, we might need to use a dynamic allocation*/ 58 if(idima*idimc>600){ 59 IssmDouble *dtemp_dynamic=xNew<IssmDouble>(idima*idimc); 60 dtemp = dtemp_dynamic; 61 } 62 else{ 63 dtemp = &dtemp_static[0]; 64 } 54 65 55 66 /* perform the matrix triple product in the order that minimizes the 56 67 number of multiplies and the temporary space used, noting that … … 60 71 61 72 /* multiply (a*b)*c+d */ 62 73 if (idima*idimc*(idimb+idimd) <= idimb*idimd*(idima+idimc)) { 63 if(idima*idimc>600) { 64 /*Use dynamic allocation instead of dtemp_static*/ 65 IssmDouble *dtemp; 66 dtemp=xNew<IssmDouble>(idima*idimc); 67 68 MatrixMultiply(a,nrowa,ncola,itrna,b,nrowb,ncolb,itrnb,dtemp,0); 69 MatrixMultiply(dtemp,idima,idimc,0,c,nrowc,ncolc,itrnc,d,iaddd); 70 xDelete<IssmDouble>(dtemp); 71 return 1; 72 } 73 MatrixMultiply(a,nrowa,ncola,itrna,b,nrowb,ncolb,itrnb,&dtemp_static[0],0); 74 MatrixMultiply(&dtemp_static[0],idima,idimc,0,c,nrowc,ncolc,itrnc,d,iaddd); 74 MatrixMultiply(a,nrowa,ncola,itrna,b,nrowb,ncolb,itrnb,dtemp,0); 75 MatrixMultiply(dtemp,idima,idimc,0,c,nrowc,ncolc,itrnc,d,iaddd); 75 76 } 76 77 77 78 /* multiply a*(b*c)+d */ 78 79 else{ 79 if(idimb*idimd>600) { 80 /*Use dynamic allocation instead of dtemp_static*/ 81 IssmDouble *dtemp; 82 dtemp=xNew<IssmDouble>(idimb*idimd); 83 84 MatrixMultiply(b,nrowb,ncolb,itrnb,c,nrowc,ncolc,itrnc,dtemp,0); 85 MatrixMultiply(a,nrowa,ncola,itrna,dtemp,idimb,idimd,0,d,iaddd); 86 xDelete<IssmDouble>(dtemp); 87 return 1; 88 } 89 MatrixMultiply(b,nrowb,ncolb,itrnb,c,nrowc,ncolc,itrnc,&dtemp_static[0],0); 90 MatrixMultiply(a,nrowa,ncola,itrna,&dtemp_static[0],idimb,idimd,0,d,iaddd); 80 MatrixMultiply(b,nrowb,ncolb,itrnb,c,nrowc,ncolc,itrnc,dtemp,0); 81 MatrixMultiply(a,nrowa,ncola,itrna,dtemp,idimb,idimd,0,d,iaddd); 91 82 } 92 83 84 /*Cleanup and return*/ 85 xDelete<IssmDouble>(dtemp); 93 86 return 1; 94 87 }/*}}}*/ 95 88 int MatrixMultiply(IssmDouble* a, int nrowa, int ncola, int itrna, IssmDouble* b, int nrowb, int ncolb, int itrnb, IssmDouble* c, int iaddc ){/*{{{*/ … … 336 329 void Matrix2x2Invert(IssmDouble* Ainv,IssmDouble* A){/*{{{*/ 337 330 338 331 /*Intermediaries*/ 339 IssmDouble det; 340 IssmDouble det_reciprocal; 332 IssmDouble det,det_reciprocal; 341 333 342 334 /*Compute determinant*/ 343 335 Matrix2x2Determinant(&det,A); 344 336 if (fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon"); 345 337 346 338 /*Multiplication is faster than divsion, so we multiply by the reciprocal*/ 347 det_reciprocal = 1 /det;339 det_reciprocal = 1./det; 348 340 349 341 /*Compute invert*/ 350 342 Ainv[0]= A[3]*det_reciprocal; /* = d/det */ … … 457 449 void Matrix3x3Invert(IssmDouble* Ainv,IssmDouble* A){/*{{{*/ 458 450 459 451 /*Intermediaries*/ 460 IssmDouble det; 461 IssmDouble det_reciprocal; 452 IssmDouble det,det_reciprocal; 462 453 463 454 /*Compute determinant*/ 464 455 Matrix3x3Determinant(&det,A); 465 456 if (fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon"); 466 457 467 458 /*Multiplication is faster than divsion, so we multiply by the reciprocal*/ 468 det_reciprocal = 1 /det;459 det_reciprocal = 1./det; 469 460 470 461 /*Compute invert*/ 471 462 Ainv[0]=(A[4]*A[8]-A[5]*A[7])*det_reciprocal; /* = (e*i-f*h)/det */ … … 562 553 void Matrix4x4Invert(IssmDouble* Ainv,IssmDouble* A){/*{{{*/ 563 554 564 555 /*Intermediaries*/ 565 IssmDouble det; 566 IssmDouble det_reciprocal; 556 IssmDouble det,det_reciprocal; 567 557 568 558 /*Compute determinant*/ 569 559 Matrix4x4Determinant(&det,A); 570 //if(fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon");560 if(fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon"); 571 561 572 562 /*Multiplication is faster than division, so we multiply by the reciprocal*/ 573 det_reciprocal = 1 /det;563 det_reciprocal = 1./det; 574 564 575 565 /*Compute adjoint matrix*/ 576 566 Matrix4x4Adjoint(Ainv,A);
Note:
See TracBrowser
for help on using the repository browser.