Typedefs | Functions | Variables
test_eigen.cxx File Reference
#include "WireCellUtil/Testing.h"
#include "WireCellUtil/ExecMon.h"
#include <Eigen/Core>
#include <unsupported/Eigen/FFT>
#include <cmath>
#include <vector>
#include <memory>
#include <iostream>

Go to the source code of this file.

Typedefs

template<typename Derived >
using shared_dense = std::shared_ptr< Eigen::DenseBase< Derived > >
 
template<typename Derived >
using const_shared_dense = std::shared_ptr< const Eigen::DenseBase< Derived > >
 
typedef Eigen::ArrayXXf array_xxf
 
typedef shared_dense< array_xxfshared_array_xxf
 
typedef const_shared_dense< array_xxfconst_shared_array_xxf
 
typedef Eigen::ArrayXXcf array_xxc
 
typedef shared_dense< array_xxcshared_array_xxc
 

Functions

Eigen::ArrayXf vec2arr (const std::vector< float > &v)
 
Eigen::ArrayXf filter_array (const Eigen::ArrayXf &arr)
 
Eigen::ArrayXf select_row (const Eigen::ArrayXXf &arr, int ind, WireCell::ExecMon &em)
 
template<typename Derived >
Eigen::Block< const Derivedreturn_block (WireCell::ExecMon &em, const_shared_dense< Derived > dense, int i, int j, int p, int q)
 
void do_fft (WireCell::ExecMon &em, const array_xxf &arr)
 
void take_pointer (WireCell::ExecMon &em, const_shared_array_xxf ba)
 
void test_bigass (WireCell::ExecMon &em)
 
int main ()
 

Variables

const int nbig_rows = 3000
 
const int nbig_cols = 10000
 

Typedef Documentation

typedef Eigen::ArrayXXcf array_xxc

Definition at line 71 of file test_eigen.cxx.

typedef Eigen::ArrayXXf array_xxf

Definition at line 67 of file test_eigen.cxx.

Definition at line 69 of file test_eigen.cxx.

template<typename Derived >
using const_shared_dense = std::shared_ptr< const Eigen::DenseBase<Derived> >

Definition at line 65 of file test_eigen.cxx.

Definition at line 72 of file test_eigen.cxx.

Definition at line 68 of file test_eigen.cxx.

template<typename Derived >
using shared_dense = std::shared_ptr< Eigen::DenseBase<Derived> >

Definition at line 63 of file test_eigen.cxx.

Function Documentation

void do_fft ( WireCell::ExecMon em,
const array_xxf arr 
)

Definition at line 86 of file test_eigen.cxx.

87 {
88  const int nrows = arr.rows();
89  const int ncols = arr.cols();
90 
91  em("fft: start");
92 
93  Eigen::MatrixXf in = arr.matrix();
94  em("fft: convert to matrix");
95  Eigen::MatrixXcf matc(nrows, ncols);
96  em("fft: made temp complex matrix");
97 
98  Eigen::FFT< float > fft;
99  em("fft: made fft object");
100 
101  for (int irow = 0; irow < nrows; ++irow) {
102  Eigen::VectorXcf fspec(ncols); // frequency spectrum
103  fft.fwd(fspec, in.row(irow));
104  matc.row(irow) = fspec;
105  }
106  em("fft: first dimension");
107 
108  for (int icol = 0; icol < ncols; ++icol) {
109  Eigen::VectorXcf pspec(nrows); // periodicity spectrum
110  fft.fwd(pspec, matc.col(icol));
111  matc.col(icol) = pspec;
112  }
113  em("fft: second dimension");
114 
115  shared_array_xxc ret = std::make_shared<array_xxc> (nrows, ncols);
116  em("fft: make shared for return");
117  (*ret) = matc;
118  em("fft: set shared for return");
119  ret = nullptr;
120  em("fft: nullify return");
121 }
shared_dense< array_xxc > shared_array_xxc
Definition: test_eigen.cxx:72
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
Eigen::ArrayXf filter_array ( const Eigen::ArrayXf &  arr)

