Functions
test_array.cxx File Reference
#include "WireCellUtil/Array.h"
#include "WireCellUtil/ExecMon.h"
#include "WireCellUtil/Testing.h"
#include <iostream>

Go to the source code of this file.

Functions

array_xxf my_great_array (ExecMon &em, int nrows, int ncols)
 
array_xxf dup (const array_xxf &arr)
 
void test_copy (ExecMon &em)
 
void test_return (ExecMon &em)
 
template<typename arrtype >
bool same (const arrtype &a1, const arrtype &a2, double eps=1.0e-6)
 
void test_partial (ExecMon &em)
 
void test_dft (ExecMon &em)
 
void test_deconv (ExecMon &em)
 
void test_division (ExecMon &em)
 
void test_division_complex (ExecMon &em)
 
int main ()
 

Function Documentation

array_xxf dup ( const array_xxf arr)

Definition at line 23 of file test_array.cxx.

24 {
25  return arr;
26 }
int main ( void  )

Definition at line 221 of file test_array.cxx.

222 {
224 
225  test_partial(em);
226  test_copy(em);
227  test_return(em);
228  test_dft(em);
229  test_deconv(em);
230  test_division(em);
232 
233 
234  em("the end");
235  cerr << em.summary() << endl;
236  return 0;
237 }
void test_division_complex(ExecMon &em)
Definition: test_array.cxx:193
void test_division(ExecMon &em)
Definition: test_array.cxx:164
void test_copy(ExecMon &em)
Definition: test_array.cxx:28
void test_return(ExecMon &em)
Definition: test_array.cxx:46
void test_dft(ExecMon &em)
Definition: test_array.cxx:117
void test_deconv(ExecMon &em)
Definition: test_array.cxx:142
void test_partial(ExecMon &em)
Definition: test_array.cxx:79
std::string summary() const
Return summary up to now.
Definition: ExecMon.cxx:21
QTextStream & endl(QTextStream &s)
array_xxf my_great_array ( ExecMon em,
int  nrows,
int  ncols 
)

Definition at line 12 of file test_array.cxx.

13 {
14  std::stringstream ss;
15  ss << "array(" << nrows << "," << ncols<<")";
16  em(ss.str() + ": constructing");
17  array_xxf arr = Eigen::ArrayXXf::Random(nrows, ncols); // 197 ms opt, 646 ms debug.
18  em(ss.str() + ": constructed");
19 
20  return arr;
21 }
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
template<typename arrtype >
bool same ( const arrtype &  a1,
const arrtype &  a2,
double  eps = 1.0e-6 
)

Definition at line 61 of file test_array.cxx.

62 {
63  double n1 = a1.matrix().squaredNorm();
64  double n2 = a2.matrix().squaredNorm();
65  if (n1==n2) {
66  return true;
67  }
68  if (n2 == 0.0) {
69  cerr << "norm2=0, norm1=" << n2 << endl;
70  return false;
71  }
72  double diff = std::abs(1-n1/n2);
73  if (diff > eps) {
74  cerr << "differ: " << diff << " n1=" << n1 << " n2=" << n2 << endl;
75  }
76  return (diff <= eps);
77 }
#define a2
T abs(T value)
QTextStream & endl(QTextStream &s)
#define a1
void test_copy ( ExecMon em)

Definition at line 28 of file test_array.cxx.

29 {
30  const int nrows = 3000;
31  const int ncols = 10000;
32  {
33  array_xxf arr = my_great_array(em, nrows, ncols);
34  em("array by value: returned");
35 
36  // This loop over 100 copies takes 19.513 s (debug), 1.715 s (opt)
37  for (int ind=0; ind<100; ++ind) {
38  array_xxf tmp = dup(arr);
39  }
40  em("array by value: 100 dups");
41 
42  }
43  em("array by value: left scope");
44 }
array_xxf dup(const array_xxf &arr)
Definition: test_array.cxx:23
string tmp
Definition: languages.py:63
array_xxf my_great_array(ExecMon &em, int nrows, int ncols)
Definition: test_array.cxx:12
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
void test_deconv ( ExecMon em)

