📚 The CoCalc Library - books, templates and other resources
License: OTHER
Math 157: Intro to Mathematical Software
UC San Diego, winter 2018
March 7, 2018: Julia (part 3 of 3)
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.
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 final project group.
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.
Final project news
Both parts will be collected Sunday, March 18 at 8pm.
The groups for part 2 have been assigned. See the file
final_project_groups.md
in the shared project; then contact your group members as soon as possible. (I have created workspaces and chat rooms which may help with this; these are described in the same file.)For part 1, problem 4b, instead of the exhaustive search, do random sampling with samples. Ditto for 4c.
A significant portion of the lecture hour on Friday, March 9 will be devoted to questions about Part 1 of the final project. I would recommend having a look at Part 1 before Friday if you have not yet done so.
The Nemo project
Since Julia was designed to be a general language for scientific computation, it does not include much of the functionality of a computer algebra system. In Python, we can remedy this by using Sage. Recently, the Nemo project was created to provide some (although for now only a little) of the same functionality in Julia.
In Nemo, mathematical objects are generally constructed by first constructing a parent object which represents the collection of all objects of a particular type. So for example, Nemo big integers are constructed using the special object ZZ
which stands for the collection of all integers.
Some more examples from this list:
MethodError: no method matching (::Nemo.FlintRationalField)(::Float64)
Closest candidates are:
FlintRationalField(::Nemo.fmpq) at /ext/julia/julia-0.6.2/share/julia/site/v0.6/Nemo/src/flint/fmpq.jl:776
FlintRationalField(::Nemo.fmpz, ::Nemo.fmpz) at /ext/julia/julia-0.6.2/share/julia/site/v0.6/Nemo/src/flint/fmpq.jl:774
FlintRationalField(::Nemo.fmpz) at /ext/julia/julia-0.6.2/share/julia/site/v0.6/Nemo/src/flint/fmpq.jl:766
...
Stacktrace:
[1] include_string(::String, ::String) at ./loading.jl:522
By the way, real numbers in Nemo automatically handle interval arithmetic; that is, they keep track of error bounds as you carry out operations. This functionality is provided by the Arb library.
Performance comparisons
Here is an example from the book Julia High Performance by way of Stack Overflow. Switch to the Python 3 (Ubuntu Linux) kernel now.
Now switch the kernel back to Julia.
Note that the second approach is 2x faster than scipy and about 100x faster than basic Python; however, the first approach is no faster than basic Python. Why the discrepancy?
Note that u_sum
is originally created as an integer 0, and then at the first step it is converted to a floating-point 0. That doesn't seem like a big penalty to pay, and at runtime it isn't.
But remember that Julia's performance benefits derive from it having a just-in-time (JIT) compiler. The issue here is that the Julia compiler sees that this conversion is necessary and is forced to compile in a different (and less efficient) way because of this. This phenomenon is known as type instability.
The moral is that, while Julia attempts to make it easier to write efficient code, this is not entirely automatic; one must still pay some attention to the underlying structure of the language in order to make things not just run, but run efficiently.
Here is an example of a different issue.
What is going on here? I avoided type instability and I still have slow code.
This time, the issue is that the global scope does not allow type specificity. That is, anything time-sensitive should be done inside a function call.
See this blog post for other examples of easy traps to fall into when trying to write Julia code.
More examples
The page Julia by Example has some basic examples which might be of use when getting started with the language. Here are a few of them.
Printing
Error handling
MethodError: no method matching pop!(::Array{Any,1}, ::Int64)
Closest candidates are:
pop!(::IntSet, ::Integer) at intset.jl:77
pop!(::IntSet, ::Integer, ::Any) at intset.jl:80
pop!(::ObjectIdDict, ::ANY) at associative.jl:445
...
Stacktrace:
[1] backtrace() at ./error.jl:46
[2] include_string(::String, ::String) at ./loading.jl:522
[3] include_string(::Module, ::String, ::String) at /ext/julia/julia-0.6.2/share/julia/site/v0.6/Compat/src/Compat.jl:174
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /ext/julia/julia-0.6.2/share/julia/site/v0.6/IJulia/src/execute_request.jl:154
[5] (::Compat.#inner#16{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() at /ext/julia/julia-0.6.2/share/julia/site/v0.6/Compat/src/Compat.jl:496
[6] eventloop(::ZMQ.Socket) at /ext/julia/julia-0.6.2/share/julia/site/v0.6/IJulia/src/eventloop.jl:8
[7] (::IJulia.##14#17)() at ./task.jl:335
Plotting
Plotting is provided using an external package called Winston. (I don't know where the name comes from.)
DataFrames
The DataFrames package does what you might expect. Let's get the irises dataset as a CSV from here, then try the following.
For more on Julia...
The page Julia by Example has a few very basic examples which might be of use when getting started with the language.
The page Learning Julia has a number of links to tutorials, videos, sample Jupyter notebooks, and even books on the topic.