Scopira  20080306
vectormath.h
1 
2 /*
3  * Copyright (c) 2002-2011 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_BASEKIT_VECTORMATH_H__
15 #define __INCLUDED_SCOPIRA_BASEKIT_VECTORMATH_H__
16 
17 #include <math.h>
18 
19 #include <scopira/basekit/narray.h>
20 
21 // THIS FILE HAS BEEN FULLY DOCUMENTED
22 
23 namespace scopira
24 {
25  namespace basekit
26  {
35  template <class T>
36  void add_scalar(T &v, typename T::data_type val);
45  template <class T>
46  void sub_scalar(T &v, typename T::data_type val);
55  template <class T>
56  void mul_scalar(T &v, typename T::data_type val);
65  template <class T>
66  void div_scalar(T &v, typename T::data_type val);
76  template <class T>
77  void invdiv_scalar(T &v, typename T::data_type val);
78 
79 
80  // not counst parms for muffinization
81  // probably wont need a switcher function because of micro-functions
82  // median (TODO)
83 
92  template <class V, class T>
93  size_t min(const V &v, T& out);
102  template <class V, class T>
103  size_t max(const V &v, T& out);
111  template <class V, class T>
112  void sum(const V &v, T& out);
120  template <class V, class T>
121  void mean(const V &v, T& out);
129  template <class V, class T>
130  void variance(const V &v, T& out);
138  template <class V, class T>
139  void stddev(const V &v, T& out)
140  { variance<V,T>(v, out); out = static_cast<T>(::sqrt(out)); }
148  template <class LH, class RH>
149  void add_vector(LH &target, const RH& delta);
157  template <class LH, class RH>
158  void sub_vector(LH &target, const RH& delta);
166  template <class LH, class RH>
167  void mul_vector(LH &target, const RH& delta);
175  template <class LH, class RH>
176  void div_vector(LH &target, const RH& delta);
187  template <class LH, class RH>
188  void abs_vector(LH &target, const RH& source);
196  template <class V>
197  void print_vector(scopira::tool::oflow_i &o, const V &vv);
205  template <class T>
206  void pearson_scalar(T &x, T &y, double& rho);
207 
216  template <class T>
217  void construct_index(narray<T> &array, narray<int> &index);
218 
219  template <class DATAT, class REFT>
221  }
222 }
223 
224 template <class T>
225  void scopira::basekit::add_scalar(T &v, typename T::data_type val)
226 {
227  typedef typename T::iterator iter;
228  iter ii=v.begin(), endii = v.end();
229 
230  for (; ii != endii; ++ii)
231  (*ii) += val;
232 }
233 
234 template <class T>
235  void scopira::basekit::sub_scalar(T &v, typename T::data_type val)
236 {
237  typedef typename T::iterator iter;
238  iter ii=v.begin(), endii = v.end();
239 
240  for (; ii != endii; ++ii)
241  (*ii) -= val;
242 }
243 
244 template <class T>
245  void scopira::basekit::mul_scalar(T &v, typename T::data_type val)
246 {
247  typedef typename T::iterator iter;
248  iter ii=v.begin(), endii = v.end();
249 
250  for (; ii != endii; ++ii)
251  (*ii) *= val;
252 }
253 
254 template <class T>
255  void scopira::basekit::div_scalar(T &v, typename T::data_type val)
256 {
257  typedef typename T::iterator iter;
258  iter ii=v.begin(), endii = v.end();
259 
260  for (; ii != endii; ++ii)
261  (*ii) /= val;
262 }
263 
264 template <class T>
265  void scopira::basekit::invdiv_scalar(T &v, typename T::data_type val)
266 {
267  typedef typename T::iterator iter;
268  iter ii=v.begin(), endii = v.end();
269 
270  for (; ii != endii; ++ii)
271  (*ii) = val / (*ii);
272 }
273 
274 template <class V, class T>
275  size_t scopira::basekit::min(const V &v, T& out)
276 {
277  typedef typename V::const_iterator iter;
278  iter ii, endii;
279  size_t idx, cur;
280  T m;
281 
282  ii = v.begin();
283  endii = v.end();
284  assert(ii != endii);
285  m = *ii;
286  cur = idx = 0;
287  for (++ii, ++cur; ii!=endii; ++ii, ++cur)
288  if (*ii < m) {
289  m = *ii;
290  idx = cur;
291  }
292  out = m;
293  return idx;
294 }
295 
296 template <class V, class T>
297  size_t scopira::basekit::max(const V &v, T& out)
298 {
299  typedef typename V::const_iterator iter;
300  iter ii, endii;
301  size_t idx, cur;
302  T m;
303 
304  ii = v.begin();
305  endii = v.end();
306  assert(ii != endii);
307  m = *ii;
308  cur = idx = 0;
309  for (++ii, ++cur; ii!=endii; ++ii, ++cur)
310  if (*ii > m) {
311  m = *ii;
312  idx = cur;
313  }
314  out = m;
315  return idx;
316 }
317 
318 template <class V, class T>
319  void scopira::basekit::sum(const V &v, T& out)
320 {
321  typedef typename V::const_iterator iter;
322  iter ii, endii;
323  T m;
324 
325  m = 0;
326  ii = v.begin();
327  endii = v.end();
328  for (; ii!=endii; ++ii)
329  m += *ii;
330  out = m;
331 }
332 
333 template <class V, class T>
334  void scopira::basekit::mean(const V &v, T& out)
335 {
336  if (v.empty()) {
337  out = 0;
338  return;
339  }
340  sum(v, out);
341  out = out / v.size();
342 }
343 
344 template <class V, class T>
345  void scopira::basekit::variance(const V &v, T& out)
346 {
347  typedef typename V::const_iterator iter;
348  iter ii, endii;
349  size_t mx;
350  T avg, sum, tt;
351 
352  mx = v.size();
353  if (mx <= 1) {
354  out = 0; //special, stupid case
355  return;
356  }
357 
358  mean(v, avg);
359 
360  sum = 0;
361  endii = v.end();
362  for (ii=v.begin(); ii != endii; ++ii) {
363  tt = *ii - avg;
364  sum += tt * tt;
365  }
366 
367  out = sum / (mx - 1);
368 }
369 
370 template <class LH, class RH>
371  void scopira::basekit::add_vector(LH &target, const RH& delta)
372 {
373  typename LH::iterator tar, endtar;
374  typename RH::const_iterator src, endsrc;
375 
376  tar = target.begin();
377  endtar = target.end();
378  src = delta.begin();
379  endsrc = delta.end();
380  while (tar != endtar) {
381  assert(src != endsrc);
382  *tar += *src;
383  ++tar;
384  ++src;
385  }
386 }
387 
388 template <class LH, class RH>
389  void scopira::basekit::sub_vector(LH &target, const RH& delta)
390 {
391  typename LH::iterator tar, endtar;
392  typename RH::const_iterator src, endsrc;
393 
394  tar = target.begin();
395  endtar = target.end();
396  src = delta.begin();
397  endsrc = delta.end();
398  while (tar != endtar) {
399  assert(src != endsrc);
400  *tar -= *src;
401  ++tar;
402  ++src;
403  }
404 }
405 
406 template <class LH, class RH>
407  void scopira::basekit::mul_vector(LH &target, const RH& delta)
408 {
409  typename LH::iterator tar, endtar;
410  typename RH::const_iterator src, endsrc;
411 
412  tar = target.begin();
413  endtar = target.end();
414  src = delta.begin();
415  endsrc = delta.end();
416  while (tar != endtar) {
417  assert(src != endsrc);
418  *tar *= *src;
419  ++tar;
420  ++src;
421  }
422 }
423 
424 template <class LH, class RH>
425  void scopira::basekit::div_vector(LH &target, const RH& delta)
426 {
427  typename LH::iterator tar, endtar;
428  typename RH::const_iterator src, endsrc;
429 
430  tar = target.begin();
431  endtar = target.end();
432  src = delta.begin();
433  endsrc = delta.end();
434  while (tar != endtar) {
435  assert(src != endsrc);
436  *tar /= *src;
437  ++tar;
438  ++src;
439  }
440 }
441 
442 template <class LH, class RH>
443  void scopira::basekit::abs_vector(LH &target, const RH& source)
444 {
445  typename LH::iterator tar, endtar;
446  typename RH::const_iterator src, endsrc;
447 
448  tar = target.begin();
449  endtar = target.end();
450  src = source.begin();
451  endsrc = source.end();
452  while (tar != endtar) {
453  assert(src != endsrc);
454  *tar = (*src < 0.0) ? (*src * -1) : *src;
455  ++tar;
456  ++src;
457  }
458 }
459 
460 template <class V>
462 {
463  typedef typename V::const_iterator iter;
464  iter ii = vv.begin(), endii = vv.end();
465  o << '(';
466  for (; ii != endii; ++ii)
467  o << *ii << ',';
468  o << ")\n";
469 }
470 
482 // see Evident Scorpio implementation
483 template <class T>
484  void scopira::basekit::pearson_scalar(T &x, T &y, double& rho)
485 {
486  double sX; // sum of X pts
487  double sXX; // sum of squares
488  double sXY;
489  double sY;
490  double sYY;
491  double top, btm;
492 
493  sX = sXX = sY = sYY = sXY = 0;
494  int n = x.size();
495 
496  assert(x.size() == y.size());
497 
498  for (long i=0; i<n; i++)
499  {
500  sX += x[i];
501  sXX += x[i] * x[i];
502  sXY += x[i] * y[i];
503  sY += y[i];
504  sYY += y[i] * y[i];
505  }
506 
507  top = sXY - (sX*sY/n);
508  btm = (sXX-sX*sX/n) * (sYY-sY*sY/n);
509 
510  if (top==0 || btm==0)
511  rho = 0;
512  else
513  rho = top / sqrt(btm);
514 }
515 
516 // constructs an index for a vector
517 template <class T>
519 {
520  int i, j, k, n, l=0, ir, temp_index, temp;
521  int stack_index = -1;
522  T array_val;
523  narray<int> stack;
524 
525  stack.resize(50);
526  n = array.size();
527  ir = n-1;
528 
529  for (j=0; j<n; j++)
530  index[j] = j;
531  while (true) {
532  if (ir-l < 7) {
533  for (j=l+1; j<=ir; j++) {
534  temp_index = index[j];
535  array_val = array[temp_index];
536  for (i=j-1; i>=1; i--) {
537  if (array[index[i]] <= array_val)
538  break;
539  index[i+1] = index[i];
540  }
541  index[i+1] = temp_index;
542  }
543  if (stack_index < 0)
544  break;
545  ir = stack[stack_index--];
546  l = stack[stack_index--];
547  } else {
548  k = (l+ir) >> 1;
549  temp = index[k]; index[k] = index[l+1]; index[l+1] = temp;
550  if (array[index[l+1]] > array[index[ir]]) {
551  temp = index[l+1]; index[l+1] = index[ir]; index[ir] = temp;
552  }
553  if (array[index[l]] > array[index[ir]]) {
554  temp = index[l]; index[l] = index[ir]; index[ir] = temp;
555  }
556  if (array[index[l+1]] > array[index[l]]) {
557  temp = index[l+1]; index[l+1] = index[l]; index[l] = temp;
558  }
559  i = l+1;
560  j = ir;
561  temp_index = index[l+1];
562  array_val = array[temp_index];
563  while (true) {
564  i++;
565  while (array[index[i]] < array_val)
566  i++;
567  j--;
568  while (array[index[j]] > array_val)
569  j--;
570  if (j < i)
571  break;
572  temp = index[i]; index[i] = index[j]; index[j] = temp;
573  }
574  index[l+1] = index[j];
575  index[j] = temp_index;
576  stack_index += 2;
577  if (stack_index >= 50) {
578  //KERROR << "Stack size too small in constructing index\n";
579  return;
580  }
581  if (ir-i+1 >- j-l) {
582  stack[stack_index] = ir;
583  stack[stack_index-1] = i;
584  ir = j-1;
585  } else {
586  stack[stack_index] = j-1;
587  stack[stack_index-1] = l;
588  l = i;
589  }
590  }
591  }
592 }
593 
604 template <class DATAT, class REFT>
606 {
607  public:
608  typedef DATAT data_type;
609  typedef REFT ref_type;
610  private:
611  data_type &dm_data;
612  const ref_type &dm_ref;
613  public:
614  vecindex_sortable(DATAT &data, REFT &ref) : dm_data(data), dm_ref(ref) { }
615  int compare_element(int lidx, int ridx) const {
616  typedef typename ref_type::data_type t;
617  t l, r;
618 
619  l = dm_ref[dm_data[lidx]];
620  r = dm_ref[dm_data[ridx]];
621  if (l<r)
622  return -1;
623  else if (l>r)
624  return 1;
625  else
626  return 0;
627  }
628  void swap_element(int lidx, int ridx) {
629  typedef typename data_type::data_type t;
630  t tmp;
631 
632  tmp = dm_data[lidx];
633  dm_data[lidx] = dm_data[ridx];
634  dm_data[ridx] = tmp;
635  }
636 };
637 
638 #endif
639 
640 
Definition: vectormath.h:220
void construct_index(narray< T > &array, narray< int > &index)
Definition: vectormath.h:518
Definition: archiveflow.h:20
void print_vector(scopira::tool::oflow_i &o, const V &vv)
Definition: vectormath.h:461
void div_scalar(T &v, typename T::data_type val)
Definition: vectormath.h:255
void abs_vector(LH &target, const RH &source)
Definition: vectormath.h:443
size_t size(void) const
gets the size (1D)
Definition: narray.h:859
Definition: narray.h:96
void add_scalar(T &v, typename T::data_type val)
Definition: vectormath.h:225
void sum(const V &v, T &out)
Definition: vectormath.h:319
void pearson_scalar(T &x, T &y, double &rho)
Definition: vectormath.h:484
void mul_vector(LH &target, const RH &delta)
Definition: vectormath.h:407
void invdiv_scalar(T &v, typename T::data_type val)
Definition: vectormath.h:265
void sub_vector(LH &target, const RH &delta)
Definition: vectormath.h:389
size_t min(const V &v, T &out)
Definition: vectormath.h:275
void sub_scalar(T &v, typename T::data_type val)
Definition: vectormath.h:235
void div_vector(LH &target, const RH &delta)
Definition: vectormath.h:425
void stddev(const V &v, T &out)
Definition: vectormath.h:139
void variance(const V &v, T &out)
Definition: vectormath.h:345
size_t max(const V &v, T &out)
Definition: vectormath.h:297
void mean(const V &v, T &out)
Definition: vectormath.h:334
void mul_scalar(T &v, typename T::data_type val)
Definition: vectormath.h:245
void resize(size_t len)
Definition: narray.h:877
Definition: flow.h:159
void add_vector(LH &target, const RH &delta)
Definition: vectormath.h:371