File: genstencils.py

package info (click to toggle)
blitz++ 1:1.0.2+ds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,568 kB
  • sloc: cpp: 57,803; python: 1,941; fortran: 1,510; f90: 852; makefile: 833; sh: 321
file content (1464 lines) | stat: -rw-r--r-- 59,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
#!/usr/bin/python3

# Generates stencil code. This replaces the macros in stencil-et.h,
# which make it impossible to debug the generated code.

from __future__ import print_function

import sys

os=open(sys.argv[1],'w')
print("Generating file %s"%sys.argv[1])

def BZ_ET_STENCIL_REDIRECT(name):
    stub="""


/* Explicit operators for arrays for stencil name. */
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
#name#(const Array<T,N>& d1)
{ return #name#(d1.wrap()); }

template<typename T, int N>
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
#name#(Array<T,N>& d1)
{ return #name#(d1.wrap()); }
"""
    os.write(stub.replace("#name#",name))

def BZ_ET_STENCIL(name, result, etresult, MINB, MAXB):
    stub="""


/** Defines a stencil ET "#name#" that operates on an Array<P_numtype, N_rank>
   and specifies the return type as Array<#result#, N_rank>. The result
   type is used when running on an array and the etresult type when
   running on an expression. The extent of the stencil is MINB-MAXB.
   If you want to refer to the native type
   of the expression, set result="P_numtype" and etresult="typename
   T1::T_numtype". Sorry for that ugliness, but they define types
   differently. The stencil ET calls the stencil operator
   name_stencilop, defined in stencilops.h. **/

  template<typename P_expr, _bz_typename P_numtype>			
  class #name#_et : public _bz_StencilExpr<P_expr, P_numtype>		
  {									
  public:								
    typedef _bz_StencilExpr<P_expr, P_numtype> T_base;			
    typedef _bz_typename T_base::T_numtype T_numtype;			
    typedef _bz_typename T_base::T_expr T_expr;				

  // if P_numtype is an ET-type, we need to return an expr
  typedef typename selectET<P_numtype,
			    T_numtype, 
      ETBase<_bz_ArrayExpr<_bz_ArrayExprConstant<P_numtype> > > >::T_selected T_typeprop;
  typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
  typedef T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et<
      typename T_expr::template tvresult<N>::Type,
      T_numtype> Type;
  };

    typedef  #name#_et<_bz_typename P_expr::T_range_result, T_numtype> T_range_result; 
									
    using T_base::iter_;						
    using T_base::rank_;							
  public:								
    #name#_et(const #name#_et& a) :					
    _bz_StencilExpr<P_expr, T_numtype>(a)				
      { }								
									
    #name#_et(BZ_ETPARM(T_expr) a) :					
    _bz_StencilExpr<P_expr, T_numtype>(a)				
      { }								
									
    #name#_et(_bz_typename T_expr::T_ctorArg1 a) :			
    _bz_StencilExpr<P_expr, T_numtype>(a)				
      { }								
									
    T_result operator*() const						
    { return #name#_stencilop(iter_); }						
									
    /* this is not really const, because we don't undo the moveTo, but	
       that should not be visible to outside. It would be if we used	
       some kind of mixed index and stack traversal, but that will	
       screw things up, const or not. */				
    template<int N_rank2>						
      T_result operator()(const TinyVector<int, N_rank2>& i) const	
    { iter_.moveTo(i); return #name#_stencilop(iter_); }    									
    T_range_result operator()(const RectDomain<rank_>& d) const		
    { return T_range_result(iter_(d)); }				
									
    T_result operator[](int i) const					
    { return #name#_stencilop(iter_[i]); }									
    T_result fastRead(diffType i) const				
    {/* this probably isn't very fast... */				
      iter_._bz_offsetData(i);						
      T_result r = #name#_stencilop(iter_);					
      iter_._bz_offsetData(-i);						
      return r;								
    }									

    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return iter_.template fastRead_tv<N>(i); }
      
    T_result shift(int offset, int dim) const				
    {									
      iter_._bz_offsetData(offset, dim);				
      T_result r = #name#_stencilop(iter_);					
      iter_._bz_offsetData(-offset, dim);				
      return r;								
    }									
									
    T_result shift(int offset1, int dim1, int offset2, int dim2) const	
    {									
      iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
      T_result r = #name#_stencilop (iter_);					
      iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
      return r;								
    }									
									
    void prettyPrint(std::string &str,				
		     prettyPrintFormat& format) const			
    {									
      str += "#name# (stencil)";						
      str += "(";							
      iter_.prettyPrint(str, format);					
      str += ")";							
    }									
    									
    template<typename T1, typename T2 = nilArraySection,		
      class T3 = nilArraySection, typename T4 = nilArraySection,	
      class T5 = nilArraySection, typename T6 = nilArraySection,	
      class T7 = nilArraySection, typename T8 = nilArraySection,	
      class T9 = nilArraySection, typename T10 = nilArraySection,	
      class T11 = nilArraySection>					
      class SliceInfo {							
      public:								
      typedef #name#_et<T_expr, T_numtype> T_slice;			
      };								
    									
    template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
      typename T7, typename T8, typename T9, typename T10, typename T11> 
      #name#_et							
      operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
    {									
    /* because stencils work inherently in several dimensions it's	
       complicated to slice the domain. slices will be changed to unit	
        ranges instead. slicing stencil result thus *never* changes	
       the rank of the expression, unlike the normal case. */		
      return #name#_et						
	(iter_(_bz_makeRange(r1),					
	       _bz_makeRange(r2),					
	       _bz_makeRange(r3),					
	       _bz_makeRange(r4),					
	       _bz_makeRange(r5),					
	       _bz_makeRange(r6),					
	       _bz_makeRange(r7),					
	       _bz_makeRange(r8),					
	       _bz_makeRange(r9),					
	       _bz_makeRange(r10),					
	       _bz_makeRange(r11)));					
    }									
									
  };									
  /* generate an ET object from an expression */			
  template<typename T1>							
  inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result, #etresult#> > 
  #name#(const blitz::ETBase<T1>& d1)				
  {									
    return _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result, #etresult#> > 
      (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB))); 
  }									
  /* redirect calls with bare arrays to the main function */		
  template<typename T, int N>						
  inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> > 
  #name#(const Array<T,N>& d1)						
  { return #name#(d1.wrap()); }						
									
  template<typename T, int N>						
  inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> > 
  #name#(Array<T,N>& d1)							
   { return #name#(d1.wrap()); }"""
    os.write(stub.replace("#name#",name).replace("#result#",result).replace("#etresult#",etresult).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))


