Intrepid
Intrepid_MiniTensor_Utilities.i.h
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions: Alejandro Mota (amota@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #if !defined(Intrepid_MiniTensor_Utilities_i_h)
43 #define Intrepid_MiniTensor_Utilities_i_h
44 
45 #include <cmath>
46 #include <limits>
47 
48 namespace Intrepid {
49 
50 //
51 // Sign function
52 //
53 template <typename T>
54 inline
55 int
56 sgn(T const & s)
57 {
58  return (T(0) < s) - (s < T(0));
59 }
60 
61 //
62 // Copysign function
63 //
64 template<typename T>
65 inline
66 T
67 copysign(T const & a, T const & b)
68 {
69  return b >= 0 ? std::abs(a) : -std::abs(a);
70 }
71 
72 //
73 // NaN function. Necessary to choose the proper underlying NaN
74 // for non-floating-point types.
75 // Assumption: non-floating-point types have a typedef that
76 // determines the underlying floating-point type.
77 //
78 template<typename T>
79 inline
80 typename Sacado::ScalarType<T>::type
81 not_a_number()
82 {
83  return
84  std::numeric_limits<typename Sacado::ScalarType<T>::type>::quiet_NaN();
85 }
86 
87 //
88 // Machine epsilon function. Necessary to choose the proper underlying
89 // machine epsilon for non-floating-point types.
90 // Assumption: non-floating-point types have a typedef that
91 // determines the underlying floating-point type.
92 //
93 template<typename T>
94 inline
95 typename Sacado::ScalarType<T>::type
96 machine_epsilon()
97 {
98  return
99  std::numeric_limits<typename Sacado::ScalarType<T>::type>::epsilon();
100 }
101 
102 //
103 // Compute a non-negative integer power by binary manipulation.
104 //
105 template<typename T>
106 T
107 integer_power(T const & X, Index const exponent)
108 {
109  switch (exponent) {
110  default:
111  break;
112  case 0:
113  return 1;
114  break;
115  case 1:
116  return X;
117  break;
118  case 2:
119  return X * X;
120  break;
121  case 3:
122  return X * X * X;
123  break;
124  case 4:
125  {
126  T const Y = X * X;
127  return Y * Y;
128  }
129  break;
130  }
131 
132  Index const
133  rightmost_bit = 1;
134 
135  Index const
136  number_digits = std::numeric_limits<Index>::digits;
137 
138  Index const
139  leftmost_bit = rightmost_bit << (number_digits - 1);
140 
141  Index
142  t = 0;
143 
144  for (Index j = 0; j < number_digits; ++j) {
145 
146  if (((exponent << j) & leftmost_bit) != 0) {
147 
148  t = number_digits - j - 1;
149  break;
150 
151  }
152 
153  }
154 
155  T
156  P = X;
157 
158  Index
159  i = 0;
160 
161  Index
162  m = exponent;
163 
164  while ((m & rightmost_bit) == 0) {
165  P = P * P;
166  ++i;
167  m = m >> 1;
168  }
169 
170  T
171  Y = P;
172 
173  for (Index j = i + 1; j <= t; ++j) {
174  P = P * P;
175 
176  if (((exponent >> j) & rightmost_bit) != 0) {
177  Y = Y * P;
178  }
179  }
180 
181  return Y;
182 }
183 
184 } // namespace Intrepid
185 
186 #endif // Intrepid_MiniTensor_Utilities_i_h