2
#include <blitz/array.h>
3
#include <blitz/array.cc>
4
#include <blitz/tinyvec2.cc>
5
#include <blitz/tinymat2.h>
6
#include <blitz/tinymat2.cc>
8
BZ_USING_NAMESPACE(blitz)
10
// Tests that operations on multicomponent arrays work as
11
// expected. This is a bit tricky because now they involve two
12
// recursive ET applications.
14
static const double eps = 0.0001;
16
#define ISCLOSE(a, b) BZTEST(fabs((a)-(b))<eps)
18
typedef TinyVector<double,2> tv;
19
typedef TinyMatrix<double,2,2> tmt;
22
typedef Array<double, 1> a1;
23
typedef Array<tv, 1> a1v;
24
typedef Array<tmt, 1> a1m;
25
typedef TinyVector<tv, 5> tvv;
26
typedef TinyMatrix<tv, 2,2> tmv;
27
// and the hardcore multi-level multicomponent types
28
typedef Array<TinyVector<tv, 2>, 1> a1vv;
29
typedef Array<TinyMatrix<tv, 2,2>, 1> a1mv;
36
a1 a1A(sz), a1B(sz), a1C(sz);
37
a1v a1vA(sz), a1vB(sz), a1vC(sz);
38
a1m a1mA(sz), a1mB(sz), a1mC(sz);
41
a1vv a1vvA(sz), a1vvB(sz), a1vvC(sz);
42
a1mv a1mvA(sz), a1mvB(sz), a1mvC(sz);
44
// fill them with data
46
a1vA=tv(1,-1),tv(2,-2),tv(3,-3),tv(4),tv(5);
47
a1mA=tmt(1),tmt(2),tmt(3),tmt(4),tmt(5);
49
tvvA=tv(1,-1),tv(2,-2),tv(3,-3),tv(4),tv(5);
50
tmvA=tv(1,-1),tv(2,-2),tv(3,-3),tv(4);
51
a1vvA=TinyVector<TinyVector<double,2>,2>(tv(1,-1),tv(2,-2)),
52
TinyVector<TinyVector<double,2>,2>(tv(3,-3),tv(3,-3)),
53
TinyVector<TinyVector<double,2>,2>(tv(4,-4),tv(5,-5)),
54
TinyVector<TinyVector<double,2>,2>(tv(6,6),tv(7,7)),
55
TinyVector<TinyVector<double,2>,2>(-8);
56
a1mvA=TinyMatrix<TinyVector<double,2>,2,2>(tv(1,-1));
58
a1mvA(2)=tv(1,-1),tv(2,-2),tv(3,-3),tv(4);
61
// test that at least a subset was initialized correctly
63
ISCLOSE(a1vA(2)(1),-3);
64
ISCLOSE(a1mA(2)(1,0),2);
65
ISCLOSE(tvvA(2)(1),-3);
66
ISCLOSE(a1vvA(2)(1)(0),5);
67
ISCLOSE(a1mvA(2)(1,0)(1),-3);
74
cout << a1vvA << endl;
75
cout << a1mvA << endl;
77
// evaluate a complicated expression to exercise unary, binary,
78
// constant, and funcs
80
a1B = 2*(-a1A)+sqrt(a1A*a1A);
81
a1vB = 2*(-a1vA)+sqrt(a1vA*a1vA);
82
a1mB = 2*(-a1mA)+sqrt(a1mA*a1mA);
83
tvvB = 2*(-tvvA)+sqrt(tvvA*tvvA);
84
tmvB = 2*(-tmvA)+sqrt(tmvA*tmvA);
85
a1vvB = 2*(-a1vvA)+sqrt(a1vvA*a1vvA);
86
a1mvB = 2*(-a1mvA)+sqrt(a1mvA*a1mvA);
88
cout << "\nTesting element-wise expression evaluation:\n";
94
cout << a1vvB << endl;
95
cout << a1mvB << endl;
97
// test results (we are not testing reductions here so we loop over
99
for(int i=0; i<sz; ++i) {
100
ISCLOSE ( a1B(i), -a1A(i) );
102
for(int j=0; j<2; ++j) {
103
ISCLOSE(a1vB(i)(j), a1vA(i)(j)>0?-a1vA(i)(j):-3*a1vA(i)(j));
104
ISCLOSE(tvvB(i)(j), tvvA(i)(j)>0?-tvvA(i)(j):-3*tvvA(i)(j));
105
for(int k=0; k<2; ++k) {
106
ISCLOSE(a1mB(i)(j,k),a1mA(i)(j,k)>0?-a1mA(i)(j,k):-3*a1mA(i)(j,k));
107
ISCLOSE(a1vvB(i)(j)(k),a1vvA(i)(j)(k)>0?-a1vvA(i)(j)(k):-3*a1vvA(i)(j)(k));
109
ISCLOSE(tmvB(j,k)(i),tmvA(j,k)(i)>0?-tmvA(j,k)(i):-3*tmvA(j,k)(i));
111
for(int l=0; l<2; ++l) {
112
ISCLOSE(a1mvB(i)(j,k)(l),a1mvA(i)(j,k)(l)>0?-a1mvA(i)(j,k)(l):-3*a1mvA(i)(j,k)(l));
118
// also test that the update versions of the operators work
119
a1B -= 2*(-a1A)+sqrt(a1A*a1A);
120
a1vB -= 2*(-a1vA)+sqrt(a1vA*a1vA);
121
a1mB -= 2*(-a1mA)+sqrt(a1mA*a1mA);
122
tvvB -= 2*(-tvvA)+sqrt(tvvA*tvvA);
123
tmvB -= 2*(-tmvA)+sqrt(tmvA*tmvA);
124
a1vvB -= 2*(-a1vvA)+sqrt(a1vvA*a1vvA);
125
a1mvB -= 2*(-a1mvA)+sqrt(a1mvA*a1mvA);
127
for(int i=0; i<sz; ++i) {
128
ISCLOSE ( a1B(i), 0 );
130
for(int j=0; j<2; ++j) {
131
ISCLOSE(a1vB(i)(j), 0);
132
ISCLOSE(tvvB(i)(j), 0);
133
for(int k=0; k<2; ++k) {
134
ISCLOSE(a1mB(i)(j,k), 0);
135
ISCLOSE(a1vvB(i)(j)(k), 0);
137
ISCLOSE(tmvB(j,k)(i), 0 );
139
for(int l=0; l<2; ++l) {
140
ISCLOSE(a1mvB(i)(j,k)(l), 0);
146
cout << "\nTesting scalar wrapper:\n";
148
// now we also want to test the "scalar" wrapper.
149
a1vB = a1vA*scalar(tv(2,-2));
152
a1mB = a1mA*scalar(sc);
154
cout << a1vB << endl;
155
cout << a1mB << endl;
157
ISCLOSE(a1vB(1)(1),4);
158
ISCLOSE(a1vB(4)(1),-10);
159
ISCLOSE(a1mB(1)(0,1),4);
160
ISCLOSE(a1mB(2)(1,1),12);