1 #ifndef VTESTBED_BASE_BLITZ_HH 2 #define VTESTBED_BASE_BLITZ_HH 10 #include <blitz/array.h> 12 #include <vtestbed/base/bstream.hh> 13 #include <vtestbed/base/constants.hh> 19 isfinite(
float rhs) noexcept {
20 return ::std::isfinite(rhs);
24 isfinite(
double rhs) noexcept {
25 return ::std::isfinite(rhs);
28 BZ_DECLARE_FUNCTION(isfinite);
31 is_power_of_two(
int n) {
32 return n > 0 && (n & (n-1)) == 0;
35 BZ_DECLARE_FUNCTION(is_power_of_two);
38 next_power_of_two(
int n) {
49 BZ_DECLARE_FUNCTION(next_power_of_two);
51 inline float radians_to_degrees(
float x) {
55 inline double radians_to_degrees(
double x) {
59 BZ_DECLARE_FUNCTION(radians_to_degrees);
61 template <
class T,
int N>
64 for (
int i=0; i<N; ++i) {
70 template <
class T,
int N>
73 for (
int i=0; i<N; ++i) {
79 template <
class T,
int N,
int M>
82 for (
int i=0; i<N; ++i) {
for (
int j=0; j<M; ++j) { out << rhs(i,j); } }
86 template <
class T,
int N,
int M>
89 for (
int i=0; i<N; ++i) {
for (
int j=0; j<M; ++j) { in >> rhs(i,j); } }
93 template <
class T,
int N>
96 blitz::TinyVector<uint32_t,N> shape = rhs.shape();
98 const T* data = rhs.data();
99 const int n = rhs.numElements();
100 for (
int i=0; i<n; ++i) {
106 template <
class T,
int N>
109 blitz::TinyVector<uint32_t,N> shape;
113 T* data = rhs.data();
114 const int n = rhs.numElements();
115 for (
int i=0; i<n; ++i) {
121 template <
class T,
int N>
122 inline blitz::TinyVector<T,N>
123 zero(blitz::TinyVector<T,N>) {
124 return blitz::TinyVector<T,N>{T{0}};
127 template <
class T,
int N>
128 inline blitz::TinyVector<T,N>
129 one(blitz::TinyVector<T,N>) {
130 return blitz::TinyVector<T,N>{T{1}};
133 template <
class T,
int N,
int M>
134 inline TinyVector<T,N>
135 product(
const TinyMatrix<T,N,M>& lhs,
const TinyVector<T,M>& rhs) {
136 TinyVector<T,N> result;
137 for (
int i=0; i<N; ++i) {
139 for (
int j=0; j<M; ++j) {
140 sum += lhs(i,j)*rhs(j);
147 template <
class T,
int N,
int M>
148 inline blitz::TinyVector<T,N>
149 operator*(
const TinyMatrix<T,N,M>& lhs,
const TinyVector<T,M>& rhs) {
150 return product(lhs, rhs);
153 template <
class T,
int N,
int M,
int K>
154 inline TinyMatrix<T,N,M>
155 product(
const TinyMatrix<T,N,K>& lhs,
const TinyMatrix<T,K,M>& rhs) {
156 TinyMatrix<T,N,M> result;
157 for (
int i=0; i<N; ++i) {
158 for (
int j=0; j<M; ++j) {
160 for (
int k=0; k<K; ++k) { sum += lhs(i,k)*rhs(k,j); }
167 template <
class T,
int N,
int M,
int K>
168 inline TinyMatrix<T,N,M>
169 operator*(
const TinyMatrix<T,N,K>& lhs,
const TinyMatrix<T,K,M>& rhs) {
170 return product(lhs, rhs);
173 template <
class T,
int N>
176 const blitz::TinyVector<T,N>& lhs,
177 const blitz::TinyVector<T,N>& rhs,
182 return !any(abs(lhs - rhs) > eps);
185 template <
class T,
int N>
188 const blitz::TinyVector<T,N>& lhs,
194 return !any(abs(lhs - rhs) > eps);
199 empty(
const RectDomain<N>& rect){
200 return any(rect.lbound() > rect.ubound());
203 template <
class T,
int N,
int M>
204 MinMaxValue<TinyVector<T,N>>
205 minmax(Array<TinyVector<T,N>,M> x) {
206 MinMaxValue<TinyVector<T,N>> result;
212 for (
const auto& v : x) {
213 for (
int i=0; i<N; ++i) {
215 if (m < result.min(i)) {
218 if (result.max(i) < m) {
226 template <
class T,
int N>
227 inline blitz::TinyVector<T,N>
228 min_vector(Array<blitz::TinyVector<T,N>,1> x) {
230 for (
const auto& v : x) {
231 m = blitz::min(m, v);
236 template <
class T,
int N>
237 inline blitz::TinyVector<T,N>
238 max_vector(Array<blitz::TinyVector<T,N>,1> x) {
240 for (
const auto& v : x) {
241 m = blitz::max(m, v);
246 template <
class T,
int N,
int M>
247 inline TinyVector<T,N*M>
248 flatten(
const TinyVector<T,N> x[M]) {
249 TinyVector<T,N*M> result;
250 for (
int i=0; i<M; ++i) {
251 for (
int j=0; j<N; ++j) {
252 result(i*N + j) = x[i][j];
258 template <
class T,
int N,
class ... Args>
259 inline TinyVector<T,N*(
sizeof...(Args)+1)>
260 flatten(
const TinyVector<T,N>& head,
const Args& ... tail) {
261 const TinyVector<T,N> args[
sizeof...(Args)+1] = {head, tail...};
262 return flatten<T,N,
sizeof...(Args)+1>(args);
267 template <
class T,
int N,
int M>
271 operator=(
const TinyVector<T,N*M>& rhs) {
272 for (
int i=0; i<M; ++i) {
273 for (
int j=0; j<N; ++j) {
274 arr[i].get()[j] = rhs(i*N + j);
282 template <
class T,
int N,
class ... Args>
283 inline bits::Tie<T,N,
sizeof...(Args)+1>
284 tie(TinyVector<T,N>& head, Args& ... tail) {
285 return {{head, tail...}};
288 template <
class T,
int N>
289 inline blitz::TinyMatrix<T,N,N>
291 blitz::TinyMatrix<T,N,N> result;
293 for (
int i=0; i<N; ++i) {
299 template <
class T,
int N>
300 inline blitz::TinyMatrix<T,N,N>
302 blitz::TinyMatrix<T,N,N> m;
303 for (
int i=0; i<N*N; ++i) { m.data()[i] = rhs[i]; }
307 template <
class T,
int N>
308 inline blitz::TinyVector<T,N>
309 const_vector(T value) {
310 blitz::TinyVector<T,N> result;
315 template <
class T,
int N>
316 inline blitz::TinyVector<T,1>
317 slice(
const blitz::TinyVector<T,N>& x,
int i) {
321 template <
class T,
int N>
322 inline blitz::TinyVector<T,2>
323 slice(
const blitz::TinyVector<T,N>& x,
int i,
int j) {
327 template <
class T,
int N>
328 inline blitz::TinyVector<T,3>
329 slice(
const blitz::TinyVector<T,N>& x,
int i,
int j,
int k) {
330 return {x(i), x(j), x(k)};
333 template <
class T,
int N>
334 inline blitz::TinyVector<T,4>
335 slice(
const blitz::TinyVector<T,N>& x,
int i,
int j,
int k,
int l) {
336 return {x(i), x(j), x(k), x(l)};
341 clamp(T x, T x_min, T x_max) {
342 if (x < x_min) x = x_min;
343 if (x > x_max) x = x_max;
347 template <
class T,
int N>
348 inline blitz::TinyVector<T,N>
350 const blitz::TinyVector<T,N>& x,
351 const blitz::TinyVector<T,N>& x_min,
352 const blitz::TinyVector<T,N>& x_max
354 return blitz::max(x_min, blitz::min(x_max, x));
358 div_ceil(
int lhs,
int rhs) noexcept {
359 return lhs/rhs + (lhs%rhs == 0 ? 0 : 1);
362 BZ_DECLARE_FUNCTION2(div_ceil);
382 count()
const noexcept {
387 mean()
const noexcept {
392 variance(T addon)
const noexcept {
393 return this->_sum2 / (this->_count + addon);
397 population_variance()
const noexcept {
398 return this->variance(0);
402 unbiased_variance()
const noexcept {
403 return this->variance(-1);
407 variance()
const noexcept {
408 return this->unbiased_variance();
414 const T delta = x - _sum1;
415 _sum1 += delta / _count;
416 const T delta2 = x - _sum1;
417 _sum2 += delta*delta2;
430 <<
"mean=" << rhs.mean()
431 <<
",variance=" << rhs.variance();
443 template <
class T,
int N>
446 const int n = rhs.numElements();
447 const T* data = rhs.data();
449 for (
int i=0; i<n; ++i) {
450 stats.update(data[i]);
456 template <
class T,
int N>
459 return statistics<T,N>(rhs).variance();
462 template <
class T,
int N>
463 inline const Array<T,N>&
464 real(
const Array<T,N>& x) {
469 template <
class T,
int N>
471 length(
const blitz::TinyVector<T,N>& x) {
477 const T s = max(abs(x));
481 return s*sqrt(sum(pow2(x/s)));
487 length(
const blitz::TinyVector<T,2>& x) {
488 return blitz::hypot(x(0), x(1));
493 length(
const blitz::TinyVector<T,1>& x) {
494 return std::abs(x(0));
497 template <
class T,
int N>
499 sum_of_squares(
const blitz::TinyVector<T,N>& x) {
505 sum_of_squares(
const blitz::TinyVector<T,2>& x) {
506 return pow2(x(0)) + pow2(x(1));
511 sum_of_squares(
const blitz::TinyVector<T,3>& x) {
512 return pow2(x(0)) + pow2(x(1)) + pow2(x(2));
517 sum_of_squares(
const blitz::TinyVector<T,4>& x) {
518 return pow2(x(0)) + pow2(x(1)) + pow2(x(2)) + pow2(x(3));
521 template <
class T,
int N>
523 distance(
const blitz::TinyVector<T,N>& v1,
const blitz::TinyVector<T,N>& v2) {
524 return length(blitz::TinyVector<T,N>(v2 - v1));
527 template <
class T,
int N>
529 transpose(TinyMatrix<T,N,N>& m) {
531 for (
int i=0; i<N; ++i) {
532 for (
int j=i+1; j<N; ++j) {
533 swap(m(i,j), m(j,i));
543 template <
class T,
int N>
544 inline TinyVector<T,N-1>
547 for (
int i=0; i<j; ++i) { r(i) = v(i); }
548 for (
int i=j+1; i<N; ++i) { r(i-1) = v(i); }
557 template <
class T,
int N>
558 inline TinyVector<T,N+1>
561 for (
int i=0; i<j; ++i) { r(i) = v(i); }
563 for (
int i=j; i<N; ++i) { r(i+1) = v(i); }
569 inline TinyVector<T,1>
570 cross(
const TinyVector<T,2>& a,
const TinyVector<T,2>& b) {
571 return a(0)*b(1) - a(1)*b(0);
576 #endif // vim:filetype=cpp
TinyVector< T, N-1 > puncture(const TinyVector< T, N > &v, int j)
Remove j dimension of vector v.
Estimates sample mean and variance without overflows.
T variance(const Array< T, N > &rhs)
Sample variance.
TinyVector< T, 1 > cross(const TinyVector< T, 2 > &a, const TinyVector< T, 2 > &b)
Two-dimensional cross product.
TinyVector< T, N+1 > restore(const TinyVector< T, N > &v, int j)
Add dimension j to vector v.
New and missing Blitz++ functions.
static constexpr const T pi()
T length(const blitz::TinyVector< T, N > &x)
Computes vector length without overflow.
Statistics< T > statistics(const Array< T, N > &rhs)
Estimate sample mean and variance.