Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / disembl / biopython-1.50 / Bio / mathfns.py
1 # Copyright 2000 by Jeffrey Chang.  All rights reserved.
2 # This code is part of the Biopython distribution and governed by its
3 # license.  Please see the LICENSE file that should have been included
4 # as part of this package.
5
6 """This provides useful general math tools (DEPRECATED).
7
8 This module and its C code equivalent are considered to be deprecated, and
9 are likely to be removed in a future release of Biopython.  Please get in
10 touch via the mailing list if this will affect you.
11
12 Functions:
13 fcmp       Compare two floating point numbers, up to a specified precision.
14 intd       Represent a floating point number as an integer.
15 safe_log   log, but returns an arbitrarily small number for log(0).
16 safe_exp   exp, but returns a large or small number instead of overflows.
17
18 """
19 import warnings
20 warnings.warn("Bio.mathfns and its C code equivalent Bio.cmathfns are" \
21               +" deprecated, and will be removed in a future release of"\
22               +" Biopython.  If you want to continue to use this code,"\
23               +" please get in contact with the Biopython developers via"\
24               +" the mailing lists to avoid its permanent removal from"\
25               +" Biopython.", \
26               DeprecationWarning)
27
28 import math
29
30 def fcmp(x, y, precision):
31     """fcmp(x, y, precision) -> -1, 0, or 1"""
32     if math.fabs(x-y) < precision:
33         return 0
34     elif x < y:
35         return -1
36     return 1
37
38 def intd(x, digits_after_decimal=0):
39     """intd(x[, digits_after_decimal]) -> int x, rounded
40
41     Represent a floating point number with some digits after the
42     decimal point as an integer.  This is useful when floating point
43     comparisons are failing due to precision problems.  e.g.
44     intd(5.35, 1) -> 54.
45
46     """
47     precision = 10.**digits_after_decimal
48     if x >= 0:
49         x = int(x * precision + 0.5)
50     else:
51         x = int(x * precision - 0.5)
52     return x
53
54 def safe_log(n, zero=None, neg=None):
55     """safe_log(n, zero=None, neg=None) -> log(n)
56
57     Calculate the log of n.  If n is 0, returns the value of zero.  If n is
58     negative, returns the value of neg.
59
60     """
61     if n < 0:
62         return neg
63     elif n < 1E-100:
64         return zero
65     return math.log(n)
66
67 LOG2 = math.log(2)
68 def safe_log2(n, zero=None, neg=None):
69     """safe_log2(n, zero=None, neg=None) -> log(n)
70
71     Calculate the log base 2 of n.  If n is 0, returns the value of
72     zero.  If n is negative, returns the value of neg.
73
74     """
75     l = safe_log(n, zero=zero, neg=neg)
76     if l is None:
77         return l
78     return l/LOG2
79
80 def safe_exp(n, under=None, over=None):
81     """safe_exp(n, under=None, over=None) -> e**n
82
83     Guaranteed not to overflow.  Instead of overflowing, it returns
84     the values of 'under' for underflows or 'over' for overflows.
85
86     """
87     try:
88         return math.exp(n)
89     except OverflowError:
90         if n < 0:
91             return under
92         return over
93     raise "How did I get here?"
94
95 # Try and load C implementations of functions.  If I can't,
96 # then just ignore and use the pure python implementations.
97 try:
98     from cmathfns import *
99 except ImportError:
100     pass