00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_SPLINE_LINEAR_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_SPLINE_LINEAR_INL
00047
00048 #include "num_common.h"
00049 #include "spline_linear.h"
00050 #include "../stde/extended_iterator.h"
00051
00052 namespace lass
00053 {
00054 namespace num
00055 {
00056
00057
00058
00059 template <typename S, typename D, typename T>
00060 SplineLinear<S, D, T>::SplineLinear()
00061 {
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 template <typename S, typename D, typename T>
00085 template <typename PairInputIterator>
00086 SplineLinear<S, D, T>::SplineLinear(PairInputIterator iFirst,
00087 PairInputIterator iLast)
00088 {
00089 while (iFirst != iLast)
00090 {
00091 Node node;
00092 node.x = iFirst->first;
00093 node.y = iFirst->second;
00094 nodes_.push_back(node);
00095 ++iFirst;
00096 }
00097 init();
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 template <typename S, typename D, typename T>
00120 template <typename ScalarInputIterator, typename DataInputIterator>
00121 SplineLinear<S, D, T>::SplineLinear(ScalarInputIterator iFirstControl,
00122 ScalarInputIterator iLastControl,
00123 DataInputIterator iFirstData)
00124 {
00125 while (iFirstControl != iLastControl)
00126 {
00127 Node node;
00128 node.x = *iFirstControl++;
00129 node.y = *iFirstData++;
00130 nodes_.push_back(node);
00131 }
00132 init();
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 template <typename S, typename D, typename T>
00148 const typename SplineLinear<S, D, T>::TData
00149 SplineLinear<S, D, T>::operator ()(TScalar iX) const
00150 {
00151 LASS_ASSERT(!isEmpty());
00152
00153 const typename TNodes::const_iterator n = findNode(iX);
00154 const TScalar dx = iX - n->x;
00155
00156 TData result(n->y);
00157 TDataTraits::multiplyAccumulate(result, n->dy, dx);
00158 return result;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 template <typename S, typename D, typename T>
00178 const typename SplineLinear<S, D, T>::TData
00179 SplineLinear<S, D, T>::derivative(TScalar iX) const
00180 {
00181 LASS_ASSERT(!isEmpty());
00182
00183 return findNode(iX)->dy;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 template <typename S, typename D, typename T>
00202 const typename SplineLinear<S, D, T>::TData
00203 SplineLinear<S, D, T>::derivative2(TScalar ) const
00204 {
00205 LASS_ASSERT(!isEmpty());
00206
00207 TData result;
00208 TDataTraits::zero(result, dataDimension_);
00209 return result;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 template <typename S, typename D, typename T>
00226 const typename SplineLinear<S, D, T>::TData
00227 SplineLinear<S, D, T>::integral(TScalar iBegin, TScalar iEnd) const
00228 {
00229 LASS_ASSERT(!isEmpty());
00230
00231 TNodeConstIterator first = findNode(iBegin);
00232 TNodeConstIterator last = findNode(iEnd);
00233 if (first == last)
00234 {
00235 TData result(first->y);
00236 TDataTraits::scale(result, iEnd - iBegin);
00237 TDataTraits::multiplyAccumulate(result, first->dy,
00238 (num::sqr(iEnd - first->x) - num::sqr(iBegin - first->x)) / 2);
00239 return result;
00240 }
00241 else
00242 {
00243 TScalar multiplier = 1;
00244 if (iEnd < iBegin)
00245 {
00246 std::swap(iBegin, iEnd);
00247 std::swap(first, last);
00248 multiplier = -1;
00249 }
00250
00251 TNodeConstIterator next = stde::next(first);
00252
00253 TData result(first->y);
00254 TDataTraits::scale(result, next->x - iBegin);
00255 TDataTraits::multiplyAccumulate(result, first->dy,
00256 (num::sqr(next->x - first->x) - num::sqr(iBegin - first->x)) / 2);
00257 first = next++;
00258
00259 while (first != last)
00260 {
00261 const TScalar dx = next->x - first->x;
00262 TDataTraits::multiplyAccumulate(result, first->y, dx);
00263 TDataTraits::multiplyAccumulate(result, first->dy, num::sqr(dx) / 2);
00264 first = next++;
00265 }
00266
00267 const TScalar dx = iEnd - first->x;
00268 TDataTraits::multiplyAccumulate(result, first->y, dx);
00269 TDataTraits::multiplyAccumulate(result, first->dy, num::sqr(dx) / 2);
00270
00271 TDataTraits::scale(result, multiplier);
00272 return result;
00273 }
00274 }
00275
00276
00277
00278
00279
00280
00281
00282 template <typename S, typename D, typename T>
00283 const bool SplineLinear<S, D, T>::isEmpty() const
00284 {
00285 return nodes_.empty();
00286 }
00287
00288
00289
00290
00291
00292
00293
00294 template <typename S, typename D, typename T>
00295 const typename SplineLinear<S, D, T>::TControlRange
00296 SplineLinear<S, D, T>::controlRange() const
00297 {
00298 typedef typename SplineLinear<S, D, T>::TControlRange TRange;
00299 return TRange(nodes_.front().x, nodes_.back().x);
00300 }
00301
00302
00303
00304
00305
00306 template <typename S, typename D, typename T>
00307 void SplineLinear<S, D, T>::init()
00308 {
00309
00310
00311 const typename TNodes::size_type size = nodes_.size();
00312 if (size < 2)
00313 {
00314 LASS_THROW("A linear interpolator needs at least two nodes! This one only has '"
00315 << static_cast<unsigned>(nodes_.size()) << "'.");
00316 }
00317
00318 typename TNodes::iterator prev = nodes_.begin();
00319 dataDimension_ = TDataTraits::dimension(prev->y);
00320 const typename TNodes::iterator end = nodes_.end();
00321 for (typename TNodes::iterator i = stde::next(nodes_.begin()); i != end; prev = i++)
00322 {
00323
00324
00325 if (prev->x >= i->x)
00326 {
00327 LASS_THROW("Nodes in linear interpolator must have absolutely increasing control "
00328 "values.");
00329 }
00330
00331
00332
00333 if (TDataTraits::dimension(i->y) != dataDimension_)
00334 {
00335 LASS_THROW("All data in linear interpolator must be of same dimension.");
00336 }
00337
00338
00339
00340 prev->dy = i->y;
00341 TDataTraits::multiplyAccumulate(prev->dy, prev->y, -1);
00342 TDataTraits::scale(prev->dy, num::inv(i->x - prev->x));
00343 }
00344
00345
00346 stde::prev(end)->dy = stde::prev(end, 2)->dy;
00347 }
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 template <typename S, typename D, typename T>
00361 const typename SplineLinear<S, D, T>::TNodeConstIterator
00362 SplineLinear<S, D, T>::findNode(TScalar iX) const
00363 {
00364 LASS_ASSERT(nodes_.size() >= 2);
00365
00366
00367
00368 if (iX < nodes_.front().x)
00369 {
00370 return nodes_.begin();
00371 }
00372 if (iX >= nodes_.back().x)
00373 {
00374 return stde::prev(nodes_.end());
00375 }
00376
00377
00378
00379 TNodeConstIterator first = nodes_.begin();
00380 TNodeConstIterator last = nodes_.end();
00381 while (stde::next(first) != last)
00382 {
00383 TNodeConstIterator middle = stde::next(first, std::distance(first, last) / 2);
00384 LASS_ASSERT(middle != first && middle != last);
00385
00386 if (middle->x <= iX)
00387 {
00388 first = middle;
00389 }
00390 else
00391 {
00392 last = middle;
00393 }
00394 LASS_ASSERT(first != last);
00395 }
00396
00397 LASS_ASSERT(first->x <= iX && last->x > iX);
00398 return first;
00399 }
00400
00401
00402
00403 }
00404
00405 }
00406
00407 #endif
00408
00409