This repository contains the course materials from Math 157: Intro to Mathematical Software.
Creative Commons BY-SA 4.0 license.
License: OTHER
Math 157: Intro to Mathematical Software
UC San Diego, winter 2018
March 2, 2018: Julia (part 1 of 3)
This lecture was prepared in cooperation with Alyson Deines (Center for Communications Research), who will be giving the second part of this series on Monday, March 5.
Administrivia:
CAPE evaluations are available! They close Monday, March 19 at 8am. Since this course is highly experimental, your feedback will be very helpful for shaping future offerings.
HW5 has been returned.
Final project news:
After consulting with the TAs, I propose to change the due date to Sunday, March 18 at 8pm (for both parts).
If you are unable to be on campus during week 10 to meet with your group, please contact me immediately to set up a workaround.
For part 1, problem 4b, if you have trouble with the exhaustive search you may also do random sampling for this part. (This might be necessary to make 4c terminate.)
For part 2, you should proceed with your first choice provided that you fill out the [Google Form](https://docs.google.com/forms/d/e/1FAIpQLScw8RMWcCdWyzJK2xUgBNHoCgIkQcFmhw0_AYjZZu_j5KcKkg/viewform?usp=sf_linkGoogle Form) by 8pm tonight. Otherwise, you may be assigned a topic at random.
Advance notice for week 9:
No sections on Tuesday, March 6. However, during this time, you are welcome to use APM 6402 as a study room; we will also try to monitor the chat room.
Thomas's office hours (usually Tuesday 11am-12pm) are moved to Friday 11:30am-12:50pm.
Peter's office hours (usually Wednesday 3-5pm) are moved to Wednesday 5-7pm.
There will be an extra virtual office hour Thursday 6-7pm.
Advance notice for week 10:
No lectures on Monday, March 12 or Wednesday, March 14. You may wish to use this time to meet your assigned group for Part 2 of the final project.
There will be a lecture on Friday, March 16, on the topic of "Where to go from here?" This lecture will not be counted for course attendance; that is, the last lecture for which attendance counts is Friday, March 9.
My office hours on Thursday, March 15 are cancelled. All other sections and office hours meet as scheduled.
Comments on the homework
I will not collect this assignment before Saturday, 8pm.
General: if you are having trouble with your kernel crashing, you may wish to try:
restarting your project (click the wrench icon, then look under "Project control");
switching from "SageMath 8.1" to "Python 3 (Ubuntu Linux)".
Problem 1: The "Orange" dataset is not available via statsmodels. However, you can access it directly from R using the
rpy2
module..Problem 3a: The mpg dataset is in ggplot, not statsmodels:
from ggplot import mpg
Pause for additional questions.
Julia: an overview
What is Julia?
Development on Julia was started in 2009
Goal: high-level + fast and geared toward numerical/technical computation
Officially launched in 2012
Currently on v0.6.2
MIT licensed, free and open source
Features
Multiple dispatch: providing the ability to define function behavior across many combinations of argument types
Dynamic type system: types for documentation, optimization, and dispatch
Good performance, approaching that of statically-compiled languages like C
Built-in package manager
Lisp-like macros and other metaprogramming facilities
Call Python functions: use the PyCall package
Call C functions directly: no wrappers or special APIs
Powerful shell-like capabilities for managing other processes
Designed for parallelism and distributed computation
Coroutines: lightweight “green” threading
User-defined types are as fast and compact as built-ins
Automatic generation of efficient, specialized code for different argument types
Elegant and extensible conversions and promotions for numeric and other types
Efficient support for Unicode, including but not limited to UTF-8
Competition
Python + Numpy/Scipy/Numba/Cython
Matlab
Octave
R
Why Julia when we already have good options?
https://julialang.org/blog/2012/02/why-we-created-julia
Short story: the creators wanted to start from scratch and build a high-level language specifically for scientific computing.
Reservations
Since Julia is so new, it suffers from some associated defects.
Still not stable (no 1.0 in sight)
Smaller ecosystem than the competition
This Worksheet: Basic Syntax
Basic arithmetic
Assign variables
Comments
Documentation
Printing
Strings
Data structures
Loops
Array comprehensions
Conditionals
Functions
Multiple dispatch
Complex Numbers are also native:
Rationals:
Floats:
Unlike in Python (or Sage), integers are not by default created to arbitrary precision; however, "big" integers are readily available.
Assignment
Unicode allowed:
Can reassign without worrying about types:
Comments
Documentation
To see the documentation for a function foo: ?foo
search: typeof typejoin TypedSlot TypeError
Get the concrete type of x
.
search: div divrem DivideError A_rdiv_Bt A_rdiv_Bc A_ldiv_Bt A_ldiv_Bc A_ldiv_B!
The quotient from Euclidean division. Computes x/y
, truncated to an integer.
Tab-completion also works.
search: divrem
The quotient and remainder from Euclidean division. Equivalent to (div(x,y), rem(x,y))
or (x÷y, x%y)
.
Strings
Use " " or """ """ (if you want to use " in your string)
Single quotes '' give a character. (This is different from Python.)
syntax: invalid character literal
Stacktrace:
[1] include_string(::String, ::String) at ./loading.jl:522
String interpolation: a $ allows us to insert variables into a string.
String concatenation:
This tripped me up:
MethodError: no method matching +(::String, ::String)
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:424
Stacktrace:
[1] include_string(::String, ::String) at ./loading.jl:522
Data Structures
tuples
arrays
dictionaries
Yeah, yeah, indexing starts at 1 and not 0. Beware when switching back and forth between Julia and Python!
As in Python, tuples are immutable:
MethodError: no method matching setindex!(::Tuple{Int64,Int64,Char}, ::Int64, ::Int64)
Stacktrace:
[1] include_string(::String, ::String) at ./loading.jl:522
Arrays can be very Pythonesque:
or more Matlabish:
Array are mutable:
Arrays can have weird shapes.
As with Python, be careful when assigning arrays!
Instead, make a copy.
Dictionaries! Different syntax than in Python.
As with Python, dictionaries are not ordered.
KeyError: key 2 not found
Stacktrace:
[1] getindex(::Dict{String,Float64}, ::Int64) at ./dict.jl:474
[2] include_string(::String, ::String) at ./loading.jl:522
Loops
while loops
for loops
Note the end
command to close the loop. That's needed because unlike in Python, whitespace in Julia is not syntactic.
Note: starts at 1 and prints 10, inclusive. Again, this is a different convention than Python.
Can use to fill in arrays
Nested for loops:
Array comprehensions
A more Julia way to do this is to use an array comprehension (analogous to a Python list comprehension).
As in Python, array comprehensions allow conditionals:
Conditionals
Functions
Three ways to define a function:
Like Python, the functions work on whatever type makes sense...
but don't work when they don't make sense.
MethodError: no method matching +(::String, ::Int64)
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:424
+(::Complex{Bool}, ::Real) at complex.jl:247
+(::Char, ::Integer) at char.jl:40
...
Stacktrace:
[1] addthree(::String) at ./In[94]:1
[2] include_string(::String, ::String) at ./loading.jl:522
Mutating vs Non-Mutating functions
Mutating functions alter the input to the function, non-mutating functions do not.
Non-mutating:
Mutating (use an !)
Functions as inputs
The Python map
function has a Julia analogue:
You can also broadcast: f.(v) is the same as map(f, v). (The dot does not have its Python meaning because Julia does not have a class hierarchy.)
Multiple Dispatch
This does not have a Python analogue
You can define multiple functions of the same name, but declaring the input type. This gives you one generic function with multiple methods; Julia will decide at runtime which to use based on the input.
Which definition am I using?
And we can also make a fall-back method: