Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2 (development)
tsgMathUtils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Miroslav Stoyanov
3  *
4  * This file is part of
5  * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
12  * and the following disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND IMPLIED.
25  * THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY PATENT,
26  * COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE.
27  * THE USER ASSUMES RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING FROM OR ARISING OUT OF,
28  * IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
29  */
30 
31 #ifndef __TASMANIAN_SPARSE_GRID_MATHUTILS_HPP
32 #define __TASMANIAN_SPARSE_GRID_MATHUTILS_HPP
33 
53 namespace TasGrid{
54 
60 namespace Maths{
65 inline int intlog2(int i){
66  int result = 0;
67  while (i >>= 1){ result++; }
68  return result;
69 }
74 inline int int2log2(int i){ // this is effectively: 2^(floor(log_2(i))), when i == 0, this returns 1
75  int result = 1;
76  while (i >>= 1){ result <<= 1; }
77  return result;
78 }
83 inline int int3log3(int i){
84  int result = 1;
85  while(i >= 1){ i /= 3; result *= 3; }
86  return result;
87 }
92 inline int pow2(int p){ return (1 << p); }
97 inline int pow3(int p){
98  int result = 1;
99  for(int i=0; i<p; i++) result *= 3;
100  return result;
101 }
102 
107 inline double sign(double p){ return (p >= 0.0) ? 1.0 : -1.0; }
108 
117 constexpr double pi = 3.14159265358979323846;
118 
132 constexpr double num_tol = 1.E-12;
133 
134 } // namespace Maths
135 
136 } // namespace TasGrid
137 
138 #endif
int pow3(int p)
Computes std::pow(3, p), but uses only integer operations.
Definition: tsgMathUtils.hpp:97
double sign(double p)
Returns +1 or -1 with the same sign as p.
Definition: tsgMathUtils.hpp:107
int int3log3(int i)
Computes std::pow(3, std::floor(std::log(i) / std::log(2))), but uses only integer operations.
Definition: tsgMathUtils.hpp:83
constexpr double num_tol
Numerical tolerance for various algorithms.
Definition: tsgMathUtils.hpp:132
constexpr double pi
Half-period of the std::sin() and std::cos() functions.
Definition: tsgMathUtils.hpp:117
int pow2(int p)
Computes std::pow(2, p), but uses only integer operations.
Definition: tsgMathUtils.hpp:92
int int2log2(int i)
Computes std::pow(2, std::floor(std::log2(i))), but uses only integer operations.
Definition: tsgMathUtils.hpp:74
int intlog2(int i)
Computes std::floor(std::log2(i)), but uses only integer operations.
Definition: tsgMathUtils.hpp:65
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68