{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "faecad82",
   "metadata": {},
   "source": [
    "<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/VU_logo.png/800px-VU_logo.png?20161029201021\" width=\"350\">\n",
    "\n",
    "### Programming large-scale parallel systems\n",
    "\n",
    "\n",
    "# Julia basics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f54a3f8",
   "metadata": {},
   "source": [
    "## Contents\n",
    "\n",
    "In this notebook, we will cover the basic parts of Julia needed later to learn parallel computing. In particular, we will learn about:\n",
    "\n",
    "- Variables\n",
    "- Functions\n",
    "- Arrays\n",
    "\n",
    "For a more general introduction to Julia see the nice tutorials made available by JuliaAcademy [here](https://github.com/JuliaAcademy/JuliaTutorials/) or the official Julia educational resources [here](https://julialang.org/learning/).\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a23a48f4",
   "metadata": {},
   "source": [
    "## Using Jupyter notebooks in Julia\n",
    "\n",
    "We are going to use Jupyter notebooks in this and other lectures. You provably have worked with notebooks (in Python). If not, here are the basic concepts you need to know to follow the lessons.\n",
    "\n",
    "<div class=\"alert alert-block alert-info\">\n",
    "<b>Tip:</b> Did you know that Jupyter stands for Julia, Python and R?\n",
    "</div>\n",
    "\n",
    "### How to start a Jupyter notebook in Julia\n",
    "\n",
    "To run a Julia Jupyter notebook, open a Julia REPL and type\n",
    "\n",
    "```julia\n",
    "julia> ]\n",
    "pkg> add IJulia\n",
    "julia> using IJulia\n",
    "julia> notebook()\n",
    "```\n",
    "A new browser window will open. Navigate to the corresponding notebook and open it.\n",
    "\n",
    "<div class=\"alert alert-block alert-warning\">\n",
    "<b>Warning:</b> Make sure that the notebook is using the same Julia version as the one you used to launch `IJulia`. If it is not the same, go to Kernel > Change Kernel and choose the right version (see figure below).\n",
    "</div>\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "attachments": {
    "figkernel.png": {
     "image/png": "[encoded data, 74964 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "928ae06c",
   "metadata": {},
   "source": [
    "<div>\n",
    "<img src=\"attachment:figkernel.png\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcefe994",
   "metadata": {},
   "source": [
    "### Running a cell\n",
    "To run a cell, click on a cell and press `Shift` + `Enter`. You can also use the \"Run\" button in the toolbar above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4eae9070",
   "metadata": {},
   "outputs": [],
   "source": [
    "1+3\n",
    "4*5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a02fb75",
   "metadata": {},
   "source": [
    "As you can see from the output of previous cell, the value of the last line is displayed. We can suppress the output with a semicolon. Try it. Execute next cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3923b7a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "1+3\n",
    "4*5;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffd3a444",
   "metadata": {},
   "source": [
    "### Cell order is important\n",
    "\n",
    "Running the two cells below in reverse order won't work (try it). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7597471",
   "metadata": {},
   "outputs": [],
   "source": [
    "foo() = \"Well done!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a5d4f50",
   "metadata": {},
   "outputs": [],
   "source": [
    "foo()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d18e679d",
   "metadata": {},
   "source": [
    "### A very easy first exercise\n",
    "\n",
    "Run the following cell. It contains definitions used later in the notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81678b3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "function why_q1()\n",
    "    msg = \"\"\"\n",
    "    In the first line, we assign a variable to a value. In the second line, we assign another variable to the same value. Thus, we have 2 variables associated with the same value. In line 3, we associate y to a new value (re-assignment). Thus, we have 2 variables associated with 2 different values. Variable x is still associated with its original value. Thus, the value at the final line is x=1.\n",
    "    \"\"\"\n",
    "    println(msg)\n",
    "end\n",
    "function why_q2()\n",
    "    msg = \"\"\"\n",
    "    It will be 1 for very similar reasons as in the previous questions: we are reassigning a local variable, not the global variable defined outside the function.\n",
    "    \"\"\"\n",
    "    println(msg)\n",
    "end\n",
    "function why_q3()\n",
    "    msg = \"\"\"\n",
    "    It will be 6. In the returned function f2, x is equal to 2. Thus, when calling f2(3) we compute 2*3.\n",
    "    \"\"\"\n",
    "    println(msg)\n",
    "end\n",
    "println(\"🥳 Well done! \")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92112bd1",
   "metadata": {},
   "source": [
    "### REPL modes\n",
    "\n",
    "This is particular to Julia notebooks. You can use package, help, and shell mode just like in the Julia REPL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a445020",
   "metadata": {},
   "outputs": [],
   "source": [
    "] add MPI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edefc54f",
   "metadata": {},
   "outputs": [],
   "source": [
    "? print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e69fd5f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "; ls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b94fc33",
   "metadata": {},
   "source": [
    "## Variables\n",
    "\n",
    "The usage of variables in Julia is pretty similar to Python and quite different from C/C++ and Fortran. However, there are also some differences with Python. \n",
    "\n",
    "### Creating a variable\n",
    "\n",
    "A variable is a name associated (bound) to a value. We associate variables with values with `=` as usual."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "149b7a62",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b557a8",
   "metadata": {},
   "source": [
    "When assigning a variable, the value on the right hand side is not copied into the variable. It is just an association of a name with a value (much like in Python)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba08ea11",
   "metadata": {},
   "source": [
    "### Re-assign a variable\n",
    "\n",
    "We can re-assign a variable, even with a value of another type. However, avoid changing the variable type for performance reasons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f62e49c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86ac71ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e67d8480",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"Hi!\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "767563a7",
   "metadata": {},
   "source": [
    "### Unreachable objects\n",
    "\n",
    "When an object is not associated with a variable any more,  it cannot be reached by the user. This can happen, e.g., when we re-assign a variable. Another case is when local variables, e.g in a function, go out of scope. The following line allocates a large array and assigns it to variable a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "202e222b",
   "metadata": {},
   "outputs": [],
   "source": [
    " a = zeros(300000000);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35742a64",
   "metadata": {},
   "source": [
    "If we re-assign the variable to another value, the large array will be inaccessible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6cd1c24",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = nothing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2883bf04",
   "metadata": {},
   "source": [
    "### Garbage collector\n",
    "\n",
    "Luckily, Julia has a garbage collector that deallocates unreachable objects. You don't need to bother about manual deallocation! Julia is not constantly looking for unreachable objects. Thus, garbage collection does not happen instantaneously, but it will happen at some point. You can also explicitly call the garbage collector, but it is almost never done in practice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89516ede",
   "metadata": {},
   "outputs": [],
   "source": [
    "GC.gc()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c110aa22",
   "metadata": {},
   "source": [
    "### Type declarations are optional\n",
    "\n",
    "Julia knows the type of the object associated with a variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8d93d64",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "typeof(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df2e48c2",
   "metadata": {},
   "source": [
    "We can annotate types if we want, but this will not improve performance (except in very special situations). Thus, annotating types is not done in practice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95fd3ef4",
   "metadata": {},
   "outputs": [],
   "source": [
    "c::Int = 1\n",
    "typeof(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67883016",
   "metadata": {},
   "source": [
    "If you annotate a variable with a type, then it cannot refer to objects of other types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3cdc27b",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = \"I am a string\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64f5aa94",
   "metadata": {},
   "source": [
    "### Variable names\n",
    "\n",
    "There is a great flexibility to choose variable names in Julia. See all the rules in the [manual](https://docs.julialang.org/en/v1/manual/variables/#man-allowed-variable-names) if you are interested."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09eb4365",
   "metadata": {},
   "source": [
    "We can use Unicode (UTF-8 encoding) characters in variables and function names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94667496",
   "metadata": {},
   "outputs": [],
   "source": [
    "🐱 = \"I am a cat\"\n",
    "🐶 = \"I am a dog\"\n",
    "🐱 == 🐶"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beaf9680",
   "metadata": {},
   "source": [
    "We can also use Greek letters and other mathematical symbols. Just write the corresponding [LaTeX](https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols) command and press `Tab`. For example: `ω` is written `\\omega` + `Tab`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9b3079e",
   "metadata": {},
   "outputs": [],
   "source": [
    "ω = 1.234\n",
    "sin(ω)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f7de553",
   "metadata": {},
   "source": [
    "In fact, some useful mathematical constants are predefined in Julia with math Greek letters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89d6d94e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sin(π/2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d36ba0ae",
   "metadata": {},
   "source": [
    "\n",
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>Question (NB1-Q1):</b>  What will be the value of `x` in the last line ? (Think your answer before executing next cell to find out the result)    \n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6f4e13e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 1\n",
    "y = x\n",
    "y = 2\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2f94960",
   "metadata": {},
   "source": [
    "Run next cell to get an explanation of this question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc562337",
   "metadata": {},
   "outputs": [],
   "source": [
    "why_q1()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d2cb752",
   "metadata": {},
   "source": [
    "## Functions\n",
    "\n",
    "Julia is very much a functional programming language. In consequence, Julia is more centered on functions than on types. This is in contrast to object-oriented languages, which are more centered on types (classes). For instance, you don't need to know the details of the Julia type system to learn parallel programming in Julia, but you need to have a quite advanced knowledge of how Julia functions work.\n",
    "\n",
    "### Defining functions\n",
    "\n",
    "Functions are defined as shown in next cell. The closing `end` is necessary. Do not forget it! However, the `return` is optional. The value of last line is returned by default. Indentation is recommended, but it is also optional. That's why the closing `end` is needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "266b0e1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "function add(a,b)\n",
    "    return a + b  \n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de95c63c",
   "metadata": {},
   "source": [
    "Once defined, a function can be called using bracket notation as you would expect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39b452a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "add(1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afceb0fc",
   "metadata": {},
   "source": [
    "### Broadcast syntax\n",
    "\n",
    "We can apply functions to arrays element by element using broadcast (dot) syntax."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e65be5e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3]\n",
    "b = [4,5,6]\n",
    "add.(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4ca76a4",
   "metadata": {},
   "source": [
    "Mathematical operators can also be broadcasted (like in Matlab). Multiplying the vectors `a * b` directly won't work. If we want to multiply element by element, we can use the broadcasted version below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae0b3f6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a .* b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b131e208",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>Question (NB1-Q2):</b>  What will be the value of `x` in the last line ?\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d637d87",
   "metadata": {},
   "outputs": [],
   "source": [
    "function q(x)\n",
    "    x = 2\n",
    "    x\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "882d8e59",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 1\n",
    "y = q(x)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f69108c2",
   "metadata": {},
   "source": [
    "Run next cell to get an explanation of this question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05c62aa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "why_q2()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fc5eb9b",
   "metadata": {},
   "source": [
    "### References\n",
    "\n",
    "As you can see variables are passed \"by value\". Passing variables \"by reference\" is done using a reference.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ef5fe2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "function q!(x)\n",
    "    x[] = 2\n",
    "    x\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba245808",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = Ref(1)\n",
    "q!(x)\n",
    "x[]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d20bc3a4",
   "metadata": {},
   "source": [
    "### Defining functions (shorter way)\n",
    "\n",
    "For short functions, we can skip the `function` and `end` keywords as follows.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7815aea5",
   "metadata": {},
   "outputs": [],
   "source": [
    "add_short(a,b) = a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7aa48db2",
   "metadata": {},
   "outputs": [],
   "source": [
    "add_short(1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f39c31c0",
   "metadata": {},
   "source": [
    "### Anonymous (lambda) functions\n",
    "\n",
    "Since we can assign function to variables, it is not needed for a function to have a function name in many cases. We can simply create an anonymous function (i.e., a function without name) and assign it to a variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94ce2be7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "add_anonymous = (a,b) -> a+b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b15b84d8",
   "metadata": {},
   "source": [
    "We can call the function by using the variable name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d80a59",
   "metadata": {},
   "outputs": [],
   "source": [
    "add_anonymous(2.0,3.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1723e4f1",
   "metadata": {},
   "source": [
    "Note that `add_anonymous` is not a function name. It is just a variable associated with a function with no function name (well, it has a name technically, but with an arbitrary value)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bf111a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "nameof(add_anonymous)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e23f9f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "nameof(add)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e530acd3",
   "metadata": {},
   "source": [
    "### Functions are first-class objects\n",
    "\n",
    "We can work with Julia functions like with any other type of object. For instance, we can assign functions to variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e57fe028",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = add"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a8f4bb0",
   "metadata": {},
   "source": [
    "Now, we can call the function using the variable name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7e4d4ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "a(4,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c01be92e",
   "metadata": {},
   "source": [
    "We can also create an array of functions (this will not work in Python)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3837bf25",
   "metadata": {},
   "outputs": [],
   "source": [
    "funs = [+,-,*]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aac151b",
   "metadata": {},
   "source": [
    "To call a specific function in the array, we index the array and then call the returned function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aab45992",
   "metadata": {},
   "outputs": [],
   "source": [
    "funs[2](2,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0c668c8",
   "metadata": {},
   "source": [
    "### Higher-order functions\n",
    "\n",
    "Higher order functions are functions that take and/or return other functions. And example is the `count` function in Julia.\n",
    "\n",
    "For instance, we can pass a user-defined function to count the number of even elements in an array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22115f82",
   "metadata": {},
   "outputs": [],
   "source": [
    "func = i->i%2==0\n",
    "a = [1,2,3,5,32,2,4]\n",
    "count(func,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b352cdc",
   "metadata": {},
   "source": [
    "### Do-blocks\n",
    "\n",
    "There is yet another way to define anonymous functions. If a function takes a function in its first argument (like `count`) we can skip the first argument, when calling the function, and define the function we want to pass in a do-block. This is useful, e.g., if we want to define a multi-line anonymous function. The two next cells are equivalent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "533a21f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "function f(i)\n",
    "    m = i%2\n",
    "    m != 0\n",
    "end\n",
    "count(f,[1,2,3,5,32,2,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68b5165a",
   "metadata": {},
   "outputs": [],
   "source": [
    "count([1,2,3,5,32,2,4]) do i\n",
    "    m = i%2\n",
    "    m != 0\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "559a084d",
   "metadata": {},
   "source": [
    "### Returning multiple values\n",
    "\n",
    "Julia functions always return a single variable. To return multiple values, we can wrap them in a tuple.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eba30561",
   "metadata": {},
   "outputs": [],
   "source": [
    "function divrem(a,b)\n",
    "    α = div(a,b)\n",
    "    β = rem(a,b)\n",
    "    (α,β)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a6e9d0d",
   "metadata": {},
   "source": [
    "The output is a tuple as expected, but we can recover the individual values by unpacking the tuple."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb0c2806",
   "metadata": {},
   "outputs": [],
   "source": [
    "d,r = divrem(10,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31428737",
   "metadata": {},
   "outputs": [],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76424bb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c73ae75b",
   "metadata": {},
   "source": [
    "### Variable number of input arguments\n",
    "\n",
    "Functions with multiple arguments are also supported. The following example iterates over the given arguments and prints them. `args` is just a tuple with all arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7f94ff0",
   "metadata": {},
   "outputs": [],
   "source": [
    "function showargs(args...)\n",
    "    for (i,arg) in enumerate(args)\n",
    "        println(\"args[$i] = $arg\")\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7828534e",
   "metadata": {},
   "outputs": [],
   "source": [
    "showargs(1,\"Hi!\",π)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f31fc3ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "showargs(6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc47d83b",
   "metadata": {},
   "source": [
    "### Positional and keyword arguments\n",
    "\n",
    "Functions can combine positional and keyword arguments much like in Python, but keyword arguments start with semicolon `;` in Julia."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37cd0314",
   "metadata": {},
   "outputs": [],
   "source": [
    "function foo(a,b;c,d)\n",
    "    println(\"Positional: a=$a, b=$b. Keyword: c=$c, d=$d\")\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee19db3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(3,4,d=2,c=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4919ef95",
   "metadata": {},
   "source": [
    "### Optional arguments\n",
    "\n",
    "We can provide default values to arguments to make them optional."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "650e6d67",
   "metadata": {},
   "outputs": [],
   "source": [
    "function bar(a,b=0;c,d=1)\n",
    "    println(\"Positional: a=$a, b=$b. Keyword: c=$c, d=$d\")\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5d03536",
   "metadata": {},
   "outputs": [],
   "source": [
    "bar(1,c=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94599fa3",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>Question (NB1-Q3):</b>  Which will be the value of `x` below?  \n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79bd84bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "function hofun(x)\n",
    "    y -> x*y\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9a7b774",
   "metadata": {},
   "outputs": [],
   "source": [
    "f2 = hofun(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6606086",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = f2(3)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "062ff145",
   "metadata": {},
   "source": [
    "Run next cell to get an explanation of this question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bf7818e",
   "metadata": {},
   "outputs": [],
   "source": [
    "why_q3()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc8e9bcf",
   "metadata": {},
   "source": [
    "## Arrays\n",
    "\n",
    "Julia supports multi-dimensional arrays. They are very similar to Numpy arrays in Python. Let's learn the basics of Julia arrays.\n",
    "\n",
    "\n",
    "### Array literals\n",
    "\n",
    "We can create (small) arrays from the given values using array literals.\n",
    "\n",
    "Next cell creates a vector with 3 integers. Note for Python users: there is no difference between vectors and lists in Julia."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08652696",
   "metadata": {},
   "outputs": [],
   "source": [
    "vec = [1,2,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70f15e92",
   "metadata": {},
   "source": [
    "We can create a matrix as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3aacf71",
   "metadata": {},
   "outputs": [],
   "source": [
    "mat = [1 2 3 4\n",
    "       5 6 7 8\n",
    "       9 10 11 12]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e096b198",
   "metadata": {},
   "source": [
    "### Array initialization\n",
    "\n",
    "We can create arrays with all the entries equal to zero, to one, or to a specific given value. The value can be any Julia object, even a function!\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9357edac",
   "metadata": {},
   "outputs": [],
   "source": [
    "zeros(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfc88af3",
   "metadata": {},
   "outputs": [],
   "source": [
    "zeros(Int,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49e5a779",
   "metadata": {},
   "outputs": [],
   "source": [
    "ones(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bbde94a",
   "metadata": {},
   "outputs": [],
   "source": [
    "fill(5.0,3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2c44cce",
   "metadata": {},
   "outputs": [],
   "source": [
    "fill(add,3,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "495827bc",
   "metadata": {},
   "source": [
    "### Array comprehensions\n",
    "\n",
    "We can also create the items in the array using a loop within an array comprehension."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b3c56b4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "squares = [ i^2 for i in 1:8 ]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cee954ce",
   "metadata": {},
   "source": [
    "### Indexing\n",
    "\n",
    "We can get and set the items of an array by indexing the array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df901c08",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc58cf4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares[end]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa7936e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cf66ef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares[4] = 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9125cbeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares[2:3] = [4,9]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f64021ab",
   "metadata": {},
   "source": [
    "### Immutable element type"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6650d6e7",
   "metadata": {},
   "source": [
    "Note that once set, the type of the elements in the array cannot be changed. If we try to set an item with an object of a different type, Julia will try to do a conversion, which can fail depending on the passed value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c79b297e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10,11,12,13]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2694f3d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[2] = \"Hi!\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92bae51d",
   "metadata": {},
   "source": [
    "### Arrays of any element type\n",
    "\n",
    "Arrays of fixed element type seem to be very rigid, right? Python list do not this limitation. However, we can use arrays of the `Any` type, which are as flexible as Python lists, or even more since they can also contain functions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d656b9ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = Any[10,11,12,13]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d08e4ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[3] = \"HI!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92bf2cab",
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c72351a",
   "metadata": {},
   "source": [
    "### Loops\n",
    "\n",
    "The loop in next cell visits the elements in `a` one after the other. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72dfc0f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10,20,30,40]\n",
    "for ai in a\n",
    "    @show ai\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4906337",
   "metadata": {},
   "source": [
    "This loop visits the integers from 1 to the length of the array and indexes the array at each of these integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75f5ab89",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in 1:length(a)\n",
    "    ai = a[i]\n",
    "    @show (i,ai)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7abfdd6",
   "metadata": {},
   "source": [
    "This loop \"enumerates\" the items in the array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96dfab12",
   "metadata": {},
   "outputs": [],
   "source": [
    "for (i,ai) in enumerate(a)\n",
    "    @show (i,ai)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4747037",
   "metadata": {},
   "source": [
    "### Arrays indices are 1-based by default\n",
    "\n",
    "Be aware of this if you are a C or Python user."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "694ec903",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10,20,30,40]\n",
    "a[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2ed5b01",
   "metadata": {},
   "source": [
    "### Slicing allocates a new array\n",
    "\n",
    "This is also different from Numpy in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "961a82a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1 2 3\n",
    "     4 5 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c9f2162",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = a[:,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4664eeb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "s[2] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c356e67d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b75a2b83",
   "metadata": {},
   "source": [
    "### Array views\n",
    "\n",
    "If you want to modify the original array, use `view` instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d8e2ab7",
   "metadata": {},
   "outputs": [],
   "source": [
    "v = view(a,:,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "329079bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "v[1] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b26a483",
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fef8495e",
   "metadata": {},
   "source": [
    "## Exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d84ceaf",
   "metadata": {},
   "source": [
    "### Exercise 1\n",
    "\n",
    "Implement a function `ex1(a)` that finds the largest item in the array `a`. It should return the largest item and its corresponding position in the array. If there are multiple maximal elements, then the first one will be returned. Assume that the array is not empty. Implement the function in the next cell. Test your implementation with the other one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e6c7091",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Implement here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "370afcca",
   "metadata": {},
   "outputs": [],
   "source": [
    "using Test\n",
    "arr = [3,4,7,3,1,7,2]\n",
    "@test ex1(arr) == (7,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7be19eb4",
   "metadata": {},
   "source": [
    "### Exercise 2\n",
    "\n",
    "Implement a function `ex2(f,g)` that takes two functions `f(x)` and `g(x)` and returns a new function `h(x)` representing the sum of `f` and `g`, i.e., `h(x)=f(x)+g(x)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "331e90b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Implement here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e86c981",
   "metadata": {},
   "outputs": [],
   "source": [
    "h = ex2(sin,cos)\n",
    "xs = LinRange(0,2π,100)\n",
    "@test all(x-> h(x) == sin(x)+cos(x), xs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3324a991",
   "metadata": {},
   "source": [
    "### Exercise 3 (hard)\n",
    "\n",
    "Function `mandel` estimates if a given point `(x,y)` in the complex plane belongs to the [Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ac481c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "function mandel(x,y,max_iters)\n",
    "    z = Complex(x,y)\n",
    "    c = z\n",
    "    threshold=2\n",
    "    for n in 1:max_iters\n",
    "        if abs(z)>threshold\n",
    "            return n-1\n",
    "        end\n",
    "        z = z^2 +c\n",
    "    end\n",
    "    max_iters\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86ea2591",
   "metadata": {},
   "source": [
    "If the value of `mandel` is less than `max_iters`, the point is provably outside the Mandelbrot set. If `mandel` is equal to  `max_iters`, then the point is provably inside the set. The larger `max_iters`, the better the quality of the estimate (the nicer will be your plot).\n",
    "\n",
    "Plot the value of function `mandel` for each pixel in a 2D grid of the box.\n",
    "\n",
    "$$(-1.7,0.7)\\times(-1.2,1.2).$$\n",
    "\n",
    "Use a grid resolution of at least 1000 points in each direction and `max_iters` at least 10. You can increase these values to get nicer plots. To plot the values use function `heatmap` from the Julia package `GLMakie`. Use `LinRange` to divide the horizontal and vertical axes into pixels. See the documentation of these functions for help. `GLMakie` is a GPU-accelerated plotting back-end for Julia. It is a large package and it can take some time to install and to generate the first plot. Be patient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33137ad9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Implement here"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "357e0490",
   "metadata": {},
   "source": [
    "# License\n",
    "\n",
    "This notebook is part of the course [Programming Large Scale Parallel Systems](https://www.francescverdugo.com/XM_40017/) at Vrije Universiteit Amsterdam and may be used under a [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) license."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.10.0",
   "language": "julia",
   "name": "julia-1.10"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
