Library of Assembled Shared Sources
interval.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45template<typename C>
46interval<C>::interval(typename util::CallTraits<baseType>::TParam iValue)
47{
48 v[0] = v[1] = iValue;
49}
50
51
52template<typename C>
53interval<C>::interval(typename util::CallTraits<baseType>::TParam iInf,typename util::CallTraits<baseType>::TParam iSup)
54{
55 v[0] = iInf;
56 v[1] = iSup;
57}
58
59template<typename C>
60interval<C>& interval<C>::operator=(const interval<C>& i)
61{
62 v[0] = i.v[0];
63 v[1] = i.v[1];
64 return *this;
65}
66
67template<typename C>
68const interval<C>& interval<C>::operator+() const
69{
70 return *this;
71}
72
73template<typename C>
74void interval<C>::set(typename util::CallTraits<C>::TParam iInf,typename util::CallTraits<C>::TParam iSup)
75{
76 v[0] = iInf;
77 v[1] = iSup;
78}
79
80template<typename C>
81bool interval<C>::isEmpty() const
82{
83 return v[0]>v[1];
84}
85
86
87template<typename C>
88bool interval<C>::isSingleton() const
89{
90 return v[0]==v[1];
91}
92
93
94template<typename C>
95bool interval<C>::contains(typename util::CallTraits<baseType>::TParam ix) const
96{
97 return (ix>=v[0]) && (ix<=v[1]);
98}
99
100template<typename C>
101C& interval<C>::operator[](int i)
102{
103 return v[i];
104}
105
106template<typename C>
107C interval<C>::operator[](int i) const
108{
109 return v[i];
110}
111
112template<typename C>
113C interval<C>::inf() const
114{
115 return v[0];
116}
117
118template<typename C>
119C interval<C>::sup() const
120{
121 return v[1];
122}
123
124template<typename C>
125C& interval<C>::inf()
126{
127 return v[0];
128}
129
130template<typename C>
131C& interval<C>::sup()
132{
133 return v[1];
134}
135
136template<typename C>
137C interval<C>::mid() const
138{
139 return static_cast<C>(0.5)*(v[0]+v[1]);
140}
141
142template<typename C>
143C interval<C>::diam()const
144{
145 return v[1]-v[0];
146}
147
148template<typename C>
149C interval<C>::ratio() const
150{
151 if (v[0]!=static_cast<C>(0.0))
152 return (v[1]/v[0]);
153 else
154 return 0.0;
155}
156
157template<typename C>
158bool interval<C>::operator> (const interval<C>& i) const
159{
160 return v[0]>i.v[1];
161}
162
163template<typename C>
164bool interval<C>::operator< (const interval<C>& i) const
165{
166 return v[1]<i.v[0];
167}
168
169template<typename C>
170bool interval<C>::operator==(const interval<C>& i) const
171{
172 return (v[0]==i.v[0]) && (v[1]==i.v[1]);
173}
174
175template<typename C>
176bool interval<C>::operator!=(const interval<C>& i) const
177{
178 return !(*this==i);
179}
180
181template<typename C>
182bool interval<C>::operator>=(const interval<C>& i) const
183{
184 return (v[0]>=i.v[0]) && (v[1]>=i.v[1]);
185}
186
187template<typename C>
188bool interval<C>::operator<=(const interval<C>& i) const
189{
190 return (v[0]<=i.v[0]) && (v[1]<=i.v[1]);
191}
192
193template<typename C>
194bool interval<C>::pe(const interval<C>& i) const
195{
196 return ((v[0]<i.v[0]) && (v[1]>i.v[0])) || ((v[0]<i.v[1])&&(v[1]>i.v[1]));
197}
198
199template<typename C>
200bool interval<C>::pne(const interval<C>& i) const
201{
202 return *this!=i;
203}
204
205template<typename C>
206bool interval<C>::pg(const interval<C>& i) const
207{
208 return v[1]>i.v[0];
209}
210
211template<typename C>
212bool interval<C>::pge(const interval<C>& i) const
213{
214 return v[1]>=i.v[0];
215}
216
217template<typename C>
218bool interval<C>::pl(const interval<C>& i) const
219{
220 return v[0]<i.v[1];
221}
222
223template<typename C>
224bool interval<C>::ple(const interval<C>& i) const
225{
226 return v[0]>=i.v[1];
227}
228
229template<typename C>
230std::ostream& operator<<( std::ostream& os, const interval<C>& iV )
231{
232 os << "<" << iV.inf() << "," << iV.sup() << ">";
233 return os;
234}
235
236template<typename C> std::string str( const interval<C>& iV )
237{
238 return "<"+ str(iV.inf()) + "," + str(iV.sup()) + ">";
239}
240
241
242namespace impl
243{
244
245template<typename C> C numMin4(const C& k1,const C& k2,const C& k3,const C& k4)
246{
247 C tmpmin;
248
249 tmpmin = k2;
250 if (k1<k2)
251 tmpmin = k1;
252 if (k3<tmpmin)
253 tmpmin = k3;
254 if (k4<tmpmin)
255 tmpmin = k4;
256 return tmpmin;
257}
258
259template<typename C> C numMax4(const C& k1,const C& k2,const C& k3,const C& k4)
260{
261 C tmpmax;
262
263 tmpmax = k2;
264 if (k1>k2)
265 tmpmax = k1;
266 if (k3>tmpmax)
267 tmpmax = k3;
268 if (k4>tmpmax)
269 tmpmax = k4;
270 return tmpmax;
271}
272
273template<typename C> inline void numMinMax(const C& k1,const C& k2,C& amin,C& amax)
274{
275 if (k1<k2)
276 {
277 amin = k1;
278 amax = k2;
279 }
280 else
281 {
282 amin = k2;
283 amax = k1;
284 }
285}
286
287template<typename C> inline void numMin(const C& k1,const C& k2,C& amin)
288{
289 if (k1<k2)
290 amin = k1;
291 else
292 amin = k2;
293}
294
295template<typename C> inline void numMax(const C& k1,const C& k2,C& amax)
296{
297 if (k2<k1)
298 amax = k1;
299 else
300 amax = k2;
301}
302
303}
304
305template<typename C> interval<C> operator+(const interval<C>& i1,const interval<C>& i2);
306template<typename C> interval<C> operator-(const interval<C>& i1,const interval<C>& i2);
307template<typename C> interval<C> operator*(const interval<C>& i1,const interval<C>& i2);
308template<typename C> interval<C> operator/(const interval<C>& i1,const interval<C>& i2);
309
310template<typename C>
311interval<C> interval<C>::operator-() const
312{
313 return interval<C>(-v[1],-v[0]);
314}
315
316template<typename C>
317interval<C>& interval<C>::operator+=(const interval<C>& i)
318{
319 v[0] += i.v[0];
320 v[1] += i.v[1];
321 return *this;
322}
323
324template<typename C>
325interval<C>& interval<C>::operator-=(const interval<C>& i)
326{
327 v[0] -= i.v[1];
328 v[1] -= i.v[0];
329 return *this;
330}
331
332template<typename C>
333interval<C>& interval<C>::operator*=(const interval<C>& i)
334{
335 C m1,M1,m2,M2;
336 impl::numMinMax(v[0]*i.v[0],v[0]*i.v[1],m1,M1);
337 impl::numMinMax(v[1]*i.v[0],v[1]*i.v[1],m2,M2);
338 impl::numMin(m1,m2,v[0]);
339 impl::numMax(M1,M2,v[1]);
340 return *this;
341}
342
343
344template<typename C>
345interval<C>& interval<C>::operator/=(const interval<C>& i)
346{
347 if ((i.v[0]<=0.0) && (i.v[1]>0.0))
348 LASS_THROW("division by zero");
349 C m1,M1,m2,M2;
350 m1 = NumTraits<C>::one / i.v[0];
351 m2 = NumTraits<C>::one / i.v[1];
352
353 impl::numMinMax(v[0]*m1,v[0]*m1,m1,M1);
354 impl::numMinMax(v[1]*m2,v[1]*m2,m2,M2);
355 impl::numMin(m1,m2,v[0]);
356 impl::numMax(M1,M2,v[1]);
357 return *this;
358}
359
360
361template<typename C>
362interval<C>& interval<C>::operator+=(typename util::CallTraits<baseType>::TParam s)
363{
364 v[0] += s;
365 v[1] += s;
366 return *this;
367}
368
369template<typename C>
370interval<C>& interval<C>::operator-=(typename util::CallTraits<baseType>::TParam s)
371{
372 v[0] -= s;
373 v[1] -= s;
374 return *this;
375}
376
377template<typename C>
378interval<C>& interval<C>::operator*=(typename util::CallTraits<baseType>::TParam s)
379{
380 v[0] *= s;
381 v[1] *= s;
382
383 if (s<0.0)
384 std::swap(v[0],v[1]);
385
386 return *this;
387}
388
389template<typename C>
390interval<C>& interval<C>::operator/=(typename util::CallTraits<baseType>::TParam s)
391{
392 C t = static_cast<C>(1.0)/s;
393 v[0] *= t;
394 v[1] *= t;
395
396 if (s<0.0)
397 std::swap(v[0],v[1]);
398
399 return *this;
400}
401
402template<typename C> inline
403interval<C> operator+(const interval<C>& i1,const interval<C>& i2)
404{
405 interval<C> t(i1);
406 t+=i2;
407 return t;
408}
409
410template<typename C> inline
411interval<C> operator-(const interval<C>& i1,const interval<C>& i2)
412{
413 interval<C> t(i1);
414 t-=i2;
415 return t;
416}
417
418template<typename C> inline
419interval<C> operator*(const interval<C>& i1,const interval<C>& i2)
420{
421 interval<C> t(i1);
422 t*=i2;
423 return t;
424}
425
426template<typename C> inline
427interval<C> operator/(const interval<C>& i1,const interval<C>& i2)
428{
429 interval<C> t(i1);
430 t/=i2;
431 return t;
432}
433
434/** fuzzy equal */
435template<typename C>
436C interval<C>::fe(const interval<C>& i) const
437{
438 if (i.isSingleton())
439 {
440 if (isSingleton())
441 if (v[0]==i.v[0])
442 return static_cast<C>(1.0);
443 return static_cast<C>(0.0);
444 }
445 if (i.v[0]<v[1])
446 {
447 return (v[1]-i.v[0])/(i.v[1]-v[0]);
448 }
449 return (i.v[1]-v[0])/(v[1]-i.v[0]);
450}
451
452/** fuzzy not equal */
453template<typename C>
454C interval<C>::fne(const interval<C>& i) const
455{
456 return static_cast<C>(1.0)-fe(i);
457}
458
459/** fuzzy greater than*/
460template<typename C>
461C interval<C>::fg(const interval<C>& i) const
462{
463 if (i.v[0]<v[1])
464 {
465 return (i.v[0]-v[0])/(i.v[1]-v[0]);
466 }
467 return (v[0]-i.v[0])/(v[1]-i.v[0]);
468}
469
470/** fuzzy greater or equal than*/
471template<typename C>
472C interval<C>::fge(const interval<C>& i) const
473{
474 return std::max(fg(i),fe(i));
475}
476
477/** fuzzy less than */
478template<typename C>
479C interval<C>::fl(const interval<C>& i) const
480{
481 return static_cast<C>(1.0)-fg(i);
482}
483
484/** fuzzy less or equal than*/
485template<typename C>
486C interval<C>::fle(const interval<C>& i) const
487{
488 return std::max(fl(i),fe(i));
489}
490
491template<typename C>
492void inpsqr(interval<C>& i)
493{
494 C m1,M1;
495 m1 = i.v[0]*i.v[0];
496 M1 = i.v[1]*i.v[1];
497
498 impl::numMinMax(m1,M1,i.v[0],i.v[1]);
499 if ((i.v[0] < 0.0) && (i.v[1]>=0.0))
500 i.v[0] = 0.0;
501}
502
503template<typename C>
504void inpsqrt(interval<C>& i)
505{
506 i.v[0] = lass::num::sqrt(i.v[0]);
507 i.v[1] = lass::num::sqrt(i.v[1]);
508}
509
510template<typename C>
511void inpexp(interval<C>& i)
512{
513 i.v[0] = lass::num::exp(i.v[0]);
514 i.v[1] = lass::num::exp(i.v[1]);
515}
516
517template<typename C>
518void inplog(interval<C>& i)
519{
520 i[0] = lass::num::log(i[0]);
521 i[1] = lass::num::log(i[1]);
522}
523
524template<typename C>
525void inpnorm(interval<C>& i)
526{
527 i[0] = lass::num::norm( i[0] );
528 i[1] = lass::num::norm( i[1] );
529}
530
531template<typename C>
532void inpinv(interval<C>& i)
533{
534 i[0] = lass::num::inv( i[1] );
535 i[1] = lass::num::inv( i[0] );
536}
537
538
539template<typename C>
540interval<C> sqr(const interval<C>& i)
541{
542 C m1,M1,m2,M2;
543 m1 = i[0]*i[0];
544 M1 = i[1]*i[1];
545 impl::numMinMax(m1,M1,m2,M2);
546
547 if (i.contains(0.0))
548 m2 = 0.0;
549
550 return num::interval<C>(m2,M2);
551}
552
553template<typename C>
554interval<C> sqrt(const interval<C>& i)
555{
556 return interval<C>(lass::num::sqrt(i.inf()),lass::num::sqrt(i.sup()));
557}
558
559template<typename C>
560interval<C> exp(const interval<C>& i)
561{
562 return interval<C>(lass::num::exp(i.inf()),lass::num::exp(i.sup()));
563}
564
565template<typename C>
566interval<C> log(const interval<C>& i)
567{
568 return interval<C>(lass::num::log(i.inf()),lass::num::log(i.sup()));
569}
570
571template<typename C>
572interval<C> set_union(const interval<C>& i1, const interval<C>& i2)
573{
574 return interval<C>( std::min(i1.inf(), i2.inf()), std::max( i1.sup(), i2.sup() ) );
575}
576
577template<typename C>
578interval<C> set_intersect(const interval<C>& i1, const interval<C>& i2)
579{
580 return interval<C>( std::max(i1.inf(), i2.inf()), std::min( i1.sup(), i2.sup() ) );
581}
582
583
584template<typename C,typename f>
585interval<C> applyFunction(const interval<C>& iV, f func )
586{
587 C t1,t2,r1,r2;
588 t1 = func(iV.inf());
589 t2 = func(iV.sup());
590 impl::numMinMax(t1,t2,r1,r2);
591 return interval<C>(r1,r2);
592}
T inv(const T &x)
return x ^ -1
Definition basic_ops.h:178
T norm(const T &x)
return norm of x as if x is real part of complex number: sqr(x)
Definition basic_ops.h:211
T sqr(const T &x)
return x * x