/usr/lib/python3/dist-packages/pyutilib/math/util.py is in python3-pyutilib 5.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # _________________________________________________________________________
#
# PyUtilib: A Python utility library.
# Copyright (c) 2008 Sandia Corporation.
# This software is distributed under the BSD License.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# _________________________________________________________________________
__all__ = ['approx_equal', 'as_number', 'isint', 'argmax', 'argmin', 'mean', 'median', 'factorial', 'perm']
import math
import sys
import six
from six.moves import zip
from six.moves import xrange
def approx_equal(A, B, abstol, reltol):
if abstol is None:
abstol = 1e-8
if reltol is None:
reltol = 1e-8
if math.fabs(A-B) <= abstol:
return True
if math.fabs(B) > math.fabs(A):
relError = math.fabs((A-B) // B)
else:
relError = math.fabs((A-B) // A)
if relError <= reltol:
return True
return False
try:
long
def as_number(value):
if type(value) in [int, float, long]:
return value
if isinstance(value,six.string_types):
try:
tmp = int(value)
return tmp
except ValueError:
pass
try:
tmp = long(value)
return tmp
except ValueError:
pass
try:
tmp = float(value)
return tmp
except ValueError:
pass
return value
except:
def as_number(value):
if type(value) in [int, float]:
return value
if isinstance(value,six.string_types):
try:
tmp = int(value)
return tmp
except ValueError:
pass
try:
tmp = float(value)
return tmp
except ValueError:
pass
return value
def isint(arg):
"""
Returns true if the argument is an integer
"""
if type(arg) is int:
return True
if type(arg) is float:
tmp = int(arg)
return (tmp == arg)
if isinstance(arg,six.string_types):
try:
num=float(arg)
tmp = int(num)
return (tmp == num)
except ValueError:
return False
return False
def argmax(array):
""" Return the index to the maximum element of an array """
return max(zip(array, xrange(len(array))))[1]
def argmin(array):
""" Return the index to the maximum element of an array """
return min(zip(array, xrange(len(array))))[1]
def mean(mylist):
"""
Returns the mean value of a list
"""
total = 1.0*sum(mylist)
length = len(mylist)
if length == 0.0:
raise ArithmeticError("Attempting to compute the mean of a zero-length list")
return (total/length)
if sys.version_info < (3,0):
from pyutilib.math.median2 import median
else:
from pyutilib.math.median3 import median
def factorial(z):
"""
Computes z!
"""
if z<0:
raise ArithmeticError("Cannot compute the factorial of a negative number")
if z==0:
return 1
else:
return z*factorial(z-1)
def perm(x,y):
"""
Computes 'x choose y'
"""
w = 1
for i in range(y+1,x+1):
w = w * i
return w/factorial(x-y)
|