diff --git a/ 1. Getting started.ipynb b/ 1. Getting started.ipynb
index 222d22a..63be1fe 100644
--- a/ 1. Getting started.ipynb
+++ b/ 1. Getting started.ipynb
@@ -115,8 +115,8 @@
},
"outputs": [],
"source": [
- "my_name = \"Jane\"\n",
- "typeof(my_name)"
+ "😺 = \"smiley cat!\"\n",
+ "typeof(😺)"
]
},
{
@@ -134,7 +134,7 @@
},
"outputs": [],
"source": [
- "my_answer = my_name"
+ "my_answer = 😺"
]
},
{
@@ -261,7 +261,8 @@
"source": [
"### Exercises\n",
"\n",
- "1.1 Look up docs for the `convert` and `parse` functions."
+ "#### 1.1 \n",
+ "Look up docs for the `convert` function."
]
},
{
@@ -277,7 +278,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "1.2 Assign `365` to a variable named `days`. Convert `days` to a float."
+ "#### 1.2 \n",
+ "Assign `365` to a variable named `days`. Convert `days` to a float."
]
},
{
@@ -293,18 +295,17 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "1.3 See what happens when you execute\n",
+ "#### 1.3 \n",
+ "See what happens when you execute\n",
"\n",
"```julia\n",
- "convert(Int64, '1')\n",
+ "convert(Int64, \"1\")\n",
"```\n",
"and\n",
"\n",
"```julia\n",
- "parse(Int64, '1')\n",
- "```\n",
- "\n",
- "What's the difference?"
+ "parse(Int64, \"1\")\n",
+ "```"
]
},
{
diff --git a/ 2. Strings.ipynb b/ 2. Strings.ipynb
index 558745a..ffa88ac 100644
--- a/ 2. Strings.ipynb
+++ b/ 2. Strings.ipynb
@@ -124,10 +124,30 @@
"source": [
"name = \"Jane\"\n",
"num_fingers = 10\n",
- "num_toes = 10\n",
- "\n",
+ "num_toes = 10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
"println(\"Hello, my name is $name.\")\n",
- "println(\"I have $num_fingers fingers and $num_toes toes. That is $(num_fingers + num_toes) digits in all!!\")"
+ "println(\"I have $num_fingers fingers and $num_toes toes.)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ " println(\"That is $(num_fingers + num_toes) digits in all!!\")"
]
},
{
@@ -149,7 +169,8 @@
},
"outputs": [],
"source": [
- "string(\"How many cats \", \"are too many cats?\")"
+ "s3 = \"How many cats \";\n",
+ "s4 = \"are too many cats?\";"
]
},
{
@@ -160,14 +181,7 @@
},
"outputs": [],
"source": [
- "string(\"I don't know, but \", 10, \" are too few.\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We can also use `*` or string interpolation!"
+ "string(s3, s4)"
]
},
{
@@ -178,8 +192,14 @@
},
"outputs": [],
"source": [
- "s3 = \"How many cats \";\n",
- "s4 = \"are too many cats?\";"
+ "string(\"I don't know, but \", 10, \" are too few.\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We can also use `*` or string interpolation!"
]
},
{
@@ -210,13 +230,16 @@
"source": [
"### Exercises\n",
"\n",
- "2.1 Create a string that says \"hi\" 1000 times."
+ "#### 2.1 \n",
+ "Create a string that says \"hi\" 1000 times."
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": []
},
@@ -224,7 +247,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "2.2 Add two numbers together within a string."
+ "#### 2.2 \n",
+ "Declare two variables\n",
+ "\n",
+ "```julia\n",
+ "a = 3\n",
+ "b = 4\n",
+ "```\n",
+ "and use them to create two strings:\n",
+ "```julia\n",
+ "\"3 + 4\"\n",
+ "\"7\"\n",
+ "```"
]
},
{
diff --git a/ 3. Data structures.ipynb b/ 3. Data structures.ipynb
index de3c44a..5aefb34 100644
--- a/ 3. Data structures.ipynb
+++ b/ 3. Data structures.ipynb
@@ -84,11 +84,13 @@
"source": [
"## Dictionaries\n",
"\n",
- "If we have sets of data related to one another, we may choose to store that data in a dictionary. A good example is a contacts list, where we associate names with phone numbers.\n",
+ "If we have sets of data related to one another, we may choose to store that data in a dictionary. We can create a dictionary using the `Dict()` function, which we can initialize as an empty dictionary or one storing key, value pairs.\n",
"\n",
"Syntax:\n",
"```julia\n",
- "Dict(key1 => value1, key2 => value2, ....)```"
+ "Dict(key1 => value1, key2 => value2, ...)```\n",
+ "\n",
+ "A good example is a contacts list, where we associate names with phone numbers."
]
},
{
@@ -537,8 +539,14 @@
"source": [
"### Exercises\n",
"\n",
- "3.1 Create an array, `a_ray`, that is a 2-element 1D array of 1-element 1D arrays, each storing the number 0.\n",
- "Index into `a_ray` to add a second number, `1`, to each of the arrays it contains."
+ "#### 3.1 \n",
+ "Create an array, `a_ray`, with the following code:\n",
+ "\n",
+ "```julia\n",
+ "a_ray = [1, 2, 3]\n",
+ "```\n",
+ "\n",
+ "Add the number `4` to the end of this array and then remove it."
]
},
{
@@ -554,7 +562,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "3.2 Try to add \"Emergency\" as key to `myphonebook` with the value `911`. Try to add `911` as an integer rather than as a string. Why doesn't this work?"
+ "#### 3.2 \n",
+ "Try to add \"Emergency\" as key to `myphonebook` with the value `string(911)` with the following code\n",
+ "```julia\n",
+ "myphonebook[\"Emergency\"] = 911\n",
+ "```\n",
+ "\n",
+ "Why doesn't this work?"
]
},
{
@@ -570,7 +584,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "3.3 Create a new dictionary called `flexible_phonebook` that has Jenny's number stored as a string and Ghostbusters' number stored as an integer. "
+ "#### 3.3 \n",
+ "Create a new dictionary called `flexible_phonebook` that has Jenny's number stored as an integer and Ghostbusters' number stored as a string with the following code\n",
+ "\n",
+ "```julia\n",
+ "flexible_phonebook = Dict(\"Jenny\" => 8675309, \"Ghostbusters\" => \"555-2368\")\n",
+ "```"
]
},
{
@@ -586,7 +605,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "3.4 Add the key \"Emergency\" with the value `911` (an integer) to `flexible_phonebook`."
+ "#### 3.4 \n",
+ "Add the key \"Emergency\" with the value `911` (an integer) to `flexible_phonebook`."
]
},
{
@@ -602,7 +622,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "3.5 Why can we add an integer as a value to `flexible_phonebook` but not `myphonebook`? How could we have initialized `myphonebook` so that it would accept integers as values?"
+ "#### 3.5 \n",
+ "Why can we add an integer as a value to `flexible_phonebook` but not `myphonebook`? How could we have initialized `myphonebook` so that it would accept integers as values?"
]
},
{
diff --git a/ 4. Loops.ipynb b/ 4. Loops.ipynb
index 2778ffd..adf0682 100644
--- a/ 4. Loops.ipynb
+++ b/ 4. Loops.ipynb
@@ -104,39 +104,6 @@
"end"
]
},
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Note that we could replace `in` with either `=` or `∈`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "for n = 1:10\n",
- " println(n)\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "for n ∈ 1:10\n",
- " println(n)\n",
- "end"
- ]
- },
{
"cell_type": "markdown",
"metadata": {},
@@ -228,7 +195,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "In the next example, we embed an array comprehension in a `for` loop, generating addition tables of growing size."
+ "### Exercises\n",
+ "\n",
+ "#### 4.1 \n",
+ "Write a loop that prints the squares of integers between 1 and 100."
]
},
{
@@ -238,27 +208,18 @@
"collapsed": true
},
"outputs": [],
- "source": [
- "for n in 1:10\n",
- " A = [i + j for i in 1:n, j in 1:n]\n",
- " display(A)\n",
- "end"
- ]
+ "source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Exercises\n",
- "\n",
- "4.1 Create a dictionary, `squares`, that has integer keys from 1 to 100. The value associated with each key is the square of that key. Store values associated with even keys as integers and values associated with odd keys as strings. For example,\n",
+ "#### 4.2 \n",
+ "Add to the code above a bit to create a dictionary, `squares` that holds integers and their squares as key, value pairs such that\n",
"\n",
"```julia\n",
"squares[10] == 100\n",
- "squares[11] == \"121\"\n",
- "```\n",
- "\n",
- "(You don't need conditionals to do this!)"
+ "```"
]
},
{
@@ -276,7 +237,8 @@
"collapsed": true
},
"source": [
- "4.2 Use the `fill` function to create a `10x10` matrix of `0`'s. Populate the first ten entries of the matrix with the index of that entry. Does Julia use column-major or row-major order? (Is the \"second\" element in the first column and second row, or is it in the first row and second column?)"
+ "#### 4.3 \n",
+ "Use an array comprehension to create an an array that stores the squares of all integers between 1 and 100."
]
},
{
diff --git a/ 5. Conditionals.ipynb b/ 5. Conditionals.ipynb
index 4d6368f..4d853f3 100644
--- a/ 5. Conditionals.ipynb
+++ b/ 5. Conditionals.ipynb
@@ -171,7 +171,8 @@
"source": [
"### Exercises\n",
"\n",
- "5.1 Rewrite the FizzBuzz test without using the `elseif` keyword."
+ "#### 5.1 \n",
+ "Write a conditional statement that prints a number if the number is even."
]
},
{
@@ -187,7 +188,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "5.2 Rewrite the FizzBuzz test using ternary operators."
+ "#### 5.2 \n",
+ "Rewrite the code above using a ternary operator."
]
},
{
diff --git a/ 6. Functions.ipynb b/ 6. Functions.ipynb
index 7662884..016ae6a 100644
--- a/ 6. Functions.ipynb
+++ b/ 6. Functions.ipynb
@@ -10,7 +10,7 @@
"1. How to declare a function\n",
"2. Duck-typing in Julia\n",
"3. Mutating vs. non-mutating functions\n",
- "4. Broadcasting"
+ "4. Some higher order functions"
]
},
{
@@ -357,14 +357,20 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "## Broadcasting\n",
+ "## Some higher order functions\n",
"\n",
- "By placing a `.` between any function name and its argument list,
\n",
- "we tell that function to broadcast over the elements of the input objects.
\n",
+ "### map\n",
"\n",
- "Let's look at the difference in behavior between `f()` and `f.()`.
\n",
+ "`map` is a \"higher-order\" function in Julia that *takes a function* as one of its input arguments. \n",
+ "`map` then applies that function to every element of the data structure you pass it. For example, executing\n",
"\n",
- "First we'll define a new matrix `A` that will make the difference easier to illustrate."
+ "```julia\n",
+ "map(f, [1, 2, 3])\n",
+ "```\n",
+ "will give you an output array where the function `f` has been applied to all elements of `[1, 2, 3]`\n",
+ "```julia\n",
+ "[f(1), f(2), f(3)]\n",
+ "```"
]
},
{
@@ -375,7 +381,16 @@
},
"outputs": [],
"source": [
- "A = [i + 3*j for j in 0:2, i in 1:3]"
+ "map(f, [1, 2, 3])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Here we've squared all the elements of the vector `[1, 2, 3]`, rather than squaring the vector `[1, 2, 3]`.\n",
+ "\n",
+ "To do this, we could have passed to `map` an anonymous function rather than a named function, such as"
]
},
{
@@ -386,19 +401,37 @@
},
"outputs": [],
"source": [
- "f(A)"
+ "x -> x^3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "As before we see that for a matrix, `A`,\n",
- "```\n",
- "f(A) = A^2 = A * A\n",
- "``` \n",
+ "via"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "map(x -> x^3, [1, 2, 3])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "and now we've cubed all the elements of `[1, 2, 3]`!"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### broadcast\n",
"\n",
- "`f.(A)` on the other hand will return an object that holds the square of `A[i, j]` at its corresponding entry."
+ "`broadcast` is another higher-order function like `map`. `broadcast` is a generalization of `map`, so it can do every thing `map` can do and more. The syntax for calling `broadcast` is the same as for calling `map`"
]
},
{
@@ -409,7 +442,24 @@
},
"outputs": [],
"source": [
- "B = f.(A)"
+ "broadcast(f, [1, 2, 3])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "and again, we've applied `f` (squared) to all the elements of `[1, 2, 3]` - this time by \"broadcasting\" `f`!\n",
+ "\n",
+ "Some syntactic sugar for calling `broadcast` is to place a `.` between the name of the function you want to `broadcast` and its input arguments. For example,\n",
+ "\n",
+ "```julia\n",
+ "broadcast(f, [1, 2, 3])\n",
+ "```\n",
+ "is the same as\n",
+ "```julia\n",
+ "f.([1, 2, 3])\n",
+ "```"
]
},
{
@@ -420,7 +470,34 @@
},
"outputs": [],
"source": [
- "A[2, 2]"
+ "f.([1, 2, 3])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Notice again how different this is from calling \n",
+ "```julia\n",
+ "f([1, 2, 3])\n",
+ "```\n",
+ "We can square every element of a vector, but we can't square a vector!"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "To drive home the point, let's look at the difference between\n",
+ "\n",
+ "```julia\n",
+ "f(A)\n",
+ "```\n",
+ "and\n",
+ "```julia\n",
+ "f.(A)\n",
+ "```\n",
+ "for a matrix `A`:"
]
},
{
@@ -431,7 +508,7 @@
},
"outputs": [],
"source": [
- "A[2, 2]^2"
+ "A = [i + 3*j for j in 0:2, i in 1:3]"
]
},
{
@@ -442,14 +519,19 @@
},
"outputs": [],
"source": [
- "B[2, 2]"
+ "f(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "This means that, for a vector `v`, `f.(v)` is defined, though `f(v)` is not:"
+ "As before we see that for a matrix, `A`,\n",
+ "```\n",
+ "f(A) = A^2 = A * A\n",
+ "``` \n",
+ "\n",
+ "On the other hand,"
]
},
{
@@ -460,18 +542,16 @@
},
"outputs": [],
"source": [
- "v = [1, 2, 3]"
+ "B = f.(A)"
]
},
{
- "cell_type": "code",
- "execution_count": null,
+ "cell_type": "markdown",
"metadata": {
"collapsed": true
},
- "outputs": [],
"source": [
- "f.(v)"
+ "contains the squares of all the entries of `A`."
]
},
{
@@ -482,9 +562,8 @@
"source": [
"### Exercises\n",
"\n",
- "6.1 Instead of broadcasting function `f` over vector `v`, we could have simply executed `v .^ 2`.\n",
- "\n",
- "Without declaring a new function, add 1 to every element of a `3x3` matrix of `0`'s."
+ "#### 6.1 \n",
+ "Write a function that increments its input by 1."
]
},
{
@@ -498,11 +577,10 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"source": [
- "6.2 Instead of broadcasting function `f` over vector `v` with the dot syntax, apply `f` to all elements of `v` using the `map` function."
+ "#### 6.2 \n",
+ "Use `map` or `broadcast` to increment every element of matrix `A` by `1`."
]
},
{
@@ -520,15 +598,8 @@
"collapsed": true
},
"source": [
- "6.3 A Caesar cipher shifts each letter a certain number of places down the alphabet. A shift of 1 maps \"A\" to \"B\". Write a function called `caesar` that takes an input string and a shift and returns a decrypted string such that you'll get\n",
- "\n",
- "```julia\n",
- "caesar(\"abc\", 1)\n",
- "\"bcd\"\n",
- "\n",
- "caesar(\"hello\", 4)\n",
- "\"lipps\"\n",
- "```"
+ "#### 6.3 \n",
+ "Use the broadcast dot syntax to increment every element of matrix `A` by `1`."
]
},
{
diff --git a/ 7. Packages.ipynb b/ 7. Packages.ipynb
index 8f70660..74696ab 100644
--- a/ 7. Packages.ipynb
+++ b/ 7. Packages.ipynb
@@ -159,7 +159,25 @@
"source": [
"### Exercises\n",
"\n",
- "7.1 Use the Primes package (source code at https://github.com/JuliaMath/Primes.jl) to help you find the largest prime number less than 1,000,000."
+ "#### 7.1 \n",
+ "Load the Primes package (source code at https://github.com/JuliaMath/Primes.jl)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### 7.2 \n",
+ "Verify that you can now use the function `primes` to grab all prime numbers under 1,000,000."
]
},
{
diff --git a/ 8. Plotting.ipynb b/ 8. Plotting.ipynb
index 6c35f18..e2633d3 100644
--- a/ 8. Plotting.ipynb
+++ b/ 8. Plotting.ipynb
@@ -30,51 +30,50 @@
"source": [
"One of the advantages to `Plots.jl` is that it allows you to seamlessly change backends. In this notebook, we'll try out the `gr()` and `plotlyjs()` backends.
\n",
"\n",
- "First, let's generate some artificial data to plot!"
+ "In the name of scientific inquiry, let's use this notebook to examine the relationship between the global temperature and the number of pirates between roughly 1860 and 2000."
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "x = -3:0.1:3\n",
- "f(x) = x^2\n",
- "\n",
- "y = f.(x)"
+ "globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8]\n",
+ "numpirates = [45000, 20000, 15000, 5000, 400, 17]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "**Now let's load the GR backend**"
+ "**To get plotting, let's load the GR backend**"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"gr()"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "and now we can use commands like `plot` and `scatter` to generate plots."
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "plot(x, y, label=\"line\") \n",
- "scatter!(x, y, label=\"points\") "
+ "plot(numpirates, globaltemperatures, label=\"line\") \n",
+ "scatter!(numpirates, globaltemperatures, label=\"points\") "
]
},
{
@@ -83,123 +82,118 @@
"source": [
"The `!` at the end of the `scatter!` function name makes `scatter!` a mutating function, indicating that the scattered points will be added onto the pre-existing plot.\n",
"\n",
- "In contrast, see what happens when you replace `scatter!` in the above with the non-mutating function `scatter`."
+ "In contrast, see what happens when you replace `scatter!` in the above with the non-mutating function `scatter`.\n",
+ "\n",
+ "Next, let's update this plot with the `xlabel!`, `ylabel!`, and `title!` commands to add more information to our plot."
]
},
{
- "cell_type": "markdown",
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {},
+ "outputs": [],
"source": [
- "**Without changing syntax, we can try this with the `plotlyjs()` backend**"
+ "xlabel!(\"Number of Pirates [Approximate]\")\n",
+ "ylabel!(\"Global Temperature (C)\")\n",
+ "title!(\"Influence of pirate population on global warming\")"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
- "plotlyjs()"
+ "This still doesn't look quite right. The number of pirates has decreased since 1860, so reading the plot from left to right is like looking backwards in time rather than forwards. Let's flip the x axis to better see how pirate populations have caused global temperatures to change over time!"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "plot(x, y, label=\"line\") \n",
- "scatter!(x, y, label=\"points\") "
+ "xflip!()"
]
},
{
"cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"source": [
- "And notice how this second plot differs from the first!"
+ "And there we have it!\n",
+ "\n",
+ "**Without changing syntax, we can create this plot with the `plotlyjs()` backend**"
]
},
{
- "cell_type": "markdown",
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {},
+ "outputs": [],
"source": [
- "## Getting slightly fancier\n",
- "\n",
- "The syntax for adding titles and labels to your plots is pretty straightforward.\n",
- "\n",
- "This time, in the name of scientific inquiry, let's examine the relationship between the global temperature and the number of pirates between roughly 1860 and 2000."
+ "plotlyjs()"
]
},
{
"cell_type": "code",
"execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "plot(numpirates, globaltemperatures, label=\"line\") \n",
+ "scatter!(numpirates, globaltemperatures, label=\"points\") \n",
+ "xlabel!(\"Number of Pirates [Approximate]\")\n",
+ "ylabel!(\"Global Temperature (C)\")\n",
+ "title!(\"Influence of pirate population on global warming\")\n",
+ "xflip!()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
"metadata": {
"collapsed": true
},
- "outputs": [],
"source": [
- "globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8]\n",
- "numpirates = [45000, 20000, 15000, 5000, 400, 17]"
+ "And notice how this second plot differs from the first!"
]
},
{
- "cell_type": "code",
- "execution_count": null,
+ "cell_type": "markdown",
"metadata": {
"collapsed": true
},
- "outputs": [],
"source": [
- "# First, plot the data\n",
- "plot(numpirates, globaltemperatures, legend=false)\n",
- "scatter!(numpirates, globaltemperatures, legend=false)\n",
+ "### Exercises\n",
"\n",
- "# Add titles and labels\n",
- "xlabel!(\"Number of Pirates [Approximate]\")\n",
- "ylabel!(\"Global Temperature (C)\")\n",
- "title!(\"Influence of pirate population on global warming\")"
+ "#### 8.1 \n",
+ "Given\n",
+ "```julia\n",
+ "x = -3:.1:3\n",
+ "```\n",
+ "plot y vs. x for $y = x^2$."
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "# First, plot the data\n",
- "plot(numpirates, globaltemperatures, legend=false)\n",
- "scatter!(numpirates, globaltemperatures, legend=false)\n",
- "\n",
- "# This reverses x axis we can see how the temperature changes as we move forward in time, from 1860 to 2000\n",
- "xflip!()\n",
- "\n",
- "# Add titles and labels\n",
- "xlabel!(\"Number of Pirates [Approximate]\")\n",
- "ylabel!(\"Global Temperature (C)\")\n",
- "title!(\"Influence of pirate population on global warming\")"
+ "x = -10:10\n",
+ "f = x -> x^2\n",
+ "plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "To create a plot with subplots, we just name each of the individual subplots and then wrap those names, along with a layout specification, in another `plot` call."
+ "#### 8.2 \n",
+ "Execute the following code"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"p1 = plot(x, x)\n",
@@ -211,13 +205,9 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"source": [
- "### Exercises\n",
- "\n",
- "8.1 Plot y vs x for `y = x^2` using the PyPlot backend."
+ "and then create a $4x1$ plot that uses `p1`, `p2`, `p3`, and `p4` as subplots."
]
},
{
diff --git a/ 9. Multiple dispatch.ipynb b/ 9. Multiple dispatch.ipynb
index cbf6eb8..c3d042d 100644
--- a/ 9. Multiple dispatch.ipynb
+++ b/ 9. Multiple dispatch.ipynb
@@ -262,11 +262,15 @@
"\n",
"#### 9.1\n",
"\n",
- "Add a method for `+` that applies a Caesar cipher to an input string (as in notebook 6), such that\n",
- "\n",
+ "The `*` operator will concatenate strings together but not characters. For example, we get `\"aa\"` from\n",
+ "```julia\n",
+ "\"a\"^2\n",
+ "```\n",
+ "but an error when we run\n",
"```julia\n",
- "\"hello\" + 4 == \"lipps\"\n",
- "```"
+ "'a'^2\n",
+ "```\n",
+ "Add a method to the `*` operator so that it concatenates characters to form a string."
]
},
{
@@ -284,9 +288,7 @@
"source": [
"#### 9.2\n",
"\n",
- "Check that you've properly extended `+` by shifting the following string back by three letters:\n",
- "\n",
- "\"Gr#qrw#phggoh#lq#wkh#diidluv#ri#gudjrqv#iru#|rx#duh#fuxqfk|#dqg#wdvwh#jrrg#zlwk#nhwfkxs1\""
+ "Check that the method being dispatched when you execute `a^2` is the one you wrote."
]
},
{
diff --git a/10. Basic linear algebra.ipynb b/10. Basic linear algebra.ipynb
index 317b4a5..c577cb8 100644
--- a/10. Basic linear algebra.ipynb
+++ b/10. Basic linear algebra.ipynb
@@ -71,7 +71,7 @@
"metadata": {},
"source": [
"#### Transposition\n",
- "As in other languages `A'` is the conjugate transpose whereas `A.'` is just the transpose"
+ "As in other languages `A'` is the conjugate transpose"
]
},
{
@@ -82,15 +82,14 @@
},
"outputs": [],
"source": [
- "Asym = A + A'"
+ "A'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "#### Transposed multiplication\n",
- "Julia allows us to write this without *"
+ "and we can get the transpose with"
]
},
{
@@ -101,15 +100,15 @@
},
"outputs": [],
"source": [
- "Apd = A'A"
+ "transpose(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "#### Solving linear systems \n",
- "The problem $Ax=b$ for square $A$ is solved by the \\ function."
+ "#### Transposed multiplication\n",
+ "Julia allows us to write this without *"
]
},
{
@@ -120,17 +119,15 @@
},
"outputs": [],
"source": [
- "A\\b"
+ "A'A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "#### Overdetermined systems\n",
- "When our matrix is tall (the number of rows is greater than the number of columns), we have an overdetermined linear system. \n",
- "\n",
- "In this case \\ function calculates the least squares solution."
+ "#### Solving linear systems \n",
+ "The problem $Ax=b$ for ***square*** $A$ is solved by the \\ function."
]
},
{
@@ -141,18 +138,14 @@
},
"outputs": [],
"source": [
- "Atall = rand(3, 2)\n",
- "display(Atall)\n",
- "Atall\\b"
+ "A\\b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "The \\ function also works for rank deficient least squares problems. In this case, the least squares solution is not unique and Julia returns the solution with the smallest norm.\n",
- "\n",
- "To create a rank deficient least squares problem, let's create a rank deficient matrix with linearly dependent columns."
+ "`A\\b` gives us the *least squares solution* if we have an overdetermined linear system (a \"tall\" matrix)"
]
},
{
@@ -163,8 +156,7 @@
},
"outputs": [],
"source": [
- "v = randn(3)\n",
- "rankdef = [v v]"
+ "Atall = rand(3, 2)"
]
},
{
@@ -175,17 +167,14 @@
},
"outputs": [],
"source": [
- "rankdef\\b"
+ "Atall\\b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "#### Underdetermined systems\n",
- "when A is short (the number of columns is greater than the number of rows), we have an underdetermined linear system.\n",
- "\n",
- "In this case the \\ function returns the minimum norm solution."
+ "and the *minimum norm least squares solution* if we have a rank-deficient least squares problem"
]
},
{
@@ -196,52 +185,15 @@
},
"outputs": [],
"source": [
- "Ashort = rand(2, 3)\n",
- "display(Ashort)\n",
- "Ashort\\b[1:2]"
+ "v = rand(3)\n",
+ "[v, v]\\b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Exercises\n",
- "\n",
- "10.1 Use `circshift` to get a matrix with the columns of A cyclically shifted to the right by 3 columns.\n",
- "\n",
- "Starting with \n",
- "\n",
- "```\n",
- "A = [\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " 1 2 3 4 5 6 7 8 9 10\n",
- " ]\n",
- "```\n",
- "\n",
- "you want to get\n",
- "\n",
- "```\n",
- "A = [\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " 7 8 9 10 1 2 3 4 5 6\n",
- " ]\n",
- "```"
+ "Julia also gives us the minimum norm solution when we have an underdetermined solution (a \"short\" matrix)"
]
},
{
@@ -251,13 +203,30 @@
"collapsed": true
},
"outputs": [],
- "source": []
+ "source": [
+ "bshort = rand(2)\n",
+ "Ashort = rand(2, 3)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "Ashort\\bshort"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "10.2 Take the outer product of a vector `v` with itself."
+ "### Exercises\n",
+ "\n",
+ "#### 10.1 \n",
+ "Take the outer product of a vector `v` with itself."
]
},
{
@@ -273,7 +242,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "10.3 Take the inner product of a vector v with itself."
+ "#### 10.2 \n",
+ "Take the inner product of a vector v with itself."
]
},
{
diff --git a/11. Factorizations and other fun.ipynb b/11. Factorizations and other fun.ipynb
index a07962b..cb97b3d 100644
--- a/11. Factorizations and other fun.ipynb
+++ b/11. Factorizations and other fun.ipynb
@@ -5,8 +5,7 @@
"metadata": {},
"source": [
"# Factorizations and other fun\n",
- "Author: Andreas Noack Jensen (MIT) (http://www.econ.ku.dk/phdstudent/noack/)\n",
- "(with edits from Jane Herriman)\n",
+ "Based on work by Andreas Noack\n",
"\n",
"## Outline\n",
" - Factorizations\n",
@@ -18,190 +17,59 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Let's start by generating a linear system of the form\n",
- "\n",
- "`Ax = b`"
+ "Before we get started, let's set up a linear system"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "A = randn(3,3)"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
- "x = fill(1.0, (3))\n",
- "b = A*x"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Factorization\n",
- "The `\\` function hides how the problem is actually solved. \n",
- "\n",
- "Depending on the dimensions of `A`, different methods are chosen to solve the problem\n",
- "\n",
- "```\n",
- "Ax = b\n",
- "```\n",
- "\n",
- "An intermediate step in the solution is to calculate a factorization of the matrix `A`. \n",
- "\n",
- "Basically, a factorization of `A` is a way of expressing `A` as a product of triangular, unitary and permutation matrices. \n",
- "\n",
- "Julia stores these factorizations using a `Factorization` abstract type and several composite subtypes.\n",
- "\n",
- "A `Factorization` object should therefore be thought of as a representation of the matrix `A`.\n",
- "\n",
- "#### LU"
+ "A = rand(3, 3)\n",
+ "x = fill(1, (3))\n",
+ "b = A * x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "When `A` is square, a linear system is solved by factorizing the matrix `A` via\n",
+ "## Factorization\n",
"\n",
- "```\n",
+ "#### LU factorizations\n",
+ "In Julia we can perform an LU factorization\n",
+ "```julia\n",
"PA=LU\n",
"``` \n",
- "\n",
- "where `P` is a permutation matrix, `L` is lower triangular unit diagonal and `U` is upper triangular. \n",
+ "where `P` is a permutation matrix, `L` is lower triangular unit diagonal and `U` is upper triangular, using `lufact`.\n",
"\n",
"Julia allows computing the LU factorization and defines a composite factorization type for storing it."
]
},
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We can perform an LU factorization on `A` via either `lu(A)` or `lufact(A)`.\n",
- "\n",
- "The function `lu` returns matrices l and u and permutation vector p."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "l,u,p = lu(A)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Pivoting is on by default, so we can't assume that A == LU.\n",
- "\n",
- "Let's show this by looking at the norm of `LU - A`:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "norm(l*u - A)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "This shows that we want to account for pivoting!\n",
- "\n",
- "You can think of `A[p,:]` as the syntax for `PA`, or the product of our permutation matrix and matrix `A`"
- ]
- },
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "norm(l*u - A[p,:])"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": [
- "On the other hand, we could turn pivoting off for LU factorizations using the argument `Val{false}` in Julia 0.6 or `Val(false)` in later versions."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
- "l,u,p = lu(A, Val{false})"
+ "Alu = lufact(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "When pivoting is off, `LU = A`"
+ "Note that Julia stores the LU factorization ina composite factorization type"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "norm(l*u - A)"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": [
- "A second way to perform an LU factorization is with the function `lufact`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
- "Alu = lufact(A)"
+ "typeof(Alu)"
]
},
{
@@ -214,9 +82,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"Alu[:P]"
@@ -225,9 +91,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"Alu[:L]"
@@ -236,9 +100,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"Alu[:U]"
@@ -248,39 +110,24 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "We can compute the solution of $Ax=b$ from the factorization object"
+ "Julia can dispatch methods on factorization objects.\n",
+ "\n",
+ "For example, we can solve the linear system using either the original matrix or the factorization object."
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# PA = LU\n",
- "# A = P'LU\n",
- "# P'LUx = b\n",
- "# LUx = Pb\n",
- "# Ux = L\\Pb\n",
- "# x = U\\L\\Pb\n",
- "Alu[:U]\\(Alu[:L]\\(Alu[:P]b))"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
+ "outputs": [],
"source": [
- "*More importantly* we can dispatch on the `LU` type and simply solve the system by"
+ "A\\b"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"Alu\\b"
@@ -290,91 +137,46 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "This could be useful if the same left-hand-side is used for several right-hand-sides. The factorization can also be used for calculating the determinant because $\\det(A)=\\det(PLU)=\\det(P)\\det(U)=\\pm \\prod u_{ii}$ because $U$ is triangular and the sign is determined from $\\det(P)$."
+ "Similarly, we can calculate the determinant of `A` using either `A` or the factorization object"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "det(A)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "det(Alu)"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": [
- "#### QR\n",
- "When `A` is tall, "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
- "Atall = randn(3, 2)"
+ "det(A) ≈ det(Alu)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Julia computes the least squares solution $\\hat{x}$ that minimizes $\\|Ax-b\\|_2$. \n",
- "\n",
- "This can be done by factorizing \n",
+ "#### QR factorizations\n",
"\n",
+ "In Julia we can perform a QR factorization\n",
"```\n",
"A=QR\n",
"``` \n",
"\n",
- "where $Q$ is unitary/orthogonal and $R=\\left(\\begin{smallmatrix}R_0\\\\0\\end{smallmatrix}\\right)$ and $R_0$ is upper triangular. \n",
- "\n",
- "With the QR factorization the minimum norm can be expressed as\n",
- "\n",
- "\\begin{equation*}\n",
- "\\|Ax-b\\|=\\|QRx-b\\|=\\|Q(Rx-Q'b)\\|=\\|Rx-Q'b\\|=\\left\\|\\begin{pmatrix}R_0x-Q_0'b\\\\Q_1'b\\end{pmatrix}\\right\\|=\\|R_0x-Q_0'b\\|+\\|Q_1'b\\|\n",
- "\\end{equation*}\n",
- "\n",
- "and the problem therefore reduces to solving the square problem $R_0x=Q_0'b$ for $x$.\n",
- "\n",
- "We can QR factorize `Atall` via"
+ "where `Q` is unitary/orthogonal and `R` is upper triangular, using `qrfact`. "
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "Aqr = qrfact(Atall)"
+ "Aqr = qrfact(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Another feature of the QR factorizations is the `Q` types for storing the unitary matrices $Q$. They can be extracted from the `QR` types by indexing"
+ "The matrices `Q` and `R` can be extracted from the QR factorization object via"
]
},
{
@@ -388,13 +190,6 @@
"Aqr[:Q]"
]
},
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Similarly, the upper triangular matrix $R$ can be extracted by indexing"
- ]
- },
{
"cell_type": "code",
"execution_count": null,
@@ -410,10 +205,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "In this case, R is stored as a 2x2 matrix rather than as a 3x2 because the last row of R is filled with 0's.
\n",
+ "In this case, R is stored as a 2x2 matrix rather than as a 3x2 because the last row of R is filled with 0's.
\n",
"\n",
+ "Even though the matrix `Aqr[:Q]` is printed as a $3\\times 3$ matrix in the factorization object, in practice it can represent the thin version as well (Here, a $3\\times2$ matrix.\n",
"\n",
- "Even though the matrix `Aqr[:Q]` is printed as a $3\\times 3$ matrix in the factorization object, in practice it can represent the thin version as well. Hence"
+ "Julia infers if we want to work with the thin version"
]
},
{
@@ -431,9 +227,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "works, and represents multiplying a $3 x 2$ matrix by a 2-element vector.\n",
- "\n",
- "Similarly,"
+ "or the full version of Q"
]
},
{
@@ -447,141 +241,6 @@
"Aqr[:Q]*ones(3)"
]
},
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "works, representing the multiplication of a $3x3$ matrix and a 3-element vector.\n",
- "\n",
- "However, this does not mean that we can multiply `Q` by vectors of arbitrary length."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Aqr[:Q]*ones(4)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The matrix has a compact internal representation, so indexing is only meaningful if you know how the factorization stores data."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Aqr[:Q][1]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The QRCompactWY object `\\` has a method for the QR and the least squares problem is therefore solved with"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Aqr\\b"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "If we had instead written simply"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Atall\\b"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "rather than QR factorizing the matrix `Atall` first, Julia would have defaulted to QR factorizing *with* pivoting."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Defaulting to using pivoting with a QR factorization allows Julia to handle rank deficient problems. \n",
- "\n",
- "We can explicitly choose to use pivoting during the QR factorization (of a singular matrix, for example) with the keyword `Val{true}`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "v = randn(3)\n",
- "# Taking the outer product of a vector with itself gives a singular matrix\n",
- "singmatrix = v * v'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Aqrp = qrfact(singmatrix,Val{true})"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Notice that the type of the resulting Factorization object is different now. \n",
- "\n",
- "\n",
- "`\\` also has a method for `QRPivoted` and the rank deficient problem is therefore computed"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Aqrp\\b"
- ]
- },
{
"cell_type": "markdown",
"metadata": {},
@@ -593,7 +252,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "The results from eigendecompositions and singular value decompositions are also stored in `Factorization` types. This also includes Hessenberg and Schur factorizations.\n",
+ "The results from eigendecompositions, singular value decompositions, Hessenberg factorizations, and Schur decompositions are all stored in `Factorization` types.\n",
"\n",
"The eigendecomposition can be computed"
]
@@ -654,78 +313,15 @@
},
"outputs": [],
"source": [
- "inv(AsymEig)*Asym"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Julia also has an `eig` function which returns a tuple with the values and the vectors"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "eig(Asym)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We do not recommend this version.\n",
- "\n",
- "The `svdfact` function computes the singular value decomposition"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Asvd = svdfact(A[:,1:2])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "and again `\\` has a method for the type enabling least squares by SVD"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Asvd\\b"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "There are special functions for providing values only: `eigvals` and `svdvals`."
+ "inv(AsymEig)*AsymEig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Special matrix Structures\n",
- "The structure of matrices is very important in linear algebra. This structure can be made explicit in Julia through composite types. Examples are `Diagonal`, `Triangular`, `Symmetric`, `Hermitian`, `Tridiagonal` and `SymTridiagonal`. Specialized methods are written for the special matrix types to take advantage of their structure. Below some examples are shown"
+ "## Special matrix Structures\n",
+ "Matrix structure is very important in linear algebra. To see *how* important it is, let's work with a larger linear system"
]
},
{
@@ -736,83 +332,32 @@
},
"outputs": [],
"source": [
- "A"
+ "n = 1000\n",
+ "A = randn(n,n);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Creating a diagonal matrix:"
+ "Julia can often infer special matrix structure"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Diagonal(diag(A))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Diagonal(A)"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": [
- "Creating a lower triangular matrix:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
- "LowerTriangular(tril(A))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "LowerTriangular(A)"
+ "Asym = A + A'\n",
+ "issymmetric(Asym)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Creating symmetric matrices:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Symmetric(Asym)"
+ "but sometimes floating point error might get in the way."
]
},
{
@@ -823,21 +368,8 @@
},
"outputs": [],
"source": [
- "SymTridiagonal(diag(Asym),diag(Asym,1))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "When it is known that a matrix is e.g. triangular or symmetric Julia might be able to solve a problem faster by converting the matrix to a special matrix. \n",
- "\n",
- "For some of the procedures, Julia checks if the input matrix is triangular or symmetric and converts the matrix if such a structure is detected. \n",
- "\n",
- "It should be noted that `Symmetric`, `Hermitian` and `Triangular` do not copy the input matrix.\n",
- "\n",
- "#### Symmetric eigenproblem\n",
- "Whether or not Julia is able to detect if a matrix is symmetric/Hermitian can have a big influence on how fast an eigenvalue problem is solved. "
+ "Asym_noisy = copy(Asym)\n",
+ "Asym_noisy[1,2] += 5eps()"
]
},
{
@@ -848,78 +380,36 @@
},
"outputs": [],
"source": [
- "n = 1000;\n",
- "A = randn(n,n);"
+ "issymmetric(Asym_noisy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Let's use `A` to generate a symmetric matrix, `Asym`"
+ "Luckily we can declare structure explicitly with, for example, `Diagonal`, `Triangular`, `Symmetric`, `Hermitian`, `Tridiagonal` and `SymTridiagonal`."
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "Asym = A + A';"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": [
- "Now let's create a noisy version of Asym to simulate a symmetric matrix with floating point errors."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
- "Asym_noisy = copy(Asym); Asym_noisy[1,2] += 5eps();"
+ "Asym_explicit = Symmetric(Asym_noisy);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Can Julia determine that both `Asym` and `Asym_noisy` are symmetric matrices?"
+ "Let's compare how long it takes Julia to compute the eigenvalues of `Asym`, `Asym_noisy`, and `Asym_explicit`"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "println(\"Is Asym symmetric? \", issymmetric(Asym))\n",
- "println(\"Is Asym_noisy symmetric? \", issymmetric(Asym_noisy))"
- ]
- },
- {
- "cell_type": "markdown",
"metadata": {},
- "source": [
- "Now let's look at how the noise in `Asym_noisy` impacts the time to perform an eigendecomposition"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
"outputs": [],
"source": [
"@time eigvals(Asym);"
@@ -928,39 +418,26 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"@time eigvals(Asym_noisy);"
]
},
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Luckily we can provide explicit information about matrix structure to Julia.\n",
- "\n",
- "In this example, we do so with the `Symmetric` keyword"
- ]
- },
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "@time eigvals(Symmetric(Asym_noisy));"
+ "@time eigvals(Asym_explicit);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "And so we've made our calculations substantially more efficient :)"
+ "In this example, using `Symmetric()` on `Asym_noisy` made our calculations about `5x` more efficient :)"
]
},
{
@@ -989,27 +466,9 @@
"metadata": {},
"source": [
"### Generic linear algebra\n",
- "The usual way of adding support for numerical linear algebra is by wrapping BLAS and LAPACK subroutines. For matrices with elements of `Float32`, `Float64`, `Complex{Float32}` or `Complex{Float64}` this is also what Julia does. For a long time Julia has also had support for multiplicaton of general element types. Hence, when multiplying integer matrices, the output is also an integer matrix"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "rand(1:10,3,3)*rand(1:10,3,3)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Recently, more generic linear algebra methods has been added and Julia now supports generic `LU` and `QR` factorizations. Generic eigenvalue and SVD methods have been written more recently (some in external packages).\n",
+ "The usual way of adding support for numerical linear algebra is by wrapping BLAS and LAPACK subroutines. For matrices with elements of `Float32`, `Float64`, `Complex{Float32}` or `Complex{Float64}` this is also what Julia does.\n",
"\n",
- "In general, the `LU` factorization can be computed whenever the matrix element type is closed under the operations `+`, `-`, `*` and `\\`. Of course the matrix also has to have full rank. The generic `LU` method in Julia applies pivoting and therefore the element type also has to support `<` and `abs`. Hence it is possible to solve systems of equations of e.g. rational numbers which the following examples show."
+ "However, Julia also supports generic linear algebra, allowing you to, for example, work with matrices and vectors of rational numbers."
]
},
{
@@ -1041,35 +500,29 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "Ar = convert(Matrix{Rational{BigInt}}, rand(1:10,3,3))/10"
+ "Arational = convert(Matrix{Rational{BigInt}}, rand(1:10,3,3))/10"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
"x = fill(1, (3))\n",
- "b = Ar*x"
+ "b = Arational*x"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "Ar\\b"
+ "Arational\\b"
]
},
{
@@ -1111,7 +564,8 @@
"source": [
"### Exercises\n",
"\n",
- "11.1 What are the eigenvalues of matrix A?\n",
+ "#### 11.1\n",
+ "What are the eigenvalues of matrix A?\n",
"\n",
"```\n",
"A =\n",
@@ -1138,7 +592,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "11.2 Create a diagonal matrix from the eigenvalues of A."
+ "#### 11.2 \n",
+ "Create a diagonal matrix from the eigenvalues of A."
]
},
{
@@ -1154,7 +609,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "11.3 Perform a Hessenberg factorization on matrix A. Verify that `A = QHQ'`."
+ "#### 11.3 \n",
+ "Create a lower triangular matrix from matrix `A` using `LowerTriangular`"
]
},
{
diff --git a/12. Julia is fast.ipynb b/12. Julia is fast.ipynb
index 193f869..048c01b 100644
--- a/12. Julia is fast.ipynb
+++ b/12. Julia is fast.ipynb
@@ -52,6 +52,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -63,6 +64,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -129,6 +131,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -146,10 +149,8 @@
"\n",
"const Clib = tempname() # make a temporary file\n",
"\n",
- "\n",
"# compile to a shared library by piping C_code to gcc\n",
"# (works only if you have gcc installed):\n",
- "\n",
"open(`gcc -fPIC -O3 -msse3 -xc -shared -o $(Clib * \".\" * Libdl.dlext) -`, \"w\") do f\n",
" print(f, C_code) \n",
"end\n",
@@ -162,6 +163,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -173,6 +175,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -183,7 +186,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"≈ # alias for the `isapprox` function"
@@ -192,7 +197,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"?isapprox"
@@ -209,6 +216,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -220,6 +228,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -230,7 +239,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"d = Dict() # a \"dictionary\", i.e. an associative array\n",
@@ -238,30 +249,6 @@
"d"
]
},
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "using Plots\n",
- "gr()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "t = c_bench.times / 1e6 # times in milliseconds\n",
- "m, σ = minimum(t), std(t)\n",
- "\n",
- "histogram(t, bins=500,\n",
- " xlim=(m - 0.01, m + σ),\n",
- " xlabel=\"milliseconds\", ylabel=\"count\", label=\"\")"
- ]
- },
{
"cell_type": "markdown",
"metadata": {},
@@ -303,6 +290,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -320,6 +308,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -331,6 +320,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -342,6 +332,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -353,6 +344,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -390,6 +382,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -404,6 +397,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -415,6 +409,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -425,7 +420,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"d[\"Python numpy\"] = minimum(py_numpy_bench.times) / 1e6\n",
@@ -443,6 +440,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -462,6 +460,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -473,6 +472,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -484,6 +484,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -494,7 +495,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"d[\"Python hand-written\"] = minimum(py_hand.times) / 1e6\n",
@@ -514,6 +517,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -525,6 +529,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -535,7 +540,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"d[\"Julia built-in\"] = minimum(j_bench.times) / 1e6\n",
@@ -553,6 +560,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": false
},
"outputs": [],
@@ -570,6 +578,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -580,7 +589,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"d[\"Julia hand-written\"] = minimum(j_bench_hand.times) / 1e6\n",
@@ -598,20 +609,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
- "scrolled": true
+ "collapsed": true
},
"outputs": [],
- "source": [
- "for (key, value) in sort(collect(d))\n",
- " println(rpad(key, 20, \".\"), lpad(round(value, 1), 8, \".\"))\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
"source": [
"for (key, value) in sort(collect(d), by=x->x[2])\n",
" println(rpad(key, 20, \".\"), lpad(round(value, 2), 10, \".\"))\n",
diff --git a/Exercise_solutions.ipynb b/Exercise_solutions.ipynb
index 96cca08..7a52b1a 100644
--- a/Exercise_solutions.ipynb
+++ b/Exercise_solutions.ipynb
@@ -6,7 +6,7 @@
"source": [
"### Notebook 1\n",
"#### 1.1 \n",
- "Look up docs for the `convert` and `parse` functions."
+ "Look up docs for the `convert` function."
]
},
{
@@ -17,8 +17,7 @@
},
"outputs": [],
"source": [
- "?convert;\n",
- "# ?parse"
+ "?convert"
]
},
{
@@ -68,8 +67,7 @@
},
"outputs": [],
"source": [
- "# Here `convert` returns the ascii code (an integer) associated with the character `1`\n",
- "convert(Int64, '1')"
+ "convert(Int64, \"1\")"
]
},
{
@@ -80,8 +78,7 @@
},
"outputs": [],
"source": [
- "# Here `parse` returns the integer enclosed in single quotation marks\n",
- "parse(Int64, '1')"
+ "parse(Int64, \"1\")"
]
},
{
@@ -109,7 +106,17 @@
"metadata": {},
"source": [
"#### 2.2\n",
- "Add two numbers together within a string."
+ "Declare two variables\n",
+ "\n",
+ "```julia\n",
+ "a = 3\n",
+ "b = 4\n",
+ "```\n",
+ "and use them to create two strings:\n",
+ "```julia\n",
+ "\"3 + 4\"\n",
+ "\"7\"\n",
+ "```"
]
},
{
@@ -120,8 +127,29 @@
},
"outputs": [],
"source": [
- "m, n = 1, 1\n",
- "\"$m + $n = $(m + n)\""
+ "a = 3; b = 4"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "\"$a + $b\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "\"$(a + b)\""
]
},
{
@@ -131,8 +159,13 @@
"### Notebook 3\n",
"\n",
"#### 3.1 \n",
- "Create an array, `a_ray`, that is a 2-element 1D array of 1-element 1D arrays, each storing the number 0.\n",
- "Index into `a_ray` to add a second number, `1`, to each of the arrays it contains."
+ "Create an array, `a_ray`, with the following code:\n",
+ "\n",
+ "```julia\n",
+ "a_ray = [1, 2, 3]\n",
+ "```\n",
+ "\n",
+ "Add the number `4` to the end of this array and then remove it."
]
},
{
@@ -143,10 +176,8 @@
},
"outputs": [],
"source": [
- "a_ray = [[0], [0]]\n",
- "push!(a_ray[1], 1)\n",
- "push!(a_ray[2], 1)\n",
- "a_ray"
+ "a_ray = [1, 2, 3]\n",
+ "push!(a_ray, 4)"
]
},
{
@@ -154,7 +185,12 @@
"metadata": {},
"source": [
"#### 3.2 \n",
- "Try to add \"Emergency\" as key to `myphonebook` with the value `911`. Try to add `911` as an integer rather than as a string. Why doesn't this work?"
+ "Try to add \"Emergency\" as key to `myphonebook` with the value `string(911)` with the following code\n",
+ "```julia\n",
+ "myphonebook[\"Emergency\"] = 911\n",
+ "```\n",
+ "\n",
+ "Why doesn't this work?"
]
},
{
@@ -291,7 +327,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"squares = Dict()\n",
@@ -319,7 +357,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"A = fill(0, (10, 10))\n",
@@ -344,7 +384,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"N = 16\n",
@@ -376,6 +418,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
+ "collapsed": true,
"scrolled": true
},
"outputs": [],
@@ -399,7 +442,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"fill(0, (3, 3)) .+ 1"
@@ -417,7 +462,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"f(x) = x^2\n",
@@ -445,7 +492,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"caesar(input_string, shift) = map(x -> x + shift, input_string)"
@@ -465,7 +514,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"#Pkg.add(\"Primes\")\n",
@@ -487,7 +538,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"using Plots\n",
@@ -515,7 +568,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"import Base: +\n",
@@ -536,7 +591,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"\"Gr#qrw#phggoh#lq#wkh#diidluv#ri#gudjrqv#iru#|rx#duh#fuxqfk|#dqg#wdvwh#jrrg#zlwk#nhwfkxs1\" + -3"
@@ -590,7 +647,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"circshift(A, (0, 4))"
@@ -608,7 +667,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"v = [1, 2, 3]\n",
@@ -626,7 +687,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"v' * v"
@@ -657,7 +720,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"A =\n",
@@ -673,7 +738,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"eigdec = eigfact(A)\n",
@@ -692,7 +759,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"Diagonal(eigdec[:values])"
@@ -710,7 +779,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"F = hessfact(A)"
@@ -719,7 +790,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"isapprox(A, F[:Q] * F[:H] * F[:Q]')"