slides 169

« Julia, my new optimization friend » Intro to the Julia programming language, for MATLAB users Date: 14th of June 218 ...

0 downloads 85 Views 880KB Size
« Julia, my new optimization friend » Intro to the Julia programming language, for MATLAB users Date: 14th of June 218 Who: Lilian Besson & Pierre Haessig (SCEE & AUT team @ IETR / CentraleSupélec campus Rennes)

«

Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

1

Agenda for today [25 min] 1. What is Julia [3 min] 2. Comparison with MATLAB [3 min] 3. Examples of problems solved Julia [5 min] 4. Longer example on optimization with JuMP [1min] 5. Links for more information ? [2 min]

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

2

1. What is Julia ? Developed and popular from the last 7 years Open-source and free programming language (MIT license) Interpreted and compiled, very efficient But easy syntax, dynamic typing, inline documentation etc Multi-platform, imperative MATLAB-like syntax for linear algebra etc Designed and acknowledged as simple to learn and use Easy to run your code in parallel (multi-core & cluster) Used worldwide: research, data science, finance etc… « Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

3

Ressources Website: JuliaLang.org for the language & Pkg.JuliaLang.org for packages Documentation : docs.JuliaLang.org

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

4

Comparison with MATLAB Cost License Comes from Scope Performances

Julia Free Open-source A non-profit foundation, and the community Mainly numeric Very good performance

MATLAB Hundreds of euros / year 1 year user license (no longer after your PhD!) MathWorks company Numeric only Faster than Python, slower than Julia

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

5

Comparison with MATLAB Julia Packaging Editor/IDE Parallel computations

 Pkg  manager included. Based on  git 

+ GitHub, very easy to use Jupyter

MATLAB Toolboxes already included but have to pay if you wat more!

is recommended (Juno is also good)

Very easy, low overhead cost

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

Good IDE already included Possible, high overhead

6

Comparison with MATLAB Julia Usage Fame Support?

Generic, worldwide

MATLAB Research in academia and industry

Young but starts to be known

Old and known, in decline

Community1 (StackOverflow, mailing lists etc).

By MathWorks

Documentation OK and growing, inline/online Note1 : JuliaPro offer paid licenses, if professional support is needed.

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

OK, inline/online

7

How to install Julia

(1/2)

You can try online for free on JuliaBox.com On Linux, Mac OS or Windows: You can use the default installer from the website julialang.org/downloads Takes about 4 minutes... and it's free ! You also need Python 3 to use Jupyter , I suggest to use Anaconda.com/download if you don't have Python yet.

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

8

How to install Julia

(2/2)

1. Select the binary of your platform

2. Run the binary 3. Wait 4. Done



!

! Test with  julia  in a terminal

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

9

Different tools to use Julia Use  julia  for the command line for short experiments

Use the Juno IDE to edit large projects Demo time

!

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

1

Different tools to use Julia Use Jupyter notebooks to write or share your experiments (examples:  github.com/Naereen/notebooks )

Demo time

!

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

11

How to install modules in Julia ? Installing is easy ! julia> Pkd.add "IJulia"

# installs IJulia

Updating also! julia> Pkg.update

How to find the module you need ? First

… ask your colleagues

!

Complete list on pkg.JuliaLang.org

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

12

Overview of famous Julia modules for easy plotting like MATLAB The JuliaDiffEq collection for differential equations The JuliaOpt collection for optimization The JuliaStats collection for statistics And many more! Find more specific packages on GitHub.com/svaksha/Julia.jl/  Winston.jl 

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

13

Many packages, and a quickly growing community

Julia is still in development, in version v.6 but version 1. is planned soon! « Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

14

2. Main differences in syntax between Julia and MATLAB Ref: cheatsheets.quantecon.org

File ext. Comment Indexing Slicing Operations Block

Julia

MATLAB

 .jl 

 .m 

 # blabla

 

 a[ ]  to  a[end]   a[

]  (view)

 % blabla  a  a

 

  to  a end    (

copy)

