-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.q
More file actions
126 lines (91 loc) · 1.67 KB
/
utils.q
File metadata and controls
126 lines (91 loc) · 1.67 KB
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
// Utils functions
// Machine Learning for Q Library - (MLQ-lib)
// Documentation:
// Constants
pi:acos -1;
// Random tools
round:{
floor x+0.5
};
/ Bubble sorting algorithm
/ @example bubbleSort[(2;24;5;17;39;8;47;6;1)]
bubbleSort:{
n:-1+count x;
i:0;
do[n;j:0;
do[n;
if[>/[x k:j+0 1]; x[k]:reverse x[k]];
j+:1];i+:1];
x
};
// Trigonometric tools
/ Hyperbolic sine
sinh:{
0.5 * exp [x] - exp neg x
};
/ Hyperbolic cosine
cosh:{
0.5 * exp [x] + exp neg x
};
/ Hyperbolic tangent
tanh:{
(exp[x] - exp neg x) % (exp[x] + exp neg x)
};
/ Sigmoid function: f(x)->[0,1]
sigmoid:{
1 % (1 + exp neg x)
};
// Statistical tools
/ Returns root mean squared error
rmse:{
sqrt sum (x xexp 2) % (count x)
};
/ Exponentially-weighted moving average
ewma:{
{y+x*z-y}[x:2%1+x]\[y]
};
/ Generate n x m random variates ~N(0,1)
rand_:{
(x;y)#(x*y)?1f
};
/ Generate a n x m matrix of random normally distributed variates
norm:{
(x;y)#raze sqrt[-2*log (x*y)?1f]*/:(sin;cos)@\:(2*pi)*(x*y)?1f
};
k).q.var:{avg x*x:$[t&78h>t:@x;x-avg x;x-\:avg x]};
// Matrix manipulation tools
repmat:{[data;w;l]
flip w#enlist raze l#enlist flip data
};
/ Returns an n x m matrix of 1
ones:{
(x;y)#1f
};
/ Returns an n x m matrix of 0
zeros:{
(x;y)#0f
};
/ Returns dimensions of a matrix
size:{
(count x;count flip x)
};
/ Identity matrix generation
id:{
(x,x)#1,x#0
};
/ Returns the range range (min/max) of a vector
range:{
(min x;max x)
};
/ Returns max values of 2 arrays
pmax:{
(count x)#(desc raze x,y)
};
/ Kronecker tensor product
kron:{
raze(raze'')(flip')x*\:\:y
};
/ Retrieves the diagonal of a matrix
diag:{
x ./: 2#'(til count x)
};