Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMathBase.h
Go to the documentation of this file.
1// @(#)root/base:
2// Authors: Rene Brun, Fons Rademakers 29/07/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TMathBase
13#define ROOT_TMathBase
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TMath Base functions //
19// //
20// Define the functions Min, Max, Abs, Sign, Range for all types. //
21// NB: These functions are unfortunately not available in a portable //
22// way in std::. //
23// //
24// More functions are defined in TMath.h. TMathBase.h is designed to be //
25// a stable file and used in place of TMath.h in the ROOT miniCore. //
26// //
27//////////////////////////////////////////////////////////////////////////
28
29#include "RtypesCore.h"
30
31#include <cstdlib>
32#include <cmath>
33#include <algorithm>
34
35namespace TMath {
36
37 // Abs
38 inline Short_t Abs(Short_t d);
39 inline Int_t Abs(Int_t d);
40 inline Long_t Abs(Long_t d);
41 inline Long64_t Abs(Long64_t d);
42 inline Float_t Abs(Float_t d);
43 inline Double_t Abs(Double_t d);
45
46 // Even/Odd
47 inline Bool_t Even(Long_t a);
48 inline Bool_t Odd(Long_t a);
49
50 // SignBit
51 template<typename Integer>
52 inline Bool_t SignBit(Integer a);
53 inline Bool_t SignBit(Float_t a);
54 inline Bool_t SignBit(Double_t a);
56
57 // Sign
58 template<typename T1, typename T2>
59 inline T1 Sign( T1 a, T2 b);
60 inline Float_t Sign(Float_t a, Float_t b);
63
64 // Min, Max of two scalars
65 inline Short_t Min(Short_t a, Short_t b);
67 inline Int_t Min(Int_t a, Int_t b);
68 inline UInt_t Min(UInt_t a, UInt_t b);
69 inline Long_t Min(Long_t a, Long_t b);
70 inline ULong_t Min(ULong_t a, ULong_t b);
73 inline Float_t Min(Float_t a, Float_t b);
75
76 inline Short_t Max(Short_t a, Short_t b);
78 inline Int_t Max(Int_t a, Int_t b);
79 inline UInt_t Max(UInt_t a, UInt_t b);
80 inline Long_t Max(Long_t a, Long_t b);
81 inline ULong_t Max(ULong_t a, ULong_t b);
84 inline Float_t Max(Float_t a, Float_t b);
86
87 // Range
88 inline Short_t Range(Short_t lb, Short_t ub, Short_t x);
89 inline Int_t Range(Int_t lb, Int_t ub, Int_t x);
90 inline Long_t Range(Long_t lb, Long_t ub, Long_t x);
91 inline ULong_t Range(ULong_t lb, ULong_t ub, ULong_t x);
92 inline Double_t Range(Double_t lb, Double_t ub, Double_t x);
93
94 //NextPrime is used by the Core classes.
95 Long_t NextPrime(Long_t x); // Least prime number greater than x
96
97 // Binary search
98 template <typename T> Long64_t BinarySearch(Long64_t n, const T *array, T value);
99 template <typename T> Long64_t BinarySearch(Long64_t n, const T **array, T value);
100 template <typename Iterator, typename Element> Iterator BinarySearch(Iterator first, Iterator last, Element value);
101
102 // Sorting
103 template <typename Element, typename Index>
104 void Sort(Index n, const Element* a, Index* index, Bool_t down=kTRUE);
105 template <typename Iterator, typename IndexIterator>
106 void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE);
107}
108
109
110//---- Even/odd ----------------------------------------------------------------
111
112/// Returns `true` if `a` is even.
114 { return ! (a & 1); }
115
116/// Returns `true` if `a` is odd.
118 { return (a & 1); }
119
120//---- Abs ---------------------------------------------------------------------
121
122/// Returns the absolute value of parameter `Short_t d`.
124{ return (d >= 0) ? d : Short_t(-d); }
125
126/// Returns the absolute value of parameter `Int_t d`.
128{ return std::abs(d); }
129
130/// Returns the absolute value of parameter `Long_t d`.
132{ return std::labs(d); }
133
134/// Returns the absolute value of parameter `Long64_t d`.
136{ return std::llabs(d); }
137
138/// Returns the absolute value of parameter `Float_t d`.
140{ return std::abs(d); }
141
142/// Returns the absolute value of parameter `Double_t d`.
144{ return std::abs(d); }
145
146/// Returns the absolute value of parameter `LongDouble_t d`.
148{ return std::abs(d); }
149
150
151//---- Sign Bit--------------------------------------------------------------------
152
153/// Returns whether the sign of `Integer a` is negative.
154template<typename Integer>
155inline Bool_t TMath::SignBit( Integer a)
156 { return (a < 0); }
157
158/// Returns whether the sign of `Float_t a` is negative.
160 { return std::signbit(a); }
161
162/// Returns whether the sign of `Double_t a` is negative.
164 { return std::signbit(a); }
165
166/// Returns whether the sign of `LongDouble_t a` is negative.
168 { return std::signbit(a); }
169
170
171//---- Sign --------------------------------------------------------------------
172
173/// Returns a value with the magnitude of `a` and the sign of `b`.
174template<typename T1, typename T2>
176 { return (SignBit(b)) ? - Abs(a) : Abs(a); }
177
178/// Returns a value with the magnitude of `a` and the sign of `b`.
179/// `a`and `b` are `Short_t`.
181 { return std::copysign(a,b); }
182
183/// Returns a value with the magnitude of `a` and the sign of `b`.
184/// `a`and `b` are `Double_t`.
186 { return std::copysign(a,b); }
187
188/// Returns a value with the magnitude of `a` and the sign of `b`.
189/// `a`and `b` are `LongDouble_t`.
191 { return std::copysign(a,b); }
192
193
194//---- Min ---------------------------------------------------------------------
195
196/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
197/// `a`and `b` are `Short_t`.
199 { return a <= b ? a : b; }
200
201/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
202/// `a`and `b` are `UShort_t`.
204 { return a <= b ? a : b; }
205
206/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
207/// `a`and `b` are `Int_t`.
209 { return a <= b ? a : b; }
210
211/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
212/// `a`and `b` are `Short_t`.
214 { return a <= b ? a : b; }
215
216/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
217/// `a`and `b` are `Long_t`.
219 { return a <= b ? a : b; }
220
221/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
222/// `a`and `b` are `ULong_t`.
224 { return a <= b ? a : b; }
225
226/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
227/// `a`and `b` are `Long64_t`.
229 { return a <= b ? a : b; }
230
231/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
232/// `a`and `b` are `ULong64_t`.
234 { return a <= b ? a : b; }
235
236/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
237/// `a`and `b` are `Float_t`.
239 { return a <= b ? a : b; }
240
241/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
242/// `a`and `b` are `Double_t`.
244 { return a <= b ? a : b; }
245
246//---- Max ---------------------------------------------------------------------
247
248/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
249/// `a`and `b` are `Short_t`.
251 { return a >= b ? a : b; }
252
253/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
254/// `a`and `b` are `UShort_t`.
256 { return a >= b ? a : b; }
257
258/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
259/// `a`and `b` are `Int_t`.
261 { return a >= b ? a : b; }
262
263/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
264/// `a`and `b` are `UInt_t`.
266 { return a >= b ? a : b; }
267
268/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
269/// `a`and `b` are `Long_t`.
271 { return a >= b ? a : b; }
272
273/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
274/// `a`and `b` are `ULong_t`.
276 { return a >= b ? a : b; }
277
278/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
279/// `a`and `b` are `Long64_t`.
281 { return a >= b ? a : b; }
282
283/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
284/// `a`and `b` are `ULong64_t`.
286 { return a >= b ? a : b; }
287
288/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
289/// `a`and `b` are `Float_t`.
291 { return a >= b ? a : b; }
292
293/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
294/// `a`and `b` are `Double_t`.
296 { return a >= b ? a : b; }
297
298//---- Range -------------------------------------------------------------------
299
300/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
301/// `lb`, `ub` and `x` are `Short_t`.
303 { return x < lb ? lb : (x > ub ? ub : x); }
304
305/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
306/// `lb`, `ub` and `x` are `Int_t`.
308 { return x < lb ? lb : (x > ub ? ub : x); }
309
310/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
311/// `lb`, `ub` and `x` are `Long_t`.
313 { return x < lb ? lb : (x > ub ? ub : x); }
314
315/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
316/// `lb`, `ub` and `x` are `ULong_t`.
318 { return x < lb ? lb : (x > ub ? ub : x); }
319
320/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
321/// `lb`, `ub` and `x` are `Double_t`.
323 { return x < lb ? lb : (x > ub ? ub : x); }
324
325/// Binary search in an array defined by its iterators.
326///
327/// The values in the iterators range are supposed to be sorted
328/// prior to this call. If match is found, function returns
329/// position of element. If no match found, function gives nearest
330/// element smaller than value.
331template <typename Iterator, typename Element>
332Iterator TMath::BinarySearch(Iterator first, Iterator last, Element value)
333{
334 Iterator pind;
335 pind = std::lower_bound(first, last, value);
336 if ( (pind != last) && (*pind == value) )
337 return pind;
338 else
339 return ( pind - 1);
340}
341
342/// Binary search in an array of n values to locate value.
343///
344/// Array is supposed to be sorted prior to this call.
345/// If match is found, function returns position of element.
346/// If no match found, function gives nearest element smaller than value.
347template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T *array, T value)
348{
349 const T* pind;
350 pind = std::lower_bound(array, array + n, value);
351 if ( (pind != array + n) && (*pind == value) )
352 return (pind - array);
353 else
354 return ( pind - array - 1);
355}
356
357/// Binary search in an array of n values to locate value.
358///
359/// Array is supposed to be sorted prior to this call.
360/// If match is found, function returns position of element.
361/// If no match found, function gives nearest element smaller than value.
362template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
363{
364 const T* pind;
365 pind = std::lower_bound(*array, *array + n, value);
366 if ( (pind != *array + n) && (*pind == value) )
367 return (pind - *array);
368 else
369 return ( pind - *array - 1);
370}
371
372template<typename T>
374
376
377 template<typename Index>
378 bool operator()(Index i1, Index i2) {
379 return *(fData + i1) > *(fData + i2);
380 }
381
383};
384
385template<typename T>
387
389
390 template<typename Index>
391 bool operator()(Index i1, Index i2) {
392 return *(fData + i1) < *(fData + i2);
393 }
394
396};
397
398/// Sort the n1 elements of the Short_t array defined by its
399/// iterators. In output the array index contains the indices of
400/// the sorted array. If down is false sort in increasing order
401/// (default is decreasing order).
402///
403/// NOTE that the array index must be created with a length bigger
404/// or equal than the main array before calling this function.
405template <typename Iterator, typename IndexIterator>
406void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
407{
408 int i = 0;
409
410 IndexIterator cindex = index;
411 for ( Iterator cfirst = first; cfirst != last; ++cfirst )
412 {
413 *cindex = i++;
414 ++cindex;
415 }
416
417 if ( down )
418 std::sort(index, cindex, CompareDesc<Iterator>(first) );
419 else
420 std::sort(index, cindex, CompareAsc<Iterator>(first) );
421}
422
423/// Sort the n elements of the array a of generic templated type Element.
424/// In output the array index of type Index contains the indices of the sorted array.
425/// If down is false sort in increasing order (default is decreasing order).
426///
427/// NOTE that the array index must be created with a length >= n
428/// before calling this function.
429/// NOTE also that the size type for n must be the same type used for the index array
430/// (templated type Index)
431template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
432{
433 for(Index i = 0; i < n; i++) { index[i] = i; }
434 if ( down )
435 std::sort(index, index + n, CompareDesc<const Element*>(a) );
436 else
437 std::sort(index, index + n, CompareAsc<const Element*>(a) );
438}
439
440#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
unsigned short UShort_t
Definition RtypesCore.h:40
int Int_t
Definition RtypesCore.h:45
unsigned long ULong_t
Definition RtypesCore.h:55
long Long_t
Definition RtypesCore.h:54
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
short Short_t
Definition RtypesCore.h:39
double Double_t
Definition RtypesCore.h:59
long double LongDouble_t
Definition RtypesCore.h:61
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t cindex
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
#define T2
Definition md5.inl:147
#define T1
Definition md5.inl:146
TMath.
Definition TMathBase.h:35
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE)
Sort the n1 elements of the Short_t array defined by its iterators.
Definition TMathBase.h:406
Long_t NextPrime(Long_t x)
T1 Sign(T1 a, T2 b)
Returns a value with the magnitude of a and the sign of b.
Definition TMathBase.h:175
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Returns x if lb < x < up, lb if x < lb and ub if x > ub.
Definition TMathBase.h:302
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
Bool_t Odd(Long_t a)
Returns true if a is odd.
Definition TMathBase.h:117
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:431
Bool_t SignBit(Integer a)
Returns whether the sign of Integer a is negative.
Definition TMathBase.h:155
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:347
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
Bool_t Even(Long_t a)
Returns true if a is even.
Definition TMathBase.h:113
bool operator()(Index i1, Index i2)
Definition TMathBase.h:391
CompareAsc(T d)
Definition TMathBase.h:388
bool operator()(Index i1, Index i2)
Definition TMathBase.h:378
CompareDesc(T d)
Definition TMathBase.h:375