def BZ_ET_STENCIL2(name, result, etresult, MINB, MAXB):
    stub="""


/** Defines a stencil "#name#" ET that operates on two arrays of arbitrary type
   and specifies the return type as array<#result#, N_rank>. The result
   type is used when running on an array and the etresult type when
   running on an expression. If you want to refer to the native type
   of the expression, set result="P_numtype" and etresult="typename
   T1::T_numtype". Sorry for that ugliness, but they define types
   differently. */
template<typename P_expr1, typename P_expr2, _bz_typename P_numtype>	
class #name#_et2 : public _bz_StencilExpr2<P_expr1, P_expr2, P_numtype> 
{									
public:								
  typedef _bz_StencilExpr2<P_expr1, P_expr2, P_numtype> T_base;	
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr1 T_expr1;			
  typedef _bz_typename T_base::T_expr2 T_expr2;			

  // if P_numtype is an ET-type, we need to return an expr
  typedef typename selectET<P_numtype,
			    T_numtype, 
      ETBase<_bz_ArrayExpr<_bz_ArrayExprConstant<P_numtype> > > >::T_selected T_typeprop;
  typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
  typedef T_numtype T_optype;

  /// dummy
  template<int N> struct tvresult {
    typedef #name#_et2<
      typename T_expr1::template tvresult<N>::Type,
      typename T_expr2::template tvresult<N>::Type,
      T_numtype> Type; 
  };

  typedef  #name#_et2<_bz_typename P_expr1::T_range_result, _bz_typename P_expr2::T_range_result, T_numtype> T_range_result; 
									
  using T_base::iter1_;						
  using T_base::iter2_;						
  using T_base::rank_;							
public:								
  #name#_et2(const #name#_et2& a) :				
  _bz_StencilExpr2<P_expr1, P_expr2, T_numtype>(a)			
  { }								
									
  #name#_et2(BZ_ETPARM(T_expr1) a, BZ_ETPARM(T_expr2) b) :		
  _bz_StencilExpr2<P_expr1, P_expr2, T_numtype>(a, b)		
  { }								

  T_result operator*() const						
  { return #name#_stencilop(iter1_, iter2_); }					
									
  T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter1_.moveTo(i); iter2_.moveTo(i);
    return #name#_stencilop(iter1_, iter2_); } 
									
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter1_(d), iter2_(d)); }			
									
  T_result operator[](int i) const					
  { return #name#_stencilop(iter1_[i], iter2_[i]); }				
									
  T_result fastRead(diffType i) const					
  {/* this probably isn't very fast... */				
    iter1_._bz_offsetData(i); iter2_._bz_offsetData(i);		
    T_result r = #name#_stencilop (iter1_, iter2_);				
    iter1_._bz_offsetData(-i); iter2_._bz_offsetData(-i);		
    return r;								
  }									

    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return typename tvresult<N>::Type(iter1_.fastRead_tv<N>(i),
					iter2_.fastRead_tv<N>(i)); }
  
  T_result shift(int offset, int dim) const				
  {									
    iter1_._bz_offsetData(offset, dim);				
    iter2_._bz_offsetData(offset, dim);				
    T_result r = #name#_stencilop (iter1_, iter2_);				
    iter1_._bz_offsetData(-offset, dim);				
    iter2_._bz_offsetData(-offset, dim);				
    return r;								
  }									
									
  T_result shift(int offset1, int dim1, int offset2, int dim2) const	
  {									
    iter1_._bz_offsetData(offset1, dim1, offset2, dim2);		
    iter2_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_result r = #name#_stencilop (iter1_, iter2_);				
    iter1_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    iter2_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;								
  }									
									
  void prettyPrint(std::string &str,				
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter1_.prettyPrint(str, format);					
    str += ", ";							
    iter2_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et2<T_expr1, T_expr2, T_numtype> T_slice;	
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et2							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et2						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)));					
  }									
};									
									
/* create ET object from application to expression */			
template<typename T1, typename T2>					
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #etresult#> > 
#name#(const blitz::ETBase<T1>& d1,				
     const blitz::ETBase<T2>& d2)				
{									
  return _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #etresult#> > 
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB)), 
     blitz::asExpr<T2>::getExpr(d2.unwrap())(_bz_shrinkDomain(d2.unwrap().domain(),MINB, MAXB))); 
}									
/* matches to calls involving bare arrays (this is very annoying	
   because we have to exactly match every possible call combination	
   to ensure that this matches instead of the operator in		
   stencilops.h) */							
template<typename T1, typename T2, int N2>				
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> > 
#name#(const blitz::ETBase<T1>& d1, Array<T2,N2>& d2)		
{ return #name#(d1.wrap(), d2.wrap()); }				
									
template<typename T1, typename T2, int N2>				
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> > 
#name#(const blitz::ETBase<T1>& d1, const Array<T2,N2>& d2)	
{ return #name#(d1.wrap(), d2.wrap()); }				
									
template<typename T1, int N1, typename T2>				
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #result#> > 
#name#(Array<T1,N1>& d1, const blitz::ETBase<T2>& d2)		
{ return #name#(d1.wrap(), d2.wrap()); }				
									
template<typename T1, int N1, typename T2>				
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #result#> > 
#name#(const Array<T1,N1>& d1, const blitz::ETBase<T2>& d2)	
{ return #name#(d1.wrap(), d2.wrap()); }				
									
template<typename T1, int N1, typename T2, int N2>			
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> > 
#name#(const Array<T1,N1>& d1, Array<T2,N2>& d2)				
{ return #name#(d1.wrap(), d2.wrap()); }				
									
template<typename T1, int N1, typename T2, int N2>			
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> > 
#name#(Array<T1,N1>& d1, const Array<T2,N2>& d2)			
{ return #name#(d1.wrap(), d2.wrap()); }				
  									
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> > 
#name#(Array<T,N>& d1, Array<T,N>& d2)					
{ return #name#(d1.wrap(), d2.wrap()); }				
									
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> > 
#name#(const Array<T,N>& d1, const Array<T,N>& d2)			
{ return #name#(d1.wrap(), d2.wrap()); }"""
    os.write(stub.replace("#name#",name).replace("#result#",result).replace("#etresult#",etresult).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))

