english translation started
This commit is contained in:
@@ -11,10 +11,10 @@ using InteractiveUtils
|
||||
|
||||
# First Contact
|
||||
|
||||
Dieses Kapitel soll beim 'Loslegen' helfen. Es läßt viele Details weg und die Codebeispiele sind oft eher suboptimal.
|
||||
This chapter helps you get started. It omits many details and the code examples are often rather suboptimal.
|
||||
|
||||
|
||||
## Julia als Taschenrechner
|
||||
## Julia as a Calculator
|
||||
|
||||
```{julia}
|
||||
#| eval: true
|
||||
@@ -36,28 +36,28 @@ function Tab(s)
|
||||
return # return nothing, since broadcast println produces empty vector
|
||||
end
|
||||
|
||||
▷ = |>
|
||||
▶ = |>
|
||||
|
||||
# IJulia.set_verbose(true)
|
||||
```
|
||||
|
||||
Berechne $\qquad 12^{1/3} + \frac{3\sqrt{2}}{\sin(0.5)-\cos(\frac{\pi}{4})\log(3)}+ e^5$
|
||||
Compute $\qquad 12^{1/3} + \frac{3\sqrt{2}}{\sin(0.5)-\cos(\frac{\pi}{4})\log(3)}+ e^5$
|
||||
|
||||
```{julia}
|
||||
12^(1/3) + 3sqrt(2) / (sin(.5) - cos(pi/4)*log(3)) + exp(5)
|
||||
```
|
||||
|
||||
Man beachte:
|
||||
Note that:
|
||||
|
||||
- Potenzen schreibt man `a^b`.
|
||||
- Die Konstante `pi` ist vordefiniert.
|
||||
- `log()` ist der natürliche Logarithmus.
|
||||
- Das Multiplikationszeichen `a*b` kann nach einer Zahl weggelassen werden, wenn eine Variable, Funktion oder Klammer folgt.
|
||||
- Powers are written as `a^b`.
|
||||
- The constant `pi` is predefined.
|
||||
- `log()` is the natural logarithm.
|
||||
- The multiplication sign `a*b` can be omitted after a number if followed by a variable, function, or parenthesis.
|
||||
|
||||
|
||||
## Die wichtigsten Tasten: `Tab` und `?` {#sec-tab}
|
||||
## The most important keys: `Tab` and `?` {#sec-tab}
|
||||
|
||||
Man drücke beim Programmieren immer wieder die Tabulatortaste, sobald 2...3 Buchstaben eines Wortes getippt sind. Es werden dann mögliche Ergänzungen angezeigt bzw. ergänzt, wenn die Ergänzung eindeutig ist. Das spart Zeit und bildet ungemein:
|
||||
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:
|
||||
|
||||
```{julia}
|
||||
lo = "lo" #| hide_line
|
||||
@@ -69,7 +69,7 @@ pri = "pri" #| hide_line
|
||||
pri ▷ Tab
|
||||
```
|
||||
|
||||
Die eingebaute Julia-Hilfe `?name` zu allen Funktionen und Konstrukten ist sehr umfassend. Hier ein eher kurzes Beispiel:
|
||||
The built-in Julia help `?name` for all functions and constructs is very comprehensive. Here is a rather short example:
|
||||
|
||||
```{julia}
|
||||
?for
|
||||
@@ -78,7 +78,7 @@ Die eingebaute Julia-Hilfe `?name` zu allen Funktionen und Konstrukten ist sehr
|
||||
:::{.content-hidden unless-format="xxx"}
|
||||
|
||||
::: {.cell }
|
||||
``` {.julia .cell-code}
|
||||
```{.julia .cell-code}
|
||||
?for
|
||||
```
|
||||
|
||||
@@ -103,101 +103,101 @@ julia> for i in [1, 4, 0]
|
||||
:::
|
||||
|
||||
|
||||
## Variablen und Zuweisungen
|
||||
## Variables and Assignments
|
||||
|
||||
Variablen entstehen durch Zuweisung *(assignment)* mit dem Zuweisungsoperator `=` . Danach können sie in weiteren Anweisungen verwendet werden.
|
||||
Variables are created through assignment with the assignment operator `=` . Afterward, they can be used in further statements.
|
||||
|
||||
```{julia}
|
||||
x = 1 + sqrt(5)
|
||||
y = x / 2
|
||||
```
|
||||
|
||||
Im interaktiven Betrieb zeigt Julia das Ergebnis der letzten Operation an.
|
||||
In interactive mode, Julia displays the result of the last operation.
|
||||
|
||||
:::{.callout-note .titlenormal}
|
||||
Zuweisungen sind keine mathematischen Gleichungen. Die Semantik des Zuweisungsoperators (Gleichheitszeichens) ist:
|
||||
Assignments are not mathematical equations. The semantics of the assignment operator (equals sign) is:
|
||||
|
||||
- berechne die rechte Seite und
|
||||
- weise das Ergebnis der linken Seite zu.
|
||||
- Compute the right side and
|
||||
- Assign the result to the left side.
|
||||
|
||||
Ausdrücke wie `x + y = sin(2)` sind daher unzulässig. Links darf nur ein Variablenname stehen.
|
||||
Expressions like `x + y = sin(2)` are therefore invalid. Only a variable name may appear on the left side.
|
||||
:::
|
||||
|
||||
|
||||
## Datentypen
|
||||
## Data types
|
||||
|
||||
Julia ist eine [stark typisierte](https://de.wikipedia.org/wiki/Starke_Typisierung) Sprache. Alle Objekte haben einen Typ.
|
||||
So gibt es unter anderem die Basistypen
|
||||
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
|
||||
|
||||
- Ganze Zahlen *(integers)*,
|
||||
- Gleitkommazahlen *(floating point numbers)*,
|
||||
- Zeichenketten *(strings)* und
|
||||
- Wahrheitswerte *(booleans)*.
|
||||
- Integers,
|
||||
- Floating-point numbers,
|
||||
- Strings and
|
||||
- Booleans.
|
||||
|
||||
Den Typ einer Variablen kann man mit der Funktion `typeof()` ermitteln.
|
||||
The type of a variable can be determined using the `typeof()` function.
|
||||
```{julia}
|
||||
#| warning: true
|
||||
#| error: true
|
||||
for x ∈ (42, 12.0, 3.3e4, "Hallo!", true)
|
||||
println("x = ", x, " ..... Typ: ", typeof(x))
|
||||
for x ∈ (42, 12.0, 3.3e4, "Hello!", true)
|
||||
println("x = ", x, " ..... Type: ", typeof(x))
|
||||
end
|
||||
```
|
||||
Die Standard-Gleitkommazahl hat eine Länge von 64 Bit, entspricht also einer `double` in C/C++/Java.
|
||||
The standard floating-point number has a length of 64 bits, which corresponds to a `double` in C/C++/Java.
|
||||
|
||||
Julia ist eine [dynamisch typisierte](https://de.wikipedia.org/wiki/Dynamische_Typisierung) Sprache.
|
||||
Variablen haben keinen Typ. Sie sind typlose Referenzen (Zeiger) auf Objekte.
|
||||
Wenn man vom „Typ einer Variablen“ spricht, meint man den Typ des Objektes, das der Variablen gerade zugewiesen ist.
|
||||
Julia is a [dynamically typed](https://en.wikipedia.org/wiki/Dynamic_typing) language.
|
||||
Variables have no type. They are typeless references (pointers) to objects.
|
||||
When one speaks of the "type of a variable", one means the type of the object currently assigned to the variable.
|
||||
|
||||
```{julia}
|
||||
x = sqrt(2)
|
||||
|
||||
println( typeof(x), " - Wert von x = $x" )
|
||||
println( typeof(x), " - Value of x = $x" )
|
||||
|
||||
x = "Jetzt bin ich keine Gleitkommazahl mehr!"
|
||||
x = "Now I'm no longer a floating-point number!"
|
||||
|
||||
println( typeof(x), " - Wert von x = $x" )
|
||||
println( typeof(x), " - Value of x = $x" )
|
||||
```
|
||||
|
||||
## Druckanweisungen
|
||||
Die Funktion `println()` unterscheidet sich von `print()` dadurch, dass sie am Ende einen Zeilenvorschub ausgibt.
|
||||
## Print statements
|
||||
The `println()` function differs from `print()` in that it outputs a line break at the end.
|
||||
```{julia}
|
||||
print(y)
|
||||
print("...die Zeile geht weiter...")
|
||||
print("immernoch...")
|
||||
print("...the line continues...")
|
||||
print("still...")
|
||||
println(y)
|
||||
println("Neue Zeile")
|
||||
println("Neue Zeile")
|
||||
println("New line")
|
||||
println("New line")
|
||||
```
|
||||
Beide Funkionen können als Argument eine Liste von *strings* und Variablen bekommen. Man kann Variablen auch in *strings* einbetten, indem man dem Variablennamen ein Dollarzeichen voranstellt *(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 = "Fertig!"
|
||||
println("x= ", x, " ...und y= ", y, "...", zz) # 1. Variante
|
||||
println("x= $x ...und y= $y...$zz") # Variante mit string interpolation
|
||||
zz = "Done!"
|
||||
println("x= ", x, " ...and y= ", y, "...", zz) # 1. variant
|
||||
println("x= $x ...and y= $y...$zz") # variant with string interpolation
|
||||
```
|
||||
|
||||
|
||||
:::{.callout-note .titlenormal collapse=true icon=false }
|
||||
## Wenn man ein Dollarzeichen drucken will...
|
||||
## If you want to print a dollar sign...
|
||||
|
||||
muss man einen *backslash* voranstellen. Wenn man einen *backslash* drucken will, muss man ihn verdoppeln.
|
||||
you must prepend a *backslash*. If you want to print a *backslash*, you must double it.
|
||||
```{julia}
|
||||
println("Ein Dollar: 1\$ und drei backslashs: \\\\\\ ")
|
||||
println("One dollar: 1\$ and three backslashes: \\\\\\ ")
|
||||
```
|
||||
:::
|
||||
|
||||
## Funktionen
|
||||
Funktionsdefinitionen beginnen mit dem Schlüsselwort `function` und enden mit dem Schlüsselwort `end`. In der Regel haben sie eine oder mehrere Argumente und geben beim Aufruf ein berechnetes Objekt mit der `return`-Anweisung zurück.
|
||||
## 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 object with the `return` statement on calling.
|
||||
```{julia}
|
||||
function hypotenuse(a, b) # heute besonders umständlich
|
||||
function hypotenuse(a, b) # particularly cumbersome today
|
||||
c2 = a^2 + b^2
|
||||
c = sqrt(c2)
|
||||
return c
|
||||
end
|
||||
```
|
||||
Nach ihrer Definition kann die Funktion benutzt (aufgerufen) werden. Die in der Definition verwendeten Variablen `a,b,c,c2` sind lokale Variablen und stehen außerhalb der Funktionsdefinition nicht zur Verfügung.
|
||||
After their definition, the function can be used (called). The variables `a,b,c,c2` used in the definition are local variables and are not available outside the function definition.
|
||||
```{julia}
|
||||
#| error: true
|
||||
x = 3
|
||||
@@ -206,72 +206,72 @@ println("z = $z")
|
||||
println("c = $c")
|
||||
```
|
||||
|
||||
Sehr einfache Funktionen können auch als Einzeiler definiert werden.
|
||||
Very simple functions can also be defined as single-line functions.
|
||||
```{julia}
|
||||
hypotenuse(a, b) = sqrt(a^2+b^2)
|
||||
```
|
||||
|
||||
|
||||
## Tests
|
||||
Tests liefern einen Wahrheitswert zurück.
|
||||
Tests return a Boolean value.
|
||||
```{julia}
|
||||
x = 3^2
|
||||
x < 2^3
|
||||
```
|
||||
Neben den üblichen arithmetischen Vergleichen `==, !=, <, <= ,> ,>=`
|
||||
gibt es noch viele andere Tests. Natürlich kann man das Ergebnis eines Tests auch einer Variablen zuweisen, welche dann vom Typ `Bool` ist. Die logischen Operatoren `&&`, `||` und Negation `!` können in Tests verwendet werden.
|
||||
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.
|
||||
|
||||
```{julia}
|
||||
test1 = "Auto" in ["Fahrrad", "Auto", "Bahn"]
|
||||
test1 = "Car" in ["Bicycle", "Car", "Train"]
|
||||
test2 = x == 100 || !(x <= 30 && x > 8)
|
||||
test3 = startswith("Lampenschirm", "Lamp")
|
||||
test3 = startswith("lampshade", "Lamp")
|
||||
println("$test1 $test2 $test3")
|
||||
```
|
||||
|
||||
## Verzweigungen
|
||||
Verzweigungen (bedingte Anweisungen) haben die Form
|
||||
|
||||
## Branches
|
||||
Branches (conditional statements) have the form
|
||||
|
||||
```default
|
||||
if <Test>
|
||||
<Anweisung1>
|
||||
<Anweisung2>
|
||||
if <test>
|
||||
<statement1>
|
||||
<statement2>
|
||||
...
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
Ein `else`-Zweig und `elseif`-Zweige sind möglich.
|
||||
An `else` branch and `elseif` branches are possible.
|
||||
```{julia}
|
||||
x = sqrt(100)
|
||||
|
||||
if x > 20
|
||||
println("Seltsam!")
|
||||
println("Strange!")
|
||||
else
|
||||
println("OK")
|
||||
y = x + 3
|
||||
end
|
||||
```
|
||||
|
||||
Einrückungen verbessern die Lesbarkeit, sind aber fakultativ. Zeilenumbrüche trennen Anweisungen. Das ist auch durch Semikolon möglich. Obiger Codeblock ist für Julia identisch zu folgender Zeile:
|
||||
Indentation improves readability but is optional. Line breaks separate statements. This can also be done with semicolons. The above code block is identical to the following line for Julia:
|
||||
```{julia}
|
||||
# Bitte nicht so programmieren! Sie werden es bereuen!
|
||||
x=sqrt(100); if x > 20 println("Seltsam!") else println("OK"); y = x + 3 end
|
||||
# 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
|
||||
```
|
||||
|
||||
Es wird dringend empfohlen, von Anfang an den eigenen Code übersichtlich mit sauberen Einrückungen zu formatieren!
|
||||
It is strongly recommended to format your own code from the beginning with clear indentation!
|
||||
|
||||
|
||||
## Einfache `for`-Schleifen
|
||||
## Simple `for` loops
|
||||
|
||||
zum wiederholten Abarbeiten von Anweisungen haben die Form
|
||||
for repeated execution of statements have the form
|
||||
```default
|
||||
for <Zähler> = Start:Ende
|
||||
<Anweisung1>
|
||||
<Anweisung2>
|
||||
for <counter> = start:end
|
||||
<statement1>
|
||||
<statement2>
|
||||
...
|
||||
end
|
||||
```
|
||||
Beispiel:
|
||||
Example:
|
||||
|
||||
```{julia}
|
||||
sum = 0
|
||||
@@ -281,9 +281,10 @@ end
|
||||
sum
|
||||
```
|
||||
|
||||
|
||||
## Arrays
|
||||
1-dimensionale Arrays (Vektoren) sind eine einfache Form von Containern. Man kann sie mit echigen Klammern anlegen
|
||||
und auf die Elemente per Index zugreifen. Die Indizierung beginnt mit 1.
|
||||
1-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}
|
||||
v = [12, 33.2, 17, 19, 22]
|
||||
@@ -298,17 +299,18 @@ v[1] = v[4] + 10
|
||||
v
|
||||
```
|
||||
|
||||
Man kann leere Vektoren anlegen und sie verlängern.
|
||||
Empty vectors can be created and extended.
|
||||
```{julia}
|
||||
v = [] # leerer Vektor
|
||||
v = [] # empty vector
|
||||
push!(v, 42)
|
||||
push!(v, 13)
|
||||
v
|
||||
```
|
||||
|
||||
|
||||
:::{.callout-note icon="false" .titlenormal collapse="true" font-variant-ligatures="no-contextual" }
|
||||
|
||||
## Nachtrag: wie die Wirkung der Tab-Taste auf dieser Seite simuliert wurde...
|
||||
## Postscript: how the effect of the Tab key was simulated on this page...
|
||||
|
||||
```{julia}
|
||||
using REPL
|
||||
|
||||
Reference in New Issue
Block a user