Github: mfherbst
EMail: michael.herbst@enpc.fr
Web: https://michael-herbst.com
Very recent: v1.0. released August 2018
Compiled scripting language
JIT with LLVM backend
High-level syntax, dynamical
Garbage collected
Strongly typed
A bit like python
with integrated numpy
Multiple dispatch
Rich interoperability: Fortran
, C
, C++
, python
, R
, ...
Tries to facilitate modern developments:
function mysquare(x)
x * x
end
# or alternatively just:
mysquare(x) = x * x
mysquare(3) # Integer arithmetic
mysquare(2im) # Complex arithmetic
typeof(["abc"])
typeof([1, 2, 3])
typeof([1.1, 2, 3])
typeof(["abc", 1, 2.3])
arr = [1.1, 2, 3]
mysquare.(arr) # Vectorised call
sin.(exp.(mysquare.(arr)))
# or:
@. sin(exp(mysquare(arr)))
using Printf
function printarray(array::Array{T, 1}) where T
println("General array passed, elements:")
for element in array
println(" $element")
end
end
function printarray(array::Array{T, 1}) where T <: Number
println("Numeric array passed, elements:")
for element in array
println(" $element")
end
end
function printarray(array::Array{Float64, 1})
println("Float array passed, elements:")
for element in array
@printf " %10.5f\n" element
end
end
printarray(["a", "b", 1])
println()
printarray([1, 2, 3])
println()
printarray([1., 2., 3.])
Type = Float16
a = rand(Type, 3, 4)
sum(mysquare.(a))
using StaticArrays
# Array operations on static-sized arrays:
a = @SArray[1,2,3,4]
mysquare.(a)
using SparseArrays
# random sparse array
a = sprandn(6, 6, 0.3)
mysquare.(a)
using Distributed
# Add 4 processes and setup environment at each of them
#addprocs(4)
@everywhere begin
mysquare(x) = x * x
using DistributedArrays
end
a = drandn(100,100,10)
println(sum(mysquare.(a)))
close(a)
struct MyPoint
x::Float64
y::Float64
end
Note: A struct contains no methods and is immutable by default.
p = MyPoint(1, 2)
p.x = 6
Functionality is provided by external functions only:
pointlength(p::MyPoint) = sqrt(p.x^2 + p.y^2)
pointlength(p)
Abstract types are only names to "group" concrete structs:
using StaticArrays
@show AbstractArray{Float64} <: AbstractArray
@show SArray <: AbstractArray
@show Array{Float64} <: AbstractArray{Float64}
@show Array{Int64} <: AbstractArray{Float64};
# Function, which works on all Abstract Arrays of element type Float64
function strangesum(a::AbstractArray{Float64})
a[1] + sum(a[2:end])
end
# ok:
strangesum([1., 2., 3.])
strangesum(@SArray[1., 2., 3.])
# not ok:
strangesum([1, 2, 3])
using Zygote
using SpecialFunctions
f(x) = 2x*sin(x) - x^2/2
@time f'(1)
@time f'(2)
python
, C++
, Fortan
, R
# Call to lapack to compute dot product
v = [1.0, 2.0, 3.0, 4.0]
w = [2.0, 3.0, 4.0, 5.0]
VdotW = ccall((:ddot_, "liblapack"), Float64,
(Ref{Int32}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ref{Int32}),
length(v), v, 1, w, 1)
println("v ⋅ w = $VdotW")
# Plot function in matplotlib
using PyPlot
# f defined as above
x = collect(-4:0.2:7)
PyPlot.plot(x, f.(x), label="f(x) = 2x*sin(x) - x^2/2 ")
PyPlot.plot(x, f'.(x), label="f'(x)")
PyPlot.axhline(0, color="grey", linewidth=0.5)
PyPlot.legend()
PyPlot.show()
| | duration (s)
-------- | ------------------------- | ---------------
python | array operation (numpy) | 11.8
| |
C | gcc | 8.1
C | gcc -O3 | 1.1
C | gcc -O3 -march=native | 0.5
| |
C | clang | 8.0
C | clang -O3 | 1.1
C | clang -O3 -march=native | 0.8
| |
julia | array operations | 3.3
julia | loops | 1.9
julia | loops, no bounds check | 0.5
Stronger emphasis on functional-style programming
Multiple dispatch based on JIT-compiling and strong typing
Two-way interoperability with Fortran, C / C++, python, R
Threading, vectorisation and distributed memory in high-level syntax
Uses cases:
Fortran
and C
in high-performance computing