Changeset 13142


Ignore:
Timestamp:
08/22/12 17:21:15 (13 years ago)
Author:
Mathieu Morlighem
Message:

CHG: cosmetics

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/shared/Matrix/MatrixUtils.cpp

    r13056 r13142  
    1717
    1818/*FUNCTION TripleMultiply {{{*/
    19 int TripleMultiply( IssmDouble* a, int nrowa, int ncola, int itrna, IssmDouble* b, int nrowb, int ncolb, int itrnb, IssmDouble* c, int nrowc, int ncolc, int itrnc, IssmDouble* d, int iaddd){
     19int TripleMultiply(IssmDouble* a, int nrowa, int ncola, int itrna, IssmDouble* b, int nrowb, int ncolb, int itrnb, IssmDouble* c, int nrowc, int ncolc, int itrnc, IssmDouble* d, int iaddd){
    2020        /*TripleMultiply    Perform triple matrix product a*b*c+d.*/
    21        
    22         int idima,idimb,idimc,idimd;
    23         IssmDouble* dtemp;
    24 
    25 /*  set up dimensions for triple product  */
    26 
    27         if (!itrna) {
     21
     22        int         idima,idimb,idimc,idimd;
     23        IssmDouble *dtemp;
     24
     25        /*  set up dimensions for triple product  */
     26
     27        if (!itrna){
    2828                idima=nrowa;
    2929                idimb=ncola;
    3030        }
    31         else {
     31        else{
    3232                idima=ncola;
    3333                idimb=nrowa;
    3434        }
    3535
    36         if (!itrnb) {
    37                 if (nrowb != idimb) {
    38                         _error_("Matrix A and B inner vectors not equal size.");
    39                 }
     36        if (!itrnb){
     37                if (nrowb != idimb) _error_("Matrix A and B inner vectors not equal size.");
    4038                idimc=ncolb;
    4139        }
    42         else {
    43                 if (ncolb != idimb) {
    44                         _error_("Matrix A and B inner vectors not equal size.");
    45                 }
     40        else{
     41                if (ncolb != idimb) _error_("Matrix A and B inner vectors not equal size.");
    4642                idimc=nrowb;
    4743        }
    4844
    4945        if (!itrnc) {
    50                 if (nrowc != idimc) {
    51                         _error_("Matrix B and C inner vectors not equal size.");
    52                 }
     46                if (nrowc != idimc) _error_("Matrix B and C inner vectors not equal size.");
    5347                idimd=ncolc;
    5448        }
    55         else {
    56                 if (ncolc != idimc) {
    57                         _error_("Matrix B and C inner vectors not equal size.");
    58                 }
     49        else{
     50                if (ncolc != idimc) _error_("Matrix B and C inner vectors not equal size.");
    5951                idimd=nrowc;
    6052        }
    6153
    62 /*  perform the matrix triple product in the order that minimizes the
    63         number of multiplies and the temporary space used, noting that
    64         (a*b)*c requires ac(b+d) multiplies and ac IssmDoubles, and a*(b*c)
    65         requires bd(a+c) multiplies and bd IssmDoubles (both are the same for
    66         a symmetric triple product)  */
    67 
    68 /*  multiply (a*b)*c+d  */
    69 
     54        /*  perform the matrix triple product in the order that minimizes the
     55                 number of multiplies and the temporary space used, noting that
     56                 (a*b)*c requires ac(b+d) multiplies and ac IssmDoubles, and a*(b*c)
     57                 requires bd(a+c) multiplies and bd IssmDoubles (both are the same for
     58                 a symmetric triple product)  */
     59
     60        /*  multiply (a*b)*c+d  */
    7061        if (idima*idimc*(idimb+idimd) <= idimb*idimd*(idima+idimc)) {
    7162                dtemp=xNew<IssmDouble>(idima*idimc);
     
    7667        }
    7768
    78 /*  multiply a*(b*c)+d  */
    79 
    80         else {
     69        /*  multiply a*(b*c)+d  */
     70        else{
    8171                dtemp=xNew<IssmDouble>(idimb*idimd);
    8272
     
    8979}/*}}}*/
    9080/*FUNCTION MatrixMuliply {{{*/
    91 int MatrixMultiply( IssmDouble* a, int nrowa, int ncola, int itrna, IssmDouble* b, int nrowb, int ncolb, int itrnb, IssmDouble* c, int iaddc ){
     81int MatrixMultiply(IssmDouble* a, int nrowa, int ncola, int itrna, IssmDouble* b, int nrowb, int ncolb, int itrnb, IssmDouble* c, int iaddc ){
    9282        /*MatrixMultiply    Perform matrix multiplication a*b+c.*/
    9383        int noerr=1;
     
    9585        int nrowc,ncolc,iinca,jinca,iincb,jincb,ntrma,ntrmb,nterm;
    9686
    97 /*  set up dimensions and increments for matrix a  */
     87        /*  set up dimensions and increments for matrix a  */
    9888        if (!itrna) {
    9989                nrowc=nrowa;
     
    10999        }
    110100
    111 /*  set up dimensions and increments for matrix b  */
     101        /*  set up dimensions and increments for matrix b  */
    112102        if (!itrnb) {
    113103                ncolc=ncolb;
     
    123113        }
    124114
    125         if (ntrma != ntrmb) {
    126                 _error_("Matrix A and B inner vectors not equal size");
    127             noerr=0;   
    128                 return noerr;
    129         }
    130         else
    131                 nterm=ntrma;
    132 
    133 /*  zero matrix c, if not being added to product  */
    134 
    135         if (!iaddc)
    136                 for (i=0; i<nrowc*ncolc; i++)
    137                         *(c+i)=0.;
    138 
    139 /*  perform the matrix multiplication  */
    140 
     115        if (ntrma != ntrmb) _error_("Matrix A and B inner vectors not equal size");
     116
     117        nterm=ntrma;
     118
     119        /*  zero matrix c, if not being added to product  */
     120        if (!iaddc) for (i=0;i<nrowc*ncolc;i++) c[i]=0.;
     121
     122        /*  perform the matrix multiplication  */
    141123        iptc=0;
    142         for (i=0; i<nrowc; i++) {
    143                 for (j=0; j<ncolc; j++) {
     124        for (i=0; i<nrowc; i++){
     125                for (j=0; j<ncolc; j++){
    144126                        ipta=i*iinca;
    145127                        iptb=j*jincb;
    146128
    147                         for (k=0; k<nterm; k++) {
    148                                 *(c+iptc)+=*(a+ipta)**(b+iptb);
     129                        for (k=0; k<nterm; k++){
     130                                c[iptc]+=a[ipta]*b[iptb];
    149131                                ipta+=jinca;
    150132                                iptb+=iincb;
    151133                        }
    152 
    153134                        iptc++;
    154135                }
     
    159140/*FUNCTION MatrixInverse {{{*/
    160141int MatrixInverse( IssmDouble* a, int ndim, int nrow, IssmDouble* b, int nvec, IssmDouble* pdet ){
    161 /* MatrixInverse    Perform matrix inversion and linear equation solution.
    162 
    163         This function uses Gaussian elimination on the original matrix
    164         augmented by an identity matrix of the same size to calculate
    165         the inverse (see for example, "Modern Methods of Engineering
    166         Computation", Sec. 6.4).  By noting how the matrices are
    167         unpopulated and repopulated, the calculation may be done in place.
    168 
    169         Gaussian elimination is inherently inefficient, and so this is
    170         intended for small matrices.  */
     142        /* MatrixInverse    Perform matrix inversion and linear equation solution.
     143
     144                This function uses Gaussian elimination on the original matrix
     145                augmented by an identity matrix of the same size to calculate
     146                the inverse (see for example, "Modern Methods of Engineering
     147                Computation", Sec. 6.4).  By noting how the matrices are
     148                unpopulated and repopulated, the calculation may be done in place.
     149
     150                Gaussian elimination is inherently inefficient, and so this is
     151                intended for small matrices.  */
    171152        int noerr=1;
    172153        int i,j,k,ipt,jpt,irow,icol,ipiv,ncol;
     
    184165        _assert_(!(ndim==3 && nrow==3));
    185166
    186 /*  initialize local variables and arrays  */
     167        /*  initialize local variables and arrays  */
    187168
    188169        ncol=nrow;
     
    192173        pindx =xNew<int>(nrow);
    193174
    194 /*  loop over the rows/columns of the matrix  */
     175        /*  loop over the rows/columns of the matrix  */
    195176
    196177        for (i=0; i<nrow; i++) {
    197178
    198 /*  search for pivot, finding the term with the greatest magnitude
    199         in the rows/columns not yet used  */
     179                /*  search for pivot, finding the term with the greatest magnitude
     180                         in the rows/columns not yet used  */
    200181
    201182                pivot=0.;
    202183                for (j=0; j<nrow; j++)
    203                         if (!pindx[j])
    204                                 for (k=0; k<ncol; k++)
    205                                         if (!pindx[k])
    206                                                 if (fabs(a[j*ndim+k]) > fabs(pivot)) {
    207                                                         irow=j;
    208                                                         icol=k;
    209                                                         pivot=a[j*ndim+k];
    210                                                 }
     184                 if (!pindx[j])
     185                  for (k=0; k<ncol; k++)
     186                        if (!pindx[k])
     187                         if (fabs(a[j*ndim+k]) > fabs(pivot)) {
     188                                 irow=j;
     189                                 icol=k;
     190                                 pivot=a[j*ndim+k];
     191                         }
    211192
    212193                if (fabs(pivot) < DBL_EPSILON) {
     
    225206                pindx[ipiv]++;
    226207
    227 //              _printf_(true,"pivot for i=%d: irow=%d, icol=%d, pindx[%d]=%d\n",
    228 //                               i,irow,icol,ipiv,pindx[ipiv]);
    229 
    230 /*  switch rows to put pivot element on diagonal, noting that the
    231         column stays the same and the determinant changes sign  */
     208                //              _printf_(true,"pivot for i=%d: irow=%d, icol=%d, pindx[%d]=%d\n",
     209                //                               i,irow,icol,ipiv,pindx[ipiv]);
     210
     211                /*  switch rows to put pivot element on diagonal, noting that the
     212                         column stays the same and the determinant changes sign  */
    232213
    233214                if (irow != icol) {
    234 //                      _printf_(true,"row switch for i=%d: irow=%d, icol=%d\n",
    235 //                                       i,irow,icol);
     215                        //                      _printf_(true,"row switch for i=%d: irow=%d, icol=%d\n",
     216                        //                                       i,irow,icol);
    236217
    237218                        ipt=irow*ndim;
     
    254235                }
    255236
    256 /*  divide pivot row by pivot element, noting that the original
    257         matrix will have 1 on the diagonal, which will be discarded,
    258         and the augmented matrix will start with 1 from the identity
    259         matrix and then have 1/pivot, which is part of the inverse.  */
     237                /*  divide pivot row by pivot element, noting that the original
     238                         matrix will have 1 on the diagonal, which will be discarded,
     239                         and the augmented matrix will start with 1 from the identity
     240                         matrix and then have 1/pivot, which is part of the inverse.  */
    260241
    261242                a[ipiv*ndim+ipiv]=1.;
     
    263244                ipt=ipiv*ndim;
    264245                for (k=0; k<ncol; k++)
    265                         a[ipt+k]/=pivot;
     246                 a[ipt+k]/=pivot;
    266247
    267248                ipt=ipiv*nvec;
    268249                for (k=0; k<nvec; k++)
    269                         b[ipt+k]/=pivot;
    270 
    271 /*  reduce non-pivot rows such that they will have 0 in the pivot
    272         column, which will be discarded, and the augmented matrix will
    273         start with 0 from the identity matrix and then have non-zero
    274         in the corresponding column, which is part of the inverse.
    275         only one column of the augmented matrix is populated at a time,
    276         which corresponds to the only column of the original matrix
    277         being zeroed, so that the inverse may be done in place.  */
     250                 b[ipt+k]/=pivot;
     251
     252                /*  reduce non-pivot rows such that they will have 0 in the pivot
     253                         column, which will be discarded, and the augmented matrix will
     254                         start with 0 from the identity matrix and then have non-zero
     255                         in the corresponding column, which is part of the inverse.
     256                         only one column of the augmented matrix is populated at a time,
     257                         which corresponds to the only column of the original matrix
     258                         being zeroed, so that the inverse may be done in place.  */
    278259
    279260                for (j=0; j<nrow; j++) {
     
    287268                                jpt=ipiv*ndim;
    288269                                for (k=0; k<ncol; k++)
    289                                         a[ipt+k]-=dtemp*a[jpt+k];
     270                                 a[ipt+k]-=dtemp*a[jpt+k];
    290271
    291272                                ipt=j   *nvec;
    292273                                jpt=ipiv*nvec;
    293274                                for (k=0; k<nvec; k++)
    294                                         b[ipt+k]-=dtemp*b[jpt+k];
    295                         }
    296                 }
    297 
    298 /*  for a diagonal matrix, the determinant is the product of the
    299         diagonal terms, and so it may be accumulated from the pivots,
    300         noting that switching rows changes the sign as above  */
     275                                 b[ipt+k]-=dtemp*b[jpt+k];
     276                        }
     277                }
     278
     279                /*  for a diagonal matrix, the determinant is the product of the
     280                         diagonal terms, and so it may be accumulated from the pivots,
     281                         noting that switching rows changes the sign as above  */
    301282
    302283                det*=pivot;
    303284        }
    304285
    305 /*  switch columns back in reverse order, noting that a row switch
    306         in the original matrix corresponds to a column switch in the
    307         inverse matrix  */
     286        /*  switch columns back in reverse order, noting that a row switch
     287                 in the original matrix corresponds to a column switch in the
     288                 inverse matrix  */
    308289
    309290        for (i=0; i<nrow; i++) {
     
    314295                        icol=pivrc2[j];
    315296
    316 //                      _printf_(true,"column switch back for j=%d: irow=%d, icol=%d\n",
    317 //                                       j,irow,icol);
     297                        //                      _printf_(true,"column switch back for j=%d: irow=%d, icol=%d\n",
     298                        //                                       j,irow,icol);
    318299
    319300                        ipt=0;
     
    363344
    364345        /*det = a*(e*i-f*h)-b*(d*i-f*g)+c*(d*h-e*g)*/
    365    *Adet= A[0]*A[4]*A[8]-A[0]*A[5]*A[7]-A[3]*A[1]*A[8]+A[3]*A[2]*A[7]+A[6]*A[1]*A[5]-A[6]*A[2]*A[4];
     346        *Adet= A[0]*A[4]*A[8]-A[0]*A[5]*A[7]-A[3]*A[1]*A[8]+A[3]*A[2]*A[7]+A[6]*A[1]*A[5]-A[6]*A[2]*A[4];
    366347}
    367348/*}}}*/
Note: See TracChangeset for help on using the changeset viewer.