Definition at line 142 of file test_array.cxx.

143 {
144  const int nrows = 300;
145  const int ncols = 1000;
146 
147  em("deconv: start");
148  auto arr = my_great_array(em, nrows, ncols);
149  em("deconv: got array");
150  array_xxc filt = Eigen::ArrayXXcf::Zero(nrows, ncols) + 1.0;
151  em("deconv: got filter");
152  auto deco = deconv(arr, filt);
153  em("deconv: done"); // 38ms opt, 274 debug
154 
155  array_xxf diff = arr - deco;
156  em("deconv: diff");
157 
158  double norm = diff.matrix().norm();
159  em("deconv: norm");
160  cerr << "got norm of diff " << norm << endl;
161  Assert(norm < 0.001);
162 }
Eigen::ArrayXXcf array_xxc
A complex, 2D array.
Definition: Array.h:57
#define Assert
Definition: Testing.h:7
array_xxf deconv(const array_xxf &arr, const array_xxc &filter)
Definition: Array.cxx:187
auto norm(Vector const &v)
Return norm of the specified vector.
array_xxf my_great_array(ExecMon &em, int nrows, int ncols)
Definition: test_array.cxx:12
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
QTextStream & endl(QTextStream &s)
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
void test_dft ( ExecMon em)

Definition at line 117 of file test_array.cxx.

118 {
119  const int nrows = 300;
120  const int ncols = 1000;
121 
122  const int nrounds = 100;
123 
124  auto arr = my_great_array(em, nrows, ncols);
125  em("dft: make array");
126  for (int count = 0; count < nrounds; ++count) {
127 
128  auto spec = dft(arr);
129  auto orig = idft(spec);
130 
131  }
132  em("dft with floats");
133 #ifdef WCT_HACK_FOR_FFTW_NO_SP
134  for (int count = 0; count < nrounds; ++count) {
135  auto spec = dftd(arr);
136  auto orig = idftd(spec);
137  }
138  em("dft up/down cast through doubles");
139 #endif
140 }
array_xxf my_great_array(ExecMon &em, int nrows, int ncols)
Definition: test_array.cxx:12
array_xxf idft(const array_xxc &arr)
Definition: Array.cxx:96
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
array_xxc dft(const array_xxf &arr)
Definition: Array.cxx:15
void test_division ( ExecMon em)

Definition at line 164 of file test_array.cxx.

165 {
166  array_xxf arr1(3,2), arr2(3,2), arr3(3,2);
167  arr1 <<
168  0.0, 1.0,
169  2.0, 3.0,
170  4.0, 5.0;
171  arr2 <<
172  0.0, 0.5,
173  0.0, 2.0,
174  0.0, -5.0;
175  arr3 = arr1/arr2;
176  cerr << "arr3 before NaN zeroing\n" << arr3 << endl;
177 
178  for (int irow=0; irow<arr3.rows(); ++irow) {
179  for (int icol=0; icol<arr3.cols(); ++icol) {
180  float val = arr3(irow,icol);
181  if (std::isnan(val)) {
182  arr3(irow,icol) = -0.0;
183  }
184  if (std::isinf(val)) {
185  arr3(irow,icol) = 0.0;
186  }
187  }
188  }
189  cerr << "arr3 after NaN zeroing\n" << arr3 << endl;
190 }
dummy_int isinf(...)
Definition: format.h:276
QTextStream & endl(QTextStream &s)
dummy_int isnan(...)
Definition: format.h:278
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
void test_division_complex ( ExecMon em)

Definition at line 193 of file test_array.cxx.

