GremlinEq
Tensors.h
1 //---------------------------------------------------------------------------
2 //
3 // $Id: Tensors.h,v 1.3 2019/01/24 20:03:19 sahughes Exp $
4 //
5 //---------------------------------------------------------------------------
6 //
7 // Class Tensor ... a container for defining multi-index array and tensor
8 // type objects in a numerical-recipes like fashion, but good for any
9 // type. Note all the methods are static functions, so you don't need
10 // create an instance of the class to call them.
11 //
12 // Example: To make a 4-index tensor of type Complex, with indices
13 // running (al, ah; bl, bh; cl, ch; dl, dh):
14 //
15 // Complex ****Z;
16 // Z = Tensor<Complex>::tensor4(al, ah, bl, bh, cl, ch, dl, dh);
17 //
18 // To free the memory associated with this:
19 // Tensor<Complex>::free_tensor4(Z, al, ah, bl, bh, cl, ch, dl, dh);
20 //
21 
22 #ifndef _Tensors_H
23 #define _Tensors_H
24 
25 #include "Globals.h"
26 
27 #define OFFSET 0
28 
29 template<class TypeHere>
30 
32 
33 class Tensor {
34  public:
35  static TypeHere *vector(const long al, const long ah)
36  {
37  TypeHere *t;
38  long da = ah - al + 1;
39 
40  t = (TypeHere *)malloc((size_t)((da + OFFSET)*sizeof(TypeHere)));
41 
42  return t - al + OFFSET;
43  }
44 
45  static void free_vector(TypeHere *t,
46  const long al, const long ah)
47  {
48  free(t + al - OFFSET);
49  }
50 
51  static TypeHere **vectorptr(const long al, const long ah)
52  {
53  TypeHere **t;
54  long da = ah - al + 1;
55 
56  t = (TypeHere **)malloc((size_t)((da + OFFSET)*sizeof(TypeHere)));
57 
58  return t - al + OFFSET;
59  }
60 
61  static void free_vectorptr(TypeHere **t,
62  const long al, const long ah)
63  {
64  free(t + al - OFFSET);
65  }
66 
67  static TypeHere **matrix(const long al, const long ah,
68  const long bl, const long bh)
69  {
70  TypeHere **t;
71  long i, da = ah - al + 1;
72 
73  t = (TypeHere **)malloc((size_t)(da + OFFSET)*sizeof(TypeHere*));
74  t += OFFSET;
75  t -= al;
76 
77  for (i = al; i <= ah; i++)
78  t[i] = vector(bl, bh);
79 
80  return t;
81  }
82 
83  static void free_matrix(TypeHere **t,
84  const long al, const long ah,
85  const long bl, const long bh)
86  {
87  long i;
88  for (i = ah; i >= al; i--)
89  free_vector(t[i], bl, bh);
90 
91  free(t + al - OFFSET);
92  }
93 
94  static TypeHere ***matrixptr(const long al, const long ah,
95  const long bl, const long bh)
96  {
97  TypeHere ***t;
98  long i, da = ah - al + 1;
99 
100  t = (TypeHere ***)malloc((size_t)(da + OFFSET)*sizeof(TypeHere**));
101  t += OFFSET;
102  t -= al;
103 
104  for (i = al; i <= ah; i++)
105  t[i] = vectorptr(bl, bh);
106 
107  return t;
108  }
109 
110  static void free_matrixptr(TypeHere ***t,
111  const long al, const long ah,
112  const long bl, const long bh)
113  {
114  long i;
115  for (i = ah; i >= al; i--)
116  free_vectorptr(t[i], bl, bh);
117 
118  free(t + al - OFFSET);
119  }
120 
121  static TypeHere ***tensor3(const long al, const long ah,
122  const long bl, const long bh,
123  const long cl, const long ch)
124  {
125  TypeHere ***t;
126  long i, da = ah - al + 1;
127 
128  t = (TypeHere ***)malloc((size_t)(da + OFFSET)*sizeof(TypeHere**));
129  t += OFFSET;
130  t -= al;
131 
132  for (i = al; i <= ah; i++)
133  t[i] = matrix(bl, bh, cl, ch);
134 
135  return t;
136  }
137 
138  static void free_tensor3(TypeHere ***t,
139  const long al, const long ah,
140  const long bl, const long bh,
141  const long cl, const long ch)
142  {
143  long i;
144  for (i = ah; i >= al; i--)
145  free_matrix(t[i], bl, bh, cl, ch);
146 
147  free(t + al - OFFSET);
148  }
149 
150  static TypeHere ****tensor4(const long al, const long ah,
151  const long bl, const long bh,
152  const long cl, const long ch,
153  const long dl, const long dh)
154  {
155  TypeHere ****t;
156  long i, da = ah - al + 1;
157 
158  t = (TypeHere ****)malloc((size_t)(da + OFFSET)*sizeof(TypeHere***));
159  t += OFFSET;
160  t -= al;
161 
162  for (i = al; i <= ah; i++)
163  t[i] = tensor3(bl, bh, cl, ch, dl, dh);
164 
165  return t;
166  }
167 
168  static void free_tensor4(TypeHere ****t,
169  const long al, const long ah,
170  const long bl, const long bh,
171  const long cl, const long ch,
172  const long dl, const long dh)
173  {
174  long i;
175  for (i = ah; i >= al; i--)
176  free_tensor3(t[i], bl, bh, cl, ch, dl, dh);
177 
178  free(t + al - OFFSET);
179  }
180 
181  static TypeHere *****tensor5(const long al, const long ah,
182  const long bl, const long bh,
183  const long cl, const long ch,
184  const long dl, const long dh,
185  const long el, const long eh)
186  {
187  TypeHere *****t;
188  long i, da = ah - al + 1;
189 
190  t = (TypeHere *****)malloc((size_t)(da + OFFSET)*sizeof(TypeHere****));
191  t += OFFSET;
192  t -= al;
193 
194  for (i = al; i <= ah; i++)
195  t[i] = tensor4(bl, bh, cl, ch, dl, dh, el, eh);
196 
197  return t;
198  }
199 
200  static void free_tensor5(TypeHere *****t,
201  const long al, const long ah,
202  const long bl, const long bh,
203  const long cl, const long ch,
204  const long dl, const long dh,
205  const long el, const long eh)
206  {
207  long i;
208  for (i = ah; i >= al; i--)
209  free_tensor4(t[i], bl, bh, cl, ch, dl, dh, el, eh);
210 
211  free(t + al - OFFSET);
212  }
213 
214  static TypeHere ******tensor6(const long al, const long ah,
215  const long bl, const long bh,
216  const long cl, const long ch,
217  const long dl, const long dh,
218  const long el, const long eh,
219  const long fl, const long fh)
220  {
221  TypeHere ******t;
222  long i, da = ah - al + 1;
223 
224  t = (TypeHere ******)malloc((size_t)(da + OFFSET)*sizeof(TypeHere*****));
225  t += OFFSET;
226  t -= al;
227 
228  for (i = al; i <= ah; i++)
229  t[i] = tensor5(bl, bh, cl, ch, dl, dh, el, eh, fl, fh);
230 
231  return t;
232  }
233 
234  static void free_tensor6(TypeHere ******t,
235  const long al, const long ah,
236  const long bl, const long bh,
237  const long cl, const long ch,
238  const long dl, const long dh,
239  const long el, const long eh,
240  const long fl, const long fh)
241  {
242  long i;
243  for (i = ah; i >= al; i--)
244  free_tensor5(t[i], bl, bh, cl, ch, dl, dh, el, eh, fl, fh);
245 
246  free(t + al - OFFSET);
247  }
248 
249  static TypeHere *******tensor7(const long al, const long ah,
250  const long bl, const long bh,
251  const long cl, const long ch,
252  const long dl, const long dh,
253  const long el, const long eh,
254  const long fl, const long fh,
255  const long gl, const long gh)
256  {
257  TypeHere *******t;
258  long i, da = ah - al + 1;
259 
260  t = (TypeHere *******)malloc((size_t)(da + OFFSET)*sizeof(TypeHere******));
261  t += OFFSET;
262  t -= al;
263 
264  for (i = al; i <= ah; i++)
265  t[i] = tensor6(bl, bh, cl, ch, dl, dh, el, eh, fl, fh, gl, gh);
266 
267  return t;
268  }
269 
270  static void free_tensor7(TypeHere *******t,
271  const long al, const long ah,
272  const long bl, const long bh,
273  const long cl, const long ch,
274  const long dl, const long dh,
275  const long el, const long eh,
276  const long fl, const long fh,
277  const long gl, const long gh)
278  {
279  long i;
280  for (i = ah; i >= al; i--)
281  free_tensor6(t[i], bl, bh, cl, ch, dl, dh, el, eh, fl, fh, gl, gh);
282 
283  free(t + al - OFFSET);
284  }
285 };
286 
287 #endif
Tensors Class.
Definition: Tensors.h:33