-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.typ
204 lines (168 loc) · 4.09 KB
/
lib.typ
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// == types ==
#let arrarr(a,b) = (type(a) == array and type(b) == array)
#let arrflt(a,b) = (type(a) == array and type(b) != array)
#let fltarr(a,b) = (type(a) != array and type(b) == array)
#let fltflt(a,b) = (type(a) != array and type(b) != array)
#let is-arr(a) = (type(a) == array)
#let is-mat(m) = (is-arr(m) and is-arr(m.at(0)))
#let matmat(m,n) = is-mat(m) and is-mat(n)
#let matflt(m,n) = is-mat(m) and type(n) != array
#let fltmat(m,n) = is-mat(n) and type(m) != array
// == boolean ==
#let isna(v) = {
if is-arr(v){
v.map(i => if (type(i)==float){i.is-nan()} else {false})
}
else{
if (type(v)==float){v.is-nan()} else {false}
}
}
#let all(v) ={
if is-arr(v){
v.flatten().all(a => a == true or a ==1)
}
else{
v == true or v == 1
}
}
#let op(a,b, fun) ={
// generic operator with broacasting
if matmat(a,b) {
a.zip(b).map(((a,b)) => op(a,b, fun))
}
else if matflt(a,b){ // supports n-dim matrices
a.map(v=> op(v,b, fun))
}
else if fltmat(a,b){
b.map(v=> op(a,v, fun))
}
else if arrarr(a,b) {
a.zip(b).map(((i,j)) => fun(i,j))
}
else if arrflt(a,b) {
a.map(a => fun(a,b))
}
else if fltarr(a,b) {
b.map(i => fun(a,i))
}
else {
fun(a,b)
}
}
#let _eq(i,j, equal-nan) ={
i==j or (all(isna((i,j))) and equal-nan)
}
#let eq(u,v, equal-nan: false) = {
// Checks for equality element wise
// eq((1,2,3), (1,2,3)) = (true, true, true)
// eq((1,2,3), 1) = (true, false, false)
let _eqf(i,j)={_eq(i,j, equal-nan)}
op(u,v, _eqf)
}
#let any(v) ={
// check if any item is true after iterating everything
if is-arr(v){
v.flatten().any(a => a == true or a ==1)
}
else{
v == true or v == 1
}
}
#let all-eq(u,v) = all(eq(u,v))
#let apply(a, fun) ={
// vectorize
// consider returnding a function of a instead?
if is-arr(a){ //recursion exploted for n-dim
a.map(v=>apply(v, fun))
}
else{
fun(a)
}
}
#let abs(a)= apply(a, calc.abs)
// == Operators ==
#let _add(a,b)=(a + b)
#let _sub(a,b)=(a - b)
#let _mul(a,b)=(a * b)
#let _div(a,b)= if (b!=0) {a/b} else {float.nan}
#let add(u,v) = op(u,v, _add)
#let sub(u, v) = op(u,v, _sub)
#let mult(u, v) = op(u,v, _mul)
#let div(u, v) = op(u,v, _div)
#let pow(u, v) = op(u,v, calc.pow)
// == vectorial ==
#let normalize(a, l:2) = {
// normalize a vector, defaults to L2 normalization
let aux = pow(pow(abs(a),l).sum(),1/l)
a.map(b => b/aux)
}
// dot product
#let dot(a,b) = mult(a,b).sum()
// == Algebra, trigonometry ==
#let sin(a) = apply(a,calc.sin)
#let cos(a) = apply(a,calc.cos)
#let tan(a) = apply(a,calc.tan)
#let log(a) = apply(a, j => if (j>0) {calc.log(j)} else {float.nan} )
// matrix
#let transpose(m) = {
// Get dimensions of the matrix
let rows = m.len()
let cols = m.at(0).len()
range(0, cols).map(c => range(0, rows).map(r => m.at(r).at(c)))
}
#let matmul(a,b) = {
let bt = transpose(b)
a.map(a_row => bt.map(b_col => dot(a_row,b_col)))
}
// others:
#let linspace = (start, stop, num) => {
// mimics numpy linspace
let step = (stop - start) / (num - 1)
range(0, num).map(v => start + v * step)
}
#let logspace = (start, stop, num, base: 10) => {
// mimics numpy logspace
let step = (stop - start) / (num - 1)
range(0, num).map(v => calc.pow(base, (start + v * step)))
}
#let geomspace = (start, stop, num) => {
// mimics numpy geomspace
let step = calc.pow( stop / start, 1 / (num - 1))
range(0, num).map(v => start * calc.pow(step,v))
}
#let to-str(a) = {
if (type(a) == bool){
if(a){
"value1"
}
else {
"value2"
}
}
else{
str(a)
}
}
#let print(M) = {
if is-mat(M) {
eval("$ mat(" + M.map(v => v.map(j=>to-str(j)).join(",")).join(";")+ ") $")
}
else if is-arr(M){
eval("$ vec(" + M.map(v => str(v)).join(",")+ ") $")
}
else{
eval(" $"+str(M)+"$ ")
}
}
#let p(M) = {
let scope = (value1: "true", value2: "false")
if is-mat(M) {
eval("$mat(" + M.map(v => v.map(j=>to-str(j)).join(",")).join(";")+ ")$", scope: scope)
}
else if is-arr(M){
eval("$vec(" + M.map(v => str(v)).join(",")+ ")$")
}
else{
eval("$"+str(M)+"$")
}
}