-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sm
177 lines (152 loc) · 3.4 KB
/
utils.sm
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
__init_utils
pass
pass
# do nothing
return
len 1
# print length of input vector
echo $1: $(dimen($1))
mima 1
# print minimum and maximum value of input vector
echo $(min($1)) $(max($1))
resort 2
# sort two input vectors along the first one, in reverse order
local set _x = $1
local set _y = $2
sort {_x _y}
set $1=reverse(_x)
set $2=reverse(_y)
linspace 3
# : linspace start stop num
# return evenly spaced numbers over a specified interval
set $0=$1,$2,$(($2-$1)/($3-1))
zeros 1
# return a vector of zeros, with dimension $1
set dimen($0)=$1
ones 1
# return a vector of ones, with dimension $1
set $0=zeros($1)+1
empty
# empty vector
set $0=zeros(0)
zeros_like 1
# return a vector of zeros, same dimension as $1
set $0=zeros(dimen($1))
ones_like 1
# return a vector of ones, same dimension as $1
set $0=ones(dimen($1))
random_like 1
# return a vector of random values, same dimension as $1
set $0=random(dimen($1))
random_int 12
# : random_int N [M=10]
# generate N integer random numbers between 0 and M-1
local define M 10
if ($?2) {define M $2}
set $0=int(random($1)*$M)
random_gauss 13
# : random_gauss N [mean=0 [sigma=1]]
# generate N random numbers N(μ,σ) distributed
local define m 0
if ($?2) {define m $2}
local define s 1
if ($?3) {define s $3}
set $0=gaussdev($1)*$s+$m
d10
# roll a 10-sided dice
#echo $(random_int(1,10)+1)
set $0=random_int(1,10)+1
m2a 4
# : m2a i j k Nx
# convert matrix indices (i,j) to array index k
# Nx is the horizontal dimension
define $3 ($1+$2*$4)
a2m 4
# : m2a k i j Nx
# convert array index k to matrix indices (i,j)
# Nx is the horizontal dimension
define $2 (int($1/$4))
define $3 ($1%$4)
subset 3
# : subset x y xx
# return a subset from y where xx matches x
set $3=sort($3)
set $0=$2 if (is_present($3,$1))
slice 3
# : slice vec imin imax
# return a slice of input vector
declare $0 0
local define imin $2
local define imax $3
if ($imin<0) {define imin (dimen($1)+$imin)}
if ($imax<0) {define imax (dimen($1)+$imax)}
if ($imin<0) {define imin 0}
if ($imax==0) {define imax (dimen($1))}
if ($imax<$imin) {return}
local define dim ($imax-$imin)
declare $0 $dim
define i local
do i=0,$dim-1 {
set $0[$i]=$1[$imin+$i]
}
sfind 2
define i local
do i=0,dimen($1)-1 {
if ($1[$i]=='$!2') {
set $0=$i
return
}
}
slice_all 29
define i local
do i=3,9 {
if ($?$i) {set $$i=slice($$i,$1,$2)}
}
nindex 2
# return indices of $2 in $1
# $1 needs to be sorted beforehand
# returns -1 for elements of $2 not in $1
if (!dimen($1)) {
set $0 = -ones_like($2)
return
}
local set idx = ifloor($1,$2)
set idx = idx > -1 ? idx : 0
set $0 = $1[idx]==$2 ? idx : -1
is_present 2
# check if $2 is present in $1
# returns -1 for elements of $2 not in $1
local set tmp = sort($1)
set $0 = nindex(tmp,$2)>-1 ? 1 : 0
sindex 2
# return the index of string $2 in string list $1
define i local
do i=0,dimen($1)-1 {
if ($1[$i]=='$!2') {
set $0=$i
return
}
}
unique 1
# find the unique elements of a vector
local set x=sort($1)
declare $0 dimen(x)
define i local
set $0[0] = x[0]
local define k 0
do i=1,dimen(x)-1 {
if (x[$i]!=$0[$k]) {
define k $($k+1)
set $0[$k]=x[$i]
}
}
set $0=slice($0,0,$k+1)
count 2
# count the occurrences of the elements of $2 in $1
set tmp local
define i local
set $0 = zeros_like($2)
do i=0,dimen($2)-1 {
set tmp = $1 if ($1==$2[$i])
set $0[$i] = dimen(tmp)
}