diff --git a/.gitignore b/.gitignore index 002e0d5..c67cd87 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ xelatex.py nb/*.qmd nb/*.ipynb .cache/ + +**/*.quarto_ipynb diff --git a/_quarto.yml b/_quarto.yml index b643167..d87205c 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -4,15 +4,15 @@ project: - fonts/ - images/ -lang: de -language: - title-block-published: "Updated" - title-block-author-single: "Autor" - toc-title-document: "Auf dieser Seite" +lang: en +#language: +# title-block-published: "Updated" +# title-block-author-single: "Autor" +# toc-title-document: "Auf dieser Seite" book: - title: "Julia für Numerik" - subtitle: "Einführung in die wissenschaftliche Programmierung" + title: "Julia for Numerical Analysis" + subtitle: "Introduction to Scientific Computing" author: "Meik Hellmund" site-url: "https://www.math.uni-leipzig.de/~hellmund/juliabook" favicon: "images/favicon-32x32.png" @@ -23,7 +23,7 @@ book: - icon: github href: https://gitea.math.uni-leipzig.de/hellmund/JuliaKurs23 - icon: download - href: "Julia-f%C3%BCr-Numerik.pdf" + href: "Julia-for-Numerical-Analysis.pdf" search: true @@ -56,7 +56,7 @@ book: - chapters/10_Strings.qmd - chapters/13_IO.qmd - chapters/14_Plot.qmd - # - chapters/makie.qmd + format: @@ -82,12 +82,32 @@ format: cap-location: bottom html-math-method: mathjax - julia-color-pdf: - pdf-engine: xelatex +# julia-color-pdf: +# pdf-engine: xelatex +# papersize: a4 +# documentclass: scrreprt +# classoption: [notitlepage,DIV=16,BCOR=5mm,10pt] +# toc: false +# number-depth: 2 +# fig-cap-location: bottom +# fig-pos: H +# tbl-cap-location: bottom +# hyperrefoptions: +# - linktoc=all +# colorlinks: true +# linkcolor: "blue" +# keep-tex: true +# keep-ipynb: true +# keep-md: true +# code-block-bg: "#e0e0e0" +# code-block-border-left: "#e0e0e0" +# code-overflow: wrap +# include-in-header: +# - file: macros.tex + + julia-color-typst: papersize: a4 - documentclass: scrreprt - classoption: [notitlepage,DIV=16,BCOR=5mm,10pt] - toc: false + toc: true number-depth: 2 fig-cap-location: bottom fig-pos: H @@ -95,16 +115,12 @@ format: hyperrefoptions: - linktoc=all colorlinks: true - linkcolor: "blue" - keep-tex: true - keep-ipynb: true - keep-md: true - code-block-bg: "#e0e0e0" - code-block-border-left: "#e0e0e0" - code-overflow: wrap + linkcolor: #0000FF + keep-typ: true include-in-header: - - file: macros.tex - + - file: macros.typ + + latex-auto-install: false execute: @@ -122,8 +138,6 @@ keep-ipynb: true keep-tex: true keep-md: true -jupyter: julia-1.10 - filters: - code-visibility - latex-environment diff --git a/chapters/11_LinAlg.qmd b/chapters/11_LinAlg.qmd index 55b6c8d..d7a9943 100644 --- a/chapters/11_LinAlg.qmd +++ b/chapters/11_LinAlg.qmd @@ -93,14 +93,14 @@ A + 4I To be able to study questions such as conditioning or convergence of an algorithm, we need a metric. For linear spaces, it is appropriate to define the metric via a norm: $$ -d(x,y) := ||x-y|| +d(x,y) := \lVert x-y\rVert $$ ### $p$-Norms A simple class of norms in $ℝ^n$ are the $p$-norms $$ -||\mathbf{x}||_p = \left(\sum |x_i|^p\right)^\frac{1}{p}, +\lVert \mathbf{x} \rVert_p = \left(\sum |x_i|^p\right)^\frac{1}{p}, $$ which generalize the Euclidean norm $p=2$. @@ -110,13 +110,13 @@ which generalize the Euclidean norm $p=2$. ## The Max-Norm $p=\infty$ Let $x_{\text{max}}$ be the _largest in absolute value_ component of $\mathbf{x}\in ℝ^n$. Then always -$$ |x_{\text{max}}| \le ||\mathbf{x}||_p \le n^\frac{1}{p} |x_{\text{max}}| +$$ \lvert x_{\text{max}}\rvert \le \lVert\mathbf{x}\rVert_p \le n^\frac{1}{p} \lvert x_{\text{max}}\rvert $$ (Consider a vector whose components are all equal to $x_{\text{max}}$ respectively a vector whose components are all equal to zero except $x_{\text{max}}$.) Thus follows $$ -\lim_{p \rightarrow \infty} ||\mathbf{x}||_p = |x_{\text{max}}| =: ||\mathbf{x}||_\infty. +\lim_{p \rightarrow \infty} \lVert\mathbf{x}\rVert_p = \lvert x_{\text{max}}\rvert =: \lVert\mathbf{x}\rVert_\infty. $$ ::: @@ -132,7 +132,7 @@ w = [-1, 2, 33.2] - If the 2nd argument `p` is missing, `p=2` is set. - The 2nd argument can also be `Inf` (i.e., $+\infty$). -- The 1st argument can be any container full of numbers. The sum $\sum |x_i|^p$ extends over *all* elements of the container. +- The 1st argument can be any container full of numbers. The sum $\sum \lvert x_i\rvert^p$ extends over *all* elements of the container. - Thus, for a matrix `norm(A)` is equal to the _Frobenius norm_ of the matrix `A`. ```{julia} @@ -145,7 +145,7 @@ norm(A) # Frobenius norm Since norms are homogeneous under multiplication with scalars, -$||\lambda \mathbf{x}|| = |\lambda|\cdot||\mathbf{x}||$, they are completely determined by the specification of the unit ball. Subadditivity of the norm (triangle inequality) is equivalent to the convexity of the unit ball +$\lVert\lambda \mathbf{x}\rVert = |\lambda|\cdot\lVert\mathbf{x}\rVert$, they are completely determined by the specification of the unit ball. Subadditivity of the norm (triangle inequality) is equivalent to the convexity of the unit ball (code visible by clicking). ```{julia} #| code-fold: true @@ -163,7 +163,7 @@ for p ∈ (0.8, 1, 1.5, 2, 3.001, 1000) end fig1 ``` -As one can see, $p\ge 1$ must hold so that the unit ball is convex and $||.||_p$ is a norm. +As one can see, $p\ge 1$ must hold so that the unit ball is convex and $\lVert.\rVert_p$ is a norm. However, the Julia function `norm(v, p)` returns a result for arbitrary parameters `p`. @@ -181,7 +181,7 @@ Due to the homogeneity of the norm under multiplication with scalars, it is suff Let $V$ be a vector space with dimension $0 2. Install **Visual Studio Code** as editor/IDE: -3. Install the **Julia language extension** in VS Code: +3. Install the **Julia language extension** in VS Code: Entry point: @@ -32,7 +32,7 @@ $ julia (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | - | | |_| | | | (_| | | Version 1.11.4 (2025-03-10) + | | |_| | | | (_| | | Version 1.12.5 (2026-02-09) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | @@ -88,7 +88,7 @@ This can be used to: In text cells, the markup language [Markdown](https://en.wikipedia.org/wiki/Markdown) can be used for formatting and LaTeX for mathematical equations. ::: {.callout-tip } -Please check the `User Interface Tour` and `Keyboard Shortcuts` points in the `Help` menu! +Please check the `User Interface Tour` and `Keyboard Shortcuts` topics in the `Help` menu! ::: The cell currently being worked on can be in `command mode` or `edit mode`. diff --git a/chapters/first_contact.qmd b/chapters/first_contact.qmd index 4351795..b13ff6e 100644 --- a/chapters/first_contact.qmd +++ b/chapters/first_contact.qmd @@ -1,5 +1,7 @@ --- engine: julia +julia: + exeflags: ["--color=yes"] --- ```{julia} @@ -36,7 +38,7 @@ function Tab(s) return # return nothing, since broadcast println produces empty vector end -▶ = |> +▷ = |> # IJulia.set_verbose(true) ``` @@ -57,7 +59,7 @@ Note that: ## The most important keys: `Tab` and `?` {#sec-tab} -When programming, repeatedly press the Tab key as soon as 2–3 letters of a word have been typed. Potential completions are then displayed or completed if the completion is unique. This saves time and is extremely helpful. +When programming, press the Tab key as soon as 2–3 letters of a word have been typed. Potential completions are then displayed or completed if the completion is unique. This saves time and you learn a lot about Julia. ```{julia} lo = "lo" #| hide_line @@ -71,11 +73,14 @@ pri ▷ Tab The built-in Julia help `?name` for all functions and constructs is very comprehensive. Here is a rather brief example: +:::{.content-visible unless-format="typst"} + ```{julia} ?for ``` +::: -:::{.content-hidden unless-format="xxx"} +:::{.content-hidden unless-format="typst"} ::: {.cell } ```{.julia .cell-code} @@ -84,18 +89,23 @@ The built-in Julia help `?name` for all functions and constructs is very compreh ::: {.cell-output .cell-output-stdout} ``` -search: for foreach foldr floor mapfoldr factorial EOFError OverflowError +search: for nor xor foldr floor Core sort -for loops repeatedly evaluate a block of statements while iterating over a sequence of values. + for -Examples + for loops repeatedly evaluate a block of statements while iterating over a sequence of values. -julia> for i in [1, 4, 0] - println(i) - end -1 -4 -0 + The iteration variable is always a new variable, even if a variable of the same name exists in the enclosing scope. Use outer to reuse an existing local variable for iteration. + + Examples + ======== + + julia> for i in [1, 4, 0] + println(i) + end + 1 + 4 + 0 ``` ::: @@ -105,7 +115,7 @@ julia> for i in [1, 4, 0] ## Variables and Assignments -Variables are created through assignment with the assignment operator `=`. Afterwards, they can be used in further statements. +Variables are created through assignment with the assignment operator `=`. ```{julia} x = 1 + sqrt(5) @@ -124,10 +134,11 @@ Expressions like `x + y = sin(2)` are therefore invalid. Only a variable name ma ::: -## Data types +## Data types + +Julia is a [strongly typed](https://en.wikipedia.org/wiki/Strong_and_weak_typing) language +where every object possesses a type. Among the fundamental types are: -Julia is a [strongly typed](https://en.wikipedia.org/wiki/Strong_and_weak_typing) language. All objects have a type. -Among other things, there are the basic types - Integers, - Floating-point numbers, @@ -139,7 +150,7 @@ The type of a variable can be determined using the `typeof()` function. #| warning: true #| error: true for x ∈ (42, 12.0, 3.3e4, "Hello!", true) - println("x = ", x, " ..... Type: ", typeof(x)) + println("x = ", x, " ... Type: ", typeof(x)) end ``` The standard floating-point number has a size of 64 bits, which corresponds to a `double` in C/C++/Java. @@ -159,7 +170,7 @@ println( typeof(x), " - Value of x = $x" ) ``` ## Print statements -The `println()` function differs from `print()` in that it outputs a line break at the end. +The `println()` function puts a line break at the end, `print()` doesn't. ```{julia} print(y) print("...the line continues...") @@ -168,14 +179,15 @@ println(y) println("New line") println("New line") ``` -Both functions can accept a list of *strings* and variables as arguments. Variables can also be embedded in *strings* by prefixing the variable name with a dollar sign *(string interpolation)*. + +Both functions can accept a list of strings and variables as arguments. Variables can also be embedded in strings by prefixing the variable name with a dollar sign *(string interpolation)*. ```{julia} x = 23 y = 3x + 5 zz = "Done!" -println("x= ", x, " ...and y= ", y, "...", zz) # 1. variant -println("x= $x ...and y= $y...$zz") # variant with string interpolation +println("x= ", x, " ... and y= ", y, "...", zz) # 1. variant +println("x= $x ... and y= $y...$zz") # variant with string interpolation ``` @@ -189,7 +201,7 @@ println("One dollar: 1\$ and three backslashes: \\\\\\ ") ::: ## Functions -Function definitions begin with the keyword `function` and end with the keyword `end`. Typically, they have one or more arguments and return a computed result via the `return` statement. +Function definitions start with the keyword `function` and end with the keyword `end`. Typically, they have one or more arguments and return a computed result via a `return` statement. ```{julia} function hypotenuse(a, b) # particularly cumbersome today c2 = a^2 + b^2 @@ -206,7 +218,7 @@ println("z = $z") println("c = $c") ``` -Very simple functions can also be defined as single-line functions. +Very simple functions can also be defined as one-liners: ```{julia} hypotenuse(a, b) = sqrt(a^2+b^2) ``` @@ -219,7 +231,7 @@ x = 3^2 x < 2^3 ``` In addition to the usual arithmetic comparisons `==, !=, <, <= ,> ,>=` -there are many other tests. Of course, the result of a test can also be assigned to a variable, which is then of type `Bool`. The logical operators `&&`, `||` and negation `!` can be used in tests. +there are many other tests. The result of a test can also be assigned to a variable, which is then of type `Bool`. The logical operators `&&`, `||` and negation `!` can be used in tests. ```{julia} test1 = "Car" in ["Bicycle", "Car", "Train"] @@ -229,9 +241,9 @@ println("$test1 $test2 $test3") ``` -## Branches -Branches (conditional statements) have the form +## Conditional Evaluation +A simple `if` has the form ```default if @@ -240,7 +252,7 @@ if end ``` -An `else` branch and `elseif` branches are possible. +There can one or several `elseif` blocks and a final `else` block. ```{julia} x = sqrt(100) @@ -252,13 +264,12 @@ else end ``` -Indentation improves readability but is optional. Line breaks separate statements, and this can also be done with semicolons. The above code block is identical to the following line in Julia: +Indentation enhances readability but is optional. Line breaks separate statements, as do semicolons. The above code block is equivalent to the following line in Julia: ```{julia} # Please don't program like this! You will regret it! x=sqrt(100); if x > 20 println("Strange!") else println("OK"); y = x + 3 end ``` -It is strongly recommended to format your own code from the beginning with clear indentation! ## Simple `for` loops @@ -283,7 +294,7 @@ sum ## Arrays -1-dimensional arrays (vectors) are a simple form of containers. They can be created with square brackets +One-dimensional arrays (vectors) are a simple form of containers. They can be created with square brackets and accessed by index. Indexing starts at 1. ```{julia} diff --git a/chapters/numerictypes.qmd b/chapters/numerictypes.qmd index cac0498..e8baab4 100644 --- a/chapters/numerictypes.qmd +++ b/chapters/numerictypes.qmd @@ -1,5 +1,7 @@ --- engine: julia +julia: + exeflags: ["--color=yes"] --- ```{julia} diff --git a/chapters/pcomplex.qmd b/chapters/pcomplex.qmd index 541879e..c934646 100644 --- a/chapters/pcomplex.qmd +++ b/chapters/pcomplex.qmd @@ -2,7 +2,7 @@ engine: julia --- -# A Case Study: The Parametric Data Type PComplex +# Example: The Parametric Data Type `PComplex` ```{julia} #| error: false @@ -109,9 +109,19 @@ z2 = PComplex(2.0, 0.3) Julia uses `//` as an infix constructor for the type `Rational`. We want something equally nice. In electronics/electrical engineering, [AC quantities are described by complex numbers.](https://en.wikipedia.org/wiki/Phasor_analysis) A representation of complex numbers by "magnitude" and "phase" is common and is often represented in so-called [phasor form](https://en.wikipedia.org/wiki/Phasor): + +:::{.content-visible unless-format="typst"} $$ z= r\enclose{phasorangle}{\phi} = 3.4\;\enclose{phasorangle}{45^\circ} $$ +::: + +:::{.content-hidden unless-format="typst"} +```{=typst} +$ z = r angle.acute phi = 3.4 angle.acute 45 degree $ +``` +::: + where the angle is usually noted in degrees. :::{.callout-note .titlenormal collapse="true"} diff --git a/chapters/syntax.qmd b/chapters/syntax.qmd index 78a8f82..5a90611 100644 --- a/chapters/syntax.qmd +++ b/chapters/syntax.qmd @@ -1,5 +1,7 @@ --- engine: julia +julia: + exeflags: ["--color=yes"] --- ```{julia} @@ -513,7 +515,7 @@ end ------- The C semantics of `for(i=1; i<5; i++)` corresponds to the `while` loop: -``` +```{.julia} i = 1 while i<5 *loop body* # here one can also mess with i effectively diff --git a/index.qmd b/index.qmd index d23cebb..b5cac77 100644 --- a/index.qmd +++ b/index.qmd @@ -1,13 +1,20 @@ --- engine: julia +julia: + exeflags: ["--color=yes"] --- # What is Julia? {.unnumbered} +```{=typst} +#set math.equation(numbering: none) +// some things need to be here, to counter the typst style +``` + Julia is a relatively young, modern programming language designed for *scientific computing*. -A small code example: +A code example: ```{julia} #| error: false @@ -40,12 +47,16 @@ f Intelligence Laboratory* of MIT - 2012 first release v0.1 - 2018 Version v1.0 -- current: v1.11.4 from March 10, 2025 +- February 2026: Version v1.12.5 -For the first release in 2012, the creators of Julia summarized their goals and motivation in a blog post [Why we created Julia](https://julialang.org/blog/2012/02/why-we-created-julia) interestingly. + + +In their 2012 inaugural blog post [Why we created Julia](https://julialang.org/blog/2012/02/why-we-created-julia), +the developers provide an insightful and funny overview of their objectives and motivations for creating Julia. For a picture of *Stefan Karpinski, Viral Shah, Jeff -Bezanson*, and *Alan Edelman*, please click here: . +Bezanson*, and *Alan Edelman*, please +click here: . @@ -95,39 +106,45 @@ Julia has a built-in package manager." ::: -*open source* -: - open development on [GitHub](https://github.com/JuliaLang/julia) +***open source*** + +- open development on [GitHub](https://github.com/JuliaLang/julia) - implementations for all common operating systems -*high-performance programming language for technical computing* -: - many functions for *scientific computing* built-in, +***high-performance programming language for technical computing*** + +- many functions for *scientific computing* built-in, - (intentional) similarity to Python, R and Matlab, - complex calculations in a few lines - simple interface to other languages like C or Python -*a JIT compiler* -: - interactive work possible: `read-eval-print loop (REPL)` with +***a JIT compiler*** + +- interactive work possible: `read-eval-print loop (REPL)` with - just-in-time (JIT) compilation - thereby runtimes comparable to static languages like C/C++, Fortran or Rust -*a built-in package manager* -: - huge *ecosystem* of easily installable packages, e.g. - - [Mathematical Optimization](https://jump.dev/) - - [Machine Learning](https://fluxml.ai/) - - [Data Visualization](https://docs.makie.org/stable/) - - [Differential Equations](https://docs.sciml.ai/DiffEqDocs/stable/) - - [Mathematical Modeling](https://sciml.ai/) +***a built-in package manager*** + +- huge *ecosystem* of easily installable packages, e.g. + + - [Mathematical Optimization](https://jump.dev/) + - [Machine Learning](https://fluxml.ai/) + - [Data Visualization](https://docs.makie.org/stable/) + - [Differential Equations](https://docs.sciml.ai/DiffEqDocs/stable/) + - [Mathematical Modeling](https://sciml.ai/) -## A Small Selection of Online Materials on Julia {.unnumbered} +## Selected Links {.unnumbered} -- [Documentation](https://docs.julialang.org/en/v1/) - the official documentation -- [Cheat Sheet](https://cheatsheet.juliadocs.org/) - "a quick & dirty overview" -- [Introducing Julia](https://en.wikibooks.org/wiki/Introducing_Julia)-- a WikiBook -- [The Julia Express](http://bogumilkaminski.pl/files/julia_express.pdf) - short version, Julia in 16 pages -- [Think Julia](https://benlauwens.github.io/ThinkJulia.jl/latest/book.html) - introduction to programming with Julia as a language +- [Documentation](https://docs.julialang.org/en/v1/) -- the official documentation +- [Cheat Sheet](https://cheatsheet.juliadocs.org/) -- "a quick & dirty overview" +- [Introducing Julia](https://en.wikibooks.org/wiki/Introducing_Julia) -- a WikiBook +- [The Julia Express](http://bogumilkaminski.pl/files/julia_express.pdf) -- Julia in 16 pages +- [Think Julia](https://benlauwens.github.io/ThinkJulia.jl/latest/book.html) -- introduction to programming using Julia as first language - The [Julia Forum](https://discourse.julialang.org/) - For the eyes: [Examples for the Julia graphics package `Makie`](https://beautiful.makie.org/) +