From bed3fcc809c4bea414d9dda90bf990f5c1dcc927 Mon Sep 17 00:00:00 2001 From: Michele Nottoli <michele.nottoli@gmail.com> Date: Thu, 5 Oct 2023 11:46:37 +0200 Subject: [PATCH] Added a partial version of notebook 1. --- 1_Basics.ipynb | 630 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 630 insertions(+) create mode 100644 1_Basics.ipynb diff --git a/1_Basics.ipynb b/1_Basics.ipynb new file mode 100644 index 0000000..22fc794 --- /dev/null +++ b/1_Basics.ipynb @@ -0,0 +1,630 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7aad3586-d57e-4000-8688-aa5b53d48416", + "metadata": {}, + "source": [ + "# Basics" + ] + }, + { + "cell_type": "markdown", + "id": "bf904dbe-3197-4a91-bbb8-05e1c7f7dfee", + "metadata": {}, + "source": [ + "## Variables and elementary types\n", + "\n", + "Defining variables in Julia works like in most languages:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c44023b-06c9-4420-9af3-da2ec37b02da", + "metadata": {}, + "outputs": [], + "source": [ + "int = 4 # An integer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b2f9ba27-bf65-40d8-b0ac-e3d86c99dd40", + "metadata": {}, + "outputs": [], + "source": [ + "str = \"Hi\" # A string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87a83355-5088-47d3-b698-2c6902aaa6a0", + "metadata": {}, + "outputs": [], + "source": [ + "float = 1.2 # A floating-point number" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "597523f1-4041-4fc6-9be2-2c94a93fd633", + "metadata": {}, + "outputs": [], + "source": [ + "bool = true # A boolean (also false)" + ] + }, + { + "cell_type": "markdown", + "id": "0c538bdb-f995-4589-bee7-0989c922090c", + "metadata": {}, + "source": [ + "The type is automatically inferred:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b80c6777-3dea-4f45-98b3-0a3eee196183", + "metadata": {}, + "outputs": [], + "source": [ + "typeof(int)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1fb6cbd2-871f-40a4-ae84-e7ee46e75a61", + "metadata": {}, + "outputs": [], + "source": [ + "typeof(str)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a16a6f08-f19a-42e0-baac-aaa88ebba1af", + "metadata": {}, + "outputs": [], + "source": [ + "typeof(float)" + ] + }, + { + "cell_type": "markdown", + "id": "09d035eb-22aa-49f5-9fd1-a5b16973f386", + "metadata": {}, + "source": [ + "Julia supports a large range of integer and floating-point types out of the box, for example:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88f24b01-f4a5-4030-af2c-a5d30223d047", + "metadata": {}, + "outputs": [], + "source": [ + "x = UInt8(1) # 8-bit wide unsigned integer\n", + "y = Int32(-1) # 32-bit wide signed integer\n", + "z = Float32(0.2) # single precision\n", + "α = Float16(-1.0) # half precision\n", + "β = ComplexF64(2. + 8im) # Complex number (composed of two Float64)" + ] + }, + { + "cell_type": "markdown", + "id": "a615bbc0-732e-461f-8c4c-a972fa2be731", + "metadata": {}, + "source": [ + "If you give a variable an illegal name, you get a syntax error:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ca20515-6d66-40c3-84f1-2e24ece096cd", + "metadata": {}, + "outputs": [], + "source": [ + "76trombones = \"big parade\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c61f59e-2c2f-4049-8624-35e0712b2cf1", + "metadata": {}, + "outputs": [], + "source": [ + "more@ = 1000000" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "85040b19-b0a9-4baa-8748-5ea62f8b5633", + "metadata": {}, + "outputs": [], + "source": [ + "struct = \"Advanced Theoretical Zymurgy\"" + ] + }, + { + "cell_type": "markdown", + "id": "cc36688b-c983-43f8-8bdf-9b82aae59909", + "metadata": {}, + "source": [ + "It turns out that struct is one of Julia’s keywords. The REPL uses keywords to recognize the structure of the program, and they cannot be used as variable names." + ] + }, + { + "cell_type": "markdown", + "id": "de111dbe-f7ed-4e1a-8ec0-c5e65bd63bbf", + "metadata": {}, + "source": [ + "### Strings" + ] + }, + { + "cell_type": "markdown", + "id": "1b3531e9-33db-4c51-b394-c1def6621519", + "metadata": {}, + "source": [ + "In general, you can’t perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "246f1db8-d734-413d-9237-b2434e071914", + "metadata": {}, + "outputs": [], + "source": [ + "\"2\" - \"1\"\n", + "\"eggs\" / \"easy\"\n", + "\"third\" + \"a charm\"" + ] + }, + { + "cell_type": "markdown", + "id": "c94f1187-67f8-473e-910c-2eb145ed59dd", + "metadata": {}, + "source": [ + "But there are two exceptions, * and ^." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3a0f6e1-e28b-4fb7-baa3-bb003de3dc24", + "metadata": {}, + "outputs": [], + "source": [ + "\"Hello \" * \"world\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c963c3c-47c7-4805-8be6-621b72fdb62e", + "metadata": {}, + "outputs": [], + "source": [ + "\"hello! \"^5" + ] + }, + { + "cell_type": "markdown", + "id": "61ff8194-669c-4ece-ba31-44e88ed808ac", + "metadata": {}, + "source": [ + "## Exercises" + ] + }, + { + "cell_type": "markdown", + "id": "e0aecc9c-e77f-449e-8aff-e1ffcf5d7833", + "metadata": {}, + "source": [ + "Practice using Julia as a calculator:\n", + " - The volume of a sphere with radius r is 4/3πr^3. What is the volume of a sphere with radius 5?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc538246-418c-4ebe-b487-81feba4383c8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "10f1604d-8e81-4f96-8b6f-95753f0b5c4f", + "metadata": {}, + "source": [ + " - Suppose the cover price of a book is € 24.95, but bookstores get a 40 % discount. Shipping costs € 3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d99fb94-0d78-4812-abbb-b528bc8cbf73", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "4f6858ae-316a-4102-b9d8-9a5e087af1ca", + "metadata": {}, + "source": [ + " - If I leave my house at 6:52 am and run 1 km at an easy pace (6:00 per km), then 3 km at tempo (5:00 per km) and 1 km at easy pace again, what time o I get home for breakfast?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd4150cf-005a-4087-aa92-5f4b18e4d197", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "dc9cddb9-717d-4ba0-b478-beb00afc46c8", + "metadata": {}, + "source": [ + "## Conditionals and Boolean Expressions\n", + "\n", + "A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces true if they are equal and false otherwise:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7de4ffe9-4730-47dd-b074-1525bf9937b1", + "metadata": {}, + "outputs": [], + "source": [ + "5 == 5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c02939dd-6f25-4916-8394-a5f95d0200df", + "metadata": {}, + "outputs": [], + "source": [ + "5 == 6" + ] + }, + { + "cell_type": "markdown", + "id": "9a108e63-7d58-489d-b7ef-49629c879d29", + "metadata": {}, + "source": [ + "The == operator is one of the relational operators; the others are:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b59cd314-fd03-4609-ac1b-7329a2c3cc38", + "metadata": {}, + "outputs": [], + "source": [ + "x = 3; y = 4\n", + "x != y # x is not equal to y\n", + "x ≠ y # (to get the ≠ symbol, type \\ne and then press TAB)\n", + "x > y # x is greater than y\n", + "x < y # x is less than y\n", + "x >= y # x is greater than or equal to y\n", + "x ≥ y # (to get the ≥ symbol, type \\ge and then press TAB)\n", + "x <= y # x is less than or equal to y\n", + "x ≤ y # (to get the ≤ symbol, type \\le and then press TAB)" + ] + }, + { + "cell_type": "markdown", + "id": "15e20f55-1a43-4d0d-94bc-d2de0ee2fe8c", + "metadata": {}, + "source": [ + "There are three logical operators: && (and), || (or), and ! (not). The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 && x < 10 is true only if x is greater than 0 and less than 10. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1e6e2ce-bb0c-4776-80b6-22ceb9c68397", + "metadata": {}, + "outputs": [], + "source": [ + "x = 4\n", + "x > 0 && x < 10" + ] + }, + { + "cell_type": "markdown", + "id": "da591654-6360-4f56-956b-07493d1d2247", + "metadata": {}, + "source": [ + "It is also worth mentioning that in combined logical expression not all conditions are necessarily executed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e92abf7b-5414-4aee-abaf-a4e52b1c06d3", + "metadata": {}, + "outputs": [], + "source": [ + "true || println(\"The RHS of || is only run\")\n", + "false || println(\"if the LHS is false\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8a9d8ac-5378-4ebb-810b-9ae3194d670c", + "metadata": {}, + "outputs": [], + "source": [ + "iseven(3) && println(\"The RHS of || is only run\")\n", + "isodd(3) && println(\"if the LHS is true\")" + ] + }, + { + "cell_type": "markdown", + "id": "be0c63e0-35a9-43c7-9b30-d04072ee6904", + "metadata": {}, + "source": [ + "Finally, the ! operator negates a boolean expression, so !(x > y) is true if x > y is false, that is, if x is less than or equal to y." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e43ab736-03e2-4040-a15e-48305037ba23", + "metadata": {}, + "outputs": [], + "source": [ + "x = 4\n", + "!(x > 0 && x < 10)" + ] + }, + { + "cell_type": "markdown", + "id": "7064ae20-6d63-40fc-85c5-080ace54e7b6", + "metadata": {}, + "source": [ + "Unsurprisingly Julia has the standard conditionals." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f140e2c-a240-496d-904c-c60778397bcf", + "metadata": {}, + "outputs": [], + "source": [ + "x = 3; y = 5\n", + "\n", + "if x < y\n", + " println(\"x is less than y\")\n", + "elseif x > y\n", + " println(\"x is greater than y\")\n", + "else\n", + " println(\"x is equal to y\")\n", + "end" + ] + }, + { + "cell_type": "markdown", + "id": "4c57139e-da0c-48ef-a86f-4de80e1d7ca4", + "metadata": {}, + "source": [ + "There is also an inline conditional." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d0801fd-b0d3-41e0-8b54-19a6b4539231", + "metadata": {}, + "outputs": [], + "source": [ + "x = 3\n", + "x < 5 ? \"smaller than 5\" : \"larger or equal 5\"" + ] + }, + { + "cell_type": "markdown", + "id": "e4510840-0890-4217-8a8c-c24aa5d3405b", + "metadata": {}, + "source": [ + "## Iterations\n", + "\n", + "Julia implements the standard for and while constructs." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9632f210-c713-4829-8187-cf5554adad87", + "metadata": {}, + "outputs": [], + "source": [ + "for i in 1:5\n", + " println(\"Hello from number $i\")\n", + "end" + ] + }, + { + "cell_type": "markdown", + "id": "f00caf15-4992-4c0a-8324-9d2be966346d", + "metadata": {}, + "source": [ + "Where the `1:5` is a `Range`-object, but it could be any iterable. There are a few syntax variations, including:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70a9f620-e4ce-4367-b0f5-de37db3f48b5", + "metadata": {}, + "outputs": [], + "source": [ + "sum = 0\n", + "# To get ∈ write \\in and then press TAB\n", + "for j ∈ 1:0.5:3 # Note the step parameter\n", + " sum += j\n", + "end\n", + "println(sum)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e83b2d88-b0f2-43f1-9187-65fbc75f0d3b", + "metadata": {}, + "outputs": [], + "source": [ + "n = 10\n", + "while n > 0\n", + " print(n, \" \")\n", + " n = n - 1\n", + "end" + ] + }, + { + "cell_type": "markdown", + "id": "f19d5051-ce82-49cc-80aa-2434a6adc9d0", + "metadata": {}, + "source": [ + "The loops can be further controlled with the break and continue keywords." + ] + }, + { + "cell_type": "markdown", + "id": "d0ebce97-3054-4eba-821d-92f27793d752", + "metadata": {}, + "source": [ + "Sometimes you don’t know it’s time to end a loop until you get half way through the body. In that case you can use the break statement to jump out of the loop." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bb642e3-e3b1-488d-939f-6932feb62479", + "metadata": {}, + "outputs": [], + "source": [ + "while true\n", + " print(\"> \")\n", + " line = readline()\n", + " if line == \"done\"\n", + " break\n", + " end\n", + " println(line)\n", + "end\n", + "println(\"Done!\")" + ] + }, + { + "cell_type": "markdown", + "id": "98eee547-21fe-4022-bde5-b02ababa26dd", + "metadata": {}, + "source": [ + "The break statement exits the loop. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the body of the loop for the current iteration. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f1c2595-8836-4308-9769-f4140d402582", + "metadata": {}, + "outputs": [], + "source": [ + "for i in 1:10\n", + " if i % 3 == 0\n", + " continue\n", + " end\n", + " print(i, \" \")\n", + "end" + ] + }, + { + "cell_type": "markdown", + "id": "990cefae-abfe-4649-8d68-3fac1b1e268b", + "metadata": {}, + "source": [ + "### Exercises" + ] + }, + { + "cell_type": "markdown", + "id": "bdb2eef9-bdee-4cf3-8bab-d6c1d46ecc2f", + "metadata": {}, + "source": [ + "The mathematician Srinivasa Ramanujan found an infinite series that can be used to generate a numerical approximation of 1/π" + ] + }, + { + "cell_type": "markdown", + "id": "feeb613b-544b-4a01-8817-9db86b8775e3", + "metadata": {}, + "source": [ + " 1 2 √2 ∞ (4k)! (1103 + 26390 k)\n", + " - = ------ ∑ ------------------------\n", + " π 9801 k=0 (k!)^4 396^(4 k)" + ] + }, + { + "cell_type": "markdown", + "id": "8a8ae5ef-f9ec-480e-9927-8d21ecdf1915", + "metadata": {}, + "source": [ + "Use this formula to compute an estimate of π. It should use a while loop ot compute terms of the summation untile the last term is smaller than 1e-15. Afterwards, you can check the result by comparing it to π (to get π in Julia, write \\pi and then press TAB)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd1a716d-ddaf-4167-a83f-129dc3163ea1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ff56e28-969e-4a2d-b33c-21b4a4ecc4de", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.9.3", + "language": "julia", + "name": "julia-1.9" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- GitLab