264 lines
7.7 KiB
Plaintext
264 lines
7.7 KiB
Plaintext
---
|
|
engine: julia
|
|
julia:
|
|
exeflags: ["--color=yes"]
|
|
---
|
|
|
|
```{julia}
|
|
#| error: false
|
|
#| echo: false
|
|
#| output: false
|
|
using InteractiveUtils
|
|
|
|
#struct M a::Int end; x = M(22); @show x
|
|
#should not print "Main.Notebook.M(22)" but only "M(22)"
|
|
function Base.show(io::IO, x::T) where T
|
|
if parentmodule(T) == @__MODULE__
|
|
# Print "TypeName(fields...)" without module prefix
|
|
print(io, nameof(T), "(")
|
|
fields = fieldnames(T)
|
|
for (i, f) in enumerate(fields)
|
|
print(io, getfield(x, f))
|
|
i < length(fields) && print(io, ", ")
|
|
end
|
|
print(io, ")")
|
|
else
|
|
invoke(Base.show, Tuple{IO, Any}, io, x)
|
|
end
|
|
end
|
|
```
|
|
|
|
# Working with Julia: The REPL, Packages, and Introspection
|
|
|
|
```{julia}
|
|
#| error: false
|
|
#| echo: false
|
|
#| output: false
|
|
using InteractiveUtils
|
|
import QuartoNotebookWorker
|
|
Base.stdout = QuartoNotebookWorker.with_context(stdout)
|
|
myactive_module() = Main.Notebook
|
|
Base.active_module() = myactive_module()
|
|
# https://github.com/JuliaLang/julia/blob/master/base/show.jl#L516-L520
|
|
# https://github.com/JuliaLang/julia/blob/master/base/show.jl#L3073-L3077
|
|
```
|
|
|
|
## Official Documentation
|
|
|
|
The official Julia documentation [https://docs.julialang.org/](https://docs.julialang.org/) contains several overviews, including:
|
|
|
|
- [https://docs.julialang.org/en/v1/base/punctuation/](https://docs.julialang.org/en/v1/base/punctuation/) List of symbols
|
|
- [https://docs.julialang.org/en/v1/manual/unicode-input/](https://docs.julialang.org/en/v1/manual/unicode-input/) List of special Unicode symbols and their input methods via tab completion in Julia
|
|
- [https://docs.julialang.org/en/v1/manual/mathematical-operations/#Rounding-functions](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Rounding-functions) List of mathematical functions
|
|
|
|
|
|
|
|
## Julia REPL (Read - Eval - Print - Loop)
|
|
|
|
After starting Julia in a terminal, you can enter both Julia code and various commands:
|
|
|
|
:::{.narrow}
|
|
| Command | Action |
|
|
| :----------------------------| :------------------------ |
|
|
| `exit()` or `Ctrl-d` | exit Julia |
|
|
| `Ctrl-c` | interrupt |
|
|
| `Ctrl-l` | clear screen |
|
|
| End command with `;` | suppress output |
|
|
| `include("filename.jl")` | read and execute file with Julia code |
|
|
|
|
|
|
|
|
The REPL supports several modes:
|
|
|
|
| Mode | Prompt | Start mode | Exit mode |
|
|
| :- | :- | :- | :- |
|
|
| default| `julia>` | | `Ctrl-d` (exits Julia) |
|
|
| Package manager | `pkg>` | `]` | `backspace` |
|
|
| Help | `help?>` | `?`| `backspace`|
|
|
|Shell | `shell>` | `;` | `backspace`|
|
|
|
|
:::
|
|
|
|
|
|
## Jupyter Notebooks (IJulia)
|
|
|
|
|
|
In a Jupyter notebook, the modes are usable as single-line commands in their own input cells:
|
|
|
|
(i) a package manager command:
|
|
|
|
|
|
```{julia}
|
|
#| eval: false
|
|
] status
|
|
```
|
|
|
|
(ii) a help query:
|
|
|
|
|
|
```{julia}
|
|
#| eval: false
|
|
?sin
|
|
```
|
|
|
|
(iii) a shell command:
|
|
|
|
|
|
```{julia}
|
|
#| eval: false
|
|
;ls
|
|
```
|
|
|
|
## The Package Manager
|
|
|
|
An important part of the _Julia ecosystem_ is the extensive collection of packages that extend Julia's functionality.
|
|
|
|
- Some packages are part of every Julia installation and only need to be activated with `using Packagename`.
|
|
- They form the so-called _standard library_, which includes
|
|
- `LinearAlgebra`, `Statistics`, `SparseArrays`, `Printf`, `Pkg`, and others.
|
|
- Over 10000 packages are officially registered, see [https://julialang.org/packages/](https://julialang.org/packages/).
|
|
- These can be downloaded and installed with just a few keystrokes using the _package manager_ `Pkg`.
|
|
- `Pkg` can be called in two ways:
|
|
- as normal Julia statements that can also be in a `.jl` program file:
|
|
```{.Julia}
|
|
using Pkg
|
|
Pkg.add("PackageXY")
|
|
```
|
|
- in the special pkg-mode of the Julia REPL:
|
|
```{.julia}
|
|
] add PackageXY
|
|
```
|
|
- Afterward, the package can be used with `using PackageXY`.
|
|
- You can also install packages from other sources and self-written packages.
|
|
|
|
|
|
### Some Package Manager Functions
|
|
|
|
| Function | `pkg` - Mode | Explanation |
|
|
|:------------------------|:--------------------------| :-------------------------------------------------------|
|
|
| `Pkg.add("PackageXY")` | `pkg> add PackageXY` | add to current environment |
|
|
| `Pkg.rm("PackageXY")` | `pkg> remove PackageXY` | remove from current environment |
|
|
| `Pkg.update()` | `pkg> update` | update packages in current environment |
|
|
| `Pkg.activate("mydir")` | `pkg> activate mydir` | activate directory as current environment |
|
|
| `Pkg.status()` | `pkg> status` | list packages |
|
|
| `Pkg.instantiate()` | `pkg> instantiate` | install all packages according to `Project.toml` |
|
|
|
|
: {tbl-colwidths="[35,40,25]"}
|
|
|
|
### Installed Packages and Environments
|
|
|
|
- Julia's package manager maintains:
|
|
1. a list of packages explicitly installed with the command `Pkg.add()` or `]add` with exact version specifications in a file `Project.toml` and
|
|
2. a list of all packages installed as implicit dependencies in the file `Manifest.toml`.
|
|
- The directory in which these files are located is the `environment` and is displayed with `Pkg.status()` or `]status`.
|
|
- Without an expplicit project environment, this looks as follows:
|
|
|
|
```
|
|
(@v1.10) pkg> status
|
|
Status `~/.julia/environments/v1.12/Project.toml`
|
|
[6e4b80f9] BenchmarkTools v1.6.3
|
|
[5fb14364] OhMyREPL v0.5.29
|
|
[91a5bcdd] JuliaFormatter v2.3.0
|
|
[295af30f] Revise v3.13.2
|
|
```
|
|
|
|
- You can use separate `environments` for different projects. You can either start Julia with
|
|
```shell
|
|
julia --project=path/to/myproject
|
|
```
|
|
or activate the environment in Julia with `Pkg.activate("path/to/myproject")`. Then `Project.toml` and `Manifest.toml` files are created and managed there. (The installation of package files still takes place somewhere under `$HOME/.julia`)
|
|
|
|
|
|
### Installing Packages on our Jupyter Server `misun103`:
|
|
|
|
- A central repository already contains all packages mentioned in this course.
|
|
- You have no write permissions there.
|
|
- However, you can install additional packages in your `HOME`. As a first command, you need to activate the current directory:
|
|
|
|
|
|
```{julia}
|
|
#| eval: false
|
|
] activate .
|
|
```
|
|
|
|
(Note the dot!)
|
|
|
|
|
|
|
|
|
|
After that, you can install packages with `add` in the pkg-mode:
|
|
|
|
```{julia}
|
|
#| eval: false
|
|
] add PackageXY
|
|
```
|
|
|
|
|
|
Package installation can take a significant amount of time. Many packages have complex dependencies and trigger the installation of further packages. Many packages are precompiled during installation. You can see the installation progress in the REPL, but unfortunately not in the Jupyter notebook.
|
|
|
|
|
|
|
|
|
|
## The Julia JIT _(just in time)_ Compiler: Introspection
|
|
|
|
The Julia compiler is based on the _LLVM Compiler Infrastructure Project_.
|
|
|
|
:::{.narrow}
|
|
Stages of Compilation
|
|
|
|
| stage & result | introspection command |
|
|
| :--- | :--- |
|
|
|Parse $\Longrightarrow$ Abstract Syntax Tree (AST) | `Meta.parse()` |
|
|
| Lowering: transform AST $\Longrightarrow$ Static Single Assignment (SSA) form | `@code_lowered`|
|
|
| Type Inference | `@code_warntype`, `@code_typed` |
|
|
| Generate LLVM intermediate representation | `@code_llvm`|
|
|
| Generate native machine code | `@code_native` |
|
|
|
|
:::
|
|
|
|
|
|
|
|
```{julia}
|
|
function f(x,y)
|
|
z = x^2 + log(y)
|
|
return 2z
|
|
end
|
|
```
|
|
|
|
|
|
```{julia}
|
|
p = Meta.parse( "function f(x,y); z=x^2+log(y); return 2x; end")
|
|
```
|
|
|
|
|
|
```{julia}
|
|
using TreeView
|
|
|
|
walk_tree(p)
|
|
```
|
|
|
|
|
|
```{julia}
|
|
@code_lowered f(2,4)
|
|
```
|
|
|
|
|
|
```{julia}
|
|
@code_warntype f(2,4)
|
|
```
|
|
|
|
|
|
```{julia}
|
|
@code_typed f(2,4)
|
|
```
|
|
|
|
|
|
```{julia}
|
|
@code_llvm f(2,4)
|
|
```
|
|
|
|
|
|
```{julia}
|
|
@code_native f(2,4)
|
|
```
|