22 #ifndef KJB_L_CPP_ALGORITHM_H
23 #define KJB_L_CPP_ALGORITHM_H
46 #include <boost/concept_check.hpp>
68 template <
class Iterator,
class Real>
69 typename std::iterator_traits<Iterator>::value_type
lerp(Iterator begin, Iterator end, Real
x)
71 typedef typename std::iterator_traits<Iterator>::value_type value_type;
75 int max_index = std::distance(begin, end) - 1;
76 int integer = x * max_index;
77 Real rational = x * max_index - integer;
80 assert(integer <= (
int) max_index);
81 assert(rational >= 0.0);
82 assert(rational < 1.0);
84 if(integer == (
int) max_index)
86 assert(rational < FLT_EPSILON);
88 std::advance(it, integer);
92 Iterator lower = begin;
93 std::advance(lower, integer);
95 Iterator upper = lower;
98 value_type interp = (1.0 - rational) * (*lower) + rational * (*upper);
120 template <
class IIterator,
class OIterator,
class QueryType>
121 typename std::iterator_traits<OIterator>::value_type
122 lerp(IIterator ibegin, IIterator iend, OIterator obegin,
const QueryType& query_point)
124 size_t n = std::distance(ibegin, iend);
125 OIterator oend = obegin + n;
127 typedef typename std::iterator_traits<IIterator>::value_type InputType ;
128 typedef typename std::iterator_traits<OIterator>::value_type OutputType;
130 BOOST_CONCEPT_ASSERT((boost::Convertible<QueryType, InputType>));
135 IIterator iit = ibegin;
136 std::advance(iit, n-1);
138 const InputType& lower_bound = *ibegin;
139 const InputType& upper_bound = *iit;
141 if(!(lower_bound <= query_point && query_point <= upper_bound))
144 iit = std::upper_bound(ibegin, iend, query_point);
145 const InputType& iupper = *iit;
146 const InputType& ilower = *(--iit);
148 size_t dist = std::distance(ibegin, iit);
150 OIterator oit = obegin;
151 std::advance(oit, dist);
152 const OutputType& olower = *oit;
154 if(query_point == ilower)
157 const OutputType& oupper = *(++oit);
159 const InputType offset = query_point - ilower;
160 const InputType range_size = iupper - ilower;
161 return olower + offset * (oupper - olower) / range_size;
176 template <
class IIterator,
class OIterator,
class I2Iterator,
class O2Iterator>
185 size_t n = std::distance(ibegin, iend);
186 OIterator oend = obegin + n;
188 size_t n2 = std::distance(i2begin, i2end);
189 O2Iterator o2end = o2begin + n2;
191 typedef typename std::iterator_traits<IIterator>::value_type InputType;
192 typedef typename std::iterator_traits<OIterator>::value_type OutputType;
193 typedef typename std::iterator_traits<I2Iterator>::value_type Input2Type;
194 typedef typename std::iterator_traits<O2Iterator>::value_type Output2Type;
196 BOOST_CONCEPT_ASSERT((boost::Convertible<InputType, Input2Type>));
204 IIterator iit = ibegin;
205 std::advance(iit, n-1);
207 IIterator i2it = i2begin;
208 std::advance(i2it, n2-1);
211 const InputType& lower_bound = *ibegin;
212 const InputType& upper_bound = *iit;
215 const Input2Type& query_lower_bound = *i2begin;
216 const Input2Type& query_upper_bound = *i2it;
219 if(!(lower_bound <= query_lower_bound && query_upper_bound <= upper_bound))
225 OIterator oit = obegin;
228 O2Iterator o2it = o2begin;
237 InputType* prev_input;
238 OutputType* prev_output;
239 Input2Type range_size;
243 for(; i2it != i2end; ++i2it)
245 const Input2Type& query = *i2it;
258 range_size = *iit - *prev_input;
270 Input2Type distance = query -
static_cast<Input2Type
>(*prev_input);
273 distance /= range_size;
276 *o2it++ = *prev_output + distance * (*oit - *prev_output);
293 template <
class Key_type,
class Value_type,
class Query_type>
294 Value_type
lerp(
const std::map<Key_type, Value_type>& piecewise_function,
const Query_type& query_point)
296 typedef std::map<Key_type, Value_type> Data_set;
297 typedef typename Data_set::const_iterator Iterator;
298 const Data_set& data_set = piecewise_function;
300 BOOST_CONCEPT_ASSERT((boost::Convertible<Query_type, Key_type>));
306 if(!(data_set.begin()->first <= query_point && query_point <= data_set.rbegin()->first))
309 Iterator upper = data_set.upper_bound(query_point);
310 Iterator lower = upper;
313 if(query_point == lower->first)
314 return lower->second;
316 const Key_type offset = query_point - lower->first;
317 const Key_type range_size = upper->first - lower->first;
318 return lower->second + offset * (upper->second - lower->second) / range_size;
328 template <
class ForwardIterator>
329 void linspace(
double min,
double max,
size_t n, ForwardIterator begin,
bool endpoint =
true)
334 incr = (max -
min) / (n - 1);
336 incr = (max -
min) / n;
338 ForwardIterator end = begin;
339 std::advance(end, n);
342 double last = (endpoint ? n-1 : n);
344 ForwardIterator it = begin;
345 for(
size_t i = 0;
i < n-1; ++
i)
346 *it++ = min +
i / last * (max - min);
354 template <
class ForwardIterator>
359 ForwardIterator end = begin;
360 std::advance(end, n);
361 std::transform(begin, end, begin, std::bind1st(std::ptr_fun(
static_cast<double(*)(
double,
double)
>(pow)), 10));
383 template <
class InType,
class OutIterator,
class UnaryOperator>
388 const OutIterator& out_begin,
389 const UnaryOperator& f)
391 const InType* range[2];
396 std::vector<double>
x(N);
420 OutIterator it = out_begin;
421 InType diff = upper - lower;
422 #pragma omp parallel for
423 for(
size_t i = 0;
i < x.size(); ++
i)
424 *it++ = f(lower + x[
i] * diff);
445 template <
class InType,
class OutIterator>
458 namespace kjb_parallel
466 template <
class InType,
class OutIterator,
class UnaryOperator>
471 const OutIterator& out_begin,
472 const UnaryOperator& f)
474 const InType* range[2];
479 std::vector<double>
x(N);
503 OutIterator it = out_begin;
504 InType diff = upper - lower;
505 #pragma omp parallel for
506 for(
size_t i = 0;
i < x.size(); ++
i)
507 *it++ = f(lower + x[
i] * diff);
Identity function.
Definition: l_functors.h:144
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
void ordered_lerp(IIterator ibegin, IIterator iend, OIterator obegin, I2Iterator i2begin, I2Iterator i2end, O2Iterator o2begin)
Definition: l_algorithm.h:177
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
Generator that increases (using +=) itself by the given value everytime it is called.
Definition: l_functors.h:65
#define KJB_THROW(ex)
Definition: l_exception.h:46
std::iterator_traits< Iterator >::value_type lerp(Iterator begin, Iterator end, Real x)
Definition: l_algorithm.h:69
double dist(const pt a, const pt b)
compute approx. Great Circle distance between two UTM points
Definition: layer.cpp:45
x
Definition: APPgetLargeConnectedEdges.m:100
void linspace(double min, double max, size_t n, ForwardIterator begin, bool endpoint=true)
Definition: l_algorithm.h:329
void linspace(const InType &lower, const InType &upper, size_t N, const OutIterator &out_begin, const UnaryOperator &f)
Definition: l_algorithm.h:467
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
void logspace(double min, double max, size_t n, ForwardIterator begin)
Definition: l_algorithm.h:355
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Support for error handling exception classes in libKJB.