You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- define two methods `_polynomial(x, a...)` and `_polynomial(x, a)`
184
-
- recall that these kind of optimization are run just after type inference
209
+
- define two methods `_polynomial!(ac, x, a...)` and `_polynomial!(ac, x, a)` for the case of ≥2 coefficients and the last coefficient
210
+
- use splatting together with range indexing `a[1:end-1]...`
211
+
- the correctness can be checked using the built-in `evalpoly`
212
+
- recall that these kind of optimization are possible just around the type inference stage
185
213
- use container of known length to store the coefficients
186
214
187
215
```@raw html
@@ -200,8 +228,8 @@ a = Tuple(ones(Int, 21)) # everything less than 22 gets inlined
200
228
x = 2
201
229
polynomial(a,x) == evalpoly(x,a) # compare with built-in function
202
230
231
+
# @code_llvm polynomial(a,x) # seen here too, but code_typed is a better option
203
232
@code_lowered polynomial(a,x) # cannot be seen here as optimizations are not applied
204
-
@code_llvm polynomial(a,x) # seen here too, but code_typed is a better option
205
233
nothing #hide
206
234
```
207
235
@@ -216,7 +244,7 @@ nothing #hide
216
244
## AST manipulation: The first steps to metaprogramming
217
245
Julia is so called homoiconic language, as it allows the language to reason about its code. This capability is inspired by years of development in other languages such as Lisp, Clojure or Prolog.
218
246
219
-
There are two easy ways to extract/construct the code structure [^3]
247
+
There are two easy ways to extract/construct the code structure [^5]
220
248
- parsing code stored in string with internal `Meta.parse`
221
249
```@repl lab06_meta
222
250
code_parse = Meta.parse("x = 2") # for single line expressions (additional spaces are ignored)
@@ -317,15 +345,14 @@ Given a function `replace_i`, which replaces variables `i` for `k` in an express
317
345
ex = :(i + i*i + y*i - sin(z))
318
346
@test replace_i(ex) == :(k + k*k + y*k - sin(z))
319
347
```
320
-
write function `sreplace_i(s)` which does the same thing but instead of a parsed expression (AST) it manipulates a string, such as
348
+
write a different function `sreplace_i(s)`, which does the same thing but instead of a parsed expression (AST) it manipulates a string, such as
321
349
```@repl lab06_meta
322
350
s = string(ex)
323
351
```
324
-
Think of some corner cases, that the method may not handle properly.
325
-
326
352
**HINTS**:
327
353
- Use `Meta.parse` in combination with `replace_i`**ONLY** for checking of correctness.
328
-
- You can use the `replace` function.
354
+
- You can use the `replace` function in combination with regular expressions.
355
+
- Think of some corner cases, that the method may not handle properly.
329
356
330
357
```@raw html
331
358
</div></div>
@@ -394,9 +421,8 @@ eval(ex)
394
421
395
422
This kind of manipulation is at the core of some pkgs, such as aforementioned [`IntervalArithmetics.jl`](https://github.com/JuliaIntervals/IntervalArithmetic.jl) where every number is replaced with a narrow interval in order to find some bounds on the result of a computation.
396
423
397
-
398
-
[^3]: Once you understand the recursive structure of expressions, the AST can be constructed manually like any other type.
399
424
---
425
+
[^5]: Once you understand the recursive structure of expressions, the AST can be constructed manually like any other type.
400
426
401
427
## Resources
402
428
- Julia's manual on [metaprogramming](https://docs.julialang.org/en/v1/manual/metaprogramming/)
0 commit comments