---
# System prepended metadata

title: Introduction to Julia
tags: [Worshop]

---

---

tags: Worshop

---

# Introduction to Julia

This document: https://hackmd.io/@jarno/S1g_QtAkK/edit
Zoom room: 613 3982 7511 (no password)
Zoom link: https://aalto.zoom.us/j/61339827511
Materials: https://github.com/AaltoRSE/julia-introduction#cloning-the-materials
Code of conduct: https://www.aalto.fi/en/aalto-university/code-of-conduct-values-into-practice

Garage: https://scicomp.aalto.fi/help/garage/
Seminar on Julia Packaging: https://nordic-rse.org/events/seminar-series/

## Schedule

### Tuesday

This schedule is tentative.

| Time  | Duration | Session
| ----  | -------- | -------
| 09:00	| 60min    | Intro to Julia
| 10:00	| 10min    | Coffee break
| 10:10	| 50min    | Functions
| 11:00	| 10min    | Coffee break
| 11:10	| 50min    | Control flow
| 12:00	| 10min    | Coffee break
| 12:10	| 50min    | IO
| 13:00	|          | End of day

### Wednesday


| Time  | Duration | Session
| ----  | -------- | -------
| 09:00	| 60min    | Plotting and Using Packages
| 10:00	| 10min    | Coffee break
| 10:10	| 50min    | Types and Multiple Dispatch
| 11:00	| 10min    | Coffee break
| 11:10	| 50min    | Development Practices
| 12:00	| 10min    | Coffee break
| 12:10	| 50min    | Performance Tips
| 13:00	| 	       | End of day



---------

## Icebreaker questions:

- What programming languages do you know?
  - Matlab: 7
  - Python: 13
  - IDL: 1
  - FORTRAN: 2
  - C: 7
  - C++: 5
  - Swift: 1
  - Objective-C: 1
  - BASIC: 1
  - R: 6
  - JavaScript: 2
  - LISP (Scheme, CLOS, Clojure): 1
  - Java: 3
  - Shell: 5
  - Scala: 1
  - Tcl: 1
  - ...

- Have you used Julia in a project?
  - No: 13
  - ...
  - ...

- What would you like to learn on this course?
  - Whether or not I should invest effort into learning Julia. +1 +1
  - What the benefits of using Julia are? +1
  - How difficult it would be to switch to Julia?
  - Tips on porting stuff from C or Python +1 +1
  - Is parallel programming possible with Julia? +1
  - Does Julia work with GPUs? +1 +1
  - ...
  - ...
  - ...


- Do you know any programming language?
    - Yes: oooooooooooooo
    - No: 

---------
### Things I love about Julia already

 * Native support for fractions: `1//3`
 * Multiplication without `*`: `2x`

---------

# Questions for day 1

- As questions like this
  - And answer like this
    - To reply to an answer, indent a bit more
- git URL: `git clone https://github.com/AaltoRSE/julia-introduction.git`
- I guess if you are using jupyter.cs.aalto.fi this is not needed, is the interface already there?
  - Yes
- How do you get out of the shell?
  - I guess this is in REPL.  backspace does it, or running any command.

