[21337] | 1 | Index: ../trunk-jpl/src/c/shared/Matrix/MatrixUtils.cpp
|
---|
| 2 | ===================================================================
|
---|
| 3 | --- ../trunk-jpl/src/c/shared/Matrix/MatrixUtils.cpp (revision 20728)
|
---|
| 4 | +++ ../trunk-jpl/src/c/shared/Matrix/MatrixUtils.cpp (revision 20729)
|
---|
| 5 | @@ -21,7 +21,9 @@
|
---|
| 6 | /*TripleMultiply Perform triple matrix product a*b*c+d.*/
|
---|
| 7 |
|
---|
| 8 | int idima,idimb,idimc,idimd;
|
---|
| 9 | - IssmDouble dtemp_static[600];
|
---|
| 10 | + IssmDouble dtemp_static[600];
|
---|
| 11 | + IssmDouble* dtemp_dynamic = NULL;
|
---|
| 12 | + IssmDouble* dtemp = NULL;
|
---|
| 13 | _assert_(a && b && c && d);
|
---|
| 14 |
|
---|
| 15 | /* set up dimensions for triple product */
|
---|
| 16 | @@ -51,6 +53,15 @@
|
---|
| 17 | if (ncolc != idimc) _error_("Matrix B and C inner vectors not equal size.");
|
---|
| 18 | idimd=nrowc;
|
---|
| 19 | }
|
---|
| 20 | +
|
---|
| 21 | + /*Depending on the size of incoming matrices, we might need to use a dynamic allocation*/
|
---|
| 22 | + if(idima*idimc>600){
|
---|
| 23 | + IssmDouble *dtemp_dynamic=xNew<IssmDouble>(idima*idimc);
|
---|
| 24 | + dtemp = dtemp_dynamic;
|
---|
| 25 | + }
|
---|
| 26 | + else{
|
---|
| 27 | + dtemp = &dtemp_static[0];
|
---|
| 28 | + }
|
---|
| 29 |
|
---|
| 30 | /* perform the matrix triple product in the order that minimizes the
|
---|
| 31 | number of multiplies and the temporary space used, noting that
|
---|
| 32 | @@ -60,36 +71,18 @@
|
---|
| 33 |
|
---|
| 34 | /* multiply (a*b)*c+d */
|
---|
| 35 | if (idima*idimc*(idimb+idimd) <= idimb*idimd*(idima+idimc)) {
|
---|
| 36 | - if(idima*idimc>600) {
|
---|
| 37 | - /*Use dynamic allocation instead of dtemp_static*/
|
---|
| 38 | - IssmDouble *dtemp;
|
---|
| 39 | - dtemp=xNew<IssmDouble>(idima*idimc);
|
---|
| 40 | -
|
---|
| 41 | - MatrixMultiply(a,nrowa,ncola,itrna,b,nrowb,ncolb,itrnb,dtemp,0);
|
---|
| 42 | - MatrixMultiply(dtemp,idima,idimc,0,c,nrowc,ncolc,itrnc,d,iaddd);
|
---|
| 43 | - xDelete<IssmDouble>(dtemp);
|
---|
| 44 | - return 1;
|
---|
| 45 | - }
|
---|
| 46 | - MatrixMultiply(a,nrowa,ncola,itrna,b,nrowb,ncolb,itrnb,&dtemp_static[0],0);
|
---|
| 47 | - MatrixMultiply(&dtemp_static[0],idima,idimc,0,c,nrowc,ncolc,itrnc,d,iaddd);
|
---|
| 48 | + MatrixMultiply(a,nrowa,ncola,itrna,b,nrowb,ncolb,itrnb,dtemp,0);
|
---|
| 49 | + MatrixMultiply(dtemp,idima,idimc,0,c,nrowc,ncolc,itrnc,d,iaddd);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /* multiply a*(b*c)+d */
|
---|
| 53 | else{
|
---|
| 54 | - if(idimb*idimd>600) {
|
---|
| 55 | - /*Use dynamic allocation instead of dtemp_static*/
|
---|
| 56 | - IssmDouble *dtemp;
|
---|
| 57 | - dtemp=xNew<IssmDouble>(idimb*idimd);
|
---|
| 58 | -
|
---|
| 59 | - MatrixMultiply(b,nrowb,ncolb,itrnb,c,nrowc,ncolc,itrnc,dtemp,0);
|
---|
| 60 | - MatrixMultiply(a,nrowa,ncola,itrna,dtemp,idimb,idimd,0,d,iaddd);
|
---|
| 61 | - xDelete<IssmDouble>(dtemp);
|
---|
| 62 | - return 1;
|
---|
| 63 | - }
|
---|
| 64 | - MatrixMultiply(b,nrowb,ncolb,itrnb,c,nrowc,ncolc,itrnc,&dtemp_static[0],0);
|
---|
| 65 | - MatrixMultiply(a,nrowa,ncola,itrna,&dtemp_static[0],idimb,idimd,0,d,iaddd);
|
---|
| 66 | + MatrixMultiply(b,nrowb,ncolb,itrnb,c,nrowc,ncolc,itrnc,dtemp,0);
|
---|
| 67 | + MatrixMultiply(a,nrowa,ncola,itrna,dtemp,idimb,idimd,0,d,iaddd);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | + /*Cleanup and return*/
|
---|
| 71 | + xDelete<IssmDouble>(dtemp);
|
---|
| 72 | return 1;
|
---|
| 73 | }/*}}}*/
|
---|
| 74 | int MatrixMultiply(IssmDouble* a, int nrowa, int ncola, int itrna, IssmDouble* b, int nrowb, int ncolb, int itrnb, IssmDouble* c, int iaddc ){/*{{{*/
|
---|
| 75 | @@ -336,15 +329,14 @@
|
---|
| 76 | void Matrix2x2Invert(IssmDouble* Ainv,IssmDouble* A){/*{{{*/
|
---|
| 77 |
|
---|
| 78 | /*Intermediaries*/
|
---|
| 79 | - IssmDouble det;
|
---|
| 80 | - IssmDouble det_reciprocal;
|
---|
| 81 | + IssmDouble det,det_reciprocal;
|
---|
| 82 |
|
---|
| 83 | /*Compute determinant*/
|
---|
| 84 | Matrix2x2Determinant(&det,A);
|
---|
| 85 | if (fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon");
|
---|
| 86 |
|
---|
| 87 | /*Multiplication is faster than divsion, so we multiply by the reciprocal*/
|
---|
| 88 | - det_reciprocal = 1/det;
|
---|
| 89 | + det_reciprocal = 1./det;
|
---|
| 90 |
|
---|
| 91 | /*Compute invert*/
|
---|
| 92 | Ainv[0]= A[3]*det_reciprocal; /* = d/det */
|
---|
| 93 | @@ -457,15 +449,14 @@
|
---|
| 94 | void Matrix3x3Invert(IssmDouble* Ainv,IssmDouble* A){/*{{{*/
|
---|
| 95 |
|
---|
| 96 | /*Intermediaries*/
|
---|
| 97 | - IssmDouble det;
|
---|
| 98 | - IssmDouble det_reciprocal;
|
---|
| 99 | + IssmDouble det,det_reciprocal;
|
---|
| 100 |
|
---|
| 101 | /*Compute determinant*/
|
---|
| 102 | Matrix3x3Determinant(&det,A);
|
---|
| 103 | if (fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon");
|
---|
| 104 |
|
---|
| 105 | /*Multiplication is faster than divsion, so we multiply by the reciprocal*/
|
---|
| 106 | - det_reciprocal = 1/det;
|
---|
| 107 | + det_reciprocal = 1./det;
|
---|
| 108 |
|
---|
| 109 | /*Compute invert*/
|
---|
| 110 | Ainv[0]=(A[4]*A[8]-A[5]*A[7])*det_reciprocal; /* = (e*i-f*h)/det */
|
---|
| 111 | @@ -562,15 +553,14 @@
|
---|
| 112 | void Matrix4x4Invert(IssmDouble* Ainv,IssmDouble* A){/*{{{*/
|
---|
| 113 |
|
---|
| 114 | /*Intermediaries*/
|
---|
| 115 | - IssmDouble det;
|
---|
| 116 | - IssmDouble det_reciprocal;
|
---|
| 117 | + IssmDouble det,det_reciprocal;
|
---|
| 118 |
|
---|
| 119 | /*Compute determinant*/
|
---|
| 120 | Matrix4x4Determinant(&det,A);
|
---|
| 121 | - //if(fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon");
|
---|
| 122 | + if(fabs(det) < DBL_EPSILON) _error_("Determinant smaller than machine epsilon");
|
---|
| 123 |
|
---|
| 124 | /*Multiplication is faster than division, so we multiply by the reciprocal*/
|
---|
| 125 | - det_reciprocal = 1/det;
|
---|
| 126 | + det_reciprocal = 1./det;
|
---|
| 127 |
|
---|
| 128 | /*Compute adjoint matrix*/
|
---|
| 129 | Matrix4x4Adjoint(Ainv,A);
|
---|