diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e32e8c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.ipynb_checkpoints +/01-Introduction/.ipynb_checkpoints +/02-DataFrames/.ipynb_checkpoints +/03-HypothesisTests/.ipynb_checkpoints +/04-GLM/.ipynb_checkpoints +/05-Optimization/.ipynb_checkpoints +/06-MCMC/.ipynb_checkpoints +/07-MachineLearning/.ipynb_checkpoints diff --git a/01-Introduction/01-01-Introduction.ipynb b/01-Introduction/01-01-Introduction.ipynb new file mode 100644 index 0000000..d1f64c7 --- /dev/null +++ b/01-Introduction/01-01-Introduction.ipynb @@ -0,0 +1,64 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Some Advantageous Julia Features" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Julia is a valuable programming language for a number of reasons, including:\n", + "- performance (usually within a factor of two of C code),\n", + "- ease of programming providing a high-level syntax,\n", + "- carefully designed numerical accuracy,\n", + "- integration of mature C and Fortran libraries for linear algebra,\n", + "- bindings for a range of mature libraries such as GSL, NLopt, Sundials and MPI,\n", + "- a message passing implementation for parallel and distributed computation,\n", + "- ability to call C functions directly without needing wrappers,\n", + "- ability to call Python functions,\n", + "- a built-in git-based package manager,\n", + "- centralized package ecosystem,\n", + "- an active community that reviews and improves the code base and contributes packages,\n", + "- free usage (MIT licensed)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How to Get Started with Julia" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are various Julia environments to get started:\n", + "- the REPL, a shell-alike interactive environment,\n", + "- the command line; write your script using your favourite text editor and pass it to Julia as an argument (`julia script.jl arg1 arg2`),\n", + "- [IJulia](https://github.com/JuliaLang/IJulia.jl), the browser-based graphical notebook interface to Julia,\n", + "- [Sublime-IJulia](https://github.com/quinnj/Sublime-IJulia), a Julia plugin for Sublime,\n", + "- [Julia-LT](https://github.com/one-more-minute/Julia-LT), a Julia plugin for Light Table." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-02-BasicScalarTypes.ipynb b/01-Introduction/01-02-BasicScalarTypes.ipynb new file mode 100644 index 0000000..7d10774 --- /dev/null +++ b/01-Introduction/01-02-BasicScalarTypes.ipynb @@ -0,0 +1,491 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Basic Scalar Types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Integers and Floating-Point Numbers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign the value 1 to variable x\n", + "x = 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Find out the type of x\n", + "typeof(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether x is of type Int64\n", + "isa(x, Int64)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether x is of type Int64\n", + "isa(x, Float64)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether x is a string\n", + "isa(x, String)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Convert integer x to float\n", + "convert(Float64, x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# A more convenient way to convert integer x to float\n", + "y = float64(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether the resulting value is of type Float64\n", + "isa(y, Float64)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Do arithmetic with x, for example increment its value by 1\n", + "x += 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Print x using println\n", + "println(\"x = \", x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Print x using C printf() style format specification string\n", + "@printf(\"%d\", x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Round π to the second decimal place\n", + "round(pi, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# NaN is a floating point number\n", + "typeof(NaN)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign 1.0 and NaN to x and y respectively\n", + "x, y = 1.0, NaN" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether x is NaN\n", + "x == NaN" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether y is NaN\n", + "isnan(y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# NaN behaves as a floating-point number, so operations on it are defined\n", + "1.0*NaN" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Inf and -Inf are also floating point numbers\n", + "typeof(Inf), typeof(-Inf)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Inf and -Inf behave as floating-point numbers, so operations on them are defined\n", + "1*(-Inf), 1/Inf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether a floating point number is finite\n", + "isfinite(1.0), isfinite(Inf)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Complex Numbers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a complex number with real and imaginary parts equal to 1 and 3 respectively\n", + "complex(1, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign a complex number to x\n", + "x = 1 + 2im" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the real part of x\n", + "real(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the imaginary part of x\n", + "imag(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the conjugate of x\n", + "conj(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the absolute value of x\n", + "abs(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the phase angle of x in radians\n", + "angle(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Compute the cosine of x\n", + "cos(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Rational Numbers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a rational number using the // operator\n", + "x = 6//9" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the numerator of x\n", + "num(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the denominator of x\n", + "den(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether two rationals are equal\n", + "isequal(2//3, x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Convert a rational to a float\n", + "float64(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether a rational and a float are equal\n", + "isequal(2//3, float(x)), isequal(3//4, float(3//4))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The promotion system makes the necessary convertions to operate on rational and floating point numbers\n", + "x+pi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# It is possible to define a rational with 0 denominator\n", + "1//0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# If the denominator is 0, then the rational is infinite\n", + "isfinite(1//0)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-03-Strings.ipynb b/01-Introduction/01-03-Strings.ipynb new file mode 100644 index 0000000..f5b3d6b --- /dev/null +++ b/01-Introduction/01-03-Strings.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Strings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign string \"Julia\" to name variable\n", + "name = \"Julia\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# \"Julia\" is an ASCII string, whereas \"Юлия\" is a UTF-8 string\n", + "typeof(name), typeof(\"Юлия\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get name length\n", + "endof(name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# A more conventional way of getting name length\n", + "length(name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the first character of name\n", + "name[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the last character of name (end equals endof(name))\n", + "name[end]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get all but last characters of name\n", + "name[1:end-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Iterate through (and print) the characters of name\n", + "for c in name\n", + " println(c)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Concatenate strings\n", + "\"Hi \"*name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of concatenating strings\n", + "string(\"Hi \", name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Yet another way of concatenating strings via string interpolation\n", + "\"Hi $name\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# String interpolation can be used with characters\n", + "\"Hi $(name[1])\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# In fact, string interpolation is possible with any expression\n", + "\"Hi $(4+1) times $name\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Repeat string\n", + "\"Hi, \"^2*name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# An alternative way of repeating strings\n", + "repeat(\"Hi, \", 2)*name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Search for the 'u' character in name\n", + "search(name, 'u')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check if name contains \"ul\" string\n", + "contains(name, \"ul\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Non-Standard String Literals" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Julia provides rich string processing support. It comes with non-standard string literals, such as regular expressions, byte array literals and version number literals. See the Julia documentation for more information." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-04-TuplesAndRanges.ipynb b/01-Introduction/01-04-TuplesAndRanges.ipynb new file mode 100644 index 0000000..309f1c8 --- /dev/null +++ b/01-Introduction/01-04-TuplesAndRanges.ipynb @@ -0,0 +1,273 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tuples" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Empty tuple\n", + "()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# One-element tuple\n", + "(1,)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Two-element tuple\n", + "x = (1, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# It is possible to create a tuple even if the parentheses are omitted\n", + "y = 1, 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Tuples are immutable, the following returns an error\n", + "try:\n", + " x[1] = 3\n", + "catch e\n", + " println(e)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Tuple unpacking\n", + "a, b = x\n", + "println(\"a = \", a, \", b = \", b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the type of tuple elements\n", + "eltype(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get tuple length\n", + "length(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check whether number 2 is contained in tuple x\n", + "in(2, x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Ranges" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define a range from 1 to 2 with step 0.5\n", + "x = 1:0.5:2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the type of range elements\n", + "eltype(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the length of x\n", + "length(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the first element of x\n", + "first(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of getting the first element of x\n", + "x[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the step of x\n", + "step(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the last element of x\n", + "last(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of getting the last element of x\n", + "x[end]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Note in the example below how the last element of the range\n", + "y = 1:0.5:2.1\n", + "last(y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get all but the last element of x\n", + "x[1:end-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Collect the elements specified by the range into an one-dimensional array\n", + "collect(x)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-05-Arrays.ipynb b/01-Introduction/01-05-Arrays.ipynb new file mode 100644 index 0000000..4246d5a --- /dev/null +++ b/01-Introduction/01-05-Arrays.ipynb @@ -0,0 +1,921 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Arrays" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## One-Dimensional Arrays" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array (vector) of length 3\n", + "x = [1.0, 3.5, 9.0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Note the differnce; omitting the commas creates a two-dimensional array of size (1, 3)\n", + "[1.0 3.5 9.0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The same result is attained by transposing x\n", + "x'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the type of array elements\n", + "eltype(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get array length\n", + "length(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get number of array dimesions\n", + "ndims(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get array size (dimensions)\n", + "size(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an uninitialized one-dimesional array of length 0\n", + "Array(Float64, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an uninitialized one-dimensional dense array of length 3\n", + "Array(Float64, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array of length 3 filled with zeros\n", + "zeros(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array of length 3 filled with zeros specifying the type of the array's elements\n", + "zeros(Int64, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array of length 3 filled with ones\n", + "ones(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array of length 3 filled with fives\n", + "fill(5.0, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array of length 3 filled with trues\n", + "trues(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an one-dimensional array of length 3 filled with falses\n", + "falses(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Draw 3 samples from a uniform U(0, 1) distribution\n", + "rand(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Draw 3 samples from a normal N(0, 1) distribution\n", + "randn(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the first element of x\n", + "getindex(x, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of getting the first element of x\n", + "x[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the last element of x\n", + "x[end]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get all but the last element of x\n", + "x[1:end-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Shallow copy of x\n", + "y = copy(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Deep copy of x\n", + "z = deepcopy(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Common operations can be performed vector-wise or, by using a \".\" prefix, element-wise\n", + "y = [3.0, 5.0, 2.5] \n", + "x+y, x.+y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Multiply the (1, 3) array x' with the (3, ) array y\n", + "x'*y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The same result is attained by computing the dot product of x with y\n", + "dot(x, y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Multiply element-wise the vectors x and y\n", + "x.*y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Perform vectorized exponentiation\n", + "exp(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Two-Dimensional Arrays" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an two-dimensional array (matrix) of size (3, 2)\n", + "A = [1.0 3.5; 0.5 2.0; 7.0 1.5]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of constructing the same matrix\n", + "[[1.0 3.5]; [0.5 2.0]; [7.0 1.5]]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Note the differnce; adding the commas creates a one-dimensional array of length 6\n", + "[[1.0, 3.5]; [0.5, 2.0]; [7.0, 1.5]]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the type of array elements\n", + "eltype(A)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get array length\n", + "length(A)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get number of array dimesions\n", + "ndims(A)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get array size (dimensions)\n", + "size(A)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get number of rows\n", + "size(A, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get number of columns\n", + "size(A, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an uninitialized two-dimesional array of size (0, 0)\n", + "Array(Float64, 0, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an uninitialized two-dimensional dense array of size (3, 2)\n", + "Array(Float64, 3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a two-dimensional array of size (3, 2) filled with zeros\n", + "zeros(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a two-dimensional array of size (3, 2) filled with zeros specifying the type of the array's elements\n", + "zeros(Int64, 3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a two-dimensional array of size (3, 2) filled with ones\n", + "ones(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a two-dimensional array of size (3, 2) filled with fives\n", + "fill(5.0, 3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a two-dimensional array of size (3, 2) filled with trues\n", + "trues(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a two-dimensional array of size (3, 2) filled with falses\n", + "falses(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an identity matrix of size (3, 3)\n", + "eye(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Draw 6 samples from a uniform U(0, 1) distribution and place them in a matrix of size (3, 2)\n", + "rand(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Draw 6 samples from a normal N(0, 1) distribution and place them in a matrix of size (3, 2)\n", + "randn(3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct two vectors, each of length 3\n", + "x, y = [1.0, 3.5, 10.0], [7.0, 2.5, 9.0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Concatenated the two vectors horizontally into a matrix of size (3, 2)\n", + "C = hcat(x, y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Concatenated the two vectors vertically into a vector of length 6\n", + "vcat(x, y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the transpose of C\n", + "C'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Reshape the (3, 2) matrix C to a (2, 3) matrix\n", + "# Notice that this is not tha same as taking the transpose of C\n", + "reshape(C, 2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the element of matrix C in the position (2, 2)\n", + "getindex(C, 2, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of getting the element of matrix C in the position (2, 2)\n", + "C[2, 2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the elements in the first and second row of the second column of C\n", + "getindex(C, 1:2, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of getting the elements in the first and second row of the second column of C\n", + "C[1:2, 2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Shallow copy\n", + "U = copy(C)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Deep copy\n", + "V = deepcopy(C)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Common operations can be performed matrix-wise or, by using a \".\" prefix, element-wise\n", + "A+C, A.+C" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Perform matrix multiplication of (3, 2) matrix A with (2, 3) matrix C'\n", + "A*C'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Perform elementi-wise multiplication of (3, 2) matrix A with (3, 2) matrix C\n", + "A.*C" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Perform matrix-wise exponentiation\n", + "exp(C)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Multi-Dimensional Arrays" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an uninitialized three-dimensional array of size (3, 3, 2)\n", + "Array(Float64, 3, 3, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a matrix by repeating the identity (2, 2) matrix 2 times in the first and 3 times in the second dimension\n", + "repmat(eye(2), 2, 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Cell Arrays (Heterogeneous Arrays)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a cell array\n", + "x = cell(2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Fill the first componenet of x with a Float64 vector of length 3\n", + "x[1] = [1, 2, 5]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Fill the second component of x with a string vector of length 2\n", + "x[2] = [\"Hi \", \"Julia\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the first element of the second component of x\n", + "x[2][1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Comprehensions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Use an empty comprehension to construct a vector with no elements\n", + "Float64[]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a vector using a comprehension (trivial example)\n", + "Float64[1.0, 2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct a vector using a comprehenstion which squares each element of the given range\n", + "[i^2 for i = 1:0.5:2]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-06-Dictionaries.ipynb b/01-Introduction/01-06-Dictionaries.ipynb new file mode 100644 index 0000000..113a261 --- /dev/null +++ b/01-Introduction/01-06-Dictionaries.ipynb @@ -0,0 +1,179 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dictionaries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an empty dictionary\n", + "Dict()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct an empty dictionary with keys of type ASCIIString and values of type Float64\n", + "Dict{ASCIIString, Float64}()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct dictionary with basketball player names as keys and number of NBA championships won by each player as values\n", + "champions = [\"Bird\"=>3, \"Johnson\"=>5, \"Jordan\"=>6]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the number of NBA of championships that Jordan has won\n", + "champions[\"Jordan\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the number of championships won by the players in the dictionary\n", + "values(champions)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check if Bird is in the dictionary\n", + "haskey(champions, \"Bird\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check if Pippen is in the dictionary\n", + "haskey(champions, \"Pippen\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "champions[\"Pippen\"] = 6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Check if Jordan has won 6 champions according to the dictionary\n", + "in((\"Jordan\", 6), champions)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of checking if Jordan has won 6 champions according to the dictionary\n", + "champions[\"Jordan\"] == 6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Iterate through dictionary to get name player and associated number of NBA championships won by him\n", + "for c in champions\n", + " println(\"$(c[1]) has won $(c[2]) champions\")\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "delete!(champions, \"Johnson\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Alternative way of iterating through dictionary to get name player and associated number of NBA championships won by him\n", + "# Notice that Johnson is not in the dictionary anymore since his entry has been deleted\n", + "for (k, v) in champions\n", + " println(\"$k has won $v champions\")\n", + "end" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-07-Functions.ipynb b/01-Introduction/01-07-Functions.ipynb new file mode 100644 index 0000000..d533b9a --- /dev/null +++ b/01-Introduction/01-07-Functions.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-08-CompositeTypes.ipynb b/01-Introduction/01-08-CompositeTypes.ipynb new file mode 100644 index 0000000..0b2e245 --- /dev/null +++ b/01-Introduction/01-08-CompositeTypes.ipynb @@ -0,0 +1,349 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Abstract Types" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define abstract type Shape\n", + "abstract Shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define abstract ConicSection type as sub-type of abstract type Shape\n", + "abstract ConicSection <: Shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Composite Types" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define composite type Point with fields the point coordinates x and y\n", + "type Point\n", + " x::Float64\n", + " y::Float64\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign the point Point(1.0, 2.0) to p\n", + "p = Point(1.0, 2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the abscissa (x field of p) and the ordinate (y field o p) of point p\n", + "p.x, p.y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Change the ordinate y of p\n", + "p.y = 3.0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The point p has been changed\n", + "show(p)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# A type can be declared as immutable\n", + "# Immutable types can be more efficient in some cases\n", + "# Once instantiated, they can't be mutated\n", + "immutable ImmutablePoint\n", + " x::Float64\n", + " y::Float64\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign the point Point(1.0, 2.0) to q\n", + "q = ImmutablePoint(1.0, 2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Once instantiated, the fields of an immutable type can't be mutated\n", + "try:\n", + " q.y = 3\n", + "catch e\n", + " println(e)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define Circle to be a sub-type of the abstract ConicSection type\n", + "type Circle <: ConicSection\n", + " centre::Point # Circle center is of type Point\n", + " radius::Float64 # Circle radius is a floating point number\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct the unit circle (with centre (0.0, 0.0) and radius 1)\n", + "circle = Circle(Point(0.0, 0.0), 1.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the ordinate of the circle's centre\n", + "circle.centre.y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "circle.radius" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define Rectangle to be a sub-type of the abstract Shape type\n", + "type Rectangle <: Shape\n", + " ll::Point # Lower left vertex of rectangle is of type Point\n", + " ur::Point # Upper right vertex of rectangle is of type Point\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct the rectangle with lower left vertex (0.0, 0.0) and upper right vertex (2.0, 1.0)\n", + "rectangle = Rectangle(Point(0.0, 0.0), Point(2.0, 1.0))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the ordinate of the rectangle's lower left vertex\n", + "rectangle.ll.y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Parametric Composite Types" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define parametric composite type Point with fields the point coordinates x and y and type parameter T\n", + "type ParametricPoint{T}\n", + " x::T\n", + " y::T\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign the point Point(1.0, 2.0) to p\n", + "p = ParametricPoint(1.0, 2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# p is of type ParametricPoint{Float64}\n", + "typeof(p)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign the point Point(1, 2) to p\n", + "p = ParametricPoint(1, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# p is of type ParametricPoint{Int64}\n", + "typeof(p)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Assign the point Point(1, 2) to p specifying explicitly the type parameter as Float64\n", + "p = ParametricPoint{Float64}(1, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# p is of type ParametricPoint{Float64}\n", + "typeof(p)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-09-Methods.ipynb b/01-Introduction/01-09-Methods.ipynb new file mode 100644 index 0000000..3102998 --- /dev/null +++ b/01-Introduction/01-09-Methods.ipynb @@ -0,0 +1,468 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Follow-Up Example Using User-Defined Shape Types" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "abstract Shape\n", + "abstract ConicSection <: Shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "type Point{T}\n", + " x::T\n", + " y::T\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "type Circle{T} <: ConicSection\n", + " centre::Point{T} # Circle center is of type Point{T}\n", + " radius::T # Circle radius is of type T\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define Rectangle to be a sub-type of the abstract Shape type\n", + "type Rectangle{T} <: Shape\n", + " ll::Point{T} # Lower left vertex of rectangle is of type Point{T}\n", + " ur::Point{T} # Upper right vertex of rectangle is of type Point{T}\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct the circle with centre (0.0, 0.0) and radius 2\n", + "circle = Circle(Point(0.0, 0.0), 2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Construct the rectangle with lower left vertex (0.0, 0.0) and upper right vertex (2.0, 1.0)\n", + "rectangle = Rectangle(Point(0.0, 0.0), Point(2.0, 1.0))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Defining Functions (Methods)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define the area function for Circle arguments, giving the circle's area\n", + "function area(shape::Circle)\n", + " pi*abs2(circle.radius)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "area(circle)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# A more succinct idiom for defining the area function for Circle arguments\n", + "area(shape::Circle) = pi*abs2(circle.radius)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "area(circle)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Multiple Dispatch" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define the area function for Rectangle arguments\n", + "# The name function is the same for different input arguments\n", + "# This is called multiple dispatch\n", + "area(shape::Rectangle) = abs(shape.ll.y-shape.ur.y)*abs(shape.ll.x-shape.ur.x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "area(rectangle)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Parametric Functions and Type Inference on Input Arguments" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# resize function returns a new circle with the same centre and radius rescaled by some coefficient c \n", + "resize(shape::Circle, c) = Circle(shape.centre, c*shape.radius)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Resize circle by halving its radius\n", + "resize(circle, 0.5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The type of c was previously omitted\n", + "# It is now stated explicitly that c is of type Float64\n", + "resize(shape::Circle, c::Float64) = Circle(shape.centre, c*shape.radius)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Resize circle by halving its radius using the redefined resize function\n", + "resize(circle, 0.5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Now resize is a parametric function with type parameter T shared between its input arguments\n", + "resize{T}(shape::Circle{T}, c::T) = Circle(shape.centre, c*shape.radius)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Resize circle by halving its radius using the redefined resize function\n", + "resize(circle, 0.5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Optional Arguments" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Provide the scaling factor c as an optional argument\n", + "# The default value of c is set to return a circle of radius 1\n", + "resize(shape::Circle, c=1/shape.radius) = Circle(shape.centre, c*shape.radius)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Call resize with one argument (the second argument takes its default value)\n", + "resize(circle)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Named Arguments" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Provide the scaling factor as the named argument \"scaling\"\n", + "# The default value of \"scaling\" is set to return a circle of radius 1\n", + "resize(shape::Circle; scaling=1/shape.radius) = Circle(shape.centre, scaling*shape.radius)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Call resize with scaling=2\n", + "resize(circle, scaling=2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Functions Changing Their Input" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# resize! is defined in Base already, so import it to extend it\n", + "import Base.resize!\n", + "\n", + "# resize function returns a new circle with the same centre and radius rescaled by some coefficient c\n", + "# Names of functions changing their input are conventionally suffixed by \"!\"\n", + "function resize!(shape::Circle, c)\n", + " shape.radius = c*shape.radius\n", + " shape\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "resize!(circle, 0.5);\n", + "circle" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Inner Constructors" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# It is possible to call the default Circle constructor with a negative radius\n", + "Circle(Point(0.0, 0.0), -1.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# This is one way of prohibiting negative radius\n", + "type CircleWithCheck{T} <: ConicSection\n", + " centre::Point{T} # Circle center is of type Point{T}\n", + " radius::T # Circle radius is of type T\n", + " \n", + " function CircleWitchCheck{T}(c::Point{T}, r::T)\n", + " radius > zero(radius) || error(\"Circle radius must be positive.\")\n", + " new(CircleWitchCheck(c, r))\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Passing a negative radius to the new constructor throws an error\n", + "try:\n", + " CircleWithCheck(Point(0.0, 0.0), -1.0)\n", + "catch e\n", + " println(e)\n", + "end" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Outer Constructors" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Constructors can be provided outside the type definition\n", + "# The following constructor facilitates defining a unit circle with fewer arguments\n", + "Circle() = Circle(Point(0.0, 0.0), 1.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Call the outer constructor to define a unit circle\n", + "Circle()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-10-FilesystemAndDataIO.ipynb b/01-Introduction/01-10-FilesystemAndDataIO.ipynb new file mode 100644 index 0000000..d2a2db6 --- /dev/null +++ b/01-Introduction/01-10-FilesystemAndDataIO.ipynb @@ -0,0 +1,206 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Filesystem and Data I/O" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Filesystem" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Print working directory\n", + "pwd()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the files and directories in the current directory\n", + "readdir()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Get the home directory\n", + "ENV[\"HOME\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Another way of getting the home directory\n", + "homedir()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Create the path ~/tmp/julia\n", + "JULIATMPDIR = joinpath(homedir(), \"tmp/julia\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# If ~/tmp/julia directory doesn't exist, then create it\n", + "if !isdir(JULIATMPDIR)\n", + " mkpath(JULIATMPDIR)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Create the directory ~/tmp/julia/dir01/dir02\n", + "mkpath(joinpath(JULIATMPDIR, \"dir01/dir02\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Delete the directory ~/tmp/julia/dir01/dir02\n", + "rm(joinpath(JULIATMPDIR, \"dir01\"), recursive=true)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data I/O" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Define a vector\n", + "x = [2.5, 3.0, 1.0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Write x to a file\n", + "writedlm(joinpath(JULIATMPDIR, \"out01.txt\"), x, ' ')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Write x to a file line by line\n", + "OUTFILE = joinpath(JULIATMPDIR, \"out02.txt\");\n", + "\n", + "outstream = open(OUTFILE, \"w+\");\n", + "\n", + "for i = 1:length(x)\n", + " @printf(outstream, \"%.f\\n\", x[i])\n", + "end\n", + "\n", + "close(outstream)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Read a file into a vector\n", + "y = readdlm(joinpath(JULIATMPDIR, \"out01.txt\"), ' ')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Read a file line by line\n", + "instream = open(OUTFILE, \"r\");\n", + "\n", + "while !eof(instream)\n", + " println(float64(readline(instream)))\n", + "end\n", + "\n", + "close(instream)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-11-Plotting.ipynb b/01-Introduction/01-11-Plotting.ipynb new file mode 100644 index 0000000..716720a --- /dev/null +++ b/01-Introduction/01-11-Plotting.ipynb @@ -0,0 +1,160 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Plotting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Julia Packages for Plotting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- [Gadfly](https://github.com/dcjones/Gadfly.jl),\n", + "- [Winston](https://github.com/nolta/Winston.jl),\n", + "- [Gaston](https://github.com/mbaz/Gaston.jl), gnuplot wrapper,\n", + "- [PyPlot](https://github.com/stevengj/PyPlot.jl), interface to the Matplotlib plotting library from Python,\n", + "- [Plotly](https://github.com/plotly/Plotly-Julia), interface to the plot.ly API,\n", + "- [Vega](https://github.com/johnmyleswhite/Vega.jl), interface for Vega visualization." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Gadfly Examples" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "using Gadfly" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example 1: Generating a histogram for a sample from a Beta(0.5, 0.5) distribution" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "using Distributions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "d = Beta(0.5, 0.5);\n", + "data = rand(d, 1000);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "plot(x=data, Geom.histogram)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example 2: Plotting overlayed paths of simulated Brownian motion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "using Brownian" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n = 500;\n", + "timepoints = 0:10/n:100;\n", + "processes = BrownianMotion(timepoints, 3);\n", + "paths = rand(processes);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "set_default_plot_size(19cm, 8cm)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "plot(x=repeat(collect(timepoints), outer=[3]),\n", + " y=reshape(paths, size(paths, 2)*size(paths, 1), 1),\n", + " color=repeat([\"1\", \"2\", \"3\"], inner=[length(timepoints)]),\n", + " Scale.discrete_color_manual(\"red\",\"blue\", \"green\"),\n", + " Geom.line,\n", + " Guide.xlabel(\"Time points\"),\n", + " Guide.ylabel(\"\"),\n", + " Guide.title(\"Simulated Brownian paths\"),\n", + " Guide.colorkey(\"Paths\"))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/01-Introduction/01-12-ModulesAndPackages.ipynb b/01-Introduction/01-12-ModulesAndPackages.ipynb new file mode 100644 index 0000000..cac15b7 --- /dev/null +++ b/01-Introduction/01-12-ModulesAndPackages.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Modules and Packages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02-DataFrames/02-01-DataFramesBasics.ipynb b/02-DataFrames/02-01-DataFramesBasics.ipynb new file mode 100644 index 0000000..765fc2d --- /dev/null +++ b/02-DataFrames/02-01-DataFramesBasics.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to DataFrames" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/03-HypothesisTests/03-01-HypothesisTestsBasics.ipynb b/03-HypothesisTests/03-01-HypothesisTestsBasics.ipynb new file mode 100644 index 0000000..ae2e27c --- /dev/null +++ b/03-HypothesisTests/03-01-HypothesisTestsBasics.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to HypothesisTests" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/04-GLM/04-01-GLMBasics.ipynb b/04-GLM/04-01-GLMBasics.ipynb new file mode 100644 index 0000000..3170e32 --- /dev/null +++ b/04-GLM/04-01-GLMBasics.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to GLM" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/05-Optimization/05-01-OptimizationBasics.ipynb b/05-Optimization/05-01-OptimizationBasics.ipynb new file mode 100644 index 0000000..a109e40 --- /dev/null +++ b/05-Optimization/05-01-OptimizationBasics.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to JuMP" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/06-MCMC/06-01-MCMCBasics.ipynb b/06-MCMC/06-01-MCMCBasics.ipynb new file mode 100644 index 0000000..60c695d --- /dev/null +++ b/06-MCMC/06-01-MCMCBasics.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to Lora" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/07-MachineLearning/07-01-MachineLearningBasics.ipynb b/07-MachineLearning/07-01-MachineLearningBasics.ipynb new file mode 100644 index 0000000..d2634f0 --- /dev/null +++ b/07-MachineLearning/07-01-MachineLearningBasics.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to MXNet" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "To be written-up soon." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays.ipynb b/arrays.ipynb deleted file mode 100644 index b2f83e3..0000000 --- a/arrays.ipynb +++ /dev/null @@ -1,857 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Arrays" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "One-Dimensional Arrays" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array (vector) of length 3\n", - "x = [1.0, 3.5, 9.0]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Note the differnce; omitting the commas creates a two-dimensional array of size (1, 3)\n", - "[1.0 3.5 9.0]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The same result is attained by transposing x\n", - "x'" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the type of array elements\n", - "eltype(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get array length\n", - "length(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get number of array dimesions\n", - "ndims(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get array size (dimensions)\n", - "size(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an uninitialized one-dimesional array of length 0\n", - "Array(Float64, 0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an uninitialized one-dimensional dense array of length 3\n", - "Array(Float64, 3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array of length 3 filled with zeros\n", - "zeros(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array of length 3 filled with zeros specifying the type of the array's elements\n", - "zeros(Int64, 3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array of length 3 filled with ones\n", - "ones(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array of length 3 filled with fives\n", - "fill(5.0, 3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array of length 3 filled with trues\n", - "trues(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an one-dimensional array of length 3 filled with falses\n", - "falses(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Draw 3 samples from a uniform U(0, 1) distribution\n", - "rand(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Draw 3 samples from a normal N(0, 1) distribution\n", - "randn(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the first element of x\n", - "getindex(x, 1)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of getting the first element of x\n", - "x[1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the last element of x\n", - "x[end]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get all but the last element of x\n", - "x[1:end-1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Shallow copy of x\n", - "y = copy(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Deep copy of x\n", - "z = deepcopy(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Common operations can be performed vector-wise or, by using a \".\" prefix, element-wise\n", - "y = [3.0, 5.0, 2.5] \n", - "x+y, x.+y" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Multiply the (1, 3) array x' with the (3, ) array y\n", - "x'*y" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The same result is attained by computing the dot product of x with y\n", - "dot(x, y)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Multiply element-wise the vectors x and y\n", - "x.*y" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Perform vectorized exponentiation\n", - "exp(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Two-Dimensional Arrays" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an two-dimensional array (matrix) of size (3, 2)\n", - "A = [1.0 3.5; 0.5 2.0; 7.0 1.5]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of constructing the same matrix\n", - "[[1.0 3.5]; [0.5 2.0]; [7.0 1.5]]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Note the differnce; adding the commas creates a one-dimensional array of length 6\n", - "[[1.0, 3.5]; [0.5, 2.0]; [7.0, 1.5]]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the type of array elements\n", - "eltype(A)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get array length\n", - "length(A)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get number of array dimesions\n", - "ndims(A)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get array size (dimensions)\n", - "size(A)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get number of rows\n", - "size(A, 1)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get number of columns\n", - "size(A, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an uninitialized two-dimesional array of size (0, 0)\n", - "Array(Float64, 0, 0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an uninitialized two-dimensional dense array of size (3, 2)\n", - "Array(Float64, 3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a two-dimensional array of size (3, 2) filled with zeros\n", - "zeros(3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a two-dimensional array of size (3, 2) filled with zeros specifying the type of the array's elements\n", - "zeros(Int64, 3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a two-dimensional array of size (3, 2) filled with ones\n", - "ones(3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a two-dimensional array of size (3, 2) filled with fives\n", - "fill(5.0, 3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a two-dimensional array of size (3, 2) filled with trues\n", - "trues(3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a two-dimensional array of size (3, 2) filled with falses\n", - "falses(3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an identity matrix of size (3, 3)\n", - "eye(3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Draw 6 samples from a uniform U(0, 1) distribution and place them in a matrix of size (3, 2)\n", - "rand(3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Draw 6 samples from a normal N(0, 1) distribution and place them in a matrix of size (3, 2)\n", - "randn(3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct two vectors, each of length 3\n", - "x, y = [1.0, 3.5, 10.0], [7.0, 2.5, 9.0]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Concatenated the two vectors horizontally into a matrix of size (3, 2)\n", - "C = hcat(x, y)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Concatenated the two vectors vertically into a vector of length 6\n", - "vcat(x, y)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the transpose of C\n", - "C'" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Reshape the (3, 2) matrix C to a (2, 3) matrix\n", - "# Notice that this is not tha same as taking the transpose of C\n", - "reshape(C, 2, 3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the element of matrix C in the position (2, 2)\n", - "getindex(C, 2, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of getting the element of matrix C in the position (2, 2)\n", - "C[2, 2]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the elements in the first and second row of the second column of C\n", - "getindex(C, 1:2, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of getting the elements in the first and second row of the second column of C\n", - "C[1:2, 2]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Shallow copy\n", - "U = copy(C)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Deep copy\n", - "V = deepcopy(C)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Common operations can be performed matrix-wise or, by using a \".\" prefix, element-wise\n", - "A+C, A.+C" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Perform matrix multiplication of (3, 2) matrix A with (2, 3) matrix C'\n", - "A*C'" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Perform elementi-wise multiplication of (3, 2) matrix A with (3, 2) matrix C\n", - "A.*C" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Perform matrix-wise exponentiation\n", - "exp(C)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Multi-Dimensional Arrays" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an uninitialized three-dimensional array of size (3, 3, 2)\n", - "Array(Float64, 3, 3, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a matrix by repeating the identity (2, 2) matrix 2 times in the first and 3 times in the second dimension\n", - "repmat(eye(2), 2, 3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Cell Arrays (Heterogeneous Arrays)" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a cell array\n", - "x = cell(2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Fill the first componenet of x with a Float64 vector of length 3\n", - "x[1] = [1, 2, 5]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Fill the second component of x with a string vector of length 2\n", - "x[2] = [\"Hi \", \"Julia\"]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the first element of the second component of x\n", - "x[2][1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Comprehensions" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Use an empty comprehension to construct a vector with no elements\n", - "Float64[]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a vector using a comprehension (trivial example)\n", - "Float64[1.0, 2]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a vector using a comprehenstion which squares each element of the given range\n", - "[i^2 for i = 1:0.5:2]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/basic_scalar_types.ipynb b/basic_scalar_types.ipynb deleted file mode 100644 index 99efdb6..0000000 --- a/basic_scalar_types.ipynb +++ /dev/null @@ -1,454 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Basic Scalar Types" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Integers and Floating-Point Numbers" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign the value 1 to variable x\n", - "x = 1" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Find out the type of x\n", - "typeof(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether x is of type Int64\n", - "isa(x, Int64)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether x is of type Int64\n", - "isa(x, Float64)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether x is a string\n", - "isa(x, String)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Convert integer x to float\n", - "convert(Float64, x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A more convenient way to convert integer x to float\n", - "y = float64(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether the resulting value is of type Float64\n", - "isa(y, Float64)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Do arithmetic with x, for example increment its value by 1\n", - "x += 1" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Print x using println\n", - "println(\"x = \", x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Print x using C printf() style format specification string\n", - "@printf(\"%d\", x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Round \u03c0 to the second decimal place\n", - "round(pi, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# NaN is a floating point number\n", - "typeof(NaN)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign 1.0 and NaN to x and y respectively\n", - "x, y = 1.0, NaN" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether x is NaN\n", - "x == NaN" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether y is NaN\n", - "isnan(y)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# NaN behaves as a floating-point number, so operations on it are defined\n", - "1.0*NaN" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Inf and -Inf are also floating point numbers\n", - "typeof(Inf), typeof(-Inf)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Inf and -Inf behave as floating-point numbers, so operations on them are defined\n", - "1*(-Inf), 1/Inf" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether a floating point number is finite\n", - "isfinite(1.0), isfinite(Inf)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Complex Numbers" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a complex number with real and imaginary parts equal to 1 and 3 respectively\n", - "complex(1, 3)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign a complex number to x\n", - "x = 1 + 2im" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the real part of x\n", - "real(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the imaginary part of x\n", - "imag(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the conjugate of x\n", - "conj(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the absolute value of x\n", - "abs(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the phase angle of x in radians\n", - "angle(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Compute the cosine of x\n", - "cos(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Rational Numbers" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct a rational number using the // operator\n", - "x = 6//9" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the numerator of x\n", - "num(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the denominator of x\n", - "den(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether two rationals are equal\n", - "isequal(2//3, x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Convert a rational to a float\n", - "float64(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether a rational and a float are equal\n", - "isequal(2//3, float(x)), isequal(3//4, float(3//4))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The promotion system makes the necessary convertions to operate on rational and floating point numbers\n", - "x+pi" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# It is possible to define a rational with 0 denominator\n", - "1//0" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# If the denominator is 0, then the rational is infinite\n", - "isfinite(1//0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/composite_types.ipynb b/composite_types.ipynb deleted file mode 100644 index 196a338..0000000 --- a/composite_types.ipynb +++ /dev/null @@ -1,331 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Types" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Abstract Types" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define abstract type Shape\n", - "abstract Shape" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define abstract ConicSection type as sub-type of abstract type Shape\n", - "abstract ConicSection <: Shape" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Composite Types" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define composite type Point with fields the point coordinates x and y\n", - "type Point\n", - " x::Float64\n", - " y::Float64\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign the point Point(1.0, 2.0) to p\n", - "p = Point(1.0, 2.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the abscissa (x field of p) and the ordinate (y field o p) of point p\n", - "p.x, p.y" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Change the ordinate y of p\n", - "p.y = 3.0" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The point p has been changed\n", - "show(p)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A type can be declared as immutable\n", - "# Immutable types can be more efficient in some cases\n", - "# Once instantiated, they can't be mutated\n", - "immutable ImmutablePoint\n", - " x::Float64\n", - " y::Float64\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign the point Point(1.0, 2.0) to q\n", - "q = ImmutablePoint(1.0, 2.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Once instantiated, the fields of an immutable type can't be mutated\n", - "try:\n", - " q.y = 3\n", - "catch e\n", - " println(e)\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define Circle to be a sub-type of the abstract ConicSection type\n", - "type Circle <: ConicSection\n", - " centre::Point # Circle center is of type Point\n", - " radius::Float64 # Circle radius is a floating point number\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct the unit circle (with centre (0.0, 0.0) and radius 1)\n", - "circle = Circle(Point(0.0, 0.0), 1.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the ordinate of the circle's centre\n", - "circle.centre.y" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "circle.radius" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define Rectangle to be a sub-type of the abstract Shape type\n", - "type Rectangle <: Shape\n", - " ll::Point # Lower left vertex of rectangle is of type Point\n", - " ur::Point # Upper right vertex of rectangle is of type Point\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct the rectangle with lower left vertex (0.0, 0.0) and upper right vertex (2.0, 1.0)\n", - "rectangle = Rectangle(Point(0.0, 0.0), Point(2.0, 1.0))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the ordinate of the rectangle's lower left vertex\n", - "rectangle.ll.y" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Parametric Composite Types" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define parametric composite type Point with fields the point coordinates x and y and type parameter T\n", - "type ParametricPoint{T}\n", - " x::T\n", - " y::T\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign the point Point(1.0, 2.0) to p\n", - "p = ParametricPoint(1.0, 2.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# p is of type ParametricPoint{Float64}\n", - "typeof(p)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign the point Point(1, 2) to p\n", - "p = ParametricPoint(1, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# p is of type ParametricPoint{Int64}\n", - "typeof(p)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign the point Point(1, 2) to p specifying explicitly the type parameter as Float64\n", - "p = ParametricPoint{Float64}(1, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# p is of type ParametricPoint{Float64}\n", - "typeof(p)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/dictionaries.ipynb b/dictionaries.ipynb deleted file mode 100644 index 7814383..0000000 --- a/dictionaries.ipynb +++ /dev/null @@ -1,169 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Dictionaries" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an empty dictionary\n", - "Dict()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct an empty dictionary with keys of type ASCIIString and values of type Float64\n", - "Dict{ASCIIString, Float64}()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct dictionary with basketball player names as keys and number of NBA championships won by each player as values\n", - "champions = [\"Bird\"=>3, \"Johnson\"=>5, \"Jordan\"=>6]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the number of NBA of championships that Jordan has won\n", - "champions[\"Jordan\"]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the number of championships won by the players in the dictionary\n", - "values(champions)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check if Bird is in the dictionary\n", - "haskey(champions, \"Bird\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check if Pippen is in the dictionary\n", - "haskey(champions, \"Pippen\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "champions[\"Pippen\"] = 6" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check if Jordan has won 6 champions according to the dictionary\n", - "in((\"Jordan\", 6), champions)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of checking if Jordan has won 6 champions according to the dictionary\n", - "champions[\"Jordan\"] == 6" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Iterate through dictionary to get name player and associated number of NBA championships won by him\n", - "for c in champions\n", - " println(\"$(c[1]) has won $(c[2]) champions\")\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "delete!(champions, \"Johnson\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Alternative way of iterating through dictionary to get name player and associated number of NBA championships won by him\n", - "# Notice that Johnson is not in the dictionary anymore since his entry has been deleted\n", - "for (k, v) in champions\n", - " println(\"$k has won $v champions\")\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/filesystem_and_data.ipynb b/filesystem_and_data.ipynb deleted file mode 100644 index 515adfe..0000000 --- a/filesystem_and_data.ipynb +++ /dev/null @@ -1,198 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Filesystem and Data I/O" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Filesystem" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Print working directory\n", - "pwd()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the files and directories in the current directory\n", - "readdir()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the home directory\n", - "ENV[\"HOME\"]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of getting the home directory\n", - "homedir()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Create the path ~/tmp/julia\n", - "JULIATMPDIR = joinpath(homedir(), \"tmp/julia\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# If ~/tmp/julia directory doesn't exist, then create it\n", - "if !isdir(JULIATMPDIR)\n", - " mkpath(JULIATMPDIR)\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Create the directory ~/tmp/julia/dir01/dir02\n", - "mkpath(joinpath(JULIATMPDIR, \"dir01/dir02\"))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Delete the directory ~/tmp/julia/dir01/dir02\n", - "rm(joinpath(JULIATMPDIR, \"dir01\"), recursive=true)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Data I/O" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define a vector\n", - "x = [2.5, 3.0, 1.0]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Write x to a file\n", - "writedlm(joinpath(JULIATMPDIR, \"out01.txt\"), x, ' ')" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Write x to a file line by line\n", - "OUTFILE = joinpath(JULIATMPDIR, \"out02.txt\");\n", - "\n", - "outstream = open(OUTFILE, \"w+\");\n", - "\n", - "for i = 1:length(x)\n", - " @printf(outstream, \"%.f\\n\", x[i])\n", - "end\n", - "\n", - "close(outstream)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Read a file into a vector\n", - "y = readdlm(joinpath(JULIATMPDIR, \"out01.txt\"), ' ')" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Read a file line by line\n", - "instream = open(OUTFILE, \"r\");\n", - "\n", - "while !eof(instream)\n", - " println(float64(readline(instream)))\n", - "end\n", - "\n", - "close(instream)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/functions.ipynb b/functions.ipynb deleted file mode 100644 index d35620c..0000000 --- a/functions.ipynb +++ /dev/null @@ -1,445 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Functions" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Follow-Up Example Using User-Defined Shape Types" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "abstract Shape\n", - "abstract ConicSection <: Shape" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "type Point{T}\n", - " x::T\n", - " y::T\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "type Circle{T} <: ConicSection\n", - " centre::Point{T} # Circle center is of type Point{T}\n", - " radius::T # Circle radius is of type T\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define Rectangle to be a sub-type of the abstract Shape type\n", - "type Rectangle{T} <: Shape\n", - " ll::Point{T} # Lower left vertex of rectangle is of type Point{T}\n", - " ur::Point{T} # Upper right vertex of rectangle is of type Point{T}\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct the circle with centre (0.0, 0.0) and radius 2\n", - "circle = Circle(Point(0.0, 0.0), 2.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Construct the rectangle with lower left vertex (0.0, 0.0) and upper right vertex (2.0, 1.0)\n", - "rectangle = Rectangle(Point(0.0, 0.0), Point(2.0, 1.0))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Defining Functions (Methods)" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define the area function for Circle arguments, giving the circle's area\n", - "function area(shape::Circle)\n", - " pi*abs2(circle.radius)\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "area(circle)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A more succinct idiom for defining the area function for Circle arguments\n", - "area(shape::Circle) = pi*abs2(circle.radius)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "area(circle)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Multiple Dispatch" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define the area function for Rectangle arguments\n", - "# The name function is the same for different input arguments\n", - "# This is called multiple dispatch\n", - "area(shape::Rectangle) = abs(shape.ll.y-shape.ur.y)*abs(shape.ll.x-shape.ur.x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "area(rectangle)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Parametric Functions and Type Inference on Input Arguments" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# resize function returns a new circle with the same centre and radius rescaled by some coefficient c \n", - "resize(shape::Circle, c) = Circle(shape.centre, c*shape.radius)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Resize circle by halving its radius\n", - "resize(circle, 0.5)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The type of c was previously omitted\n", - "# It is now stated explicitly that c is of type Float64\n", - "resize(shape::Circle, c::Float64) = Circle(shape.centre, c*shape.radius)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Resize circle by halving its radius using the redefined resize function\n", - "resize(circle, 0.5)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Now resize is a parametric function with type parameter T shared between its input arguments\n", - "resize{T}(shape::Circle{T}, c::T) = Circle(shape.centre, c*shape.radius)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Resize circle by halving its radius using the redefined resize function\n", - "resize(circle, 0.5)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Optional Arguments" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Provide the scaling factor c as an optional argument\n", - "# The default value of c is set to return a circle of radius 1\n", - "resize(shape::Circle, c=1/shape.radius) = Circle(shape.centre, c*shape.radius)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Call resize with one argument (the second argument takes its default value)\n", - "resize(circle)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Named Arguments" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Provide the scaling factor as the named argument \"scaling\"\n", - "# The default value of \"scaling\" is set to return a circle of radius 1\n", - "resize(shape::Circle; scaling=1/shape.radius) = Circle(shape.centre, scaling*shape.radius)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Call resize with scaling=2\n", - "resize(circle, scaling=2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Functions Changing Their Input" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# resize! is defined in Base already, so import it to extend it\n", - "import Base.resize!\n", - "\n", - "# resize function returns a new circle with the same centre and radius rescaled by some coefficient c\n", - "# Names of functions changing their input are conventionally suffixed by \"!\"\n", - "function resize!(shape::Circle, c)\n", - " shape.radius = c*shape.radius\n", - " shape\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "resize!(circle, 0.5);\n", - "circle" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Inner Constructors" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# It is possible to call the default Circle constructor with a negative radius\n", - "Circle(Point(0.0, 0.0), -1.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# This is one way of prohibiting negative radius\n", - "type CircleWithCheck{T} <: ConicSection\n", - " centre::Point{T} # Circle center is of type Point{T}\n", - " radius::T # Circle radius is of type T\n", - " \n", - " function CircleWitchCheck{T}(c::Point{T}, r::T)\n", - " radius > zero(radius) || error(\"Circle radius must be positive.\")\n", - " new(CircleWitchCheck(c, r))\n", - " end\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Passing a negative radius to the new constructor throws an error\n", - "try:\n", - " CircleWithCheck(Point(0.0, 0.0), -1.0)\n", - "catch e\n", - " println(e)\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Outer Constructors" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Constructors can be provided outside the type definition\n", - "# The following constructor facilitates defining a unit circle with fewer arguments\n", - "Circle() = Circle(Point(0.0, 0.0), 1.0)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Call the outer constructor to define a unit circle\n", - "Circle()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/index.ipynb b/index.ipynb index f591085..e3e6296 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,55 +1,141 @@ { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction to Julia" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a short Julia tutorial covering elementary concepts to get started with Julia.\n", + "- [Introduction](01-Introduction/01-01-Introduction.ipynb)\n", + "- [Basic Scalar Types](01-Introduction/01-02-BasicScalarTypes.ipynb)\n", + "- [Strings](01-Introduction/01-03-Strings.ipynb)\n", + "- [Tuples and Ranges](01-Introduction/01-04-TuplesAndRanges.ipynb)\n", + "- [Arrays](01-Introduction/01-05-Arrays.ipynb)\n", + "- [Dictionaries](01-Introduction/01-06-Dictionaries.ipynb)\n", + "- [Functions](01-Introduction/01-07-Functions.ipynb)\n", + "- [Composite Types](01-Introduction/01-08-CompositeTypes.ipynb)\n", + "- [Methods](01-Introduction/01-09-Methods.ipynb)\n", + "- [Filesystem and Data I/O](01-Introduction/01-10-FilesystemAndDataIO.ipynb)\n", + "- [Plotting](01-Introduction/01-11-Plotting.ipynb)\n", + "- [Modules and Packages](01-Introduction/01-12-ModulesAndPackages.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# DataFrames" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "- [Introduction to DataFrames](02-DataFrames/02-01-DataFramesBasics.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# HypothesisTests" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "- [Introduction to HypothesisTests](03-HypothesisTests/03-01-HypothesisTestsBasics.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# GLM" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "- [Introduction to GLM](04-GLM/04-01-GLMBasics.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Optimization" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "- [Introduction](05-Optimization/05-01-OptimizationBasics.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# MCMC" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "- [Introduction to Lora](06-MCMC/06-01-MCMCBasics.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Machine Learning" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "- [Introduction to MXNet](07-MachineLearning/07-01-MachineLearningBasics.ipynb)" + ] + } + ], "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Short Julia Tutorial" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is a short Julia tutorial covering elementary concepts to get started with Julia.\n", - "- [Introduction](introduction.ipynb)\n", - "- [Basic Scalar Types](basic_scalar_types.ipynb)\n", - "- [Strings](strings.ipynb)\n", - "- [Tuples and Ranges](tuples_and_ranges.ipynb)\n", - "- [Arrays](arrays.ipynb)\n", - "- [Dictionaries](dictionaries.ipynb)\n", - "- [Composite Types](composite_types.ipynb)\n", - "- [Functions](functions.ipynb)" - ] - }, - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Julia Toy Examples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- [Filesystem and Data I/O](filesystem_and_data.ipynb)\n", - "- [Distributions](distributions.ipynb)\n", - "- [Plotting](plotting.ipynb)" - ] - } - ], - "metadata": {} + "kernelspec": { + "display_name": "Julia 0.4.5", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.5" } - ] -} \ No newline at end of file + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/introduction.ipynb b/introduction.ipynb deleted file mode 100644 index 430f10e..0000000 --- a/introduction.ipynb +++ /dev/null @@ -1,62 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Some Advantageous Julia Features" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Julia is a valuable programming language for a number of reasons, including:\n", - "- performance (usually within a factor of two of C code),\n", - "- ease of programming providing a high-level syntax,\n", - "- carefully designed numerical accuracy,\n", - "- integration of mature C and Fortran libraries for linear algebra,\n", - "- bindings for a range of mature libraries such as GSL, NLopt, Sundials and MPI,\n", - "- a message passing implementation for parallel and distributed computation,\n", - "- ability to call C functions directly without needing wrappers,\n", - "- ability to call Python functions,\n", - "- a built-in git-based package manager,\n", - "- centralized package ecosystem,\n", - "- an active community that reviews and improves the code base and contributes packages,\n", - "- free usage (MIT licensed)." - ] - }, - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "How to Get Started with Julia" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "There are various Julia environments to get started:\n", - "- the REPL, a shell-alike interactive environment,\n", - "- the command line; write your script using your favourite text editor and pass it to Julia as an argument (`julia script.jl arg1 arg2`),\n", - "- [IJulia](https://github.com/JuliaLang/IJulia.jl), the browser-based graphical notebook interface to Julia,\n", - "- [Sublime-IJulia](https://github.com/quinnj/Sublime-IJulia), a Julia plugin for Sublime,\n", - "- [Julia-LT](https://github.com/one-more-minute/Julia-LT), a Julia plugin for Light Table." - ] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/plotting.ipynb b/plotting.ipynb deleted file mode 100644 index 6f0bf03..0000000 --- a/plotting.ipynb +++ /dev/null @@ -1,159 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Plotting" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Julia Packages for Plotting" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- [Gadfly](https://github.com/dcjones/Gadfly.jl),\n", - "- [Winston](https://github.com/nolta/Winston.jl),\n", - "- [Gaston](https://github.com/mbaz/Gaston.jl), gnuplot wrapper,\n", - "- [PyPlot](https://github.com/stevengj/PyPlot.jl), interface to the Matplotlib plotting library from Python,\n", - "- [Plotly](https://github.com/plotly/Plotly-Julia), interface to the plot.ly API,\n", - "- [Vega](https://github.com/johnmyleswhite/Vega.jl), interface for Vega visualization." - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Gadfly Examples" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "using Gadfly" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 3, - "metadata": {}, - "source": [ - "Example 1: Generating a histogram for a sample from a Beta(0.5, 0.5) distribution" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "using Distributions" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "d = Beta(0.5, 0.5);\n", - "data = rand(d, 1000);" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "plot(x=data, Geom.histogram)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 3, - "metadata": {}, - "source": [ - "Example 2: Plotting overlayed paths of simulated Brownian motion" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "using Brownian" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "n = 500;\n", - "timepoints = 0:10/n:100;\n", - "processes = BrownianMotion(timepoints, 3);\n", - "paths = rand(processes);" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "set_default_plot_size(19cm, 8cm)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "plot(x=repeat(collect(timepoints), outer=[3]),\n", - " y=reshape(paths, size(paths, 2)*size(paths, 1), 1),\n", - " color=repeat([\"1\", \"2\", \"3\"], inner=[length(timepoints)]),\n", - " Scale.discrete_color_manual(\"red\",\"blue\", \"green\"),\n", - " Geom.line,\n", - " Guide.xlabel(\"Time points\"),\n", - " Guide.ylabel(\"\"),\n", - " Guide.title(\"Simulated Brownian paths\"),\n", - " Guide.colorkey(\"Paths\"))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/strings.ipynb b/strings.ipynb deleted file mode 100644 index 7440cae..0000000 --- a/strings.ipynb +++ /dev/null @@ -1,227 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Strings" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Assign string \"Julia\" to name variable\n", - "name = \"Julia\"" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# \"Julia\" is an ASCII string, whereas \"\u042e\u043b\u0438\u044f\" is a UTF-8 string\n", - "typeof(name), typeof(\"\u042e\u043b\u0438\u044f\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get name length\n", - "endof(name)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A more conventional way of getting name length\n", - "length(name)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the first character of name\n", - "name[1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the last character of name (end equals endof(name))\n", - "name[end]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get all but last characters of name\n", - "name[1:end-1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Iterate through (and print) the characters of name\n", - "for c in name\n", - " println(c)\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Concatenate strings\n", - "\"Hi \"*name" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of concatenating strings\n", - "string(\"Hi \", name)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Yet another way of concatenating strings via string interpolation\n", - "\"Hi $name\"" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# String interpolation can be used with characters\n", - "\"Hi $(name[1])\"" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# In fact, string interpolation is possible with any expression\n", - "\"Hi $(4+1) times $name\"" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Repeat string\n", - "\"Hi, \"^2*name" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An alternative way of repeating strings\n", - "repeat(\"Hi, \", 2)*name" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Search for the 'u' character in name\n", - "search(name, 'u')" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check if name contains \"ul\" string\n", - "contains(name, \"ul\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Non-Standard String Literals" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Julia provides rich string processing support. It comes with non-standard string literals, such as regular expressions, byte array literals and version number literals. See the Julia documentation for more information." - ] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file diff --git a/tuples_and_ranges.ipynb b/tuples_and_ranges.ipynb deleted file mode 100644 index 1104f4d..0000000 --- a/tuples_and_ranges.ipynb +++ /dev/null @@ -1,257 +0,0 @@ -{ - "metadata": { - "language": "Julia", - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Tuples" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Empty tuple\n", - "()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# One-element tuple\n", - "(1,)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Two-element tuple\n", - "x = (1, 2)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# It is possible to create a tuple even if the parentheses are omitted\n", - "y = 1, 2" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Tuples are immutable, the following returns an error\n", - "try:\n", - " x[1] = 3\n", - "catch e\n", - " println(e)\n", - "end" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Tuple unpacking\n", - "a, b = x\n", - "println(\"a = \", a, \", b = \", b)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the type of tuple elements\n", - "eltype(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get tuple length\n", - "length(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Check whether number 2 is contained in tuple x\n", - "in(2, x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Ranges" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Define a range from 1 to 2 with step 0.5\n", - "x = 1:0.5:2" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the type of range elements\n", - "eltype(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the length of x\n", - "length(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the first element of x\n", - "first(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of getting the first element of x\n", - "x[1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the step of x\n", - "step(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get the last element of x\n", - "last(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another way of getting the last element of x\n", - "x[end]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Note in the example below how the last element of the range\n", - "y = 1:0.5:2.1\n", - "last(y)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Get all but the last element of x\n", - "x[1:end-1]" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Collect the elements specified by the range into an one-dimensional array\n", - "collect(x)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file