├── .gitignore
├── README.md
├── class
├── class1
│ ├── class1.csv
│ ├── class1.ipynb
│ ├── class1.jl
│ ├── hello.txt
│ ├── worksheet1.md
│ └── worksheet1_answers.ipynb
├── class2
│ ├── a_note_about_scope.md
│ ├── class2.ipynb
│ ├── worksheet1.md
│ ├── worksheet2.md
│ └── worksheets.ipynb
├── class3
│ ├── class3.ipynb
│ ├── data_and_plots.ipynb
│ └── iterative_solvers.ipynb
├── class4
│ └── class4.md
├── class5
│ └── class5.ipynb
├── class6
│ ├── Makefile
│ ├── __pycache__
│ │ └── cme257.cpython-37.pyc
│ ├── class6.ipynb
│ ├── cme257.c
│ ├── cme257.o
│ ├── cme257.py
│ ├── libcme257.so
│ └── test.py
├── class7
│ └── metaprogramming.ipynb
├── class8
│ └── multithreading.ipynb
├── getting_started.md
├── system_admin.md
└── using_notebooks.ipynb
├── hw
├── Solutions
│ └── hw1soln.jl
├── hw1.md
├── hw2.md
├── hw3.md
├── hw4.md
├── hw5.md
└── hw6.md
├── packages
├── 2015
│ ├── BayesNets.ipynb
│ ├── Calculus.ipynb
│ ├── ColorBrewer.ipynb
│ ├── Control.ipynb
│ ├── FiniteStateMachine.ipynb
│ ├── HyperDualNumbers.ipynb
│ ├── ImageView.ipynb
│ ├── JuMP.ipynb
│ ├── LightGraphs.ipynb
│ ├── MotionPlanning.ipynb
│ ├── Requests.ipynb
│ ├── asciiplot.ipynb
│ ├── devectorize.ipynb
│ ├── equations.ipynb
│ ├── faker.ipynb
│ ├── graphs.ipynb
│ ├── phylogenetics.ipynb
│ └── progressmeter.ipynb
├── CapacityExpansion.ipynb
├── Clustering.ipynb
├── Contour.ipynb
├── Convex.ipynb
├── DecisionTree.ipynb
├── DifferentialEquations.ipynb
├── DifferentialEquations2.ipynb
├── Distributions-aumurphy.ipynb
├── Distributions.ipynb
├── DynamicalSystems.ipynb
├── Fatou.ipynb
├── IterativeSolvers.ipynb
├── Latexify.ipynb
├── LightGraphs.ipynb
├── POMDPs.ipynb
├── README.md
├── TensorFlow.ipynb
├── TimeSeries.ipynb
├── gadfly.ipynb
├── juliafem.ipynb
└── sundials.ipynb
├── resources.md
└── syllabus.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .ipynb_checkpoints
2 | *.gif
3 | *.DS_Store
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CME 257 - Advanced Topics in Scientific Computing with Julia
2 |
3 | Welcome to the home of CME 257 at Stanford. This git repository will be used to host course materials.
4 |
5 | [Syllabus](syllabus.md)
6 |
7 | [Resources](resources.md)
8 |
9 | Class material will be put in the [class](class) folder.
10 |
11 | Homework will be put in the [hw](hw) folder
12 |
13 | For instructions installing/accessing Julia, see [class/getting_started](class/getting_started.md)
14 |
15 | 
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
16 |
--------------------------------------------------------------------------------
/class/class1/class1.csv:
--------------------------------------------------------------------------------
1 | 1, 2, 3, 4
2 | 5, 6, 7, 8
3 |
--------------------------------------------------------------------------------
/class/class1/class1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# CME 257 - Getting Started"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## What is Julia?\n",
15 | "\n",
16 | "[Julia](https://github.com/JuliaLang/julia) is a relatively new language designed for scientific computing. You'll find similarities to MATLAB and Python, but will also find differences. It is rapidly gaining popularity in the scientific computing community (for example, its optimization pacakges are very popular). Stanford has its fair share of users and contributors.\n",
17 | "\n",
18 | "Some features (see [julialang.org](http://julialang.org/)):\n",
19 | "* just-in-time compiliation (JIT)\n",
20 | "* parallelism and distributed execution\n",
21 | "* call python and C (and other languages!)\n",
22 | "* macros and metaprogramming\n",
23 | "* multiple dispatch functions (overload for different argument type combinations)\n",
24 | "* dynamic type system (user-defined types perform well)\n",
25 | "* built on LLVM\n"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "## Why use Julia?\n",
33 | "\n",
34 | "Julia is designed to balance two goals\n",
35 | "* Ease of programming\n",
36 | "* Speed of execution\n",
37 | "\n",
38 | "This means that you can get near C/Fortran-like performance, with the programming effort of MATLAB or Python. Julias tries to incorporate the best features of a variety of languages\n",
39 | "\n",
40 | "* General purpose code reads like Python\n",
41 | "* Linear algebra looks like MATLAB\n",
42 | "* Macros give Lisp-like abilities\n",
43 | "* Loops give speed like C/Fortran\n",
44 | "* Statistics looks like R (see Dataframes, Distributions pacakages)\n",
45 | "\n",
46 | "To see a performance comparison, see [here](https://julialang.org/benchmarks/)\n",
47 | "\n",
48 | "Some other reasons:\n",
49 | "* Open-source (MIT license)\n",
50 | "* Built with scientific computing in mind\n",
51 | "* Built with parallelism and distribution in mind\n",
52 | "* Performance ([julia's figure](http://julialang.org/benchmarks/))\n",
53 | "* Packages (built-in package manager)\n",
54 | "* Easy to call complied libraries, python\n",
55 | "* Supports multiple programming paradigms (OO, functional)\n",
56 | "* You can have an impact!\n",
57 | "\n",
58 | "If you want to compare Julia to MATLAB/Python/R, check out the following:\n",
59 | "https://docs.julialang.org/en/release-0.5/manual/noteworthy-differences/\n",
60 | "\n",
61 | "This cheatsheet gives syntax translations between MATLAB, Numpy, and Julia:\n",
62 | "https://cheatsheets.quantecon.org/index.html"
63 | ]
64 | },
65 | {
66 | "cell_type": "markdown",
67 | "metadata": {},
68 | "source": [
69 | "## A Mental Model for Julia\n",
70 | "\n",
71 | "The following analogy is borrowed from:\n",
72 | "https://ucidatascienceinitiative.github.io/IntroToJulia/Html/JuliaMentalModel\n",
73 | "\n",
74 | "### A Mental Model for Python/R/MATLAB: Talking to a Politician\n",
75 | "* These scripting languages were developed to \"be easy\".\n",
76 | "* You tell them something, and they try to give you want you want.\n",
77 | "* There may be some things hidden behind the scenes to make everything \"work better\".\n",
78 | "* They may not give you the fastest reply.\n",
79 | "\n",
80 | "### A Mental Model for C/Fortran: Talking to a Philosopher\n",
81 | "* You say something, and they want something more specific.\n",
82 | "* You spend hours digging deep into the specifics of something.\n",
83 | "* After finally getting it right, you know how to quickly get a specific answer from them.\n",
84 | "* Everytime you want to talk about something new, you have to start all the way at the basics again.\n",
85 | "\n",
86 | "### A Mental Model for Julia: Talking to a Scientist\n",
87 | "* When you're talking, everything looks general. However, you really mean very specific details determined by context.\n",
88 | "* You can quickly dig deep into a subject, assuming many rules, theories, and terminology.\n",
89 | "* Nothing is hidden: if you ever want to hear about every little detail, you can ask.\n",
90 | "* They will get mad (and throw errors at you) if you begin to be loose with the specific details.\n"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {},
96 | "source": [
97 | "## How to Use Julia\n",
98 | "\n",
99 | "1. [Jupyter notebooks](https://jupyter.org/). Same as IPython notebooks. One way to install is from the REPL with ```\n",
100 | "using Pkg; Pkg.add(\"IJulia\")\n",
101 | "```\n",
102 | "2. [The REPL (Read/Evaluate/Print/Loop)](https://en.wikibooks.org/wiki/Introducing_Julia/The_REPL). This is what you see when you launch Julia from the command line.\n",
103 | "3. [Juno IDE](http://junolab.org/). An IDE for Julia built on the [open source editor Atom](https://atom.io/).\n",
104 | "\n",
105 | "You should already be able to launch the REPL. Notes and homeworks for this course will primarily be contained in Jupyter notebooks posted to GitHub, so install the IJulia package on your system to use them (GitHub will render them online)."
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "## Time to Jump In\n",
113 | "\n",
114 | "Open up a Julia REPL, and start working on [worksheet1](worksheet1.md).\n",
115 | "Feel free to jump to a section that interests you. I highly recommend working in IJulia."
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {
121 | "collapsed": true
122 | },
123 | "source": [
124 | "## Scripts\n",
125 | "\n",
126 | "You can run julia scripts from the command line or use them to import code into a live session\n",
127 | "\n",
128 | "You can see an example in class1.jl:\n",
129 | "\n",
130 | "```julia\n",
131 | "println(\"Hello from class1.jl\")\n",
132 | "```\n",
133 | "\n",
134 | "Bash:\n",
135 | "```bash\n",
136 | "julia script.jl\n",
137 | "```\n",
138 | "\n",
139 | "Julia:\n",
140 | "```julia\n",
141 | "include(\"script.jl\")\n",
142 | "```\n",
143 | "\n",
144 | "You can use scripts to automate what you might do manually using the Julia prompt.\n",
145 | "\n",
146 | "Your first homework will involve writing a simple script."
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "metadata": {},
152 | "source": [
153 | "## Jupyter Notebooks\n",
154 | "\n",
155 | "To set up jupyter notebooks for Julia, see [here](https://github.com/icme/cme257-advanced-julia/blob/master/class/using_notebooks.ipynb)\n"
156 | ]
157 | },
158 | {
159 | "cell_type": "markdown",
160 | "metadata": {},
161 | "source": [
162 | "## Integration Problems\n",
163 | "\n",
164 | "Try to solve one or more of the following problems using Julia:\n",
165 | "\n",
166 | "* Solving Linear Systems: Julia offers [several special matrix types](https://docs.julialang.org/en/stable/manual/linear-algebra/#Special-matrices-1) for structured problems. Solve ```A x=b``` for x using backslash (```\\```, as in MATLAB) for several types of matrices, at several sizes. Is there a difference in the speed? What about asymptotic scaling?\n",
167 | "\n",
168 | "* Random walk on a grid: Simulate a random walk on an integer lattice in d dimensions, where d = 1,2,3,..., starting at the origin. Over how 10000 steps, how many times do you return to the origin?\n",
169 | "\n",
170 | "\n",
171 | "\n",
172 | "If you'd like to try some more relatively easy practice problems, check out the following links:\n",
173 | "\n",
174 | "https://lectures.quantecon.org/jl/julia_by_example.html\n",
175 | "\n",
176 | "https://ucidatascienceinitiative.github.io/IntroToJulia/Html/BasicProblems"
177 | ]
178 | }
179 | ],
180 | "metadata": {
181 | "kernelspec": {
182 | "display_name": "Julia 1.2.0",
183 | "language": "julia",
184 | "name": "julia-1.2"
185 | },
186 | "language_info": {
187 | "file_extension": ".jl",
188 | "mimetype": "application/julia",
189 | "name": "julia",
190 | "version": "1.2.0"
191 | }
192 | },
193 | "nbformat": 4,
194 | "nbformat_minor": 1
195 | }
196 |
--------------------------------------------------------------------------------
/class/class1/class1.jl:
--------------------------------------------------------------------------------
1 | println("Hello from class1.jl")
2 |
--------------------------------------------------------------------------------
/class/class1/hello.txt:
--------------------------------------------------------------------------------
1 | Hello world!
--------------------------------------------------------------------------------
/class/class1/worksheet1.md:
--------------------------------------------------------------------------------
1 | # Worksheet - Getting Started
2 |
3 | Work with your neighbors to discuss/solve the following questions. Feel free to look online if you're not sure how to get started. Some of these may have more than one answer.
4 |
5 | ---
6 |
7 | ### Syntax
8 |
9 | 1. How do you assign a variable?
10 | 2. How do you make a comment?
11 | 3. If you assign variable x, and then set y=x, what happens to y when you change the value of x?
12 | 4. How can you print a statement?
13 | 5. How can you print the contents of a variable?
14 | 6. What is the syntax for a for-loop?
15 | 7. What is the syntax for if-else statements?
16 | 8. How would you write a function that takes inputs x and y and returns x+2*y?
17 |
18 | ### Files
19 |
20 | 9. How would you load a file? (try class1.csv) (You may need to import a package for this.)
21 | 10. How would you run a script? (try class1.jl)
22 | 11. How can you print to a file?
23 |
24 | ### Arrays
25 |
26 | 12. How would you create an array? What are some operations you may wish to perform? Do array indices begin with 1 or 0?
27 | 13. Create a 10x10 random matrix A, a 1x10 random vector b, and solve the linear system Ax = b.
28 | 14. Allocate a 3x4 array of Int64 (don't initialize)
29 | 15. Allocate a 3x4 array of Int64, initialized to zero
30 |
31 | ### Packages
32 |
33 | 16. Where can you find a listing of Julia packages installed on the system?
34 | 17. How can you add a Julia package?
35 | 18. Add the Plots package, and plot y=x^2 for x in [0,10].
36 | 19. Add the Convex package and solve the convex problem minimize x^2 + y^2 where 3x+2y > 5 and x - 4y < 10
37 |
38 | ### Data Types
39 |
40 | 20. How can you represent imaginary numbers?
41 | 21. How can you represent fractions?
42 | 22. What's the difference between an array and tuple?
43 | 23. How can you create a dict (i.e. dictionary, associative array, map)?
44 | 24. How can you create a set?
45 | 25. How can you use explicit boolean "True" and "False" values?
46 | 26. How can I add an entry to the end of a vector?
47 | 27. How do I create a queue and add and remove entries from it?
48 |
49 | ### Programming Tools
50 |
51 | 28. How can you time an operation?
52 | 29. How can you find the type of something?
53 | 30. How can you see LLVM code?
54 | 31. What is an example of a macro?
55 | 32. How can you tell which version of Julia you are running?
56 | 33. How can you check the documentation of a function from the REPL/notebook?
--------------------------------------------------------------------------------
/class/class2/a_note_about_scope.md:
--------------------------------------------------------------------------------
1 | # A Note about Scope
2 |
3 | In Julia, the scope of variables can change depending on where you run your code. Code that runs in an IJulia notebook may sometimes fail to run in the REPL or when calling Julia from the command line.
4 |
5 |
6 | # The Issue
7 |
8 | In short, Julia by default restricts scope: If I define a variable ``` x ``` outside of a loop and want to change ``` x ```'s variable inside of a loop, Julia will throw an error:
9 |
10 | ```
11 | x = 1
12 | for i in 1:10
13 | x+= 1
14 | end
15 | println(x)
16 | ```
17 | in the REPL or in a script returns
18 |
19 | ```
20 | ERROR: UndefVarError: x not defined
21 | Stacktrace:
22 | [1] top-level scope at ./REPL[2]:2
23 | ```
24 |
25 | However, in an IJulia notebook all variables are considered to have "global" scope: they can be accessed and changed at all times by anything. Thus the above code runs without issue in a notebook: it returns ``` 11 ``` as you might expect.
26 |
27 | The reason for this difference is that in Julia loops and functions are by default only allowed write-access to variables defined within "local scope" (they can read from anywhere). Julia posesses two scopes: "global" and "local". Global scope is seen on the REPL and by default in Julia, while local scope is entered in functions, loops, macros, and comprehensions (which we haven't discussed yet). You may notice that since there are only two scopes, the rules stated above allow for a variable defined in an outer loop to be modified in an inner one. Indeed, this is the case:
28 |
29 | ```
30 | for i = 1:1
31 | z = i
32 | for j = 1:1
33 | z = 0
34 | end
35 | println(z)
36 | end
37 | ```
38 |
39 | prints ``` 0 ``` on both the REPL and in IJulia as expected.
40 |
41 | As mentioned, reading from variables outside of a loop or function is supported:
42 |
43 | ```
44 | w = 2
45 | for i = 1:1
46 | println(w)
47 | end
48 | ```
49 | simply prints ```w```'s value of ```1```.
50 |
51 | The reason this issue doesn't arise in IJulia is that IJulia notebooks by default enforce global scope everywhere through the package ```SoftGlobalScope```. You can imagine how restrictive scoping like this might break a cell-based model of programming like a notebook.
52 |
53 | # Fixing the Issue
54 |
55 | To fix this issue, there are two easy options:
56 |
57 | 1. Use IJulia notebooks.
58 | 2. Use the keyword ```global```.
59 |
60 | Although Julia will not allow us to modify an out-of-scope variable by default, we can force it to let us with the keyword ```global```. As an example,
61 |
62 | ```
63 | x = 1
64 | for i in 1:10
65 | global x+= 1
66 | end
67 | println(x)
68 | ```
69 | returns ``` 11``` just as it did on the REPL. ```global``` tells Julia to look for the variable ```x``` in its global memory and modify it. The reason for this subtlety is performance: if Julia's compiler can guarantee that all but a few variables in memory will not be changed inside a loop or a function, it can optimize much more aggressively than otherwise. We will go into more depth when we talk about optimizing Julia code.
70 |
71 |
72 | ## Arrays
73 | You might notice that this does not come up with arrays at all. Indeed, the code
74 |
75 | ```
76 | x = [1 1]
77 | for i = 1:2
78 | x[i] += 1
79 | end
80 | println(x)
81 | ```
82 |
83 | runs as expected and prints ```[2 2]``` on the REPL. However, we are not really modifying the array ```x``` here: we are simply changing the entries that ```x``` points to in memory. This is independent of local or global scope. If we tried to change the array ```x``` itself we would run into issues:
84 |
85 | ```
86 | x = [1 1]
87 | for i = 1:1
88 | x = [2 2]
89 | end
90 | println(x)
91 | ```
92 | runs into errors. Other data structures which serve as pointers to their entries (like dicts and queues) work in the same way.
93 |
--------------------------------------------------------------------------------
/class/class2/worksheet1.md:
--------------------------------------------------------------------------------
1 | # Worksheet 1: Types
2 |
3 | 1. What happens if you try to instantiate a cme257int with a float? Why did this happen?
4 |
5 | 2. Create a type called "cme257OrderedPair" with the following properties:
6 | 1. It is a child of cme257abstract
7 | 2. It has two fields: "a" and "b"
8 | 3. The fields "a" and "b" are the same parameterized type
9 |
10 | 3. How do you create a Complex number in Julia? Investigate the resulting type:
11 | 1. Is it a primitive type? If not, what fields does it have?
12 | 2. What else do you notice? Is it immutable? Is it parameterized?
13 |
--------------------------------------------------------------------------------
/class/class2/worksheet2.md:
--------------------------------------------------------------------------------
1 | # Worksheet 2: Functions
2 |
3 | 1. Overload the addition and multiplication operators to do element-wise addition and multiplication on cme257OrderedPair.
4 |
5 | 2. Overload addition and multiplication to work with cme257ff{N,T} for N any positive integer (don't worry about checking positivity), where the operations are defined mod N:
6 |
7 | (a +/* b).val = a.val +/* b.val mod N
8 |
9 | This gives this type the structure of a ring (in the mathematical sense). If N is prime, the type has the structure of a field (in the mathematical sense).
10 |
11 | 3. Modify ```yell_my_type``` to yell all types correctly.
12 |
13 |
14 |
--------------------------------------------------------------------------------
/class/class2/worksheets.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Worksheet 1: Types\n",
8 | "\n",
9 | "1. What happens if you try to instantiate a cme257int with a float? Why did this happen?\n",
10 | "\n",
11 | "2. Create a type called \"cme257OrderedPair\" with the following properties:\n",
12 | " 1. It is a child of cme257abstract\n",
13 | " 2. It has two fields: \"a\" and \"b\"\n",
14 | " 3. The fields \"a\" and \"b\" are the same parameterized type\n",
15 | " \n",
16 | "3. How do you create a Complex number in Julia? Investigate the resulting type:\n",
17 | " 1. Is it a primitive type? If not, what fields does it have?\n",
18 | " 2. What else do you notice? Is it immutable? Is it parameterized?"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 9,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "abstract type cme257abstract end"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 10,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "# an abstract type for our real number types\n",
37 | "abstract type cme257real <: cme257abstract end\n",
38 | "\n",
39 | "struct cme257int <: cme257real # <: denotes \"child of\"\n",
40 | " x::Int64 # :: tells us exactly what type x should be\n",
41 | "end\n",
42 | "struct cme257float <: cme257real\n",
43 | " x::Float64\n",
44 | "end"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 11,
50 | "metadata": {},
51 | "outputs": [
52 | {
53 | "ename": "InexactError",
54 | "evalue": "InexactError: Int64(5.5)",
55 | "output_type": "error",
56 | "traceback": [
57 | "InexactError: Int64(5.5)",
58 | "",
59 | "Stacktrace:",
60 | " [1] Type at ./float.jl:703 [inlined]",
61 | " [2] convert at ./number.jl:7 [inlined]",
62 | " [3] cme257int(::Float64) at ./In[10]:5",
63 | " [4] top-level scope at In[11]:1"
64 | ]
65 | }
66 | ],
67 | "source": [
68 | "#a0 = cme257int(5.0)\n",
69 | "a1 = cme257int(5.5)"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 12,
75 | "metadata": {},
76 | "outputs": [],
77 | "source": [
78 | "struct cme257OrderedPair{T} <: cme257abstract\n",
79 | " a::T\n",
80 | " b::T\n",
81 | "end"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 13,
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "data": {
91 | "text/plain": [
92 | "\"a\""
93 | ]
94 | },
95 | "execution_count": 13,
96 | "metadata": {},
97 | "output_type": "execute_result"
98 | }
99 | ],
100 | "source": [
101 | "op1 = cme257OrderedPair(\"a\",\"b\")\n",
102 | "op1.a"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 14,
108 | "metadata": {},
109 | "outputs": [
110 | {
111 | "data": {
112 | "text/plain": [
113 | "5.0 + 1.0im"
114 | ]
115 | },
116 | "execution_count": 14,
117 | "metadata": {},
118 | "output_type": "execute_result"
119 | }
120 | ],
121 | "source": [
122 | "z = 5.0 + 1.0im"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": 15,
128 | "metadata": {},
129 | "outputs": [
130 | {
131 | "data": {
132 | "text/plain": [
133 | "Complex{Float64}"
134 | ]
135 | },
136 | "execution_count": 15,
137 | "metadata": {},
138 | "output_type": "execute_result"
139 | }
140 | ],
141 | "source": [
142 | "typeof(z)"
143 | ]
144 | },
145 | {
146 | "cell_type": "code",
147 | "execution_count": 16,
148 | "metadata": {},
149 | "outputs": [
150 | {
151 | "name": "stdout",
152 | "output_type": "stream",
153 | "text": [
154 | "z.re = 5.0\n",
155 | "z.im = 1.0\n"
156 | ]
157 | },
158 | {
159 | "ename": "ErrorException",
160 | "evalue": "setfield! immutable struct of type Complex cannot be changed",
161 | "output_type": "error",
162 | "traceback": [
163 | "setfield! immutable struct of type Complex cannot be changed",
164 | "",
165 | "Stacktrace:",
166 | " [1] setproperty!(::Complex{Float64}, ::Symbol, ::Float64) at ./Base.jl:21",
167 | " [2] top-level scope at In[16]:4"
168 | ]
169 | }
170 | ],
171 | "source": [
172 | "# fields of complex numbers\n",
173 | "@show z.re\n",
174 | "@show z.im\n",
175 | "z.re = 3.0 # immutable\n",
176 | ";"
177 | ]
178 | },
179 | {
180 | "cell_type": "markdown",
181 | "metadata": {},
182 | "source": [
183 | "# Worksheet 2: Functions\n",
184 | "\n",
185 | "1. Overload the addition and multiplication operators to do element-wise addition and multiplication on cme257OrderedPair.\n",
186 | "\n",
187 | "2. Overload addition and multiplication to work with cme257ff{N,T} for N any positive integer (don't worry about checking positivity).\n",
188 | "\n",
189 | "(a +/* b).val = a.val +/* b.val mod N\n",
190 | "\n",
191 | "Generally, this gives this type the structure of a ring. If N is prime, the type has the structure of a field.\n",
192 | "\n",
193 | "3. Modify ```yell_my_type``` to yell all types correctly."
194 | ]
195 | },
196 | {
197 | "cell_type": "code",
198 | "execution_count": 17,
199 | "metadata": {},
200 | "outputs": [
201 | {
202 | "name": "stdout",
203 | "output_type": "stream",
204 | "text": [
205 | "x + y = cme257OrderedPair{Int64}(5, 7)\n",
206 | "x * y = cme257OrderedPair{Int64}(6, 12)\n"
207 | ]
208 | }
209 | ],
210 | "source": [
211 | "import Base.+, Base.*\n",
212 | "\n",
213 | "x = cme257OrderedPair(2, 3)\n",
214 | "y = cme257OrderedPair(3, 4)\n",
215 | "\n",
216 | "function +(x::cme257OrderedPair{T}, y::cme257OrderedPair{T}) where T\n",
217 | " return cme257OrderedPair(x.a + y.a, x.b + y.b) \n",
218 | "end\n",
219 | "\n",
220 | "@show x + y\n",
221 | "\n",
222 | "function *(x::cme257OrderedPair{T}, y::cme257OrderedPair{T}) where T\n",
223 | " return cme257OrderedPair(x.a * y.a, x.b * y.b)\n",
224 | "end\n",
225 | "\n",
226 | "@show x * y\n",
227 | ";"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": 18,
233 | "metadata": {},
234 | "outputs": [],
235 | "source": [
236 | "# we'll use this for finite fields with characteristic N\n",
237 | "struct cme257ff{N, T<:Integer} #<: cme257abstract\n",
238 | " val::T\n",
239 | " # override the default constructor to store things mod N\n",
240 | " function cme257ff{N,T}(val::T) where {N,T<:Integer} \n",
241 | " return new(mod(val,N))\n",
242 | " end\n",
243 | "end\n",
244 | "\n",
245 | "# You'll also see the N convention in Julia with types like Array{T,N}, where T is a type and N is a number\n",
246 | "\n",
247 | "# This will create constructors where the type of the value is inferred\n",
248 | "function cme257ff{N}(val::T) where {N, T}\n",
249 | " return cme257ff{N,T}(val)\n",
250 | "end\n",
251 | "# this will create constructors where signed integers are converted to unsigned integers\n",
252 | "function cme257ff{N,T}(val::T) where {N, T<:Signed} \n",
253 | " return cme257ff{N}(Unsigned(val))\n",
254 | "end\n",
255 | "# Note that the above are examples of creating functions\n",
256 | "\n",
257 | "function Base.show(io::IO, x::cme257ff{N,T}) where {N,T}\n",
258 | " print(io, x.val, \" mod \", N, \" (\", T, \")\")\n",
259 | " return\n",
260 | "end"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": 19,
266 | "metadata": {},
267 | "outputs": [
268 | {
269 | "name": "stdout",
270 | "output_type": "stream",
271 | "text": [
272 | "x * y = 3 mod 5 (UInt64)\n",
273 | "x + y = 1 mod 5 (UInt64)\n"
274 | ]
275 | }
276 | ],
277 | "source": [
278 | "function *(a::cme257ff{N,T}, b::cme257ff{N,T}) where {N,T}\n",
279 | " cme257ff{N,T}(T(mod(a.val * b.val, N)))\n",
280 | "end\n",
281 | "\n",
282 | "function +(a::cme257ff{N,T}, b::cme257ff{N,T}) where {N,T}\n",
283 | " cme257ff{N,T}(T(mod(a.val + b.val,N)))\n",
284 | "end\n",
285 | "\n",
286 | "x = cme257ff{5}(2)\n",
287 | "y = cme257ff{5}(4)\n",
288 | "@show x*y # 2 * 4 = 8 = 3 mod 5\n",
289 | "@show x+y # 2 + 4 = 6 = 1 mod 5\n",
290 | ";"
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 20,
296 | "metadata": {},
297 | "outputs": [
298 | {
299 | "ename": "MethodError",
300 | "evalue": "MethodError: no method matching +(::cme257ff{5,UInt64}, ::cme257ff{7,UInt64})\nClosest candidates are:\n +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529\n +(::cme257ff{N,T}, !Matched::cme257ff{N,T}) where {N, T} at In[19]:6",
301 | "output_type": "error",
302 | "traceback": [
303 | "MethodError: no method matching +(::cme257ff{5,UInt64}, ::cme257ff{7,UInt64})\nClosest candidates are:\n +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529\n +(::cme257ff{N,T}, !Matched::cme257ff{N,T}) where {N, T} at In[19]:6",
304 | "",
305 | "Stacktrace:",
306 | " [1] top-level scope at show.jl:576",
307 | " [2] top-level scope at In[20]:4"
308 | ]
309 | }
310 | ],
311 | "source": [
312 | "# note that with our cme257ff type, we can't add or multiply elements of different rings\n",
313 | "x = cme257ff{5}(2)\n",
314 | "y = cme257ff{7}(4)\n",
315 | "@show x+y\n",
316 | ";"
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "execution_count": 21,
322 | "metadata": {},
323 | "outputs": [
324 | {
325 | "ename": "MethodError",
326 | "evalue": "MethodError: no method matching *(::cme257ff{5,UInt16}, ::cme257ff{5,UInt32})\nClosest candidates are:\n *(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529\n *(::cme257ff{N,T}, !Matched::cme257ff{N,T}) where {N, T} at In[19]:2",
327 | "output_type": "error",
328 | "traceback": [
329 | "MethodError: no method matching *(::cme257ff{5,UInt16}, ::cme257ff{5,UInt32})\nClosest candidates are:\n *(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529\n *(::cme257ff{N,T}, !Matched::cme257ff{N,T}) where {N, T} at In[19]:2",
330 | "",
331 | "Stacktrace:",
332 | " [1] top-level scope at show.jl:576",
333 | " [2] top-level scope at In[21]:4"
334 | ]
335 | }
336 | ],
337 | "source": [
338 | "# we also need the same underlying type\n",
339 | "x = cme257ff{5}(UInt16(2))\n",
340 | "y = cme257ff{5}(UInt32(4))\n",
341 | "@show x*y\n",
342 | ";"
343 | ]
344 | },
345 | {
346 | "cell_type": "code",
347 | "execution_count": 22,
348 | "metadata": {},
349 | "outputs": [
350 | {
351 | "name": "stdout",
352 | "output_type": "stream",
353 | "text": [
354 | "MY TYPE IS RATIONAL{INT64}\n"
355 | ]
356 | }
357 | ],
358 | "source": [
359 | "function yell_my_type(x::T) where T\n",
360 | " println(uppercase(\"MY TYPE IS $(T)\"))\n",
361 | "end\n",
362 | "\n",
363 | "yell_my_type(5//7)"
364 | ]
365 | }
366 | ],
367 | "metadata": {
368 | "kernelspec": {
369 | "display_name": "Julia 1.2.0",
370 | "language": "julia",
371 | "name": "julia-1.2"
372 | },
373 | "language_info": {
374 | "file_extension": ".jl",
375 | "mimetype": "application/julia",
376 | "name": "julia",
377 | "version": "1.2.0"
378 | }
379 | },
380 | "nbformat": 4,
381 | "nbformat_minor": 1
382 | }
383 |
--------------------------------------------------------------------------------
/class/class3/iterative_solvers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Iterative Solvers with Linear Operators\n",
8 | "\n",
9 | "Here we demo how to solve data-sparse linear operators with an iterative solver. Essentially we'd like to solve\n",
10 | "$$Ax = b$$\n",
11 | "for $x$, where $A$ may have some special structure\n",
12 | "\n",
13 | "The two packages we'll use are\n",
14 | "* [LinearOperators.jl](https://juliasmoothoptimizers.github.io/LinearOperators.jl/latest/index.html)\n",
15 | "* [IterativeSolvers.jl](https://juliamath.github.io/IterativeSolvers.jl/latest/)\n",
16 | "\n",
17 | "In the exercises, you can also try out\n",
18 | "* [RandomizedLinAlg.jl](https://haampie.github.io/RandomizedLinAlg.jl/latest/)\n",
19 | "\n",
20 | "Which allows you to use randomized algorithms on linear operators. Note that this package is still not very well developed."
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": 2,
26 | "metadata": {},
27 | "outputs": [
28 | {
29 | "name": "stdout",
30 | "output_type": "stream",
31 | "text": [
32 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n",
33 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n",
34 | "\u001b[2K\u001b[?25h[1mFetching:\u001b[22m\u001b[39m [========================================>] 100.0 %.0 %\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
35 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Project.toml`\n",
36 | "\u001b[90m [no changes]\u001b[39m\n",
37 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Manifest.toml`\n",
38 | "\u001b[90m [no changes]\u001b[39m\n"
39 | ]
40 | }
41 | ],
42 | "source": [
43 | "using Pkg; Pkg.add([\"IterativeSolvers\",\"LinearOperators\"])\n",
44 | "using LinearOperators, IterativeSolvers, LinearAlgebra"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "LinearOperators let you work with low-rank and otherwise sparse objects as if they were matrices, but with basically no performance penalty for doing so."
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 3,
57 | "metadata": {},
58 | "outputs": [],
59 | "source": [
60 | "n = 1000\n",
61 | "v = randn(n,1)\n",
62 | "v /= norm(v)\n",
63 | "opV = LinearOperator(v)\n",
64 | "A = v*v'\n",
65 | "opA = opV * opV'\n",
66 | ";"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 5,
72 | "metadata": {},
73 | "outputs": [
74 | {
75 | "name": "stdout",
76 | "output_type": "stream",
77 | "text": [
78 | " 0.002600 seconds (5 allocations: 8.094 KiB)\n",
79 | " 0.000019 seconds (6 allocations: 8.188 KiB)\n",
80 | " 0.000017 seconds (6 allocations: 8.109 KiB)\n",
81 | "norm(b1 - b2) = 4.2090860693657047e-16\n",
82 | "norm(b2 - b3) = 0.0\n"
83 | ]
84 | }
85 | ],
86 | "source": [
87 | "x = randn(n)\n",
88 | "@time b1 = A * x\n",
89 | "@time b2 = opA*x \n",
90 | "@time b3 = dot(v,x)*v\n",
91 | "@show norm(b1-b2)\n",
92 | "@show norm(b2-b3)\n",
93 | ";"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 6,
99 | "metadata": {},
100 | "outputs": [
101 | {
102 | "data": {
103 | "text/plain": [
104 | "f2 (generic function with 1 method)"
105 | ]
106 | },
107 | "execution_count": 6,
108 | "metadata": {},
109 | "output_type": "execute_result"
110 | }
111 | ],
112 | "source": [
113 | "#comparing how LinearOperators do when compared to the equivalent vectorized code\n",
114 | "function f1(n)\n",
115 | " local x = randn(n)\n",
116 | " for i=1:10*n\n",
117 | " local v = randn(n,1)\n",
118 | " local v /= norm(v)\n",
119 | " local opV = LinearOperator(v)\n",
120 | " local b2 = opA*x\n",
121 | " end\n",
122 | "end\n",
123 | "\n",
124 | "function f2(n)\n",
125 | " local x = randn(n)\n",
126 | " for i=1:10*n\n",
127 | " local v = randn(n)\n",
128 | " local v /= norm(v)\n",
129 | " local b3 = dot(v,x)*v\n",
130 | " end\n",
131 | "end"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 8,
137 | "metadata": {},
138 | "outputs": [
139 | {
140 | "name": "stdout",
141 | "output_type": "stream",
142 | "text": [
143 | " 0.258557 seconds (40.01 k allocations: 233.467 MiB, 12.30% gc time)\n",
144 | " 0.273919 seconds (30.00 k allocations: 232.552 MiB, 14.11% gc time)\n"
145 | ]
146 | }
147 | ],
148 | "source": [
149 | "@time f1(1000)\n",
150 | "@time f2(1000)"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "## Use with IterativeSolvers package"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": 9,
163 | "metadata": {},
164 | "outputs": [
165 | {
166 | "name": "stdout",
167 | "output_type": "stream",
168 | "text": [
169 | "c = sqrt(2 * log(n)) = 3.7169221888498383\n"
170 | ]
171 | },
172 | {
173 | "data": {
174 | "text/plain": [
175 | "Linear operator\n",
176 | " nrow: 1000\n",
177 | " ncol: 1000\n",
178 | " eltype: Float64\n",
179 | " symmetric: true\n",
180 | " hermitian: true\n",
181 | "\n"
182 | ]
183 | },
184 | "execution_count": 9,
185 | "metadata": {},
186 | "output_type": "execute_result"
187 | }
188 | ],
189 | "source": [
190 | "using Random\n",
191 | "Random.seed!(1)\n",
192 | "@show c = sqrt(2*log(n))\n",
193 | "opA2 = (2*c)*opA + Diagonal(abs.(randn(n))) # spiked model\n",
194 | "opA2.symmetric=true\n",
195 | "opA2.hermitian=true\n",
196 | "opA2"
197 | ]
198 | },
199 | {
200 | "cell_type": "code",
201 | "execution_count": 18,
202 | "metadata": {},
203 | "outputs": [
204 | {
205 | "name": "stdout",
206 | "output_type": "stream",
207 | "text": [
208 | " 0.013801 seconds (1.24 k allocations: 7.643 MiB, 80.02% gc time)\n",
209 | " 0.002155 seconds (1.29 k allocations: 7.970 MiB)\n",
210 | "norm(b - opA2 * x_est_minres) = 4.036826124819918e-7\n",
211 | "norm(b - opA2 * x_est_cg) = 3.754567418356935e-7\n"
212 | ]
213 | }
214 | ],
215 | "source": [
216 | "x_true = randn(n)\n",
217 | "b = opA2 * x_true\n",
218 | "@time x_est_minres = minres(opA2, b)\n",
219 | "@time x_est_cg = cg(opA2, b)\n",
220 | "@show norm(b - opA2*x_est_minres)\n",
221 | "@show norm(b - opA2*x_est_cg)\n",
222 | ";"
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {},
228 | "source": [
229 | "As a comparison, here's how solving the dense matrix with backslash goes:"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 15,
235 | "metadata": {},
236 | "outputs": [
237 | {
238 | "name": "stdout",
239 | "output_type": "stream",
240 | "text": [
241 | " 0.126188 seconds (9 allocations: 7.645 MiB, 65.32% gc time)\n",
242 | "norm(b - A2 * x_est) = 2.019170560589031e-14\n"
243 | ]
244 | }
245 | ],
246 | "source": [
247 | "Random.seed!(1)\n",
248 | "A2 = (2*c)*A + Diagonal(abs.(randn(n)))\n",
249 | "@time x_est = A2\\b\n",
250 | "@show norm(b - A2*x_est)\n",
251 | ";"
252 | ]
253 | },
254 | {
255 | "cell_type": "markdown",
256 | "metadata": {},
257 | "source": [
258 | "## Exercises/Extras\n",
259 | "\n",
260 | "If you're interested, try out one or more of the following exercises:\n",
261 | "\n",
262 | "1. Try out the randomized linear algebra package [RandomizedLinAlg.jl](https://haampie.github.io/RandomizedLinAlg.jl/latest/). Try using the ```rnorms``` function to estimate the matrix norm.\n",
263 | "\n",
264 | "2. Try out the Krylov methods package [Krylov.jl](https://github.com/JuliaSmoothOptimizers/Krylov.jl), or mess with matrix exponentials with [Expokit.jl](https://github.com/acroy/Expokit.jl).\n",
265 | "\n",
266 | "3. Make a plot of how long it takes to solve $Ax = b$ for $A$ diagonal + rank-1, for various sizes of problems. Estimate how long it would take to solve the equivalent problem using the full matrix\n",
267 | "\n",
268 | "4. You can also use sparse matrices as LinearOperators, and with iterative solvers. Use [sprand](https://docs.julialang.org/en/stable/stdlib/arrays/#Base.SparseArrays.sprand) to generate sparse matrices of various sizes and try using [gmres](https://juliamath.github.io/IterativeSolvers.jl/latest/linear_systems/gmres.html) to solve some linear systems.\n",
269 | "\n",
270 | "5. Check out the [tutorial](https://juliasmoothoptimizers.github.io/LinearOperators.jl/latest/tutorial.html#Using-functions-1) on how to use functions as linear operators"
271 | ]
272 | }
273 | ],
274 | "metadata": {
275 | "kernelspec": {
276 | "display_name": "Julia 1.2.0",
277 | "language": "julia",
278 | "name": "julia-1.2"
279 | },
280 | "language_info": {
281 | "file_extension": ".jl",
282 | "mimetype": "application/julia",
283 | "name": "julia",
284 | "version": "1.2.0"
285 | }
286 | },
287 | "nbformat": 4,
288 | "nbformat_minor": 1
289 | }
290 |
--------------------------------------------------------------------------------
/class/class4/class4.md:
--------------------------------------------------------------------------------
1 | # CME 257 Class 4 - using Git
2 |
3 | (Some of this material was originally borrowed from the 2015 version of CME211, taught by Nick Henderson)
4 |
5 | If you don't have a mac or linux computer, it may be easiest to ssh into farmshare to follow along.
6 |
7 | ## Getting started
8 | You may already be familiar with Git already from other classes or from research. You may have also used it to install Julia back at the start of the course.
9 |
10 | We've also seen that Julia's package manager is doing something with git under the hood, when we do something like
11 | ```julia
12 | julia> Pkg.clone("Flux")
13 | ┌ Warning: Pkg.clone is only kept for legacy CI script reasons, please use `add`
14 | └ @ Pkg.API /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.2/Pkg/src/API.jl:406
15 | Cloning git-repo `https://github.com/FluxML/Flux.jl.git`
16 | Updating git-repo `https://github.com/FluxML/Flux.jl.git`
17 | Resolving package versions...
18 | Installed ZygoteRules ─ v0.1.0
19 | Installed IRTools ───── v0.2.3
20 | Installed Zygote ────── v0.3.4¸
21 | Updating `~/.julia/environments/v1.2/Project.toml`
22 | [587475ba] ~ Flux v0.9.0 ⇒ v0.9.0 [`~/.julia/dev/Flux`]
23 | Updating `~/.julia/environments/v1.2/Manifest.toml`
24 | [587475ba] ~ Flux v0.9.0 ⇒ v0.9.0 [`~/.julia/dev/Flux`]
25 | [7869d1d1] + IRTools v0.2.3
26 | [9f7883ad] - Tracker v0.2.3
27 | [e88e6eb3] + Zygote v0.3.4
28 | [700de1a5] + ZygoteRules v0.1.0
29 | ```
30 |
31 | (Ignore that `Pkg.clone()` is deprecated for this example)
32 |
33 | Today we'll talk a bit about what's going on here - version control using git.
34 |
35 | ## What is git?
36 |
37 | First, let's note that GitHub is not git (and git is not GitHub). We use GitHub in this course because that is where Julia and its packages are stored. GitHub is a host for remote git repositories, and there are other such hosts (for example bitbucket.org or gitlab.com). You can also set up your own remote repository on a private server, or even use git without a remote repository. If you're a student, you can request an academic account on GitHub (use your Stanford email) to let you have private repositories for free. This is nice for research, or other projects you're not quite ready to share with the world.
38 |
39 | What is git? It is a version control system. This enables:
40 | * periodic saving of work (called *committing*)
41 | * returning to old versions when a problem is introduced
42 | * creation of experimental code *branches* with out disturbing the main or working code
43 | * *merging* the concurrent work of independent developers
44 | * *remote* backup and storage of work
45 | * tracking a *log* of project history
46 |
47 | Git is not the only tool used for version control, although it is one of the most popular. Others include
48 | * CVS (Concurrent Versioning System)
49 | * SVN (Subversion)
50 | * HG (Mercurial)
51 |
52 | If you haven't used version control before, this class will introduce you to the basics of git, and why it is a good idea to use it. This isn't intended to be a comprehensive overview of git let alone version control, but will hopefully give you a bit of familiarity and experience with 95% of the operations you will use in git.
53 |
54 | ## Resources
55 | * Git homepage: http://git-scm.com/
56 | * Git documentation: http://git-scm.com/doc
57 | * Git Book: http://git-scm.com/book/en/v2
58 |
59 | ## Setup
60 |
61 | The first time you use git on a machine you should give it your name and email, so it knows who is commiting changes.
62 |
63 | ```bash
64 | git config --global user.name "Your name"
65 | git config --global user.email your@email.com
66 | ```
67 |
68 | This creates a file at ~/.gitconfig, which you can view/edit.
69 |
70 | You can see what your settings are at any time with
71 |
72 | ```bash
73 | git config --list
74 | ```
75 |
76 | When using GitHub, you will also need to upload ssh keys in order to push changes to the remote repository.
77 |
78 | ```bash
79 | ssh-keygen -t rsa -b 4096 -C "youremail@email.com"
80 | ```
81 |
82 | You can view the contents of your public key
83 | ```bash
84 | cat ~/.ssh/id_rsa.pub
85 | ```
86 | and copy and paste it to your [GitHub settings](https://github.com/settings/ssh).
87 |
88 | GitHub has more detailed instructions [here](https://help.github.com/articles/generating-ssh-keys/)
89 |
90 | ## Basics
91 |
92 | initializing a repository:
93 | ```bash
94 | cd my_directory
95 | git init .
96 | ```
97 |
98 | View the current status of your repository:
99 | ```bash
100 | git status
101 | ```
102 |
103 | adding a file to commit:
104 | ```bash
105 | git add file.txt
106 | ```
107 |
108 | remove a file from the repository:
109 | ```bash
110 | git rm badfile.txt
111 | ```
112 | moving a file:
113 | ```bash
114 | git mv moved.txt
115 | ```
116 |
117 | commit changes:
118 | ```bash
119 | git commit -m "brief description"
120 | ```
121 | if you don't pass a message, git will open up a text editor for you to comment in (you can specify a default editor using git config). Writing some sort of informative message is important, since it helps you roll back changes in the future if you accidentally break something.
122 |
123 | ## Exercise 1
124 | On farmshare (or on your own computer) create a new folder, and initialize it as a git repository. I'll use cme257 as my folder name if you want to agree with what you see here.
125 | * setup git on your system if you have not already
126 | * touch a file 'README.md' in your folder,
127 | * Make the first line of the file
128 | ```
129 | # Toy git repository
130 | description coming soon
131 | ```
132 | * add the file and commit it to the repository.
133 |
134 | ## DAGs and git
135 |
136 | One useful way to think about what git is doing is to think of its operations creating vertices and edges on a [directed acyclic graph](https://en.wikipedia.org/wiki/Directed_acyclic_graph) (DAG) ([here](http://eagain.net/articles/git-for-computer-scientists/) is a reference for the graph-inclined folks).
137 |
138 | * a commit creates a vertex
139 | * you add an edge between two sequential commits
140 |
141 | So far the DAG we've made is not very interesting. Here are some
142 |
143 | * git branch - create a new branch. This allows you to add more than one child to a vertex on your DAG. There is always one branch by default - master.
144 |
145 | ```bash
146 | git branch # print out available branches
147 | git branch branch1 # creates a new branch 'branch1'
148 | git checkout branch1 # create content on branch1: read checkout as "check out"
149 | git checkout master # create content on the master branch
150 | git checkout -b branch2 # create a new branch 'branch2', and check it out
151 | ```
152 |
153 | * git merge - merge two branches into one. Creates edges from the two branch nodes onto one vertex. This new node has two parents in your DAG.
154 |
155 | ```bash
156 | git checkout master # checkout the branch you want to merge to
157 | git merge branch1 # merge the added content.
158 | ```
159 |
160 | Sometimes a merge is a clean process. Other times, the same file on different branches may be in a different state. Git provides a mergetool to help resolve these differences.
161 |
162 | ```bash
163 | git mergetool
164 | ```
165 |
166 | After you are done with a branch, you can delete it.
167 |
168 | ```bash
169 | git branch -d branch1 # delete branch 'branch1'
170 | ```
171 |
172 | ## Exercise 2
173 | use git status to keep track of what is going on.
174 | * create a branch in your repository and edit README.md to read
175 | ```
176 | # Toy git repository
177 | this repo is just for show
178 | ```
179 | * commit your changes
180 | * change to the master branch
181 | * edit your README to say
182 | ```
183 | # Toy git repository
184 | description coming soon
185 | installation note
186 | ```
187 | * commit your changes.
188 | * merge your branch with master. Keep the third line of README in master, but use the second line in README from your branch
189 | * can you draw a DAG that describes what just happened?
190 |
191 | ## More git commands
192 |
193 | * git log
194 |
195 | Git log will display a history of commits and messages
196 |
197 | ```bash
198 | git log
199 | git log --stat # show which files were changed and # lines changed
200 | ```
201 |
202 | * git reset
203 |
204 | git reset can perform the opposite of git add if you don't wish to commit changes to a particular file
205 | ```bash
206 | git add file.txt # stage file for commit
207 | git reset file.txt # remove file from staging
208 | ```
209 | Sometimes you may want to completely discard your changes because you went down the wrong path.
210 | ```bash
211 | git reset --hard # return tracked files to the state they were in at
212 | git reset --hard # return to state of last commit
213 | ```
214 |
215 | * git rebase
216 |
217 | Sometimes, you may wish to change the start of a branch from one place to another. This is useful if changes on a branch do not rely on changes that occurred after the branch was formed.
218 |
219 | ```bash
220 | git rebase master branch # rebases branch to be on master
221 | ```
222 |
223 | * .gitignore
224 |
225 | This isn't actually a command, but you can create a file that will tell git to ignore certain files or types of files.
226 |
227 | ```
228 | # .gitignore
229 | secrets.txt # don't share secrets
230 | *~ # emacs temp files - * is a wildcard
231 | ```
232 |
233 |
234 | ## Exercise 3
235 |
236 | * Edit your README, git add, then git reset it. Then hard reset your changes.
237 | * Create a branch, commit a change. Switch to master and make a different change. Rebase your branch to the most recent commit on master.
238 |
239 |
240 | ## Creating a GitHub repository
241 |
242 | * git clone - create a copy of a remote repository on your machine
243 |
244 | ```bash
245 | git clone
246 | ```
247 |
248 | * git pull - this will update your local repository from the remote repository. You will do this often if you're collaborating on a project, or are using a project that is being actively worked on.
249 |
250 | ```bash
251 | git pull origin master # pull contents of origin to update master
252 | ```
253 |
254 | * git push - this will update the remote repository with changes that you've made locally
255 |
256 | ```bash
257 | git push origin master
258 | ```
259 |
260 | #### Demo
261 |
262 | 1. start on GitHub
263 | 2. clone to your machine
264 |
265 | #### Syncing with the remote repo
266 |
267 | Push local changes to remote:
268 |
269 | 1. add file on `corn.stanford.edu`
270 | 2. commit locally
271 | 3. push to GitHub
272 |
273 | Pull down remote changes
274 |
275 | 1. add a new file on GitHub
276 | 2. pull changes to `corn.stanford.edu`
277 |
278 |
279 | ## Contributing to projects on GitHub (or elsewhere)
280 |
281 | There are several ways to contribute to a project on GitHub
282 | * you are an owner of the project and can commit directly to the main repository
283 | * you fork someone else's repository, change your version, and submit a pull request
284 |
285 | Not all contributions involve writing code or documentation. If you use someone else's code and find a bug, even if you don't know how to fix it you can file an issue which may let the developer track down your problem. You can also file issues that request new features if you think they should be implemented. This takes a little effort, but if someone fixes your problem, you're getting a great deal.
286 |
287 | #### Workflow
288 |
289 | * git remote - This will give you information on remote repositories.
290 |
291 | ```bash
292 | git remote -v # list remote repository for your local repository
293 | ```
294 |
295 | * create a fork - this can be done from GitHub. Clone it on your machine.
296 | * Now you want to tell your local repo where the origin of your fork was. This will be referred to as 'upstream'
297 |
298 | ```bash
299 | git remote add upstream
300 | git remote -v # see what changed
301 | ```
302 |
303 | * updating your fork
304 |
305 | If you've set upstream, and you want to update your fork from the original project, you can do the following:
306 |
307 | ```bash
308 | git fetch upstream # retrieves state of upstream repo
309 | git checkout master
310 | git merge upstream/master # merge the upstream master into your master
311 | git push origin master # push changes to your remote fork.
312 | ```
313 |
314 | * create a pull request
315 |
316 | This is fairly easy to do from GitHub.
317 | 1. Update your fork to be as close as possible to the upstream master
318 | 2. Commit your changes to your fork
319 | 3. Push your changes to origin on GitHub.
320 | 4. In GitHub, from your fork you can create a pull request.
321 |
322 | #### Continuous integration
323 |
324 | Online repository hosting is also useful for continuous integration testing. One popular system is [Travis CI](https://travis-ci.org/) (you can get a free account with your GitHub student account). CI systems will test your repository regularly by installing it on a virtual machine and running tests. This is a good way to verify that it is easy for others to use code that you have written and reproduce your results. Note that many Julia packages and Julia itself use Travis CI, as well as [AppVeyor](https://ci.appveyor.com) (CI for Windows). You can also use code coverage tools (such as [Coveralls](https://coveralls.io)) to tell you how much of your code base is covered by your test suite.
325 |
326 | ## Exercise 4
327 | You'll need to do this for HW3.
328 | * fork a copy of the course git repository.
329 | * add the main repository as upstream.
330 |
--------------------------------------------------------------------------------
/class/class6/Makefile:
--------------------------------------------------------------------------------
1 | # Make a shared object library to call from Julia
2 |
3 | CC = gcc
4 | CFLAGS = -std=c99 -O3 -Wall -fPIC
5 | LIBFLAGS = -shared
6 |
7 | default: libcme257
8 |
9 | %.o: %.c
10 | $(CC) -c -o $@ $< $(CFLAGS)
11 |
12 | libcme257: cme257.o
13 | #$(CC) -o libcme257.dylib cme257.o $(LIBFLAGS) # for mac
14 | $(CC) -o libcme257.so cme257.o $(LIBFLAGS) # for linux
15 |
16 | clean:
17 | rm -rf *.o *.so *.o *~ *.pyc *.dylib
18 |
--------------------------------------------------------------------------------
/class/class6/__pycache__/cme257.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/icme/cme257-advanced-julia/90cf5757027e3ca0ec5c10e3469a2963676eb89f/class/class6/__pycache__/cme257.cpython-37.pyc
--------------------------------------------------------------------------------
/class/class6/cme257.c:
--------------------------------------------------------------------------------
1 | // c file intended to be compiled as a shared object library
2 | #include
3 | #include
4 |
5 | void hello();
6 | int c_sum(int a, int b);
7 | int c_mult(int a, int b);
8 |
9 | void hello() {
10 | printf("hello world!");
11 | }
12 |
13 | int c_sum(int a, int b) {
14 | return a + b;
15 | }
16 |
17 | int c_mult(int a, int b) {
18 | return a * b;
19 | }
20 |
--------------------------------------------------------------------------------
/class/class6/cme257.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/icme/cme257-advanced-julia/90cf5757027e3ca0ec5c10e3469a2963676eb89f/class/class6/cme257.o
--------------------------------------------------------------------------------
/class/class6/cme257.py:
--------------------------------------------------------------------------------
1 | def fibonacci(n):
2 | if n == 0:
3 | return 0
4 | if n == 1:
5 | return 1
6 | return fibonacci(n - 1) + fibonacci(n - 2)
7 |
--------------------------------------------------------------------------------
/class/class6/libcme257.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/icme/cme257-advanced-julia/90cf5757027e3ca0ec5c10e3469a2963676eb89f/class/class6/libcme257.so
--------------------------------------------------------------------------------
/class/class6/test.py:
--------------------------------------------------------------------------------
1 | import cme257
2 |
3 | print "fib(5) = ", cme257.fibonacci(5)
4 |
--------------------------------------------------------------------------------
/class/getting_started.md:
--------------------------------------------------------------------------------
1 | # Getting Started
2 |
3 | This document covers two ways to get started using Julia. We'll use the most recent release, which is currently v0.6.2
4 |
5 | ## A note on Operating Systems
6 |
7 | The vast majority of computing is performed on \*nix operating systems, the most familiar of which include Linux, OpenBSD, and MacOS. A common feature on all these systems is the [bash shell](https://en.wikipedia.org/wiki/Bash_(Unix_shell)), often via a terminal. If you aren't already familiar with the bash shell, use this course as an opportunity to get started - it will open up all sorts of computing resources to you. This class will assume that you are using such a system.
8 |
9 | To Windows users: Julia can be installed on Windows, but for the purposes of this course, you are encouraged to choose one of the following options:
10 |
11 | 0. [likely easiest] Use Ubuntu (a Linux distribution) inside Windows 10 - see [here](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
12 | 1. Obtain a Linux virtual machine. You can try installing this on your computer, but many departments at Stanford offer them to students.
13 | 2. SSH into a \*nix computing resource, such as Farmshare 2. For example, see [here](https://www.ssh.com/ssh/client) for some options.
14 | 3. Dual boot your Windows machine with a \*nix operating system.
15 |
16 | If you have significant development experience on Windows, you may be able to navigate the course with Julia on Windows. However, the instructor will not be very useful for the purposes of troubleshooting any problems.
17 |
18 | ### If You're Unfamiliar with BASH
19 |
20 | Don't worry - you don't need much for this course. Many references can be found online, but [here's one quick reference](https://courses.cs.washington.edu/courses/cse390a/14au/bash.html).
21 |
22 | Once you can navigate the filesystem using the commands ```ls, cd, pwd, mkdir, rmdir```, you should be able to follow the instructions below.
23 |
24 | ## Installing Julia
25 |
26 | We will be using the latest stable release of Julia in this class (at the time of writing, Julia 1.2.0).
27 |
28 | ## Install on Your Computer
29 |
30 | ### Installing from Binaries
31 |
32 | The easiest and recommended option is to [download Julia for your machine here](https://julialang.org/downloads/).
33 |
34 | #### Instructions for Mac OSX
35 |
36 | On Macs, download the Julia .dmg file at [this link](https://julialang-s3.julialang.org/bin/mac/x64/1.2/julia-1.2.0-mac64.dmg). Open the disk image and copy the Julia executable to your Applications folder. Double click the application to launch Julia. Once here, you may add a symlink to this executable so that you can call Julia from the command line-- use the command
37 | ```
38 | sudo ln -s /Applications/Julia-1.2.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia
39 |
40 | ```
41 | #### Instructions for Debian/Ubuntu
42 |
43 | Download the correct file for your system at [the download link](https://julialang.org/downloads/). Extract the contents to a known folder on your computer (I suggest creating a folder at ```~/app```.) Once you have extracted the contents of the archive, open Terminal and run the magic words
44 |
45 | ```
46 | cd /usr/local/bin
47 | sudo ln -s {folder you extracted Julia to}/julia-1.2.0/bin/julia .
48 | ```
49 |
50 | to create a symlink on the terminal. To run Julia, type ```julia``` into Terminal.
51 |
52 | ### Installing from Source
53 |
54 | For advanced users, another option is to install from source. This will get you started using git version control, and if you want to contribute to Julia down the road you'll be half way there. In earlier versions of Julia compiling your own copy of Julia would give you compiler optimizations not present in the official binaries: this is typically no longer the case though.
55 |
56 | ### Installing Julia From Source
57 | Instructions can be found on the [Julia GitHub page](https://github.com/JuliaLang/julia#source-download-and-compilation)
58 |
59 | 1. [recommended] if you don't already have one, set up a [GitHub](https://github.com/) account - much of the Julia ecosystem lives on GitHub, and if you ever want to contribute you'll need an account
60 | 2. Open up a terminal on your machine
61 | 3. Create/choose a directory you would like your Julia build to live in, and clone the Julia repository
62 | ```
63 | cd /your/path/
64 | git clone git://github.com/JuliaLang/julia.git
65 | ```
66 | 4. checkout the current latest version
67 | ```
68 | cd julia
69 | git checkout v1.2.0
70 | ```
71 | 5. [optional] Configure Julia's build
72 | 6. build julia
73 | ```
74 | make -j 4 # parallelizes build
75 | ```
76 | The path to the Julia executable is now
77 | ```
78 | /your/path/julia/julia
79 | ```
80 | 7. [optional] Edit your ```.bashrc``` file to allow you to start Julia from anywhere in your terminal. There are several ways to achieve this, but one way is using an alias, e.g. see [here](https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias)
81 |
82 | ## Use on a Cluster
83 |
84 | You may have a lab or department cluster you'd like to use. Here we'll cover how to start Julia on Stanford's community computing cluster [Farmshare 2](https://srcc.stanford.edu/farmshare2)
85 |
86 | 1. ssh into Farmshare 2
87 | ```
88 | ssh @rice.stanford.edu
89 | ```
90 | Where `````` is replaced by your stanford ID. Follow the login prompts
91 |
92 | 2. The default version of Julia on rice is v0.5.2. Since we want the most up-to-date version, we'll load the Julia module
93 | ~~~
94 | module load julia
95 | ~~~
96 | 3. Launch Julia using
97 | ~~~
98 | julia
99 | ~~~
100 |
101 | ## Now What?
102 |
103 | When you call the ```julia``` command from the terminal or run the executable, you enter the interactive REPL (Read/Evaluate/Print/Loop). You can read more [here](https://en.wikibooks.org/wiki/Introducing_Julia/The_REPL)
104 |
105 | ## Which Version of Julia am I Using?
106 |
107 | Remember, we're going to use the current version (v1.2.0)
108 |
109 | * You can determine this from the command line using ```julia --version```
110 | * When the REPL launches you can see version information in the start-up splash.
111 | * Julia has a command for displaying version information: ```versioninfo()```
112 |
113 | ## How to Exit Julia
114 |
115 | Now that you can start the Julia REPL, you may wish to be able to close it. This can be done using ```exit() ```
116 |
--------------------------------------------------------------------------------
/class/system_admin.md:
--------------------------------------------------------------------------------
1 | # Becoming Your Own System Administrator
2 |
3 | For some of you, this class may be the first time using a personal computer for coding/development outside a controlled environment such as an IDE. Then you login to a remote cluster, often someone else (a system administrator) maintains user accounts and software on that machine, and you can request that software be added. When you use your own machine for software development, you have to become your own system administrator. This means you need to learn a few things about how your computer works under the hood, but also gives you a lot of control over your machine.
4 |
5 | This document contains a few hints to get you started as your own system administrator. Again, this is assuming you have a *nix system. Of course, there is a lot out there, but you can go a long way just knowing some basics.
6 |
7 | ## Mac Users
8 |
9 | Mac users should begin by installing [Xcode](https://developer.apple.com/xcode/) which is Apple's development toolkit.
10 | There's a lot in there, but at a high level it transforms your Mac from a consumer product into a professional development machine.
11 |
12 | If you are running Linux, congratulations - you already have a professional development machine.
13 |
14 | ## Pacakge Managers
15 |
16 | Pacakge managers are your friend. They make installing, upgrading, and maintaining software super easy. Most Linux distributions come with a package manager (e.g. apt-get or dnf).
17 |
18 | Mac users will want to install a package manager. One popular option is [homebrew](https://brew.sh/), but there are others out there.
19 |
20 | If you aren't sure what your package manager is, search "(your operating system) package manager" on the internet. The first few results will get you started.
21 |
22 | Many programming languages (and other software bundles) have their own package managers.
23 | For example, python has pip (or conda), node has npm, atom as apm, and Julia has Pkg.
24 |
25 | We've seen how Julia's package manager makes installing Julia packages very easy. The same is true for system package managers.
26 | Let's say that I need to have a fortran complier on my system (my package manager is dnf on Fedora linux). Then I can just open up a terminal and type
27 | ```bash
28 | dnf install gfortran
29 | ```
30 | and after an automated intstallation process completes, I can now use gfortran on my system
31 | ```bash
32 | gfortran --version
33 | ```
34 |
35 | You'll be surprised how much you can obtain via a package manager. Most popular software tools/libraries for scientific computing and data science can be obtained this way.
36 |
37 | ## User Privileges
38 |
39 | *nix systems try to keep you (and others) from removing critical files and programs from your computer, or installing software that has abilities to read/write in sensitive areas of the file system. This is useful, because it makes it harder for you to accidentaly break your computer, or for a hacker to execute malicious code on your system. However, sometimes you'll need to acutually need to modify these files or add programs to your system that require elevated privileges.
40 | Often you will find this out when you get an error that says so.
41 |
42 | This is where the ```sudo``` command (super-user-do) comes into play. To edit certain files or install programs in certain locations you need to have elevated privileges on your system. To invoke these privileges you can begin a command with ```sudo``` e.g. ```sudo gedit file.txt``` then type your password at the prompt.
43 |
44 | Note that when you elevate your privileges, you can potentially do some very bad things, like delete all files from your computer, or make it so your operating system no longer works.
45 | This means that you shouldn't use it without thinking about it, and don't just blindly copy commands from the internet without understanding what they will do.
46 | You can also do some very powerful good things, like partition your hard drive, install software for all users, and modify operating system settings.
47 |
48 | You can also navigate the bash shell as a super user using the ```su``` command.
49 |
50 | If you ever run into an error with file permissions, ```chmod``` is a command that may help you out.
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/class/using_notebooks.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# CME 257 - IJulia Notebooks"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## What is this?\n",
15 | "\n",
16 | "This is a [IJulia notebook](https://github.com/JuliaLang/IJulia.jl). You may have seen IPython (now Jupyter) notebooks before. I'll put demos in IJulia notebooks that will let me annotate code, and which you can run from your browser.\n",
17 | "\n",
18 | "## How Can I Run This?\n",
19 | "\n",
20 | "To run an IJulia notebook, open up the Julia REPL and run \n",
21 | "```\n",
22 | "using Pkg\n",
23 | "Pkg.add(\"IJulia\")\n",
24 | "```\n",
25 | "\n",
26 | "You'll need to add more packages throughout the course as we explore different topics.\n",
27 | "\n",
28 | "Now to use IJulia, from Julia type\n",
29 | "\n",
30 | "```julia\n",
31 | "using IJulia\n",
32 | "notebook()\n",
33 | "```\n",
34 | "\n",
35 | "This should open up a new window in your browser that will let you navigate to IJulia notebooks. When you open up a notebook, you'll be able to run cells by pressing Shift+Enter or Ctl+Enter. Note that this won't work if you're viewing the notebook in GitHub. Try it in the next cell:"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": 2,
41 | "metadata": {},
42 | "outputs": [
43 | {
44 | "data": {
45 | "text/plain": [
46 | "4"
47 | ]
48 | },
49 | "execution_count": 2,
50 | "metadata": {},
51 | "output_type": "execute_result"
52 | }
53 | ],
54 | "source": [
55 | "x = 2 # change this number and press Shift+Enter/Control+Enter\n",
56 | "x + x"
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "## Additional Resources\n",
64 | "\n",
65 | "* [IJulia](https://github.com/JuliaLang/IJulia.jl) GitHub page (good for troubleshooting)\n",
66 | "* [JuliaBox](https://juliabox.org) also includes a short tutorial on IJulia\n"
67 | ]
68 | }
69 | ],
70 | "metadata": {
71 | "kernelspec": {
72 | "display_name": "Julia 1.2.0",
73 | "language": "julia",
74 | "name": "julia-1.2"
75 | },
76 | "language_info": {
77 | "file_extension": ".jl",
78 | "mimetype": "application/julia",
79 | "name": "julia",
80 | "version": "1.2.0"
81 | }
82 | },
83 | "nbformat": 4,
84 | "nbformat_minor": 1
85 | }
86 |
--------------------------------------------------------------------------------
/hw/Solutions/hw1soln.jl:
--------------------------------------------------------------------------------
1 | function isprime(n::Int64) #reimplement an isprime function (with type delcarations!)
2 | for p = 2:round(Int,sqrt(n)) #only need to check to sqrt(n)
3 | if n%p == 0
4 | return false
5 | end
6 | end
7 | return true
8 | end
9 |
10 |
11 | function my_sieve(n::Int64)
12 | out = []
13 | for i = 2:n
14 | if isprime(i)
15 | append!(out,i)
16 | end
17 | end
18 | return out
19 | end
20 |
21 | println("Solution 1: ", sum(my_sieve(10000)))
22 |
23 |
24 | function eratosthenes(n::Int64)
25 | out = fill(true,n) #we track the true-false values for whether the i^th number is prime: we will construct the sum of the primes less than 10000 later.
26 | out[1] = false # remove 1 from the set of primes
27 | for i = 2:round(Int,sqrt(n)) #only need to check to sqrt(n)
28 | if out[i]
29 | for index = 2*i:i:n
30 | out[index] = 0
31 | end
32 | end
33 | end
34 | return out
35 | end
36 |
37 |
38 | println("Solution 2: ", sum(collect(1:10000)[eratosthenes(10000)]))
39 | #Run these in IJulia to see which is really faster: both are bound primarily by compilation time.
40 |
41 | #Here are two solutions by students that I thought were particularly good.
42 |
43 | #Solution by Frederik Johan Mellbye
44 |
45 | function get_primes(N)
46 | A = ones(Bool, N-1)
47 | A[1] = false
48 |
49 | for i in 2:round(Int, sqrt(N-1))
50 | if A[i]
51 | for j in (i^2):i:N-1
52 | A[j] = false
53 | end
54 | end
55 | end
56 | return collect(1:N-1)[A]
57 | end
58 |
59 | function sum_primes(N)
60 | return sum(get_primes(N))
61 | end
62 |
63 | @show sum_primes(10000)
64 |
65 | #Solution by Sarah Hensley
66 |
67 | function sieve(N)
68 | maxValue = ceil(Int,sqrt(N))
69 | A = fill(true,(N))
70 | A[1] = false
71 | for i in 2:maxValue
72 | if A[i]
73 | for j in collect(i^2:i:N)
74 | A[j] = false
75 | end
76 | end
77 | end
78 | return A
79 | end
80 |
81 | @show sum(collect(1:10000)[sieve(10000)])
82 |
--------------------------------------------------------------------------------
/hw/hw1.md:
--------------------------------------------------------------------------------
1 | # CME 257 Homework 1 (Survey)
2 | Due Sunday 10/6 by 11:59pm
3 |
4 | Email your scripts to jmblpati@stanford.edu with subject "cme 257 hw 1 submission"
5 |
6 | The script should be in files named "lastname\_hw1\_p1.jl" "lastname\_hw1\_p2.jl" (replace "lastname" with your own last name). The scripts should run from the command line:
7 | ```bash
8 | julia lastname_hw1.jl
9 | ```
10 |
11 |
12 | You may also submit your submission as a Jupyter notebook. If you choose do to so please submit one notebook for both problems.
13 |
14 | If you find yourself struggling on either of these problems, please do not hesitate to contact me or come to office hours on Friday.
15 | ---
16 |
17 | ## Problem 1
18 | Write a Julia script that will print out your answers to the following survey. You don't need to repeat the question, but please enumerate the answers. It's OK if you don't have experience in everything asked about.
19 |
20 | 1. Your name
21 | 2. Program/year/other Stanford affiliation
22 | 3. Academic, Research, or Business interests
23 | 4. Why you want to take the course
24 | 5. Prior Julia experience
25 | 6. Programming experience - list languages with proficiency (1=novice, 2=intermediate, 3=experienced).
26 | 1. If you don't have any experience with at least one of R, Python, or MATLAB, make a note
27 | 2. If you don't have any experience with at least one of C, C++ or fortran, make a note
28 | 7. What operating system do you have?
29 | 8. How comfortable are you with the bash shell (0-3)
30 | 9. Have you ever used a version control system, such as git before?
31 | 10. What is one thing you'd really like to learn in this class?
32 | 11. Comments/concerns
33 |
34 | ## Problem 2
35 |
36 | Implement the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) to compute the set of all primes less than an integer N. Using this, compute the sum of all primes less than 10000. Keep in mind that 1 is not a prime. Do not use any dedicated prime-testing functions in Julia like ``isprime()``.
37 |
--------------------------------------------------------------------------------
/hw/hw2.md:
--------------------------------------------------------------------------------
1 | # CME 257 Homework 2
2 | Due Sunday 10/13 at 11:59 pm.
3 |
4 | Please submit the assignment in a IJulia notebook (.ipynb). Name your ipynb "lastname_hw2.ipynb". This assignment shouldn't take you more than 90 minutes.
5 |
6 | Email your .ipynb to jmblpati@stanford.edu with subject "cme 257 hw 2 submission"
7 |
8 | The first 4 parts should be fairly straightforward based on the material covered in the second class. Part 5 is a bonus question, meaning you should only do it if you find it interesting.
9 |
10 | ---
11 |
12 | In this homework you'll see a simple application of Julia's type system and multiple-dispatch functions to a common scientific computing problem: matrix multiplication.
13 |
14 | A matrix encodes a linear transformation between vector spaces. In scientific computing, they generally appear as a m x n array of numbers. Matrix-vector multiplication takes O(mn^2) time when the matrix is stored in this form, but some matrices have simple structure that we can use to make multiplication much faster. One example is a [diagonal matrix](https://en.wikipedia.org/wiki/Diagonal_matrix), which is zero everywhere but the diagonal, and will simply scale entries of a vector. Another is a [rank-1 matrix](https://en.wikipedia.org/wiki/Rank_(linear_algebra)), which can be stored as the outer product of two vectors. We'll use both of these in this assignment. To simplify things as much as possible, assume all matrices are square.
15 |
16 | ---
17 |
18 | * (Part 1) create an abstract cme257matrix type.
19 | * (Part 2) Here is some code to define a cme257rank1 type, and vector multiplication:
20 |
21 | ```julia
22 |
23 | struct cme257rank1{T} <: cme257matrix
24 | ## a rank-1 matrix type
25 | # A = u*v'
26 | # m is size of matrix
27 | u::Array{T, 1}
28 | v::Array{T, 1}
29 | m::Int64
30 | end
31 |
32 | function cme257rank1(u::Array{T, 1}, v::Array{T, 1}) where {T}
33 | # a special constructor for cme257rank1
34 | @assert length(u) == length(v) # we're only dealing with square matrices
35 | return cme257rank1(u, v, length(u))
36 | end
37 |
38 | import Base.*
39 | using LinearAlgebra
40 | function *(A::cme257rank1{T}, v::Array{T,1}) where {T}
41 | @assert A.m == length(v) # make sure vector is right size for matrix
42 | c = dot(A.v, v)
43 | return copy(A.u) * c
44 | end
45 | ```
46 | copy and paste the code, construct a cme257rank1 matrix, and verify that you can use it to multiply with a vector. The following functions might help:
47 |
48 | ~~~julia
49 | randn(10) # generates a random vector of length n
50 | ones(10) # generates a vector of all ones of length n
51 | ~~~
52 |
53 | * (Part 3) create a cme257diagonal type, which behaves like a diagonal matrix:
54 | 1. it should be parameterized by T
55 | 2. it should be a child of cme257matrix
56 | 3. it should have two fields: "d" which is a m x 1 array of entries on the diagonal, and "m" which is the size of the matrix.
57 | 4. create a special constructor for cme257diagonal which only takes a vector (m x 1 array), and infers m from its length. (See the lecture notes if you have questions about this.)
58 | 5. Add a method to the ```* ``` function which allows you to multiply a vector (m x 1 array) with a cme257diagonal matrix. remember to check that the size matches! (hint: " .* " behaves like it does in MATLAB)
59 |
60 | verify that your special constructor and matrix multiplication work as expected.
61 |
62 | (Aside) You essentially just implemented Julia's native Diagonal type. Look at the first few lines of code [here](https://github.com/JuliaLang/julia/blob/master/base/linalg/diagonal.jl). Julia has several [special matrix types](https://docs.julialang.org/en/latest/manual/linear-algebra/#Special-matrices-1), which you can use for better storage and better performance if your application allows.
63 |
64 | * (Part 4) Now, we're going to create a plot that compares how fast multiplication is with a regular matrix versus one of our special matrices.
65 | 1. Choose one of cme257diagonal or cme257rank1
66 | 2. Use PyPlot (or another plotting package) to plot the time it takes to complete multiplication for m in [10, 50, 100, 500, 1000] for both cme257diagonal and a full matrix.
67 |
68 | * to time something, wrap it with time() and compute the difference:
69 | ```julia
70 | old = time()
71 | # do something here
72 | t = time() - old # saves the time to do something
73 | ```
74 |
75 | * [bonus] (Part 5) Finding the top [eigenvector](https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors) of a matrix (by top eigenvector, I mean the eigenvector with largest magnitude eigenvalue). One method of computing the top eigenvector of a matrix is the [power iteration](https://en.wikipedia.org/wiki/Power_iteration). A version of the power iteration is written in pseudocode below
76 |
77 | ```
78 | Input:
79 | A = m x m matrix
80 | Output:
81 | v = m x 1 vector
82 | Alg:
83 | v = random m x 1 vector
84 | vprev = m x 1 vector of zeros
85 | while ( v != vprev)
86 | vprev = copy(v)
87 | v = A * vprev
88 | v = v/norm(v)
89 | end while
90 | return v
91 | ```
92 |
93 | write a single function that takes either of the cme257matrix children as input and uses the power iteration to compute the top eigenvector. To avoid some issues, just stick to symmetric matrices. Test it on 10 x 10 matrices.
94 |
95 | for the v != vprev check, try using
96 |
97 | ```julia
98 | !isapprox(v, vprev)
99 | ```
100 |
--------------------------------------------------------------------------------
/hw/hw3.md:
--------------------------------------------------------------------------------
1 | # Homework 3
2 |
3 | Due Sunday 11/3/19 by 11:59:59 pm.
4 |
5 | ---
6 |
7 | In this homework you'll create an IJulia notebook that explores a package, and add it to the class GitHub repository so that others can see what you discovered.
8 |
9 | ---
10 |
11 | ## Part 1 - Explore a Package
12 | Choose a Julia package that interests you. Create an IJulia notebook that introduces the class to the package, and contains the following:
13 | * What version of Julia you used when creating the notebook
14 | * What packages you use in the notebook (and any nonstandard information needed to run it)
15 | * Demonstrate some of the basic features of the package.
16 | * Include an interesting example or two (consider using interact, or create cool figure, or use it on a real world dataset)
17 | * Make sure to include commentary on what you are doing.
18 |
19 | ## Part 2 - Share Your Notebook
20 | Add your package to the course GitHub repository.
21 | * Create a GitHub account if you have not already done so
22 | * Fork the [course GitHub repository](https://github.com/icme/cme257-advanced-julia), and clone the fork on your computer.
23 | * Put your notebook in the [packages directory](https://github.com/icme/cme257-advanced-julia/tree/master/packages)
24 | * Name your notebook "packagename.ipynb" (where packagename is replaced by the package you explored)
25 | * commit your changes, push your changes to your fork to GitHub, then create a pull request to merge your changes to the original course repository
26 |
--------------------------------------------------------------------------------
/hw/hw4.md:
--------------------------------------------------------------------------------
1 | # CME 257 Homework 4
2 | Due Sunday 11/10 at 11:59 pm.
3 |
4 | Please submit the assignment in a IJulia notebook (.ipynb). Name your ipynb "lastname_hw4.ipynb". This assignment shouldn't take you more than 90 minutes.
5 |
6 | Email your .ipynb to jmblpati@stanford.edu with subject "cme 257 hw 4 submission"
7 |
8 | ---
9 |
10 | In this homework you'll implement a strange type of matrix multiplication in Julia, and use the techniques from lecture 5 to optimize your implementation. You will also make use of PyCall to call a Python package for doing graph theory, and finally use your strange matrix-vector product to compute distances in graphs.
11 |
12 | This assignment shouldn't take you more than 90 minutes.
13 |
14 | ---
15 |
16 | * (Part 1) We define the [*min-plus*](https://en.wikipedia.org/wiki/Min-plus_matrix_multiplication) on matrices as follows. Given two (n by n) matrices A and B, we want an output matrix C where C[i,j] = mink (A[i,k] + B[k,j]). In other words, to construct C[i,j] we take the ith row from A and the jth column from B, and find k to minimize A[i,k] + B[k,j]. Write a naive implementation of min-plus multiplication in Julia.
17 |
18 | * (Part 2) Using some of ^e tricks from Lecture 5, optimize your implementation of the min-plus product. Your code should use some of the vectorization macros, and you should experiment with pre-allocating memory. Keep in mind some of the things we discussed about the speed directly modifying the entries of an array versus using a local variable. Benchmark your implementations on random n by n integer matrices with n = 10,50,100,500,1000,2000.
19 |
20 | * (Part 3) Now, we will generate graph adjacency matrices using Python and PyCall. To make PyCall find packages installed in our system-wide python3 installation, we add to Julia's ENV list and rebuild Pycall. From the terminal, run the command
21 | ```bash
22 | which python3
23 | ```
24 | and take the output-- it should look like `/usr/local/bin/python3` or `/usr/bin/python3` on Mac or Linux. Run the following code
25 | ```julia
26 | using PyCall
27 | ENV["PYTHON"] = {Whatever string that you got fron the previous step}
28 | using Pkg
29 | Pkg.build("PyCall")
30 | ```
31 | and then restart your Julia notebook's kernel.
32 |
33 | * (Part 4) Using NetworkX, Numpy, and Scipy through PyCall, we will write a function which generates a random graph and returns its adjacency matrix and diameter. Here is some starter code.
34 | ```julia
35 | using PyCall
36 | nx = pyimport("networkx")
37 | np = pyimport("numpy")
38 | sp = pyimport("scipy.sparse")
39 | function generate_random_graph(n)
40 | G = nx.gnm_random_graph(n,5*n)
41 | return G
42 | end
43 | function adjacency_from_graph(G)
44 | *your code here*
45 | end
46 | ```
47 | So far, the above code returns a random Erdos-Renyi random graph G with n vertices and 5n edges. Extend the function `adjacency_from_graph` to return the adjacency matrix of G represented as a dense matrix.
48 | (**Hint**: You may find the functions `nx.adjacency_matrix()` and `sp.csr_matrix.todense()` to be helpful.)
49 |
50 | * (Part 5) Now, we're going to use our special min-plus product routine to compute the diameter of random graphs. Here is some Julia code to compute the diameter of random graphs generated by the code in part 3:
51 | ```julia
52 | g = nx.gnm_random_graph(n,n)
53 | function python_diameter(g)
54 | if nx.is_connected(g)
55 | return(nx.diameter(g))
56 | else
57 | return(g.number_of_nodes()+1)
58 | end
59 | end
60 | ```
61 | If A is the standard adjacency matrix of an n node graph, let B be the matrix where every 0 entry in A that is not on the diagonal is replaced with n+1. Hence if A was
62 | ```julia
63 |
64 | 0 1 0 0 1
65 | 1 0 1 0 1
66 | 0 1 0 1 1
67 | 0 0 1 0 0
68 | 1 1 1 0 0
69 | ```
70 | , B is
71 | ```julia
72 | 0 1 6 6 1
73 | 1 0 1 6 1
74 | 6 1 0 1 1
75 | 6 6 1 0 6
76 | 1 1 1 6 0
77 | ```
78 | Write a function to turn a normal adjacency matrix into this "modified" adjacency matrix. It turns out that if G is an n-node graph with adjacency matrix A and modified adjacency matrix B, the diameter of G is precisely the maximum value in the nth power of B (or n+1 if G is not connected). In other words, if we take B, copy it to another matrix C, and multiply B by C n times using the min-plus product, we can compute the diameter of G! Implement this using your optimized min-plus product implementation, and compare its output with `python_diameter`.
79 | (**Note**: it is important to use the min plus product here! Using normal matrix multiplication will just cause B to go off to infinity.)
80 |
81 | * (Part 6 (Bonus)) It also turns out that the min-plus product is associative. Thus, we can compute the nth min-plus power by [repeated squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring). Implement this in Julia, and compare its running time to the naive method in Part 5.
82 |
--------------------------------------------------------------------------------
/hw/hw5.md:
--------------------------------------------------------------------------------
1 | # CME 257 Homework 5
2 | Due Sunday 11/24 at 11:59 pm.
3 |
4 | Please submit the assignment in a IJulia notebook (.ipynb). Name your ipynb "lastname_hw5.ipynb". This assignment shouldn't take you more than 30 minutes.
5 |
6 | Email your .ipynb to jmblpati@stanford.edu with subject "cme 257 hw 5 submission"
7 |
8 | ---
9 |
10 | In this homework you'll implement some simple utility macros.
11 |
12 | * (Part 1) Julia lacks an `until` keyword in its basic functionality. This may be occasionally useful however: you may want to write code that loops until some invariant fails. For example, we may ask that the following code
13 | ```julia
14 | i = 1
15 | until i > 1000
16 | println(i)
17 | global i *=2
18 | end
19 | ```
20 | prints the powers of 2 less than 1000. In this exercise we will implement this macro. Fill in the below code snippet to implement the @until macro:
21 | ```julia
22 | macro until(condition,codeblock)
23 | return quote
24 | #your code here
25 | end
26 | end
27 | ```
28 | We recall that macros can (but do not have to) return expressions in Julia just like functions. However, as we would like to generate some code to run with our `@until` macro we would like to return an `Expr`. We do this by returning some code nested in a `quote` block: this is of `Expr` type. You may test your macro with
29 | ```julia
30 | i = 1
31 | @until i > 1000 begin
32 | println(i)
33 | global i *=2
34 | end
35 | ```
36 | Your macro should print the powers of 2 from 1 to 512 inclusive. (The syntax `begin... end` allows us to provide Julia with a block of code over multiple lines.) Be mindful of escaping the variable names `condition` and `codeblock`!
37 |
38 | * (Part 2) We'll now implement a macro to do _list comprehensions_. A list comprehension is a convenient way to call `map()` in Julia. You may know them from the `f.(x)` syntax:
39 | ```julia
40 | function f(x)
41 | x^2
42 | end
43 |
44 | f.(collect(0:10))
45 | ```
46 |
47 | This returns the list of perfect squares between 0 and 100. Implement a macro form of this by completing the code below
48 |
49 | ```julia
50 | macro lcomp(f,rangestart,rangeend)
51 | return quote
52 | #your code here
53 | end
54 | end
55 | ```
56 |
57 | Your may test your code with
58 | ```julia
59 | @lcomp f 0 10
60 | ```
61 | (A hint: you'll need to call the arguments `f, rangestart, rangeend` with `$`. You can query `f` at a point `i` with `$(f)(i)`. )
62 |
63 | * (Part 3) Implement a function `lcomp2` which is a function version of the `lcomp` macro from part 2. Using `f(x) = x^2`, compare the running times of
64 | 1. @lcomp f 0 10000
65 | 2. lcomp2(f,0,10000) without precompiling
66 | 3. lcomp2(f,0,10000) after precompiling
67 |
68 | Is there a significant performance difference? Write a brief explanation of any effect you observe.
69 |
70 |
--------------------------------------------------------------------------------
/hw/hw6.md:
--------------------------------------------------------------------------------
1 | # CME 257 Homework 6
2 | Due Friday 12/5 at 11:59 pm.
3 |
4 | Please submit the assignment in a IJulia notebook (.ipynb). Name your ipynb "lastname_hw5.ipynb". This assignment shouldn't take you more than 90 minutes.
5 |
6 | Email your .ipynb to jmblpati@stanford.edu with subject "cme 257 hw 6 submission"
7 |
8 | ---
9 |
10 | In this homework you'll use Julia 1.3.0 to write some multithreaded code for some classic algorithms.
11 |
12 | * (Part 0) Install Julia 1.3.0 on your machine, and start it with 4 threads. If you are having problems with this please let me know.
13 |
14 | * (Part 1) Here are two implementations of the discrete Fourier transform:
15 | ```julia
16 | function DFTslow(x)
17 | N = length(x)
18 | v = collect(0:N-1)
19 | M = exp.(-2*im*pi*v*v'/N)
20 | return M*x
21 | end
22 |
23 | function DFTfaster(x)
24 | N = length(x)
25 | if N%2 != 0
26 | println("N needs to be a power of 2")
27 | elseif N <= 32
28 | return DFTslow(x)
29 | end
30 | x_even = DFTfaster(x[1:2:end])
31 | x_odd = DFTfaster(x[2:2:end])
32 | f = exp.(-2*im*pi*collect(0:(div(N,2)-1))/N)
33 | y_even = x_even + f.*x_odd
34 | y_odd = x_even - f.*x_odd
35 | return vcat(y_even,y_odd)
36 | end
37 | ```
38 | Install the package `FFTW.jl` and verify that these implementations are correct. Try computing the DFT of a sum of sime waves to see if you can recover the linear combination of waves that they are composed of.
39 |
40 | * (Part 2) Modify `DFTfaster` to use multiple threads. Benchmark your implementation against `DFTfaster` and `FFTW.jl`'s implementation on random vectors.
41 |
42 | * (Part 3) Try using the `@simd` macro or thread-local storage to improve your code's memory usage and speed even more. Did it have any effect?
43 |
44 | * (Part 4) The [prefix sum](https://en.wikipedia.org/wiki/Prefix_sum) operation is a classic primitive in parallel algorithms. A serial implementation is found in Julia as `cumsum`. Implement either of the algorithms from [here](https://en.wikipedia.org/wiki/Prefix_sum#Parallel_algorithms) in Julia. If you use only one thread, how does your implementation compare to `cumsum`?
45 |
46 | * (Part 5) Write a multithreaded version of your prefix sum algorithm and compare it to `cumsum`. Try to optimize the point of tradeoff between recursing and running the serial algorithm: can you make your implementation faster than `cumsum`?
47 |
--------------------------------------------------------------------------------
/packages/2015/FiniteStateMachine.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#FiniteStateMachine.jl"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "This notebook was created by R. Davis Born using Julia 0.3.11 in order to demonstrate the functionality of the FiniteStateMachine Julia package created by Jack Peterson.\n",
15 | "\n",
16 | "FiniteStateMachine is the only package used in this notebook."
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 1,
22 | "metadata": {
23 | "collapsed": false
24 | },
25 | "outputs": [
26 | {
27 | "name": "stderr",
28 | "output_type": "stream",
29 | "text": [
30 | "INFO: Nothing to be done\n",
31 | "INFO: METADATA is out-of-date — you may not have the latest version of FiniteStateMachine\n",
32 | "INFO: Use `Pkg.update()` to get the latest versions of your packages\n"
33 | ]
34 | }
35 | ],
36 | "source": [
37 | "Pkg.add(\"FiniteStateMachine\")"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": 2,
43 | "metadata": {
44 | "collapsed": true
45 | },
46 | "outputs": [],
47 | "source": [
48 | "using FiniteStateMachine"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "##Basic Functionality"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "This package serves to create a simple finite state machine that can support any number of states and actions.\n",
63 | "\n",
64 | "Actions must have at least one \"from\" state, exactly one \"to\" state, and exactly one \"initial\" state."
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": 3,
70 | "metadata": {
71 | "collapsed": false
72 | },
73 | "outputs": [],
74 | "source": [
75 | "Ferry = state_machine({\n",
76 | " \"initial\" => \"EastBank\", # the initial state is where the machine starts when defined\n",
77 | " \"events\" => [{\n",
78 | " \"name\" => \"CrossRiver\", # the name of the action or \"event\"\n",
79 | " \"from\" => \"EastBank\", # the state the action can be taken from\n",
80 | " \"to\" => \"WestBank\", # the state the action leads to\n",
81 | " }, {\n",
82 | " \"name\" => \"CrossRiver\",\n",
83 | " \"from\" => \"WestBank\",\n",
84 | " \"to\" => \"EastBank\",\n",
85 | " }]\n",
86 | " })\n",
87 | ";"
88 | ]
89 | },
90 | {
91 | "cell_type": "markdown",
92 | "metadata": {},
93 | "source": [
94 | "The state can be changed by imposing an action (or \"event\") with fire(). See the current state with state_maching.current"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": 4,
100 | "metadata": {
101 | "collapsed": false,
102 | "scrolled": true
103 | },
104 | "outputs": [
105 | {
106 | "name": "stdout",
107 | "output_type": "stream",
108 | "text": [
109 | "Ferry.current => \"EastBank\"\n",
110 | "Ferry.current => \"WestBank\"\n"
111 | ]
112 | }
113 | ],
114 | "source": [
115 | "@show Ferry.current\n",
116 | "fire(Ferry,\"CrossRiver\")\n",
117 | "@show Ferry.current\n",
118 | ";"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "metadata": {},
124 | "source": [
125 | "###Multiple \"from\" states and \"final\" states"
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "metadata": {},
131 | "source": [
132 | "State machines support multiple \"from\" states, and you have the option to define a \"final\" state that will throw a flag indicating that the state machine has \"finished\". Check the status of this flag with fire(state_machine, \"finished\"). Note that it is possible to define an action that can move away from the final state and flip the flag back to false."
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 5,
138 | "metadata": {
139 | "collapsed": false
140 | },
141 | "outputs": [],
142 | "source": [
143 | "Ferry = state_machine({\n",
144 | " \"initial\" => \"Dock\",\n",
145 | " \"final\" => \"RiverBottom\", # the final state indicates the state machine has \"finished\"\n",
146 | " \"events\" => [{\n",
147 | " \"name\" => \"Launch\",\n",
148 | " \"from\" => \"Dock\",\n",
149 | " \"to\" => \"EastBank\",\n",
150 | " }, {\n",
151 | " \"name\" => \"CrossRiver\",\n",
152 | " \"from\" => \"EastBank\",\n",
153 | " \"to\" => \"WestBank\",\n",
154 | " }, {\n",
155 | " \"name\" => \"CrossRiver\",\n",
156 | " \"from\" => \"WestBank\",\n",
157 | " \"to\" => \"EastBank\",\n",
158 | " }, {\n",
159 | " \"name\" => \"Sink\",\n",
160 | " \"from\" => [\"EastBank\", \"WestBank\"],\n",
161 | " \"to\" => \"RiverBottom\",\n",
162 | " }]\n",
163 | " })\n",
164 | ";"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": 6,
170 | "metadata": {
171 | "collapsed": false
172 | },
173 | "outputs": [
174 | {
175 | "name": "stdout",
176 | "output_type": "stream",
177 | "text": [
178 | "Ferry.current => \"Dock\"\n",
179 | "Ferry.current => \"EastBank\"\n",
180 | "fire(Ferry,\"finished\") => false\n",
181 | "Ferry.current => \"RiverBottom\"\n",
182 | "fire(Ferry,\"finished\") => true\n"
183 | ]
184 | }
185 | ],
186 | "source": [
187 | "@show Ferry.current # Ferry starts at the Dock\n",
188 | "fire(Ferry,\"Launch\")\n",
189 | "@show Ferry.current # Ferry launches to the East Bank\n",
190 | "@show fire(Ferry, \"finished\") # Ferry is at East Bank, which is not the final state\n",
191 | "fire(Ferry,\"Sink\")\n",
192 | "@show Ferry.current # Ferry sunk to the Bottom of the River\n",
193 | "@show fire(Ferry, \"finished\") # Ferry is at the Bottom of the River, which is the final state\n",
194 | ";"
195 | ]
196 | },
197 | {
198 | "cell_type": "markdown",
199 | "metadata": {},
200 | "source": [
201 | "##A Game for You..."
202 | ]
203 | },
204 | {
205 | "cell_type": "markdown",
206 | "metadata": {},
207 | "source": [
208 | "Enjoy this simple Monty Hall simulation implemented with FiniteStateMachine :)"
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": 5,
214 | "metadata": {
215 | "collapsed": false
216 | },
217 | "outputs": [],
218 | "source": [
219 | "MontyHall = state_machine({\n",
220 | " \"initial\" => \"begin\",\n",
221 | " \"final\" => \"end\",\n",
222 | " \"events\" => [{\n",
223 | " \"name\" => \"GuessRight\",\n",
224 | " \"from\" => [\"begin\",\"wrong\"],\n",
225 | " \"to\" => \"right\",\n",
226 | " }, {\n",
227 | " \"name\" => \"GuessWrong\",\n",
228 | " \"from\" => [\"begin\",\"right\"],\n",
229 | " \"to\" => \"wrong\",\n",
230 | " }, {\n",
231 | " \"name\" => \"switch\",\n",
232 | " \"from\" => \"right\",\n",
233 | " \"to\" => \"lose\",\n",
234 | " }, {\n",
235 | " \"name\" => \"switch\",\n",
236 | " \"from\" => \"wrong\",\n",
237 | " \"to\" => \"win\",\n",
238 | " }, {\n",
239 | " \"name\" => \"stay\",\n",
240 | " \"from\" => \"right\",\n",
241 | " \"to\" => \"win\",\n",
242 | " }, {\n",
243 | " \"name\" => \"stay\",\n",
244 | " \"from\" => \"wrong\",\n",
245 | " \"to\" => \"lose\",\n",
246 | " }, {\n",
247 | " \"name\" => \"start\",\n",
248 | " \"from\" => [\"right\", \"wrong\", \"win\", \"lose\", \"end\", \"begin\"],\n",
249 | " \"to\" => \"begin\",\n",
250 | " }, {\n",
251 | " \"name\" => \"yes\",\n",
252 | " \"from\" => [\"win\", \"lose\"],\n",
253 | " \"to\" => \"begin\",\n",
254 | " }, {\n",
255 | " \"name\" => \"no\",\n",
256 | " \"from\" => [\"win\", \"lose\"],\n",
257 | " \"to\" => \"end\",\n",
258 | " }]\n",
259 | " })\n",
260 | "\n",
261 | "function MontyHallSetup()\n",
262 | " winner = int(ceil(3*rand()))\n",
263 | " doors = Array(String,3)\n",
264 | " for i = 1:length(doors)\n",
265 | " doors[i] = \"GOAT\"\n",
266 | " end\n",
267 | " doors[winner] = \"CAR\"\n",
268 | " return doors, winner\n",
269 | "end\n",
270 | ";"
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "execution_count": 6,
276 | "metadata": {
277 | "collapsed": false
278 | },
279 | "outputs": [
280 | {
281 | "name": "stdout",
282 | "output_type": "stream",
283 | "text": [
284 | "Welcome to the Monty Hall Game Show!\n",
285 | "We have three doors for you to choose from. Two doors hide goats, and one door hides a NEW CAR!!\n",
286 | "You will guess which door hides the car, and then one of the other two doors will be shown to hide a goat...\n",
287 | "You will then be given the opportunity to switch your guess to the other unopened door...\n",
288 | "If your final guess is correct, you win the car!!\n",
289 | "\n",
290 | "Go ahead and make your first guess! Type 1, 2, or 3:\n",
291 | "STDIN> 1\n",
292 | "\n",
293 | "We will now reveal one of the losing doors...\n",
294 | "Door 2 hides a GOAT! Would you like to switch or stay?\n",
295 | "STDIN> switch\n",
296 | "\n",
297 | "Sorry! You lose! Door 1 hid the car.\n",
298 | "Enjoy your new pet goat. Would you like to play again (yes/no)?\n",
299 | "STDIN> yes\n",
300 | "\n",
301 | "Go ahead and make your first guess! Type 1, 2, or 3:\n",
302 | "STDIN> 2\n",
303 | "\n",
304 | "We will now reveal one of the losing doors...\n",
305 | "Door 1 hides a GOAT! Would you like to switch or stay?\n",
306 | "STDIN> stay\n",
307 | "\n",
308 | "Congratulations! You win! Door 2 hides the car!\n",
309 | "Pick up your keys on your way out. Would you like to play again (yes/no)?\n",
310 | "STDIN> no\n",
311 | "\n",
312 | "Thanks for playing! Goodbye.\n"
313 | ]
314 | }
315 | ],
316 | "source": [
317 | "fire(MontyHall, \"start\")\n",
318 | "\n",
319 | "println(\"Welcome to the Monty Hall Game Show!\")\n",
320 | "println(\"We have three doors for you to choose from. Two doors hide goats, and one door hides a NEW CAR!!\")\n",
321 | "println(\"You will guess which door hides the car, and then one of the other two doors will be shown to hide a goat...\")\n",
322 | "println(\"You will then be given the opportunity to switch your guess to the other unopened door...\")\n",
323 | "println(\"If your final guess is correct, you win the car!!\")\n",
324 | "\n",
325 | "while !fire(MontyHall, \"finished\")\n",
326 | " if MontyHall.current == \"begin\"\n",
327 | " (doors, winner) = MontyHallSetup()\n",
328 | " println()\n",
329 | " println(\"Go ahead and make your first guess! Type 1, 2, or 3:\")\n",
330 | " guess = readline(STDIN)\n",
331 | " if guess == string(winner)\n",
332 | " fire(MontyHall, \"GuessRight\")\n",
333 | " else\n",
334 | " fire(MontyHall, \"GuessWrong\")\n",
335 | " end\n",
336 | " elseif MontyHall.current == \"right\" || MontyHall.current == \"wrong\"\n",
337 | " reveal = int(ceil(3*rand()))\n",
338 | " while reveal == winner || string(reveal) == guess\n",
339 | " reveal = int(ceil(3*rand()))\n",
340 | " end\n",
341 | " println()\n",
342 | " println(\"We will now reveal one of the losing doors...\")\n",
343 | " println(\"Door \", reveal, \" hides a GOAT! Would you like to switch or stay?\")\n",
344 | " fire(MontyHall, readline(STDIN))\n",
345 | " elseif MontyHall.current == \"win\"\n",
346 | " println()\n",
347 | " println(\"Congratulations! You win! Door \", winner, \" hides the car!\")\n",
348 | " println(\"Pick up your keys on your way out. Would you like to play again (yes/no)?\")\n",
349 | " fire(MontyHall, readline(STDIN))\n",
350 | " elseif MontyHall.current == \"lose\"\n",
351 | " println()\n",
352 | " println(\"Sorry! You lose! Door \", winner, \" hid the car.\")\n",
353 | " println(\"Enjoy your new pet goat. Would you like to play again (yes/no)?\")\n",
354 | " fire(MontyHall, readline(STDIN))\n",
355 | " end\n",
356 | "end\n",
357 | "\n",
358 | "println()\n",
359 | "println(\"Thanks for playing! Goodbye.\")"
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": null,
365 | "metadata": {
366 | "collapsed": true
367 | },
368 | "outputs": [],
369 | "source": []
370 | }
371 | ],
372 | "metadata": {
373 | "kernelspec": {
374 | "display_name": "Julia 0.3.11",
375 | "language": "julia",
376 | "name": "julia-0.3"
377 | },
378 | "language_info": {
379 | "file_extension": ".jl",
380 | "mimetype": "application/julia",
381 | "name": "julia",
382 | "version": "0.3.11"
383 | }
384 | },
385 | "nbformat": 4,
386 | "nbformat_minor": 0
387 | }
388 |
--------------------------------------------------------------------------------
/packages/2015/JuMP.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 6,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [
10 | {
11 | "name": "stderr",
12 | "output_type": "stream",
13 | "text": [
14 | "INFO: Nothing to be done\n",
15 | "INFO: Nothing to be done\n",
16 | "INFO: Cloning cache of FactCheck from git://github.com/JuliaLang/FactCheck.jl.git\n",
17 | "INFO: Cloning cache of Interact from git://github.com/JuliaLang/Interact.jl.git\n",
18 | "INFO: Cloning cache of Reactive from git://github.com/JuliaLang/Reactive.jl.git\n",
19 | "INFO: Installing FactCheck v0.4.1\n",
20 | "INFO: Installing Interact v0.2.1\n",
21 | "INFO: Installing Reactive v0.2.4\n",
22 | "INFO: Package database updated\n"
23 | ]
24 | }
25 | ],
26 | "source": [
27 | "Pkg.add(\"Ipopt\")\n",
28 | "Pkg.add(\"JuMP\")\n",
29 | "Pkg.add(\"Interact\")"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 22,
35 | "metadata": {
36 | "collapsed": false
37 | },
38 | "outputs": [
39 | {
40 | "ename": "LoadError",
41 | "evalue": "LoadError: ArgumentError: Gadfly not found in path\nwhile loading In[22], in expression starting on line 1",
42 | "output_type": "error",
43 | "traceback": [
44 | "LoadError: ArgumentError: Gadfly not found in path\nwhile loading In[22], in expression starting on line 1",
45 | "",
46 | " in require at loading.jl:233"
47 | ]
48 | }
49 | ],
50 | "source": []
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 25,
55 | "metadata": {
56 | "collapsed": false
57 | },
58 | "outputs": [
59 | {
60 | "data": {
61 | "text/html": [],
62 | "text/plain": [
63 | "Interact.Slider{Int64}([Reactive.Input{Int64}] 2,\"upper_bound\",2,0:5)"
64 | ]
65 | },
66 | "metadata": {},
67 | "output_type": "display_data"
68 | },
69 | {
70 | "data": {
71 | "text/plain": [
72 | "\"Optimal solution: x = 2.0 y = 0.20000000199712123\""
73 | ]
74 | },
75 | "execution_count": 25,
76 | "metadata": {
77 | "comm_id": "d2475be7-0cbb-4b41-8759-c53999043c30",
78 | "reactive": true
79 | },
80 | "output_type": "execute_result"
81 | }
82 | ],
83 | "source": [
84 | "using Ipopt\n",
85 | "using JuMP\n",
86 | "using Interact\n",
87 | "\n",
88 | "# solve a linear program with different upper bounds on the x variable \n",
89 | "#(sorry this is a boring example but I am very pushed for time)\n",
90 | "@manipulate for upper_bound = 0:5\n",
91 | " m = Model(solver=IpoptSolver())\n",
92 | " @defVar(m, 0 <= x <= upper_bound )\n",
93 | " @defVar(m, 0 <= y <= 30 )\n",
94 | "\n",
95 | " @setObjective(m, Max, 5x + 3*y )\n",
96 | " @addConstraint(m, 1x + 5y <= 3.0 )\n",
97 | " \n",
98 | " status = solve(m)\n",
99 | " \n",
100 | " return \"Optimal solution: x = \" * string(getValue(x)) * \" y = \" * string(getValue(y))\n",
101 | "end"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": null,
107 | "metadata": {
108 | "collapsed": true
109 | },
110 | "outputs": [],
111 | "source": []
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {
117 | "collapsed": true
118 | },
119 | "outputs": [],
120 | "source": []
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": null,
125 | "metadata": {
126 | "collapsed": true
127 | },
128 | "outputs": [],
129 | "source": []
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": null,
134 | "metadata": {
135 | "collapsed": true
136 | },
137 | "outputs": [],
138 | "source": []
139 | }
140 | ],
141 | "metadata": {
142 | "kernelspec": {
143 | "display_name": "Julia 0.4.0",
144 | "language": "julia",
145 | "name": "julia-0.4"
146 | },
147 | "language_info": {
148 | "file_extension": ".jl",
149 | "mimetype": "application/julia",
150 | "name": "julia",
151 | "version": "0.4.0"
152 | }
153 | },
154 | "nbformat": 4,
155 | "nbformat_minor": 0
156 | }
157 |
--------------------------------------------------------------------------------
/packages/2015/LightGraphs.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "source": [
11 | "using LightGraphs\n",
12 | "#Graphs.jl is absolutely terrible; you can't even remove edges!\n",
13 | "#Arun Jambulapati, HW #3, CME 257 submission: LightGraphs"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": 2,
19 | "metadata": {
20 | "collapsed": false
21 | },
22 | "outputs": [
23 | {
24 | "name": "stdout",
25 | "output_type": "stream",
26 | "text": [
27 | "edge 4 - 3\n",
28 | "edge 1 - 4\n",
29 | "edge 9 - 5\n",
30 | "edge 5 - 4\n",
31 | "edge 10 - 5\n",
32 | "edge 3 - 6\n",
33 | "edge 8 - 3\n",
34 | "edge 3 - 5\n",
35 | "edge 8 - 6\n",
36 | "edge 6 - 4\n",
37 | "edge 4 - 9\n",
38 | "edge 8 - 5\n",
39 | "edge 7 - 1\n",
40 | "edge 8 - 9\n",
41 | "edge 2 - 7\n",
42 | "edge 9 - 7\n",
43 | "edge 10 - 3\n",
44 | "edge 6 - 7\n",
45 | "edge 6 - 9\n",
46 | "edge 8 - 1\n",
47 | "edge 3 - 9\n",
48 | "edge 1 - 10\n",
49 | "edge 5 - 6\n",
50 | "edge 2 - 8\n",
51 | "edge 7 - 8\n",
52 | "edge 10 - 8\n",
53 | "edge 6 - 10\n",
54 | "edge 1 - 9\n",
55 | "edge 1 - 3\n",
56 | "edge 4 - 8\n",
57 | "edge 4 - 10\n",
58 | "edge 4 - 2\n",
59 | "Neighbors of 4: [8,2,6,1,9,5,3,10]\n",
60 | "Distances from 4: [1,1,1,0,1,1,2,1,1,1]\n",
61 | "Mincut: (Bool[true,false,true,true,true,true,true,true,true,true],3)\n"
62 | ]
63 | }
64 | ],
65 | "source": [
66 | "#brief exhibit of the features of this package\n",
67 | "\n",
68 | "test = Graph(10)\n",
69 | "#create a 10 node graph\n",
70 | "test1 = Graph(10, 32)\n",
71 | "#create another 10 node graph with 32 randomly chosen edges\n",
72 | "\n",
73 | "add_edge!(test, 4, 5)\n",
74 | "#add an edge between 4 and 5\n",
75 | "\n",
76 | "for i in edges(test1)\n",
77 | " println(i)\n",
78 | "end\n",
79 | "#print the edges of test\n",
80 | "rem_edge!(test, 4, 5)\n",
81 | "#remove an edge from test\n",
82 | "\n",
83 | "print(\"Neighbors of 4: \")\n",
84 | "println(neighbors(test1, 4))\n",
85 | "#print the neighborhood of vertex 4\n",
86 | "\n",
87 | "#compute the shortest paths from vertex 4 to all other nodes\n",
88 | "print(\"Distances from 4: \")\n",
89 | "println(dijkstra_shortest_paths(test1, 4).dists)\n",
90 | "\n",
91 | "#set test to test1's complement\n",
92 | "test = complement(test1)\n",
93 | "\n",
94 | "#mincut of test1\n",
95 | "print(\"Mincut: \")\n",
96 | "println(mincut(test1))\n",
97 | "\n"
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": 3,
103 | "metadata": {
104 | "collapsed": false
105 | },
106 | "outputs": [
107 | {
108 | "data": {
109 | "text/plain": [
110 | "281"
111 | ]
112 | },
113 | "execution_count": 3,
114 | "metadata": {},
115 | "output_type": "execute_result"
116 | }
117 | ],
118 | "source": [
119 | "n = 250000\n",
120 | "p_1 = .00001\n",
121 | "p_2 = .002\n",
122 | "G = erdos_renyi(n, p_1, is_directed=false)\n",
123 | "H = erdos_renyi(round(Int,3*sqrt(n)), p_2, is_directed=false)\n",
124 | "G = union(G,H)\n",
125 | "#generate a random graph with a large set of relatively sparse edges and a smaller dense cluster of edges\n",
126 | "threshold = round(Int, .5*(length(edges(G)))^.5)\n",
127 | "#triangle detection low-degree high-degree threshold\n",
128 | "#changing the probabilities and graphs if you want to test different densities and sizes\n",
129 | "#generating this graph takes a while due to the number of random bits required; a better test graph would require\n",
130 | "#much more work\n",
131 | "#the reason this takes so long is because to generate the several billion random bits required to create the graph, \n",
132 | "#we have to do several cpu cycles to get each of them\n",
133 | "#if this takes too long, just change the graph size to something smaller"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": 4,
139 | "metadata": {
140 | "collapsed": false
141 | },
142 | "outputs": [
143 | {
144 | "name": "stdout",
145 | "output_type": "stream",
146 | "text": [
147 | "(156,709,979)"
148 | ]
149 | }
150 | ],
151 | "source": [
152 | "neighborhoods = Dict()\n",
153 | "inducing = []\n",
154 | "for i in vertices(G)\n",
155 | " nb = neighbors(G, i)\n",
156 | " c = length(nb)\n",
157 | " if c >= threshold\n",
158 | " push!(inducing, i)\n",
159 | " end\n",
160 | " neighborhoods[i] = nb\n",
161 | "end\n",
162 | "#create a dictionary of the neighborhoods of the graph; a better implementation is possible but this works OK\n",
163 | "hideg_graph = induced_subgraph(G, inducing)\n",
164 | "#create an induced subgraph for the high degree nodes\n",
165 | "\n",
166 | "function sparsetrianglefind(G)\n",
167 | " for i in edges(G)\n",
168 | " a = neighborhoods[i.first]\n",
169 | " b = neighborhoods[i.second]\n",
170 | " c = length(a)\n",
171 | " d = length(b)\n",
172 | " if c > threshold && d > threshold\n",
173 | " \n",
174 | " else \n",
175 | " if c >= d\n",
176 | " for j in b\n",
177 | " if in(j,a)\n",
178 | " return i.first, i.second, j\n",
179 | " end\n",
180 | " end\n",
181 | " else\n",
182 | " for j in a\n",
183 | " if in(j,b)\n",
184 | " return i.first, i.second, j\n",
185 | " end\n",
186 | " end\n",
187 | " end\n",
188 | " end\n",
189 | " end\n",
190 | " return 0\n",
191 | "end\n",
192 | "#over the sparse graph components, test every edge's endpoints' neighborhood and look for a node in common\n",
193 | "#we skip the dense edges for this\n",
194 | "\n",
195 | "function densetrianglefind(hideg_graph)\n",
196 | " A = adjacency_matrix(hideg_graph)\n",
197 | " B = A*A\n",
198 | " for i = 1:size(A,1)\n",
199 | " for j = 1:size(A,1)\n",
200 | " if A[i,j] > 0 && B[i,j] > 0\n",
201 | " a = neighborhoods[i]\n",
202 | " b = neighborhoods[j]\n",
203 | " for k in b\n",
204 | " if in(k,a)\n",
205 | " return inducing[i], inducing[j], inducing[k]\n",
206 | " end\n",
207 | " end\n",
208 | " end\n",
209 | " end\n",
210 | " end\n",
211 | " return \"No Triangles\"\n",
212 | "end\n",
213 | "#for the dense part, look at the graph induced over the dense nodes and do a search by matrix multiplication\n",
214 | "#we know that i and j form a triangle with another vertex k iff A_i,j is nonzero and A^2_i,j is nonzero,\n",
215 | "#where A is the adjacency matrix. Once such i and j are found, we check their neighborhoods for an intersection.\n",
216 | "\n",
217 | "function trianglefind(G, hideg_graph)\n",
218 | " a = sparsetrianglefind(G)\n",
219 | " if a == 0\n",
220 | " return densetrianglefind(hideg_graph)\n",
221 | " else\n",
222 | " return a \n",
223 | " end\n",
224 | "end\n",
225 | "#we first check the sparse componment; if no edge is found, check the dense component.\n",
226 | "\n",
227 | "tic()\n",
228 | "a = trianglefind(G, hideg_graph)\n",
229 | "t = toq()\n",
230 | "#test on our hybrid graph\n",
231 | "\n",
232 | "print(a)\n"
233 | ]
234 | },
235 | {
236 | "cell_type": "code",
237 | "execution_count": 5,
238 | "metadata": {
239 | "collapsed": false
240 | },
241 | "outputs": [
242 | {
243 | "name": "stdout",
244 | "output_type": "stream",
245 | "text": [
246 | "Time Elapsed: 0.190297998\n"
247 | ]
248 | }
249 | ],
250 | "source": [
251 | "print(\"Time Elapsed: \")\n",
252 | "println(t)\n",
253 | "#even on graphs of a quarter million nodes, this algorithm runs in under a fifth of a second"
254 | ]
255 | }
256 | ],
257 | "metadata": {
258 | "kernelspec": {
259 | "display_name": "Julia 0.4.0-rc3",
260 | "language": "julia",
261 | "name": "julia-0.4"
262 | },
263 | "language_info": {
264 | "file_extension": ".jl",
265 | "mimetype": "application/julia",
266 | "name": "julia",
267 | "version": "0.4.0"
268 | }
269 | },
270 | "nbformat": 4,
271 | "nbformat_minor": 0
272 | }
273 |
--------------------------------------------------------------------------------
/packages/2015/asciiplot.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 21,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [
10 | {
11 | "data": {
12 | "text/html": [],
13 | "text/plain": [
14 | "Interact.Slider{Float64}([Reactive.Input{Float64}] 3.141592653589793,\"ϕ\",3.141592653589793,0.0:0.39269908169872414:6.283185307179586)"
15 | ]
16 | },
17 | "metadata": {},
18 | "output_type": "display_data"
19 | },
20 | {
21 | "data": {
22 | "text/html": [],
23 | "text/plain": [
24 | "Interact.Options{:ToggleButtons,Function}([Reactive.Input{Function}] sin,\"f\",sin,\"sin\",Interact.OptionDict(Any[\"sin\",\"cos\"],Dict{Any,Any}(\"cos\"=>cos,\"sin\"=>sin)),Any[],Any[])"
25 | ]
26 | },
27 | "metadata": {},
28 | "output_type": "display_data"
29 | },
30 | {
31 | "data": {
32 | "text/plain": [
33 | "\n",
34 | "\n",
35 | "\t. . . . . + + + + + + + + + + + + # # # \n",
36 | "\t. . . . . . . . + + + + + # # # # # # # \n",
37 | "\t . . . . . . + + + + # # # # # # # # \n",
38 | "\t . . . . . + + # # # # # @#@#@#@#\n",
39 | "\t . . . . + + # # # # @#@#@#@#@#\n",
40 | "\t . . . + + # # # @#@#@#@#@#@#\n",
41 | "\t . . . + + # # @#@#@#@#@#@#@#\n",
42 | "\t . . + # # # @#@#@#@#@#@#@#\n",
43 | "\t . . + # # # @#@#@#@#@#@#@#\n",
44 | "\t . . + # # # @#@#@#@#@#@#@#\n",
45 | "\t . . + # # # @#@#@#@#@#@#@#\n",
46 | "\t . . + # # # @#@#@#@#@#@#@#\n",
47 | "\t . . . + + # # @#@#@#@#@#@#@#\n",
48 | "\t . . . + + # # # @#@#@#@#@#@#\n",
49 | "\t . . . . + + # # # # @#@#@#@#@#\n",
50 | "\t . . . . . + + # # # # # @#@#@#@#\n",
51 | "\t . . . . . . + + + + # # # # # # # # \n",
52 | "\t. . . . . . . . + + + + # # # # # # # # \n",
53 | "\t. . . . . . + + + + + + + + + + + # # # \n",
54 | "\t+ + + + + + + + + + + + + + + + + + + + \n",
55 | "\n"
56 | ]
57 | },
58 | "execution_count": 21,
59 | "metadata": {
60 | "comm_id": "3664ed4d-c2ce-487e-b185-bef9af6f8bc9",
61 | "reactive": true
62 | },
63 | "output_type": "execute_result"
64 | }
65 | ],
66 | "source": [
67 | "# CME 257 - Assignment 2\n",
68 | "# - Julia v0.5\n",
69 | "# - ASCIIPlots is just a package for plotting things with ASCII characters\n",
70 | "# - You can make a heatmap plot using imagesc\n",
71 | "\n",
72 | "# Pkg.add(\"ASCIIPlots\")\n",
73 | "\n",
74 | "using ASCIIPlots\n",
75 | "using Interact\n",
76 | "\n",
77 | "n = 20\n",
78 | "vals = zeros(n,n)\n",
79 | "\n",
80 | "@manipulate for ϕ=0:π/8:2π, f=[:sin => sin, :cos => cos]\n",
81 | " for x = 1:n\n",
82 | " for y = 1:n\n",
83 | " vals[x,y] = sin(x*π/n)*cos(y*π/n + ϕ)\n",
84 | " end\n",
85 | " end\n",
86 | " \n",
87 | " imagesc(vals)\n",
88 | "end\n",
89 | "\n"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 19,
95 | "metadata": {
96 | "collapsed": false
97 | },
98 | "outputs": [
99 | {
100 | "data": {
101 | "text/html": [],
102 | "text/plain": [
103 | "Interact.Slider{Float64}([Reactive.Input{Float64}] 3.141592653589793,\"ϕ\",3.141592653589793,0.0:0.39269908169872414:6.283185307179586)"
104 | ]
105 | },
106 | "metadata": {},
107 | "output_type": "display_data"
108 | },
109 | {
110 | "data": {
111 | "text/html": [],
112 | "text/plain": [
113 | "Interact.Options{:ToggleButtons,Function}([Reactive.Input{Function}] sin,\"f\",sin,\"sin\",Interact.OptionDict(Any[\"sin\",\"cos\"],Dict{Any,Any}(\"cos\"=>cos,\"sin\"=>sin)),Any[],Any[])"
114 | ]
115 | },
116 | "metadata": {},
117 | "output_type": "display_data"
118 | },
119 | {
120 | "data": {
121 | "text/plain": [
122 | "\n",
123 | "\t-------------------------------------------------------------\n",
124 | "\t| -| 1.00\n",
125 | "\t| - - |\n",
126 | "\t| / / |\n",
127 | "\t| / |\n",
128 | "\t| / |\n",
129 | "\t| |\n",
130 | "\t| / |\n",
131 | "\t| / |\n",
132 | "\t| |\n",
133 | "\t| / |\n",
134 | "\t| / |\n",
135 | "\t| |\n",
136 | "\t| / |\n",
137 | "\t| / |\n",
138 | "\t| / |\n",
139 | "\t| |\n",
140 | "\t| / |\n",
141 | "\t| / |\n",
142 | "\t| / |\n",
143 | "\t|- / / | -0.99\n",
144 | "\t-------------------------------------------------------------\n",
145 | "\t1.00 20.00\n"
146 | ]
147 | },
148 | "execution_count": 19,
149 | "metadata": {
150 | "comm_id": "ac8b1c31-b51d-49c6-a9d8-ce024386d9c8",
151 | "reactive": true
152 | },
153 | "output_type": "execute_result"
154 | }
155 | ],
156 | "source": [
157 | "# - Lineplots can also be made\n",
158 | "\n",
159 | "@manipulate for ϕ=0:π/8:2π, f=[:sin => sin, :cos => cos]\n",
160 | " for x = 1:n\n",
161 | " for y = 1:n\n",
162 | " vals[x,y] = sin(x*π/n)*cos(y*π/n + ϕ)\n",
163 | " end\n",
164 | " end\n",
165 | "\n",
166 | " lineplot(1:n, vals[n/2,:])\n",
167 | "end"
168 | ]
169 | }
170 | ],
171 | "metadata": {
172 | "kernelspec": {
173 | "display_name": "Julia 0.5.0-dev",
174 | "language": "julia",
175 | "name": "julia-0.5"
176 | },
177 | "language_info": {
178 | "file_extension": ".jl",
179 | "mimetype": "application/julia",
180 | "name": "julia",
181 | "version": "0.5.0"
182 | }
183 | },
184 | "nbformat": 4,
185 | "nbformat_minor": 0
186 | }
187 |
--------------------------------------------------------------------------------
/packages/2015/faker.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 4,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [
10 | {
11 | "name": "stdout",
12 | "output_type": "stream",
13 | "text": [
14 | "Julia Version 0.4.0\n",
15 | "Commit 0ff703b* (2015-10-08 06:20 UTC)\n",
16 | "Platform Info:\n",
17 | " System: Windows (x86_64-w64-mingw32)\n",
18 | " CPU: Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz\n",
19 | " WORD_SIZE: 64\n",
20 | " BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Prescott)\n",
21 | " LAPACK: libopenblas64_\n",
22 | " LIBM: libopenlibm\n",
23 | " LLVM: libLLVM-3.3\n"
24 | ]
25 | }
26 | ],
27 | "source": [
28 | "#The version of using I am using to create the notebok:\n",
29 | "versioninfo()"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 8,
35 | "metadata": {
36 | "collapsed": false
37 | },
38 | "outputs": [],
39 | "source": [
40 | "#The package I chose to explore was called Faker\n",
41 | "#The purpose of this package is to randomly generate different\n",
42 | "#types of fake data.\n",
43 | "\n",
44 | "#Faker only seems to require itself\n",
45 | "using Faker"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 9,
51 | "metadata": {
52 | "collapsed": true
53 | },
54 | "outputs": [],
55 | "source": [
56 | "#The package has a whole slew of of different data types that it can\n",
57 | "#randomly generate for the users. Some of the different types are:\n",
58 | "\n",
59 | "#Letters/numbers\n",
60 | "#Addresses\n",
61 | "#Company names/information\n",
62 | "#Dates\n",
63 | "#E-mail and internet url's\n",
64 | "#Work, sentences, paragraphs\n",
65 | "#Phone numbers\n",
66 | "#Credit cards information\n",
67 | "#Colors\n",
68 | "#File names/extenstions\n",
69 | "#Personal profiles"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 19,
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [
79 | {
80 | "name": "stdout",
81 | "output_type": "stream",
82 | "text": [
83 | "Faker.random_digit() = \"7\"\n",
84 | "Faker.color_name() = \"DeepPink\"\n",
85 | "Faker.rgb_color() = \"59,113,237\"\n"
86 | ]
87 | }
88 | ],
89 | "source": [
90 | "#Using the package is very simple:\n",
91 | "\n",
92 | "#Random Digit Generation\n",
93 | "@show Faker.random_digit()\n",
94 | "\n",
95 | "#Fake color generation\n",
96 | "@show Faker.color_name()\n",
97 | "@show Faker.rgb_color()\n",
98 | ";\n",
99 | "\n",
100 | "#And so on there is a whole list of general fake commands"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 23,
106 | "metadata": {
107 | "collapsed": false
108 | },
109 | "outputs": [
110 | {
111 | "name": "stdout",
112 | "output_type": "stream",
113 | "text": [
114 | "Faker.simple_profile() = Dict{ASCIIString,Any}(\"name\"=>\"Salma\",\"mail\"=>\"WValle@hotmail.com\",\"username\"=>\"uValle\",\"birthdate\"=>\"1996-2-16\",\"sex\"=>\"M\",\"address\"=>\"Peatonal Filipinas 552 701 Vieja Etiopía, VER 09048\")\n",
115 | "Faker.time(\"%H:%M:%S\") = \"14:52:19\"\n",
116 | "Faker.date_time_between(\"-30y\",\"now\") = \"1998-12-05 16:32:51\"\n"
117 | ]
118 | }
119 | ],
120 | "source": [
121 | "#While at first, this type of package may seem quite useless\n",
122 | "#there is real value in such package (and I assume the reason for\n",
123 | "#its generation) is to be used to test form filling\n",
124 | "\n",
125 | "#Especially the fake profile information:\n",
126 | "@show Faker.simple_profile()\n",
127 | "\n",
128 | "#Such fake data could be used many times to fill out forms when they\n",
129 | "#are still testing\n",
130 | "\n",
131 | "#While coding up using random numbers or pre-made lists could be done,\n",
132 | "#this package makes it nice and simple, especially for random data\n",
133 | "#data entry that is expected in different formats, like the data/time\n",
134 | "@show Faker.time(\"%H:%M:%S\")\n",
135 | "@show Faker.date_time_between(\"-30y\",\"now\")\n",
136 | "\n",
137 | "#It's simple implementation makes it a useful package\n",
138 | ";"
139 | ]
140 | }
141 | ],
142 | "metadata": {
143 | "kernelspec": {
144 | "display_name": "Julia 0.4.0-rc4",
145 | "language": "julia",
146 | "name": "julia-0.4"
147 | },
148 | "language_info": {
149 | "file_extension": ".jl",
150 | "mimetype": "application/julia",
151 | "name": "julia",
152 | "version": "0.4.0"
153 | }
154 | },
155 | "nbformat": 4,
156 | "nbformat_minor": 0
157 | }
158 |
--------------------------------------------------------------------------------
/packages/2015/graphs.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Graphs.jl\n",
8 | "\n",
9 | "This notebook is intended to demonstrate the Julia package [Graphs.jl](https://github.com/JuliaLang/Graphs.jl). This notebook was created using Julia 0.4.0-rc4."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "#Basic usage"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "We can easily create graphs by specifying the number of vertices and adding edges one by one."
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 25,
29 | "metadata": {
30 | "collapsed": false
31 | },
32 | "outputs": [
33 | {
34 | "name": "stdout",
35 | "output_type": "stream",
36 | "text": [
37 | "[edge [1]: 1 -- 2,edge [2]: 2 -- 3,edge [3]: 3 -- 1]\n",
38 | "1:3\n",
39 | "Bool[false true false\n",
40 | " false false true\n",
41 | " true false false]\n"
42 | ]
43 | }
44 | ],
45 | "source": [
46 | "using Graphs\n",
47 | "\n",
48 | "# Creating a directed graph.\n",
49 | "g = simple_graph(3)\n",
50 | "add_edge!(g, 1, 2)\n",
51 | "add_edge!(g, 2, 3)\n",
52 | "add_edge!(g, 3, 1)\n",
53 | "\n",
54 | "# Querying which edges and vertices exist.\n",
55 | "println(edges(g))\n",
56 | "println(vertices(g))\n",
57 | "\n",
58 | "# Representing g as an adjecency matrix.\n",
59 | "println(adjacency_matrix(g))"
60 | ]
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {},
65 | "source": [
66 | "We can also create graphs using a number of graph generators."
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 38,
72 | "metadata": {
73 | "collapsed": false
74 | },
75 | "outputs": [
76 | {
77 | "data": {
78 | "text/plain": [
79 | "Directed Graph (5 vertices, 4 edges)"
80 | ]
81 | },
82 | "execution_count": 38,
83 | "metadata": {},
84 | "output_type": "execute_result"
85 | }
86 | ],
87 | "source": [
88 | "using Graphs\n",
89 | "\n",
90 | "g = simple_cubical_graph()\n",
91 | "h = simple_house_graph()\n",
92 | "i = simple_star_graph(5)"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "#Graph algorithms"
100 | ]
101 | },
102 | {
103 | "cell_type": "markdown",
104 | "metadata": {},
105 | "source": [
106 | "Here we demonstrate how to use some common graph algorithms."
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": 44,
112 | "metadata": {
113 | "collapsed": false
114 | },
115 | "outputs": [
116 | {
117 | "data": {
118 | "text/plain": [
119 | "true"
120 | ]
121 | },
122 | "execution_count": 44,
123 | "metadata": {},
124 | "output_type": "execute_result"
125 | }
126 | ],
127 | "source": [
128 | "# Cycle detection using DFS.\n",
129 | "using Graphs\n",
130 | "test_cyclic_by_dfs(simple_house_graph())"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": 53,
136 | "metadata": {
137 | "collapsed": false
138 | },
139 | "outputs": [
140 | {
141 | "name": "stdout",
142 | "output_type": "stream",
143 | "text": [
144 | "5\n",
145 | "3\n",
146 | "1\n"
147 | ]
148 | }
149 | ],
150 | "source": [
151 | "# Dijkstra's Algorithm (Shortest Path).\n",
152 | "using Graphs\n",
153 | "\n",
154 | "# Creating the graph.\n",
155 | "g = simple_graph(5)\n",
156 | "\n",
157 | "# Each element is (u, v, dist).\n",
158 | "edge_inputs = [\n",
159 | " (1, 2, 10.),\n",
160 | " (1, 3, 5.),\n",
161 | " (2, 3, 2.),\n",
162 | " (3, 2, 3.),\n",
163 | " (2, 4, 1.),\n",
164 | " (3, 5, 2.),\n",
165 | " (4, 5, 4.),\n",
166 | " (5, 4, 6.),\n",
167 | " (5, 1, 7.),\n",
168 | " (3, 4, 9.)\n",
169 | "]\n",
170 | "\n",
171 | "ne = length(edge_inputs)\n",
172 | "dists = zeros(ne)\n",
173 | "for i = 1 : ne\n",
174 | " curr = edge_inputs[i]\n",
175 | " add_edge!(g, curr[1], curr[2])\n",
176 | " dists[i] = curr[3]\n",
177 | "end\n",
178 | "\n",
179 | "# Find shortest path from all vertices to 1.\n",
180 | "r = dijkstra_shortest_paths(g, dists, 1)\n",
181 | "\n",
182 | "# Print path from vertex 5 to 1:\n",
183 | "curr_vertex = 5\n",
184 | "println(curr_vertex)\n",
185 | "while curr_vertex != 1\n",
186 | " curr_vertex = r.parents[curr_vertex]\n",
187 | " println(curr_vertex)\n",
188 | "end"
189 | ]
190 | },
191 | {
192 | "cell_type": "code",
193 | "execution_count": 73,
194 | "metadata": {
195 | "collapsed": false
196 | },
197 | "outputs": [
198 | {
199 | "name": "stdout",
200 | "output_type": "stream",
201 | "text": [
202 | "[edge [1]: 1 -- 2,edge [2]: 1 -- 3,edge [3]: 2 -- 4,edge [5]: 3 -- 5]\n"
203 | ]
204 | }
205 | ],
206 | "source": [
207 | "# Kruskal's Algorithm (Minimum Spanning Tree).\n",
208 | "using Graphs\n",
209 | "\n",
210 | "# Print MST for undirected graph with 6 edges (house-shaped).\n",
211 | "g = simple_house_graph()\n",
212 | "weights = ones(length(edges(g)))\n",
213 | "println(kruskal_minimum_spantree(g, weights)[1])"
214 | ]
215 | },
216 | {
217 | "cell_type": "code",
218 | "execution_count": 82,
219 | "metadata": {
220 | "collapsed": false
221 | },
222 | "outputs": [
223 | {
224 | "name": "stdout",
225 | "output_type": "stream",
226 | "text": [
227 | "1:5\n",
228 | "Bool[true,false,false,false,false]\n",
229 | "2.0\n"
230 | ]
231 | }
232 | ],
233 | "source": [
234 | "# Stoer's Simple Minimum Cut.\n",
235 | "using Graphs\n",
236 | "\n",
237 | "g = simple_house_graph()\n",
238 | "println(vertices(g))\n",
239 | "\n",
240 | "# Printing the resulting partition.\n",
241 | "println(min_cut(g)[1])\n",
242 | "\n",
243 | "# Printing the min-cut weight. Since this is an unweighted graph,\n",
244 | "# this is the number of edges we had to cut.\n",
245 | "println(min_cut(g)[2])"
246 | ]
247 | },
248 | {
249 | "cell_type": "code",
250 | "execution_count": null,
251 | "metadata": {
252 | "collapsed": true
253 | },
254 | "outputs": [],
255 | "source": []
256 | }
257 | ],
258 | "metadata": {
259 | "kernelspec": {
260 | "display_name": "Julia 0.4.0-rc4",
261 | "language": "julia",
262 | "name": "julia-0.4"
263 | },
264 | "language_info": {
265 | "file_extension": ".jl",
266 | "mimetype": "application/julia",
267 | "name": "julia",
268 | "version": "0.4.0"
269 | }
270 | },
271 | "nbformat": 4,
272 | "nbformat_minor": 0
273 | }
274 |
--------------------------------------------------------------------------------
/packages/2015/phylogenetics.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 82,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [
10 | {
11 | "name": "stderr",
12 | "output_type": "stream",
13 | "text": [
14 | "INFO: Initializing package repository /home/juser/.julia/v0.4\n",
15 | "INFO: Package directory /home/juser/.julia/v0.4 is already initialized.\n",
16 | "INFO: Nothing to be done\n",
17 | "INFO: Updating METADATA...\n",
18 | "INFO: Computing changes...\n",
19 | "INFO: No packages to install, update or remove\n"
20 | ]
21 | }
22 | ],
23 | "source": [
24 | "# This notebook uses Julia 0.4.0.\n",
25 | "\n",
26 | "# A package that I found interesting is Phylogenetics.\n",
27 | "# Phylogenetics offers tools to analyze evolution and evolutionary history\n",
28 | "Pkg.init()\n",
29 | "Pkg.add(\"Phylogenetics\") # Install Phylogenetics.jl\n",
30 | "Pkg.update()\n",
31 | "using Phylogenetics # to use Phylogenetics"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 22,
37 | "metadata": {
38 | "collapsed": false
39 | },
40 | "outputs": [
41 | {
42 | "data": {
43 | "text/plain": [
44 | "Phylogenetics.Clado(\"\",5x2 Array{Int64,2}:\n",
45 | " 5 1\n",
46 | " 5 2\n",
47 | " 5 6\n",
48 | " 6 3\n",
49 | " 6 4,2,AbstractString[\"A\",\"B\",\"C\",\"D\"],AbstractString[\"\",\"\"])"
50 | ]
51 | },
52 | "execution_count": 22,
53 | "metadata": {},
54 | "output_type": "execute_result"
55 | }
56 | ],
57 | "source": [
58 | "# In the study of phylogeny, a common way to visualize evolution is with a phylogenetic tree. \n",
59 | "# This is a branching diagram that represents evolutionary relationships between species, \n",
60 | "# inferred by looking at similarities in their genome. Sometimes, branches are labeled with \n",
61 | "# distances. These trees are usually represented in Newick format (https://en.wikipedia.org/wiki/Newick_format)\n",
62 | "\n",
63 | "# This package allows for the representation of Cladograms (no distances)\n",
64 | "# and Phylogenetic trees (with distances) in Newick format. \n",
65 | "\n",
66 | "# Here is how to create a simple Cladogram \"Clado\" from Newick format\n",
67 | "simple_clado = tr\"(A,B,(C,D));\"\n",
68 | "\n",
69 | "# Notice that although we initially only entered 4 nodes into the tree, the result refers to nodes 5 and 6. \n",
70 | "# This is because to create this cladogram, 2 more nodes are necessary to properly combine the existing nodes. \n",
71 | "# See this image to see the other two nodes (called F and E) https://en.wikipedia.org/wiki/Newick_format#/media/File:NewickExample.svg\n",
72 | "\n",
73 | "\n"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {
80 | "collapsed": true
81 | },
82 | "outputs": [],
83 | "source": [
84 | "# You can also create a phylogenetic tree with distances using Newick format\n",
85 | "\n",
86 | "simple_phylo = tr\"(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);\" \n"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 23,
92 | "metadata": {
93 | "collapsed": false
94 | },
95 | "outputs": [
96 | {
97 | "data": {
98 | "text/plain": [
99 | "6-element Array{Array{Int64,N},1}:\n",
100 | " Int64[]\n",
101 | " Int64[]\n",
102 | " Int64[]\n",
103 | " Int64[]\n",
104 | " [1,2,6]\n",
105 | " [3,4] "
106 | ]
107 | },
108 | "execution_count": 23,
109 | "metadata": {},
110 | "output_type": "execute_result"
111 | }
112 | ],
113 | "source": [
114 | "# One of the most useful functions in this package is getkids, which returns an array associating\n",
115 | "# each node with its child nodes\n",
116 | "getkids(simple_clado)\n",
117 | "\n",
118 | "# If you refer to the image of the tree (https://en.wikipedia.org/wiki/Newick_format#/media/File:NewickExample.svg),\n",
119 | "# the only two nodes with children are nodes F (child nodes A, B, E) and E (child nodes C, D)"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 43,
125 | "metadata": {
126 | "collapsed": false
127 | },
128 | "outputs": [
129 | {
130 | "data": {
131 | "text/plain": [
132 | "5"
133 | ]
134 | },
135 | "execution_count": 43,
136 | "metadata": {},
137 | "output_type": "execute_result"
138 | }
139 | ],
140 | "source": [
141 | "# Another useful function is getroot, which finds the root node of the tree.\n",
142 | "getroot(simple_clado.edge[:,2],simple_clado.edge[:,1])\n",
143 | "\n",
144 | "# As you can see, it returns the correct root node (recall that node 5 maps to F on the phylogenetic tree image)"
145 | ]
146 | },
147 | {
148 | "cell_type": "code",
149 | "execution_count": 44,
150 | "metadata": {
151 | "collapsed": false
152 | },
153 | "outputs": [
154 | {
155 | "data": {
156 | "text/plain": [
157 | "Phylogenetics.Phylo(\"\",13x2 Array{Int64,2}:\n",
158 | " 9 10\n",
159 | " 10 1\n",
160 | " 10 2\n",
161 | " 9 11\n",
162 | " 11 12\n",
163 | " 12 3\n",
164 | " 12 4\n",
165 | " 11 13\n",
166 | " 13 14\n",
167 | " 14 5\n",
168 | " 14 6\n",
169 | " 13 7\n",
170 | " 9 8,6,AbstractString[\"raccoon\",\"bear\",\"sea_lion\",\"seal\",\"monkey\",\"cat\",\"weasel\",\"dog\"],[0.846,19.19959,6.80041,3.87382,7.52973,11.997,12.003,2.0946,20.59201,100.8593,47.14069,18.87953,25.46154],AbstractString[\"\",\"\",\"\",\"\",\"\",\"\"],-1.0)"
171 | ]
172 | },
173 | "execution_count": 44,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "# Now, we can use these tools to represent a real life data set that examines the overlapping evolutionary history of \n",
180 | "# raccoons, bears, seals, sea lions, monkeys, cats, weasels, and dogs.\n",
181 | "\n",
182 | "# We first construct the phylogenetic tree using Newick format\n",
183 | "\n",
184 | "real_tree = tr\"((raccoon:19.19959,bear:6.80041):0.84600,((sea_lion:11.99700, seal:12.00300):7.52973,((monkey:100.85930,cat:47.14069):20.59201, weasel:18.87953):2.09460):3.87382,dog:25.46154);\"\n",
185 | "\n"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": 64,
191 | "metadata": {
192 | "collapsed": false
193 | },
194 | "outputs": [
195 | {
196 | "data": {
197 | "text/plain": [
198 | "1"
199 | ]
200 | },
201 | "execution_count": 64,
202 | "metadata": {},
203 | "output_type": "execute_result"
204 | }
205 | ],
206 | "source": [
207 | "# What if we wanted to know which of these other animals is most closely related to a raccoon evolutionarily?\n",
208 | "# In a phylogenetic tree, it will be the animal that shares the most recent parent with the raccoon. \n",
209 | "\n",
210 | "# To figure this out, we first, we need to find out which index represents raccoon in this list.\n",
211 | "\n",
212 | "# Here is the type definition for Phylo (which real_tree is a member of):\n",
213 | "#immutable Phylo <: Phylogeny\n",
214 | "#\tname::String\n",
215 | "#\tedge::Array{Int,2}\n",
216 | "#\tNnode::Int\n",
217 | "#\ttipLabel::Array{String}\n",
218 | "#\tedgeLength::Array{Float64}\n",
219 | "#\tnodeLabel::Array{String}\n",
220 | "#\trootEdge::Float64\n",
221 | "#end\n",
222 | "\n",
223 | "# The component containing the node labels is \"tipLabel\", so we search for \"raccoon\" within this list to retrieve the index\n",
224 | "raccoon_array = [\"raccoon\"]\n",
225 | "index = findin(raccoon_array, real_tree.tipLabel)\n"
226 | ]
227 | },
228 | {
229 | "cell_type": "code",
230 | "execution_count": 81,
231 | "metadata": {
232 | "collapsed": false
233 | },
234 | "outputs": [
235 | {
236 | "name": "stdout",
237 | "output_type": "stream",
238 | "text": [
239 | "Parent is additional node 10\n",
240 | "Children are: AbstractString[\"raccoon\",\"bear\"]"
241 | ]
242 | }
243 | ],
244 | "source": [
245 | "# Now that we know raccoon represents index \"index\", we can use the get kids method again.\n",
246 | "kids = getkids(real_tree)\n",
247 | "\n",
248 | "for i = [1:length(kids)] #for every parent-child list\n",
249 | " childlist = kids[i]\n",
250 | " if length(findin(childlist, index)) > 0 #if the raccoon index can be found in the parent-child list\n",
251 | " if (i < length(real_tree.tipLabel)) #print out pertinent information about the parent and child\n",
252 | " print(\"Parent is \")\n",
253 | " print(real_tree.tipLabel[i])\n",
254 | " else \n",
255 | " print(\"Parent is additional node \")\n",
256 | " print(i)\n",
257 | " print(\"\\n\")\n",
258 | " print(\"Children are: \")\n",
259 | " print(real_tree.tipLabel[childlist])\n",
260 | " end\n",
261 | " end\n",
262 | "end\n",
263 | "\n",
264 | "# The resulting print out tells us that the parent of the raccoon is a new node 10, and its \"sibling\" in the tree is \n",
265 | "# the bear. So, out of the animals in this list, the bear is the most closely related to the raccoon.\n"
266 | ]
267 | }
268 | ],
269 | "metadata": {
270 | "kernelspec": {
271 | "display_name": "Julia 0.4.0-rc2",
272 | "language": "julia",
273 | "name": "julia-0.4"
274 | },
275 | "language_info": {
276 | "file_extension": ".jl",
277 | "mimetype": "application/julia",
278 | "name": "julia",
279 | "version": "0.4.0"
280 | }
281 | },
282 | "nbformat": 4,
283 | "nbformat_minor": 0
284 | }
285 |
--------------------------------------------------------------------------------
/packages/2015/progressmeter.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 23,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "# CME257 HW3 solution - Yair Carmon\n",
12 | "# Demonstration of ProgressMeter package\n",
13 | "# Julia version 0.4.0-rc4\n",
14 | "\n",
15 | "# Pkg.add(\"ProgressMeter\")\n",
16 | "using ProgressMeter"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "Introduction\n",
24 | "---\n",
25 | "\n",
26 | "ProgressMeter is a small and simple package that creates a textual progress meter\n",
27 | "\n"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "Usage\n",
35 | "---"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": 21,
41 | "metadata": {
42 | "collapsed": false
43 | },
44 | "outputs": [
45 | {
46 | "name": "stdout",
47 | "output_type": "stream",
48 | "text": [
49 | "Progress: 100% Time: 0:00:11\n"
50 | ]
51 | }
52 | ],
53 | "source": [
54 | "# the progress meter object is initialized as follows\n",
55 | "# pMeterObj = Progress(TotalWork, Minimum update interval (in seconds), Display string, Number of characters for progress \"plot\")\n",
56 | "\n",
57 | "# Simple progress meter - notes that estimated time remaining is evaluated pretty much correctly\n",
58 | "pMeterObjSimple = Progress(100,0.05);\n",
59 | "\n",
60 | "for k = 1:100\n",
61 | " sleep(0.1); # run time = 0.1*100 = 10 sec\n",
62 | " next!(pMeterObjSimple); # one increment of progress\n",
63 | "end"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": 24,
69 | "metadata": {
70 | "collapsed": false
71 | },
72 | "outputs": [
73 | {
74 | "name": "stdout",
75 | "output_type": "stream",
76 | "text": [
77 | "Zzz...100%|██████████████████████████████████████████████████| Time: 0:00:11\n"
78 | ]
79 | }
80 | ],
81 | "source": [
82 | "# Same loop, but with a nicer progress bar\n",
83 | "pMeterObjSimple = Progress(100,0.05,\"Zzz...\",50);\n",
84 | "\n",
85 | "for k = 1:100\n",
86 | " sleep(0.1); # run time = 0.1*100 = 10 sec\n",
87 | " next!(pMeterObjSimple); # one increment of progress\n",
88 | "end"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 27,
94 | "metadata": {
95 | "collapsed": false
96 | },
97 | "outputs": [
98 | {
99 | "name": "stdout",
100 | "output_type": "stream",
101 | "text": [
102 | "Zzz...100%|██████████████████████████████████████████████████| Time: 0:00:11\n"
103 | ]
104 | }
105 | ],
106 | "source": [
107 | "# When the task time changes, task time prediction suffers\n",
108 | "pMeterObjNicer = Progress(100,0.05,\"Zzz...\",50);\n",
109 | "\n",
110 | "for k = 1:100\n",
111 | " sleep(0.002*k); # run time = ~ 10 sec\n",
112 | " next!(pMeterObjNicer); # one increment of progress\n",
113 | "end"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": 45,
119 | "metadata": {
120 | "collapsed": false
121 | },
122 | "outputs": [
123 | {
124 | "name": "stdout",
125 | "output_type": "stream",
126 | "text": [
127 | "Zzz...100%|██████████████████████████████████████████████████| Time: 0:00:11\n"
128 | ]
129 | }
130 | ],
131 | "source": [
132 | "# This can be solved by suitable update of the progress amount\n",
133 | "pMeterObjNicerCalibrated = Progress(100*10,0.05,\"Zzz...\",50);\n",
134 | "\n",
135 | "tot = 0;\n",
136 | "for k = 1:100\n",
137 | " sleep(0.002*k); # run time = ~ 10 sec\n",
138 | " tot += 0.002*k;\n",
139 | " update!(pMeterObjNicerCalibrated, convert(Int64,round(100*tot)));\n",
140 | "end\n",
141 | "update!(pMeterObjNicerCalibrated, 1000); # to finish the run"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": 52,
147 | "metadata": {
148 | "collapsed": false
149 | },
150 | "outputs": [
151 | {
152 | "name": "stdout",
153 | "output_type": "stream",
154 | "text": [
155 | "Automating...100%|██████████████████████████████████████████████████| Time: 0:00:11\n"
156 | ]
157 | }
158 | ],
159 | "source": [
160 | "# There is also a nice macro, but it does not seem to be able to time the loop very well\n",
161 | "@showprogress 0.05 \"Automating...\" 50 for k in 1:100\n",
162 | " sleep(0.002*k)\n",
163 | "end"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "Windows 98 progress bar :)\n",
171 | "---"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {
178 | "collapsed": false
179 | },
180 | "outputs": [
181 | {
182 | "name": "stdout",
183 | "output_type": "stream",
184 | "text": [
185 | "Transfering file... 15%|████████ | ETA: 0:03:06"
186 | ]
187 | }
188 | ],
189 | "source": [
190 | "p98 = Progress(1000,0.05,\"Transfering file...\",50);\n",
191 | "tot = 0;\n",
192 | "while tot < 1000\n",
193 | " sleep(0.01)\n",
194 | " tot = round(min(max(0,tot+0.1+randn(1)[1]),1000));\n",
195 | " update!(p98, convert(Int64,tot));\n",
196 | "end"
197 | ]
198 | }
199 | ],
200 | "metadata": {
201 | "kernelspec": {
202 | "display_name": "Julia 0.4.0-rc4",
203 | "language": "julia",
204 | "name": "julia-0.4"
205 | },
206 | "language_info": {
207 | "file_extension": ".jl",
208 | "mimetype": "application/julia",
209 | "name": "julia",
210 | "version": "0.4.0"
211 | }
212 | },
213 | "nbformat": 4,
214 | "nbformat_minor": 0
215 | }
216 |
--------------------------------------------------------------------------------
/packages/DecisionTree.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### CME 257 Homework 3\n",
8 | "### February 4, 2018"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 124,
14 | "metadata": {},
15 | "outputs": [],
16 | "source": [
17 | "# Julia version: 0.6.2\n",
18 | "# Decision Tree Classifier and Regressor in Julia\n",
19 | "using DecisionTree"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 125,
25 | "metadata": {},
26 | "outputs": [
27 | {
28 | "data": {
29 | "text/plain": [
30 | "5-element Array{Int64,1}:\n",
31 | " 1\n",
32 | " -1\n",
33 | " -2\n",
34 | " -2\n",
35 | " 0"
36 | ]
37 | },
38 | "execution_count": 125,
39 | "metadata": {},
40 | "output_type": "execute_result"
41 | }
42 | ],
43 | "source": [
44 | "# Example 1: Regression Tree\n",
45 | "srand(1000)\n",
46 | "n, m = 100000, 5 ;\n",
47 | "features = randn(n, m);\n",
48 | "weights = rand(-2:2, m)"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 126,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": [
57 | "labels = features * weights;"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 127,
63 | "metadata": {},
64 | "outputs": [
65 | {
66 | "data": {
67 | "text/plain": [
68 | "Decision Tree\n",
69 | "Leaves: 32149\n",
70 | "Depth: 25"
71 | ]
72 | },
73 | "execution_count": 127,
74 | "metadata": {},
75 | "output_type": "execute_result"
76 | }
77 | ],
78 | "source": [
79 | "# train regression tree: \n",
80 | "# regression automatically considered since lables are floats\n",
81 | "model = build_tree(labels, features)"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 128,
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "data": {
91 | "text/plain": [
92 | "-12.041004980600466"
93 | ]
94 | },
95 | "execution_count": 128,
96 | "metadata": {},
97 | "output_type": "execute_result"
98 | }
99 | ],
100 | "source": [
101 | "# apply learned model\n",
102 | "apply_tree(model, [-0.9,3.0,5.1,1.9,0.0]) # true value: -17.9"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 131,
108 | "metadata": {},
109 | "outputs": [
110 | {
111 | "name": "stdout",
112 | "output_type": "stream",
113 | "text": [
114 | "\n",
115 | "Fold 1\n",
116 | "Mean Squared Error: 0.20829236079547908\n",
117 | "Correlation Coeff: 0.9895886122990418\n",
118 | "Coeff of Determination: 0.9792134405255102\n",
119 | "\n",
120 | "Fold 2\n",
121 | "Mean Squared Error: 0.21806033294027016\n",
122 | "Correlation Coeff: 0.9893057444420884\n",
123 | "Coeff of Determination: 0.9786393684596367\n",
124 | "\n",
125 | "Fold 3\n",
126 | "Mean Squared Error: 0.21772785846409598\n",
127 | "Correlation Coeff: 0.9891128958485246\n",
128 | "Coeff of Determination: 0.9782416565982884\n",
129 | "\n",
130 | "Fold 4\n",
131 | "Mean Squared Error: 0.21552233309764496\n",
132 | "Correlation Coeff: 0.9891754812363047\n",
133 | "Coeff of Determination: 0.9783921655035095\n",
134 | "\n",
135 | "Fold 5\n",
136 | "Mean Squared Error: 0.2143584781106968\n",
137 | "Correlation Coeff: 0.9890917768405755\n",
138 | "Coeff of Determination: 0.9782139980432937\n",
139 | "\n",
140 | "Mean Coeff of Determination: 0.9785401258260477\n"
141 | ]
142 | },
143 | {
144 | "data": {
145 | "text/plain": [
146 | "5-element Array{Float64,1}:\n",
147 | " 0.979213\n",
148 | " 0.978639\n",
149 | " 0.978242\n",
150 | " 0.978392\n",
151 | " 0.978214"
152 | ]
153 | },
154 | "execution_count": 131,
155 | "metadata": {},
156 | "output_type": "execute_result"
157 | }
158 | ],
159 | "source": [
160 | "# run n-fold cross validation, using 5 folds\n",
161 | "# returns array of coefficients of determination (R^2)\n",
162 | "r2 = nfoldCV_tree(labels, features, 5)"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 132,
168 | "metadata": {},
169 | "outputs": [],
170 | "source": [
171 | "# Example 2: Classification Tree\n",
172 | "using RDatasets: dataset"
173 | ]
174 | },
175 | {
176 | "cell_type": "code",
177 | "execution_count": 133,
178 | "metadata": {},
179 | "outputs": [],
180 | "source": [
181 | "# import iris dataset data\n",
182 | "iris = dataset(\"datasets\", \"iris\")\n",
183 | "features = convert(Array, iris[:, 1:4]);\n",
184 | "labels = convert(Array, iris[:, 5]);"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 112,
190 | "metadata": {},
191 | "outputs": [
192 | {
193 | "name": "stdout",
194 | "output_type": "stream",
195 | "text": [
196 | "Feature 3, Threshold 3.0\n",
197 | "L-> setosa : 50/50\n",
198 | "R-> Feature 4, Threshold 1.8\n",
199 | " L-> Feature 3, Threshold 5.0\n",
200 | " L-> versicolor : 47/48\n",
201 | " R-> Feature 4, Threshold 1.6\n",
202 | " L-> virginica : 3/3\n",
203 | " R-> Feature 1, Threshold 7.2\n",
204 | " L-> versicolor : 2/2\n",
205 | " R-> virginica : 1/1\n",
206 | " R-> Feature 3, Threshold 4.9\n",
207 | " L-> Feature 1, Threshold 6.0\n",
208 | " L-> versicolor : 1/1\n",
209 | " R-> virginica : 2/2\n",
210 | " R-> virginica : 43/43\n"
211 | ]
212 | }
213 | ],
214 | "source": [
215 | "# train a full-tree classifier\n",
216 | "model = build_tree(labels, features)\n",
217 | "# prune the full tree: merge leaves with >= 80% purity\n",
218 | "model = prune_tree(model, 0.8)\n",
219 | "# graphical print of the tree\n",
220 | "print_tree(model)"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": 116,
226 | "metadata": {},
227 | "outputs": [
228 | {
229 | "data": {
230 | "text/plain": [
231 | "\"setosa\""
232 | ]
233 | },
234 | "execution_count": 116,
235 | "metadata": {},
236 | "output_type": "execute_result"
237 | }
238 | ],
239 | "source": [
240 | "# apply learned model\n",
241 | "apply_tree(model, [2.4,4.2,0.1,2.8])"
242 | ]
243 | },
244 | {
245 | "cell_type": "code",
246 | "execution_count": 134,
247 | "metadata": {},
248 | "outputs": [
249 | {
250 | "data": {
251 | "text/plain": [
252 | "3×3 Array{Int64,2}:\n",
253 | " 7 0 0\n",
254 | " 0 9 0\n",
255 | " 0 1 13"
256 | ]
257 | },
258 | "metadata": {},
259 | "output_type": "display_data"
260 | },
261 | {
262 | "data": {
263 | "text/plain": [
264 | "3×3 Array{Int64,2}:\n",
265 | " 9 0 0\n",
266 | " 0 8 2\n",
267 | " 0 2 9"
268 | ]
269 | },
270 | "metadata": {},
271 | "output_type": "display_data"
272 | },
273 | {
274 | "data": {
275 | "text/plain": [
276 | "3×3 Array{Int64,2}:\n",
277 | " 9 0 0\n",
278 | " 0 9 0\n",
279 | " 0 1 11"
280 | ]
281 | },
282 | "metadata": {},
283 | "output_type": "display_data"
284 | },
285 | {
286 | "data": {
287 | "text/plain": [
288 | "3×3 Array{Int64,2}:\n",
289 | " 11 0 0\n",
290 | " 0 8 1\n",
291 | " 0 0 10"
292 | ]
293 | },
294 | "metadata": {},
295 | "output_type": "display_data"
296 | },
297 | {
298 | "data": {
299 | "text/plain": [
300 | "3×3 Array{Int64,2}:\n",
301 | " 14 0 0\n",
302 | " 1 11 1\n",
303 | " 0 1 2"
304 | ]
305 | },
306 | "metadata": {},
307 | "output_type": "display_data"
308 | },
309 | {
310 | "name": "stdout",
311 | "output_type": "stream",
312 | "text": [
313 | "\n",
314 | "Fold 1\n",
315 | "Classes: String[\"setosa\", \"versicolor\", \"virginica\"]\n",
316 | "Matrix: \n",
317 | "Accuracy: 0.9666666666666667\n",
318 | "Kappa: 0.9481865284974094\n",
319 | "\n",
320 | "Fold 2\n",
321 | "Classes: String[\"setosa\", \"versicolor\", \"virginica\"]\n",
322 | "Matrix: \n",
323 | "Accuracy: 0.8666666666666667\n",
324 | "Kappa: 0.7993311036789298\n",
325 | "\n",
326 | "Fold 3\n",
327 | "Classes: String[\"setosa\", \"versicolor\", \"virginica\"]\n",
328 | "Matrix: \n",
329 | "Accuracy: 0.9666666666666667\n",
330 | "Kappa: 0.949748743718593\n",
331 | "\n",
332 | "Fold 4\n",
333 | "Classes: String[\"setosa\", \"versicolor\", \"virginica\"]\n",
334 | "Matrix: \n",
335 | "Accuracy: 0.9666666666666667\n",
336 | "Kappa: 0.949748743718593\n",
337 | "\n",
338 | "Fold 5\n",
339 | "Classes: String[\"setosa\", \"versicolor\", \"virginica\"]\n",
340 | "Matrix: \n",
341 | "Accuracy: 0.9\n",
342 | "Kappa: 0.8285714285714287\n",
343 | "\n",
344 | "Mean Accuracy: 0.9333333333333333\n"
345 | ]
346 | },
347 | {
348 | "data": {
349 | "text/plain": [
350 | "5-element Array{Float64,1}:\n",
351 | " 0.966667\n",
352 | " 0.866667\n",
353 | " 0.966667\n",
354 | " 0.966667\n",
355 | " 0.9 "
356 | ]
357 | },
358 | "execution_count": 134,
359 | "metadata": {},
360 | "output_type": "execute_result"
361 | }
362 | ],
363 | "source": [
364 | "# run n-fold cross validation for pruned tree, using 80% purity threshold pruning, and 5 CV folds\n",
365 | "accuracy = nfoldCV_tree(labels, features, 0.8, 5)"
366 | ]
367 | },
368 | {
369 | "cell_type": "markdown",
370 | "metadata": {},
371 | "source": [
372 | "##### Summary: This package supports decision tree regression and classification, which allows pruning and cross validation. In addition, this package also supports parallelized bagging and adaptive boosting. It also allows ScikitLearn.jl interface and algorithms."
373 | ]
374 | }
375 | ],
376 | "metadata": {
377 | "kernelspec": {
378 | "display_name": "Julia 0.6.2",
379 | "language": "julia",
380 | "name": "julia-0.6"
381 | },
382 | "language_info": {
383 | "file_extension": ".jl",
384 | "mimetype": "application/julia",
385 | "name": "julia",
386 | "version": "0.6.2"
387 | }
388 | },
389 | "nbformat": 4,
390 | "nbformat_minor": 2
391 | }
392 |
--------------------------------------------------------------------------------
/packages/IterativeSolvers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "data": {
10 | "text/plain": [
11 | "v\"0.6.0\""
12 | ]
13 | },
14 | "execution_count": 1,
15 | "metadata": {},
16 | "output_type": "execute_result"
17 | }
18 | ],
19 | "source": [
20 | "VERSION"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": 2,
26 | "metadata": {
27 | "collapsed": true
28 | },
29 | "outputs": [],
30 | "source": [
31 | "using IterativeSolvers"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 6,
37 | "metadata": {},
38 | "outputs": [
39 | {
40 | "data": {
41 | "text/html": [
42 | "9.635229849022109e-10"
43 | ],
44 | "text/plain": [
45 | "9.635229849022109e-10"
46 | ]
47 | },
48 | "execution_count": 6,
49 | "metadata": {},
50 | "output_type": "execute_result"
51 | }
52 | ],
53 | "source": [
54 | "# Solving linear system, where matrix A is SPD.\n",
55 | "# For this problem, we can iterative solvers. In particular, since A is SPD,\n",
56 | "# we can pick Conjugate Gradient as an example.\n",
57 | "\n",
58 | "m = 100\n",
59 | "b = ones(m, 1)\n",
60 | "A = randn(m, m)\n",
61 | "A = 0.5*(A+A') + m*eye(m)\n",
62 | "x = A\\b\n",
63 | "x_cg = IterativeSolvers.cg(A, b)\n",
64 | "eps = norm(x - x_cg)"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": 28,
70 | "metadata": {},
71 | "outputs": [
72 | {
73 | "data": {
74 | "text/html": [
75 | "11.27718067441524"
76 | ],
77 | "text/plain": [
78 | "11.27718067441524"
79 | ]
80 | },
81 | "execution_count": 28,
82 | "metadata": {},
83 | "output_type": "execute_result"
84 | }
85 | ],
86 | "source": [
87 | "# Now let us explore a matrix A which is just symmetric.\n",
88 | "# For this case, we can use MINRES. MINRES is similar to SPD\n",
89 | "\n",
90 | "m = 100\n",
91 | "b = ones(m, 1)\n",
92 | "A = randn(m, m)\n",
93 | "A = 0.5*(A+A')\n",
94 | "x = A\\b\n",
95 | "x_minres = IterativeSolvers.minres(A, b)\n",
96 | "eps = norm(x - x_minres)"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 48,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/html": [
107 | "5.678521008267138"
108 | ],
109 | "text/plain": [
110 | "5.678521008267138"
111 | ]
112 | },
113 | "execution_count": 48,
114 | "metadata": {},
115 | "output_type": "execute_result"
116 | }
117 | ],
118 | "source": [
119 | "# The last common example is GMRES\n",
120 | "# which is for any invertible matrix\n",
121 | "\n",
122 | "m = 100\n",
123 | "b = ones(m, 1)\n",
124 | "x_0 = ones(m, 1)\n",
125 | "A = randn(m, m)\n",
126 | "x = A\\b\n",
127 | "x_gmres = IterativeSolvers.gmres(A, b)\n",
128 | "eps = norm(x - x_gmres)"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": null,
134 | "metadata": {
135 | "collapsed": true
136 | },
137 | "outputs": [],
138 | "source": []
139 | }
140 | ],
141 | "metadata": {
142 | "kernelspec": {
143 | "display_name": "Julia 0.6.0",
144 | "language": "julia",
145 | "name": "julia-0.6"
146 | },
147 | "language_info": {
148 | "file_extension": ".jl",
149 | "mimetype": "application/julia",
150 | "name": "julia",
151 | "version": "0.6.0"
152 | }
153 | },
154 | "nbformat": 4,
155 | "nbformat_minor": 2
156 | }
157 |
--------------------------------------------------------------------------------
/packages/Latexify.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# HW 3 - Package Exploration\n",
8 | "### Package - Latexify.jl\n",
9 | "Madison Maloney\n",
10 | "\n",
11 | "This notebook gives an overview of Julia's Latexify package. Latexify can be used to provide LaTeX formatting for objects in Julia. The formatted objects can be displayed within the Julia environment, and the formatting string can also be copied to a user's clipboard and pasted into an environment that accepts LaTeX formatting.\n",
12 | "\n",
13 | "Version used: 1.2.0\n",
14 | "\n",
15 | "Package used: Latexify.jl\n",
16 | "\n",
17 | "Features explored: \n",
18 | "\n",
19 | "Reference: https://pkg.julialang.org/docs/Latexify"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 12,
25 | "metadata": {},
26 | "outputs": [
27 | {
28 | "data": {
29 | "text/plain": [
30 | "v\"1.2.0\""
31 | ]
32 | },
33 | "execution_count": 12,
34 | "metadata": {},
35 | "output_type": "execute_result"
36 | }
37 | ],
38 | "source": [
39 | "VERSION"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "First you need to add the Latexify package and prepare to use it by implementing the code block below."
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": 37,
52 | "metadata": {},
53 | "outputs": [
54 | {
55 | "name": "stdout",
56 | "output_type": "stream",
57 | "text": [
58 | "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
59 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\madis\\.julia\\environments\\v1.2\\Project.toml`\n",
60 | "\u001b[90m [no changes]\u001b[39m\n",
61 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\madis\\.julia\\environments\\v1.2\\Manifest.toml`\n",
62 | "\u001b[90m [no changes]\u001b[39m\n",
63 | "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
64 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\madis\\.julia\\environments\\v1.2\\Project.toml`\n",
65 | " \u001b[90m [37e2e46d]\u001b[39m\u001b[92m + LinearAlgebra \u001b[39m\n",
66 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\madis\\.julia\\environments\\v1.2\\Manifest.toml`\n",
67 | "\u001b[90m [no changes]\u001b[39m\n"
68 | ]
69 | }
70 | ],
71 | "source": [
72 | "using Pkg\n",
73 | "Pkg.add(\"Latexify\")\n",
74 | "using Latexify"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "## Basic Latexify\n",
82 | "\n",
83 | "Latexify works for the following Julia object types:\n",
84 | "* Expressions\n",
85 | "* Strings\n",
86 | "* Numbers\n",
87 | "* Missings' Missing type (references Missings.jl)\n",
88 | "* Symbols\n",
89 | "* Symbolic expressions (references SymEngine.jl)\n",
90 | "* DataFrame (references DataFrames.jl)\n",
91 | "* Any shape of array containing a mix of any of the above types\n",
92 | "* ParameterizedFunctions (references DifferentialEquations.jl)\n",
93 | "* ReactionNetworks (references DifferentialEquations.jl)\n",
94 | "\n",
95 | "Supported LaTeX output environments include:\n",
96 | "* no env (:raw)\n",
97 | "* inline (:inline)\n",
98 | "* align (:align)\n",
99 | "* equation (:equation)\n",
100 | "* array (:array)\n",
101 | "* tabular (:table)\n",
102 | "* markdown table (:mdtable)\n",
103 | "* markdown text (:mdtext)\n",
104 | "* chemical arrow notation (:chem)\n",
105 | "\n",
106 | "Latexify automatically chooses and implements an output environment based on the inputs to the latexify function, but users can enforce a different output environment by executing\n",
107 | "\n",
108 | " env = () \n",
109 | "\n",
110 | "where () is filled by one of the enclosed examples from the list above (i.e. env = :table)\n",
111 | "\n",
112 | "\n",
113 | "For basic functionality, this notebook will focus on a few inputs that do not rely on any other packages. For more information on the inputs which interact with other packages, refer to the documentation linked at the top of this notebook.\n",
114 | "\n",
115 | "To use the basic latexify function of this package, define an input and call latexify, giving it the input that was just defined."
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {},
121 | "source": [
122 | "__Expressions__\n",
123 | "\n",
124 | "The example below defines an expression and latexifies it."
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": 111,
130 | "metadata": {},
131 | "outputs": [
132 | {
133 | "name": "stdout",
134 | "output_type": "stream",
135 | "text": [
136 | "typeof(exp) = Expr\n"
137 | ]
138 | },
139 | {
140 | "data": {
141 | "text/latex": [
142 | "$1 + \\frac{3}{x^{2}} - e^{-1}$"
143 | ],
144 | "text/plain": [
145 | "L\"$1 + \\frac{3}{x^{2}} - e^{-1}$\""
146 | ]
147 | },
148 | "execution_count": 111,
149 | "metadata": {},
150 | "output_type": "execute_result"
151 | }
152 | ],
153 | "source": [
154 | "exp = :(1+3/x^2-e^-1);\n",
155 | "@show typeof(exp)\n",
156 | "latexify(exp)"
157 | ]
158 | },
159 | {
160 | "cell_type": "markdown",
161 | "metadata": {},
162 | "source": [
163 | "__Strings__\n",
164 | "\n",
165 | "The example below defines a string and latexifies it."
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": 112,
171 | "metadata": {},
172 | "outputs": [
173 | {
174 | "name": "stdout",
175 | "output_type": "stream",
176 | "text": [
177 | "typeof(str) = String\n"
178 | ]
179 | },
180 | {
181 | "data": {
182 | "text/latex": [
183 | "$\\frac{\\beta\\left( n \\right) \\cdot e^{-2 \\cdot x}}{x + n!} - \\log_{10}\\left( \\frac{x}{n} \\right)$"
184 | ],
185 | "text/plain": [
186 | "L\"$\\frac{\\beta\\left( n \\right) \\cdot e^{-2 \\cdot x}}{x + n!} - \\log_{10}\\left( \\frac{x}{n} \\right)$\""
187 | ]
188 | },
189 | "execution_count": 112,
190 | "metadata": {},
191 | "output_type": "execute_result"
192 | }
193 | ],
194 | "source": [
195 | "str = \"beta(n)*e^(-2x)/(x+n!)-(log10(x/n))\"\n",
196 | "@show typeof(str)\n",
197 | "latexify(str)"
198 | ]
199 | },
200 | {
201 | "cell_type": "markdown",
202 | "metadata": {},
203 | "source": [
204 | "__Numbers__\n",
205 | "\n",
206 | "The example below defines an array of numbers and latexifies it."
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": 114,
212 | "metadata": {},
213 | "outputs": [
214 | {
215 | "name": "stdout",
216 | "output_type": "stream",
217 | "text": [
218 | "typeof(A) = Array{Int64,2}\n"
219 | ]
220 | },
221 | {
222 | "data": {
223 | "text/latex": [
224 | "\\begin{equation}\n",
225 | "\\left[\n",
226 | "\\begin{array}{ccc}\n",
227 | "1 & 2 & 3 \\\\\n",
228 | "4 & 5 & 6 \\\\\n",
229 | "7 & 8 & 9 \\\\\n",
230 | "\\end{array}\n",
231 | "\\right]\n",
232 | "\\end{equation}\n"
233 | ],
234 | "text/plain": [
235 | "L\"\\begin{equation}\n",
236 | "\\left[\n",
237 | "\\begin{array}{ccc}\n",
238 | "1 & 2 & 3 \\\\\n",
239 | "4 & 5 & 6 \\\\\n",
240 | "7 & 8 & 9 \\\\\n",
241 | "\\end{array}\n",
242 | "\\right]\n",
243 | "\\end{equation}\n",
244 | "\""
245 | ]
246 | },
247 | "execution_count": 114,
248 | "metadata": {},
249 | "output_type": "execute_result"
250 | }
251 | ],
252 | "source": [
253 | "A = [1 2 3;4 5 6;7 8 9]\n",
254 | "@show typeof(A)\n",
255 | "as = latexify(A)"
256 | ]
257 | },
258 | {
259 | "cell_type": "markdown",
260 | "metadata": {},
261 | "source": [
262 | "## Copy to Clipboard\n",
263 | "\n",
264 | "The function copy_to_clipboard can be used to copy the formatting string for the LaTeX-formatted object to a computer's clipboard. This string can conveniently be pasted into any interface that accepts LaTeX formatting. Once copy_to_clipboard is set to true, it will continue to copy to the clipboard for subsequent latexify calls until it is set to false.\n",
265 | "\n",
266 | "__Example__"
267 | ]
268 | },
269 | {
270 | "cell_type": "code",
271 | "execution_count": 122,
272 | "metadata": {},
273 | "outputs": [
274 | {
275 | "data": {
276 | "text/latex": [
277 | "$\\frac{x}{2 \\cdot k_{1} + x^{2}}$"
278 | ],
279 | "text/plain": [
280 | "L\"$\\frac{x}{2 \\cdot k_{1} + x^{2}}$\""
281 | ]
282 | },
283 | "execution_count": 122,
284 | "metadata": {},
285 | "output_type": "execute_result"
286 | }
287 | ],
288 | "source": [
289 | "c = \"x/(2*k_1+x^2)\"\n",
290 | "copy_to_clipboard(true)\n",
291 | "latexify(c)"
292 | ]
293 | },
294 | {
295 | "cell_type": "markdown",
296 | "metadata": {},
297 | "source": [
298 | "If you run the code block above, the formatting string for c will be copied to your clipboard. Pasting after running the code block yields:\n",
299 | "\n",
300 | " $\\frac{x}{2 \\cdot k_{1} + x^{2}}$\n",
301 | " \n",
302 | "You can test out the formatting string by pasting it into an interface such as Overleaf."
303 | ]
304 | },
305 | {
306 | "cell_type": "markdown",
307 | "metadata": {},
308 | "source": [
309 | "## Formatting\n",
310 | "\n",
311 | "Latexify allows you to format the numbers in the output. A few helpful examples include printf formatting, significant digit specification (using FancyNumberFormatting within Latexify.jl), digits beyond the decimal (using FancyNumberFormatting within Latexify.jl), and exponential formatting (using StyledNumberFormatting within Latexify.jl)."
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 145,
317 | "metadata": {},
318 | "outputs": [
319 | {
320 | "data": {
321 | "text/latex": [
322 | "$3.142$"
323 | ],
324 | "text/plain": [
325 | "L\"$3.142$\""
326 | ]
327 | },
328 | "execution_count": 145,
329 | "metadata": {},
330 | "output_type": "execute_result"
331 | }
332 | ],
333 | "source": [
334 | "num = pi\n",
335 | "latexify(num;fmt = \"%.3f\")\n",
336 | "# notice number of digits after decimal"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 143,
342 | "metadata": {},
343 | "outputs": [
344 | {
345 | "data": {
346 | "text/latex": [
347 | "$1.4865e+06$"
348 | ],
349 | "text/plain": [
350 | "L\"$1.4865e+06$\""
351 | ]
352 | },
353 | "execution_count": 143,
354 | "metadata": {},
355 | "output_type": "execute_result"
356 | }
357 | ],
358 | "source": [
359 | "num2 = 1486480\n",
360 | "latexify(num2;fmt = \"%.4e\")\n",
361 | "# notice number of digits after decimal and exp form"
362 | ]
363 | },
364 | {
365 | "cell_type": "code",
366 | "execution_count": 156,
367 | "metadata": {},
368 | "outputs": [
369 | {
370 | "name": "stdout",
371 | "output_type": "stream",
372 | "text": [
373 | "num3 = 1.2001\n"
374 | ]
375 | },
376 | {
377 | "data": {
378 | "text/latex": [
379 | "$1.2$"
380 | ],
381 | "text/plain": [
382 | "L\"$1.2$\""
383 | ]
384 | },
385 | "execution_count": 156,
386 | "metadata": {},
387 | "output_type": "execute_result"
388 | }
389 | ],
390 | "source": [
391 | "num3 = 1.200100\n",
392 | "latexify(num3;fmt = FancyNumberFormatter(2))\n",
393 | "# notice significant digits"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 169,
399 | "metadata": {},
400 | "outputs": [
401 | {
402 | "data": {
403 | "text/latex": [
404 | "$1.23 \\cdot 10^{7}$"
405 | ],
406 | "text/plain": [
407 | "L\"$1.23 \\cdot 10^{7}$\""
408 | ]
409 | },
410 | "execution_count": 169,
411 | "metadata": {},
412 | "output_type": "execute_result"
413 | }
414 | ],
415 | "source": [
416 | "num4 = 12345e3\n",
417 | "latexify(num4;fmt = FancyNumberFormatter(3))\n",
418 | "# notice the exp form now appears as *10^7"
419 | ]
420 | },
421 | {
422 | "cell_type": "markdown",
423 | "metadata": {},
424 | "source": [
425 | "You can also set default formatting by using set_default."
426 | ]
427 | },
428 | {
429 | "cell_type": "code",
430 | "execution_count": 171,
431 | "metadata": {},
432 | "outputs": [
433 | {
434 | "data": {
435 | "text/latex": [
436 | "$123.45600$"
437 | ],
438 | "text/plain": [
439 | "L\"$123.45600$\""
440 | ]
441 | },
442 | "execution_count": 171,
443 | "metadata": {},
444 | "output_type": "execute_result"
445 | }
446 | ],
447 | "source": [
448 | "set_default(fmt = \"%.5f\")\n",
449 | "latexify(123.456)\n",
450 | "# notice formatting was implemented without including fmt in the latexify call"
451 | ]
452 | },
453 | {
454 | "cell_type": "markdown",
455 | "metadata": {},
456 | "source": [
457 | "## Realistic Example\n",
458 | "\n",
459 | "Imagine you have written code for a project but are cutting it close to the deadline to submit your code along with a write-up of your results. Rather than wasting time writing the relevant equations in LaTeX format for your write up, you can use the Latexify package to save the day.\n"
460 | ]
461 | },
462 | {
463 | "cell_type": "code",
464 | "execution_count": 203,
465 | "metadata": {},
466 | "outputs": [
467 | {
468 | "data": {
469 | "text/latex": [
470 | "\\begin{equation}\n",
471 | "\\left[\n",
472 | "\\begin{array}{ccc}\n",
473 | "5.0 & 5.0 & -1.0 \\\\\n",
474 | "10.0 & 10.0 & -2.0 \\\\\n",
475 | "15.0 & 15.0 & -3.0 \\\\\n",
476 | "\\end{array}\n",
477 | "\\right]\n",
478 | "\\end{equation}\n"
479 | ],
480 | "text/plain": [
481 | "L\"\\begin{equation}\n",
482 | "\\left[\n",
483 | "\\begin{array}{ccc}\n",
484 | "5.0 & 5.0 & -1.0 \\\\\n",
485 | "10.0 & 10.0 & -2.0 \\\\\n",
486 | "15.0 & 15.0 & -3.0 \\\\\n",
487 | "\\end{array}\n",
488 | "\\right]\n",
489 | "\\end{equation}\n",
490 | "\""
491 | ]
492 | },
493 | "execution_count": 203,
494 | "metadata": {},
495 | "output_type": "execute_result"
496 | }
497 | ],
498 | "source": [
499 | "A = [5 5 -1;10 10 -2;15 15 -3];\n",
500 | "latexify(A,fmt = \"%.1f\")"
501 | ]
502 | },
503 | {
504 | "cell_type": "code",
505 | "execution_count": 204,
506 | "metadata": {},
507 | "outputs": [
508 | {
509 | "data": {
510 | "text/latex": [
511 | "\\begin{equation}\n",
512 | "\\left[\n",
513 | "\\begin{array}{c}\n",
514 | "2.4 \\\\\n",
515 | "-1.1 \\\\\n",
516 | "5.9 \\\\\n",
517 | "\\end{array}\n",
518 | "\\right]\n",
519 | "\\end{equation}\n"
520 | ],
521 | "text/plain": [
522 | "L\"\\begin{equation}\n",
523 | "\\left[\n",
524 | "\\begin{array}{c}\n",
525 | "2.4 \\\\\n",
526 | "-1.1 \\\\\n",
527 | "5.9 \\\\\n",
528 | "\\end{array}\n",
529 | "\\right]\n",
530 | "\\end{equation}\n",
531 | "\""
532 | ]
533 | },
534 | "execution_count": 204,
535 | "metadata": {},
536 | "output_type": "execute_result"
537 | }
538 | ],
539 | "source": [
540 | "x = [2.4;-1.1;5.9];\n",
541 | "latexify(x,fmt = \"%.1f\")"
542 | ]
543 | },
544 | {
545 | "cell_type": "code",
546 | "execution_count": 206,
547 | "metadata": {},
548 | "outputs": [
549 | {
550 | "data": {
551 | "text/latex": [
552 | "\\begin{equation}\n",
553 | "\\left[\n",
554 | "\\begin{array}{c}\n",
555 | "0.6 \\\\\n",
556 | "1.2 \\\\\n",
557 | "1.8 \\\\\n",
558 | "\\end{array}\n",
559 | "\\right]\n",
560 | "\\end{equation}\n"
561 | ],
562 | "text/plain": [
563 | "L\"\\begin{equation}\n",
564 | "\\left[\n",
565 | "\\begin{array}{c}\n",
566 | "0.6 \\\\\n",
567 | "1.2 \\\\\n",
568 | "1.8 \\\\\n",
569 | "\\end{array}\n",
570 | "\\right]\n",
571 | "\\end{equation}\n",
572 | "\""
573 | ]
574 | },
575 | "execution_count": 206,
576 | "metadata": {},
577 | "output_type": "execute_result"
578 | }
579 | ],
580 | "source": [
581 | "b = A*x;\n",
582 | "latexify(b,fmt = \"%.1f\") "
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 208,
588 | "metadata": {},
589 | "outputs": [
590 | {
591 | "data": {
592 | "text/latex": [
593 | "$Ax = b$"
594 | ],
595 | "text/plain": [
596 | "L\"$Ax = b$\""
597 | ]
598 | },
599 | "execution_count": 208,
600 | "metadata": {},
601 | "output_type": "execute_result"
602 | }
603 | ],
604 | "source": [
605 | "eqn = \"Ax=b\"\n",
606 | "latexify(eqn)"
607 | ]
608 | },
609 | {
610 | "attachments": {},
611 | "cell_type": "markdown",
612 | "metadata": {},
613 | "source": [
614 | "Pasting these into Overleaf, I can quickly get:\n",
615 | "\n",
616 | "https://github.com/madisonmaloney/Random/blob/master/example.PNG\n",
617 | "\n",
618 | "This saves time and could even help you catch a mistake in your equation if the LaTeX version looks different than the equation you thought you implemented."
619 | ]
620 | }
621 | ],
622 | "metadata": {
623 | "kernelspec": {
624 | "display_name": "Julia 1.2.0",
625 | "language": "julia",
626 | "name": "julia-1.2"
627 | },
628 | "language_info": {
629 | "file_extension": ".jl",
630 | "mimetype": "application/julia",
631 | "name": "julia",
632 | "version": "1.2.0"
633 | }
634 | },
635 | "nbformat": 4,
636 | "nbformat_minor": 2
637 | }
638 |
--------------------------------------------------------------------------------
/packages/POMDPs.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "# This is a brief and by no means complete introduction to the POMDPs.jl package.\n",
10 | "# MDPs are Markov decision processes, described by states S, actions A, and rewards R.\n",
11 | "# POMDPs are partially-observable Markov decision processes. In POMDPs, states are uncertain, so we add observations O.\n",
12 | "# Applications of POMDPs include collision avoidance systems, path planning, and long-term infrastructure maintenance.\n",
13 | "# --------------------------------------------------------------------------------------------------------------------\n",
14 | "# Julia v 0.6.0\n",
15 | "# Packages used: POMDPs.jl (https://github.com/JuliaPOMDP/POMDPs.jl)\n",
16 | "# Tutorials are available for MDPs (the grid world example - http://nbviewer.jupyter.org/github/sisl/POMDPs.jl/blob/master/examples/GridWorld.ipynb)\n",
17 | "# and for POMDPs (the tiger problem - http://nbviewer.jupyter.org/github/sisl/POMDPs.jl/blob/master/examples/Tiger.ipynb)"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 2,
23 | "metadata": {},
24 | "outputs": [
25 | {
26 | "name": "stderr",
27 | "output_type": "stream",
28 | "text": [
29 | "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mRecompiling stale cache file /Users/gitanjali/.julia/lib/v0.6/SortingAlgorithms.ji for module SortingAlgorithms.\n",
30 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mRecompiling stale cache file /Users/gitanjali/.julia/lib/v0.6/POMDPs.ji for module POMDPs.\n",
31 | "\u001b[39m"
32 | ]
33 | }
34 | ],
35 | "source": [
36 | "using POMDPs"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": 3,
42 | "metadata": {},
43 | "outputs": [
44 | {
45 | "data": {
46 | "text/plain": [
47 | "sequal (generic function with 1 method)"
48 | ]
49 | },
50 | "execution_count": 3,
51 | "metadata": {},
52 | "output_type": "execute_result"
53 | }
54 | ],
55 | "source": [
56 | "# In this example, we will consider how to set up an MDP to calculate optimal decision-making \n",
57 | "# for a portfolio of buildings in earthquake country.\n",
58 | "# This example is adapted from the grid world example linked above.\n",
59 | "\n",
60 | "# Initialize an MDP type\n",
61 | "type PortfolioMDP <: MDP{Int64, Int64} # MDP{StateType, ActionType}\n",
62 | "end\n",
63 | "\n",
64 | "# for simplicity, buildings can be damaged or undamaged\n",
65 | "\n",
66 | "struct PortfolioState \n",
67 | " b1::Int64 # damage state of Building 1\n",
68 | " b2::Int64 # damage state of Building 2\n",
69 | " done::Bool # terminal state \n",
70 | "end\n",
71 | "\n",
72 | "# checks if the position of two states are the same\n",
73 | "sequal(s1::PortfolioState, s2::PortfolioState) = s1.b1 == s2.b1 && s1.b1 == s2.b2"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": 4,
79 | "metadata": {},
80 | "outputs": [
81 | {
82 | "data": {
83 | "text/plain": [
84 | "PortfolioAct (generic function with 1 method)"
85 | ]
86 | },
87 | "execution_count": 4,
88 | "metadata": {},
89 | "output_type": "execute_result"
90 | }
91 | ],
92 | "source": [
93 | "# Defining binary states - damaged or not damaged - and setting up an initial state constructor\n",
94 | "PortfolioState(b1::Int64, b2::Int64) = PortfolioState(b1,b2, false)\n",
95 | "\n",
96 | "# Defining actions - we can retrofit a building or repair a building, if it is damaged, or do nothing\n",
97 | "# a1 - action associated with Building 1; a2 - action associated with Building 2\n",
98 | "PortfolioAct(a1::Int64, a2::Int64) = PortfolioAct(a1,a2)"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": 5,
104 | "metadata": {},
105 | "outputs": [
106 | {
107 | "data": {
108 | "text/plain": [
109 | "Portfolio"
110 | ]
111 | },
112 | "execution_count": 5,
113 | "metadata": {},
114 | "output_type": "execute_result"
115 | }
116 | ],
117 | "source": [
118 | "# Defining the building MDP type - we will use this as a data container\n",
119 | "type Portfolio <: MDP{PortfolioState, PortfolioAct} # Note that our MDP is parametarized by the state and the action\n",
120 | " size::Int64 # number of buildings in the portfolio\n",
121 | " reward_states::Vector{PortfolioState} # the states in which agent recieves reward\n",
122 | " reward_values::Vector{Float64} # reward values for those states\n",
123 | " tprob::Float64 # probability of transitioning to the desired state\n",
124 | " gamma::Float64 # discount factor - this expresses how much we value future reward relative to present rewards (from 0 to 1)\n",
125 | "end\n",
126 | "\n",
127 | "# we use key worded arguments so we can change any of the values we pass in \n",
128 | "function Portfolio(size = 2,\n",
129 | " reward_states::Vector{PortfolioState}=[PortfolioState(0,0), PortfolioState(0,1), PortfolioState(1,0), PortfolioState(1,1)], # reward states\n",
130 | " reward_values::Vector{Float64}=rv = [10000,4000,6000,-100000], # reward values\n",
131 | " tprob::Float64=0.5, # tprob\n",
132 | " gamma::Float64=0.5) # discount factor\n",
133 | " return Portfolio(size, rs, rv, tp, gamma)\n",
134 | "end"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": 6,
140 | "metadata": {},
141 | "outputs": [
142 | {
143 | "data": {
144 | "text/plain": [
145 | "4-element Array{PortfolioState,1}:\n",
146 | " PortfolioState(0, 0, false)\n",
147 | " PortfolioState(0, 1, false)\n",
148 | " PortfolioState(1, 0, false)\n",
149 | " PortfolioState(1, 1, false)"
150 | ]
151 | },
152 | "execution_count": 6,
153 | "metadata": {},
154 | "output_type": "execute_result"
155 | }
156 | ],
157 | "source": [
158 | "# we can now create a Portfolio mdp instance like this:\n",
159 | "mdp = Portfolio()\n",
160 | "mdp.reward_states # mdp contains all the default values from the constructor"
161 | ]
162 | },
163 | {
164 | "cell_type": "code",
165 | "execution_count": 7,
166 | "metadata": {},
167 | "outputs": [],
168 | "source": [
169 | "function POMDPs.states(mdp::Portfolio)\n",
170 | " s = PortfolioState[] # initialize an array of PortfolioStates\n",
171 | " # loop over all our states, remember there are two binary variables:\n",
172 | " # done (d)\n",
173 | " for d = 0:1, b1 = 0:1, b2 = 0:1\n",
174 | " push!(s, PortfolioState(b1,b2,d))\n",
175 | " end\n",
176 | " return s\n",
177 | "end;"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 8,
183 | "metadata": {},
184 | "outputs": [
185 | {
186 | "data": {
187 | "text/plain": [
188 | "PortfolioState(0, 0, false)"
189 | ]
190 | },
191 | "execution_count": 8,
192 | "metadata": {},
193 | "output_type": "execute_result"
194 | }
195 | ],
196 | "source": [
197 | "mdp = Portfolio()\n",
198 | "state_space = states(mdp);\n",
199 | "state_space[1]"
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": 9,
205 | "metadata": {},
206 | "outputs": [],
207 | "source": [
208 | "POMDPs.actions(mdp::Portfolio) = [0, 1, 2];"
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": 10,
214 | "metadata": {},
215 | "outputs": [],
216 | "source": [
217 | "function POMDPs.transition(mdp::Portfolio, state::PortfolioState, action::Int64)\n",
218 | " a = action\n",
219 | " x = state.x\n",
220 | " y = state.y\n",
221 | " \n",
222 | " if state.done\n",
223 | " return SparseCat([GridWorldState(b1, b2, true)], [1.0])\n",
224 | " elseif state in mdp.reward_states\n",
225 | " return SparseCat([GridWorldState(b1, b2, true)], [1.0])\n",
226 | " end\n",
227 | " \n",
228 | " prob = 0.7\n",
229 | " \n",
230 | " return prob\n",
231 | "end\n",
232 | " "
233 | ]
234 | },
235 | {
236 | "cell_type": "code",
237 | "execution_count": 11,
238 | "metadata": {},
239 | "outputs": [],
240 | "source": [
241 | "function POMDPs.reward(mdp::Portfolio, state::PortfolioState, action::Int64, statep::PortfolioState)\n",
242 | " if state.done\n",
243 | " return 0.0\n",
244 | " end\n",
245 | " r = 0.0\n",
246 | " n = length(mdp.reward_states)\n",
247 | " for i = 1:n\n",
248 | " if sequal(state, mdp.reward_states[i])\n",
249 | " r += mdp.reward_values[i]\n",
250 | " end\n",
251 | " end\n",
252 | " return r\n",
253 | "end;"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 12,
259 | "metadata": {},
260 | "outputs": [],
261 | "source": [
262 | "POMDPs.n_states(mdp::Portfolio) = 2*mdp.size\n",
263 | "POMDPs.n_actions(mdp::Portfolio) = 3\n",
264 | "POMDPs.discount(mdp::Portfolio) = mdp.gamma;"
265 | ]
266 | },
267 | {
268 | "cell_type": "code",
269 | "execution_count": null,
270 | "metadata": {},
271 | "outputs": [],
272 | "source": [
273 | "# install support tools we'll use for simulation\n",
274 | "#POMDPs.add(\"POMDPToolbox\")\n",
275 | "#Pkg.update(\"POMDPToolbox\")\n",
276 | "\n",
277 | "#mdp = Portfolio()\n",
278 | "#mdp.tprob=1.0\n",
279 | "#sim(mdp, Portfolio(4,1), max_steps=10) do s\n",
280 | "# println(\"state is: $s\")\n",
281 | "# a = :right\n",
282 | "# println(\"moving $a\")\n",
283 | "# return a\n",
284 | "#end;"
285 | ]
286 | },
287 | {
288 | "cell_type": "code",
289 | "execution_count": 13,
290 | "metadata": {},
291 | "outputs": [
292 | {
293 | "name": "stdout",
294 | "output_type": "stream",
295 | "text": [
296 | "Package already installed\n"
297 | ]
298 | },
299 | {
300 | "name": "stderr",
301 | "output_type": "stream",
302 | "text": [
303 | "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mCloning DiscreteValueIteration from https://github.com/JuliaPOMDP/DiscreteValueIteration.jl\n",
304 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mPrecompiling module POMDPToolbox.\n",
305 | "\u001b[39m"
306 | ]
307 | },
308 | {
309 | "ename": "LoadError",
310 | "evalue": "\u001b[91mMethodError: no method matching ordered_vector(::#PortfolioAct, ::POMDPToolbox.##51#52{Portfolio}, ::Array{Int64,1}, ::Int64, ::String)\u001b[0m\nClosest candidates are:\n ordered_vector(\u001b[91m::Type\u001b[39m, ::Function, ::Any, ::Any, ::Any) at /Users/gitanjali/.julia/v0.6/POMDPToolbox/src/model/ordered_spaces.jl:31\n ordered_vector(\u001b[91m::Type\u001b[39m, ::Function, ::Any, ::Any, ::Any, \u001b[91m::Any\u001b[39m) at /Users/gitanjali/.julia/v0.6/POMDPToolbox/src/model/ordered_spaces.jl:31\u001b[39m",
311 | "output_type": "error",
312 | "traceback": [
313 | "\u001b[91mMethodError: no method matching ordered_vector(::#PortfolioAct, ::POMDPToolbox.##51#52{Portfolio}, ::Array{Int64,1}, ::Int64, ::String)\u001b[0m\nClosest candidates are:\n ordered_vector(\u001b[91m::Type\u001b[39m, ::Function, ::Any, ::Any, ::Any) at /Users/gitanjali/.julia/v0.6/POMDPToolbox/src/model/ordered_spaces.jl:31\n ordered_vector(\u001b[91m::Type\u001b[39m, ::Function, ::Any, ::Any, ::Any, \u001b[91m::Any\u001b[39m) at /Users/gitanjali/.julia/v0.6/POMDPToolbox/src/model/ordered_spaces.jl:31\u001b[39m",
314 | "",
315 | "Stacktrace:",
316 | " [1] \u001b[1m(::DiscreteValueIteration.##call#2#3)\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Array{Float64,1}, ::Bool, ::Type{T} where T, ::Portfolio\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/Users/gitanjali/.julia/v0.6/DiscreteValueIteration/src/vanilla.jl:32\u001b[22m\u001b[22m",
317 | " [2] \u001b[1mDiscreteValueIteration.ValueIterationPolicy\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Portfolio\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/Users/gitanjali/.julia/v0.6/DiscreteValueIteration/src/vanilla.jl:23\u001b[22m\u001b[22m",
318 | " [3] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:515\u001b[22m\u001b[22m"
319 | ]
320 | }
321 | ],
322 | "source": [
323 | "# first let's load the value iteration module\n",
324 | "POMDPs.add(\"DiscreteValueIteration\")\n",
325 | "using DiscreteValueIteration\n",
326 | "\n",
327 | "# initialize the problem\n",
328 | "mdp = Portfolio()\n",
329 | "\n",
330 | "# initialize the solver\n",
331 | "# max_iterations: maximum number of iterations value iteration runs for (default is 100)\n",
332 | "# belres: the value of Bellman residual used in the solver (defualt is 1e-3)\n",
333 | "solver = ValueIterationSolver(max_iterations=100, belres=1e-3)\n",
334 | "\n",
335 | "# initialize the policy by passing in your problem\n",
336 | "policy = ValueIterationPolicy(mdp) \n",
337 | "\n",
338 | "# solve for an optimal policy\n",
339 | "# if verbose=false, the text output will be supressed (false by default)\n",
340 | "solve(solver, mdp, policy, verbose=true);"
341 | ]
342 | },
343 | {
344 | "cell_type": "code",
345 | "execution_count": null,
346 | "metadata": {},
347 | "outputs": [],
348 | "source": []
349 | }
350 | ],
351 | "metadata": {
352 | "kernelspec": {
353 | "display_name": "Julia 0.6.0",
354 | "language": "julia",
355 | "name": "julia-0.6"
356 | },
357 | "language_info": {
358 | "file_extension": ".jl",
359 | "mimetype": "application/julia",
360 | "name": "julia",
361 | "version": "0.6.0"
362 | }
363 | },
364 | "nbformat": 4,
365 | "nbformat_minor": 2
366 | }
367 |
--------------------------------------------------------------------------------
/packages/README.md:
--------------------------------------------------------------------------------
1 | Put your IJulia notebook for homework 3 in this directory. Please name your notebook "packagename.ipynb" (where packagename is replaced by the package you explored)
2 |
3 | the 2015 folder holds old notebooks from the 2015 class, and most notebooks there should run with Julia v0.4
4 |
--------------------------------------------------------------------------------
/packages/TensorFlow.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# TensorFlow.jl\n",
8 | "\n",
9 | "This notebook is an introduction to TensorFlow package by using Julia 0.6.2. Packages we used here includes TensorFlow, MNIST, Distributions. \n",
10 | "\n",
11 | "Created by Lijing Wang, based on the TensorFlow.jl instructions and examples. "
12 | ]
13 | },
14 | {
15 | "cell_type": "code",
16 | "execution_count": null,
17 | "metadata": {},
18 | "outputs": [],
19 | "source": [
20 | "Pkg.add(\"TensorFlow\")\n",
21 | "Pkg.add(\"MNIST\")\n",
22 | "Pkg.add(\"Distributions\")"
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "## 1. Load MNIST dataset\n",
30 | "\n",
31 | "This part is based on the tutorial for Package MNIST"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 1,
37 | "metadata": {},
38 | "outputs": [
39 | {
40 | "data": {
41 | "text/plain": [
42 | "DataLoader"
43 | ]
44 | },
45 | "execution_count": 1,
46 | "metadata": {},
47 | "output_type": "execute_result"
48 | }
49 | ],
50 | "source": [
51 | "using MNIST\n",
52 | "\n",
53 | "#Define a type for loading dataset\n",
54 | "type DataLoader\n",
55 | " cur_id::Int\n",
56 | " order::Vector{Int}\n",
57 | "end\n",
58 | "\n",
59 | "DataLoader() = DataLoader(1, shuffle(1:60000))"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 2,
65 | "metadata": {},
66 | "outputs": [
67 | {
68 | "data": {
69 | "text/plain": [
70 | "next_batch (generic function with 1 method)"
71 | ]
72 | },
73 | "execution_count": 2,
74 | "metadata": {},
75 | "output_type": "execute_result"
76 | }
77 | ],
78 | "source": [
79 | "#Read the training dataset by the giving batch_size\n",
80 | "function next_batch(loader::DataLoader, batch_size)\n",
81 | " x = zeros(Float32, batch_size, 784)\n",
82 | " y = zeros(Float32, batch_size, 10)\n",
83 | " for i in 1:batch_size\n",
84 | " x[i, :] = trainfeatures(loader.order[loader.cur_id])\n",
85 | " label = trainlabel(loader.order[loader.cur_id])\n",
86 | " y[i, Int(label)+1] = 1.0\n",
87 | " loader.cur_id += 1\n",
88 | " if loader.cur_id > 60000\n",
89 | " loader.cur_id = 1\n",
90 | " end\n",
91 | " end\n",
92 | " x, y\n",
93 | "end"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 3,
99 | "metadata": {},
100 | "outputs": [
101 | {
102 | "data": {
103 | "text/plain": [
104 | "load_test_set (generic function with 2 methods)"
105 | ]
106 | },
107 | "execution_count": 3,
108 | "metadata": {},
109 | "output_type": "execute_result"
110 | }
111 | ],
112 | "source": [
113 | "#Load test set\n",
114 | "function load_test_set(N=10000)\n",
115 | " x = zeros(Float32, N, 784)\n",
116 | " y = zeros(Float32, N, 10)\n",
117 | " for i in 1:N\n",
118 | " x[i, :] = testfeatures(i)\n",
119 | " label = testlabel(i)\n",
120 | " y[i, Int(label)+1] = 1.0 \n",
121 | " #Julia API assumes 1-based indexing\n",
122 | " end\n",
123 | " x,y\n",
124 | "end"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": 4,
130 | "metadata": {},
131 | "outputs": [
132 | {
133 | "data": {
134 | "text/plain": [
135 | "DataLoader(1, [30662, 41926, 20864, 58356, 1826, 38976, 33608, 47393, 12972, 10720 … 30512, 38238, 42194, 17062, 44210, 58204, 2111, 1841, 33052, 56974])"
136 | ]
137 | },
138 | "execution_count": 4,
139 | "metadata": {},
140 | "output_type": "execute_result"
141 | }
142 | ],
143 | "source": [
144 | "loader = DataLoader()"
145 | ]
146 | },
147 | {
148 | "cell_type": "markdown",
149 | "metadata": {},
150 | "source": [
151 | "## 2. Start TensorFlow session\n",
152 | "In this session if you have GPU to be used, you can link it to your session. \n",
153 | "\n",
154 | "#### Julia Code\n",
155 | "ENV[\"TF_USE_GPU\"] = \"1\""
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": 5,
161 | "metadata": {},
162 | "outputs": [
163 | {
164 | "name": "stderr",
165 | "output_type": "stream",
166 | "text": [
167 | "2018-01-31 15:02:18.625400: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.\n",
168 | "2018-01-31 15:02:18.625433: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.\n",
169 | "2018-01-31 15:02:18.625440: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.\n",
170 | "2018-01-31 15:02:18.625446: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.\n"
171 | ]
172 | },
173 | {
174 | "data": {
175 | "text/plain": [
176 | "Session(Ptr{Void} @0x0000000118be7090)"
177 | ]
178 | },
179 | "execution_count": 5,
180 | "metadata": {},
181 | "output_type": "execute_result"
182 | }
183 | ],
184 | "source": [
185 | "using TensorFlow\n",
186 | "sess = Session()"
187 | ]
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {},
192 | "source": [
193 | "## 3. Build a softmax regression Model"
194 | ]
195 | },
196 | {
197 | "cell_type": "markdown",
198 | "metadata": {},
199 | "source": [
200 | "### Set up Placeholders"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 6,
206 | "metadata": {},
207 | "outputs": [
208 | {
209 | "data": {
210 | "text/plain": [
211 | ""
212 | ]
213 | },
214 | "execution_count": 6,
215 | "metadata": {},
216 | "output_type": "execute_result"
217 | }
218 | ],
219 | "source": [
220 | "x = placeholder(Float32)\n",
221 | "y_ = placeholder(Float32) #True Y"
222 | ]
223 | },
224 | {
225 | "cell_type": "markdown",
226 | "metadata": {},
227 | "source": [
228 | "### Initiate Parameters"
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": 7,
234 | "metadata": {},
235 | "outputs": [],
236 | "source": [
237 | "W = Variable(zeros(Float32, 784, 10))\n",
238 | "b = Variable(zeros(Float32, 10))\n",
239 | "\n",
240 | "run(sess, global_variables_initializer())"
241 | ]
242 | },
243 | {
244 | "cell_type": "markdown",
245 | "metadata": {},
246 | "source": [
247 | "### Predicted Class and Loss Function"
248 | ]
249 | },
250 | {
251 | "cell_type": "code",
252 | "execution_count": 8,
253 | "metadata": {},
254 | "outputs": [
255 | {
256 | "data": {
257 | "text/plain": [
258 | ""
259 | ]
260 | },
261 | "execution_count": 8,
262 | "metadata": {},
263 | "output_type": "execute_result"
264 | }
265 | ],
266 | "source": [
267 | "y = nn.softmax(x*W + b) #Predict Y\n",
268 | "\n",
269 | "#Cross Entropy Loss Function\n",
270 | "cross_entropy = reduce_mean(-reduce_sum(y_ .* log(y), axis=[2]))"
271 | ]
272 | },
273 | {
274 | "cell_type": "markdown",
275 | "metadata": {},
276 | "source": [
277 | "### Train the model with mini-batch and Gradient Descent Optimizer\n",
278 | "This step may take a while. "
279 | ]
280 | },
281 | {
282 | "cell_type": "code",
283 | "execution_count": 9,
284 | "metadata": {},
285 | "outputs": [],
286 | "source": [
287 | "train_step = train.minimize(train.GradientDescentOptimizer(.00001), cross_entropy)\n",
288 | "for i in 1:1000\n",
289 | " batch = next_batch(loader, 100)\n",
290 | " run(sess, train_step, Dict(x=>batch[1], y_=>batch[2]))\n",
291 | "end"
292 | ]
293 | },
294 | {
295 | "cell_type": "markdown",
296 | "metadata": {},
297 | "source": [
298 | "### Evaluate the model with test set"
299 | ]
300 | },
301 | {
302 | "cell_type": "code",
303 | "execution_count": 10,
304 | "metadata": {},
305 | "outputs": [
306 | {
307 | "name": "stdout",
308 | "output_type": "stream",
309 | "text": [
310 | "0.9093\n"
311 | ]
312 | }
313 | ],
314 | "source": [
315 | "correct_prediction = equal(indmax(y,2), indmax(y_, 2))\n",
316 | "accuracy=reduce_mean(cast(correct_prediction, Float32))\n",
317 | "testx, testy = load_test_set()\n",
318 | "\n",
319 | "println(run(sess, accuracy, Dict(x=>testx, y_=>testy)))"
320 | ]
321 | },
322 | {
323 | "cell_type": "markdown",
324 | "metadata": {},
325 | "source": [
326 | "We only have one softmax node so we may not get high accuracy model. "
327 | ]
328 | },
329 | {
330 | "cell_type": "markdown",
331 | "metadata": {},
332 | "source": [
333 | "## 4. Build a multi-layer convolutional network\n",
334 | "\n",
335 | "Here we set up CNN model for MNIST. "
336 | ]
337 | },
338 | {
339 | "cell_type": "code",
340 | "execution_count": 11,
341 | "metadata": {},
342 | "outputs": [
343 | {
344 | "data": {
345 | "text/plain": [
346 | "Session(Ptr{Void} @0x0000000117270150)"
347 | ]
348 | },
349 | "execution_count": 11,
350 | "metadata": {},
351 | "output_type": "execute_result"
352 | }
353 | ],
354 | "source": [
355 | "loader = DataLoader()\n",
356 | "\n",
357 | "session = Session(Graph())\n"
358 | ]
359 | },
360 | {
361 | "cell_type": "markdown",
362 | "metadata": {},
363 | "source": [
364 | "### Initiate weight_variable W and bias_variable b"
365 | ]
366 | },
367 | {
368 | "cell_type": "code",
369 | "execution_count": 12,
370 | "metadata": {},
371 | "outputs": [
372 | {
373 | "data": {
374 | "text/plain": [
375 | "bias_variable (generic function with 1 method)"
376 | ]
377 | },
378 | "execution_count": 12,
379 | "metadata": {},
380 | "output_type": "execute_result"
381 | }
382 | ],
383 | "source": [
384 | "function weight_variable(shape)\n",
385 | " initial = map(Float32, rand(Normal(0, .001), shape...))\n",
386 | " return Variable(initial)\n",
387 | "end\n",
388 | "\n",
389 | "function bias_variable(shape)\n",
390 | " initial = fill(Float32(.1), shape...)\n",
391 | " return Variable(initial)\n",
392 | "end"
393 | ]
394 | },
395 | {
396 | "cell_type": "markdown",
397 | "metadata": {},
398 | "source": [
399 | "### Build 2D convolutional function and perform maxpooling"
400 | ]
401 | },
402 | {
403 | "cell_type": "code",
404 | "execution_count": 13,
405 | "metadata": {},
406 | "outputs": [
407 | {
408 | "data": {
409 | "text/plain": [
410 | "max_pool_2x2 (generic function with 1 method)"
411 | ]
412 | },
413 | "execution_count": 13,
414 | "metadata": {},
415 | "output_type": "execute_result"
416 | }
417 | ],
418 | "source": [
419 | "function conv2d(x, W)\n",
420 | " nn.conv2d(x, W, [1, 1, 1, 1], \"SAME\")\n",
421 | "end\n",
422 | "\n",
423 | "function max_pool_2x2(x)\n",
424 | " nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], \"SAME\")\n",
425 | "end"
426 | ]
427 | },
428 | {
429 | "cell_type": "markdown",
430 | "metadata": {},
431 | "source": [
432 | "### Build your CNN"
433 | ]
434 | },
435 | {
436 | "cell_type": "code",
437 | "execution_count": 14,
438 | "metadata": {},
439 | "outputs": [
440 | {
441 | "data": {
442 | "text/plain": [
443 | ""
444 | ]
445 | },
446 | "execution_count": 14,
447 | "metadata": {},
448 | "output_type": "execute_result"
449 | }
450 | ],
451 | "source": [
452 | "using Distributions\n",
453 | "\n",
454 | "\n",
455 | "x = placeholder(Float32)\n",
456 | "y_ = placeholder(Float32)\n",
457 | "\n",
458 | "W_conv1 = weight_variable([5, 5, 1, 32])\n",
459 | "b_conv1 = bias_variable([32])\n",
460 | "\n",
461 | "x_image = reshape(x, [-1, 28, 28, 1])\n",
462 | " \n",
463 | "h_conv1 = nn.relu(conv2d(x_image, W_conv1) + b_conv1)\n",
464 | "h_pool1 = max_pool_2x2(h_conv1)\n",
465 | "\n",
466 | "W_conv2 = weight_variable([5, 5, 32, 64])\n",
467 | "b_conv2 = bias_variable([64])\n",
468 | "\n",
469 | "h_conv2 = nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)\n",
470 | "h_pool2 = max_pool_2x2(h_conv2)\n",
471 | "\n",
472 | "W_fc1 = weight_variable([7*7*64, 1024])\n",
473 | "b_fc1 = bias_variable([1024])\n",
474 | "\n",
475 | "h_pool2_flat = reshape(h_pool2, [-1, 7*7*64])\n",
476 | "h_fc1 = nn.relu(h_pool2_flat * W_fc1 + b_fc1)\n",
477 | "\n",
478 | "keep_prob = placeholder(Float32)\n",
479 | "h_fc1_drop = nn.dropout(h_fc1, keep_prob)\n",
480 | "\n",
481 | "W_fc2 = weight_variable([1024, 10])\n",
482 | "b_fc2 = bias_variable([10])\n",
483 | "\n",
484 | "y_conv = nn.softmax(h_fc1_drop * W_fc2 + b_fc2)\n",
485 | "\n",
486 | "cross_entropy = reduce_mean(-reduce_sum(y_.*log(y_conv), axis=[2]))\n",
487 | "\n",
488 | "train_step = train.minimize(train.AdamOptimizer(1e-4), cross_entropy)\n",
489 | "\n",
490 | "correct_prediction = equal(indmax(y_,2), indmax(y_conv, 2))\n",
491 | "\n",
492 | "accuracy = reduce_mean(cast(correct_prediction, Float32))\n"
493 | ]
494 | },
495 | {
496 | "cell_type": "markdown",
497 | "metadata": {},
498 | "source": [
499 | "### Initialized parameters"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": 15,
505 | "metadata": {},
506 | "outputs": [],
507 | "source": [
508 | "run(session, global_variables_initializer())"
509 | ]
510 | },
511 | {
512 | "cell_type": "markdown",
513 | "metadata": {},
514 | "source": [
515 | "### Training with Minibatch, dropout and AdamOptimizer\n",
516 | "This step may take a while. "
517 | ]
518 | },
519 | {
520 | "cell_type": "code",
521 | "execution_count": 17,
522 | "metadata": {},
523 | "outputs": [
524 | {
525 | "name": "stderr",
526 | "output_type": "stream",
527 | "text": [
528 | "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 1, training accuracy 0.12\n",
529 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 101, training accuracy 0.84\n",
530 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 201, training accuracy 0.96\n",
531 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 301, training accuracy 0.96\n",
532 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 401, training accuracy 0.96\n",
533 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 501, training accuracy 0.98\n",
534 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 601, training accuracy 0.96\n",
535 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 701, training accuracy 0.9\n",
536 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 801, training accuracy 0.96\n",
537 | "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mstep 901, training accuracy 0.96\n",
538 | "\u001b[39m"
539 | ]
540 | }
541 | ],
542 | "source": [
543 | "for i in 1:1000\n",
544 | " batch = next_batch(loader, 50)\n",
545 | " if i%100 == 1\n",
546 | " train_accuracy = run(session, accuracy, Dict(x=>batch[1], y_=>batch[2], keep_prob=>1.0))\n",
547 | " info(\"step $i, training accuracy $train_accuracy\")\n",
548 | " end\n",
549 | " run(session, train_step, Dict(x=>batch[1], y_=>batch[2], keep_prob=>.5))\n",
550 | "end"
551 | ]
552 | },
553 | {
554 | "cell_type": "markdown",
555 | "metadata": {},
556 | "source": [
557 | "### Test accuracy"
558 | ]
559 | },
560 | {
561 | "cell_type": "code",
562 | "execution_count": 18,
563 | "metadata": {},
564 | "outputs": [
565 | {
566 | "name": "stderr",
567 | "output_type": "stream",
568 | "text": [
569 | "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mtest accuracy 0.9784\n",
570 | "\u001b[39m"
571 | ]
572 | }
573 | ],
574 | "source": [
575 | "testx, testy = load_test_set()\n",
576 | "test_accuracy = run(session, accuracy, Dict(x=>testx, y_=>testy, keep_prob=>1.0))\n",
577 | "info(\"test accuracy $test_accuracy\")"
578 | ]
579 | },
580 | {
581 | "cell_type": "markdown",
582 | "metadata": {},
583 | "source": [
584 | "Yeah! Now we get good prediction for MNIST by CNN!\n",
585 | "\n",
586 | "Here we do not have overfitting because of the dropout. "
587 | ]
588 | },
589 | {
590 | "cell_type": "code",
591 | "execution_count": null,
592 | "metadata": {},
593 | "outputs": [],
594 | "source": []
595 | }
596 | ],
597 | "metadata": {
598 | "kernelspec": {
599 | "display_name": "Julia 0.6.2",
600 | "language": "julia",
601 | "name": "julia-0.6"
602 | },
603 | "language_info": {
604 | "file_extension": ".jl",
605 | "mimetype": "application/julia",
606 | "name": "julia",
607 | "version": "0.6.2"
608 | }
609 | },
610 | "nbformat": 4,
611 | "nbformat_minor": 2
612 | }
613 |
--------------------------------------------------------------------------------
/packages/juliafem.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# CME 257 HW 3\n",
8 | "## Ren Gibbons \n",
9 | "\n",
10 | "This notebook contains a simple example of a finite element assembly procedure for a 1D truss simulation using the Julia package JuliaFEM. The example is slightly modified from a tutorial page on JuliaFEM found [here](http://www.juliafem.org/examples/2017-08-30-defining-new-problems-to-juliafem/). This notebook was greated using Julia 0.6.2 version and the package JuliaFEM. Here, I import JuliaFEM for use in this notebook and check the status."
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 9,
16 | "metadata": {},
17 | "outputs": [
18 | {
19 | "name": "stdout",
20 | "output_type": "stream",
21 | "text": [
22 | " - JuliaFEM 0.3.6\n"
23 | ]
24 | }
25 | ],
26 | "source": [
27 | "using JuliaFEM\n",
28 | "import JuliaFEM: assemble!\n",
29 | "Pkg.status(\"JuliaFEM\")"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "Next, I define the element type Truss as a descendant of FieldProblem, where FieldProblem describes a general framework for a finite element problem to be solved."
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": 16,
42 | "metadata": {},
43 | "outputs": [],
44 | "source": [
45 | "type Truss <: FieldProblem\n",
46 | "end"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "Here, I define the assembly procecedure. First, the element stiffness matrices are created given geometrc and material properties. Next, I assign the values of the element stiffness matrix to the appropriate degrees of freedom in the global stiffness matrix."
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 11,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "data": {
63 | "text/plain": [
64 | "assemble! (generic function with 37 methods)"
65 | ]
66 | },
67 | "execution_count": 11,
68 | "metadata": {},
69 | "output_type": "execute_result"
70 | }
71 | ],
72 | "source": [
73 | "function assemble!(assembly::Assembly, problem::Problem{Truss},\n",
74 | " element::Element{Seg2}, time)\n",
75 | " X = element(\"geometry\", time) # get geometry\n",
76 | " L = norm(X[2] - X[1]) # calculate length of rod\n",
77 | " E = 1.0 # Young's modulus\n",
78 | " A = 1.0 # cross sectional area of element\n",
79 | " q = 1.0 # magnitude of force\n",
80 | " Ke = E*A/L*[1.0 -1.0; -1.0 1.0] # element stiffness matrix \n",
81 | " fe = q*L/2*[1.0, 1.0] # element force vector\n",
82 | " gdofs = get_gdofs(problem, element) # find global dofs of element\n",
83 | " add!(assembly.K, gdofs, gdofs, Ke) # element -> global \n",
84 | " add!(assembly.f, gdofs, fe) # force -> global\n",
85 | "end"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "Finally, I test the element by defining a 1D structure with three truss elements with nodes located at positions $$x=[0.0,1.0,2.0,3.0]$$"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 18,
98 | "metadata": {
99 | "scrolled": true
100 | },
101 | "outputs": [
102 | {
103 | "name": "stderr",
104 | "output_type": "stream",
105 | "text": [
106 | "04-Feb 18:03:09:WARNING:root:assemble!(problem) will be deprecated. Use assemble!(problem, time)\n",
107 | "04-Feb 18:03:09:WARNING:root:Assembling elements for problem test problem: seems that problem is uninitialized. To initialize problem, use `initialize!(problem, time)`.\n",
108 | "04-Feb 18:03:09:INFO:root:Initializing problem test problem at time 0.0 automatically.\n",
109 | "04-Feb 18:03:09:WARNING:root:This is default assemble! function. Decreased performance can be expected without preallocation of memory. One should implement `assemble_elements!(problem, assembly, elements, time)` function.\n"
110 | ]
111 | }
112 | ],
113 | "source": [
114 | "X = Dict(1 => [0.0], 2 => [1.0], 3 => [2.0], 4 => [3.0]) # Location of nodes (or Truss element end points)\n",
115 | "element1 = Element(Seg2, [1, 2]) # First 1D element (with ends at node 1 and 2)\n",
116 | "element2 = Element(Seg2, [2, 3]) # Second 1D element (with ends at node 2 and 3)\n",
117 | "element3 = Element(Seg2, [3, 4]) # Third 1D element (with ends at node 3 and 4)\n",
118 | "elements = [element1, element2, element3]\n",
119 | "update!(elements, \"geometry\", X) # Create the mesh\n",
120 | "problem = Problem(Truss, \"test problem\", 1) # Initialize problem\n",
121 | "add_elements!(problem, elements) # Add elements to problem\n",
122 | "assemble!(problem) # Assemble stiffness matrix and force vector for Truss"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "I verify the correctness of the element implementation by printing the stiffness matrix and force vector."
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": 19,
135 | "metadata": {},
136 | "outputs": [
137 | {
138 | "data": {
139 | "text/plain": [
140 | "4×4 Array{Float64,2}:\n",
141 | " 1.0 -1.0 0.0 0.0\n",
142 | " -1.0 2.0 -1.0 0.0\n",
143 | " 0.0 -1.0 2.0 -1.0\n",
144 | " 0.0 0.0 -1.0 1.0"
145 | ]
146 | },
147 | "execution_count": 19,
148 | "metadata": {},
149 | "output_type": "execute_result"
150 | }
151 | ],
152 | "source": [
153 | "full(problem.assembly.K)"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": 20,
159 | "metadata": {},
160 | "outputs": [
161 | {
162 | "data": {
163 | "text/plain": [
164 | "4×1 Array{Float64,2}:\n",
165 | " 0.5\n",
166 | " 1.0\n",
167 | " 1.0\n",
168 | " 0.5"
169 | ]
170 | },
171 | "execution_count": 20,
172 | "metadata": {},
173 | "output_type": "execute_result"
174 | }
175 | ],
176 | "source": [
177 | "full(problem.assembly.f)"
178 | ]
179 | },
180 | {
181 | "cell_type": "markdown",
182 | "metadata": {},
183 | "source": [
184 | "This example only shows how to create a new element type. This [page](http://www.juliafem.org/JuliaFEM.jl/v0.3.2/examples.html) provides an insightful tutorial for setting up and solving a simple finite element problem for a single element 2D quad elastic element."
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": null,
190 | "metadata": {},
191 | "outputs": [],
192 | "source": []
193 | }
194 | ],
195 | "metadata": {
196 | "kernelspec": {
197 | "display_name": "Julia 0.6.2",
198 | "language": "julia",
199 | "name": "julia-0.6"
200 | },
201 | "language_info": {
202 | "file_extension": ".jl",
203 | "mimetype": "application/julia",
204 | "name": "julia",
205 | "version": "0.6.2"
206 | }
207 | },
208 | "nbformat": 4,
209 | "nbformat_minor": 2
210 | }
211 |
--------------------------------------------------------------------------------
/resources.md:
--------------------------------------------------------------------------------
1 | # Julia Resources
2 |
3 | Additional resources and websites for learning and exploring Julia will be posted here
4 |
5 | ## General Resources
6 | * [Julia's documentation](https://docs.julialang.org/en/stable/)
7 | * [Julia's package listing](https://pkg.julialang.org/) - see what you can install!
8 | * [JuliaBox](https://juliabox.org) - try out Julia in your browser
9 |
10 |
11 | ## Tutorials
12 | Julia maintains a list [here](https://julialang.org/learning/)
13 |
14 | * [A Deep Introduction to Julia for Data Science and Scientific Computing](ucidatascienceinitiative.github.io/IntroToJulia/) - An excellent introduction to Julia. Highly recommended.
15 | * [Introducing Julia Wikibook](https://en.wikibooks.org/wiki/Introducing_Julia) - A good reference guide for a lot of Julia's basic functionality
16 |
17 | ## LLVM
18 |
19 | [LLVM for Grad Students](http://www.cs.cornell.edu/~asampson/blog/llvm.html)
20 |
21 | ## Packages
22 |
23 | * [IJulia](https://github.com/JuliaLang/IJulia.jl) - create notebooks that mix markdown and live code.
24 |
25 | ## Git
26 | * Git homepage: https://git-scm.com/
27 | * Git documentation: https://git-scm.com/doc
28 | * Git Book: https://git-scm.com/book/en/v2
29 |
30 |
31 | ## Fun Stuff
32 |
33 | * [Unicode Table](https://docs.julialang.org/en/stable/manual/unicode-input) - make all your variables unicode!
34 |
--------------------------------------------------------------------------------
/syllabus.md:
--------------------------------------------------------------------------------
1 | # CME 257 - Advanced Topics in Scientific Computing with Julia
2 |
3 | * Location: [McCullough 126 ](https://campus-map.stanford.edu/?srch=Mccull126)
4 | * Time: Mondays 3:00 PM -- 4:20 PM
5 | * This 1-unit course meets once a week on weeks 2 through 9 of the quarter
6 |
7 | ---
8 |
9 | * Instructor: Arun Jambulapati
10 | * Email: [jmblpati@stanford](mailto:jmblpati@stanford.edu)
11 | * Office Hours: Friday 1-3 Huang Basement or by appointment
12 |
13 | ---
14 |
15 |
16 |
17 | ### Overview
18 |
19 | This course is intended to introduce students who are already somewhat familiar with scientific computing to what Julia has to offer. It is not intended to be a first course in programming (there are several other short courses that are more suited for this purpose). This is not to say that Julia itself is a difficult language to learn - it is pretty easy to get started with a search online, but that the course will cover material that likely won't be useful if you don't have some context to place it in.
20 |
21 | Briefly, this course covers topics that you may find useful when using Julia for a research project. In addition to an introduction to the Julia language, we'll cover version control using git, interfacing with existing code (including in different languages), performance, and parallelism. Examples and assignments will focus on the workhorses of scientific computing - data structures, linear algebra and optimization, but we'll also try to showcase some applications.
22 |
23 | A tentative schedule follows (topics may bleed into more than one day)
24 |
25 | 1. Programming in Julia
26 | * Basics / getting started
27 | * Object Oriented Features, type system
28 | 2. The Julia Ecosystem
29 | * Packages (common scientific computing packages, and how to use them) - graphics, math packages, graph theory, optimization, etc.
30 | * Intro to git
31 | 3. Practical Julia
32 | * How to write your own package/module, more GitHub
33 | * Interfacing with C/Fortran shared object libraries, Python, other languages
34 | 4. Julia in the Wild
35 | * Debugging, performance, etc.
36 | * Special topics - parallelism, linear algebra implementations, blas calls, etc.,
37 |
38 |
39 | I want to cover material that is interesting and useful to you. If there is something in particular that you would like to see, let me know!
40 |
41 | ### Goals
42 |
43 | * Learn the basics of Julia and git, and learn where to find more information
44 | * Explore some of the scientific computing capabilities of Julia
45 | * Learn how to create a Julia package for your own projects
46 |
47 | ### Resources
48 |
49 | * See [resources.md](resources.md)
50 | * You are also welcome to use Google to find tutorials or for troubleshooting solutions online. Let me know if you find a good resource!
51 |
52 | ### Prerequisites
53 | Prerequisites are rather soft - if you're thinking of using Julia for a project, you probably have sufficient background. The only real prerequisite is some experience with programming (this course isn't an introduction to programming).
54 |
55 | What you may find useful to have seen before (not necessarily in any great depth)
56 | * Experience with one or more scientific computing languages (e.g. Python, Matlab, R, ...)
57 | * Some idea of what matrices and vectors are, and some idea of why one may wish to find a minimum or maximum of a function
58 | * Some familiarity with the Unix shell
59 | * Data structures (e.g. at the level of CS106B)
60 |
61 | No prior experience with Julia or git is required.
62 |
63 |
64 | ## Structure of the course
65 | ### Class
66 | I hope to make the class as hands-on and interactive as possible. It is fairly easy to learn the basics of Julia on your own, so instead of lectures on something you can find better explained on Google, the class will be held in a workshop-like style. Please bring a laptop to class.
67 |
68 | ### Homework
69 | Since the best way to learn a new language is through practice, there will be some homework.
70 | * There will be 5 or 6 assignments, which should take about half an hour to an hour each.
71 | * On weeks without a full homework, there will be a short exercise (less than 15 minutes to complete)
72 |
73 | Homework will generally be assigned after class on Mondays, and will be before the next class.
74 |
75 | This is a 1-unit course, and the goal is not to overwhelm you. If you are stuck on an assignment, please ask for help instead of spending large amounts of time searching for an answer.
76 |
77 | ### Grading
78 | This course is offered only on a credit/no-credit basis. You don't need to worry about perfect assignments, just make sure code runs as expected and is intelligible. Assignments (50%) will be marked as complete or incomplete, and I will also try to give useful feedback. I expect you to try all the homework (unless otherwise noted), and if you're stuck a partial answer or short note explaining what you've tried will be fine. You can have one 24-hour extension, otherwise late assignments will not be marked complete or receive feedback.
79 |
80 | Participation (50%) will also be important given the style of the course. I expect you to come to class (there are only 8), but understand that you may have unavoidable conflicts come up. If you need to miss a class please send me an email.
81 |
82 | ## Stanford Policies
83 |
84 | ### Honor Code
85 | This course is intended to be collaborative. You can (and should) work with other students in class and on homework. You should turn in your own solutions (don't copy others). If you worked closely with someone or found an answer on the web, please acknowledge the source of your solution.
86 |
87 |
88 | ### Students with Documented Disabilities
89 | Students who may need an academic accommodation based on the impact of a disability must initiate the request with the Office of Accessible Education (OAE). Professional staff will evaluate the request with required documentation, recommend reasonable accommodations, and prepare an Accommodation Letter for faculty dated in the current quarter in which the request is being made. Students should contact the OAE as soon as possible since timely notice is needed to coordinate accommodations. The OAE is located at 563 Salvatierra Walk (phone: 723-1066, URL: http://studentaffairs.stanford.edu/oae).
90 |
--------------------------------------------------------------------------------