Linear algebra by default

Linear algebra by default

Use  end  to close all blocks

Use  endif   endfor  etc

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

15

Help And Or Datatype Array Size Nb Dim Last

Julia

MATLAB

 ?func 

 help func 

 a & b 

 a



 a | b 

 a



 Arra   of any type  [

;



multi-dim doubles array  [

;



 size a  

 size a  

 ndims a  

 ndims a  

 a[end] 

 a end  

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

16

Tranpose Conj. transpose Matrix x Element-wise x Element-wise / Element-wise ^ Zeros Ones Identity Range

Julia

MATLAB

 a.' 

 a.' 

 a' 

 a' 

 a * b 

 a * b 

 a .* b 

 a .* b 

 a ./ b 

 a ./ b 

 a ^

 a .^

 

 zeros

,

,

 

 zeros

,

,

 

 ones

,

,

 

 ones

,

,

 

 e e  range

 

,

,

    or  

 

 e e

 

 

 

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

17

Julia

MATLAB

 ma a    ma ma a   ? Maximum Random matrix  rand ,    rand ,    norm v    norm v   L2 Norm Inverse  inv a    inv a    a \ b   a \ b  Solve syst. Eigen vals  V, D = eig a    [V,D]=eig a    fft a  ,  ifft a    fft a  , ifft a   FFT/IFFT Very close to MATLAB for linear algebra!

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

18

3. Scientific problems solved with Julia Just to give examples of syntax and modules 1. 1D numerical integration and plot 2. Solving a 2nd order Ordinary Differential Equation

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

19

3.1. 1D numerical integration and plot Exercise : evaluate and plot this function on [-1, 1] :

Ei(x) := ∫



−x

eu du u

How to? Use packages and everything is easy!  QuadGK.jl  for integration  Winston.jl  for 2D plotting

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

2

using QuadGK function Ei , f = t e if > return + else return end end

minfloat= e- , ma float= p t / t # inline function, with '- >' quadgk f, , minfloat [ ] quadgk f, minfloat, ma float [ ] quadgk f,

X = linspace - , Y = [ Ei for

, in X ]

, ma float [ ]

#

points

using Winston plot X, Y title "The function Ei " label " " ; label " " savefig "figures/Ei_integral.png"

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

21

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

22

3.2. Solving a 2nd order ODE Goal : solve and plot the differential equation of a pendulum:

θ′′ (t) + b θ′ (t) + c sin(θ(t)) = 0 For b

= 1/4, c = 5, θ(0) = π − 0.1, θ′ (0) = 0, t ∈ [0, 10]

How to? Use packages!  DifferentialEquations.jl  function for ODE integration  Winston.jl  for 2D plotting

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

23

using DifferentialEquations b, c =

.

,

.

# macro magic! pend = @ode_def Pendulum begin dθ = ω # es, this is UTF8 dω = b * ω - c * sin θ end prob = ODEProblem pend, sol = solve prob t, = sol.t, hcat sol.u

, . , . # ↑ solve on interval [ , '

]

using Winston plot t, [:, ], t, [:, ] title " D Differential Equation" savefig "figures/Pendulum_solution.png"

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

24

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

25

Conclusion (1/2) Sum-up I hope you got a good introduction to Julia It's not hard to migrate from MATLAB to Julia Good start:  docs.JuliaLang.org/en/stable/manual/getting started 

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

26

Conclusion (2/2) Thanks for joining

!

Your mission, if you accept it... 1.

Train yourself a little bit on Julia ↪ JuliaBox.com ? Or install it on your laptop! And ead introduction in the Julia manual! 2. Jedi level: Try to solve a numerical system, from your research or teaching, in Julia instead of MATLAB 3. Master level: From now on, try to use open-source & free tools for your research (Julia, Python and others)… Padawan level:

« Julia, my new optimization friend » | 14 June 218 | By: Lilian Besson | IETR @ Vannes

27