-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilt-in-funcs.tex
270 lines (192 loc) · 7.9 KB
/
built-in-funcs.tex
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
\subsection{Unary operators}
\label{sec:builtin-unary-ops}
The operators are listed in order of precedence, with highest precedence at the
top.
% NOTE: Check BLOGParser.cup for precedence.
\begin{table}[H]
\centering
\caption{Unitary Operators}
\begin{tabular}{ c c c c }
\toprule
operator & argument type & result type & meaning \\
\midrule
\verb|-| & Integer & Integer & minus \\
\verb|-| & Real & Real & minus \\
\verb|-| & RealMatrix & RealMatrix & minus \\
\verb|!| & Boolean & Boolean & negation \\
\bottomrule
\end{tabular}
\end{table}
\subsection{Binary operators}
\label{sec:builtin-binary-ops}
The operators are listed in order of precedence, with highest precedence at the
top.
% NOTE: Check BLOGParser.cup for precedence.
\begin{table}[H]
\centering
\caption{Binary Operators}
\begin{tabular}{ c c c c c }
\toprule
left-hand type & operator & right-hand type & result type & meaning \\
\midrule
Integer & \verb|*| & Integer & Integer & multiply \\
Real & \verb|*| & Real & Real & multiply \\
Timestep & \verb|*| & Integer & Timestep & multiply \\
Integer & \verb|/| & Integer & Integer & divide \\
Real & \verb|/| & Real & Real & divide \\
Timestep & \verb|/| & Integer & Timestep & divide \\
Integer & \verb|%| & Integer & Integer & modulus \\
Real & \verb|^| & Real & Real & power \\
Integer & \verb|+| & Integer & Integer & plus \\
Real & \verb|+| & Real & Real & plus \\
Timestep & \verb|+| & Integer & Timestep & plus \\
String & \verb|+| & String & String & concatenate \\
Integer & \verb|-| & Integer & Integer & minus \\
Real & \verb|-| & Real & Real & minus \\
Timestep & \verb|-| & Integer & Timestep & minus \\
Integer & \verb|<| & Integer & Boolean & less than \\
Real & \verb|<| & Real & Boolean & less than \\
Timestep & \verb|<| & Timestep & Boolean & less than \\
Integer & \verb|>| & Integer & Boolean & greater than \\
Real & \verb|>| & Real & Boolean & greater than \\
Timestep & \verb|>| & Timestep & Boolean & greater than \\
Integer & \verb|<=| & Integer & Boolean & less than or equal \\
Real & \verb|<=| & Real & Boolean & less than or equal \\
Timestep & \verb|<=| & Timestep & Boolean & less than or equal \\
Integer & \verb|>=| & Integer & Boolean & greater than or equal \\
Real & \verb|>=| & Real & Boolean & greater than or equal \\
Timestep & \verb|>=| & Timestep & Boolean & greater than or equal \\
Any & \verb|==| & Any & Boolean & equal to \\
Any & \verb|!=| & Any & Boolean & not equal to \\
Boolean & \verb|&| & Boolean & Boolean & and \\
Boolean & \verb||| & Boolean & Boolean & or \\
Boolean & \verb|=>| & Boolean & Boolean & implies \\
\bottomrule
\end{tabular}
\end{table}
\subsection{Constants}
\label{sec:builtin-constants}
\blog|Real e|
\myindent The base of the natural logarithm (approximately equal to $2.71828$).
\blog|Real pi|
\myindent The $\pi$ constant (approximately equal to $3.14159$).
\subsection{Matrix operations}
\label{sec:builtin-matrix-ops}
For functions, a type signature like \verb|RealMatrix eye(Integer dim)| means
that the function \verb|eye| takes a single \verb|Integer| and produces a
\verb|RealMatrix|.
\blog|RealMatrix inv(RealMatrix x)|
\myindent Inverse of the matrix \verb|x|.
\blog|RealMatrix transpose(RealMatrix x)|
\myindent Transpose of the matrix \verb|x|.
\blog|Real det(RealMatrix x)|
\myindent Determinant of the matrix \verb|x|.
\blog|Real trace(RealMatrix x)|
\myindent Trace of the matrix \verb|x|, i.e. the sum of the elements on the diagonal.
\blog|RealMatrix diag(RealMatrix vals)|
\myindent Diagonal matrix with the given values on the diagonal. \verb|vals|
must be a column vector.
\blog|RealMatrix repmat(RealMatrix m, Integer rows, Integer cols)|
\myindent Return the matrix \verb|m| tiled \verb|rows| times vertically and
\verb|cols|
times horizontally.
\blog|RealMatrix sum(RealMatrix m)|
\myindent Column-wise sum of a matrix.
For example, \verb|sum([1, 2; 3, 4])| returns \verb|[4, 6]|.
\blog|RealMatrix hstack(RealMatrix arg1, ...)|
\blog|RealMatrix hstack(Real arg1, ...)|
\myindent Stack scalars or matrices horizontally. Accepts an arbitrary number
of
arguments.
For example, \verb|hstack(1, 2, 3)| returns the row vector {\tt [1, 2, 3]}.
\blog|RealMatrix vstack(RealMatrix arg1, ...)|
\blog|RealMatrix vstack(Real arg1, ...)|
\myindent Stack scalars or matrices vertically. Accepts an arbitrary number of
arguments.
For example, \verb|vstack(1, 2, 3)| returns the column vector {\tt [1; 2; 3]}.
\blog|RealMatrix eye(Integer dim)|
\myindent Identity matrix of the given size.
For example, \verb|eye(5)| returns a 5x5 identity matrix.
\blog|RealMatrix zeros(Integer rows, Integer cols)|
\myindent Matrix of the given size, filled with zeros.
For example, \verb|zeros(3, 4)| returns a 3x4 matrix of zeros.
\blog|RealMatrix ones(Integer rows, Integer cols)|
\myindent Matrix of the given size, filled with ones.
For example, \verb|ones(3, 4)| returns a 3x4 matrix of ones.
\blog|RealMatrix abs(RealMatrix m)|
\myindent Element-wise absolute value.
\blog|RealMatrix exp(RealMatrix m)|
\myindent Element-wise exponential of a matrix.
Use square brackets to index into a matrix. If \verb|m| is a two-dimensional
matrix, \verb|m[i]| returns the \verb|i|-th row of \verb|m|. If \verb|m| is a
row or column vector, \verb|m[i]| returns the \verb|i|-th element of \verb|m|.
Note that the resulting value is a \verb|RealMatrix|. To get the i,j-th
element, use \verb|toInt(m[i][j])|.
It is a runtime error to perform matrix operations with dimensions that do not
match.
\subsection{Set operations}
\label{sec:builtin-set-ops}
\blog|Any min(Set s)|
\myindent Minimum of a set.
\blog|Any max(Set s)|
\myindent Maximum of a set.
\blog|Real sum(Set s)|
\myindent Sum of elements in a set of Real values.
\blog|Integer size(Set s)|
\myindent Number of elements in a set.
\blog|Any iota(Set s)|
\myindent Extract element from a singleton set.
For example, \verb|iota({6})| evaluates to \verb|6|.
Note: \verb|Any| is an internal BLOG type not meant to be used by the end user.
It is necessary here because the BLOG type system does not distinguish between,
for example, a \verb|Set| of \verb|Real|s and a \verb|Set| of \verb|Integer|s.
Assigning an \verb|Any| value to a variable of the wrong type is a runtime
error.
\subsection{Conversions between types}
\label{sec:builtin-conversions}
\blog|Integer round(Real val)|
\myindent Round a Real to the nearest Integer.
For example, \verb|round(1.6)| evaluates to \verb|2|.
\blog|Integer toInt(Real val)|
\myindent Convert to Integer, rounding towards zero.
\blog|Integer toInt(Boolean val)|
\myindent Convert false to 0, true to 1.
\blog|Integer toInt(RealMatrix val)|
\myindent Extract the element from a 1x1 RealMatrix, and convert it to an
Integer by rounding towards zero.
\blog|Real toReal(Boolean val)|
\myindent Convert false to 0.0, true to 1.0.
\blog|Real toReal(Integer val)|
\myindent Convert Integer to Real.
\blog|Real toReal(RealMatrix val)|
\myindent Extract the element from a 1x1 RealMatrix, and convert it to a Real.
\subsection{Trigonometric functions}
\label{sec:builtin-trig}
\blog|Real sin(Real radians)|
\myindent Sine of the given value.
\blog|Real cos(Real radians)|
\myindent Cosine of the given value.
\blog|Real tan(Real radians)|
\myindent Tangent of the given value.
\blog|Real atan2(Real y, Real x)|
\myindent Arctangent. Analogous to the \verb|atan2| function in Java.
\subsection{Miscellaneous functions}
\label{sec:builtin-misc}
\blog|Integer abs(Integer x)|
\blog|Real abs(Real x)|
\myindent Absolute value.
\blog|Real exp(Integer x)|
\blog|Real exp(Real x)|
\myindent Exponential.
\blog|Real log(Integer x)|
\blog|Real log(Real x)|
\myindent Natural logarithm.
\blog|Timestep next(Timestep t)|
\myindent The next timestep.
\blog|Timestep prev(Timestep t)|
\myindent The previous timestep.
\blog|Boolean isEmptyString(String s)|
\myindent True iff string is empty.
\blog|RealMatrix loadRealMatrix(String s)|
\myindent Load a \verb|RealMatrix| from a text file. The space-separated
formats produced by numpy and Matlab are supported.