~daniele-bigoni/pyorthpol/pyorthpol-python3

« back to all changes in this revision

Viewing changes to PyORTHPOL/ORTHPOLxx/build/include/liborthpol-1.0/ORTHPOLxx/f77matrix.h

  • Committer: Daniele Bigoni
  • Date: 2014-12-16 10:43:03 UTC
  • Revision ID: dabi@dtu.dk-20141216104303-ba4utrt7ln5g39lq
started with launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <assert.h>
 
2
 
 
3
/*
 
4
  class FMATRIX
 
5
  =============
 
6
  A minimal class used when passing multi-dimensional array
 
7
  arguments from C++ to FORTRAN 77 (received as FORTRAN arrays),
 
8
  and subsequently returned back to C++ as properly aranged
 
9
  C++ arrays.
 
10
 
 
11
  Problem : FORTRAN organises data in a "column-first" order,
 
12
            while C++ organises data in a "row-first" order.
 
13
 
 
14
  Solution:
 
15
       (1)  The FMATRIX class can take a C++ array as a constructor
 
16
        parameter. A FORTRAN compatible copy of the array is
 
17
        then made. The destructor will then copy the result back
 
18
        to the original c++ array.
 
19
 
 
20
     (2)  The FMATRIX class provides "subscript operators" allowing
 
21
        the programmer to read and write from the array, using
 
22
        FORTRAN-like syntax and indexing semantics.
 
23
 
 
24
  Author: Carsten A. Arnholm, 04-MAR-1996
 
25
*/
 
26
 
 
27
template <class T>
 
28
class FMATRIX {
 
29
public:
 
30
   FMATRIX(size_t dim1, size_t dim2);
 
31
   FMATRIX(T* cpparr, size_t dim1, size_t dim2);
 
32
   operator T*();
 
33
   T& operator()(size_t index1, size_t index2);
 
34
  ~FMATRIX();
 
35
public:
 
36
   const size_t ndim;  // number of array dimensions
 
37
   size_t dim[7];      // size of each dimension
 
38
   T*  cpprep;         // original c++ array
 
39
   T*  f77rep;         // array used by FORTRAN
 
40
};
 
41
 
 
42
template <class T>
 
43
FMATRIX<T>::FMATRIX(size_t dim1, size_t dim2)
 
44
: cpprep(NULL),f77rep(new T[dim1*dim2]),ndim(2)
 
45
{
 
46
   dim[0]=dim1;
 
47
   dim[1]=dim2;
 
48
   dim[2]=0;
 
49
   dim[3]=0;
 
50
   dim[4]=0;
 
51
   dim[5]=0;
 
52
   dim[6]=0;
 
53
}
 
54
 
 
55
template <class T>
 
56
FMATRIX<T>::FMATRIX(T* cpparr, size_t dim1, size_t dim2)
 
57
: cpprep(cpparr),f77rep(new T[dim1*dim2]),ndim(2)
 
58
{
 
59
   dim[0]=dim1;
 
60
   dim[1]=dim2;
 
61
   dim[2]=0;
 
62
   dim[3]=0;
 
63
   dim[4]=0;
 
64
   dim[5]=0;
 
65
   dim[6]=0;
 
66
 
 
67
   // make a FORTRAN-compatible copy of the array
 
68
   size_t index_cpp=0;
 
69
   size_t index_f77;
 
70
   for(size_t i=0;i<dim[0];i++) {
 
71
      for(size_t j=0;j<dim[1];j++) {
 
72
       index_f77 = j*dim[0] + i;
 
73
       f77rep[index_f77] = cpprep[index_cpp++];
 
74
    }
 
75
   }
 
76
}
 
77
 
 
78
template <class T>
 
79
FMATRIX<T>::operator T*()
 
80
{
 
81
   // Pass the FORTRAN representation when calling a function
 
82
   return f77rep;
 
83
}
 
84
 
 
85
template <class T>
 
86
T&  FMATRIX<T>::operator()(size_t index1, size_t index2)
 
87
{
 
88
   assert(ndim==2);  // only 2d arrays supported (so far)
 
89
 
 
90
   // indexing according to F77 conventions
 
91
   size_t index_f77 = index2*dim[0] + index1;
 
92
 
 
93
   // return a reference to the array element
 
94
   return *(f77rep+index_f77);
 
95
}
 
96
 
 
97
template <class T>
 
98
FMATRIX<T>::~FMATRIX()
 
99
{
 
100
   if(cpprep) {
 
101
      assert(ndim==2);  // only 2d arrays supported (so far)
 
102
 
 
103
      // copy back from FORTRAN to C++ array
 
104
      size_t index_cpp;
 
105
      size_t index_f77=0;
 
106
      for(size_t j=0;j<dim[1];j++) {
 
107
         for(size_t i=0;i<dim[0];i++) {
 
108
            index_cpp = i*dim[1] + j;
 
109
            cpprep[index_cpp] = f77rep[index_f77++];
 
110
         }
 
111
      }
 
112
   }
 
113
 
 
114
   // delete the FORTRAN copy of the arry
 
115
   delete[] f77rep;
 
116
}