~ubuntu-branches/ubuntu/utopic/blitz++/utopic-proposed

« back to all changes in this revision

Viewing changes to testsuite/multicomponent.cpp

  • Committer: Package Import Robot
  • Author(s): Christophe Trophime
  • Date: 2012-07-06 09:15:30 UTC
  • mfrom: (11.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120706091530-vzrb8zf0vpbf8tp9
Tags: 1:0.10-1
* New upstream release
  Closes: #679407
* debian/rules:
  - update for new release
  - add override_dh_auto_test target
  - regenerate configure and Makefile.am
* debian/control:
  - add libtool, automake to BuildDepends
* debian/libblitz-doc.install
  - modify path for html files
* remove uneeded patches
* add examples.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "testsuite.h"
 
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>
 
7
 
 
8
BZ_USING_NAMESPACE(blitz)
 
9
 
 
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.
 
13
 
 
14
static const double eps = 0.0001;
 
15
 
 
16
#define ISCLOSE(a, b) BZTEST(fabs((a)-(b))<eps)
 
17
 
 
18
typedef TinyVector<double,2> tv;
 
19
typedef TinyMatrix<double,2,2> tmt;
 
20
 
 
21
// types to test
 
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;
 
30
 
 
31
int main()
 
32
{
 
33
  const int sz=5;
 
34
 
 
35
  // create operands
 
36
  a1 a1A(sz), a1B(sz), a1C(sz);
 
37
  a1v a1vA(sz), a1vB(sz), a1vC(sz);
 
38
  a1m a1mA(sz), a1mB(sz), a1mC(sz);
 
39
  tvv tvvA, tvvB, tvvC;
 
40
  tmv tmvA, tmvB, tmvC;
 
41
  a1vv a1vvA(sz), a1vvB(sz), a1vvC(sz);
 
42
  a1mv a1mvA(sz), a1mvB(sz), a1mvC(sz);
 
43
 
 
44
  // fill them with data
 
45
  a1A=1,2,3,4,5;
 
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);
 
48
  a1mA(2)=0,1,2,3;
 
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));
 
57
  a1mvA(1)=2;
 
58
  a1mvA(2)=tv(1,-1),tv(2,-2),tv(3,-3),tv(4);
 
59
  a1mvA(3)(1,1)[1]=-42;
 
60
 
 
61
  // test that at least a subset was initialized correctly
 
62
  ISCLOSE(a1A(2),3);
 
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);
 
68
 
 
69
  cout << a1A << endl;
 
70
  cout << a1vA << endl;
 
71
  cout << a1mA << endl;
 
72
  cout << tvvA << endl;
 
73
  cout << tmvA << endl;
 
74
  cout << a1vvA << endl;
 
75
  cout << a1mvA << endl;
 
76
 
 
77
  // evaluate a complicated expression to exercise unary, binary,
 
78
  // constant, and funcs
 
79
 
 
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);
 
87
 
 
88
  cout << "\nTesting element-wise expression evaluation:\n";
 
89
  cout << a1B << endl;
 
90
  cout << a1vB << endl;
 
91
  cout << a1mB << endl;
 
92
  cout << tvvB << endl;
 
93
  cout << tmvB << endl;
 
94
  cout << a1vvB << endl;
 
95
  cout << a1mvB << endl;
 
96
 
 
97
  // test results (we are not testing reductions here so we loop over
 
98
  // elements)
 
99
  for(int i=0; i<sz; ++i) {
 
100
    ISCLOSE ( a1B(i), -a1A(i) );
 
101
 
 
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));
 
108
        if(i<2) {
 
109
          ISCLOSE(tmvB(j,k)(i),tmvA(j,k)(i)>0?-tmvA(j,k)(i):-3*tmvA(j,k)(i));
 
110
        }
 
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));
 
113
        }
 
114
      }
 
115
    }
 
116
  }
 
117
 
 
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);
 
126
 
 
127
  for(int i=0; i<sz; ++i) {
 
128
    ISCLOSE ( a1B(i), 0 );
 
129
 
 
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);
 
136
        if(i<2) {
 
137
          ISCLOSE(tmvB(j,k)(i), 0 );
 
138
        }
 
139
        for(int l=0; l<2; ++l) {
 
140
          ISCLOSE(a1mvB(i)(j,k)(l), 0);
 
141
        }
 
142
      }
 
143
    }
 
144
  }
 
145
 
 
146
  cout << "\nTesting scalar wrapper:\n";
 
147
 
 
148
  // now we also want to test the "scalar" wrapper.
 
149
  a1vB = a1vA*scalar(tv(2,-2));
 
150
  tmt sc;
 
151
  sc=1,2,3,4;
 
152
  a1mB = a1mA*scalar(sc);
 
153
 
 
154
  cout << a1vB << endl;
 
155
  cout << a1mB << endl;
 
156
 
 
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);
 
161
 
 
162
  return 0;
 
163
}
 
164