-
Notifications
You must be signed in to change notification settings - Fork 1
/
finite_difference_method.py
62 lines (53 loc) · 2.07 KB
/
finite_difference_method.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
import numpy as np
def gradient(f, x, delta = 1e-5):
"""
Returns the gradient of function f at the point x
Parameters:
f (numpy.array -> double): A scalar function accepts numpy array x
x (numpy.array): A numpy array which is the same form as the argument supplied to f
delta (double): delta used in the finite difference method
Returns:
ret (numpy.array): gradient of f at the point x
"""
#TODO
pass
def jacobian(f, x, delta = 1e-5):
"""
Returns the Jacobian of function f at the point x
Parameters:
f (numpy.array -> numpy.array): A function accepts numpy array x
x (numpy.array): A numpy array which is the same form as the argument supplied to f
delta (double): delta used in the finite difference method
Returns:
ret (numpy.array): A 2D numpy array with shape (f(x).shape[0], x.shape[0])
which is the jacobian of f at the point x
"""
#TODO
pass
def hessian(f, x, delta = 1e-5):
"""
Returns the Hessian of function f at the point x
Parameters:
f (numpy.array -> double): A scalar function accepts numpy array x
x (numpy.array): A numpy array which is the same form as the argument supplied to f
delta (double): delta used in the finite difference method
Returns:
ret (numpy.array): A 2D numpy array with shape (x.shape[0], x.shape[0])
which is the Hessian of f at the point x
"""
#TODO
pass
def test():
"""
Run tests on the above functions against some known case.
"""
Q = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]], dtype=np.float32)
q = np.array([10, 11, 12], dtype=np.float32)
x_s = np.array([0.1, 0.2, 0.3], dtype=np.float32)
f1 = lambda x: Q @ (x - x_s) + np.ones(3)
f2 = lambda x: (x - x_s) @ Q @ (x - x_s) + q @ (x - x_s) + 1
assert np.allclose(jacobian(f1, np.array(x_s)), Q)
assert np.allclose(gradient(f2, np.array(x_s)), q)
assert np.allclose(hessian(f2, np.array(x_s)), Q+Q.T)
if __name__ == "__main__":
test()