-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTools.py
52 lines (34 loc) · 973 Bytes
/
Tools.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 09:16:42 2019
@author: rohrdr
"""
from numpy.linalg import norm
import numpy as np
threshold = 1e-07
eps = 1e-08
def eval_err(z, y, errmsg):
result = True
error = norm(z - y)
error = np.squeeze(error) / np.linalg.norm(z)
if error > threshold:
print(errmsg)
print("error = " + str(error))
result = False
return result
def grad_num(x, func, *args, **kwargs):
assert(isinstance(x, np.ndarray))
assert(x.shape[1] == 1)
n = x.shape[0]
y = func(x, *args, **kwargs)
grad = np.zeros((n, 1))
for i in range(n):
x[i] += eps
y2 = func(x, *args, **kwargs)
grad[i] += np.sum(y2 - y).T / eps
x[i] -= 2 * eps
y2 = func(x, *args, **kwargs)
grad[i] += np.sum(y - y2).T / eps
x[i] += eps
return grad / 2