194 {
195  array_xxc arr1(3,2), arr2(3,2), arr3(3,2);
196  arr1 <<
197  0.0, 1.0,
198  2.0, 3.0,
199  4.0, 5.0;
200  arr2 <<
201  0.0, 0.5,
202  0.0, 2.0,
203  0.0, -5.0;
204  arr3 = arr1/arr2;
205  cerr << "arr3 before NaN zeroing\n" << arr3 << endl;
206 
207  for (int irow=0; irow<arr3.rows(); ++irow) {
208  for (int icol=0; icol<arr3.cols(); ++icol) {
209  float val = abs(arr3(irow,icol));
210  if (std::isnan(val)) {
211  arr3(irow,icol) = -0.0;
212  }
213  if (std::isinf(val)) {
214  arr3(irow,icol) = 0.0;
215  }
216  }
217  }
218  cerr << "arr3 after NaN zeroing\n" << arr3 << endl;
219 }
Eigen::ArrayXXcf array_xxc
A complex, 2D array.
Definition: Array.h:57
T abs(T value)
dummy_int isinf(...)
Definition: format.h:276
QTextStream & endl(QTextStream &s)
dummy_int isnan(...)
Definition: format.h:278
void test_partial ( ExecMon em)

Definition at line 79 of file test_array.cxx.

80 {
81  const int nrows = 300;
82  const int ncols = 1000;
83 
84  auto arr = my_great_array(em, nrows, ncols);
85  em("test_partial: make array");
86 
87  auto spec = dft(arr);
88  auto spec_rc = dft_rc(arr);
89  auto spec_cc = dft_cc(spec_rc);
90  em("test_partial: forward");
91 
92  auto arr2 = idft(spec);
93  auto arr2_cc = idft_cc(spec_cc);
94  auto arr2_cr = idft_cr(arr2_cc);
95  em("test_partial: reverse");
96 
97  Assert(same(spec, spec_cc));
98  Assert(same(arr, arr2));
99  Assert(same(arr2, arr2_cr));
100 
101  const int nrounds = 100;
102 
103  for (int count = 0; count < nrounds; ++count) {
104  auto spec = dft(arr);
105  auto orig = idft(spec);
106  }
107  em("test_partial: direct round trip");
108  for (int count = 0; count < nrounds; ++count) {
109  auto spec_rc = dft_rc(arr);
110  auto spec_cc = dft_cc(spec_rc);
111  auto arr2_cc = idft_cc(spec_cc);
112  auto arr2_cr = idft_cr(arr2_cc);
113  }
114  em("test_partial: partial round trip");
115 }
array_xxc dft_cc(const array_xxc &arr, int dim=1)
Definition: Array.cxx:67
array_xxf idft_cr(const array_xxc &arr, int dim=0)
Definition: Array.cxx:153
array_xxc dft_rc(const array_xxf &arr, int dim=0)
Definition: Array.cxx:40
#define Assert
Definition: Testing.h:7
array_xxc idft_cc(const array_xxc &arr, int dim=1)
Definition: Array.cxx:125
bool same(const arrtype &a1, const arrtype &a2, double eps=1.0e-6)
Definition: test_array.cxx:61
array_xxf my_great_array(ExecMon &em, int nrows, int ncols)
Definition: test_array.cxx:12
array_xxf idft(const array_xxc &arr)
Definition: Array.cxx:96
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
array_xxc dft(const array_xxf &arr)
Definition: Array.cxx:15
void test_return ( ExecMon em)

Definition at line 46 of file test_array.cxx.

47 {
48  const int nrows = 3000;
49  const int ncols = 10000;
50  {
51  auto arr = my_great_array(em, nrows, ncols);
52  em("ret: to auto");
53  Assert(arr.rows() == nrows);
54  }
55  em("ret: out of scope");
56 
57 }
#define Assert
Definition: Testing.h:7
array_xxf my_great_array(ExecMon &em, int nrows, int ncols)
Definition: test_array.cxx:12
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84