-
Notifications
You must be signed in to change notification settings - Fork 2
/
MyMath.hs
136 lines (98 loc) · 2.69 KB
/
MyMath.hs
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
{-
MyMath: maths functions
-}
module MyMath
(
sgn,
hardlim,
hardlims,
sigmoid,
dsigmoid,
dtanh,
spiky,
gauss,
gauss_std,
same_sign,
trapint
) where
-- type OneDimFunc a = Num a => a -> a
type RealOneDimFunc = Double -> Double
sgn :: RealOneDimFunc
sgn x | x<0 = -1
| x>0 = 1
| x==0 = 0
hardlim :: RealOneDimFunc
hardlim x | x < 0 = 0
| x == 0 = 0
| x > 0 = 1
hardlims :: RealOneDimFunc
hardlims x | x < 0 = -1
| x == 0 = 0
| x > 0 = 1
sigmoid :: RealOneDimFunc
sigmoid x = 1 / (1 + exp (-x))
dsigmoid :: RealOneDimFunc
dsigmoid x = y * (1 - y)
where y = sigmoid x
dtanh :: RealOneDimFunc
dtanh x = (1+y) * (1-y)
where y = tanh x
spiky :: Double -> Double -> Double
spiky x p | x == 0 =0
| x /= 0 =abs(x)**(-1 / (2*p))
gauss :: Double -> Double -> RealOneDimFunc
gauss mu sigma = \x -> exp(-(x - mu)**2 / (2 * sigma**2))
gauss_std :: RealOneDimFunc
gauss_std x = gauss x 0 1
same_sign :: Double -> Double -> Bool
same_sign a b = (a<0 && b<0) || (a>0 && b>0) || (a==0 || b==0)
dilation :: Double -> RealOneDimFunc -> RealOneDimFunc
dilation x f y = x * (f x*y)
trapint :: RealOneDimFunc -> Double -> Double -> Int -> Double
trapint f a b n = if n == 0 then ((f a) + (f b)) * (b - a) / 2
else let m = (a + b) / 2 in (trapint f a m n-1) + (trapint f m b n-1)
antider :: RealOneDimFunc -> RealOneDimFunc
antider f = \x -> trapint f 0 x 10
err :: RealOneDimFunc
err = (*(2/pi)) . (antider gauss_std)
-- fejer :: Int -> RealOneDimFunc
-- fejer n t
-- | n == 0 = 1
-- | let tol=0.000001 in abs (floor t - t) < tol = n + 1
-- | otherwise = (sin ((n + 1) * pi * t)) / ((sin (pi * t))^2 * (n + 1))
-- bump :: Double -> Double -> RealOneDimFunc
-- -- bump type function
-- -- int(bump(lb,ub)) == (ub-lb)/2 * I, I=0.444
-- -- == (2*x-lb-ub)/(ub-lb)
-- bump lb ub x
-- | (x <= -1 || x >= 1) = 0
-- | otherwise = exp (-1/(1 - x^2))
-- where x = 2*(x-lb) / (ub-lb)-1
main :: IO ()
main = do
putStrLn "The factorial of 5 is:"
print (bump 0 2 1)
{-
def dbump(x, lb=-1, ub=1):
# bump type function
# int(bump(lb,ub)) == (ub-lb)/2 * I, I=0.444
x = 2*(x-lb)/(ub-lb)-1
if x <= -1 or x >= 1 or x == 0:
return 0
else:
return -np.exp(-1/(1-x**2)) * 2*x / (1-x**2)**2 / (ub-lb)
def dirichlet(t, n=1):
if n == 0:
return 1
if np.floor(t) == t:
return 2 * n + 1
else:
return np.sin((2 * n + 1) * np.pi * t) / np.sin(np.pi * t)
def dirichletez(t, n=1):
if n == 0:
return 1
if np.floor(t) == t:
return 2 * n + 1
else:
return np.sin((2 * n + 1) * np.pi * t) / (t * np.pi)
-}