-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton.py
executable file
·45 lines (37 loc) · 1.69 KB
/
newton.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
45
#!/usr/bin/env python
import numpy as np
import scipy.optimize
import logging
import warnings
def newton_cosh_for_m(i, j, ave_cor, guess, T):
def f(m):
numerator = np.exp(-m*i) + np.exp(-1.0*m*(T-i))
denominator = np.exp(-m*j) + np.exp(-1.0*m*(T-j))
return numerator/denominator - ave_cor[i]/ave_cor[j]
logging.debug("newtons method to find cosh (T={}) emass starting guess"
" {}, {},{}, fguess={}".format(T, guess, i, j, f(guess)))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
result = scipy.optimize.newton(f, guess, tol=1.48e-6)
except RuntimeError:
logging.error("Newtons failed to converge (T={}) using standard for {},{}".format(T, i, j))
logging.error("Newtons failed, fallback is {}".format(guess))
return guess
logging.debug("newtons method converged to {} from {}".format(result, guess))
return result
def newton_sinh_for_m(i, j, ave_cor, guess, T):
def f(m):
numerator = np.exp(-m*i) - np.exp(-1.0*m*(T-i))
denominator = np.exp(-m*j) - np.exp(-1.0*m*(T-j))
return numerator/denominator - ave_cor[i]/ave_cor[j]
logging.debug("newtons method to find sinh (T={}) emass starting guess"
" {}, {},{}, fguess={}".format(T, guess, i, j, f(guess)))
try:
result = scipy.optimize.newton(f, guess)
except RuntimeError:
logging.error("Newtons failed to converge (T={}) using standard for {},{}".format(T, i, j))
logging.error("Newtons failed, fallback is {}".format(guess))
return guess
logging.debug("newtons method converged to {}".format(result))
return result