NCBI C++ ToolKit
njn_integer.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_INTEGER
2 #define ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_INTEGER
3 
4 /* $Id: njn_integer.hpp 44808 2010-02-18 16:10:58Z boratyng $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's offical duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================*/
28 
29 /*****************************************************************************
30 
31 File name: njn_integer.hpp
32 
33 Author: John Spouge
34 
35 Contents:
36 
37 ******************************************************************************/
38 
39 #include <corelib/ncbistl.hpp>
40 
41 #include "njn_ioutil.hpp"
42 
44 BEGIN_SCOPE(blast)
45 
46 BEGIN_SCOPE(Njn)
47 BEGIN_SCOPE(Integer)
48 
49 
50  template <class Int>
51  Int minimum (
52  Int i, // number being divided
53  Int j) // divisor
54  {
55  return i < j ? i : j;
56  }
57 
58  template <class Int>
59  Int maximum (
60  Int i, // number being divided
61  Int j) // divisor
62  {
63  return i > j ? i : j;
64  }
65 
66  template <class Int>
67  Int mod ( // Portable modular function %
68  Int i, // number being divided
69  Int j) // divisor
70  {
71  Int abs_j = j >= 0 ? j : -j;
72 
73  if (j == 0)
74  {
75  Njn::IoUtil::abort ("Nks_Mod : j == 0");
76  }
77 
78  if (i < 0)
79  {
80  Int k = (i >= 0 ? i : -i) % abs_j;
81  return k == 0 ? k : abs_j - k;
82  }
83  else
84  {
85  return i % abs_j;
86  }
87  }
88 
89  template <class Int>
90  Int euclidAlgorithm ( // Euclid's algorithm returning the greatest common divisor (i, j)
91  Int i_,
92  Int j_)
93  {
94  Int abs_i = maximum <Int> (i_ >= 0 ? i_ : -i_, j_ >= 0 ? j_ : -j_);
95  Int abs_j = minimum <Int> (i_ >= 0 ? i_ : -i_, j_ >= 0 ? j_ : -j_);
96  Int remainder = 0;
97 
98  while (0 < abs_j)
99  {
100  remainder = mod <Int> (abs_i, abs_j);
101  abs_i = abs_j;
102  abs_j = remainder;
103  }
104 
105  return abs_i;
106  }
107 
108  template <class Int, class ConstIntegerPtr>
109  Int euclidAlgorithmVector ( // Euclid's algorithm returning the greatest common divisor (i, j)
110  ConstIntegerPtr begin_,
111  ConstIntegerPtr end_)
112  {
113  assert (begin_ <= end_);
114 
115  if (begin_ == end_) return 0;
116 
117  Int gcd = *begin_;
118 
119  for (ConstIntegerPtr k = begin_ + 1; k != end_; k++)
120  {
121  gcd = euclidAlgorithm (gcd, *k);
122  }
123 
124  return gcd;
125  }
126 
127  template <class Int>
128  Int minusOnePower ( // (-1)^i
129  Int i) // exponent
130  {
131  return mod <Int> (i, 2) == 0 ? 1 : -1;
132  }
133 
134  template <class Real, class Int>
135  Real integerPower (Real x, Int n) // integer power function
136  {
137  if (x == 0.0)
138  {
139  if (n < 0)
140  {
141  Njn::IoUtil::abort ("Int::integerPower <class Real, class Int> : negative exponent of zero");
142  }
143  else if (n == 0)
144  {
145  return 1.0;
146  }
147  else
148  {
149  return 0.0;
150  }
151  }
152 
153  Real y = 1.0;
154  Int i = 0;
155 
156  for (i = n > 0 ? n : -n; i > 0 ; i /= 2)
157  {
158  if (i % 2 != 0)
159  {
160  y *= x;
161  }
162 
163  x *= x;
164  }
165 
166  if (n < 0)
167  {
168  y = 1.0 / y;
169  }
170 
171  return y;
172  }
173 
174  template <class Real, class UnsignedInteger>
175  Real integerPositivePower (Real x, UnsignedInteger n) // integer power function
176  {
177  if (x == 0.0)
178  {
179  if (n == 0)
180  {
181  return 1.0;
182  }
183  else
184  {
185  return 0.0;
186  }
187  }
188 
189  Real y = 1.0;
190  UnsignedInteger i = 0;
191 
192  for (i = n; i > 0 ; i /= 2)
193  {
194  if (i % 2 != 0)
195  {
196  y *= x;
197  }
198 
199  x *= x;
200  }
201  return y;
202  }
203 
204  template <class Int, class IntTwo>
205  Int intPower (Int x, IntTwo n) // integer power function
206  {
207  assert (n >= 0);
208 
209  if (x == 0)
210  {
211  return n == 0 ? 1 : 0;
212  }
213 
214  Int y = 1;
215  IntTwo i = 0;
216 
217  for (i = n > 0 ? n : -n; i > 0 ; i /= 2)
218  {
219  if (i % 2 != 0)
220  {
221  y *= x;
222  }
223 
224  x *= x;
225  }
226  return y;
227  }
228 
229 END_SCOPE(Integer)
230 END_SCOPE(Njn)
231 
232 END_SCOPE(blast)
234 
235 #endif //! ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_INTEGER
236 
237 
238 
239 
static Int8 gcd(Int8 x, Int8 y)
Definition: expr.cpp:293
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
int i
yy_size_t n
The NCBI C++/STL use hints.
Int euclidAlgorithm(Int i_, Int j_)
Definition: njn_integer.hpp:90
Int minusOnePower(Int i)
Int intPower(Int x, IntTwo n)
Int mod(Int i, Int j)
Definition: njn_integer.hpp:67
Int maximum(Int i, Int j)
Definition: njn_integer.hpp:59
Real integerPower(Real x, Int n)
Int minimum(Int i, Int j)
Definition: njn_integer.hpp:51
Real integerPositivePower(Real x, UnsignedInteger n)
Int euclidAlgorithmVector(ConstIntegerPtr begin_, ConstIntegerPtr end_)
void abort()
#define assert(x)
Definition: srv_diag.hpp:58
Modified on Fri Sep 20 14:58:10 2024 by modify_doxy.py rev. 669887