KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
m_arith.h
Go to the documentation of this file.
1 /* $Id: m_arith.h 11086 2011-11-09 22:48:33Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2011 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef KJB_CPP_M_VECTOR_UTIL_H
23 #define KJB_CPP_M_VECTOR_UTIL_H
24 
25 #include <m_cpp/m_matrix.h>
26 #include <m_cpp/m_vector.h>
27 #include <m_cpp/m_vector_d.h>
28 
29 namespace kjb
30 {
31 
32 
33 
41 template <class Assignable_array_type>
42 double reduce(const Assignable_array_type& array, size_t length)
43 {
44  Assignable_array_type tmp = array;
45  return reduce_in_place(tmp, length);
46 }
47 
57 template <class Array_type>
58 double reduce_in_place(Array_type& array, size_t length)
59 {
60  // sum up distances (use divide-and-conquor to avoid precision loss
61  while(length > 1)
62  {
63  size_t half_length = (length+1) / 2;
64 
65  for(size_t i = 0; i < half_length; i++)
66  {
67  // TODO: can we improve cache locality here?
68  if(i + half_length < length)
69  array[i] += array[i + half_length];
70  }
71 
72  length = half_length;
73  }
74 
75  return array[0];
76 }
77 
78 } //namespace kjb
79 #endif
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
double reduce_in_place(Array_type &array, size_t length)
Definition: m_arith.h:58
size_t length(const C &cner)
Counts the total number of elements in a 2D STL-style container.
Definition: l_util.h:17
double reduce(const Assignable_array_type &array, size_t length)
Definition: m_arith.h:42
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...