{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Arithmetic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook will be used as an introduction to the basic syntax of julia. If you have any experience with Python or Matlab then much of this should seem quite familiar. We will start, as is allways the case, with hello world..." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World\n" ] } ], "source": [ "println(\"Hello, World\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can define variables quickly and easilly just as we would in Python as well as use intuitive basic arithmetic such as +, -, *, / and ^ (Not $**$ as in Python)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24, 4.0\n" ] } ], "source": [ "a = 24\n", "b = a/6\n", "println(a, \", \",b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When using the notebook we can also use unicode characters for variable names for example $\\alpha$ by typing \"\\\\alpha\" and TAB completing or using the actual code. Functions are also overloaded to use unicode characters" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "36, 7\n" ] } ], "source": [ "α = 36\n", "β = α ÷ 5\n", "println(α, \", \", β)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will have noticed that while the above divisions are easilly divisable, using the / method will always result in floating point division, whereas using the unicode character will only do integer division, even if using floating point variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also substitute variables into strings using the $ symbol" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"This is a string with the number 2 in it\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 2\n", "string = \"This is a string with the number $a in it\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The precision of a variable will be dependent on your architecture but it can be specified by passing a value to a method (all available under tab completion)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Int64,Float16\n" ] } ], "source": [ "a = 4\n", "b = float16(4)\n", "\n", "println(typeof(a), \",\", typeof(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the type conversion method does not have a capital letter, this is the name of the type." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also an arbritary precision type than machine precision which can be defined using big()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "BigInt (constructor with 10 methods)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = big(4)\n", "typeof(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also define arrays quickly and easilly with no added packages, using different deliiting characters for columns and rows. Spaces indicates column seperation and semicolons indicate row seperation. (NOTE INDEXING IN JULIA STARTS AT 1 NOT 0)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2x3 Array{Int64,2}:\n", " 1 2 3\n", " 4 5 6" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr1 = [1 2 3; 4 5 6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see above that the result is an array of type integer (the particular integer type is dependent of the architecture of your machine), and of dimension 2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "Vectors can be defined using comma seperation" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 2.0\n", " 3.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vec1 = [1.,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which we can see is now a floating point array with dimension 1. Julia automatically detects the best type to use." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1x3 Array{Any,2}:\n", " \"Hello\" 2 \"the world\"" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strangearr = [\"Hello\" 2 \"the world\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays can be sliced as you would expect, however the end of an array is specified with end, not -1 as in python" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 3\n", " 4" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = [1, 2, 3, 4, 5, 6]\n", "subarr1 = arr[3:4]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 2\n", " 3\n", " 4\n", " 5" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "subarr2 = arr[2:end-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays are dynamic, and can be pushed into using the push!() method or appended with the append!() method. Note that the type of an array is not dynamic and it cannot aquire new members of different types. To do this, define the type of the array using T[1, 2, 3] where T is the desired type (remember that the Any type contains all types)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 1" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "push!(arr, 1)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 1\n", " 3\n", " 2\n", " 1" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "append!(arr, [3, 2, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operations on arrays are not elementwise in general. Julia uses the matlab convention of prepending operations with a . for elementwise operations. Transposes can be accesed with '" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 4\n", " 9" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = [1, 2, 3]\n", "arr .* arr" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1-element Array{Int64,1}:\n", " 14" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr' * arr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that unicode chars like \\cdot and \\times both work as a dot and cross product" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other Useful Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Complex and rational numbers are built into Julia and can be accessed using im and //" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Complex{Int64}, Rational{Int64}\n" ] } ], "source": [ "compl = 1 + im\n", "rat = 1//2\n", "println(typeof(compl),\", \",typeof(rat))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Control flow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "White space in Julia means nothing as opposed to python, therefore loops and ifs require ends" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "i = 0\n", "while i < 5\n", " println(i)\n", " i += 1\n", "end" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in 0:4\n", " println(i)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that 1:3 is not an array, it is a special type unit range. To use arrays then do as below" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0,1,2,3,4]\n", "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "arr = collect(0:4)\n", "println(arr)\n", "for i in arr\n", " println(i)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If statement are intuitive" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "for i in 0:4\n", " if i == 2\n", " println(i)\n", " end\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simple functions can be written inline" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x) = 1 + 4x" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia does not require the multiplication method for this and does not require a return statement. Without a return statement Julia will return the last value that was calculated, of course and explicit return statement can be used" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Longer functions can be defined as below" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "g (generic function with 1 method)" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function g(a, b)\n", " c = rand() * b\n", " d = c^a\n", "end" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.9841976344864111" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g(2,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can look at any defined methods or function usind the methods method. Built in functions will provide a link to the definitions" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "1 method for generic function g: