Intrepid
Intrepid_RealSpaceToolsDef.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Pavel Bochev (pbboche@sandia.gov)
38 // Denis Ridzal (dridzal@sandia.gov), or
39 // Kara Peterson (kjpeter@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
50 namespace Intrepid {
51 
52 
53 
54 template<class Scalar>
55 void RealSpaceTools<Scalar>::absval(Scalar* absArray, const Scalar* inArray, const int size) {
56  for (int i=0; i<size; i++) {
57  absArray[i] = std::abs(inArray[i]);
58  }
59 }
60 
61 
62 
63 template<class Scalar>
64 void RealSpaceTools<Scalar>::absval(Scalar* inoutAbsArray, const int size) {
65  for (int i=0; i<size; i++) {
66  inoutAbsArray[i] = std::abs(inoutAbsArray[i]);
67  }
68 }
69 
70 
71 
72 template<class Scalar>
73 template<class ArrayAbs, class ArrayIn>
74 void RealSpaceTools<Scalar>::absval(ArrayAbs & absArray, const ArrayIn & inArray) {
75 #ifdef HAVE_INTREPID_DEBUG
76  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.rank() != absArray.rank() ),
77  std::invalid_argument,
78  ">>> ERROR (RealSpaceTools::absval): Array arguments must have identical ranks!");
79  for (int i=0; i<inArray.rank(); i++) {
80  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.dimension(i) != absArray.dimension(i) ),
81  std::invalid_argument,
82  ">>> ERROR (RealSpaceTools::absval): Dimensions of array arguments do not agree!");
83  }
84 #endif
85 
86  for (int i=0; i<inArray.size(); i++) {
87  absArray[i] = std::abs(inArray[i]);
88  }
89 }
90 
91 
92 
93 template<class Scalar>
94 template<class ArrayInOut>
95 void RealSpaceTools<Scalar>::absval(ArrayInOut & inoutAbsArray) {
96  for (int i=0; i<inoutAbsArray.size(); i++) {
97  inoutAbsArray[i] = std::abs(inoutAbsArray[i]);
98  }
99 }
100 
101 
102 
103 template<class Scalar>
104 Scalar RealSpaceTools<Scalar>::vectorNorm(const Scalar* inVec, const int dim, const ENorm normType) {
105  Scalar temp = (Scalar)0;
106  switch(normType) {
107  case NORM_TWO:
108  for(int i = 0; i < dim; i++){
109  temp += inVec[i]*inVec[i];
110  }
111  temp = std::sqrt(temp);
112  break;
113  case NORM_INF:
114  temp = std::abs(inVec[0]);
115  for(int i = 1; i < dim; i++){
116  Scalar absData = std::abs(inVec[i]);
117  if (temp < absData) temp = absData;
118  }
119  break;
120  case NORM_ONE:
121  for(int i = 0; i < dim; i++){
122  temp += std::abs(inVec[i]);
123  }
124  break;
125  default:
126  TEUCHOS_TEST_FOR_EXCEPTION( ( (normType != NORM_TWO) && (normType != NORM_INF) && (normType != NORM_ONE) ),
127  std::invalid_argument,
128  ">>> ERROR (RealSpaceTools::vectorNorm): Invalid argument normType.");
129  }
130  return temp;
131 }
132 
133 
134 
135 template<class Scalar>
136 template<class ArrayIn>
137 Scalar RealSpaceTools<Scalar>::vectorNorm(const ArrayIn & inVec, const ENorm normType) {
138 
139 #ifdef HAVE_INTREPID_DEBUG
140  TEUCHOS_TEST_FOR_EXCEPTION( ( inVec.rank() != 1 ),
141  std::invalid_argument,
142  ">>> ERROR (RealSpaceTools::vectorNorm): Vector argument must have rank 1!");
143 #endif
144 
145  int size = inVec.size();
146 
147  Scalar temp = (Scalar)0;
148  switch(normType) {
149  case NORM_TWO:
150  for(int i = 0; i < size; i++){
151  temp += inVec[i]*inVec[i];
152  }
153  temp = std::sqrt(temp);
154  break;
155  case NORM_INF:
156  temp = std::abs(inVec[0]);
157  for(int i = 1; i < size; i++){
158  Scalar absData = std::abs(inVec[i]);
159  if (temp < absData) temp = absData;
160  }
161  break;
162  case NORM_ONE:
163  for(int i = 0; i < size; i++){
164  temp += std::abs(inVec[i]);
165  }
166  break;
167  default:
168  TEUCHOS_TEST_FOR_EXCEPTION( ( (normType != NORM_TWO) && (normType != NORM_INF) && (normType != NORM_ONE) ),
169  std::invalid_argument,
170  ">>> ERROR (RealSpaceTools::vectorNorm): Invalid argument normType.");
171  }
172  return temp;
173 }
174 
175 
176 
177 template<class Scalar>
178 template<class ArrayNorm, class ArrayIn>
179 void RealSpaceTools<Scalar>::vectorNorm(ArrayNorm & normArray, const ArrayIn & inVecs, const ENorm normType) {
180 
181  int arrayRank = inVecs.rank();
182 
183 #ifdef HAVE_INTREPID_DEBUG
184  TEUCHOS_TEST_FOR_EXCEPTION( ( arrayRank != normArray.rank()+1 ),
185  std::invalid_argument,
186  ">>> ERROR (RealSpaceTools::vectorNorm): Ranks of norm and vector array arguments are incompatible!");
187  TEUCHOS_TEST_FOR_EXCEPTION( ( (arrayRank < 2) || (arrayRank > 3) ),
188  std::invalid_argument,
189  ">>> ERROR (RealSpaceTools::vectorNorm): Rank of vector array must be 2 or 3!");
190  for (int i=0; i<arrayRank-1; i++) {
191  TEUCHOS_TEST_FOR_EXCEPTION( ( inVecs.dimension(i) != normArray.dimension(i) ),
192  std::invalid_argument,
193  ">>> ERROR (RealSpaceTools::vectorNorm): Dimensions of norm and vector arguments do not agree!");
194  }
195 #endif
196 
197  int dim_i0 = 1; // first index dimension (e.g. cell index)
198  int dim_i1 = 1; // second index dimension (e.g. point index)
199  int dim = inVecs.dimension(arrayRank-1); // spatial dimension
200 
201  // determine i0 and i1 dimensions
202  switch(arrayRank) {
203  case 3:
204  dim_i0 = inVecs.dimension(0);
205  dim_i1 = inVecs.dimension(1);
206  break;
207  case 2:
208  dim_i1 = inVecs.dimension(0);
209  break;
210  }
211 
212  switch(normType) {
213  case NORM_TWO: {
214  int offset_i0, offset, normOffset;
215  for (int i0=0; i0<dim_i0; i0++) {
216  offset_i0 = i0*dim_i1;
217  for (int i1=0; i1<dim_i1; i1++) {
218  offset = offset_i0 + i1;
219  normOffset = offset;
220  offset *= dim;
221  Scalar temp = (Scalar)0;
222  for(int i = 0; i < dim; i++){
223  temp += inVecs[offset+i]*inVecs[offset+i];
224  }
225  normArray[normOffset] = std::sqrt(temp);
226  }
227  }
228  break;
229  } // case NORM_TWO
230 
231  case NORM_INF: {
232  int offset_i0, offset, normOffset;
233  for (int i0=0; i0<dim_i0; i0++) {
234  offset_i0 = i0*dim_i1;
235  for (int i1=0; i1<dim_i1; i1++) {
236  offset = offset_i0 + i1;
237  normOffset = offset;
238  offset *= dim;
239  Scalar temp = (Scalar)0;
240  temp = std::abs(inVecs[offset]);
241  for(int i = 1; i < dim; i++){
242  Scalar absData = std::abs(inVecs[offset+i]);
243  if (temp < absData) temp = absData;
244  }
245  normArray[normOffset] = temp;
246  }
247  }
248  break;
249  } // case NORM_INF
250 
251  case NORM_ONE: {
252  int offset_i0, offset, normOffset;
253  for (int i0=0; i0<dim_i0; i0++) {
254  offset_i0 = i0*dim_i1;
255  for (int i1=0; i1<dim_i1; i1++) {
256  offset = offset_i0 + i1;
257  normOffset = offset;
258  offset *= dim;
259  Scalar temp = (Scalar)0;
260  for(int i = 0; i < dim; i++){
261  temp += std::abs(inVecs[offset+i]);
262  }
263  normArray[normOffset] = temp;
264  }
265  }
266  break;
267  } // case NORM_ONE
268 
269  default:
270  TEUCHOS_TEST_FOR_EXCEPTION( ( (normType != NORM_TWO) && (normType != NORM_INF) && (normType != NORM_ONE) ),
271  std::invalid_argument,
272  ">>> ERROR (RealSpaceTools::vectorNorm): Invalid argument normType.");
273  }
274 }
275 
276 
277 
278 template<class Scalar>
279 void RealSpaceTools<Scalar>::transpose(Scalar* transposeMat, const Scalar* inMat, const int dim) {
280  for(int i=0; i < dim; i++){
281  transposeMat[i*dim+i]=inMat[i*dim+i]; // Set diagonal elements
282  for(int j=i+1; j < dim; j++){
283  transposeMat[i*dim+j]=inMat[j*dim+i]; // Set off-diagonal elements
284  transposeMat[j*dim+i]=inMat[i*dim+j];
285  }
286  }
287 }
288 
289 
290 
291 template<class Scalar>
292 template<class ArrayTranspose, class ArrayIn>
293 void RealSpaceTools<Scalar>::transpose(ArrayTranspose & transposeMats, const ArrayIn & inMats) {
294  int arrayRank = inMats.rank();
295 
296 #ifdef HAVE_INTREPID_DEBUG
297  TEUCHOS_TEST_FOR_EXCEPTION( ( arrayRank != transposeMats.rank() ),
298  std::invalid_argument,
299  ">>> ERROR (RealSpaceTools::transpose): Matrix array arguments do not have identical ranks!");
300  TEUCHOS_TEST_FOR_EXCEPTION( ( (arrayRank < 2) || (arrayRank > 4) ),
301  std::invalid_argument,
302  ">>> ERROR (RealSpaceTools::transpose): Rank of matrix array must be 2, 3, or 4!");
303  for (int i=0; i<arrayRank; i++) {
304  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(i) != transposeMats.dimension(i) ),
305  std::invalid_argument,
306  ">>> ERROR (RealSpaceTools::transpose): Dimensions of matrix arguments do not agree!");
307  }
308  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(arrayRank-2) != inMats.dimension(arrayRank-1) ),
309  std::invalid_argument,
310  ">>> ERROR (RealSpaceTools::transpose): Matrices are not square!");
311 #endif
312 
313  int dim_i0 = 1; // first index dimension (e.g. cell index)
314  int dim_i1 = 1; // second index dimension (e.g. point index)
315  int dim = inMats.dimension(arrayRank-2); // spatial dimension
316 
317  // determine i0 and i1 dimensions
318  switch(arrayRank) {
319  case 4:
320  dim_i0 = inMats.dimension(0);
321  dim_i1 = inMats.dimension(1);
322  break;
323  case 3:
324  dim_i1 = inMats.dimension(0);
325  break;
326  }
327 
328  int offset_i0, offset;
329 
330  for (int i0=0; i0<dim_i0; i0++) {
331  offset_i0 = i0*dim_i1;
332  for (int i1=0; i1<dim_i1; i1++) {
333  offset = offset_i0 + i1;
334  offset *= (dim*dim);
335 
336  for(int i=0; i < dim; i++){
337  transposeMats[offset+i*dim+i]=inMats[offset+i*dim+i]; // Set diagonal elements
338  for(int j=i+1; j < dim; j++){
339  transposeMats[offset+i*dim+j]=inMats[offset+j*dim+i]; // Set off-diagonal elements
340  transposeMats[offset+j*dim+i]=inMats[offset+i*dim+j];
341  }
342  }
343 
344  } // i1
345  } // i0
346 
347 }
348 
349 
350 
351 template<class Scalar>
352 void RealSpaceTools<Scalar>::inverse(Scalar* inverseMat, const Scalar* inMat, const int dim) {
353 
354  switch(dim) {
355  case 3: {
356  int i, j, rowID = 0, colID = 0;
357  int rowperm[3]={0,1,2};
358  int colperm[3]={0,1,2}; // Complete pivoting
359  Scalar emax(0);
360 
361  for(i=0; i < 3; ++i){
362  for(j=0; j < 3; ++j){
363  if( std::abs( inMat[i*3+j] ) > emax){
364  rowID = i; colID = j; emax = std::abs( inMat[i*3+j] );
365  }
366  }
367  }
368 #ifdef HAVE_INTREPID_DEBUG
369  TEUCHOS_TEST_FOR_EXCEPTION( ( emax == (Scalar)0 ),
370  std::invalid_argument,
371  ">>> ERROR (Matrix): Inverse of a zero matrix is undefined!");
372 #endif
373  if( rowID ){
374  rowperm[0] = rowID;
375  rowperm[rowID] = 0;
376  }
377  if( colID ){
378  colperm[0] = colID;
379  colperm[colID] = 0;
380  }
381  Scalar B[3][3], S[2][2], Bi[3][3]; // B=rowperm inMat colperm, S=Schur complement(Boo)
382  for(i=0; i < 3; ++i){
383  for(j=0; j < 3; ++j){
384  B[i][j] = inMat[rowperm[i]*3+colperm[j]];
385  }
386  }
387  B[1][0] /= B[0][0]; B[2][0] /= B[0][0];// B(:,0)/=pivot
388  for(i=0; i < 2; ++i){
389  for(j=0; j < 2; ++j){
390  S[i][j] = B[i+1][j+1] - B[i+1][0] * B[0][j+1]; // S = B -z*y'
391  }
392  }
393  Scalar detS = S[0][0]*S[1][1]- S[0][1]*S[1][0], Si[2][2];
394 #ifdef HAVE_INTREPID_DEBUG
395  TEUCHOS_TEST_FOR_EXCEPTION( ( detS == (Scalar)0 ),
396  std::invalid_argument,
397  ">>> ERROR (Matrix): Inverse of a singular matrix is undefined!");
398 #endif
399 
400  Si[0][0] = S[1][1]/detS; Si[0][1] = -S[0][1]/detS;
401  Si[1][0] = -S[1][0]/detS; Si[1][1] = S[0][0]/detS;
402 
403  for(j=0; j<2;j++)
404  Bi[0][j+1] = -( B[0][1]*Si[0][j] + B[0][2]* Si[1][j])/B[0][0];
405  for(i=0; i<2;i++)
406  Bi[i+1][0] = -(Si[i][0]*B[1][0] + Si[i][1]*B[2][0]);
407 
408  Bi[0][0] = ((Scalar)1/B[0][0])-Bi[0][1]*B[1][0]-Bi[0][2]*B[2][0];
409  Bi[1][1] = Si[0][0];
410  Bi[1][2] = Si[0][1];
411  Bi[2][1] = Si[1][0];
412  Bi[2][2] = Si[1][1];
413  for(i=0; i < 3; ++i){
414  for(j=0; j < 3; ++j){
415  inverseMat[i*3+j] = Bi[colperm[i]][rowperm[j]]; // set inverse
416  }
417  }
418  break;
419  } // case 3
420 
421  case 2: {
422 
423  Scalar determinant = inMat[0]*inMat[3]-inMat[1]*inMat[2];;
424 #ifdef HAVE_INTREPID_DEBUG
425  TEUCHOS_TEST_FOR_EXCEPTION( ( (inMat[0]==(Scalar)0) && (inMat[1]==(Scalar)0) &&
426  (inMat[2]==(Scalar)0) && (inMat[3]==(Scalar)0) ),
427  std::invalid_argument,
428  ">>> ERROR (Matrix): Inverse of a zero matrix is undefined!");
429  TEUCHOS_TEST_FOR_EXCEPTION( ( determinant == (Scalar)0 ),
430  std::invalid_argument,
431  ">>> ERROR (Matrix): Inverse of a singular matrix is undefined!");
432 #endif
433  inverseMat[0] = inMat[3] / determinant;
434  inverseMat[1] = - inMat[1] / determinant;
435  //
436  inverseMat[2] = - inMat[2] / determinant;
437  inverseMat[3] = inMat[0] / determinant;
438  break;
439  } // case 2
440 
441  case 1: {
442 #ifdef HAVE_INTREPID_DEBUG
443  TEUCHOS_TEST_FOR_EXCEPTION( ( inMat[0] == (Scalar)0 ),
444  std::invalid_argument,
445  ">>> ERROR (Matrix): Inverse of a zero matrix is undefined!");
446 #endif
447  inverseMat[0] = (Scalar)1 / inMat[0];
448  break;
449  } // case 1
450 
451  } // switch (dim)
452 }
453 
454 
455 
456 template<class Scalar>
457 template<class ArrayInverse, class ArrayIn>
458 void RealSpaceTools<Scalar>::inverse(ArrayInverse & inverseMats, const ArrayIn & inMats) {
459 
460  int arrayRank = inMats.rank();
461 
462 #ifdef HAVE_INTREPID_DEBUG
463  TEUCHOS_TEST_FOR_EXCEPTION( ( arrayRank != inverseMats.rank() ),
464  std::invalid_argument,
465  ">>> ERROR (RealSpaceTools::inverse): Matrix array arguments do not have identical ranks!");
466  TEUCHOS_TEST_FOR_EXCEPTION( ( (arrayRank < 2) || (arrayRank > 4) ),
467  std::invalid_argument,
468  ">>> ERROR (RealSpaceTools::inverse): Rank of matrix array must be 2, 3, or 4!");
469  for (int i=0; i<arrayRank; i++) {
470  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(i) != inverseMats.dimension(i) ),
471  std::invalid_argument,
472  ">>> ERROR (RealSpaceTools::inverse): Dimensions of matrix arguments do not agree!");
473  }
474  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(arrayRank-2) != inMats.dimension(arrayRank-1) ),
475  std::invalid_argument,
476  ">>> ERROR (RealSpaceTools::inverse): Matrices are not square!");
477  TEUCHOS_TEST_FOR_EXCEPTION( ( (inMats.dimension(arrayRank-2) < 1) || (inMats.dimension(arrayRank-2) > 3) ),
478  std::invalid_argument,
479  ">>> ERROR (RealSpaceTools::inverse): Spatial dimension must be 1, 2, or 3!");
480 #endif
481 
482  int dim_i0 = 1; // first index dimension (e.g. cell index)
483  int dim_i1 = 1; // second index dimension (e.g. point index)
484  int dim = inMats.dimension(arrayRank-2); // spatial dimension
485 
486  // determine i0 and i1 dimensions
487  switch(arrayRank) {
488  case 4:
489  dim_i0 = inMats.dimension(0);
490  dim_i1 = inMats.dimension(1);
491  break;
492  case 3:
493  dim_i1 = inMats.dimension(0);
494  break;
495  }
496 
497  switch(dim) {
498  case 3: {
499  int offset_i0, offset;
500 
501  for (int i0=0; i0<dim_i0; i0++) {
502  offset_i0 = i0*dim_i1;
503  for (int i1=0; i1<dim_i1; i1++) {
504  offset = offset_i0 + i1;
505  offset *= 9;
506 
507  int i, j, rowID = 0, colID = 0;
508  int rowperm[3]={0,1,2};
509  int colperm[3]={0,1,2}; // Complete pivoting
510  Scalar emax(0);
511 
512  for(i=0; i < 3; ++i){
513  for(j=0; j < 3; ++j){
514  if( std::abs( inMats[offset+i*3+j] ) > emax){
515  rowID = i; colID = j; emax = std::abs( inMats[offset+i*3+j] );
516  }
517  }
518  }
519 #ifdef HAVE_INTREPID_DEBUG
520 #ifdef HAVE_INTREPID_DEBUG_INF_CHECK
521  TEUCHOS_TEST_FOR_EXCEPTION( ( emax == (Scalar)0 ),
522  std::invalid_argument,
523  ">>> ERROR (Matrix): Inverse of a zero matrix is undefined!");
524 #endif
525 #endif
526  if( rowID ){
527  rowperm[0] = rowID;
528  rowperm[rowID] = 0;
529  }
530  if( colID ){
531  colperm[0] = colID;
532  colperm[colID] = 0;
533  }
534  Scalar B[3][3], S[2][2], Bi[3][3]; // B=rowperm inMat colperm, S=Schur complement(Boo)
535  for(i=0; i < 3; ++i){
536  for(j=0; j < 3; ++j){
537  B[i][j] = inMats[offset+rowperm[i]*3+colperm[j]];
538  }
539  }
540  B[1][0] /= B[0][0]; B[2][0] /= B[0][0];// B(:,0)/=pivot
541  for(i=0; i < 2; ++i){
542  for(j=0; j < 2; ++j){
543  S[i][j] = B[i+1][j+1] - B[i+1][0] * B[0][j+1]; // S = B -z*y'
544  }
545  }
546  Scalar detS = S[0][0]*S[1][1]- S[0][1]*S[1][0], Si[2][2];
547 #ifdef HAVE_INTREPID_DEBUG
548 #ifdef HAVE_INTREPID_DEBUG_INF_CHECK
549  TEUCHOS_TEST_FOR_EXCEPTION( ( detS == (Scalar)0 ),
550  std::invalid_argument,
551  ">>> ERROR (Matrix): Inverse of a singular matrix is undefined!");
552 #endif
553 #endif
554 
555  Si[0][0] = S[1][1]/detS; Si[0][1] = -S[0][1]/detS;
556  Si[1][0] = -S[1][0]/detS; Si[1][1] = S[0][0]/detS;
557 
558  for(j=0; j<2;j++)
559  Bi[0][j+1] = -( B[0][1]*Si[0][j] + B[0][2]* Si[1][j])/B[0][0];
560  for(i=0; i<2;i++)
561  Bi[i+1][0] = -(Si[i][0]*B[1][0] + Si[i][1]*B[2][0]);
562 
563  Bi[0][0] = ((Scalar)1/B[0][0])-Bi[0][1]*B[1][0]-Bi[0][2]*B[2][0];
564  Bi[1][1] = Si[0][0];
565  Bi[1][2] = Si[0][1];
566  Bi[2][1] = Si[1][0];
567  Bi[2][2] = Si[1][1];
568  for(i=0; i < 3; ++i){
569  for(j=0; j < 3; ++j){
570  inverseMats[offset+i*3+j] = Bi[colperm[i]][rowperm[j]]; // set inverse
571  }
572  }
573  } // for i1
574  } // for i0
575  break;
576  } // case 3
577 
578  case 2: {
579  int offset_i0, offset;
580 
581  for (int i0=0; i0<dim_i0; i0++) {
582  offset_i0 = i0*dim_i1;
583  for (int i1=0; i1<dim_i1; i1++) {
584  offset = offset_i0 + i1;;
585  offset *= 4;
586 
587  Scalar determinant = inMats[offset]*inMats[offset+3]-inMats[offset+1]*inMats[offset+2];;
588 #ifdef HAVE_INTREPID_DEBUG
589 #ifdef HAVE_INTREPID_DEBUG_INF_CHECK
590  TEUCHOS_TEST_FOR_EXCEPTION( ( (inMats[offset]==(Scalar)0) && (inMats[offset+1]==(Scalar)0) &&
591  (inMats[offset+2]==(Scalar)0) && (inMats[offset+3]==(Scalar)0) ),
592  std::invalid_argument,
593  ">>> ERROR (Matrix): Inverse of a zero matrix is undefined!");
594  TEUCHOS_TEST_FOR_EXCEPTION( ( determinant == (Scalar)0 ),
595  std::invalid_argument,
596  ">>> ERROR (Matrix): Inverse of a singular matrix is undefined!");
597 #endif
598 #endif
599  inverseMats[offset] = inMats[offset+3] / determinant;
600  inverseMats[offset+1] = - inMats[offset+1] / determinant;
601  //
602  inverseMats[offset+2] = - inMats[offset+2] / determinant;
603  inverseMats[offset+3] = inMats[offset] / determinant;
604  } // for i1
605  } // for i0
606  break;
607  } // case 2
608 
609  case 1: {
610  int offset_i0, offset;
611 
612  for (int i0=0; i0<dim_i0; i0++) {
613  offset_i0 = i0*dim_i1;
614  for (int i1=0; i1<dim_i1; i1++) {
615  offset = offset_i0 + i1;;
616 #ifdef HAVE_INTREPID_DEBUG
617 #ifdef HAVE_INTREPID_DEBUG_INF_CHECK
618  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats[offset] == (Scalar)0 ),
619  std::invalid_argument,
620  ">>> ERROR (Matrix): Inverse of a zero matrix is undefined!");
621 #endif
622 #endif
623  inverseMats[offset] = (Scalar)1 / inMats[offset];
624  } // for i1
625  } // for i2
626  break;
627  } // case 1
628 
629  } // switch (dim)
630 }
631 
632 
633 
634 template<class Scalar>
635 Scalar RealSpaceTools<Scalar>::det(const Scalar* inMat, const int dim) {
636  Scalar determinant(0);
637 
638  switch (dim) {
639  case 3: {
640  int i,j,rowID = 0;
641  int colID = 0;
642  int rowperm[3]={0,1,2};
643  int colperm[3]={0,1,2}; // Complete pivoting
644  Scalar emax(0);
645 
646  for(i=0; i < 3; ++i){
647  for(j=0; j < 3; ++j){
648  if( std::abs( inMat[i*dim+j] ) > emax){
649  rowID = i; colID = j; emax = std::abs( inMat[i*dim+j] );
650  }
651  }
652  }
653  if( emax > 0 ){
654  if( rowID ){
655  rowperm[0] = rowID;
656  rowperm[rowID] = 0;
657  }
658  if( colID ){
659  colperm[0] = colID;
660  colperm[colID] = 0;
661  }
662  Scalar B[3][3], S[2][2]; // B=rowperm inMat colperm, S=Schur complement(Boo)
663  for(i=0; i < 3; ++i){
664  for(j=0; j < 3; ++j){
665  B[i][j] = inMat[rowperm[i]*dim+colperm[j]];
666  }
667  }
668  B[1][0] /= B[0][0]; B[2][0] /= B[0][0];// B(:,0)/=pivot
669  for(i=0; i < 2; ++i){
670  for(j=0; j < 2; ++j){
671  S[i][j] = B[i+1][j+1] - B[i+1][0] * B[0][j+1]; // S = B -z*y'
672  }
673  }
674  determinant = B[0][0] * (S[0][0] * S[1][1] - S[0][1] * S[1][0]); // det(B)
675  if( rowID ) determinant = -determinant;
676  if( colID ) determinant = -determinant;
677  }
678  break;
679  } // case 3
680 
681  case 2:
682  determinant = inMat[0]*inMat[3]-
683  inMat[1]*inMat[2];
684  break;
685 
686  case 1:
687  determinant = inMat[0];
688  break;
689 
690  default:
691  TEUCHOS_TEST_FOR_EXCEPTION( ( (dim != 1) && (dim != 2) && (dim != 3) ),
692  std::invalid_argument,
693  ">>> ERROR (Matrix): Invalid matrix dimension.");
694  } // switch (dim)
695 
696  return determinant;
697 }
698 
699 
700 
701 template<class Scalar>
702 template<class ArrayIn>
703 Scalar RealSpaceTools<Scalar>::det(const ArrayIn & inMat) {
704 
705 #ifdef HAVE_INTREPID_DEBUG
706  TEUCHOS_TEST_FOR_EXCEPTION( (inMat.rank() != 2),
707  std::invalid_argument,
708  ">>> ERROR (RealSpaceTools::det): Rank of matrix argument must be 2!");
709  TEUCHOS_TEST_FOR_EXCEPTION( ( inMat.dimension(0) != inMat.dimension(1) ),
710  std::invalid_argument,
711  ">>> ERROR (RealSpaceTools::det): Matrix is not square!");
712  TEUCHOS_TEST_FOR_EXCEPTION( ( (inMat.dimension(0) < 1) || (inMat.dimension(0) > 3) ),
713  std::invalid_argument,
714  ">>> ERROR (RealSpaceTools::det): Spatial dimension must be 1, 2, or 3!");
715 #endif
716 
717  int dim = inMat.dimension(0);
718  Scalar determinant(0);
719 
720  switch (dim) {
721  case 3: {
722  int i,j,rowID = 0;
723  int colID = 0;
724  int rowperm[3]={0,1,2};
725  int colperm[3]={0,1,2}; // Complete pivoting
726  Scalar emax(0);
727 
728  for(i=0; i < 3; ++i){
729  for(j=0; j < 3; ++j){
730  if( std::abs( inMat[i*dim+j] ) > emax){
731  rowID = i; colID = j; emax = std::abs( inMat[i*dim+j] );
732  }
733  }
734  }
735  if( emax > 0 ){
736  if( rowID ){
737  rowperm[0] = rowID;
738  rowperm[rowID] = 0;
739  }
740  if( colID ){
741  colperm[0] = colID;
742  colperm[colID] = 0;
743  }
744  Scalar B[3][3], S[2][2]; // B=rowperm inMat colperm, S=Schur complement(Boo)
745  for(i=0; i < 3; ++i){
746  for(j=0; j < 3; ++j){
747  B[i][j] = inMat[rowperm[i]*dim+colperm[j]];
748  }
749  }
750  B[1][0] /= B[0][0]; B[2][0] /= B[0][0];// B(:,0)/=pivot
751  for(i=0; i < 2; ++i){
752  for(j=0; j < 2; ++j){
753  S[i][j] = B[i+1][j+1] - B[i+1][0] * B[0][j+1]; // S = B -z*y'
754  }
755  }
756  determinant = B[0][0] * (S[0][0] * S[1][1] - S[0][1] * S[1][0]); // det(B)
757  if( rowID ) determinant = -determinant;
758  if( colID ) determinant = -determinant;
759  }
760  break;
761  } // case 3
762 
763  case 2:
764  determinant = inMat[0]*inMat[3]-
765  inMat[1]*inMat[2];
766  break;
767 
768  case 1:
769  determinant = inMat[0];
770  break;
771 
772  default:
773  TEUCHOS_TEST_FOR_EXCEPTION( ( (dim != 1) && (dim != 2) && (dim != 3) ),
774  std::invalid_argument,
775  ">>> ERROR (Matrix): Invalid matrix dimension.");
776  } // switch (dim)
777 
778  return determinant;
779 }
780 
781 
782 
783 
784 template<class Scalar>
785 template<class ArrayDet, class ArrayIn>
786 void RealSpaceTools<Scalar>::det(ArrayDet & detArray, const ArrayIn & inMats) {
787 
788  int matArrayRank = inMats.rank();
789 
790 #ifdef HAVE_INTREPID_DEBUG
791  TEUCHOS_TEST_FOR_EXCEPTION( ( matArrayRank != detArray.rank()+2 ),
792  std::invalid_argument,
793  ">>> ERROR (RealSpaceTools::det): Determinant and matrix array arguments do not have compatible ranks!");
794  TEUCHOS_TEST_FOR_EXCEPTION( ( (matArrayRank < 3) || (matArrayRank > 4) ),
795  std::invalid_argument,
796  ">>> ERROR (RealSpaceTools::det): Rank of matrix array must be 3 or 4!");
797  for (int i=0; i<matArrayRank-2; i++) {
798  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(i) != detArray.dimension(i) ),
799  std::invalid_argument,
800  ">>> ERROR (RealSpaceTools::det): Dimensions of determinant and matrix array arguments do not agree!");
801  }
802  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(matArrayRank-2) != inMats.dimension(matArrayRank-1) ),
803  std::invalid_argument,
804  ">>> ERROR (RealSpaceTools::det): Matrices are not square!");
805  TEUCHOS_TEST_FOR_EXCEPTION( ( (inMats.dimension(matArrayRank-2) < 1) || (inMats.dimension(matArrayRank-2) > 3) ),
806  std::invalid_argument,
807  ">>> ERROR (RealSpaceTools::det): Spatial dimension must be 1, 2, or 3!");
808 #endif
809 
810  int dim_i0 = 1; // first index dimension (e.g. cell index)
811  int dim_i1 = 1; // second index dimension (e.g. point index)
812  int dim = inMats.dimension(matArrayRank-2); // spatial dimension
813 
814  // determine i0 and i1 dimensions
815  switch(matArrayRank) {
816  case 4:
817  dim_i0 = inMats.dimension(0);
818  dim_i1 = inMats.dimension(1);
819  break;
820  case 3:
821  dim_i1 = inMats.dimension(0);
822  break;
823  }
824 
825  switch(dim) {
826  case 3: {
827  int offset_i0, offset, detOffset;
828 
829  for (int i0=0; i0<dim_i0; i0++) {
830  offset_i0 = i0*dim_i1;
831  for (int i1=0; i1<dim_i1; i1++) {
832  offset = offset_i0 + i1;
833  detOffset = offset;
834  offset *= 9;
835 
836  int i,j,rowID = 0;
837  int colID = 0;
838  int rowperm[3]={0,1,2};
839  int colperm[3]={0,1,2}; // Complete pivoting
840  Scalar emax(0), determinant(0);
841 
842  for(i=0; i < 3; ++i){
843  for(j=0; j < 3; ++j){
844  if( std::abs( inMats[offset+i*3+j] ) > emax){
845  rowID = i; colID = j; emax = std::abs( inMats[offset+i*3+j] );
846  }
847  }
848  }
849  if( emax > 0 ){
850  if( rowID ){
851  rowperm[0] = rowID;
852  rowperm[rowID] = 0;
853  }
854  if( colID ){
855  colperm[0] = colID;
856  colperm[colID] = 0;
857  }
858  Scalar B[3][3], S[2][2]; // B=rowperm inMat colperm, S=Schur complement(Boo)
859  for(i=0; i < 3; ++i){
860  for(j=0; j < 3; ++j){
861  B[i][j] = inMats[offset+rowperm[i]*3+colperm[j]];
862  }
863  }
864  B[1][0] /= B[0][0]; B[2][0] /= B[0][0];// B(:,0)/=pivot
865  for(i=0; i < 2; ++i){
866  for(j=0; j < 2; ++j){
867  S[i][j] = B[i+1][j+1] - B[i+1][0] * B[0][j+1]; // S = B -z*y'
868  }
869  }
870  determinant = B[0][0] * (S[0][0] * S[1][1] - S[0][1] * S[1][0]); // det(B)
871  if( rowID ) determinant = -determinant;
872  if( colID ) determinant = -determinant;
873  }
874  detArray[detOffset] = determinant;
875  } // for i1
876  } // for i0
877  break;
878  } // case 3
879 
880  case 2: {
881  int offset_i0, offset, detOffset;
882 
883  for (int i0=0; i0<dim_i0; i0++) {
884  offset_i0 = i0*dim_i1;
885  for (int i1=0; i1<dim_i1; i1++) {
886  offset = offset_i0 + i1;
887  detOffset = offset;
888  offset *= 4;
889 
890  detArray[detOffset] = inMats[offset]*inMats[offset+3]-inMats[offset+1]*inMats[offset+2];;
891  } // for i1
892  } // for i0
893  break;
894  } // case 2
895 
896  case 1: {
897  int offset_i0, offset;
898 
899  for (int i0=0; i0<dim_i0; i0++) {
900  offset_i0 = i0*dim_i1;
901  for (int i1=0; i1<dim_i1; i1++) {
902  offset = offset_i0 + i1;;
903  detArray[offset] = inMats[offset];
904  } // for i1
905  } // for i2
906  break;
907  } // case 1
908 
909  } // switch (dim)
910 }
911 
912 
913 
914 template<class Scalar>
915 void RealSpaceTools<Scalar>::add(Scalar* sumArray, const Scalar* inArray1, const Scalar* inArray2, const int size) {
916  for (int i=0; i<size; i++) {
917  sumArray[i] = inArray1[i] + inArray2[i];
918  }
919 }
920 
921 
922 
923 template<class Scalar>
924 void RealSpaceTools<Scalar>::add(Scalar* inoutSumArray, const Scalar* inArray, const int size) {
925  for (int i=0; i<size; i++) {
926  inoutSumArray[i] += inArray[i];
927  }
928 }
929 
930 
931 
932 template<class Scalar>
933 template<class ArraySum, class ArrayIn1, class ArrayIn2>
934 void RealSpaceTools<Scalar>::add(ArraySum & sumArray, const ArrayIn1 & inArray1, const ArrayIn2 & inArray2) {
935 #ifdef HAVE_INTREPID_DEBUG
936  TEUCHOS_TEST_FOR_EXCEPTION( ( (inArray1.rank() != inArray2.rank()) || (inArray1.rank() != sumArray.rank()) ),
937  std::invalid_argument,
938  ">>> ERROR (RealSpaceTools::add): Array arguments must have identical ranks!");
939  for (int i=0; i<inArray1.rank(); i++) {
940  TEUCHOS_TEST_FOR_EXCEPTION( ( (inArray1.dimension(i) != inArray2.dimension(i)) || (inArray1.dimension(i) != sumArray.dimension(i)) ),
941  std::invalid_argument,
942  ">>> ERROR (RealSpaceTools::add): Dimensions of array arguments do not agree!");
943  }
944 #endif
945 
946  for (int i=0; i<inArray1.size(); i++) {
947  sumArray[i] = inArray1[i] + inArray2[i];
948  }
949 }
950 
951 
952 
953 template<class Scalar>
954 template<class ArraySum, class ArrayIn>
955 void RealSpaceTools<Scalar>::add(ArraySum & inoutSumArray, const ArrayIn & inArray) {
956 #ifdef HAVE_INTREPID_DEBUG
957  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.rank() != inoutSumArray.rank() ),
958  std::invalid_argument,
959  ">>> ERROR (RealSpaceTools::add): Array arguments must have identical ranks!");
960  for (int i=0; i<inArray.rank(); i++) {
961  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.dimension(i) != inoutSumArray.dimension(i) ),
962  std::invalid_argument,
963  ">>> ERROR (RealSpaceTools::add): Dimensions of array arguments do not agree!");
964  }
965 #endif
966 
967  for (int i=0; i<inArray.size(); i++) {
968  inoutSumArray[i] += inArray[i];
969  }
970 }
971 
972 
973 
974 template<class Scalar>
975 void RealSpaceTools<Scalar>::subtract(Scalar* diffArray, const Scalar* inArray1, const Scalar* inArray2, const int size) {
976  for (int i=0; i<size; i++) {
977  diffArray[i] = inArray1[i] - inArray2[i];
978  }
979 }
980 
981 
982 
983 template<class Scalar>
984 void RealSpaceTools<Scalar>::subtract(Scalar* inoutDiffArray, const Scalar* inArray, const int size) {
985  for (int i=0; i<size; i++) {
986  inoutDiffArray[i] -= inArray[i];
987  }
988 }
989 
990 
991 
992 template<class Scalar>
993 template<class ArrayDiff, class ArrayIn1, class ArrayIn2>
994 void RealSpaceTools<Scalar>::subtract(ArrayDiff & diffArray, const ArrayIn1 & inArray1, const ArrayIn2 & inArray2) {
995 #ifdef HAVE_INTREPID_DEBUG
996  TEUCHOS_TEST_FOR_EXCEPTION( ( (inArray1.rank() != inArray2.rank()) || (inArray1.rank() != diffArray.rank()) ),
997  std::invalid_argument,
998  ">>> ERROR (RealSpaceTools::subtract): Array arguments must have identical ranks!");
999  for (int i=0; i<inArray1.rank(); i++) {
1000  TEUCHOS_TEST_FOR_EXCEPTION( ( (inArray1.dimension(i) != inArray2.dimension(i)) || (inArray1.dimension(i) != diffArray.dimension(i)) ),
1001  std::invalid_argument,
1002  ">>> ERROR (RealSpaceTools::subtract): Dimensions of array arguments do not agree!");
1003  }
1004 #endif
1005 
1006  for (int i=0; i<inArray1.size(); i++) {
1007  diffArray[i] = inArray1[i] - inArray2[i];
1008  }
1009 }
1010 
1011 
1012 
1013 template<class Scalar>
1014 template<class ArrayDiff, class ArrayIn>
1015 void RealSpaceTools<Scalar>::subtract(ArrayDiff & inoutDiffArray, const ArrayIn & inArray) {
1016 #ifdef HAVE_INTREPID_DEBUG
1017  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.rank() != inoutDiffArray.rank() ),
1018  std::invalid_argument,
1019  ">>> ERROR (RealSpaceTools::subtract): Array arguments must have identical ranks!");
1020  for (int i=0; i<inArray.rank(); i++) {
1021  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.dimension(i) != inoutDiffArray.dimension(i) ),
1022  std::invalid_argument,
1023  ">>> ERROR (RealSpaceTools::subtract): Dimensions of array arguments do not agree!");
1024  }
1025 #endif
1026 
1027  for (int i=0; i<inArray.size(); i++) {
1028  inoutDiffArray[i] -= inArray[i];
1029  }
1030 }
1031 
1032 
1033 
1034 
1035 template<class Scalar>
1036 void RealSpaceTools<Scalar>::scale(Scalar* scaledArray, const Scalar* inArray, const int size, const Scalar scalar) {
1037  for (int i=0; i<size; i++) {
1038  scaledArray[i] = scalar*inArray[i];
1039  }
1040 }
1041 
1042 
1043 
1044 template<class Scalar>
1045 void RealSpaceTools<Scalar>::scale(Scalar* inoutScaledArray, const int size, const Scalar scalar) {
1046  for (int i=0; i<size; i++) {
1047  inoutScaledArray[i] *= scalar;
1048  }
1049 }
1050 
1051 
1052 
1053 template<class Scalar>
1054 template<class ArrayScaled, class ArrayIn>
1055 void RealSpaceTools<Scalar>::scale(ArrayScaled & scaledArray, const ArrayIn & inArray, const Scalar scalar) {
1056 #ifdef HAVE_INTREPID_DEBUG
1057  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.rank() != scaledArray.rank() ),
1058  std::invalid_argument,
1059  ">>> ERROR (RealSpaceTools::scale): Array arguments must have identical ranks!");
1060  for (int i=0; i<inArray.rank(); i++) {
1061  TEUCHOS_TEST_FOR_EXCEPTION( ( inArray.dimension(i) != scaledArray.dimension(i) ),
1062  std::invalid_argument,
1063  ">>> ERROR (RealSpaceTools::scale): Dimensions of array arguments do not agree!");
1064  }
1065 #endif
1066 
1067  for (int i=0; i<inArray.size(); i++) {
1068  scaledArray[i] = scalar*inArray[i];
1069  }
1070 }
1071 
1072 
1073 
1074 template<class Scalar>
1075 template<class ArrayScaled>
1076 void RealSpaceTools<Scalar>::scale(ArrayScaled & inoutScaledArray, const Scalar scalar) {
1077  for (int i=0; i<inoutScaledArray.size(); i++) {
1078  inoutScaledArray[i] *= scalar;
1079  }
1080 }
1081 
1082 
1083 
1084 
1085 template<class Scalar>
1086 Scalar RealSpaceTools<Scalar>::dot(const Scalar* inArray1, const Scalar* inArray2, const int size) {
1087  Scalar dot(0);
1088  for (int i=0; i<size; i++) {
1089  dot += inArray1[i]*inArray2[i];
1090  }
1091  return dot;
1092 }
1093 
1094 
1095 
1096 template<class Scalar>
1097 template<class ArrayVec1, class ArrayVec2>
1098 Scalar RealSpaceTools<Scalar>::dot(const ArrayVec1 & inVec1, const ArrayVec2 & inVec2) {
1099 #ifdef HAVE_INTREPID_DEBUG
1100  TEUCHOS_TEST_FOR_EXCEPTION( ( (inVec1.rank() != 1) || (inVec2.rank() != 1) ),
1101  std::invalid_argument,
1102  ">>> ERROR (RealSpaceTools::dot): Vector arguments must have rank 1!");
1103  TEUCHOS_TEST_FOR_EXCEPTION( ( inVec1.dimension(0) != inVec2.dimension(0) ),
1104  std::invalid_argument,
1105  ">>> ERROR (RealSpaceTools::dot): Dimensions of vector arguments must agree!");
1106 #endif
1107 
1108  Scalar dot(0);
1109  for (int i=0; i<inVec1.size(); i++) {
1110  dot += inVec1[i]*inVec2[i];
1111  }
1112  return dot;
1113 
1114 }
1115 
1116 
1117 
1118 template<class Scalar>
1119 template<class ArrayDot, class ArrayVec1, class ArrayVec2>
1120 void RealSpaceTools<Scalar>::dot(ArrayDot & dotArray, const ArrayVec1 & inVecs1, const ArrayVec2 & inVecs2) {
1121 
1122  int arrayRank = inVecs1.rank();
1123 
1124 #ifdef HAVE_INTREPID_DEBUG
1125  TEUCHOS_TEST_FOR_EXCEPTION( ( arrayRank != dotArray.rank()+1 ),
1126  std::invalid_argument,
1127  ">>> ERROR (RealSpaceTools::dot): Ranks of norm and vector array arguments are incompatible!");
1128  TEUCHOS_TEST_FOR_EXCEPTION( ( arrayRank != inVecs2.rank() ),
1129  std::invalid_argument,
1130  ">>> ERROR (RealSpaceTools::dot): Ranks of input vector arguments must be identical!");
1131  TEUCHOS_TEST_FOR_EXCEPTION( ( (arrayRank < 2) || (arrayRank > 3) ),
1132  std::invalid_argument,
1133  ">>> ERROR (RealSpaceTools::dot): Rank of input vector arguments must be 2 or 3!");
1134  for (int i=0; i<arrayRank; i++) {
1135  TEUCHOS_TEST_FOR_EXCEPTION( ( inVecs1.dimension(i) != inVecs2.dimension(i) ),
1136  std::invalid_argument,
1137  ">>> ERROR (RealSpaceTools::dot): Dimensions of input vector arguments do not agree!");
1138  }
1139  for (int i=0; i<arrayRank-1; i++) {
1140  TEUCHOS_TEST_FOR_EXCEPTION( ( inVecs1.dimension(i) != dotArray.dimension(i) ),
1141  std::invalid_argument,
1142  ">>> ERROR (RealSpaceTools::dot): Dimensions of dot-product and vector arrays do not agree!");
1143  }
1144 #endif
1145 
1146  int dim_i0 = 1; // first index dimension (e.g. cell index)
1147  int dim_i1 = 1; // second index dimension (e.g. point index)
1148  int dim = inVecs1.dimension(arrayRank-1); // spatial dimension
1149 
1150  // determine i0 and i1 dimensions
1151  switch(arrayRank) {
1152  case 3:
1153  dim_i0 = inVecs1.dimension(0);
1154  dim_i1 = inVecs1.dimension(1);
1155  break;
1156  case 2:
1157  dim_i1 = inVecs1.dimension(0);
1158  break;
1159  }
1160 
1161  int offset_i0, offset, dotOffset;
1162  for (int i0=0; i0<dim_i0; i0++) {
1163  offset_i0 = i0*dim_i1;
1164  for (int i1=0; i1<dim_i1; i1++) {
1165  offset = offset_i0 + i1;
1166  dotOffset = offset;
1167  offset *= dim;
1168  Scalar dot(0);
1169  for (int i=0; i<dim; i++) {
1170  dot += inVecs1[offset+i]*inVecs2[offset+i];
1171  }
1172  dotArray[dotOffset] = dot;
1173  }
1174  }
1175 }
1176 
1177 
1178 
1179 template<class Scalar>
1180 void RealSpaceTools<Scalar>::matvec(Scalar* matVec, const Scalar* inMat, const Scalar* inVec, const int dim) {
1181  for (int i=0; i<dim; i++) {
1182  Scalar sumdot(0);
1183  for (int j=0; j<dim; j++) {
1184  sumdot += inMat[i*dim+j]*inVec[j];
1185  }
1186  matVec[i] = sumdot;
1187  }
1188 }
1189 
1190 
1191 
1192 template<class Scalar>
1193 template<class ArrayMatVec, class ArrayMat, class ArrayVec>
1194 void RealSpaceTools<Scalar>::matvec(ArrayMatVec & matVecs, const ArrayMat & inMats, const ArrayVec & inVecs) {
1195  int matArrayRank = inMats.rank();
1196 
1197 #ifdef HAVE_INTREPID_DEBUG
1198  TEUCHOS_TEST_FOR_EXCEPTION( ( matArrayRank != inVecs.rank()+1 ),
1199  std::invalid_argument,
1200  ">>> ERROR (RealSpaceTools::matvec): Vector and matrix array arguments do not have compatible ranks!");
1201  TEUCHOS_TEST_FOR_EXCEPTION( ( (matArrayRank < 3) || (matArrayRank > 4) ),
1202  std::invalid_argument,
1203  ">>> ERROR (RealSpaceTools::matvec): Rank of matrix array must be 3 or 4!");
1204  TEUCHOS_TEST_FOR_EXCEPTION( ( matVecs.rank() != inVecs.rank() ),
1205  std::invalid_argument,
1206  ">>> ERROR (RealSpaceTools::matvec): Vector arrays must be have the same rank!");
1207  for (int i=0; i<matArrayRank-1; i++) {
1208  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(i) != inVecs.dimension(i) ),
1209  std::invalid_argument,
1210  ">>> ERROR (RealSpaceTools::matvec): Dimensions of vector and matrix array arguments do not agree!");
1211  }
1212  for (int i=0; i<inVecs.rank(); i++) {
1213  TEUCHOS_TEST_FOR_EXCEPTION( ( matVecs.dimension(i) != inVecs.dimension(i) ),
1214  std::invalid_argument,
1215  ">>> ERROR (RealSpaceTools::matvec): Dimensions of vector array arguments do not agree!");
1216  }
1217  TEUCHOS_TEST_FOR_EXCEPTION( ( inMats.dimension(matArrayRank-2) != inMats.dimension(matArrayRank-1) ),
1218  std::invalid_argument,
1219  ">>> ERROR (RealSpaceTools::matvec): Matrices are not square!");
1220 #endif
1221 
1222  int dim_i0 = 1; // first index dimension (e.g. cell index)
1223  int dim_i1 = 1; // second index dimension (e.g. point index)
1224  int dim = inMats.dimension(matArrayRank-2); // spatial dimension
1225 
1226  // determine i0 and i1 dimensions
1227  switch(matArrayRank) {
1228  case 4:
1229  dim_i0 = inMats.dimension(0);
1230  dim_i1 = inMats.dimension(1);
1231  break;
1232  case 3:
1233  dim_i1 = inMats.dimension(0);
1234  break;
1235  }
1236 
1237  int offset_i0, offset, vecOffset;
1238 
1239  for (int i0=0; i0<dim_i0; i0++) {
1240  offset_i0 = i0*dim_i1;
1241  for (int i1=0; i1<dim_i1; i1++) {
1242  offset = offset_i0 + i1;
1243  vecOffset = offset*dim;
1244  offset = vecOffset*dim;
1245 
1246  for (int i=0; i<dim; i++) {
1247  Scalar sumdot(0);
1248  for (int j=0; j<dim; j++) {
1249  sumdot += inMats[offset+i*dim+j]*inVecs[vecOffset+j];
1250  }
1251  matVecs[vecOffset+i] = sumdot;
1252  }
1253  }
1254  }
1255 }
1256 
1257 
1258 template<class Scalar>
1259 template<class ArrayVecProd, class ArrayIn1, class ArrayIn2>
1260 void RealSpaceTools<Scalar>::vecprod(ArrayVecProd & vecProd, const ArrayIn1 & inLeft, const ArrayIn2 & inRight) {
1261 
1262 #ifdef HAVE_INTREPID_DEBUG
1263  /*
1264  * Check array rank and spatial dimension range (if applicable)
1265  * (1) all array arguments are required to have matching dimensions and rank: (D), (I0,D) or (I0,I1,D)
1266  * (2) spatial dimension should be 2 or 3
1267  */
1268  std::string errmsg = ">>> ERROR (RealSpaceTools::vecprod):";
1269 
1270  // (1) check rank range on inLeft and then compare the other arrays with inLeft
1271  TEUCHOS_TEST_FOR_EXCEPTION( !requireRankRange(errmsg, inLeft, 1,3), std::invalid_argument, errmsg);
1272  TEUCHOS_TEST_FOR_EXCEPTION( !requireDimensionMatch(errmsg, inLeft, inRight), std::invalid_argument, errmsg);
1273  TEUCHOS_TEST_FOR_EXCEPTION( !requireDimensionMatch(errmsg, inLeft, vecProd), std::invalid_argument, errmsg);
1274 
1275  // (2) spatial dimension ordinal = array rank - 1. Suffices to check only one array because we just
1276  // checked whether or not the arrays have matching dimensions.
1277  TEUCHOS_TEST_FOR_EXCEPTION( !requireDimensionRange(errmsg, inLeft, inLeft.rank() - 1, 2,3), std::invalid_argument, errmsg);
1278 
1279 #endif
1280 
1281  int spaceDim = inLeft.dimension(inLeft.rank() - 1);
1282 
1283  switch(inLeft.rank() ){
1284 
1285  case 1:
1286  {
1287  vecProd(0) = inLeft(1)*inRight(2) - inLeft(2)*inRight(1);
1288  vecProd(1) = inLeft(2)*inRight(0) - inLeft(0)*inRight(2);
1289  vecProd(2) = inLeft(0)*inRight(1) - inLeft(1)*inRight(0);
1290  }
1291  break;
1292 
1293  case 2:
1294  {
1295  int dim0 = inLeft.dimension(0);
1296  if(spaceDim == 3) {
1297  for(int i0 = 0; i0 < dim0; i0++){
1298  vecProd(i0, 0) = inLeft(i0, 1)*inRight(i0, 2) - inLeft(i0, 2)*inRight(i0, 1);
1299  vecProd(i0, 1) = inLeft(i0, 2)*inRight(i0, 0) - inLeft(i0, 0)*inRight(i0, 2);
1300  vecProd(i0, 2) = inLeft(i0, 0)*inRight(i0, 1) - inLeft(i0, 1)*inRight(i0, 0);
1301  }// i0
1302  } //spaceDim == 3
1303  else if(spaceDim == 2){
1304  for(int i0 = 0; i0 < dim0; i0++){
1305  // vecprod is scalar - do we still want result to be (i0,i1,D)?
1306  vecProd(i0, 0) = inLeft(i0, 0)*inRight(i0, 1) - inLeft(i0, 1)*inRight(i0, 0);
1307  }// i0
1308  }// spaceDim == 2
1309  }// case 2
1310  break;
1311 
1312  case 3:
1313  {
1314  int dim0 = inLeft.dimension(0);
1315  int dim1 = inLeft.dimension(1);
1316  if(spaceDim == 3) {
1317  for(int i0 = 0; i0 < dim0; i0++){
1318  for(int i1 = 0; i1 < dim1; i1++){
1319  vecProd(i0, i1, 0) = inLeft(i0, i1, 1)*inRight(i0, i1, 2) - inLeft(i0, i1, 2)*inRight(i0, i1, 1);
1320  vecProd(i0, i1, 1) = inLeft(i0, i1, 2)*inRight(i0, i1, 0) - inLeft(i0, i1, 0)*inRight(i0, i1, 2);
1321  vecProd(i0, i1, 2) = inLeft(i0, i1, 0)*inRight(i0, i1, 1) - inLeft(i0, i1, 1)*inRight(i0, i1, 0);
1322  }// i1
1323  }// i0
1324  } //spaceDim == 3
1325  else if(spaceDim == 2){
1326  for(int i0 = 0; i0 < dim0; i0++){
1327  for(int i1 = 0; i1 < dim1; i1++){
1328  // vecprod is scalar - do we still want result to be (i0,i1,D)?
1329  vecProd(i0, i1, 0) = inLeft(i0, i1, 0)*inRight(i0, i1, 1) - inLeft(i0, i1, 1)*inRight(i0, i1, 0);
1330  }// i1
1331  }// i0
1332  }// spaceDim == 2
1333  } // case 3
1334  break;
1335 
1336  default:
1337  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
1338  ">>> ERROR (RealSpaceTools::vecprod): rank-1,2,3 arrays required");
1339  }
1340 
1341 }
1342 
1343 } // namespace Intrepid
ENorm
Enumeration of norm types for vectors and functions.
static void matvec(Scalar *matVec, const Scalar *inMat, const Scalar *inVec, const int dim)
Matrix-vector left multiply using contiguous data: matVec = inMat * inVec.
bool requireDimensionMatch(std::string &errmsg, const Array1 &array1, const int a1_dim0, const Array2 &array2, const int a2_dim0)
Checks arrays for a single matching dimension.
bool requireDimensionRange(std::string &errmsg, const Array &array, const int dim, const int lowerBound, const int upperBound)
Checks if the specified array dimension is in the required range.
static void add(Scalar *sumArray, const Scalar *inArray1, const Scalar *inArray2, const int size)
Adds contiguous data inArray1 and inArray2 of size size: sumArray = inArray1 + inArray2.
static Scalar dot(const Scalar *inArray1, const Scalar *inArray2, const int size)
Computes dot product of contiguous data inArray1 and inArray2 of size size.
static void scale(Scalar *scaledArray, const Scalar *inArray, const int size, const Scalar scalar)
Multiplies contiguous data inArray of size size by a scalar (componentwise): scaledArray = scalar * ...
static void absval(Scalar *absArray, const Scalar *inArray, const int size)
Computes absolute value of contiguous input data inArray of size size.
static void transpose(Scalar *transposeMat, const Scalar *inMat, const int dim)
Computes transpose of the square matrix inMat of size dim by dim.
static Scalar vectorNorm(const Scalar *inVec, const int dim, const ENorm normType)
Computes norm (1, 2, infinity) of the vector inVec of size dim.
static void inverse(Scalar *inverseMat, const Scalar *inMat, const int dim)
Computes inverse of the square matrix inMat of size dim by dim.
bool requireRankRange(std::string &errmsg, const Array &array, const int lowerBound, const int upperBound)
Checks if the rank of the array argument is in the required range.
static void vecprod(ArrayVecProd &vecProd, const ArrayIn1 &inLeft, const ArrayIn2 &inRight)
Vector product using multidimensional arrays: vecProd = inVecLeft x inVecRight
static void subtract(Scalar *diffArray, const Scalar *inArray1, const Scalar *inArray2, const int size)
Subtracts contiguous data inArray2 from inArray1 of size size: diffArray = inArray1 - inArray2...
static Scalar det(const Scalar *inMat, const int dim)
Computes determinant of the square matrix inMat of size dim by dim.