Definition at line 45 of file test_eigen.cxx.

46 {
47  cerr << "filter.arr = " << arr << endl;
48  auto ret = arr + 1;
49  cerr << "filter.ret = " << ret << endl;
50  return ret;
51 }
QTextStream & endl(QTextStream &s)
int main ( void  )

You must specify storage size at construction

this doesn't work:

but literal comma list does:

or, map the data

Definition at line 177 of file test_eigen.cxx.

178 {
180 
181  std::vector<float> v{1.0,1.0,2.0,3.0,4.0,4.0,4.0,3.0};
182  ArrayXf ar1 = vec2arr(v); // copy okay
183 
184  /// You must specify storage size at construction
185  //ArrayXf ar2;
186  ArrayXf ar2(v.size());
187 
188  /// this doesn't work:
189  //ar2 << v;
190  /// but literal comma list does:
191  ar2 << 1.0,1.0,2.0,3.0,4.0,4.0,4.0,3.0;
192 
193  /// or, map the data
194  ArrayXf ar3 = Map<ArrayXf>(v.data(), v.size());
195 
196  ArrayXXf table(ar1.size(), 3);
197  table.col(0) = ar1;
198  table.col(1) = ar2;
199  table.col(2) = ar3;
200 
201  ArrayXf tmp = filter_array(ar3);
202  cerr << "Tmp col:\n" << tmp << ".\n";
203 
204  cerr << "Table:\n" << table << ".\n";
205 
206  ArrayXf one_row = table.row(0);
207  cerr << "One row:\n" << one_row << ".\n";
208 
209  ArrayXf one_col = table.col(0);
210  cerr << "One col:\n" << one_col << ".\n";
211 
212 
213 
214 
215  VectorXf v1 = ar1.matrix();
216 
217  for (size_t ind=0; ind < v.size(); ++ind) {
218  Assert(v[ind] == ar1(ind));
219  Assert(v[ind] == ar2(ind));
220  Assert(v[ind] == ar3(ind));
221  Assert(v[ind] == v1(ind));
222  }
223 
224  cerr << ar1.size() << " " << ar1.sum() << " " << ar1.prod() << " " << v1.norm() << " " << v1.squaredNorm() << endl;
225  int n = v1.size();
226  float sigma = sqrt(v1.squaredNorm()/n - ar1.mean()*ar1.mean());
227  cerr << ar1.mean() << " +/- " << sigma << endl;
228 
229  ArrayXf::Index maxI=-1, minI=-1;
230  float minV = ar1.minCoeff(&minI);
231  float maxV = ar1.maxCoeff(&maxI);
232 
233  Assert(minI == 0);
234  Assert(maxI == 4);
235 
236  cerr << minV << "@" << minI
237  << " < "
238  << maxV << "@" << maxI
239  << endl;
240 
241  em("testing bigass");
242 
243  test_bigass(em);
244 
245  em("the end");
246  cerr << em.summary() << endl;
247 
248  return 0;
249 }
#define Assert
Definition: Testing.h:7
unsigned int Index
string tmp
Definition: languages.py:63
void test_bigass(WireCell::ExecMon &em)
Definition: test_eigen.cxx:140
Eigen::ArrayXf vec2arr(const std::vector< float > &v)
Definition: test_eigen.cxx:34
Eigen::ArrayXf filter_array(const Eigen::ArrayXf &arr)
Definition: test_eigen.cxx:45
std::size_t n
Definition: format.h:3399
std::string summary() const
Return summary up to now.
Definition: ExecMon.cxx:21
QTextStream & endl(QTextStream &s)
template<typename Derived >
Eigen::Block<const Derived> return_block ( WireCell::ExecMon em,
const_shared_dense< Derived dense,
int  i,
int  j,
int  p,
int  q 
)

Definition at line 76 of file test_eigen.cxx.