def BZ_ET_STENCILM(name, result_rank, MINB, MAXB):
    stub="""


/* Defines a stencil ET "#name#" that operates on an array
   array<P_numtype, N_rank> and returns a multicomponent
   array<TinyMatrix<P_numtype::T_element, rank, rank> >,
   N_rank>. P_numtype can be a TinyVector or a scalar, I think. */

template<typename P_expr>						
class #name#_et : public _bz_StencilExpr<P_expr, TinyMatrix<_bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element, result_rank, result_rank> > 
{									
public:									
  typedef _bz_StencilExpr<P_expr, TinyMatrix<_bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element, result_rank, result_rank> > T_base; 
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr T_expr;				

  // there is no return type selection, as we are returning a
  // TinyMatrix. This must be returned as a FastTMCopyIterator since the
  // output of the stencil operator is a temporary.
  typedef ETBase<_bz_ArrayExpr<FastTM2CopyIterator<typename multicomponent_traits<typename P_expr::T_numtype>::T_element, result_rank, result_rank> > > T_typeprop;
  typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
  typedef T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et<
      typename T_expr::template tvresult<N>::Type> Type;
  };

  typedef  #name#_et<_bz_typename P_expr::T_range_result> T_range_result; 
									 
  using T_base::iter_;						
  using T_base::rank_;						
public:								
  #name#_et(const #name#_et& a) :				
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
     									
  #name#_et(BZ_ETPARM(T_expr) a) :					
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
									 
  #name#_et(_bz_typename T_expr::T_ctorArg1 a) :			
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
     									
  T_result operator*() const					
  { return #name#_stencilop(iter_); }						

  T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter_.moveTo(i); return #name#_stencilop(iter_); }				
     									
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter_(d)); }				
     									
  T_result operator[](int i) const					
  { return #name#_stencilop(iter_[i]); }						
     									
  T_result fastRead(diffType i) const				
  {/* this probably isn't very fast... */				
    iter_._bz_offsetData(i);						
    T_result r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-i);					
    return r;							
  }									

    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return iter_.template fastRead_tv<N>(i); }
     									
  T_result shift(int offset, int dim) const				
  {									
    iter_._bz_offsetData(offset, dim);				
    T_result r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset, dim);				
    return r;							
  }									
									 
  T_result shift(int offset1, int dim1, int offset2, int dim2) const 
  {									
    iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_result r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;							
  }									
									 
  void prettyPrint(std::string &str,			
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et<T_expr> T_slice;				
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)));					
  }									
};									
/* create ET from application to expression */			
template<typename T1>						
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> > 
#name#(const blitz::ETBase<T1>& d1)				
{									
  return _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >	
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB))); 
}								
"""
    os.write(stub.replace("#name#",name).replace("result_rank",str(result_rank)).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
    BZ_ET_STENCIL_REDIRECT(name)						

def BZ_ET_STENCILV(name, result_rank, MINB, MAXB):
    stub="""


/* Defines a stencil ET "#name#" that operates on a (scalar) array<P_numtype,
   N_rank> and returns a multicomponent
   array<TinyVector<P_numtype::T_element, result_rank> >, N_rank>. */

template<typename P_expr>						
class #name#_et : public _bz_StencilExpr<P_expr, TinyVector<typename P_expr::T_numtype,result_rank> > 
{									
public:									
  typedef _bz_StencilExpr<P_expr, TinyVector<typename P_expr::T_numtype,result_rank> > T_base; 
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr T_expr;				

  // there is no return type selection, we assume P_numtype is scalar
  // and that we are returning a TinyVector. This needs to be returned
  // as a FastTVCopyIterator that keeps a copy of the TV it is
  // iterating over, since the result of the stencil operator is a temporary.
  typedef ETBase<_bz_ArrayExpr<FastTV2CopyIterator<typename P_expr::T_numtype, result_rank> > > T_typeprop;
  typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
  typedef typename T_expr::T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et<
      typename T_expr::template tvresult<N>::Type> Type;
  };

  typedef  #name#_et<_bz_typename P_expr::T_range_result> T_range_result; 
									 
  using T_base::iter_;						
  using T_base::rank_;							
public:								
  #name#_et(const #name#_et& a) :					
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
									 
  #name#_et(BZ_ETPARM(T_expr) a) :					
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
									 
  #name#_et(_bz_typename T_expr::T_ctorArg1 a) :			
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
									 
  T_result operator*() const					
  { return #name#_stencilop(iter_); }						

  T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter_.moveTo(i); return #name#_stencilop(iter_); }				
									 
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter_(d)); }				
									 
  T_result operator[](int i) const					
  { return #name#_stencilop(iter_[i]); }						
									 
  T_result fastRead(diffType i) const				
  {/* this probably isn't very fast... */				
    iter_._bz_offsetData(i);						
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-i);					
    return r;							
  }									
									 
    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return iter_.template fastRead_tv<N>(i); }

  T_numtype shift(int offset, int dim) const				
  {									
    iter_._bz_offsetData(offset, dim);				
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset, dim);				
    return r;								
  }									
									 
  T_numtype shift(int offset1, int dim1, int offset2, int dim2) const 
  {									
    iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;								
  }									
									 
  void prettyPrint(std::string &str,				
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et<T_expr> T_slice;				
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)));					
  }									
};									
/* create ET from application to expression */				
template<typename T1>							
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >	
#name#(const blitz::ETBase<T1>& d1)				
{									
  return _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >	
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB))); 
}									
"""
    os.write(stub.replace("#name#",name).replace("result_rank",str(result_rank)).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
    BZ_ET_STENCIL_REDIRECT(name)						


def BZ_ET_STENCIL_SCA(name, MINB, MAXB):
    stub="""


/** Defines a stencil ET "#name#" that operates on a multicomponent
   array<P_numtype, N_rank> and returns a scalar
   array<P_numtype::T_element, N_rank>. */

template<typename P_expr>						
class #name#_et : public _bz_StencilExpr<P_expr, _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element> 
{									
public:									
  typedef _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element T_result; 
  typedef _bz_StencilExpr<P_expr, T_result> T_base;			
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr T_expr;				

  // there is no selecting return type here. because we *know* it is
  // scalar T_result, there's no question of whether we could be doing
  // multicomponent evaluations.    
  typedef T_result T_typeprop;
  typedef T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et<
      typename T_expr::template tvresult<N>::Type> Type;
  };

typedef  #name#_et<_bz_typename P_expr::T_range_result> T_range_result; 
									 
  using T_base::iter_;						
  using T_base::rank_;							
public:								
  #name#_et(const #name#_et& a) :					
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
									 
  #name#_et(BZ_ETPARM(T_expr) a) :					
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
									 
  #name#_et(_bz_typename T_expr::T_ctorArg1 a) :			
  _bz_StencilExpr<P_expr, T_numtype>(a)				
  { }								
    									
  T_numtype operator*() const						
  { return #name#_stencilop(iter_); }						
  T_numtype operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter_.moveTo(i); return #name#_stencilop(iter_); }				
									
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter_(d)); }				
									
  T_numtype operator[](int i) const					
  { return #name#_stencilop(iter_[i]); }						
									
  T_numtype fastRead(diffType i) const				
  {/* this probably isn't very fast... */				
    iter_._bz_offsetData(i);						
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-i);						
    return r;								
  }									
									
    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return iter_.template fastRead_tv<N>(i); }

  T_numtype shift(int offset, int dim) const				
  {									
    iter_._bz_offsetData(offset, dim);				
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset, dim);				
    return r;								
  }									
									
  T_numtype shift(int offset1, int dim1, int offset2, int dim2) const	
  {									
    iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;								
  }									
									
  void prettyPrint(std::string &str,				
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et<T_expr> T_slice;				
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)));					
  }									
};									
/* create ET from application to expression */			
template<typename T1>							
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >	
#name#(const blitz::ETBase<T1>& d1)				
{									
  return _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >	
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB))); 
}									
"""
    os.write(stub.replace("#name#",name).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
    BZ_ET_STENCIL_REDIRECT(name)						


def BZ_ET_STENCIL_DIFF(name, MINB, MAXB):
    stub="""


/* Defines a stencil ET difference operator "#name#" that operates on
   an array<P_numtype, N_rank> and returns an array of identical
   type. (The only significance of the "difference" aspect is that the
   operator is assumed to take a second argument which is the
   dimension to do the difference in). MINB and MAXB are integer
   expressions describing the extent of the operator in the operating
   dimension. */

template<typename P_expr>						
class #name#_et : public _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype>	
{									
public:								
  typedef _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype> T_base; 
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr T_expr;				

    // select return type
  typedef typename unwrapET<typename T_expr::T_result>::T_unwrapped test;
  typedef typename selectET<typename T_expr::T_typeprop, 
			    T_numtype, 
			    #name#_et<test> >::T_selected T_typeprop;
  typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
  typedef T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et<
      typename T_expr::template tvresult<N>::Type> Type;
  };

typedef  #name#_et<_bz_typename P_expr::T_range_result> T_range_result; 
									
  using T_base::iter_;						
  using T_base::rank_;							
public:								
  #name#_et(const #name#_et& a) :				
  _bz_StencilExpr<P_expr, T_numtype>(a), dim_(a.dim_)	
  { }								
									
  #name#_et(BZ_ETPARM(T_expr) a, int dim) :			
  _bz_StencilExpr<P_expr, T_numtype>(a), dim_(dim)		
  { }								
									
  #name#_et(_bz_typename T_expr::T_ctorArg1 a, int dim) :	
  _bz_StencilExpr<P_expr, T_numtype>(a), dim_(dim)		
  { }								
									
  T_result operator*() const						
  { return #name#_stencilop(iter_, dim_); }					
  T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter_.moveTo(i); return #name#_stencilop(iter_, dim_); }			
									
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter_(d), dim_); }				
									
  T_result operator[](int i) const					
  { return #name#_stencilop(iter_[i], dim_); }					
									
  T_result fastRead(diffType i) const				
  {/* this probably isn't very fast... */				
    iter_._bz_offsetData(i);						
    T_result r = #name#_stencilop (iter_, dim_);					
    iter_._bz_offsetData(-i);						
    return r;								
  }									
									
    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return typename tvresult<N>::Type(iter_.template fastRead_tv<N>(i),dim_); }

  T_result shift(int offset, int dim) const				
  {									
    iter_._bz_offsetData(offset, dim);				
    T_result r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset, dim);				
    return r;								
  }									
									
  T_result shift(int offset1, int dim1, int offset2, int dim2) const	
  {									
    iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_result r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;								
  }									
									
  void prettyPrint(std::string &str,				
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et<T_expr> T_slice;				
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)),dim_);				
  }									
									
private:								
  int dim_;								
};									
/* create ET from application to expression */				
template<typename T1>							
inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >	
#name#(const blitz::ETBase<T1>& d1, int dim)			
{									
  TinyVector<int, blitz::asExpr<T1>::T_expr::rank_> minb(0), maxb(0); 
  minb[dim]=MINB; maxb[dim]=MAXB;					
  return _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >	
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),minb, maxb)), dim); 
}									
/* forward operations on arrays to main function */			
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> > 
#name#(const Array<T,N>& d1, int dim)					
{ return #name#(d1.wrap(), dim); }					
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> > 
#name#(Array<T,N>& d1, int dim)						
{ return #name#(d1.wrap(), dim); }
"""
    os.write(stub.replace("#name#",name).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))


def BZ_ET_STENCIL_MULTIDIFF(name, MINB, MAXB):
    stub="""


/** Defines a stencil ET difference operator "#name#" that operates on a
   multicomponent array<P_numtype, N_rank> and returns an
   array<P_numtype::T_element, N_rank>. */
template<typename P_expr>						
class #name#_et_multi : public _bz_StencilExpr<P_expr, _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element> 
{									
public:								
  typedef _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element T_result; 
  typedef _bz_StencilExpr<P_expr, T_result> T_base;		
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr T_expr;				

  // there is no selecting return type here. because we *know* it is
  // T_result, there's no question of whether we could be doing
   // multicomponent evaluations.    
  typedef T_result T_typeprop;
  typedef T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et_multi<
      typename T_expr::template tvresult<N>::Type> Type;
  };

typedef  #name#_et_multi<_bz_typename P_expr::T_range_result> T_range_result; 
									
  using T_base::iter_;						
  using T_base::rank_;							
public:								
  #name#_et_multi(const #name#_et_multi& a) :		
  _bz_StencilExpr<P_expr, T_numtype>(a), comp_(a.comp_), dim_(a.dim_) 
  { }								

  #name#_et_multi(BZ_ETPARM(T_expr) a, int comp, int dim) :	
  _bz_StencilExpr<P_expr, T_numtype>(a),			
    comp_(comp), dim_(dim)						
  { }								
									
  #name#_et_multi(_bz_typename T_expr::T_ctorArg1 a, int comp, int dim) : 
  _bz_StencilExpr<P_expr, T_numtype>(a),			
    comp_(comp), dim_(dim)						
  { }								
									
  T_numtype operator*() const						
  { return #name#_stencilop(iter_, comp_, dim_); }				
  T_numtype operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter_.moveTo(i); return #name#_stencilop(iter_, comp_, dim_); }		
									
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter_(d), comp_, dim_); }			
									
  T_numtype operator[](int i) const					
  { return #name#_stencilop(iter_[i], comp_, dim_); }				
									
  T_numtype fastRead(diffType i) const				
  {/* this probably isn't very fast... */				
    iter_._bz_offsetData(i);						
    T_numtype r = #name#_stencilop (iter_, comp_, dim_);				
    iter_._bz_offsetData(-i);						
    return r;								
  }									
									
    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return typename tvresult<N>::Type(iter_.template fastRead_tv<N>(i),comp_,dim_); }

  T_numtype shift(int offset, int dim) const				
  {									
    iter_._bz_offsetData(offset, dim);				
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset, dim);				
    return r;								
  }									
									
  T_numtype shift(int offset1, int dim1, int offset2, int dim2) const	
  {									
    iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;								
  }									
									
  void prettyPrint(std::string &str,				
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et_multi<T_expr> T_slice;				
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et_multi							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et_multi						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)),comp_, dim_);			
  }									
									
private:								
  int comp_;								
  int dim_;								
};									
/* create ET from application to expression */			
template<typename T1>							
inline _bz_ArrayExpr<#name#_et_multi<typename blitz::asExpr<T1>::T_expr::T_range_result> >	
#name#(const blitz::ETBase<T1>& d1, int comp, int dim)		
{									
  TinyVector<int, blitz::asExpr<T1>::T_expr::rank_> minb(0), maxb(0);	
  minb[dim]=MINB; maxb[dim]=MAXB;					
  return _bz_ArrayExpr<#name#_et_multi<typename blitz::asExpr<T1>::T_expr::T_range_result> > 
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),minb, maxb)), comp, dim); 
}									
/* forward operations on arrays to main function */			
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et_multi<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> > 
#name#(const Array<T,N>& d1, int comp, int dim)				
{ return #name#(d1.wrap(), comp, dim); }				
									
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et_multi<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> > 
#name#(Array<T,N>& d1, int comp, int dim)				
{ return #name#(d1.wrap(), comp, dim); }
"""
    os.write(stub.replace("#name#",name).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
    BZ_ET_STENCIL_REDIRECT(name)						


def BZ_ET_STENCIL_DIFF2(name, MINB1, MAXB1, MINB2, MAXB2):
    stub="""


/** Defines a stencil ET double-difference operator "#name#" that
   operates on an array<P_numtype, N_rank> and returns an array of
   identical type. (The only significance of the "double-difference" aspect
   is that the operator is assumed to take two extra arguments which
   are the dimensions to do the differences in). */

template<typename P_expr>						
class #name#_et : public _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype> 
{									
public:								
  typedef _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype> T_base;	
  typedef _bz_typename T_base::T_numtype T_numtype;			
  typedef _bz_typename T_base::T_expr T_expr;				

    // select return type
  typedef typename unwrapET<typename T_expr::T_result>::T_unwrapped test;
  typedef typename selectET<typename T_expr::T_typeprop, 
			    T_numtype, 
			    #name#_et<test> >::T_selected T_typeprop;
  typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
  typedef T_numtype T_optype;

  template<int N> struct tvresult {
    typedef #name#_et<
      typename T_expr::template tvresult<N>::Type> Type;
  };

typedef  #name#_et<_bz_typename P_expr::T_range_result> T_range_result; 
   									
  using T_base::iter_;							
  using T_base::rank_;							
public:								
  #name#_et(const #name#_et& a) :					
  _bz_StencilExpr<P_expr, T_numtype>(a),				
    dim1_(a.dim1_), dim2_(a.dim2_)					
  { }								
   									
  #name#_et(BZ_ETPARM(T_expr) a, int dim1, int dim2) :		
  _bz_StencilExpr<P_expr, T_numtype>(a),				
    dim1_(dim1), dim2_(dim2)						
  { }									
   									
  #name#_et(_bz_typename T_expr::T_ctorArg1 a,			
	      int dim1, int dim2) :					
  _bz_StencilExpr<P_expr, T_numtype>(a),				
    dim1_(dim1), dim2_(dim2)						
  { }									
   									
  T_numtype operator*() const						
  { return #name#_stencilop(iter_, dim1_, dim2_); }				
  T_numtype operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const 
  { iter_.moveTo(i); return #name#_stencilop(iter_, dim1_, dim2_); }		
									
  T_range_result operator()(const RectDomain<rank_>& d) const		
  { return T_range_result(iter_(d), dim1_, dim2_); }			
   									
  T_numtype operator[](int i) const					
  { return #name#_stencilop(iter_[i], dim1_, dim2_); }				
									
  T_numtype fastRead(diffType i) const					
  {/* this probably isn't very fast... */				
    iter_._bz_offsetData(i);						
    T_numtype r = #name#_stencilop (iter_, dim1_, dim2_);				
    iter_._bz_offsetData(-i);						
    return r;								
  }									
									
    /** This way of vectorizing won't work on stencils. */
    template<int N>
    typename tvresult<N>::Type fastRead_tv(diffType i) const {
      BZPRECHECK(0, "Can't vectorize stencils");
      return typename tvresult<N>::Type(iter_.template fastRead_tv<N>(i),dim1_,dim2_); }

  T_numtype shift(int offset, int dim) const				
  {									
    iter_._bz_offsetData(offset, dim);				
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset, dim);				
    return r;								
  }									
									
  T_numtype shift(int offset1, int dim1, int offset2, int dim2) const	
  {									
    iter_._bz_offsetData(offset1, dim1, offset2, dim2);		
    T_numtype r = #name#_stencilop (iter_);					
    iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);		
    return r;								
  }									
									
  void prettyPrint(std::string &str,				
		   prettyPrintFormat& format) const			
  {									
    str += "name (stencil)";						
    str += "(";							
    iter_.prettyPrint(str, format);					
    str += ")";							
  }									
									
  template<typename T1, typename T2 = nilArraySection,		
	   class T3 = nilArraySection, typename T4 = nilArraySection,	
	   class T5 = nilArraySection, typename T6 = nilArraySection,	
	   class T7 = nilArraySection, typename T8 = nilArraySection,	
	   class T9 = nilArraySection, typename T10 = nilArraySection,	
	   class T11 = nilArraySection>					
  class SliceInfo {							
  public:								
    typedef #name#_et<T_expr> T_slice;				
  };								
    									
  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, 
	   typename T7, typename T8, typename T9, typename T10, typename T11> 
  #name#_et							
  operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const 
  {									
    return #name#_et						
      (iter_(_bz_makeRange(r1),					
	     _bz_makeRange(r2),					
	     _bz_makeRange(r3),					
	     _bz_makeRange(r4),					
	     _bz_makeRange(r5),					
	     _bz_makeRange(r6),					
	     _bz_makeRange(r7),					
	     _bz_makeRange(r8),					
	     _bz_makeRange(r9),					
	     _bz_makeRange(r10),					
	     _bz_makeRange(r11)), dim1_, dim2_);			
  }									
   									
private:								
  int dim1_, dim2_;							
};									
 									
/* create ET from application to expression */
template<typename T1>							
inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >	
#name#(const blitz::ETBase<T1>& d1, int dim1, int dim2)		
{									
  TinyVector<int, blitz::asExpr<T1>::T_expr::rank_> minb(0), maxb(0);	
  minb[dim1]=MINB1; maxb[dim1]=MAXB1;					
  minb[dim2]=MINB2; maxb[dim2]=MAXB2;					
  return _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >	
    (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),minb, maxb)), dim1, dim2); 
}									
/* forward operations on arrays to main function */			
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> > 
#name#(const Array<T,N>& d1, int dim1, int dim2)			
{ return #name#(d1.wrap(), dim1, dim2); }				
									
template<typename T, int N>						
inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> > 
#name#(Array<T,N>& d1, int dim1, int dim2)				
{ return #name#(d1.wrap(), dim1, dim2); }
"""
    os.write(stub.replace("#name#",name).replace("MINB1",str(MINB1)).replace("MAXB1",str(MAXB1)).replace("MINB2",str(MINB2)).replace("MAXB2",str(MAXB2)))


# now generate the file

os.write("""
/* Definitions of ET stencil operators. This file was generated by
   generate/genstencils.py. Do not edit.

   Note: You can't pass templates with >1 parameter as macro
   parameters because cpp doesn't recognize that the comma is balanced
   between the angle brackets and interprets them as multiple
   arguments, i.e., the following alternative declaration of grad2D
   will not work:

   BZ_ET_STENCIL(grad2D, TinyVector<P_numtype, 2>, 
   TinyVector<typename T1::T_numtype, 2>, shape(-1,1), shape(1,1))

   instead, you have to use the above bzCC ("ConCatenate") macro to
   protect the things containing commas. The following would work:

   BZ_ET_STENCIL(grad2D, bzCC(TinyVector<P_numtype, 2>), 
   bzCC(TinyVector<typename T1::T_numtype, 2>), shape(-1,-1), shape(1,1))

*/

namespace blitz {

""")

BZ_ET_STENCIL_DIFF("central12", -1, 1)
BZ_ET_STENCIL_DIFF("central22", -1, 1)
BZ_ET_STENCIL_DIFF("central32", -2, 2)
BZ_ET_STENCIL_DIFF("central42", -2, 2)
BZ_ET_STENCIL_DIFF("central14", -2, 2)
BZ_ET_STENCIL_DIFF("central24", -2, 2)
BZ_ET_STENCIL_DIFF("central34", -2, 2)
BZ_ET_STENCIL_DIFF("central44", -2, 2)
BZ_ET_STENCIL_DIFF("central12n", -1, 1)
BZ_ET_STENCIL_DIFF("central22n", -1, 1)
BZ_ET_STENCIL_DIFF("central32n", -2, 2)
BZ_ET_STENCIL_DIFF("central42n", -2, 2)
BZ_ET_STENCIL_DIFF("central14n", -2, 2)
BZ_ET_STENCIL_DIFF("central24n", -2, 2)
BZ_ET_STENCIL_DIFF("central34n", -2, 2)
BZ_ET_STENCIL_DIFF("central44n", -2, 2)

BZ_ET_STENCIL_DIFF("backward11", -1, 0)
BZ_ET_STENCIL_DIFF("backward21", -2, 0)
BZ_ET_STENCIL_DIFF("backward31", -3, 0)
BZ_ET_STENCIL_DIFF("backward41", -4, 0)
BZ_ET_STENCIL_DIFF("backward12", -2, 0)
BZ_ET_STENCIL_DIFF("backward22", -3, 0)
BZ_ET_STENCIL_DIFF("backward32", -4, 0)
BZ_ET_STENCIL_DIFF("backward42", -5, 0)
BZ_ET_STENCIL_DIFF("backward11n", -1, 0)
BZ_ET_STENCIL_DIFF("backward21n", -2, 0)
BZ_ET_STENCIL_DIFF("backward31n", -3, 0)
BZ_ET_STENCIL_DIFF("backward41n", -4, 0)
BZ_ET_STENCIL_DIFF("backward12n", -2, 0)
BZ_ET_STENCIL_DIFF("backward22n", -3, 0)
BZ_ET_STENCIL_DIFF("backward32n", -4, 0)
BZ_ET_STENCIL_DIFF("backward42n", -5, 0)

BZ_ET_STENCIL_DIFF("forward11", 0, 1)
BZ_ET_STENCIL_DIFF("forward21", 0, 2)
BZ_ET_STENCIL_DIFF("forward31", 0, 3)
BZ_ET_STENCIL_DIFF("forward41", 0, 4)
BZ_ET_STENCIL_DIFF("forward12", 0, 2)
BZ_ET_STENCIL_DIFF("forward22", 0, 3)
BZ_ET_STENCIL_DIFF("forward32", 0, 4)
BZ_ET_STENCIL_DIFF("forward42", 0, 5)
BZ_ET_STENCIL_DIFF("forward11n", 0, 1)
BZ_ET_STENCIL_DIFF("forward21n", 0, 2)
BZ_ET_STENCIL_DIFF("forward31n", 0, 3)
BZ_ET_STENCIL_DIFF("forward41n", 0, 4)
BZ_ET_STENCIL_DIFF("forward12n", 0, 2)
BZ_ET_STENCIL_DIFF("forward22n", 0, 3)
BZ_ET_STENCIL_DIFF("forward32n", 0, 4)
BZ_ET_STENCIL_DIFF("forward42n", 0, 5)

BZ_ET_STENCIL_MULTIDIFF("central12", -1, 1)
BZ_ET_STENCIL_MULTIDIFF("central22", -1, 1)
BZ_ET_STENCIL_MULTIDIFF("central32", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central42", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central14", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central24", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central34", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central44", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central12n", -1, 1)
BZ_ET_STENCIL_MULTIDIFF("central22n", -1, 1)
BZ_ET_STENCIL_MULTIDIFF("central32n", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central42n", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central14n", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central24n", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central34n", -2, 2)
BZ_ET_STENCIL_MULTIDIFF("central44n", -2, 2)

BZ_ET_STENCIL_MULTIDIFF("backward11", -1, 0)
BZ_ET_STENCIL_MULTIDIFF("backward21", -2, 0)
BZ_ET_STENCIL_MULTIDIFF("backward31", -3, 0)
BZ_ET_STENCIL_MULTIDIFF("backward41", -4, 0)
BZ_ET_STENCIL_MULTIDIFF("backward12", -2, 0)
BZ_ET_STENCIL_MULTIDIFF("backward22", -3, 0)
BZ_ET_STENCIL_MULTIDIFF("backward32", -4, 0)
BZ_ET_STENCIL_MULTIDIFF("backward42", -5, 0)
BZ_ET_STENCIL_MULTIDIFF("backward11n", -1, 0)
BZ_ET_STENCIL_MULTIDIFF("backward21n", -2, 0)
BZ_ET_STENCIL_MULTIDIFF("backward31n", -3, 0)
BZ_ET_STENCIL_MULTIDIFF("backward41n", -4, 0)
BZ_ET_STENCIL_MULTIDIFF("backward12n", -2, 0)
BZ_ET_STENCIL_MULTIDIFF("backward22n", -3, 0)
BZ_ET_STENCIL_MULTIDIFF("backward32n", -4, 0)
BZ_ET_STENCIL_MULTIDIFF("backward42n", -5, 0)

BZ_ET_STENCIL_MULTIDIFF("forward11", 0, 1)
BZ_ET_STENCIL_MULTIDIFF("forward21", 0, 2)
BZ_ET_STENCIL_MULTIDIFF("forward31", 0, 3)
BZ_ET_STENCIL_MULTIDIFF("forward41", 0, 4)
BZ_ET_STENCIL_MULTIDIFF("forward12", 0, 2)
BZ_ET_STENCIL_MULTIDIFF("forward22", 0, 3)
BZ_ET_STENCIL_MULTIDIFF("forward32", 0, 4)
BZ_ET_STENCIL_MULTIDIFF("forward42", 0, 5)
BZ_ET_STENCIL_MULTIDIFF("forward11n", 0, 1)
BZ_ET_STENCIL_MULTIDIFF("forward21n", 0, 2)
BZ_ET_STENCIL_MULTIDIFF("forward31n", 0, 3)
BZ_ET_STENCIL_MULTIDIFF("forward41n", 0, 4)
BZ_ET_STENCIL_MULTIDIFF("forward12n", 0, 2)
BZ_ET_STENCIL_MULTIDIFF("forward22n", 0, 3)
BZ_ET_STENCIL_MULTIDIFF("forward32n", 0, 4)
BZ_ET_STENCIL_MULTIDIFF("forward42n", 0, 5)

BZ_ET_STENCIL("Laplacian2D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL("Laplacian3D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCIL("Laplacian2D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCIL("Laplacian2D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCIL("Laplacian3D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCIL("Laplacian3D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")

BZ_ET_STENCILV("grad2D", 2, "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCILV("grad2D4", 2, "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCILV("grad3D", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCILV("grad3D4", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCILV("grad2Dn", 2, "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCILV("grad2D4n", 2, "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCILV("grad3Dn", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCILV("grad3D4n", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCILV("gradSqr2D", 2, "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCILV("gradSqr2D4", 2, "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCILV("gradSqr3D", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCILV("gradSqr3D4", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCILV("gradSqr2Dn", 2, "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCILV("gradSqr2D4n", 2, "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCILV("gradSqr3Dn", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCILV("gradSqr3D4n", 3, "shape(-2,-2,-2)", "shape(2,2,2)")

BZ_ET_STENCILM("Jacobian3D", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCILM("Jacobian3Dn", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCILM("Jacobian3D4", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCILM("Jacobian3D4n", 3, "shape(-2,-2,-2)", "shape(2,2,2)")

BZ_ET_STENCIL("curl3D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCIL("curl3Dn", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCIL("curl3D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCIL("curl3D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
BZ_ET_STENCIL("curl2D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL("curl2Dn", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL("curl2D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCIL("curl2D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")

BZ_ET_STENCIL_SCA("div2D", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL_SCA("div2Dn", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL_SCA("div2D4", "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCIL_SCA("div2D4n", "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCIL_SCA("div3D", "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCIL_SCA("div3Dn", "shape(-1,-1,-1)", "shape(1,1,1)")
BZ_ET_STENCIL_SCA("div3D4", "shape(-2,-2,-2)", "shape(2,2,2)")


BZ_ET_STENCIL2("div", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL2("divn", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-1,-1)", "shape(1,1)")
BZ_ET_STENCIL2("div4", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-2,-2)", "shape(2,2)")
BZ_ET_STENCIL2("div4n", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-2,-2)", "shape(2,2)")

BZ_ET_STENCIL_DIFF2("mixed22", -1, 1, -1, 1)
BZ_ET_STENCIL_DIFF2("mixed22n", -1, 1, -1, 1)
BZ_ET_STENCIL_DIFF2("mixed24", -2, 2, -2, 2)
BZ_ET_STENCIL_DIFF2("mixed24n", -2, 2, -2, 2)

os.write("""
}
""")
os.close()