-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.py
executable file
·44 lines (36 loc) · 1019 Bytes
/
root.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# author : eclipse
# email : [email protected]
def root(x, debug=False, iterMax=100):
"""
root for none-negative number
"""
if x == 0:
return 0
elif x < 0:
print '***Erro,x must be nonnegative'
return None
assert x > 0. and type(x) is float or int, 'Unrecognized input'
s = 1.0
tol = 1.e-14
for k in range(iterMax):
s1 = s
s = 0.5 * (s + x / s)
if debug:
print 'Before iteration %s,s=%20.15f' % (k, s)
if abs((s - s1) / x) < tol:
return s
return s
def test():
from numpy import sqrt
xvalues = [0., 2., 100., 10000., 1.e-4]
for x in xvalues:
print 'Testing with x = %20.15e' % x
s = root(x)
s_numpy = sqrt(x)
print ' s = %20.15e, numpy.sqrt = %20.15e' % (s, s_numpy)
assert abs(s - s_numpy) < 1.e-14, 'Disagree for x = %20.15e' % x
if __name__ == '__main__':
print 'Running test.'
test()