78 {
79  //Eigen::Block<const Derived> b = dense->block(i,j,p,q);
80  auto b = dense->block(i,j,p,q);
81  cerr << em("made block") << endl;
82  cerr << " " << b.rows() << " X " << b.cols() << endl;
83  return b;
84 }
p
Definition: test.py:223
static bool * b
Definition: config.cpp:1043
QTextStream & endl(QTextStream &s)
Eigen::ArrayXf select_row ( const Eigen::ArrayXXf &  arr,
int  ind,
WireCell::ExecMon em 
)

Definition at line 53 of file test_eigen.cxx.

54 {
55  auto tmp = arr.row(ind); // no copy
56  em("after assignment to auto type");
57  Eigen::ArrayXf ret = arr.row(ind); // this does a copy
58  em("after assignment to explicit type");
59  return tmp; // this does a copy
60 }
string tmp
Definition: languages.py:63
void take_pointer ( WireCell::ExecMon em,
const_shared_array_xxf  ba 
)

Definition at line 126 of file test_eigen.cxx.

127 {
128  do_fft(em, *ba);
129  em("fft: done");
130 
131  cerr << "shared array is " << ba->rows() << " X " << ba->cols() << endl;
132  auto b = return_block(em, ba, 1,1,nbig_rows/2,nbig_cols/2);
133  cerr << "block: " << b.rows() << " X " << b.cols() << endl;
134  em("got block");
135 
136 }
const int nbig_rows
Definition: test_eigen.cxx:123
void do_fft(WireCell::ExecMon &em, const array_xxf &arr)
Definition: test_eigen.cxx:86
const int nbig_cols
Definition: test_eigen.cxx:124
Eigen::Block< const Derived > return_block(WireCell::ExecMon &em, const_shared_dense< Derived > dense, int i, int j, int p, int q)
Definition: test_eigen.cxx:76
static bool * b
Definition: config.cpp:1043
QTextStream & endl(QTextStream &s)
void test_bigass ( WireCell::ExecMon em)

Definition at line 140 of file test_eigen.cxx.

141 {
142  // not really *that* big....
143  ArrayXXf bigass = ArrayXXf::Random(nbig_rows, nbig_cols);
144  em("made big array");
145  auto part = select_row(bigass, 0, em);
146  em("got part");
147  cerr << part.rows() << " X " << part.cols() << "\n";
148  auto part2 = part * 10;
149  em("used part");
150  auto part3 = part2 * 0;
151  em("zeroed");
152  auto part4 = select_row(bigass, 0, em);
153  em("select row again");
154  int nzero=0;
155  for (int ind=0; ind<part4.rows(); ++ind) {
156  if (part4(ind) == 0.00001) {
157  ++nzero;
158  }
159  }
160  cerr << "got zero " << nzero << " times out of " << part3.rows() << endl;
161  Assert(0 == nzero);
162  em("checked");
163 
164  auto shared_bigass = std::make_shared<ArrayXXf>(nbig_rows, nbig_cols);
165  em("make_shared");
166  (*shared_bigass) = bigass;
167  em("copy shared");
168 
169  take_pointer(em, shared_bigass); // cast to const
170  em("passed as const shared pointer");
171 
172  shared_bigass = nullptr;
173  em("nullified shared");
174 }
const int nbig_rows
Definition: test_eigen.cxx:123
Eigen::ArrayXf select_row(const Eigen::ArrayXXf &arr, int ind, WireCell::ExecMon &em)
Definition: test_eigen.cxx:53
void take_pointer(WireCell::ExecMon &em, const_shared_array_xxf ba)
Definition: test_eigen.cxx:126
const int nbig_cols
Definition: test_eigen.cxx:124
#define Assert
Definition: Testing.h:7
QTextStream & endl(QTextStream &s)
Eigen::ArrayXf vec2arr ( const std::vector< float > &  v)

Definition at line 34 of file test_eigen.cxx.

35 {
36  Eigen::ArrayXf ret(v.size());
37  for (size_t ind=0; ind<v.size(); ++ind) {
38  ret(ind) = v[ind];
39  }
40  return ret;
41 }

Variable Documentation

const int nbig_cols = 10000

Definition at line 124 of file test_eigen.cxx.

const int nbig_rows = 3000

Definition at line 123 of file test_eigen.cxx.