494 lines
14 KiB
Plaintext
494 lines
14 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cd3f6abe",
|
|
"metadata": {},
|
|
"source": [
|
|
"# First Contact\n",
|
|
"\n",
|
|
"Dieses Kapitel enthält einige \"pädagogische Halbwahrheiten\", d.h., es vereinfacht und lässt Details weg.\n",
|
|
"\n",
|
|
"## Julia als Taschenrechner\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0dbe789e",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:34.132000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:33.746000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:37.286000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:37.175000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| eval: true\n",
|
|
"#| echo: false\n",
|
|
"#| output: false\n",
|
|
"IJulia.set_verbose(true)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a2b52996",
|
|
"metadata": {},
|
|
"source": [
|
|
"Berechne $\\qquad 12^{1/3} + \\frac{3\\sqrt{2}}{\\sin(0.5)-\\cos(\\frac{\\pi}{4})\\log(3)}+ e^5$\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c831420f",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:37.336000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:37.335000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:37.620000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:37.618000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"12^(1/3) + 3sqrt(2) / (sin(.5) - cos(pi/4)*log(3)) + exp(5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a9f3359b",
|
|
"metadata": {},
|
|
"source": [
|
|
"Man beachte:\n",
|
|
"\n",
|
|
"- Potenzen schreibt man `a^b`.\n",
|
|
"- Die Konstante `pi` ist vordefiniert.\n",
|
|
"- `log()` ist der natürliche Logarithmus.\n",
|
|
"- Das Multiplikationszeichen `a*b` kann nach einer Zahl weggelassen werden, wenn eine Variable, Funktion oder Klammer folgt.\n",
|
|
"\n",
|
|
"\n",
|
|
"## Variablen und Zuweisungen\n",
|
|
"\n",
|
|
"Variablen entstehen durch Zuweisung *(assignment)*. Durch Zuweisung definierte Variablen können danach in weiteren Anweisungen verwendet werden.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5ff20c70",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:37.625000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:37.624000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:37.657000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:37.655000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"x = 1 + sqrt(5) \n",
|
|
"y = x / 2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e7db7bb5",
|
|
"metadata": {},
|
|
"source": [
|
|
":::{.indentb}\n",
|
|
"Zuweisungen sind keine mathematischen Gleichungen. Die Semantik des Zuweisungsoperators (d.h., des Gleichheitszeichens) ist: \n",
|
|
"\n",
|
|
"- berechne die rechte Seite und\n",
|
|
"- weise das Ergebnis der linken Seite zu.\n",
|
|
"\n",
|
|
"Ausdrücke wie `x + y = sin(2)` sind daher unzulässig, links darf nur ein Variablenname stehen.\n",
|
|
":::\n",
|
|
"\n",
|
|
"\n",
|
|
"## Datentypen\n",
|
|
"\n",
|
|
"Julia ist eine [stark typisierte](https://de.wikipedia.org/wiki/Starke_Typisierung) Sprache. Alle Objekte haben einen Typ. \n",
|
|
"So gibt es unter anderem die Basistypen \n",
|
|
"\n",
|
|
"- Ganze Zahlen *(integers)*,\n",
|
|
"- Gleitkommazahlen *(floating point numbers)*,\n",
|
|
"- Zeichenketten *(strings)* und \n",
|
|
"- Wahrheitswerte *(booleans)*.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ae3af394",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:37.662000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:37.661000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:38.219000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:38.217000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| warning: true\n",
|
|
"#| error: true\n",
|
|
"for x ∈ (42, 12.0, 3.3e4, \"Hallo!\", true)\n",
|
|
" println(\"x = \", x, \" ..... Typ: \", typeof(x))\n",
|
|
"end"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "213bef1c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Die Standard-Gleitkommazahl hat eine Länge von 64 Bit, entspricht also einer `double` in C/C++/Java.\n",
|
|
"\n",
|
|
"Julia ist eine [dynamisch typisierte](https://de.wikipedia.org/wiki/Dynamische_Typisierung) Sprache. \n",
|
|
"Variablen haben keinen Typ. Sie sind typlose Referenzen (Zeiger) auf Objekte. \n",
|
|
"Wenn man vom „Typ einer Variablen“ spricht, meint man den Typ des Objektes, das der Variablen gerade zugewiesen ist. \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2d6c0d1b",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:38.225000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:38.223000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:38.316000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:38.315000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"x = sqrt(2)\n",
|
|
"println( typeof(x) )\n",
|
|
"x = \"Jetzt bin ich keine Gleitkommazahl mehr!\"\n",
|
|
"println( typeof(x) )"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f2329446",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Druckanweisungen\n",
|
|
"Die Funktion `println()` unterscheidet sich von `print()` dadurch, dass sie am Ende einen Zeilenvorschub ausgibt.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "d4a7260d",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:38.321000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:38.320000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:38.369000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:38.367000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(y)\n",
|
|
"print(\"...die Zeile geht weiter...\")\n",
|
|
"print(\"immernoch...\")\n",
|
|
"println(y)\n",
|
|
"println(\"Neue Zeile\")\n",
|
|
"println(\"Neue Zeile\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dccc9577",
|
|
"metadata": {},
|
|
"source": [
|
|
"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)*.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7a2116bd",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:38.374000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:38.373000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:38.402000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:38.400000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"x = 23\n",
|
|
"y = 3x + 5\n",
|
|
"zz = \"Fertig!\"\n",
|
|
"println(\"x= \", x, \" ...und y= \", y, \"...\", zz) # 1. Variante\n",
|
|
"println(\"x= $x ...und y= $y...$zz\") # Variante mit string interpolation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ae5eaf6f",
|
|
"metadata": {},
|
|
"source": [
|
|
":::{.indentb}\n",
|
|
"Wenn man ein Dollarzeichen drucken will, muss man einen *backslash* voranstellen. Wenn man einen *backslash* drucken will, muss man ihn verdoppeln.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f7d26a60",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:38.407000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:38.406000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:38.434000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:38.432000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"println(\"Ein Dollar: 1\\$ und drei backslashs: \\\\\\\\\\\\ \")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f40cee12",
|
|
"metadata": {},
|
|
"source": [
|
|
":::\n",
|
|
"\n",
|
|
"## Funktionen\n",
|
|
"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.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b6574086",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:38.439000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:38.438000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:38.633000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:38.631000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"function hypotenuse(a, b)\n",
|
|
" c2 = a^2 + b^2\n",
|
|
" c = sqrt(c2)\n",
|
|
" return c \n",
|
|
"end"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d70272a0",
|
|
"metadata": {},
|
|
"source": [
|
|
"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. \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5df842f6",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:38.638000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:38.636000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:39.346000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:39.344000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| error: true\n",
|
|
"x = 3\n",
|
|
"z = hypotenuse(x, 4)\n",
|
|
"println(z)\n",
|
|
"println(c)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dc012140",
|
|
"metadata": {},
|
|
"source": [
|
|
"Sehr einfache Funktionen können auch kürzer als Einzeiler definiert werden.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6aec78b0",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:39.351000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:39.350000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:39.381000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:39.379000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"hypotenuse(a, b) = sqrt(a^2+b^2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d57bc14f",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Tests\n",
|
|
"Tests liefern einen Wahrheitswert zurück. \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0e42d502",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:39.387000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:39.386000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:39.420000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:39.418000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"x = 2^8\n",
|
|
"x < 3"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "814e4e5c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Neben den üblichen arithmetischen Vergleichen `==, !=, <, <= ,> ,>=` \n",
|
|
"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. \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3899c1c9",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:39.425000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:39.424000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:39.479000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:39.477000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"test1 = \"Auto\" in [\"Fahrrad\", \"Auto\", \"Bahn\"]\n",
|
|
"test2 = x == 100 || x in [1,2,4,7]\n",
|
|
"test3 = startswith(\"Lampenschirm\", \"Lamp\") \n",
|
|
"println(\"$test1 $test2 $test3\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "067897ae",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Verzweigungen\n",
|
|
"Verzweigungen (bedingte Anweisungen) haben die Form `if <Test> <Anweisungen> end`. Ein `else`-Zweig ist möglich.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "34db87a5",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:39.484000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:39.483000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:39.518000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:39.516000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"x = sqrt(100)\n",
|
|
"\n",
|
|
"if x > 20\n",
|
|
" println(\"Seltsam!\")\n",
|
|
"else\n",
|
|
" println(\"OK\")\n",
|
|
" y = x + 3\n",
|
|
"end "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2c0677f6",
|
|
"metadata": {},
|
|
"source": [
|
|
"Einrückungen und Zeilenumbrüche verbessern die Lesbarkeit, sind aber fakultativ. Anweisungen können auch durch Semikolon getrennt werden.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3b169201",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2023-03-08T16:05:39.523000Z",
|
|
"iopub.status.busy": "2023-03-08T16:05:39.521000Z",
|
|
"iopub.status.idle": "2023-03-08T16:05:39.554000Z",
|
|
"shell.execute_reply": "2023-03-08T16:05:39.552000Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if x > 20 println(\"Seltsam!\") else println(\"OK\"); y = x + 3 end "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f225e8eb",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Einfache Schleifen\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Arrays\n",
|
|
"1-dimensionale Arrays (Vektoren) sind eine einfache Form von Containern. Man kann sie mit echigen Klammern anlegen\n",
|
|
"\n",
|
|
"\n",
|
|
"und auf die Elemente per Index zugreifen. Die Indizierung beginnt mit 1. \n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Julia 1.8.5",
|
|
"language": "julia",
|
|
"name": "julia-1.8"
|
|
},
|
|
"language_info": {
|
|
"file_extension": ".jl",
|
|
"mimetype": "application/julia",
|
|
"name": "julia",
|
|
"version": "1.8.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|