Transcript of my seminar about https://julialang.org/.

Calculator

1 + 1
3 * 5

Variables

x = 1
x
π  # Enter as \pi<TAB>
α = atan(1/2)

3α + 100

Vectors, Arrays

a=[10,20,30]
a[1]
a[2:3]
a[2:end]

b=[1 2; 3 4]

Element-wise operations – broadcasting

a=rand(100_000_000)
b=rand(100_000_000)

a*b
a.*b
sin(a)
sin.(a)
[sin(i) for i in a]

@time sin.(a) .+ cos.(b);
@time [sin(i) for i in a] .+ [cos(i) for i in b];

c = zeros(size(a))
@time c .= sin.(a).^2 .+ cos.(b).^2;
@. c = sin(a)^2 + cos(b)^2

REPL modes

  • Help

    ?atan
    @doc atan
    
  • Shell

    ;ls
    
  • Package manager

    ]add IJulia
    

Return with backspace

Types

Julia uses dynamic types + type inference.

typeof(1)
typeof(1.0)
typeof(Int8(1))
typeof("Hello world")

Parametric types

1//3
typeof(ans)
typeof(Int8(1)//Int8(3))
[1,2,3]
[1 2 3]
Int8[1,2,3]
[1 2;3 4.1]

Abstract types and type hierarchy

Integer <: Number
Int8 <: Integer
Int8 <: Number
Float32 <: Number
Float32 <: Integer

Functions

f(x) = x*x
f(4)
function hello(name)
    return "Hello " * name
end
hello("Michal")
f("Hello")

+(1,2)
→(a,b) = (a+b)/(a-b)
3→1

Anonymous functions

x->x^2
ans(5)
a=rand(10)
filter(x->x>0.5, a)

[x for x in a if x > 0.5]

a[a .> 0.5]

Methods and multiple dispatch

Function can have multiple implementations. The implementation to call is selected based on argument types

f(x::Number) = "Number "*string(x)
methods(f)
f(1)
f(1.1)
f("1.1")
f(x::String) = "String "*x
f("1.1")
methods(+)
methods(show)

Show examples

@doc atan
typeof(@doc atan)
show(stdout, MIME("text/plain"), @doc atan)
show(stdout, MIME("text/html"), @doc atan)

typeof(`ls`)
run(`ls`)

Units

Example of what is possible with the language. This is not a builtin functionality. Everything is programmed in Julia.

using Unitful
using Unitful.DefaultSymbols
using Unitful: hr

1m + 3cm |> cm |> float
float(cm(1m + 3cm))
1m + 3cm |> cm |> float
sin(90)
sin(90°)
sin(π/2)

15m/3s

10km/hr |> m/s
10km/hr |> m/s |> float
0°C |> K |> float

Plotting

Plots supports multiple plotting backends.

using Plots
pyplot()
plot(sin.(0:0.1:2π)

One of several available interfaces to Gnuplot.

using Gaston
plot(sin.(0:0.1:2π)

Macros

  • Inspired by Lisp
  • Work at parse tree level.
  • Can rewrite/generate programs.
  • No separate macro language, macros are written in Julia itself.

Examples (see test suites of many Julia packages):

@testset
@test

Tasks & Channels

  • Easy to use parallelism

Jupyter notebooks

using IJulia
notebook()

Integration with C/C++

Direct call to a function from shared library:

ccall(:clock, Int32, ())
ccall((:clock, "libc"), Int32, ())

Interactive C++

using Cxx

cxx"#include <stdio.h>"
icxx"""printf("Hello world\n");"""
# Passing Julia values to C/C++ functions 
x = 42
@cxx printf(pointer("X is %d\n"), x)

Complex example

Testing a C++ class and plotting results of its work.

using Cxx
import Cxx.CxxCore
using Gaston

cxx""" #include "stepper.cpp" """

# Create C++ object and store it in Julia variable
s = icxx"Stepper(nullptr, 2020, 100, 20);"
# Call a method of C++ object
@cxx s->moveTo(2000)
pos = Int32[0]
freq = Int32[0]
while @cxx s->isMoving()
    @cxx s->periodic()
    # Store values from the C++ object in Julia array for later
    # plotting
    push!(freq, @cxx s->freq)
    push!(pos, @cxx s->getPosition())
end

# Plot gathered data
set(grid="on")
x=0:length(freq)-1
plot(x, freq, xlabel="iteration", legend="freq")
plot!(x, [0; pos[2:end] .- pos[1:end-1]]./20e-3, plotstyle="points", color="red", legend="Δpos/T_s")
plot!(x, pos, plotstyle="linespoints", color="green", legend="pos")

# Cxx cannot (yet) change functions once that are defined. Use these
# to commands to “replace” the compiler object, which allows
# redefining the functions.
Cxx.CxxCore.reset_init!()
Cxx.new_clang_instance()