-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathFunctions.py
77 lines (66 loc) · 1.59 KB
/
mathFunctions.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
class Maths():
e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353
@staticmethod
def dot(a, b):
if len(a) == len(b):
sum = 0
for ix in range(len(a)):
sum += a[ix] * b[ix]
return sum
else:
raise Exception("Size Of Arrays Is Different")
@staticmethod
def tanh(a):
if isinstance(a, (int, float, long)):
sinh = (Maths.e ** (2 * a)) - 1
cosh = (Maths.e ** (2 * a)) + 1
tanh = sinh / cosh
else:
raise Exception("Integer Required")
return tanh
@staticmethod
def dtanh(a):
if isinstance(a, (int, float, long)):
dtanh = 1 - (tanh(a) ** 2)
else:
raise Exception("Integer Required")
return dtanh
@staticmethod
def sigmoid(a):
if isinstance(a, (int, float, long)):
num = Maths.e ** a
sigmoid = num / (num + 1)
else:
raise Exception("Integer Required")
return sigmoid
@staticmethod
def dsigmoid(a):
if isinstance(a, (int, float, long)):
dsigmoid = Maths.sigmoid(a) * (1 - Maths.sigmoid(a))
else:
raise Exception("Integer Required")
return dsigmoid
@staticmethod
def listProd(A, i):
return [(t * i) for t in A]
@staticmethod
def listAdd(A, B):
return [A[x] + B[x] for x in range(len(A))]
@staticmethod
def MSE(A, B):
E = 0.0
for i in xrange(len(A)):
E += (A[i] - B[i]) ** 2
return E / len(A)
@staticmethod
def ArrDiff(A, B):
return [A[x] - B[x] for x in range(len(A))]
@staticmethod
def ArrProd(A, B):
return [A[x] * B[x] for x in range(len(A))]
@staticmethod
def eTotal(A, B): # A: Actual, B: Target
E = 0.0
for i in xrange(len(A)):
E += (A[i] - B[i]) ** 2
return E / 2