-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStuffLibrary.py
145 lines (114 loc) · 3.96 KB
/
StuffLibrary.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
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
139
140
141
142
143
'''This file contains a bunch of different stuff that I have already made, so I may simply pull it from here without having to remake it every time'''
import numpy as np
from scipy import constants
"""Calculus"""
#Numerical Differentiation
def deriv(x, y, xrange): #x, y are the data used to find the derivatives. xrange is the points at which the derivative is desired.
slopes = []
for i in range(1, len(y)-1):
central_diff = (y[i-1] - y[i+1])/(x[i-1] - x[i+1])
slopes.append(central_diff)
interp_deriv = interpolate.interp1d(x[1:-1], slopes, kind="cubic", bounds_error=False, fill_value="extrapolate")
derivatives = interp_deriv(xrange)
return derivatives
#Secant Method of Differentiating a known function to any desired precision.
def Sec_Method(func, x_guess, tol, maxiter, h, *func_args):
x = x_guess
# tol = 1e-7
# maxiter = 50
# h = 1e-4
for i in range(maxiter):
deriv = (func(x + h, *func_args) - func(x - h, *func_args))/(2*h)
increment = (0.5 - func(x, *func_args))/deriv
x += increment
if np.abs(increment) < tol:
break
return x
#Trapezoidal Integration
def Trap(f, x1, x2, N):
h = (x2 - x1)/N
t = np.linspace(x1, x2, N + 1)
weights = np.ones(len(t))*h
weights[0] = weights[-1] = h/2
def ret(*args):
return np.sum(weights*f(args))
return ret
"""Distributions and Statistics"""
#Gaussian Normal
def Gaussian(x, mean, std):
pi = np.pi
a = 1/(std*np.sqrt(2*pi))
b = np.exp(-1/2*((x-mean)/std)**2)
ret = a*b
return ret
#Maxwell-Boltzmann
def Boltzmann(frequency, temperature, mass):
k = constants.k
x = (mass/(2*np.pi*k*temperature))**(3/2)
y = np.exp(-mass*frequency**2/(2*k*temperature))
ret = 4*np.pi*(frequency**2)*x*y
return ret
#General Covariance Functions
#constant
def Constant(x1, x2, a):
_x1, _x2 = np.meshgrid(x1,x2)
r = a**2*(_x1 - _x2)
return r
#Squared Exoponential
def Squared_Exp(x1, x2, a, h):
_x1, _x2 = np.meshgrid(x1, x2)
r = a**2*np.exp(-(_x1 - _x2)**2/(2*h**2))
return r
#Ornstein–Uhlenbeck
def Orn_Uhl(x1, x2, a, h):
_x1, _x2 = np.meshgrid(x1, x2)
r = a**2*np.exp(-(np.abs(_x1 - _x2)/h))
return r
#Periodic
def Periodic(x1, x2, a, h):
_x1, _x2 = np.meshgrid(x1, x2)
r = a**2*np.exp(-(2*np.sin(_x1/2 - _x2/2)**2/h**2))
return r
#Generalized Gaussian Process Regression
def GPR(params, sig_data, x, model_domain, y, cov=Constant, scaling=1):
a = params[0]
h = params[1]
Cov_data = scaling*np.identity(len(x))*sig_data**2
Kinv = np.linalg.inv(scaling*cov(x, x, a, h) + Cov_data)
y_mean = np.linalg.multi_dot([scaling*cov(x, model_domain, a, h), Kinv, y])
yvar = scaling*cov(model_domain, model_domain, a, h)
yvar -= np.linalg.multi_dot([scaling*cov(x, model_domain, a, h), Kinv, scaling*cov(model_domain, x, a, h)])
gp_err = np.sqrt(np.diag(yvar))
return y_mean, gp_err
"""Astronomy"""
rad_Earth = 6.371e6
rad_Jupiter = 6.991e7
rad_Sun = 6.957e8
mass_Earth = 5.972e24
mass_Jupiter = 1.898e27
mass_Sun = 1.988e30
#For the converters "To" and "Given" must each take the value of "Meters", "Earth", "Jupiter", or "Sun"
#Radius Converter
def Radius_Conv(radius, To="Jupiter", Given="Meters"):
conv_to = "rad_" + To
conv_from = "rad_" + Given
if Given == "Meters":
r = radius/conv_to
if Given != "Meters":
r_met = radius*conv_from
r = r_met/conv_to
return r
#Radius Converter
def Mass_Conv(mass, To="Jupiter", Given="Meters"):
conv_to = "mass_" + To
conv_from = "mass_" + Given
if Given == "Meters":
m = mass/vars(conv_to)
if Given != "Meters":
m_met = mass*conv_from
m = m_met/conv_to
return m
#Orbital Period
def Orb_Period(mass_star, sm_axis):
T = 2*np.pi*np.sqrt(sm_axis**3/(mass_star*6.674e-11))
return T