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
89 inline Int_t Range(Int_t lb, Int_t ub, Int_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 R__DEPRECATED(6,40, "Undefined behavior in case \"value\" is smaller than the first element. Use STL algorithms instead, e.g. std::lower_bound(v.rbegin(), v.rend(), value, std::greater{}).");
102
103 // Sorting
104 template <typename Element, typename Index>
105 void Sort(Index n, const Element* a, Index* index, Bool_t down=kTRUE);
106 template <typename Iterator, typename IndexIterator>
107 void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE);
108}
109
110
111//---- Even/odd ----------------------------------------------------------------
112
113/// Returns `true` if `a` is even.
114inline Bool_t TMath::Even(Long_t a)
115 { return ! (a & 1); }
116
117/// Returns `true` if `a` is odd.
118inline Bool_t TMath::Odd(Long_t a)
119 { return (a & 1); }
120
121//---- Abs ---------------------------------------------------------------------
122
123/// Returns the absolute value of parameter `Short_t d`.
124inline Short_t TMath::Abs(Short_t d)
125{ return (d >= 0) ? d : Short_t(-d); }
126
127/// Returns the absolute value of parameter `Int_t d`.
128inline Int_t TMath::Abs(Int_t d)
129{ return std::abs(d); }
130
131/// Returns the absolute value of parameter `Long_t d`.
132inline Long_t TMath::Abs(Long_t d)
133{ return std::labs(d); }
134
135/// Returns the absolute value of parameter `Long64_t d`.
136inline Long64_t TMath::Abs(Long64_t d)
137{ return std::llabs(d); }
138
139/// Returns the absolute value of parameter `Float_t d`.
140inline Float_t TMath::Abs(Float_t d)
141{ return std::abs(d); }
142
143/// Returns the absolute value of parameter `Double_t d`.
144inline Double_t TMath::Abs(Double_t d)
145{ return std::abs(d); }
146
147/// Returns the absolute value of parameter `LongDouble_t d`.
148inline LongDouble_t TMath::Abs(LongDouble_t d)
149{ return std::abs(d); }
150
151
152//---- Sign Bit--------------------------------------------------------------------
153
154/// Returns whether the sign of `Integer a` is negative.
155template<typename Integer>
156inline Bool_t TMath::SignBit( Integer a)
157 { return (a < 0); }
158
159/// Returns whether the sign of `Float_t a` is negative.
160inline Bool_t TMath::SignBit(Float_t a)
161 { return std::signbit(a); }
162
163/// Returns whether the sign of `Double_t a` is negative.
164inline Bool_t TMath::SignBit(Double_t a)
165 { return std::signbit(a); }
166
167/// Returns whether the sign of `LongDouble_t a` is negative.
168inline Bool_t TMath::SignBit(LongDouble_t a)
169 { return std::signbit(a); }
170
171
172//---- Sign --------------------------------------------------------------------
173
174/// Returns a value with the magnitude of `a` and the sign of `b`.
175template<typename T1, typename T2>
176inline T1 TMath::Sign( T1 a, T2 b)
177 { return (SignBit(b)) ? - Abs(a) : Abs(a); }
178
179/// Returns a value with the magnitude of `a` and the sign of `b`.
180/// `a`and `b` are `Short_t`.
181inline Float_t TMath::Sign(Float_t a, Float_t b)
182 { return std::copysign(a,b); }
183
184/// Returns a value with the magnitude of `a` and the sign of `b`.
185/// `a`and `b` are `Double_t`.
186inline Double_t TMath::Sign(Double_t a, Double_t b)
187 { return std::copysign(a,b); }
188
189/// Returns a value with the magnitude of `a` and the sign of `b`.
190/// `a`and `b` are `LongDouble_t`.
191inline LongDouble_t TMath::Sign(LongDouble_t a, LongDouble_t b)
192 { return std::copysign(a,b); }
193
194
195//---- Min ---------------------------------------------------------------------
196
197/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
198/// `a`and `b` are `Short_t`.
199inline Short_t TMath::Min(Short_t a, Short_t b)
200 { return a <= b ? a : b; }
201
202/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
203/// `a`and `b` are `UShort_t`.
204inline UShort_t TMath::Min(UShort_t a, UShort_t b)
205 { return a <= b ? a : b; }
206
207/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
208/// `a`and `b` are `Int_t`.
209inline Int_t TMath::Min(Int_t a, Int_t b)
210 { return a <= b ? a : b; }
211
212/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
213/// `a`and `b` are `Short_t`.
214inline UInt_t TMath::Min(UInt_t a, UInt_t b)
215 { return a <= b ? a : b; }
216
217/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
218/// `a`and `b` are `Long_t`.
219inline Long_t TMath::Min(Long_t a, Long_t b)
220 { return a <= b ? a : b; }
221
222/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
223/// `a`and `b` are `ULong_t`.
224inline ULong_t TMath::Min(ULong_t a, ULong_t b)
225 { return a <= b ? a : b; }
226
227/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
228/// `a`and `b` are `Long64_t`.
229inline Long64_t TMath::Min(Long64_t a, Long64_t b)
230 { return a <= b ? a : b; }
231
232/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
233/// `a`and `b` are `ULong64_t`.
234inline ULong64_t TMath::Min(ULong64_t a, ULong64_t b)
235 { return a <= b ? a : b; }
236
237/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
238/// `a`and `b` are `Float_t`.
239inline Float_t TMath::Min(Float_t a, Float_t b)
240 { return a <= b ? a : b; }
241
242/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
243/// `a`and `b` are `Double_t`.
244inline Double_t TMath::Min(Double_t a, Double_t b)
245 { return a <= b ? a : b; }
246
247//---- Max ---------------------------------------------------------------------
248
249/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
250/// `a`and `b` are `Short_t`.
251inline Short_t TMath::Max(Short_t a, Short_t b)
252 { return a >= b ? a : b; }
253
254/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
255/// `a`and `b` are `UShort_t`.
256inline UShort_t TMath::Max(UShort_t a, UShort_t b)
257 { return a >= b ? a : b; }
258
259/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
260/// `a`and `b` are `Int_t`.
261inline Int_t TMath::Max(Int_t a, Int_t b)
262 { return a >= b ? a : b; }
263
264/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
265/// `a`and `b` are `UInt_t`.
266inline UInt_t TMath::Max(UInt_t a, UInt_t b)
267 { return a >= b ? a : b; }
268
269/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
270/// `a`and `b` are `Long_t`.
271inline Long_t TMath::Max(Long_t a, Long_t b)
272 { return a >= b ? a : b; }
273
274/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
275/// `a`and `b` are `ULong_t`.
276inline ULong_t TMath::Max(ULong_t a, ULong_t b)
277 { return a >= b ? a : b; }
278
279/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
280/// `a`and `b` are `Long64_t`.
281inline Long64_t TMath::Max(Long64_t a, Long64_t b)
282 { return a >= b ? a : b; }
283
284/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
285/// `a`and `b` are `ULong64_t`.
286inline ULong64_t TMath::Max(ULong64_t a, ULong64_t b)
287 { return a >= b ? a : b; }
288
289/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
290/// `a`and `b` are `Float_t`.
291inline Float_t TMath::Max(Float_t a, Float_t b)
292 { return a >= b ? a : b; }
293
294/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
295/// `a`and `b` are `Double_t`.
296inline Double_t TMath::Max(Double_t a, Double_t b)
297 { return a >= b ? a : b; }
298
299//---- Range -------------------------------------------------------------------
300
301/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
302/// `lb`, `ub` and `x` are `Short_t`.
303inline Short_t TMath::Range(Short_t lb, Short_t ub, Short_t x)
304 { return x < lb ? lb : (x > ub ? ub : x); }
305
306/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
307/// `lb`, `ub` and `x` are `Int_t`.
308inline Int_t TMath::Range(Int_t lb, Int_t ub, Int_t x)
309 { return x < lb ? lb : (x > ub ? ub : x); }
310
311/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
312/// `lb`, `ub` and `x` are `Long_t`.
313inline Long_t TMath::Range(Long_t lb, Long_t ub, Long_t x)
314 { return x < lb ? lb : (x > ub ? ub : x); }
315
316/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
317/// `lb`, `ub` and `x` are `ULong_t`.
318inline ULong_t TMath::Range(ULong_t lb, ULong_t ub, ULong_t x)
319 { return x < lb ? lb : (x > ub ? ub : x); }
320
321/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
322/// `lb`, `ub` and `x` are `Double_t`.
323inline Double_t TMath::Range(Double_t lb, Double_t ub, Double_t x)
324 { return x < lb ? lb : (x > ub ? ub : x); }
325
326/// Binary search in an array defined by its iterators.
327///
328/// The values in the iterators range are supposed to be sorted
329/// prior to this call. If match is found, function returns
330/// position of element. If no match found, function gives nearest
331/// element smaller than value.
332template <typename Iterator, typename Element>
333Iterator TMath::BinarySearch(Iterator first, Iterator last, Element value)
334{
335 Iterator pind;
336 pind = std::lower_bound(first, last, value);
337 if ( (pind != last) && (*pind == value) )
338 return pind;
339 else
340 return ( pind - 1);
341}
342
343/// Binary search in an array of n values to locate value.
344///
345/// Array is supposed to be sorted prior to this call.
346/// If match is found, function returns position of element.
347/// If no match found, function gives nearest element smaller than value.
348template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T *array, T value)
349{
350 const T* pind;
351 pind = std::lower_bound(array, array + n, value);
352 if ( (pind != array + n) && (*pind == value) )
353 return (pind - array);
354 else
355 return ( pind - array - 1);
356}
357
358/// Binary search in an array of n values to locate value.
359///
360/// Array is supposed to be sorted prior to this call.
361/// If match is found, function returns position of element.
362/// If no match found, function gives nearest element smaller than value.
363template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
364{
365 const T* pind;
366 pind = std::lower_bound(*array, *array + n, value);
367 if ( (pind != *array + n) && (*pind == value) )
368 return (pind - *array);
369 else
370 return ( pind - *array - 1);
371}
372
373template<typename T>
374struct CompareDesc {
375
376 CompareDesc(T d) : fData(d) {}
377
378 template<typename Index>
379 bool operator()(Index i1, Index i2) {
380 return *(fData + i1) > *(fData + i2);
381 }
382
383 T fData;
384};
385
386template<typename T>
387struct CompareAsc {
388
389 CompareAsc(T d) : fData(d) {}
390
391 template<typename Index>
392 bool operator()(Index i1, Index i2) {
393 return *(fData + i1) < *(fData + i2);
394 }
395
396 T fData;
397};
398
399/// Sort the n1 elements of the Short_t array defined by its
400/// iterators. In output the array index contains the indices of
401/// the sorted array. If down is false sort in increasing order
402/// (default is decreasing order).
403///
404/// NOTE that the array index must be created with a length bigger
405/// or equal than the main array before calling this function.
406template <typename Iterator, typename IndexIterator>
407void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
408{
409 int i = 0;
410
411 IndexIterator cindex = index;
412 for ( Iterator cfirst = first; cfirst != last; ++cfirst )
413 {
414 *cindex = i++;
415 ++cindex;
416 }
417
418 if ( down )
419 std::sort(index, cindex, CompareDesc<Iterator>(first) );
420 else
421 std::sort(index, cindex, CompareAsc<Iterator>(first) );
422}
423
424/// Sort the n elements of the array a of generic templated type Element.
425/// In output the array index of type Index contains the indices of the sorted array.
426/// If down is false sort in increasing order (default is decreasing order).
427///
428/// NOTE that the array index must be created with a length >= n
429/// before calling this function.
430/// NOTE also that the size type for n must be the same type used for the index array
431/// (templated type Index)
432template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
433{
434 for(Index i = 0; i < n; i++) { index[i] = i; }
435 if ( down )
436 std::sort(index, index + n, CompareDesc<const Element*>(a) );
437 else
438 std::sort(index, index + n, CompareAsc<const Element*>(a) );
439}
440
441#endif
#define R__DEPRECATED(MAJOR, MINOR, REASON)
Definition RConfig.hxx:504
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
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:69
unsigned long long ULong64_t
Definition RtypesCore.h:70
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:251
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:176
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:303
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:199
Iterator Undefined behavior in case value is smaller than the first element Use STL algorithms instead
Definition TMathBase.h:101
Bool_t Odd(Long_t a)
Returns true if a is odd.
Definition TMathBase.h:118
Bool_t SignBit(Integer a)
Returns whether the sign of Integer a is negative.
Definition TMathBase.h:156
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:348
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124
Bool_t Even(Long_t a)
Returns true if a is even.
Definition TMathBase.h:114