- How to type in a emoji?
  - `\` starts a completion of unicode names, so you can `\alpha`TAB

- How are fractions stored internally?
    - Is it an int/int representation in the background or something more complicated?
    - `typeof(3//2)` shows me `Rational{Int64}` which may give some clues to storage
        - That's what is lost on me... 1//3?
        - I interpert this as "a new data type that stores the numerator and denominator separately as Int64" as a specialized type.  Just a guess though.
        - Two Int64 ints or just one somehow split in two? (asking from a memory allocation point of view)
        - Got it. sizeof(1) -> 8. sizeof(1//3) -> 16. Cheers! +1
        
- With so many pre-defined functions, isn't the namespace crowded (and we risk overwriting pre-defined stuff)?
    - I guess that the functions in a package you write yourself should always go on some namespace?

- `cell::Int8 = 1` but `cells = Int8[1 1]`. Is there a good reason for this.
  - You can also use 'cell = Int8(1)' and 'cells::Array{Int8, 2} = [1 1]'
      - Thanks! syntactic sugar it seems. Useful.
- is `broadcast` like broadcasting in numpy, increasing array dimensionality when needed?
   - Broadcast can do multiple things, depending what the inputs are. The following will use the dimension that matches
  ```julia
  a = [1,2]
  b = [1 2 3; 4 5 6]
  a .+ b
  ```
    - yes!  So like numpy.  do you find it hard to keep the different axes clear in your mind (I always have this issue with numpy, for complex stuff)
      - Yes, definitely.
    - You can also specify the index, but if it get's that complex I recommend using a for loop in a function. In Julia, loops are not slow.
- What is the difference in `[1 2]` syntax and `[1, 2]`?  (comma)  Looks like dimensionality of final result...?
  - space syntax makes an matrix instead of an array

- I did 
`function addone(value_to_add,obj)
    result=obj.+value_to_add
end
addone(3,[1,2])`
And now I would like to use the map function with the array. 
`A=zeros(5,5)
map(addone,A)`
But it's strange. Should the map function not allow me still an input parameter for the addone function?
  - ~~Map only accepts a function with 1 parameter (I think)~~
    - Okay. No, my addone function has two input arguments. The value to add and the object the value is added to.
  - You can use multiple arguments: `map(add_one, [1,2,3], 2)`
    - Solved, thanks.
  - was trying broadcast with my 2-argument version of poly2, and it silently works even with a missing argument.  I can also run poly2 with only one argument, and it works.  What happens to missing arguments?
    - Do you still have the one-argument version defined? Julia allows overloading.
        - yes!  that's it, it finds the old one.  When I try with no arguments it makes this clear:
      ```
      poly2()

      MethodError: no method matching poly2()
      Closest candidates are:
         poly2(!Matched::Any) at In[5]:1
         poly2(!Matched::Any, !Matched::Any) at In[16]:1
      ```
    - To list defined method use `methods(function_name)`


- Should one write in a Julia function `return variable` to return something out of a function, please?
  - You can use `return variable`, but if you don't, the function will return the result of the last statement. So this returns 3:
 ```julia
 function return_three()
    1+2
 end
 ```
- How do you get the inequality sign, please?
    - `\ne`TAB
- Should the 'include("../../epidemic_simple.jl")' in fact be 'include("../epidemic_simple.jl")', or did I somehow mess up the directory structure?
  - Yes, it should!

- is there a difference between "array" and "list", or is there only "array"?
  - ?
  - (Answered this below. sorry.) Julia has `Vector`, `Matrix` and `Array`. `Vector` is always 1D, `Matrix` is always 2D and `Array` can have any dimension.
- (continued from above) and is resizing a low-complexity operation?  e.g. in Python I think that numpy arrays are copied if resized, while lists are designed for in-place extension. (and I think this comes from the way `realloc` works, expanding continuous memory may return a new pointer so increasing size = possible copy.  and `list` has more overhead but designed for for expanding)
  - Not an expert, but I believe arrays are always contiguous in memory, so resizing requires copying.
      - Vectors in Julia seem to be similar to Python lists (i.e. dynamic arrays).
- is there a difference between "2-dimensional array" and "matrix"?
  - Julia has `Vector`, `Matrix` and `Array`. `Vector` is always 1D, `Matrix` is always 2D and `Array` can have any dimension.
  - A 2D Array may be implemented differently than Matrix. There could be performance differences, but the syntax is very similar.

- `using` to load packages - are things always put in one namespace?  (I guess one can make own namespace in package though?)

- why would I use a raw string instead of simply escaping the $, please?
    - If the string contains a lot of characters that need to be escaped, it is much more readable as a raw string.
- are specialized strings generic, so that they can be arbitrarily defined by libraries?
  - I cannot find a way of defining a custom specialized string. You can always define a struct that contains a string, but it has a different syntax.



-----------

# Feedback for day 1

Add "o" if you agree:
 - We did not have enough exercises: o
 - Needed more time for exercises: ooo
 - Exercises were too easy: 
     - these exercises were nice to begin with, but I would have liked additional exercises with increasing complexity +1 +1
 - Exercises were too hard: 

Write something positive here:

- Julia seems very expressive, and the basics were nicely covered. The exercises were simple but useful.
- It was nice of you to remind to use the breaks for getting up instead of sitting at the computer. It's something that is forgotten all too easily. +1 +1
- Nice exposition of language features and where it gets really powerful (e.g broadcasting functions)
- Very clear progression of material. It is a joy to grasp nearly everything on the first go.
- Nice introduction, covered the basics: this is what I expected from the course so far
- Yes, Julia seems to be a better version of Matlab. Lots of things reminded me of Matlab.

Write something constructive here:

- How about a Q&A session in-between on the exercises? It was a bit one-direction only. +1
- ...
- Would like to know more about back-end handling of data etc. 
- I follow your screen presentation all the time. I am confused when you mention exercises. Were we supposed to work on exercises besides your explanations?
  - Sorry for the confusion. Time for exercises was separate from the presentaion and always before a break (this is probably the source of the confusion.)
      - I cloned the github repo and follwed the standard exercises, not the pandemic one. When was I supposed to work on this one, please?
         - Some of the exercises (but not many) are related to the pandemic model. There are more tomorrow
         - There is one exercise in control flows and one in IO about the pandemic model
- I find it hard to follow what you are showing and typing at the same time, my screen is not big enough to display many windows (I tried to minimize zoom and have another window with the jupyter but that was not optimal and I missed what was going on in the HackMD): is there a "good" way to do that?
- It is still not clear to me what are the advantages of Julia over Python. +2
- Can't really say yet. We still have another day. :)



---------------------------

# Day 2

### Package development in Julia Seminar

Speaker: Luca Ferranti, University of Vaasa, Finland.
18.08.2021 at 14:00-17:00 EEST
Check https://nordic-rse.org/events/seminar-series/ for details.

### Comments on day 1 questions

 - Luca Ferranti answered the question about resizing arrays. Arrays are contiguous in memory, but Julia allocates them in a smart way, so that it does not always cause a copy.
   - Full technical conversation at https://coderefinery.zulipchat.com/#narrow/stream/119815-general/topic/Julia.20question


### How is Julia different from Python:
 - Functions are compiled are therefore fast
 - Multiple dispatch (will cover today)
 - No classes (replaced by multiple dispatch)

### How is Julia different from C, C++ and Fortran?
 - Dynamic typing and duck typing
 - Built-in package manager 
 - JIT compilation
 - Functions are compile units
 - Multiple dispatch (will cover today)

-----------------

## Questions about day 2:


- This is a question or a comment.
   - This is an answer

- How `rand` and palette (as defined using `colors`) interact is defined in `colors`. Is this correct?
    - I am not sure I understand your question. Do you refer to something Jarno showed?
    - If you check ?rand, you will see that one of the options for the first argument is an indexable collection (e.g. a palette).
        - Got that. I thought it was more complicated: that the called ~~object~~ decides how to react to the call. As Jarno said, it takes some time to think in the stateless/functional programming mode.

- Do all of the Julia plotting things rely on other languages?  (not saying this is bad, it's actually nice)
   - I don't think plotly does
       - Plotly is JavaScript, isn't 0000000000000000000000000000000000000000000000000000000000it?
       - There is a separate plotlyjs package. Not sure what the difference is.
       - Plotly seems to add interaction to PlotlyJS. It users PlotlyJS under the hood.

- Regarding the ! notation for functions that mutate passed values: is this purely convention or can Julia enforce it?
   - It is purely a convention and has no syntax behind it
   - :+1: 

- Interesting: https://discourse.julialang.org/t/why-is-enum-0-based/12722/6


:::info
Exercise 8 untill 12:30
:::

## What to do in the last session (poll)

Vote by adding an 'o' at the end
 - Like this: o

- Exercises: 
- Nicer functions: 
- Compilation:
- Parallel computing: oooo
- Vectorization: 
- Metaprogramming: 
- Performance tips: 

- May I suggest something else here (interoperability with other languages)?
  - I cannot cover much more than I have said. Don't really have experience here.
      - Ok
  - See section 10 for PyCall and Ccall, but I already showed these. There are also links to documentation.
    - Is there something about calling compiled Julia code in other languages?
      - From which language? Calling from C or Fortran is not complicated, but requires compiling the functions first. Calling from Python is possible with the "julia" package.
        - C calling interface works pretty much everywhere, so that will suffice, but if there is a direct way of calling from Python, that is interesting as well, for sure.
          - Use the "julia" package in Python. But you need to use the python-jl interpreter instead of the standard one (or compile a statically linked interpreter).
          - See https://pypi.org/project/julia/




# Feedback from Day 2

Write Something positive here:

 - Very interesting topics, especially those about threads and MPI, a pity we did not have time to spend more time with these (I guess that is what I will be doing in the exercises)
    - I think a good course should have more material than can be covered, so that we can adjust based on what you are interested in. Feel free to go through the rest of the materials and ask questions.
    - For asking questions, see scicomp.zulip.cs.aalto.fi (only for Aalto people) and coderefinery.zulipchat.com.
 - Clear explanations and good examples
 - A nice intro, enough to get interested to try more things and start projects +1 +1 +1 +1
 - Really nice that we kind of did our first Julia project (epidemic simulation) while going through the exercises.
 - It was good to get an overview of what is feasible with Julia, and seeing you do it and type along was instructive, however I have the feeling that we only scratched the surface and would need to spend more time on a similar course, now that I know better what to expect: is there a plan to organise that, over several more days (this structure with sessions in the morning and free afternoon was perfect for me)?
 - The coverage and choice of topics on both the days was excellent (keeping in mind the comment below). I feel ready to dive into topics that tickled my interest. +1
 - Courses like this are really good for getting started with a new language or technology. You could always go through some materials alone, but dedicating a day or two to go through some basics doesn't necessarily happen without a course like this, and if someone has thought of a good structure to do that – like now – it makes it much more effective. +111
     - this format is kinda perfect for introductory courses to any programming language that could be organised every year before the beginning of first semester in university
 - ...
 - ...
 - ...
 - ...



Write something constructive ("negative") here:
 - For me there was too much material covered too quickly on the second day. +1 +1
 - Quite "dense" day, with many topics, could have been easily split in 2 sessions (that is 3x half days in total)
 - Overall, I think that the course can be split for two audiences:
     - An Intro to Programming course: covering how to code and programming contructs using the slow pace of yesterday (you don't expect a beginner to be using threads and other similarly advanced features that were discussed today)
     - Then a How to use Julia course for those who already know programming in other languages so that they can switch to Julia if they deem fit. This would be more comparative and discuss topics that were covered today.
         - I do not necessarily agree: if as a beginer you have never heard about the advanced features offered by Julia you may never be tempted to look into these, so I think that getting a broad overview even when you start is good, and this is what this course did. Of course to get deeped into Julia more courses would be welcome, at various levels.
 - Parameters struct is different in the notebook 07 vs epidemic_no_immunity.jl used in exercises. Caused a little bit of confusion at first. +1
  - I would appreciate "real" breaks, I mean NOT mixed with exercises, and longer exercise sessions to have time to think about the problems and not simply go to the solutions because I have not enough time and the main course resumes +1
  - Today's exercises were more time consuming and having a bit longer exercise sessions would help +1
  - The epidemic exercises could perhaps be continued in the threading section
  - Maybe some quizzes using mentimeter would be nice to increase our participation.
  - I am under the impression that the attendance was a little overwhelmed after the first hour or so, and a more active participation with a bit more interactivity could have been better
  - The last session (parallel computing) was clearly an add-on, not a well thought of module like the rest of the sessions. The MPI exercise is really basic, and all the code was given, whereas the language features for parallel computing were shown very hastily, so I didn't really catch all while it was presented. I can go through some documents on my own, but since it didn't come up during the two days I had dedicated for this, it may take a while before it happens. I think it's ok to choose a focus and stick to it instead of trying to cater for everyone at the last moment. (It was nice that you polled our interests before the course, though.)
  - ...
  - ...
  - ...
  - ...





