Table of Contents

Acknowledgements

This notebook is simply my write up of notes taken from David Sanders' talk at SciPy 2014.

Installing Julia

First of all to install julia from homebrew:

$ brew cask install julia

Another way is using a tap from a guy in the JuliaLang community of github:

$  brew tap staticfloat/julia

$  brew install julia

Including the IPython kernel

To get Julia run with iPython/jupyter notebooks, simply open the julia prompt by

$ julia

Then use julia's package manager to get IJulia package.

$ julia > Pkg.add("IJulia")

Installing Julia Packages

To install Julia packages run the Pkg.add() function! For example

$ Pkg.add("PyPlot")   # to get the package to make plots as Matplotlib

This will download all the dependencies needed and include the PyPlot package in the current Julia directory.

Where are my packages stored?

Julia builds a hidden directory with information on the packages that are installed. This is in your home directory under:

~/.julia

There is also a hidden file with the julia prompt command history in:

~/.julia_history



Running Julia in Prompt

Using Julia as a calculator

You can use Julia prompt as a calculator, as you do with Python.


In [1]:
2+3


Out[1]:
5

In [2]:
4./2.


Out[2]:
2.0

In [3]:
4/2


Out[3]:
2.0

Using REPL (Read-Evaluate-Print Loop)

REPL is a high level interpreter, just as IPython command prompt, or Julia's command prompt. It is highly interactive and there are many features with it.

Getting help

When entering julia's REPL, by entering ? the prompt changes into help?>.

help?> fft
search: fft fft! FFTW fftshift rfft ifft bfft ifft! bfft! ifftshift irfft brfft plan_fft plan_fft!

  fft(A [, dims])

  Performs a multidimensional FFT of the array A. The optional dims argument specifies an iterable
  subset of dimensions (e.g. an integer, range, tuple, or array) to transform along. Most
  efficient if the size of A along the transformed dimensions is a product of small primes; see
  nextprod(). See also plan_fft() for even greater efficiency.

  A one-dimensional FFT computes the one-dimensional discrete Fourier transform (DFT) as defined
  by

\operatorname{DFT}(A)[k] =
  \sum_{n=1}^{\operatorname{length}(A)}
  \exp\left(-i\frac{2\pi
  (n-1)(k-1)}{\operatorname{length}(A)} \right) A[n].

  A multidimensional FFT simply performs this operation along each transformed dimension of A.

  Higher performance is usually possible with multi-threading. Use FFTW.set_num_threads(np) to use
  np threads, if you have np processors.

Using shell commands within REPL

Using the semi-colon ; you can run shell commands within REPL, then prompt changes into shell>.

shell> pwd
/Users/nkarast

Basic Syntax

Variables and arithmetic

Numeric values are similar to Python

Variables are similar to Python, but any unicode string can be used as variable name! Even latex notation!


In [4]:
x=3


Out[4]:
3

In [5]:
α = 4


Out[5]:
4

In [231]:
 = 5


Out[231]:
5

Functions

Functions are called using round brackets ().

Simple functions may be defined with a nice mathematical syntax ( * is not needed for simple expressions).


In [7]:
println("x = ",x)


x = 3

In [8]:
f(x) = 2x^2 + 3x +1


Out[8]:
f (generic function with 1 method)

In [9]:
g(x) = f(x) - 2f(x-2)


Out[9]:
g (generic function with 1 method)

In [10]:
g(2)


Out[10]:
13

In [11]:
f(2)


Out[11]:
15

Variable substitution

The value of variables may be substituted inside strings with the $ operator.


In [12]:
name  = "Nikos"


Out[12]:
"Nikos"

In [13]:
my_greeting = "Hello $name"


Out[13]:
"Hello Nikos"

In [14]:
println(my_greeting)


Hello Nikos

more complicated stuff can be used with wrapped parenthesis:


In [15]:
a = 3


Out[15]:
3

In [16]:
println("The sine of $a is $(sin(a))")


The sine of 3 is 0.1411200080598672

Numerical types

There are numerical types with different precision.


In [17]:
a = Int8(1)


Out[17]:
1

In [18]:
b = Int16(2)


Out[18]:
2

In [19]:
a+b


Out[19]:
3

Arbitrary-precision integers and floiting points are available through the types BigInt() and BigFloat(). The function big() converts a number into the corresponding Big type.


In [20]:
big(10)


Out[20]:
10

In [21]:
typeof(ans)


Out[21]:
BigInt

Unlike Python, integers are NOT automatically promoted to arbitrary-precision integers.

Complex Numbers

To write a complex number use im for the imaginary part


In [22]:
a=7
c = (1+3im)*a


Out[22]:
7 + 21im

To get the real and imaginary parts:


In [23]:
c.re


Out[23]:
7

In [24]:
c.im


Out[24]:
21

In [25]:
c.re, c.im ## this is a tuple, just as Python


Out[25]:
(7,21)

The conjugate of a complex is given by the conj() function:


In [26]:
conj(a)


Out[26]:
7

In [27]:
conj(c)


Out[27]:
7 - 21im

Rational Numbers

You can use rational numbers (fractions) with the built in \\ operator:


In [28]:
3//4


Out[28]:
3//4

In [29]:
3//4 + 5//6


Out[29]:
19//12

In [30]:
typeof(ans)


Out[30]:
Rational{Int64}

Operators are convenienet way of writing functions


In [31]:
//(3,4)  ## you could write +(3,4) -> ans = 7


Out[31]:
3//4

In [32]:
//


Out[32]:
// (generic function with 8 methods)

In [33]:
methods(//) # it will list all the things you can done with this
            # function or operator


Out[33]:
8 methods for generic function //:

The expression n::Integer is a type annotation that specifies that the method applies when its first argument is of type Integer



In [34]:
2.4//1.4 ## cannot do this with floats


LoadError: MethodError: `//` has no method matching //(::Float64, ::Float64)
Closest candidates are:
  //(!Matched::Complex{T<:Real}, ::Real)
  //(::Number, !Matched::Complex{T<:Real})
  //(!Matched::AbstractArray{T,N}, ::Number)
while loading In[34], in expression starting on line 1

In [35]:
(3//4)^20


Out[35]:
3486784401//1099511627776

In [36]:
(3//4)^50 # when numbers getting big it will overflow


LoadError: OverflowError()
while loading In[36], in expression starting on line 1

 in * at rational.jl:188
 in power_by_squaring at intfuncs.jl:94
 in ^ at rational.jl:314

but using bigInts...


In [37]:
(big(3)//4)^50


Out[37]:
717897987691852588770249//1267650600228229401496703205376

In [38]:
typeof(ans)


Out[38]:
Rational{BigInt}

Vectors : similar to Python lists and numpy arrays

To store several variables in one, use a "list" as in Python:


In [39]:
l = [3,4,5]


Out[39]:
3-element Array{Int64,1}:
 3
 4
 5

In [40]:
typeof(l)


Out[40]:
Array{Int64,1}

In Julia, these are called Arrays. The string Array{Int64,1} gives you the type of the array and the dimension of it.

All elements of the array, it must be of the same type!


In [41]:
k = [1,3, 9.2] # converts them all to float to be of the same type


Out[41]:
3-element Array{Float64,1}:
 1.0
 3.0
 9.2

In [42]:
m = [1, 4, "w00t"]  # that is of `Any` type


Out[42]:
3-element Array{Any,1}:
 1      
 4      
  "w00t"

In [43]:
n = [1.4, 'a']


Out[43]:
2-element Array{Any,1}:
 1.4 
  'a'

In [44]:
o = {3., 4, "hi", [2, 1]}


WARNING: deprecated syntax "{a,b, ...}" at In[44]:1.
Use "Any[a,b, ...]" instead.
Out[44]:
4-element Array{Any,1}:
 3.0   
 4     
  "hi" 
  [2,1]

So you can make lists of mixed types, but if used for numerical calculations you lose the efficiency of "numpy".


Indexing

Same as Python but everything starts from 1!


In [45]:
k[1]


Out[45]:
1.0

In [46]:
k[0]


LoadError: BoundsError: attempt to access 3-element Array{Float64,1}:
 1.0
 3.0
 9.2
  at index [0]
while loading In[46], in expression starting on line 1

 in getindex at array.jl:282

For range:


In [47]:
k[1:3] # this will give you k[1], k[2] AND k[3]


Out[47]:
3-element Array{Float64,1}:
 1.0
 3.0
 9.2

Unlike Python the limits (start, end) must be mentioned explicitly:


In [48]:
k[1:end]


Out[48]:
3-element Array{Float64,1}:
 1.0
 3.0
 9.2

In [49]:
k[1:end-1]


Out[49]:
2-element Array{Float64,1}:
 1.0
 3.0

In [50]:
k[-1] # does not work


LoadError: BoundsError: attempt to access 3-element Array{Float64,1}:
 1.0
 3.0
 9.2
  at index [-1]
while loading In[50], in expression starting on line 1

 in getindex at array.jl:282

In Julia Arrays, like Python lists, are dynamic, which is not the case for numpy arrays. But the syntax is a bit different.

To add an element at the end of the list:


In [51]:
k


Out[51]:
3-element Array{Float64,1}:
 1.0
 3.0
 9.2

In [52]:
k+k


Out[52]:
3-element Array{Float64,1}:
  2.0
  6.0
 18.4

In [53]:
push!(k,7)  # the ! is a convention of Julia, which means that it will modify the argument


Out[53]:
4-element Array{Float64,1}:
 1.0
 3.0
 9.2
 7.0

In [54]:
methods(push!)


Out[54]:
15 methods for generic function push!:

In [55]:
methodswith(Array) #all the things I can do with the type Array


Out[55]:
328-element Array{Method,1}:
  • *{T,S}(s::Base.LinAlg.SVDOperator{T,S}, v::Array{T,1}) at linalg/arnoldi.jl:261
  • +(A::Array{T,N}, B::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:1019
  • +(A::SparseMatrixCSC{Tv,Ti<:Integer}, B::Array{T,N}) at sparse/sparsematrix.jl:1017
  • -(A::Array{T,N}, B::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:1024
  • -(A::SparseMatrixCSC{Tv,Ti<:Integer}, B::Array{T,N}) at sparse/sparsematrix.jl:1022
  • ./(A::SparseMatrixCSC{Tv,Ti<:Integer}, B::Array{T,N}) at sparse/sparsematrix.jl:1032
  • ./(A::Array{T,N}, B::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:1033
  • .\(A::SparseMatrixCSC{Tv,Ti<:Integer}, B::Array{T,N}) at sparse/sparsematrix.jl:1038
  • .\(A::Array{T,N}, B::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:1039
  • .^{T<:Integer}(A::BitArray{N}, B::Array{T<:Integer,N}) at broadcast.jl:449
  • .^(A::SparseMatrixCSC{Tv,Ti<:Integer}, B::Array{T,N}) at sparse/sparsematrix.jl:1046
  • .^(A::Array{T,N}, B::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:1047
  • A_ldiv_B!{T<:Union{Complex{Float64},Float64}}(lu::Base.SparseMatrix.UMFPACK.UmfpackLU{T<:Union{Complex{Float64},Float64},Ti<:Union{Int32,Int64}}, b::Array{T<:Union{Complex{Float64},Float64},1}) at sparse/umfpack.jl:336
  • A_ldiv_B!{T<:Union{Complex{Float64},Float64}}(lu::Base.SparseMatrix.UMFPACK.UmfpackLU{T<:Union{Complex{Float64},Float64},Ti<:Union{Int32,Int64}}, b::Array{T<:Union{Complex{Float64},Float64},2}) at sparse/umfpack.jl:337
  • A_ldiv_B!{Tb<:Complex{T<:Real}}(lu::Base.SparseMatrix.UMFPACK.UmfpackLU{Float64,Ti<:Union{Int32,Int64}}, b::Array{Tb<:Complex{T<:Real},1}) at sparse/umfpack.jl:339
  • PipeBuffer(data::Array{UInt8,1}) at iobuffer.jl:33
  • PipeBuffer(data::Array{UInt8,1}, maxsize::Int64) at iobuffer.jl:33
  • abs(f::Array{Base.Pkg.Resolve.MaxSum.FieldValues.FieldValue,1}) at pkg/resolve/fieldvalue.jl:65
  • append!(A::Array{Bool,1}, items::BitArray{1}) at bitarray.jl:465
  • append!{T}(a::Array{T,1}, items::AbstractArray{T,1}) at array.jl:445
  • bytes2hex{T<:UInt8}(arr::Array{T<:UInt8,1}) at strings/util.jl:238
  • bytestring(s::Array{UInt8,1}) at strings/basic.jl:14
  • call(T::Type{ASCIIString}, d::Array{UInt8,1}) at essentials.jl:36
  • call(T::Type{UTF8String}, d::Array{UInt8,1}) at essentials.jl:37
  • call{P<:Ptr{T},T<:Ptr{T}}(::Type{Ref{P<:Ptr{T}}}, a::Array{T<:Ptr{T},N}) at array.jl:27
  • call{P<:Ptr{T},T}(::Type{Ref{P<:Ptr{T}}}, a::Array{T,N}) at array.jl:30
  • call(::Type{UTF16String}, data::Array{UInt16,1}) at unicode/types.jl:10
  • call(::Type{UTF32String}, data::Array{UInt32,1}) at unicode/types.jl:25
  • call(::Type{UTF32String}, data::Array{Char,1}) at unicode/types.jl:31
  • call(::Type{RegexMatch}, match::SubString{UTF8String}, captures::Array{Union{SubString{UTF8String},Void},1}, offset::Int64, offsets::Array{Int64,1}, regex::Regex) at regex.jl:90
  • call(::Type{IOStream}, name::AbstractString, buf::Array{UInt8,1}) at iostream.jl:13
  • call(::Type{Cmd}, exec::Array{ByteString,1}) at process.jl:11
  • call(::Type{Base.dSFMT.DSFMT_state}, val::Array{Int32,1}) at dSFMT.jl:25
  • call(::Type{Base.JoinPGRPMsg}, self_pid::Int64, other_workers::Array{T,N}, self_is_local::Bool, notify_oid::Tuple, topology::Symbol) at multi.jl:47
  • call(::Type{ProcessGroup}, w::Array{Any,1}) at multi.jl:277
  • call{T,S<:Union{ASCIIString,UTF8String}}(::Type{Base.DataFmt.DLMStore{T,S<:Union{ASCIIString,UTF8String}}}, hdr::Array{AbstractString,2}, data::Array{T,2}, nrows::Int64, ncols::Int64, lastrow::Int64, lastcol::Int64, hdr_offset::Int64, sbuff::S<:Union{ASCIIString,UTF8String}, auto::Bool, eol::Char) at datafmt.jl:121
  • call(::Type{Base.LineEdit.MIState}, interface::Base.LineEdit.ModalInterface, current_mode, aborted::Bool, mode_state, kill_buffer::ByteString, previous_key::Array{Char,1}, key_repeats::Int64) at LineEdit.jl:22
  • call(::Type{Base.REPL.REPLHistoryProvider}, history::Array{AbstractString,1}, history_file, cur_idx::Int64, last_idx::Int64, last_buffer::Base.AbstractIOBuffer{Array{UInt8,1}}, last_mode, mode_mapping, modes::Array{Symbol,1}) at REPL.jl:301
  • call{T}(::Type{SymTridiagonal{T}}, dv::Array{T,1}, ev::Array{T,1}) at linalg/tridiag.jl:10
  • call{T}(::Type{SymTridiagonal{T}}, dv::Array{T,1}, ev::Array{T,1}) at linalg/tridiag.jl:17
  • call{Td,Te}(::Type{SymTridiagonal{T}}, dv::Array{Td,1}, ev::Array{Te,1}) at linalg/tridiag.jl:20
  • call{T}(::Type{Tridiagonal{T}}, dl::Array{T,1}, d::Array{T,1}, du::Array{T,1}, du2::Array{T,1}) at linalg/tridiag.jl:267
  • call{T}(::Type{Tridiagonal{T}}, dl::Array{T,1}, d::Array{T,1}, du::Array{T,1}) at linalg/tridiag.jl:273
  • call{Tl,Td,Tu}(::Type{Tridiagonal{T}}, dl::Array{Tl,1}, d::Array{Td,1}, du::Array{Tu,1}) at linalg/tridiag.jl:280
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.QR{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:8
  • call{T}(::Type{Base.LinAlg.QR{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:10
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.QRPivoted{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}, jpvt::Array{Int64,1}) at linalg/qr.jl:23
  • call{T}(::Type{Base.LinAlg.QRPivoted{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}, jpvt::Array{Int64,1}) at linalg/qr.jl:25
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.QRPackedQ{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:113
  • call{T}(::Type{Base.LinAlg.QRPackedQ{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:115
  • call{S,M<:AbstractArray{T,2}}(::Type{Base.LinAlg.QRCompactWYQ{S,M<:AbstractArray{T,2}}}, factors::AbstractArray{S,2}, T::Array{S,2}) at linalg/qr.jl:120
  • call{S}(::Type{Base.LinAlg.QRCompactWYQ{S,M<:AbstractArray{T,2}}}, factors::AbstractArray{S,2}, T::Array{S,2}) at linalg/qr.jl:122
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.Hessenberg{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:425
  • call{T}(::Type{Base.LinAlg.Hessenberg{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:427
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.HessenbergQ{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:441
  • call{T}(::Type{Base.LinAlg.HessenbergQ{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, τ::Array{T,1}) at linalg/qr.jl:443
  • call{T,Tr,M<:AbstractArray{T,N}}(::Type{Base.LinAlg.SVD{T,Tr,M<:AbstractArray{T,N}}}, U::AbstractArray{T,N}, S::Array{Tr,1}, Vt::AbstractArray{T,N}) at linalg/svd.jl:8
  • call{T,Tr}(::Type{Base.LinAlg.SVD{T,Tr,M<:AbstractArray{T,N}}}, U::AbstractArray{T,N}, S::Array{Tr,1}, Vt::AbstractArray{T,N}) at linalg/svd.jl:10
  • call{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},M<:AbstractArray{T,2}}(::Type{Base.LinAlg.GeneralizedSchur{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},M<:AbstractArray{T,2}}}, S::AbstractArray{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2}, T::AbstractArray{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2}, alpha::Array{T,1}, beta::Array{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},1}, Q::AbstractArray{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2}, Z::AbstractArray{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2}) at linalg/schur.jl:48
  • call{Ty}(::Type{Base.LinAlg.GeneralizedSchur{Ty<:Union{Complex{Float32},Complex{Float64},Float32,Float64},M<:AbstractArray{T,2}}}, S::AbstractArray{Ty,2}, T::AbstractArray{Ty,2}, alpha::Array{T,1}, beta::Array{Ty,1}, Q::AbstractArray{Ty,2}, Z::AbstractArray{Ty,2}) at linalg/schur.jl:50
  • call{T}(::Type{Base.LinAlg.CholeskyPivoted{T,S<:AbstractArray{T,2}}}, A::AbstractArray{T,2}, uplo::Char, piv::Array{Int64,1}, rank::Int64, tol::Real, info::Int64) at linalg/cholesky.jl:23
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.LU{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, ipiv::Array{Int64,1}, info::Int64) at linalg/lu.jl:10
  • call{T}(::Type{Base.LinAlg.LU{T,S<:AbstractArray{T,2}}}, factors::AbstractArray{T,2}, ipiv::Array{Int64,1}, info::Int64) at linalg/lu.jl:12
  • call{T,S<:AbstractArray{T,2}}(::Type{Base.LinAlg.BunchKaufman{T,S<:AbstractArray{T,2}}}, LD::AbstractArray{T,2}, ipiv::Array{Int64,1}, uplo::Char, symmetric::Bool) at linalg/bunchkaufman.jl:12
  • call{T}(::Type{Base.LinAlg.BunchKaufman{T,S<:AbstractArray{T,2}}}, LD::AbstractArray{T,2}, ipiv::Array{Int64,1}, uplo::Char, symmetric::Bool) at linalg/bunchkaufman.jl:14
  • call{T}(::Type{Diagonal{T}}, diag::Array{T,1}) at linalg/diagonal.jl:6
  • call{#s335,T}(::Type{Bidiagonal{#s335}}, dv::Array{T,1}, ev::Array{T,1}, isupper::Bool) at linalg/bidiag.jl:9
  • call{T}(::Type{Base.LinAlg.Rotation{T}}, rotations::Array{Base.LinAlg.Givens{T},1}) at linalg/givens.jl:24
  • call{Tv,Ti<:Integer}(::Type{SparseMatrixCSC{Tv,Ti<:Integer}}, m::Integer, n::Integer, colptr::Array{Ti<:Integer,1}, rowval::Array{Ti<:Integer,1}, nzval::Array{Tv,1}) at sparse/sparsematrix.jl:16
  • call{Tv<:Union{Complex{Float64},Float64},Ti<:Union{Int32,Int64}}(::Type{Base.SparseMatrix.UMFPACK.UmfpackLU{Tv<:Union{Complex{Float64},Float64},Ti<:Union{Int32,Int64}}}, symbolic::Ptr{Void}, numeric::Ptr{Void}, m::Int64, n::Int64, colptr::Array{Ti<:Union{Int32,Int64},1}, rowval::Array{Ti<:Union{Int32,Int64},1}, nzval::Array{Tv<:Union{Complex{Float64},Float64},1}) at sparse/umfpack.jl:97
  • call(::Type{Base.Pkg.Types.VersionSet}, intervals::Array{Base.Pkg.Types.VersionInterval,1}) at pkg/types.jl:23
  • call(::Type{Base.Pkg.Types.VersionSet}, versions::Array{VersionNumber,1}) at pkg/types.jl:26
  • call(::Type{Base.Pkg.Reqs.Requirement}, package::AbstractString, versions::Base.Pkg.Types.VersionSet, system::Array{AbstractString,1}) at pkg/reqs.jl:36
  • call{T}(::Type{Base.Pkg.Resolve.VersionWeights.HierarchicalValue{T}}, v::Array{T,1}, rest::T) at pkg/resolve/versionweight.jl:14
  • call{T}(::Type{Base.Pkg.Resolve.VersionWeights.HierarchicalValue{T}}, v::Array{T,1}) at pkg/resolve/versionweight.jl:14
  • call(::Type{Base.Dates.CompoundPeriod}, p::Array{Base.Dates.Period,1}) at dates/periods.jl:118
  • call(::Type{Base.Dates.DateFormat}, slots::Array{Base.Dates.Slot{T},1}, prefix::AbstractString, locale::AbstractString) at dates/io.jl:53
  • call(::Type{Base.Dates.SlotRule}, rules::Array{Type{T},N}) at dates/io.jl:63
  • call(::Type{Base.Markdown.Config}, breaking::Array{Function,1}, regular::Array{Function,1}, inner::Dict{Char,Array{Function,1}}) at markdown/parse/config.jl:6
  • call(::Type{Base.Markdown.Table}, rows::Array{Array{Any,1},1}, align::Array{Symbol,1}) at markdown/GitHub/table.jl:4
  • call(::Type{Base.Docs.FuncDoc}, main, order::Array{Type{T},1}, meta::ObjectIdDict, source::ObjectIdDict) at docs/Docs.jl:220
  • call(::Type{Base.Docs.TypeDoc}, main, fields::Dict{Symbol,Any}, order::Array{Type{T},1}, meta::ObjectIdDict) at docs/Docs.jl:277
  • call(::Type{ByteString}, v::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Compat/src/Compat.jl:1264
  • call(::Type{ZMQ.Message}, a::Array{T,N}) at /Users/nkarast/.julia/v0.4/ZMQ/src/ZMQ.jl:412
  • call(::Type{JSON.Parser.MemoryParserState}, utf8data::Array{UInt8,1}, s::Int64) at /Users/nkarast/.julia/v0.4/JSON/src/Parser.jl:29
  • call(::Type{Nettle.Hasher}, hash_type::Nettle.HashType, state::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Nettle/src/hash.jl:8
  • call(::Type{Nettle.HMACState}, hash_type::Nettle.HashType, outer::Array{UInt8,1}, inner::Array{UInt8,1}, state::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Nettle/src/hmac.jl:9
  • call(::Type{Nettle.Encryptor}, cipher_type::Nettle.CipherType, state::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:36
  • call(::Type{Nettle.Decryptor}, cipher_type::Nettle.CipherType, state::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:40
  • complex{S<:Real,T<:Real}(A::Array{S<:Real,N}, B::Array{T<:Real,N}) at complex.jl:777
  • complex{T<:Real}(A::Array{T<:Real,N}, B::Real) at complex.jl:794
  • complex{T<:Real}(A::Real, B::Array{T<:Real,N}) at complex.jl:786
  • convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) at array.jl:238
  • convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) at array.jl:240
  • convert{T,n}(::Type{Array{T,N}}, x::Array{T,n}) at array.jl:237
  • convert{T,n,S}(::Type{Array{T,N}}, x::Array{S,n}) at array.jl:239
  • convert(::Type{ASCIIString}, a::Array{UInt8,1}) at ascii.jl:106
  • convert(::Type{ASCIIString}, a::Array{UInt8,1}, invalids_as::ASCIIString) at ascii.jl:115
  • convert(::Type{ASCIIString}, a::Array{UInt8,1}, invalids_as::AbstractString) at ascii.jl:132
  • convert(::Type{UTF8String}, dat::Array{UInt8,1}) at unicode/utf8.jl:241
  • convert(::Type{UTF8String}, a::Array{UInt8,1}, invalids_as::AbstractString) at unicode/utf8.jl:293
  • convert{TS,TA,N}(::Type{SharedArray{TS,N}}, A::Array{TA,N}) at sharedarray.jl:273
  • convert{T}(::Type{SharedArray{T,N}}, A::Array{T,N}) at sharedarray.jl:272
  • convert(::Type{SharedArray{T,N}}, A::Array{T,N}) at sharedarray.jl:271
  • convert{Tv<:Union{Complex{Float64},Float64}}(::Type{Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}}, m::Integer, n::Integer, colptr::Array{Int64,1}, rowval::Array{Int64,1}, nzval::Array{Tv<:Union{Complex{Float64},Float64},1}, stype) at sparse/cholmod.jl:826
  • convert{Tv<:Union{Complex{Float64},Float64}}(::Type{Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}}, m::Integer, n::Integer, colptr::Array{Int64,1}, rowval::Array{Int64,1}, nzval::Array{Tv<:Union{Complex{Float64},Float64},1}) at sparse/cholmod.jl:847
  • copy(a::Array{T,N}) at array.jl:100
  • copy!{T}(dest::Array{T,N}, doffs::Integer, src::Array{T,N}, soffs::Integer, n::Integer) at array.jl:90
  • copy!{T}(dest::Array{T,N}, src::Array{T,N}) at array.jl:97
  • copy!(S::SharedArray{T,N}, A::Array{T,N}) at sharedarray.jl:434
  • copy!{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},Ti<:Integer}(dest::Array{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},N}, rdest::Union{Range{Ti<:Integer},UnitRange{Ti<:Integer}}, src::Array{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},N}, rsrc::Union{Range{Ti<:Integer},UnitRange{Ti<:Integer}}) at linalg/blas.jl:946
  • display(d::Base.REPL.REPLDisplay{R<:Base.REPL.AbstractREPL}, md::Array{Base.Markdown.MD,1}) at markdown/Markdown.jl:61
  • div(A::BitArray{N}, B::Array{Bool,N}) at bitarray.jl:880
  • div(A::Array{Bool,N}, B::BitArray{N}) at bitarray.jl:881
  • done(a::Array{T,N}, i) at array.jl:277
  • done(f::Base.FixedPartitions, s::Array{Int64,1}) at combinatorics.jl:322
  • dot{T<:Union{Float32,Float64},TI<:Integer}(x::Array{T<:Union{Float32,Float64},1}, rx::Union{Range{TI<:Integer},UnitRange{TI<:Integer}}, y::Array{T<:Union{Float32,Float64},1}, ry::Union{Range{TI<:Integer},UnitRange{TI<:Integer}}) at linalg/matmul.jl:51
  • dot{T<:Union{Complex{Float32},Complex{Float64}},TI<:Integer}(x::Array{T<:Union{Complex{Float32},Complex{Float64}},1}, rx::Union{Range{TI<:Integer},UnitRange{TI<:Integer}}, y::Array{T<:Union{Complex{Float32},Complex{Float64}},1}, ry::Union{Range{TI<:Integer},UnitRange{TI<:Integer}}) at linalg/matmul.jl:64
  • eigvecs{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},Eigenvalue<:Real}(A::SymTridiagonal{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64}}, eigvals::Array{Eigenvalue<:Real,1}) at linalg/tridiag.jl:143
  • evalfile(path::AbstractString, args::Array{UTF8String,1}) at loading.jl:336
  • factorize{T}(A::Array{T,2}) at linalg/dense.jl:366
  • fill!{T<:Union{AbstractFloat,Integer}}(a::Array{T<:Union{AbstractFloat,Integer},N}, x) at array.jl:187
  • flipdim{T}(A::Array{T,N}, d::Integer) at arraymath.jl:212
  • float32_isvalid(s::AbstractString, out::Array{Float32,1}) at deprecated.jl:473
  • float64_isvalid(s::AbstractString, out::Array{Float64,1}) at deprecated.jl:478
  • frexp{T<:AbstractFloat}(A::Array{T<:AbstractFloat,N}) at math.jl:253
  • gensym(a::Array{UInt8,1}) at expr.jl:16
  • getindex(A::Array{T,N}, i1::Real) at array.jl:282
  • getindex(A::Array{T,N}, i1::Real, i2::Real, I::Real...) at array.jl:283
  • getindex(A::Array{T,N}, I::UnitRange{Int64}) at array.jl:288
  • getindex(A::Array{T,N}, c::Colon) at array.jl:297
  • getindex{T<:Real}(A::Array{T,N}, I::Range{T<:Real}) at array.jl:309
  • ind2sub!{T<:Integer}(sub::Array{T<:Integer,N}, dims::Tuple{Vararg{T<:Integer}}, ind::T<:Integer) at abstractarray.jl:1141
  • ind2sub!{T<:Integer}(sub::Array{T<:Integer,N}, dims::Array{T<:Integer,N}, ind::T<:Integer) at deprecated.jl:49
  • indmax(f::Array{Base.Pkg.Resolve.MaxSum.FieldValues.FieldValue,1}) at pkg/resolve/fieldvalue.jl:74
  • insert!{T}(a::Array{T,1}, i::Integer, item) at array.jl:506
  • isassigned{T}(a::Array{T,N}, i::Int64...) at array.jl:65
  • isvalid(str::Array{Char,1}) at unicode/utf32.jl:174
  • kill(ps::Array{Base.Process,1}) at process.jl:585
  • kron{T,S}(a::Array{T,2}, b::Array{S,2}) at linalg/dense.jl:154
  • launch(manager::Base.SSHManager, params::Dict{K,V}, launched::Array{T,N}, launch_ntfy::Condition) at managers.jl:59
  • launch(manager::Base.LocalManager, params::Dict{K,V}, launched::Array{T,N}, c::Condition) at managers.jl:186
  • length(a::Array{T,N}) at array.jl:56
  • lexcmp(a::Array{UInt8,1}, b::Array{UInt8,1}) at array.jl:629
  • linreg{T<:Number}(X::Union{DenseArray{T<:Number,1},DenseArray{T<:Number,2},SubArray{T<:Number,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{T<:Number,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, y::Array{T<:Number,1}) at linalg/generic.jl:403
  • map(f::Function, a::Array{Any,1}) at essentials.jl:153
  • map(::Type{Integer}, a::Array{T,N}) at abstractarray.jl:445
  • map(::Type{Signed}, a::Array{T,N}) at abstractarray.jl:446
  • map(::Type{Unsigned}, a::Array{T,N}) at abstractarray.jl:447
  • mod(A::BitArray{N}, B::Array{Bool,N}) at bitarray.jl:901
  • mod(A::Array{Bool,N}, B::BitArray{N}) at bitarray.jl:902
  • msync{T}(A::Array{T,N}) at deprecated.jl:578
  • next(a::Array{T,N}, i) at array.jl:276
  • next(f::Base.FixedPartitions, s::Array{Int64,1}) at combinatorics.jl:326
  • nextprod(a::Array{Int64,1}, x) at combinatorics.jl:524
  • permutedims!{T,N}(P::Array{T,N}, B::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, perm) at multidimensional.jl:775
  • prepend!(A::Array{Bool,1}, items::BitArray{1}) at bitarray.jl:487
  • prepend!{T}(a::Array{T,1}, items::AbstractArray{T,1}) at array.jl:452
  • prevprod(a::Array{Int64,1}, x) at combinatorics.jl:565
  • process_exited(s::Array{Base.Process,1}) at process.jl:599
  • process_running(s::Array{Base.Process,1}) at process.jl:595
  • push!(a::Array{Any,1}, item::ANY) at array.jl:439
  • push!{T}(a::Array{T,1}, item) at array.jl:432
  • rand!{T<:Union{Bool,Int128,Int16,Int32,Int64,Int8,UInt128,UInt16,UInt32,UInt64,UInt8}}(rd::RandomDevice, A::Array{T<:Union{Bool,Int128,Int16,Int32,Int64,Int8,UInt128,UInt16,UInt32,UInt64,UInt8},N}) at random.jl:39
  • rand!(r::MersenneTwister, A::Array{Float64,N}) at random.jl:333
  • rand!(r::MersenneTwister, A::Array{Float64,N}, n::Int64) at random.jl:333
  • rand!{I<:Base.Random.FloatInterval}(r::MersenneTwister, A::Array{Float64,N}, n::Int64, ::Type{I<:Base.Random.FloatInterval}) at random.jl:333
  • rand!{T<:Union{Float16,Float32}}(r::MersenneTwister, A::Array{T<:Union{Float16,Float32},N}, ::Type{Base.Random.Close1Open2}) at random.jl:360
  • rand!{T<:Union{Float16,Float32}}(r::MersenneTwister, A::Array{T<:Union{Float16,Float32},N}, ::Type{Base.Random.CloseOpen}) at random.jl:384
  • rand!{T<:Union{Float16,Float32}}(r::MersenneTwister, A::Array{T<:Union{Float16,Float32},N}) at random.jl:392
  • rand!(r::MersenneTwister, A::Array{UInt128,N}) at random.jl:396
  • rand!(r::MersenneTwister, A::Array{UInt128,N}, n::Int64) at random.jl:396
  • rand!{T<:Union{Int128,Int16,Int32,Int64,Int8,UInt16,UInt32,UInt64,UInt8}}(r::MersenneTwister, A::Array{T<:Union{Int128,Int16,Int32,Int64,Int8,UInt16,UInt32,UInt64,UInt8},N}) at random.jl:424
  • randexp!(rng::AbstractRNG, A::Array{Float64,N}) at random.jl:1167
  • randexp!(A::Array{Float64,N}) at random.jl:1173
  • read{T,N}(t::Base.Terminals.UnixTerminal, x::Array{T,N}) at Terminals.jl:195
  • read!(from::Base.AbstractIOBuffer{T<:AbstractArray{UInt8,1}}, a::Array{UInt8,1}) at iobuffer.jl:55
  • read!(from::Base.AbstractIOBuffer{T<:AbstractArray{UInt8,1}}, a::Array{T,N}) at iobuffer.jl:56
  • read!(s::IOStream, a::Array{UInt8,1}) at iostream.jl:174
  • read!(io::Base.AbstractPipe, bytes::Array{UInt8,1}) at stream.jl:584
  • read!(s::BufferStream, a::Array{UInt8,1}) at stream.jl:1189
  • read!(s::Base.LibuvStream, a::Array{UInt8,1}) at stream.jl:895
  • read!(f::Base.FS.File, a::Array{UInt8,1}) at fs.jl:235
  • read!(f::Base.FS.File, a::Array{UInt8,1}, nel) at fs.jl:235
  • read!(s::IO, a::Array{UInt8,1}) at io.jl:133
  • read!{T}(s::IO, a::Array{T,N}) at io.jl:140
  • readbytes!(io::Base.AbstractIOBuffer{T<:AbstractArray{UInt8,1}}, b::Array{UInt8,N}) at iobuffer.jl:332
  • readbytes!(io::Base.AbstractIOBuffer{T<:AbstractArray{UInt8,1}}, b::Array{UInt8,N}, nb) at iobuffer.jl:332
  • readbytes!(s::IOStream, b::Array{UInt8,N}) at iostream.jl:250
  • readbytes!(s::IOStream, b::Array{UInt8,N}, nb) at iostream.jl:250
  • reinterpret{T,S}(::Type{T}, a::Array{S,1}) at array.jl:106
  • reinterpret{T,S}(::Type{T}, a::Array{S,N}) at array.jl:112
  • reinterpret{T,S,N}(::Type{T}, a::Array{S,N}, dims::NTuple{N,Int64}) at array.jl:119
  • repeat{T}(A::Array{T,N}) at abstractarraymath.jl:206
  • reprmime(::MIME{symbol("text/vnd.graphviz")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/latex")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/calendar")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/n3")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/richtext")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/x-setext")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/sgml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/tab-separated-values")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/x-vcalendar")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/x-vcard")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/cmd")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/css")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/csv")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/html")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/javascript")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/markdown")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/plain")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/vcard")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("text/xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/atom+xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/ecmascript")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/json")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/rdf+xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/rss+xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/xml-dtd")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/postscript")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("image/svg+xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/x-latex")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/xhtml+xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/javascript")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("application/xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("model/x3d+xml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("model/x3d+vrml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(::MIME{symbol("model/vrml")}, ::Array{UInt8,1}) at multimedia.jl:65
  • reprmime(m::MIME{mime}, x::Array{UInt8,1}) at multimedia.jl:83
  • reshape{T,N}(a::Array{T,N}, dims::NTuple{N,Int64}) at array.jl:134
  • reshape{T,N}(a::Array{T,N}, dims::NTuple{N,Int64}) at array.jl:145
  • scale{Tv,Ti,T}(A::SparseMatrixCSC{Tv,Ti}, b::Array{T,1}) at sparse/linalg.jl:834
  • scale{T,Tv,Ti}(b::Array{T,1}, A::SparseMatrixCSC{Tv,Ti}) at sparse/linalg.jl:837
  • scale!{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64}}(X::Array{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},N}, s::T<:Union{Complex{Float32},Complex{Float64},Float32,Float64}) at linalg/dense.jl:13
  • scale!{T<:Union{Complex{Float32},Complex{Float64}}}(X::Array{T<:Union{Complex{Float32},Complex{Float64}},N}, s::Real) at linalg/dense.jl:23
  • scale!{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64}}(X::Array{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},N}, s::Number) at linalg/dense.jl:21
  • serialize(s::SerializationState{I<:IO}, a::Array{T,N}) at serialize.jl:169
  • setenv{S<:Union{ASCIIString,UTF8String}}(cmd::Cmd, env::Array{S<:Union{ASCIIString,UTF8String},N}) at process.jl:155
  • setindex!(A::Array{Any,N}, x::ANY, i::Int64) at essentials.jl:141
  • setindex!{T}(A::Array{T,N}, x, i1::Real) at array.jl:313
  • setindex!{T}(A::Array{T,N}, x, i1::Real, i2::Real, I::Real...) at array.jl:314
  • setindex!{T}(A::Array{T,N}, X::Array{T,N}, I::UnitRange{Int64}) at array.jl:346
  • setindex!(A::Array{T,N}, X::AbstractArray{T,N}, I::AbstractArray{Int64,1}) at array.jl:330
  • setindex!(A::Array{T,N}, x, I::AbstractArray{Int64,1}) at array.jl:323
  • setindex!{T}(A::Array{T,N}, X::Array{T,N}, c::Colon) at array.jl:355
  • similar{T}(a::Array{T,1}) at array.jl:154
  • similar{T}(a::Array{T,2}) at array.jl:155
  • similar{T}(a::Array{T,1}, dims::Tuple{Vararg{Int64}}) at array.jl:156
  • similar{T}(a::Array{T,1}, m::Int64) at array.jl:157
  • similar{T}(a::Array{T,1}, S) at array.jl:158
  • similar{T}(a::Array{T,2}, dims::Tuple{Vararg{Int64}}) at array.jl:159
  • similar{T}(a::Array{T,2}, m::Int64) at array.jl:160
  • similar{T}(a::Array{T,2}, S) at array.jl:161
  • similar(a::Array{T,N}, T, dims::Tuple{Vararg{Int64}}) at array.jl:153
  • size{_}(a::Array{_,3}) at array.jl:51
  • size{_}(a::Array{_,4}) at array.jl:52
  • size(a::Array{T,N}, d) at array.jl:48
  • size{_,N}(a::Array{_,N}) at array.jl:54
  • sizeof(a::Array{T,N}) at array.jl:58
  • slicedim(A::Array{T,N}, d::Integer, i::Integer) at arraymath.jl:178
  • sort!{O<:Union{Base.Order.ForwardOrdering,Base.Order.ReverseOrdering{Base.Order.ForwardOrdering}},T<:Union{Float32,Float64}}(v::Array{Int64,1}, a::Base.Sort.Algorithm, o::Base.Order.Perm{O<:Union{Base.Order.ForwardOrdering,Base.Order.ReverseOrdering{Base.Order.ForwardOrdering}},Array{T<:Union{Float32,Float64},1}}) at sort.jl:601
  • srand(r::MersenneTwister, seed::Array{UInt32,1}) at random.jl:111
  • start(A::Array{T,N}) at array.jl:275
  • startswith(a::Array{UInt8,1}, b::Array{UInt8,1}) at strings/util.jl:34
  • strides{T}(a::Array{T,1}) at array.jl:60
  • strides{T}(a::Array{T,2}) at array.jl:61
  • strides{T}(a::Array{T,3}) at array.jl:62
  • stringmime(::MIME{symbol("text/vnd.graphviz")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/latex")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/calendar")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/n3")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/richtext")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/x-setext")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/sgml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/tab-separated-values")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/x-vcalendar")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/x-vcard")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/cmd")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/css")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/csv")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/html")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/javascript")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/markdown")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/plain")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/vcard")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("text/xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/atom+xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/ecmascript")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/json")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/rdf+xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/rss+xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/xml-dtd")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/postscript")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("image/svg+xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/x-latex")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/xhtml+xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/javascript")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("application/xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("model/x3d+xml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("model/x3d+vrml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(::MIME{symbol("model/vrml")}, ::Array{UInt8,1}) at multimedia.jl:66
  • stringmime(m::MIME{mime}, x::Array{UInt8,1}) at multimedia.jl:85
  • sub2ind{T<:Integer}(dims::Array{T<:Integer,N}, sub::Array{T<:Integer,N}) at deprecated.jl:49
  • success(procs::Array{Base.Process,1}) at process.jl:549
  • symbol(a::Array{UInt8,1}) at expr.jl:8
  • symperm{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, pinv::Array{Ti,1}) at sparse/csparse.jl:319
  • trace{T}(A::Array{T,2}) at linalg/dense.jl:145
  • unsafe_copy!{T}(dest::Array{T,N}, doffs, src::Array{T,N}, soffs, n) at array.jl:79
  • unshift!{T}(a::Array{T,1}, item) at array.jl:490
  • write(to::Base.AbstractIOBuffer{T<:AbstractArray{UInt8,1}}, a::Array{T,N}) at iobuffer.jl:316
  • write{T}(s::IOStream, a::Array{T,N}) at iostream.jl:125
  • write(io::Base.AbstractPipe, bytes::Array{UInt8,1}) at stream.jl:576
  • write{S<:Base.AbstractPipe}(io::S<:Base.AbstractPipe, a::Array{T,N}) at stream.jl:578
  • write{T}(s::BufferStream, a::Array{T,N}) at stream.jl:1216
  • write{T}(s::Base.LibuvStream, a::Array{T,N}) at stream.jl:1009
  • write{T}(f::Base.FS.File, a::Array{T,N}) at fs.jl:200
  • write{T,N}(t::Base.Terminals.UnixTerminal, a::Array{T,N}) at Terminals.jl:191
  • xdump(fn::Function, io::IO, x::Array{Any,N}, n::Int64, indent) at show.jl:853
  • xdump(fn::Function, io::IO, x::Array{T,N}, n::Int64, indent) at show.jl:864
  • add_padding_PKCS5(data::Array{UInt8,1}, block_size::Int64) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:86
  • decrypt(state::Nettle.Decryptor, e::Symbol, iv::Array{UInt8,1}, data) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:198
  • decrypt(name::AbstractString, e::Symbol, iv::Array{UInt8,1}, key, data) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:282
  • decrypt!(state::Nettle.Decryptor, e::Symbol, iv::Array{UInt8,1}, result, data) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:141
  • encrypt(state::Nettle.Encryptor, e::Symbol, iv::Array{UInt8,1}, data) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:267
  • encrypt(name::AbstractString, e::Symbol, iv::Array{UInt8,1}, key, data) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:283
  • encrypt!(state::Nettle.Encryptor, e::Symbol, iv::Array{UInt8,1}, result, data) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:210
  • gen_key32_iv16(pw::Array{UInt8,1}, salt::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:79
  • trim_padding_PKCS5(data::Array{UInt8,1}) at /Users/nkarast/.julia/v0.4/Nettle/src/cipher.jl:92

In [56]:
append!(k,[2.,3.,4.]) # similar to push! but for multiple variables at once


Out[56]:
7-element Array{Float64,1}:
 1.0
 3.0
 9.2
 7.0
 2.0
 3.0
 4.0

Arrays work like mathematical vectors .


In [57]:
a = [1.1, 2.2, 3.3]


Out[57]:
3-element Array{Float64,1}:
 1.1
 2.2
 3.3

In [58]:
b = [4.4, 5.5, 6.6]


Out[58]:
3-element Array{Float64,1}:
 4.4
 5.5
 6.6

In [59]:
a+b


Out[59]:
3-element Array{Float64,1}:
 5.5
 7.7
 9.9

In [60]:
b-a


Out[60]:
3-element Array{Float64,1}:
 3.3
 3.3
 3.3

In [61]:
(2.5 * a )


Out[61]:
3-element Array{Float64,1}:
 2.75
 5.5 
 8.25

However, operations between arrays are not permitted because of their loose definition. (What does it mean to you a*b? Inner vector? Product of each element?)


In [62]:
a*b


LoadError: MethodError: `*` has no method matching *(::Array{Float64,1}, ::Array{Float64,1})
Closest candidates are:
  *(::Any, ::Any, !Matched::Any, !Matched::Any...)
  *{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},S}(!Matched::Union{DenseArray{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2},SubArray{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, ::Union{DenseArray{S,1},SubArray{S,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}})
  *{TA,TB}(!Matched::Base.LinAlg.AbstractTriangular{TA,S<:AbstractArray{T,2}}, ::Union{DenseArray{TB,1},DenseArray{TB,2},SubArray{TB,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{TB,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}})
  ...
while loading In[62], in expression starting on line 1

HOWEVER for element-wise operations you can use the Matlab-syntax .:


In [63]:
a.*b


Out[63]:
3-element Array{Float64,1}:
  4.84
 12.1 
 21.78

But there are also so many usefull built-in functions:


In [64]:
dot(a,b) # dot product


Out[64]:
38.72

In [65]:
a  b # using \cdot<TAB>


Out[65]:
38.72

In [66]:
cross(a,b) # cross product of vectors


Out[66]:
3-element Array{Float64,1}:
 -3.63
  7.26
 -3.63

In [67]:
a×b # \times<TAB>


Out[67]:
3-element Array{Float64,1}:
 -3.63
  7.26
 -3.63

In [68]:
norm(a) # the norm of the vector a


Out[68]:
4.115823125451335

Use the help() command for more info


In [69]:
help(dot)


LoadError: UndefVarError: help not defined
while loading In[69], in expression starting on line 1

In [70]:
?dot


search: dot vecdot STDOUT @doc_str midpoints fieldoffsets UDPSocket

Out[70]:
dot(x, y)
⋅(x,y)

Compute the dot product. For complex vectors, the first vector is conjugated.


In [71]:
transpose(a) # this is a row vector


Out[71]:
1x3 Array{Float64,2}:
 1.1  2.2  3.3

In [72]:
a' # take the conjugate and then transpose


Out[72]:
1x3 Array{Float64,2}:
 1.1  2.2  3.3

In [73]:
a.' 
# this just takes the transpose without the conjugate


Out[73]:
1x3 Array{Float64,2}:
 1.1  2.2  3.3

In [74]:
j = [5+9im, 4+2im, 8+3im]


Out[74]:
3-element Array{Complex{Int64},1}:
 5+9im
 4+2im
 8+3im

In [75]:
j'


Out[75]:
1x3 Array{Complex{Int64},2}:
 5-9im  4-2im  8-3im

In [76]:
j.'


Out[76]:
1x3 Array{Complex{Int64},2}:
 5+9im  4+2im  8+3im

To define a matrix use Matlab notation (with shape)


In [77]:
M = [2 1; 1 1]


Out[77]:
2x2 Array{Int64,2}:
 2  1
 1  1

or the numpy one by defining a list and reshaping:


In [78]:
N = reshape([1,3,4,4], (2,2))


Out[78]:
2x2 Array{Int64,2}:
 1  4
 3  4

In [79]:
O = reshape(1:8, (2,2,2))


Out[79]:
2x2x2 Array{Int64,3}:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
 5  7
 6  8

In [80]:
transpose(M)


Out[80]:
2x2 Array{Int64,2}:
 2  1
 1  1

In [81]:
inv(M) # inverse


Out[81]:
2x2 Array{Float64,2}:
  1.0  -1.0
 -1.0   2.0

In [82]:
det(M) # determinant


Out[82]:
1.0

Control Flow : No Colon (:), add and end

Indentation is not significant. The syntax is the same as in Python, but drop the colon, and add an end !


In [83]:
i = 0
while i < 5 print("$i\t")
    i+=1
end


0	1	2	3	4	

In [84]:
total = 0
for i = 1:10
    total +=i
end
println("Sum is $total")


Sum is 55

In [85]:
typeof(1:10)


Out[85]:
UnitRange{Int64}

The notation 1:10 is a range object that can be iterated over!

You can construct an array by enclosing it in square brackets:


In [86]:
[1:2:10] # 1 to 10 by a step of 2


WARNING: [a] concatenation is deprecated; use collect(a) instead
 in depwarn at deprecated.jl:73
 in oldstyle_vcat_warning at /Applications/Julia-0.4.5.app/Contents/Resources/julia/lib/julia/sys.dylib
 in vect at abstractarray.jl:32
 in include_string at loading.jl:282
 in execute_request_0x535c5df2 at /Users/nkarast/.julia/v0.4/IJulia/src/execute_request.jl:183
 in eventloop at /Users/nkarast/.julia/v0.4/IJulia/src/IJulia.jl:143
 in anonymous at task.jl:447
while loading In[86], in expression starting on line 1
Out[86]:
5-element Array{Int64,1}:
 1
 3
 5
 7
 9

In [87]:
[1:2:10, 19]


Out[87]:
6-element Array{Int64,1}:
  1
  3
  5
  7
  9
 19

Short-circuit evaluation (if-then)


In [88]:
a = 3
a < 5 && println("Small") # evaluate the second argument if the first is true


Small

In [89]:
a>10 && println("Small")


Out[89]:
false

In [90]:
a>10 || println("Small")  # this is semantics of the if_not-then


Small

Similary the terciary operator:


In [91]:
a == 3 ? println("Hello") : println("Not true")


Hello

Array Comprehensions

Just like in Python you can do array comprehensions.


In [92]:
squares = [i^2 for i in [1:2:10,7]]


WARNING: [a,b] concatenation is deprecated; use [a;b] instead
 in depwarn at deprecated.jl:73
 in oldstyle_vcat_warning at /Applications/Julia-0.4.5.app/Contents/Resources/julia/lib/julia/sys.dylib
 in vect at abstractarray.jl:38
 in anonymous at no file
 in include_string at loading.jl:282
 in execute_request_0x535c5df2 at /Users/nkarast/.julia/v0.4/IJulia/src/execute_request.jl:183
 in eventloop at /Users/nkarast/.julia/v0.4/IJulia/src/IJulia.jl:143
 in anonymous at task.jl:447
while loading In[92], in expression starting on line 1
Out[92]:
6-element Array{Any,1}:
  1
  9
 25
 49
 81
 49

In [93]:
sums = [i+j for i=1:5, j=1:5]


Out[93]:
5x5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

Matrices

The notation with commas is a 1d vector (column vector).


In [94]:
v = [1,2,3]


Out[94]:
3-element Array{Int64,1}:
 1
 2
 3

For a row vector, or matrices use the Matlab-style notation.

If we omit the commas we obtain a 1 x n matrix


In [95]:
row_vec = [3 4 5]


Out[95]:
1x3 Array{Int64,2}:
 3  4  5

Hermitian transpose : (Conjugate then transpose) by using '

Simple transpose : Transpose by using .'


In [96]:
row_vec = [3im 4 1+5im]


Out[96]:
1x3 Array{Complex{Int64},2}:
 0+3im  4+0im  1+5im

In [97]:
row_vec'


Out[97]:
3x1 Array{Complex{Int64},2}:
 0-3im
 4+0im
 1-5im

In [98]:
row_vec.'


Out[98]:
3x1 Array{Complex{Int64},2}:
 0+3im
 4+0im
 1+5im

In [99]:
M = [1 2 ; 3 4]


Out[99]:
2x2 Array{Int64,2}:
 1  2
 3  4

NB: There is a difference in the way Python and Julia treat slices of matrices

In Python: a one-dimensional slice in either direction returns a 1-dimensional vector.

In Julia: a vertical one dimensional slice gives a 1-dimensional vector ("column vector"), but a horizontal one-dimensional slice produces a 1xn matrix!


In [100]:
M[:,1]


Out[100]:
2-element Array{Int64,1}:
 1
 3

In [101]:
M[1,:]


Out[101]:
1x2 Array{Int64,2}:
 1  2

Matrix Multiplication


In [102]:
v = [1, 2]


Out[102]:
2-element Array{Int64,1}:
 1
 2

In [103]:
v*v


LoadError: MethodError: `*` has no method matching *(::Array{Int64,1}, ::Array{Int64,1})
Closest candidates are:
  *(::Any, ::Any, !Matched::Any, !Matched::Any...)
  *{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},S}(!Matched::Union{DenseArray{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2},SubArray{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, ::Union{DenseArray{S,1},SubArray{S,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}})
  *{TA,TB}(!Matched::Base.LinAlg.AbstractTriangular{TA,S<:AbstractArray{T,2}}, ::Union{DenseArray{TB,1},DenseArray{TB,2},SubArray{TB,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{TB,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}})
  ...
while loading In[103], in expression starting on line 1

In [104]:
dot(v,v)


Out[104]:
5

In [105]:
M = [1 2 ; 3 4]


Out[105]:
2x2 Array{Int64,2}:
 1  2
 3  4

In [106]:
M*v


Out[106]:
2-element Array{Int64,1}:
  5
 11

In [107]:
methods(*)


Out[107]:
138 methods for generic function *:
  • *(x::Bool, y::Bool) at bool.jl:38
  • *{T<:Unsigned}(x::Bool, y::T<:Unsigned) at bool.jl:53
  • *(x::Bool, z::Complex{Bool}) at complex.jl:122
  • *(x::Bool, z::Complex{T<:Real}) at complex.jl:129
  • *{T<:Number}(x::Bool, y::T<:Number) at bool.jl:49
  • *(x::Float32, y::Float32) at float.jl:211
  • *(x::Float64, y::Float64) at float.jl:212
  • *(z::Complex{T<:Real}, w::Complex{T<:Real}) at complex.jl:113
  • *(z::Complex{Bool}, x::Bool) at complex.jl:123
  • *(z::Complex{T<:Real}, x::Bool) at complex.jl:130
  • *(x::Real, z::Complex{Bool}) at complex.jl:140
  • *(z::Complex{Bool}, x::Real) at complex.jl:141
  • *(x::Real, z::Complex{T<:Real}) at complex.jl:152
  • *(z::Complex{T<:Real}, x::Real) at complex.jl:153
  • *(x::Rational{T<:Integer}, y::Rational{T<:Integer}) at rational.jl:186
  • *(a::Float16, b::Float16) at float16.jl:136
  • *{N}(a::Integer, index::CartesianIndex{N}) at multidimensional.jl:50
  • *(x::BigInt, y::BigInt) at gmp.jl:256
  • *(a::BigInt, b::BigInt, c::BigInt) at gmp.jl:279
  • *(a::BigInt, b::BigInt, c::BigInt, d::BigInt) at gmp.jl:285
  • *(a::BigInt, b::BigInt, c::BigInt, d::BigInt, e::BigInt) at gmp.jl:292
  • *(x::BigInt, c::Union{UInt16,UInt32,UInt64,UInt8}) at gmp.jl:326
  • *(c::Union{UInt16,UInt32,UInt64,UInt8}, x::BigInt) at gmp.jl:330
  • *(x::BigInt, c::Union{Int16,Int32,Int64,Int8}) at gmp.jl:332
  • *(c::Union{Int16,Int32,Int64,Int8}, x::BigInt) at gmp.jl:336
  • *(x::BigFloat, y::BigFloat) at mpfr.jl:208
  • *(x::BigFloat, c::Union{UInt16,UInt32,UInt64,UInt8}) at mpfr.jl:215
  • *(c::Union{UInt16,UInt32,UInt64,UInt8}, x::BigFloat) at mpfr.jl:219
  • *(x::BigFloat, c::Union{Int16,Int32,Int64,Int8}) at mpfr.jl:223
  • *(c::Union{Int16,Int32,Int64,Int8}, x::BigFloat) at mpfr.jl:227
  • *(x::BigFloat, c::Union{Float16,Float32,Float64}) at mpfr.jl:231
  • *(c::Union{Float16,Float32,Float64}, x::BigFloat) at mpfr.jl:235
  • *(x::BigFloat, c::BigInt) at mpfr.jl:239
  • *(c::BigInt, x::BigFloat) at mpfr.jl:243
  • *(a::BigFloat, b::BigFloat, c::BigFloat) at mpfr.jl:379
  • *(a::BigFloat, b::BigFloat, c::BigFloat, d::BigFloat) at mpfr.jl:385
  • *(a::BigFloat, b::BigFloat, c::BigFloat, d::BigFloat, e::BigFloat) at mpfr.jl:392
  • *{T<:Number}(x::T<:Number, D::Diagonal{T}) at linalg/diagonal.jl:89
  • *(x::Irrational{sym}, y::Irrational{sym}) at irrationals.jl:72
  • *(y::Real, x::Base.Dates.Period) at dates/periods.jl:55
  • *(x::Number) at operators.jl:74
  • *(y::Number, x::Bool) at bool.jl:55
  • *(x::Int8, y::Int8) at int.jl:19
  • *(x::UInt8, y::UInt8) at int.jl:19
  • *(x::Int16, y::Int16) at int.jl:19
  • *(x::UInt16, y::UInt16) at int.jl:19
  • *(x::Int32, y::Int32) at int.jl:19
  • *(x::UInt32, y::UInt32) at int.jl:19
  • *(x::Int64, y::Int64) at int.jl:19
  • *(x::UInt64, y::UInt64) at int.jl:19
  • *(x::Int128, y::Int128) at int.jl:456
  • *(x::UInt128, y::UInt128) at int.jl:457
  • *{T<:Number}(x::T<:Number, y::T<:Number) at promotion.jl:212
  • *(x::Number, y::Number) at promotion.jl:168
  • *{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},S}(A::Union{DenseArray{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2},SubArray{T<:Union{Complex{Float32},Complex{Float64},Float32,Float64},2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, x::Union{DenseArray{S,1},SubArray{S,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at linalg/matmul.jl:82
  • *(A::SymTridiagonal{T}, B::Number) at linalg/tridiag.jl:86
  • *(A::Tridiagonal{T}, B::Number) at linalg/tridiag.jl:406
  • *(A::UpperTriangular{T,S<:AbstractArray{T,2}}, x::Number) at linalg/triangular.jl:454
  • *(A::Base.LinAlg.UnitUpperTriangular{T,S<:AbstractArray{T,2}}, x::Number) at linalg/triangular.jl:457
  • *(A::LowerTriangular{T,S<:AbstractArray{T,2}}, x::Number) at linalg/triangular.jl:454
  • *(A::Base.LinAlg.UnitLowerTriangular{T,S<:AbstractArray{T,2}}, x::Number) at linalg/triangular.jl:457
  • *(A::Tridiagonal{T}, B::UpperTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:969
  • *(A::Tridiagonal{T}, B::Base.LinAlg.UnitUpperTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:969
  • *(A::Tridiagonal{T}, B::LowerTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:969
  • *(A::Tridiagonal{T}, B::Base.LinAlg.UnitLowerTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:969
  • *(A::Base.LinAlg.AbstractTriangular{T,S<:AbstractArray{T,2}}, B::Base.LinAlg.AbstractTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:975
  • *{TA,TB}(A::Base.LinAlg.AbstractTriangular{TA,S<:AbstractArray{T,2}}, B::Union{DenseArray{TB,1},DenseArray{TB,2},SubArray{TB,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{TB,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at linalg/triangular.jl:989
  • *{TA,TB}(A::Union{DenseArray{TA,1},DenseArray{TA,2},SubArray{TA,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{TA,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, B::Base.LinAlg.AbstractTriangular{TB,S<:AbstractArray{T,2}}) at linalg/triangular.jl:1016
  • *{TA,Tb}(A::Union{Base.LinAlg.QRCompactWYQ{TA,M<:AbstractArray{T,2}},Base.LinAlg.QRPackedQ{TA,S<:AbstractArray{T,2}}}, b::Union{DenseArray{Tb,1},SubArray{Tb,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at linalg/qr.jl:166
  • *{TA,TB}(A::Union{Base.LinAlg.QRCompactWYQ{TA,M<:AbstractArray{T,2}},Base.LinAlg.QRPackedQ{TA,S<:AbstractArray{T,2}}}, B::Union{DenseArray{TB,2},SubArray{TB,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at linalg/qr.jl:178
  • *{TA,TQ,N}(A::Union{DenseArray{TA,N},SubArray{TA,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, Q::Union{Base.LinAlg.QRCompactWYQ{TQ,M<:AbstractArray{T,2}},Base.LinAlg.QRPackedQ{TQ,S<:AbstractArray{T,2}}}) at linalg/qr.jl:253
  • *(A::Union{Hermitian{T,S},Symmetric{T,S}}, B::Union{Hermitian{T,S},Symmetric{T,S}}) at linalg/symmetric.jl:117
  • *(A::Union{DenseArray{T,2},SubArray{T,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, B::Union{Hermitian{T,S},Symmetric{T,S}}) at linalg/symmetric.jl:118
  • *{T<:Number}(D::Diagonal{T}, x::T<:Number) at linalg/diagonal.jl:90
  • *(Da::Diagonal{T}, Db::Diagonal{T}) at linalg/diagonal.jl:92
  • *(D::Diagonal{T}, V::Array{T,1}) at linalg/diagonal.jl:93
  • *(A::Array{T,2}, D::Diagonal{T}) at linalg/diagonal.jl:94
  • *(D::Diagonal{T}, A::Array{T,2}) at linalg/diagonal.jl:95
  • *(A::Bidiagonal{T}, B::Number) at linalg/bidiag.jl:192
  • *(A::Union{Base.LinAlg.AbstractTriangular{T,S<:AbstractArray{T,2}},Bidiagonal{T},Diagonal{T},SymTridiagonal{T},Tridiagonal{T}}, B::Union{Base.LinAlg.AbstractTriangular{T,S<:AbstractArray{T,2}},Bidiagonal{T},Diagonal{T},SymTridiagonal{T},Tridiagonal{T}}) at linalg/bidiag.jl:198
  • *{T}(A::Bidiagonal{T}, B::AbstractArray{T,1}) at linalg/bidiag.jl:202
  • *(B::BitArray{2}, J::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:122
  • *{T,S}(s::Base.LinAlg.SVDOperator{T,S}, v::Array{T,1}) at linalg/arnoldi.jl:261
  • *(S::SparseMatrixCSC{Tv,Ti<:Integer}, J::UniformScaling{T<:Number}) at sparse/linalg.jl:23
  • *{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}) at sparse/linalg.jl:108
  • *{TvA,TiA,TvB,TiB}(A::SparseMatrixCSC{TvA,TiA}, B::SparseMatrixCSC{TvB,TiB}) at sparse/linalg.jl:29
  • *{TX,TvA,TiA}(X::Union{DenseArray{TX,2},SubArray{TX,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, A::SparseMatrixCSC{TvA,TiA}) at sparse/linalg.jl:94
  • *(A::Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}, B::Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}) at sparse/cholmod.jl:1157
  • *(A::Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}, B::Base.SparseMatrix.CHOLMOD.Dense{T<:Union{Complex{Float64},Float64}}) at sparse/cholmod.jl:1158
  • *(A::Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}, B::Union{Array{T,1},Array{T,2}}) at sparse/cholmod.jl:1159
  • *{Ti}(A::Symmetric{Float64,SparseMatrixCSC{Float64,Ti}}, B::SparseMatrixCSC{Float64,Ti}) at sparse/cholmod.jl:1418
  • *{Ti}(A::Hermitian{Complex{Float64},SparseMatrixCSC{Complex{Float64},Ti}}, B::SparseMatrixCSC{Complex{Float64},Ti}) at sparse/cholmod.jl:1419
  • *{T<:Number}(x::AbstractArray{T<:Number,2}) at abstractarraymath.jl:50
  • *(B::Number, A::SymTridiagonal{T}) at linalg/tridiag.jl:87
  • *(B::Number, A::Tridiagonal{T}) at linalg/tridiag.jl:407
  • *(x::Number, A::UpperTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:464
  • *(x::Number, A::Base.LinAlg.UnitUpperTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:467
  • *(x::Number, A::LowerTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:464
  • *(x::Number, A::Base.LinAlg.UnitLowerTriangular{T,S<:AbstractArray{T,2}}) at linalg/triangular.jl:467
  • *(B::Number, A::Bidiagonal{T}) at linalg/bidiag.jl:193
  • *(A::Number, B::AbstractArray{T,N}) at abstractarraymath.jl:54
  • *(A::AbstractArray{T,N}, B::Number) at abstractarraymath.jl:55
  • *(s1::AbstractString, ss::AbstractString...) at strings/basic.jl:50
  • *(this::Base.Grisu.Float, other::Base.Grisu.Float) at grisu/float.jl:138
  • *(index::CartesianIndex{N}, a::Integer) at multidimensional.jl:54
  • *{T,S}(A::AbstractArray{T,2}, B::Union{DenseArray{S,2},SubArray{S,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at linalg/matmul.jl:131
  • *{T,S}(A::AbstractArray{T,2}, x::AbstractArray{S,1}) at linalg/matmul.jl:86
  • *(A::AbstractArray{T,1}, B::AbstractArray{T,2}) at linalg/matmul.jl:89
  • *(J1::UniformScaling{T<:Number}, J2::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:121
  • *(J::UniformScaling{T<:Number}, B::BitArray{2}) at linalg/uniformscaling.jl:123
  • *(A::AbstractArray{T,2}, J::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:124
  • *{Tv,Ti}(J::UniformScaling{T<:Number}, S::SparseMatrixCSC{Tv,Ti}) at sparse/linalg.jl:24
  • *(J::UniformScaling{T<:Number}, A::Union{AbstractArray{T,1},AbstractArray{T,2}}) at linalg/uniformscaling.jl:125
  • *(x::Number, J::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:127
  • *(J::UniformScaling{T<:Number}, x::Number) at linalg/uniformscaling.jl:128
  • *{T,S}(R::Base.LinAlg.AbstractRotation{T}, A::Union{AbstractArray{S,1},AbstractArray{S,2}}) at linalg/givens.jl:9
  • *{T}(G1::Base.LinAlg.Givens{T}, G2::Base.LinAlg.Givens{T}) at linalg/givens.jl:307
  • *(p::Base.DFT.ScaledPlan{T,P,N}, x::AbstractArray{T,N}) at dft.jl:262
  • *{T,K,N}(p::Base.DFT.FFTW.cFFTWPlan{T,K,false,N}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:621
  • *{T,K}(p::Base.DFT.FFTW.cFFTWPlan{T,K,true,N}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:628
  • *{N}(p::Base.DFT.FFTW.rFFTWPlan{Float32,-1,false,N}, x::Union{DenseArray{Float32,N},SubArray{Float32,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:698
  • *{N}(p::Base.DFT.FFTW.rFFTWPlan{Complex{Float32},1,false,N}, x::Union{DenseArray{Complex{Float32},N},SubArray{Complex{Float32},N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:705
  • *{N}(p::Base.DFT.FFTW.rFFTWPlan{Float64,-1,false,N}, x::Union{DenseArray{Float64,N},SubArray{Float64,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:698
  • *{N}(p::Base.DFT.FFTW.rFFTWPlan{Complex{Float64},1,false,N}, x::Union{DenseArray{Complex{Float64},N},SubArray{Complex{Float64},N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:705
  • *{T,K,N}(p::Base.DFT.FFTW.r2rFFTWPlan{T,K,false,N}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:866
  • *{T,K}(p::Base.DFT.FFTW.r2rFFTWPlan{T,K,true,N}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/FFTW.jl:873
  • *{T}(p::Base.DFT.FFTW.DCTPlan{T,5,false}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/dct.jl:188
  • *{T}(p::Base.DFT.FFTW.DCTPlan{T,4,false}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/dct.jl:191
  • *{T,K}(p::Base.DFT.FFTW.DCTPlan{T,K,true}, x::Union{DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}) at fft/dct.jl:194
  • *{T}(p::Base.DFT.Plan{T}, x::AbstractArray{T,N}) at dft.jl:221
  • *(α::Number, p::Base.DFT.Plan{T}) at dft.jl:264
  • *(p::Base.DFT.Plan{T}, α::Number) at dft.jl:265
  • *(I::UniformScaling{T<:Number}, p::Base.DFT.ScaledPlan{T,P,N}) at dft.jl:266
  • *(p::Base.DFT.ScaledPlan{T,P,N}, I::UniformScaling{T<:Number}) at dft.jl:267
  • *(I::UniformScaling{T<:Number}, p::Base.DFT.Plan{T}) at dft.jl:268
  • *(p::Base.DFT.Plan{T}, I::UniformScaling{T<:Number}) at dft.jl:269
  • *{P<:Base.Dates.Period}(x::P<:Base.Dates.Period, y::Real) at dates/periods.jl:54
  • *(a, b, c, xs...) at operators.jl:103

In [108]:
N=[5 6 ; 9 8]


Out[108]:
2x2 Array{Int64,2}:
 5  6
 9  8

In [109]:
M*N


Out[109]:
2x2 Array{Int64,2}:
 23  22
 51  50

In [110]:
@which M*v  # this tells me which exact function this uses


Out[110]:
*{T,S}(A::AbstractArray{T,2}, x::AbstractArray{S,1}) at linalg/matmul.jl:86

This @which is a macro. It takes an expression and generates an expression.

Random Numbers

The Merenne Twister psedorandom number generator is built-in Julia:


In [111]:
rand()  # Uniform in [0,1]


Out[111]:
0.7841025437188396

In [112]:
x = rand(5)


Out[112]:
5-element Array{Float64,1}:
 0.84881  
 0.0866927
 0.805384 
 0.478686 
 0.312604 

In [113]:
y = rand(5,5)


Out[113]:
5x5 Array{Float64,2}:
 0.624748  0.162645  0.740703  0.893249  0.29021  
 0.231746  0.865314  0.93289   0.921141  0.307996 
 0.384408  0.997412  0.341226  0.344836  0.0561415
 0.333108  0.185401  0.492294  0.338638  0.26685  
 0.693835  0.161323  0.764011  0.6898    0.170772 

For more distributions look at the Distributions.jl

Linear Algebra

Julia has built-in linear algebra (not only using LAPACK), but also generic routines that work for arbitrary element types, implemented completely in julia.


In [114]:
M = rand(100,100)


Out[114]:
100x100 Array{Float64,2}:
 0.0611581  0.904245   0.275913    …  0.556664   0.951898   0.937662 
 0.59785    0.755552   0.131479       0.284083   0.774629   0.26735  
 0.420103   0.58191    0.203083       0.386336   0.513552   0.159579 
 0.271309   0.475963   0.714795       0.609815   0.147643   0.0168119
 0.935174   0.370628   0.316897       0.783191   0.850693   0.851187 
 0.241464   0.907858   0.221063    …  0.569139   0.187049   0.26113  
 0.16229    0.536025   0.176873       0.951571   0.747292   0.109015 
 0.0649966  0.104308   0.668715       0.124069   0.313518   0.818385 
 0.921662   0.696362   0.0681541      0.0569162  0.277893   0.702379 
 0.80262    0.899781   0.0852833      0.953898   0.442645   0.263261 
 0.579838   0.926698   0.0351352   …  0.860054   0.0500081  0.147671 
 0.0703547  0.571836   0.215589       0.694193   0.511944   0.111797 
 0.78973    0.442169   0.178119       0.396613   0.189031   0.512302 
 ⋮                                 ⋱                                 
 0.999535   0.0734737  0.764136       0.573434   0.949589   0.12591  
 0.376184   0.915975   0.0398319      0.116594   0.303362   0.348186 
 0.519221   0.688696   0.843665    …  0.609528   0.792864   0.22388  
 0.440535   0.480726   0.401966       0.266842   0.865672   0.151348 
 0.28378    0.401597   0.901768       0.644392   0.289342   0.236365 
 0.913935   0.628486   0.653985       0.259573   0.319886   0.621647 
 0.77206    0.2949     0.156254       0.4577     0.193732   0.549314 
 0.0446112  0.363719   0.862403    …  0.685418   0.229468   0.804283 
 0.714884   0.102264   0.253281       0.208284   0.216289   0.0561974
 0.591764   0.668868   0.00401626     0.632378   0.531655   0.511602 
 0.108534   0.817408   0.367706       0.802974   0.690581   0.811402 
 0.527828   0.826553   0.233843       0.994336   0.968919   0.26693  

Eigenvectors and eigenvalues


In [115]:
eig(M) # eigenvalues and eigenvectors


Out[115]:
(Complex{Float64}[49.8357+0.0im,2.90966+0.0im,0.248617+2.83393im,0.248617-2.83393im,-0.879022+2.65027im,-0.879022-2.65027im,-2.22364+1.71452im,-2.22364-1.71452im,-2.45893+1.33873im,-2.45893-1.33873im  …  0.32299+0.787255im,0.32299-0.787255im,-0.563209+0.315835im,-0.563209-0.315835im,0.174111+0.36164im,0.174111-0.36164im,0.489603+0.14658im,0.489603-0.14658im,-0.441628+0.0im,0.848656+0.0im],
100x100 Array{Complex{Float64},2}:
 -0.0998289+0.0im      0.10788+0.0im  …     0.089037+0.0im
  -0.102965+0.0im   -0.0350871+0.0im     -0.00675766+0.0im
 -0.0994849+0.0im  -0.00515313+0.0im        0.131089+0.0im
  -0.100647+0.0im    0.0223583+0.0im      -0.0687176+0.0im
 -0.0993443+0.0im    0.0381943+0.0im      -0.0459937+0.0im
  -0.104464+0.0im   -0.0528916+0.0im  …    -0.132153+0.0im
  -0.104833+0.0im    -0.147718+0.0im       0.0307916+0.0im
 -0.0903114+0.0im   -0.0918884+0.0im        0.147257+0.0im
  -0.101632+0.0im    0.0539086+0.0im      -0.0979737+0.0im
  -0.105326+0.0im     0.210972+0.0im      -0.0951134+0.0im
 -0.0947107+0.0im   -0.0884658+0.0im  …    -0.190353+0.0im
  -0.108332+0.0im   -0.0184532+0.0im       -0.159369+0.0im
 -0.0962246+0.0im   0.00827873+0.0im      -0.0394502+0.0im
           ⋮                          ⋱                   
 -0.0920159+0.0im    -0.072428+0.0im        0.175052+0.0im
  -0.105125+0.0im  -0.00189462+0.0im       -0.177111+0.0im
 -0.0983563+0.0im    0.0286211+0.0im  …    -0.126693+0.0im
 -0.0965508+0.0im     0.114305+0.0im        0.103804+0.0im
 -0.0989609+0.0im   -0.0176114+0.0im       -0.124077+0.0im
  -0.106965+0.0im   -0.0482338+0.0im        0.137023+0.0im
  -0.101762+0.0im    0.0511526+0.0im       0.0757546+0.0im
  -0.101835+0.0im    0.0317312+0.0im  …    0.0191646+0.0im
  -0.106029+0.0im   -0.0225314+0.0im       -0.153913+0.0im
  -0.100524+0.0im    0.0111696+0.0im       0.0340394+0.0im
  -0.100881+0.0im    0.0173754+0.0im      -0.0566414+0.0im
  -0.101361+0.0im   -0.0271371+0.0im      -0.0545172+0.0im)

In [116]:
typeof(ans)


Out[116]:
Tuple{Array{Complex{Float64},1},Array{Complex{Float64},2}}

LU decomposition


In [117]:
M2 = map(big, M)  # `map` uses the function `big` over all elements of M


Out[117]:
100x100 Array{BigFloat,2}:
 6.115808062230976283046857133740559220314025878906250000000000000000000000000000e-02  …  9.376623527644252220625276095233857631683349609375000000000000000000000000000000e-01
 5.978502087972297207585370415472425520420074462890625000000000000000000000000000e-01     2.673503863598241103716190991690382361412048339843750000000000000000000000000000e-01
 4.201028116926996958113704749848693609237670898437500000000000000000000000000000e-01     1.595790089432362890420336043462157249450683593750000000000000000000000000000000e-01
 2.713091427069611594191655967733822762966156005859375000000000000000000000000000e-01     1.681192061245462276986017968738451600074768066406250000000000000000000000000000e-02
 9.351743948161701514010246683028526604175567626953125000000000000000000000000000e-01     8.511872837233238620058273227186873555183410644531250000000000000000000000000000e-01
 2.414637537877426520793733288883231580257415771484375000000000000000000000000000e-01  …  2.611303988774291706675967361661605536937713623046875000000000000000000000000000e-01
 1.622899182822674646331506664864718914031982421875000000000000000000000000000000e-01     1.090152472317698073567271421779878437519073486328125000000000000000000000000000e-01
 6.499660066795720503307620674604550004005432128906250000000000000000000000000000e-02     8.183849154325650854246987364604137837886810302734375000000000000000000000000000e-01
 9.216616126418222254557122141704894602298736572265625000000000000000000000000000e-01     7.023786072623876552967203679145313799381256103515625000000000000000000000000000e-01
 8.026204043247175956565797605435363948345184326171875000000000000000000000000000e-01     2.632605105947181112213684173184446990489959716796875000000000000000000000000000e-01
 5.798382836554583086297043337253853678703308105468750000000000000000000000000000e-01  …  1.476712500840955399894482980016618967056274414062500000000000000000000000000000e-01
 7.035472868759229392310317052761092782020568847656250000000000000000000000000000e-02     1.117972372223627886000940634403377771377563476562500000000000000000000000000000e-01
 7.897299987550796540602959794341586530208587646484375000000000000000000000000000e-01     5.123020293654978463138149891165085136890411376953125000000000000000000000000000e-01
 ⋮                                                                                     ⋱                                                                                      
 9.995348931023364524861563040758483111858367919921875000000000000000000000000000e-01     1.259097277030933881292185105849057435989379882812500000000000000000000000000000e-01
 3.761836325985226103796321694971993565559387207031250000000000000000000000000000e-01     3.481857340362595198968165277619846165180206298828125000000000000000000000000000e-01
 5.192207208380184013662983488757163286209106445312500000000000000000000000000000e-01  …  2.238799546664242967608515755273401737213134765625000000000000000000000000000000e-01
 4.405354602116386963928107434185221791267395019531250000000000000000000000000000e-01     1.513478785598909581722182338126003742218017578125000000000000000000000000000000e-01
 2.837803032391004176560045380028896033763885498046875000000000000000000000000000e-01     2.363646409063002717942936214967630803585052490234375000000000000000000000000000e-01
 9.139345331425443585970924686989746987819671630859375000000000000000000000000000e-01     6.216465282289165283913234816282056272029876708984375000000000000000000000000000e-01
 7.720604944970617111721367109566926956176757812500000000000000000000000000000000e-01     5.493138723559536362728294989210553467273712158203125000000000000000000000000000e-01
 4.461123917962495077915718866279348731040954589843750000000000000000000000000000e-02  …  8.042827872943887346224300927133299410343170166015625000000000000000000000000000e-01
 7.148842894190354790850960853276774287223815917968750000000000000000000000000000e-01     5.619739064974393905060878751100972294807434082031250000000000000000000000000000e-02
 5.917637115109319889683092696941457688808441162109375000000000000000000000000000e-01     5.116017645569626015600306345731951296329498291015625000000000000000000000000000e-01
 1.085336576293531862802410614676773548126220703125000000000000000000000000000000e-01     8.114022089366639267637992816162295639514923095703125000000000000000000000000000e-01
 5.278281383028802231649478926556184887886047363281250000000000000000000000000000e-01     2.669300811110786320057286502560600638389587402343750000000000000000000000000000e-01

In [118]:
lu(M2)


Out[118]:
(
100x100 Array{BigFloat,2}:
 1.000000000000000000000000000000000000000000000000000000000000000000000000000000      …  0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 1.800146447484079042541970541475082791051197106667219161860169842578187081327177e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 1.128707961617959434069532870275722856137696964027089927664182750446927450148403e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 4.374738066325009975734123932672522614545948701057058400103882198869464452559705e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 7.595778591079215366992013504896503602992446475515040168258250750912344734720548e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 5.788831137422141845089200725376527767194929429608127663204699269939184081286744e-02  …  0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 3.763586796164077468032075344615964868248177010328126564348240339719759041858167e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 4.463199782967203572541886748784290226593359936543047503230111885077249382796001e-02     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 5.981284024428943892019977772313485925437402638790989504051629840823680578329472e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 2.415761125039789978800865457678562747480295542977094817984824823404454810635833e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 2.890777782905160828053591546159698053213175817841461313030025005211685742441571e-01  …  0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 9.456860510420358207426047685244332578300764221811963241872759770943863363172581e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 8.029938823181654053292291015494195671915295413110824269415463059766954069139104e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 ⋮                                                                                     ⋱                                                                                  
 5.140299456399287628656515170892097005488975040660494217358477880511295394295008e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 2.714353891787381799900949650966649121198914479363774100469069362823054198942885e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 4.280152161819265083338011738161167500182288798821683969282972983646503073375033e-01  …  0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 6.502684510214751291224371695186991032333888368424397098492997147422449470940447e-02     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 2.085652272062060786802799136067275841816438882828254244001303542741047590777671e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 8.448992361211265469588934773206632502863037218833575647927632059550567418005332e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 9.289879087831435823618131483427815227772168043407478406385107286679819565288098e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 6.410822042710107321701860060951749414052810805329354989782823690484609913344373e-01  …  0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 9.143598081962827680197479465910833827051662879996430169622468777004750042374703e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 5.711588364628940944047039745812120181244390269979896528313738400095065603058531e-01     0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 8.20892892982065040652515370858817366606856000168554333362593312304392221499785e-01      0.000000000000000000000000000000000000000000000000000000000000000000000000000000
 2.899913752077832072613440571761596039983144358856519351949718483198017933617466e-01     1.000000000000000000000000000000000000000000000000000000000000000000000000000000,

100x100 Array{BigFloat,2}:
 9.995348931023364524861563040758483111858367919921875000000000000000000000000000e-01  …   1.259097277030933881292185105849057435989379882812500000000000000000000000000000e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          9.378559321083142111046997458447877923601393254432736859538740581789111117112245e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          5.299839837962289244648208871362797652969078665113839725939507667188968121362174e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          5.711517860190831735276131434904585490794968562087797648164335617609912666845839e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -1.167851734446601302886057977078432446210856976288501145139793157356531695918708    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000      …  -5.635700567398709124445798191866355128104827146931461455541608224645507952183557e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -4.869857087674871962208532804135616025328826110325667029407429251148609743960385e-02
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -1.541021244185673579480683164781420664138984539659692620087548993654491425721492e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -3.016602681783115909848655777785104976096850231596537854756291863730927729679315e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -1.069459478430411865237708781481252589645552555752201958968905086925180786050116    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000      …   1.552585347222031962521653831018308021951909504748705922429117221205127932814473    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          4.32780707466518042383207420829433492573045739635094975130118228326623105380506e-01 
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          6.360944839837167144783754415796334382699111111511191102573809644888219425272305e-02
 ⋮                                                                                     ⋱                                                                                       
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -8.770150850306949386626755882712114032719696085450949847276242186743324110425264e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -1.057306070917480548002585924566918272402311281156176331250177340042938525875951    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000      …   2.104225534488299678254831052299181762842054518570784817775958292372262358154962    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -7.829163991048855727342224595835545588140173483962429396663828803837051793767603e-02
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          1.008934997798198973030674434108785210921954666329651159674412003800204686421817    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          5.595785807906596690149668203843817365114632971402007007389488180239800896794531e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -6.355571021490289524447702438936373176534459119071441098866567838949685983131974e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000      …   2.242669053858525298202266354361874500451168239943147424803804109911655653090542    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -2.871563681296800047751377577788957500620066123792913567485109118333432267429456    
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          3.769579843405684130286454072396275069795058878673081171552777650133139169730295e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000         -9.444845298876019023566946596147472077121174788276641626297339982541970394125655e-01
 0.000000000000000000000000000000000000000000000000000000000000000000000000000000          6.31491793240865477629269725906997346494817344792850250527627230453212185943631e-01 ,

[89,75,51,14,73,76,90,96,2,6  …  62,8,28,30,36,49,94,21,44,72])

In [119]:
methods(lu)


Out[119]:
4 methods for generic function lu:

In [120]:
@edit lu(M)  # this is to open a text editor with the file


Unknown editor: no line number information passed.
The method is defined at line 93.

Interacting with the System

Command-line arguments

A julia script similar to Python is a sequence of commands placed on file with the suffix .jl.

From command-line, a script script.jl is ran as:

$ julia script.jl arg1 arg2

where arg1, arg2 are the command line arguments.

The command line arguments are placed in the variable ARGS as an array of strings.


Files

Simple file input and output is easy:


In [121]:
outfile = open("test.txt", "w")


Out[121]:
IOStream(<file test.txt>)

In [122]:
for i in 1:10
    println(outfile, "The value of i is $i") #send printl to file stream
end
close(outfile)

In [123]:
;cat test.txt # this is a shell command


/bin/bash: -c: line 1: syntax error: unexpected end of file


In [124]:
infile = open("test.txt","r")


Out[124]:
IOStream(<file test.txt>)

readlines()


In [125]:
lines = readlines(infile)


Out[125]:
10-element Array{ByteString,1}:
 "The value of i is 1\n" 
 "The value of i is 2\n" 
 "The value of i is 3\n" 
 "The value of i is 4\n" 
 "The value of i is 5\n" 
 "The value of i is 6\n" 
 "The value of i is 7\n" 
 "The value of i is 8\n" 
 "The value of i is 9\n" 
 "The value of i is 10\n"

split()


In [126]:
map(split, lines)


Out[126]:
10-element Array{Array{SubString{ASCIIString},1},1}:
 SubString{ASCIIString}["The","value","of","i","is","1"] 
 SubString{ASCIIString}["The","value","of","i","is","2"] 
 SubString{ASCIIString}["The","value","of","i","is","3"] 
 SubString{ASCIIString}["The","value","of","i","is","4"] 
 SubString{ASCIIString}["The","value","of","i","is","5"] 
 SubString{ASCIIString}["The","value","of","i","is","6"] 
 SubString{ASCIIString}["The","value","of","i","is","7"] 
 SubString{ASCIIString}["The","value","of","i","is","8"] 
 SubString{ASCIIString}["The","value","of","i","is","9"] 
 SubString{ASCIIString}["The","value","of","i","is","10"]

In [127]:
[float(line[6]) for line in map(split, lines)]


Out[127]:
10-element Array{Any,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0

writedlm : Write a collection (matrix, array, etc) in DeLiMited file

writedlm(f, A, delim='\\t')

Writes iterable A to file f using the delim :


In [128]:
x = rand(5)


Out[128]:
5-element Array{Float64,1}:
 0.0970615
 0.510425 
 0.862362 
 0.219442 
 0.850949 

In [129]:
writedlm("rand5.txt", x)

In [130]:
;cat rand5.txt


.09706147378829999
.5104252661912045
.8623623472283906
.21944197411861022
.8509490780384401

In [131]:
y = rand(5,5)


Out[131]:
5x5 Array{Float64,2}:
 0.830001  0.663191   0.790464  0.153078   0.590352 
 0.933645  0.311412   0.689223  0.0879068  0.0186356
 0.806717  0.0576021  0.16125   0.242121   0.864999 
 0.558308  0.550209   0.881796  0.611303   0.481788 
 0.67582   0.0578835  0.190116  0.398881   0.311417 

In [132]:
writedlm("rand55.txt",y)

In [133]:
;cat rand55.txt


.8300005280221432	.6631912513940934	.7904636308465385	.15307761406213616	.5903519503298555
.9336446699140641	.31141215120802523	.6892230570951388	.08790677404515757	.018635601691114445
.8067174472405798	.05760209841927755	.16124972955873096	.24212119904193186	.8649992908372628
.5583084136026759	.550209195685528	.8817959765219163	.611303299934693	.48178839023148634
.6758201873732483	.05788347229224544	.19011569965949593	.3988814226215982	.3114167575513578

readdlm : Read column file

Use readdlm to read a matrix from the source where each line (separated by eol) gives one row, with elements separated by the given delimeter. The source can be a text file, stream or byte array. Memory mapped files can be used by passing the byte array representation of the mapped segment as source.


In [134]:
z = readdlm("rand55.txt")


Out[134]:
5x5 Array{Float64,2}:
 0.830001  0.663191   0.790464  0.153078   0.590352 
 0.933645  0.311412   0.689223  0.0879068  0.0186356
 0.806717  0.0576021  0.16125   0.242121   0.864999 
 0.558308  0.550209   0.881796  0.611303   0.481788 
 0.67582   0.0578835  0.190116  0.398881   0.311417 

In [135]:
inv(z)


Out[135]:
5x5 Array{Float64,2}:
  0.700153   0.300242  -0.457812  -1.14445    1.69695
  4.51337   -2.86306   -3.34153   -2.0863     4.12453
 -2.95888    2.48253    2.22855    2.39455   -4.43407
 -0.149894  -1.08179   -1.13492    0.649068   2.49712
 -0.359994  -0.249335   1.70779    0.578212  -1.72966

Interacting with the shell

Either use the ; in the begining of the line:


In [136]:
;ls


Introduction_to_Julia_Programming.ipynb
Introduction_to_SymPy.ipynb
LICENCE
Matplotlib_tutorial.ipynb
README.md
R_Notes.ipynb
Rootpy Ratio Plots.ipynb
Tutorial_Plotting_with_Matplotlib.ipynb
csv_datasets
decision_tree_in_R.ipynb
intro_to_ggplot2.ipynb
ipythonWidgets.ipynb
numpy.ipynb
pandas_data_munging.ipynb
rand5.txt
rand55.txt
sympy1.ipynb
test.csv
test.txt
or use the `command` and then run()

In [137]:
cmd=`ls -lt`


Out[137]:
`ls -lt`

In [138]:
run(cmd)


total 23000
-rw-r--r--  1 nkarast  staff       92 21 Jun 02:06 rand5.txt
-rw-r--r--  1 nkarast  staff      459 21 Jun 02:06 rand55.txt
-rw-r--r--@ 1 nkarast  staff      201 21 Jun 02:06 test.txt
-rw-r--r--  1 nkarast  staff  1753897 21 Jun 02:05 Introduction_to_Julia_Programming.ipynb
-rw-r--r--  1 nkarast  staff    53588 20 Jun 18:14 Tutorial_Plotting_with_Matplotlib.ipynb
-rw-r--r--  1 nkarast  staff   106596 17 Jun 11:45 Introduction_to_SymPy.ipynb
-rw-r--r--  1 nkarast  staff    18004 16 Jun 11:39 sympy1.ipynb
-rw-r--r--  1 nkarast  staff    63113  7 Jun 13:18 ipythonWidgets.ipynb
-rw-r--r--  1 nkarast  staff    40545  7 Jun 12:58 numpy.ipynb
-rw-r--r--  1 nkarast  staff    96778  6 Jun 20:23 Matplotlib_tutorial.ipynb
-rw-r--r--  1 nkarast  staff       71  6 Jun 13:06 test.csv
-rw-r--r--  1 nkarast  staff   104376 20 Jan 11:44 R_Notes.ipynb
-rw-r--r--  1 nkarast  staff  1086723 10 Nov  2015 pandas_data_munging.ipynb
drwxr-xr-x  5 nkarast  staff      170 10 Nov  2015 csv_datasets
-rw-r--r--  1 nkarast  staff  4260668  4 Nov  2015 decision_tree_in_R.ipynb
-rw-r--r--  1 nkarast  staff  4050911  3 Nov  2015 intro_to_ggplot2.ipynb
-rw-r--r--  1 nkarast  staff     1084 17 Jul  2015 LICENCE
-rw-r--r--  1 nkarast  staff       38 11 Jul  2015 README.md
-rw-r--r--  1 nkarast  staff    86623 11 Jul  2015 Rootpy Ratio Plots.ipynb

Scientific Computing

There are specialized function for

  • Linear algebra
  • FFT
  • Random numbers
  • Special functions
  • Packages for optimization
  • ODE
  • etc...

Functions

Short function definition


In [139]:
quad2(x) = x^2


Out[139]:
quad2 (generic function with 1 method)

In [140]:
quad2(2)


Out[140]:
4

Longer function definition


In [141]:
function quad(x)
    x^2 # no explicit return needed
end


Out[141]:
quad (generic function with 1 method)

The last value that is computed within the function is automatically returned, so no explicit return statement is needed.


In [142]:
quad(2)


Out[142]:
4

Since every operator in Julia is a function and functions are implemented by specifying their action on different types something like this can work:


In [143]:
quad(2), quad(2.5), quad(1+3im), quad("hello")


Out[143]:
(4,6.25,-8 + 6im,"hellohello")

NB: String concat in Julia is done with the * operator and not the + operator. Repeating a string is thus done by raising it to an integer power.

So multiplying a string with a number will not work, but rainsing it to an integer power (i.e. multiplying it p times) will..


In [144]:
2*"hello"


LoadError: MethodError: `*` has no method matching *(::Int64, ::ASCIIString)
Closest candidates are:
  *(::Any, ::Any, !Matched::Any, !Matched::Any...)
  *(::Real, !Matched::Complex{Bool})
  *(::Real, !Matched::Complex{T<:Real})
  ...
while loading In[144], in expression starting on line 1

In [145]:
"hello"^2


Out[145]:
"hellohello"

In [146]:
s1 = "hello"
s2 = "Nikos"


Out[146]:
"Nikos"

In [147]:
s1+s2


LoadError: MethodError: `+` has no method matching +(::ASCIIString, ::ASCIIString)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...)
while loading In[147], in expression starting on line 1

In [148]:
s1*s2


Out[148]:
"helloNikos"

But we can define the + operator to work with string concat:


In [149]:
+(s1::String, s2::String) = string(s1, s2)


WARNING: module Main should explicitly import + from Base
WARNING: Base.String is deprecated, use AbstractString instead.
  likely near In[149]:1
WARNING: Base.String is deprecated, use AbstractString instead.
  likely near In[149]:1
Out[149]:
+ (generic function with 172 methods)

In [150]:
"First"+" second"


Out[150]:
"First second"

In [151]:
"Value is " + 3 # this is not yet defined for my +


LoadError: MethodError: `+` has no method matching +(::ASCIIString, ::Int64)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...)
  +(!Matched::Int64, ::Int64)
  +(!Matched::Complex{Bool}, ::Real)
  ...
while loading In[151], in expression starting on line 1

User Defined Types

A user-defined "composite type" is a colleciton of data. Unlike Python types do not "own" methods (functions internal to the type).

Methods are defined separately and are characterized by the types of all their arguments : multiple dispatch (dispatch is the process of choosing which "version" of a function to use).

For example, let's define a 2D vector type.


In [152]:
immutable Vector2D  # we could also use type instead of immutable
    x::Float64
    y::Float64
end

We use the immutable instead of type for efficiency: the object s stored in an efficient packed form.


In [153]:
Vector2D(3.0, 2.0)


Out[153]:
Vector2D(3.0,2.0)

We then to define the operators ... For example for vector addition:


In [154]:
+(v::Vector2D, w::Vector2D) = Vector2D(v.x+w.x, v.y+w.y)


WARNING: module Main should explicitly import + from Base
Out[154]:
+ (generic function with 173 methods)

In [155]:
v = Vector2D(3, 2)


Out[155]:
Vector2D(3.0,2.0)

In [156]:
u = Vector2D(1,3)


Out[156]:
Vector2D(1.0,3.0)

In [157]:
u+v


Out[157]:
Vector2D(4.0,5.0)

Change how Julia returns (shows) your new type

To change how is it shown, dispatch the Base.show command:


In [158]:
import Base.show

In [159]:
show(io::IO, v::Vector2D) = print(io, "[$(v.x), $(v.y)]")


Out[159]:
show (generic function with 106 methods)

In [160]:
v


Out[160]:
[3.0, 2.0]

Parametrised types (templates)

Types may have a parameter, for exampel:


In [161]:
immutable VectorND{T <: Real}
    x::T
    y::T
end

T is a type parameter.

T <: Real means that T must be a subtype of the abstract type Real.

We can investigate the hierarchy with the super() function


In [162]:
super(Real)


Out[162]:
Number

In [163]:
v = VectorND(3,4)


Out[163]:
VectorND{Int64}(3,4)

In [164]:
n = VectorND(3+1im, 2) # this does not work since VectorND works only
                       # with Real values


LoadError: MethodError: `convert` has no method matching convert(::Type{VectorND{T<:Real}}, ::Complex{Int64}, ::Int64)
This may have arisen from a call to the constructor VectorND{T<:Real}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  VectorND{T<:Real}(!Matched::T<:Real, ::T<:Real)
  call{T}(::Type{T}, ::Any)
  convert{T}(::Type{T}, !Matched::T)
while loading In[164], in expression starting on line 1

 in call at essentials.jl:57

Example: Simple type for a collection of particles


In [165]:
type Particle
    position::Vector2D
    momentum::Vector2D
end

In [166]:
move(p::Particle, dt::Real) = p.position += p.momentum*dt


Out[166]:
move (generic function with 1 method)

In [167]:
show(io::IO, p::Particle) = print(io, "pos:$(p.position); vel: $(p.momentum)")


Out[167]:
show (generic function with 107 methods)

In [168]:
+(v1::Vector2D, v2::Vector2D) = Vector2D(v1.x+v2.x, v1.y+v2.y)


WARNING: module Main should explicitly import + from Base
Out[168]:
+ (generic function with 173 methods)

In [169]:
*(v1::Vector2D, lamb::Number) = Vector2D(lamb*v.x, lamb*v.y)


WARNING: module Main should explicitly import * from Base
Out[169]:
* (generic function with 139 methods)

In [170]:
p = Particle(Vector2D(0,0), Vector2D(1,1))


Out[170]:
pos:[0.0, 0.0]; vel: [1.0, 1.0]

In [171]:
move(p, 0.1)


Out[171]:
[0.30000000000000004, 0.4]

Now we can define a gas as a collection of particles...


In [172]:
type Gas
    particles::Vector{Particle} # Array{Particle, 1}
    
    function Gas(N::Int) # constructor
        parts = [Particle(Vector2D(rand(2)...), Vector2D(rand(2)...)) for i in 1:N]
        new(parts)
    end
end

In [173]:
myg=Gas(10)


Out[173]:
Gas([pos:[0.09523281886335333, 0.4804268187214642]; vel: [0.5431659735087191, 0.3770590419308326],pos:[0.38631625672922487, 0.3082240878701721]; vel: [0.8057556698038195, 0.26204794658238395],pos:[0.09908572297877472, 0.34035917861194753]; vel: [0.43295276164044005, 0.39415469688443916],pos:[0.6228011113390532, 0.7422167996962561]; vel: [0.4924214677378167, 0.867984225574421],pos:[0.6072463241826187, 0.1290631925212835]; vel: [0.18108548332236984, 0.06994168016481095],pos:[0.5938965354591961, 0.4270429953345578]; vel: [0.5556479052228109, 0.6558989414042535],pos:[0.547158674488931, 0.6119483568935484]; vel: [0.25081444855563695, 0.12875207161992064],pos:[0.4391667216217672, 0.26745120834269653]; vel: [0.7073479784580712, 0.5737583453604922],pos:[0.4010905646112264, 0.7166046856788153]; vel: [0.344148375928115, 0.9918159225437446],pos:[0.30682829896010877, 0.6091346041986818]; vel: [0.031951454991402395, 0.4507325339072119]])

In [174]:
function move(g::Gas, dt::Number)
    for particle in g.particles
        move(particle, dt)
    end
end


Out[174]:
move (generic function with 2 methods)

In [175]:
move(myg, 2)

In [176]:
myg


Out[176]:
Gas([pos:[6.095232818863353, 8.480426818721464]; vel: [0.5431659735087191, 0.3770590419308326],pos:[6.386316256729225, 8.308224087870173]; vel: [0.8057556698038195, 0.26204794658238395],pos:[6.099085722978774, 8.340359178611948]; vel: [0.43295276164044005, 0.39415469688443916],pos:[6.6228011113390535, 8.742216799696257]; vel: [0.4924214677378167, 0.867984225574421],pos:[6.6072463241826185, 8.129063192521283]; vel: [0.18108548332236984, 0.06994168016481095],pos:[6.593896535459196, 8.427042995334558]; vel: [0.5556479052228109, 0.6558989414042535],pos:[6.547158674488931, 8.61194835689355]; vel: [0.25081444855563695, 0.12875207161992064],pos:[6.439166721621767, 8.267451208342697]; vel: [0.7073479784580712, 0.5737583453604922],pos:[6.401090564611226, 8.716604685678815]; vel: [0.344148375928115, 0.9918159225437446],pos:[6.3068282989601085, 8.609134604198681]; vel: [0.031951454991402395, 0.4507325339072119]])

Graphics and Packages

Coming from Python the most accessible (and full-featured) package is PyPlot, the Julia wrapper for pyplot submodule of Matplotlib.

To get it:


In [177]:
Pkg.add("PyPlot")


INFO: Nothing to be done

Package Manager

Pkg is the package manager in Julia. It uses git repositories that are each cloned into their own subdirectory under the ~/.julia directory.

The list of packages is available at Julia Packages


Make a package accessible to your script

To make a package accessible use


In [178]:
using PyPlot

This is similar to the Python syntax:

from <package> import *

and it makes all names in the packages available.

Instead of using PyPlot one could use:

   import PyPlot

in this case the names of the package will be available as PyPlot.plot() etc

PyPlot()


In [179]:
x = rand(10); y = rand(10);

In [180]:
p = plot(x,y, "ro-")


Out[180]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x3110e4f10>

In [181]:
p


Out[181]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x3110e4f10>

In [182]:
figure(figsize=(4,2))


Out[182]:
PyPlot.Figure(PyObject <matplotlib.figure.Figure object at 0x31111fed0>)

In [183]:
plot(rand(10),rand(10))


Out[183]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x3280a4810>

Example: Eigenvalues of random matrices


In [184]:
L = 1000


Out[184]:
1000

In [185]:
diffs = []


Out[185]:
0-element Array{Any,1}

In [186]:
for i in 1:10
    M = randn(L, L)
    M = Symmetric(M)
    
    lamb = eigvals(M)
    diffs = [diffs, diff(lamb)]
end


WARNING: [a,b] concatenation is deprecated; use [a;b] instead
 in depwarn at deprecated.jl:73
 in oldstyle_vcat_warning at /Applications/Julia-0.4.5.app/Contents/Resources/julia/lib/julia/sys.dylib
 in vect at abstractarray.jl:32
 [inlined code] from In[186]:6
 in anonymous at no file:0
 in include_string at loading.jl:282
 in execute_request_0x535c5df2 at /Users/nkarast/.julia/v0.4/IJulia/src/execute_request.jl:183
 in eventloop at /Users/nkarast/.julia/v0.4/IJulia/src/IJulia.jl:143
 in anonymous at task.jl:447
while loading In[186], in expression starting on line 1

In [187]:
diffs


Out[187]:
9990-element Array{Any,1}:
 0.883611 
 0.0232049
 0.214189 
 1.04995  
 0.202477 
 0.129447 
 0.435875 
 0.279618 
 0.230483 
 0.430748 
 0.296591 
 0.256283 
 0.229787 
 ⋮        
 0.290359 
 0.224808 
 0.25521  
 0.109965 
 0.445321 
 0.423279 
 0.479745 
 0.190492 
 0.218793 
 0.281681 
 0.186808 
 0.484201 

In [188]:
h = plt[:hist](diffs, 300)   #weird syntax


Out[188]:
([28.0,54.0,72.0,95.0,124.0,143.0,181.0,199.0,216.0,200.0  …  0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0],[0.00193826,0.0065962,0.0112541,0.0159121,0.02057,0.0252279,0.0298859,0.0345438,0.0392017,0.0438597  …  1.3574,1.36206,1.36671,1.37137,1.37603,1.38069,1.38535,1.39,1.39466,1.39932],Any[PyObject <matplotlib.patches.Rectangle object at 0x310fd7a90>,PyObject <matplotlib.patches.Rectangle object at 0x310fd7f90>,PyObject <matplotlib.patches.Rectangle object at 0x310fe77d0>,PyObject <matplotlib.patches.Rectangle object at 0x310fe7e50>,PyObject <matplotlib.patches.Rectangle object at 0x310ff6510>,PyObject <matplotlib.patches.Rectangle object at 0x310ff6b90>,PyObject <matplotlib.patches.Rectangle object at 0x311004250>,PyObject <matplotlib.patches.Rectangle object at 0x3110048d0>,PyObject <matplotlib.patches.Rectangle object at 0x311004f50>,PyObject <matplotlib.patches.Rectangle object at 0x311011610>  …  PyObject <matplotlib.patches.Rectangle object at 0x32d8a3250>,PyObject <matplotlib.patches.Rectangle object at 0x32d8a38d0>,PyObject <matplotlib.patches.Rectangle object at 0x32d8a3f50>,PyObject <matplotlib.patches.Rectangle object at 0x32d8b0610>,PyObject <matplotlib.patches.Rectangle object at 0x32d8b0c90>,PyObject <matplotlib.patches.Rectangle object at 0x32d8be350>,PyObject <matplotlib.patches.Rectangle object at 0x32d8be9d0>,PyObject <matplotlib.patches.Rectangle object at 0x32d8beed0>,PyObject <matplotlib.patches.Rectangle object at 0x32d8cb710>,PyObject <matplotlib.patches.Rectangle object at 0x32d8cbd90>])

For more examples of plots using PyPlot check:


Gadfly

Gadfly is the native Julia plotting package, based on the Grammar of Graphics syntax, i.e. similar to R's ggplot package


In [200]:
using Gadfly


WARNING: using Gadfly.plot in module Main conflicts with an existing identifier.

In [201]:
xs =  1:10


Out[201]:
1:10

In [202]:
ys = rand(10)


Out[202]:
10-element Array{Float64,1}:
 0.188809
 0.173083
 0.804887
 0.178462
 0.429665
 0.289512
 0.256826
 0.309942
 0.206432
 0.332449

In [203]:
Gadfly.plot(x=xs, y=ys)


Out[203]:
x -12.5 -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0 22.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 -10 0 10 20 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 y

The plot can be panned and moved interactively.


In [204]:
Gadfly.plot(x=xs, y=ys, Geom.line)  # adding a Geometry


Out[204]:
x -12.5 -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0 22.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 -10 0 10 20 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 y

In [205]:
Gadfly.plot(x=xs, y=ys, Geom.line, Geom.point)


Out[205]:
x -12.5 -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0 22.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 -10 0 10 20 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 y

Preserve order

By default gadfly's plot orders the data in terms of the y axis. For example the random plot:


In [206]:
Gadfly.plot(x=rand(10), y=rand(10), Geom.line)


Out[206]:
x -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 y

... is not really random at all.

Thus to preserve the order we can use the argument preserve_order=true.


In [207]:
Gadfly.plot(x=rand(10), y=rand(10), Geom.point, Geom.line(preserve_order=true))


Out[207]:
x -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 -0.62 -0.60 -0.58 -0.56 -0.54 -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30 -0.28 -0.26 -0.24 -0.22 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06 -0.04 -0.02 0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22 0.24 0.26 0.28 0.30 0.32 0.34 0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 0.62 0.64 0.66 0.68 0.70 0.72 0.74 0.76 0.78 0.80 0.82 0.84 0.86 0.88 0.90 0.92 0.94 0.96 0.98 1.00 1.02 1.04 1.06 1.08 1.10 1.12 1.14 1.16 1.18 1.20 1.22 -1 0 1 2 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 y

In [208]:
p=Gadfly.plot(layer(x=rand(10), y=rand(10), Geom.point, Geom.line(preserve_order=true)), 
layer(x=rand(10), y=rand(10), Geom.point, Geom.line(preserve_order=true)), Guide.XLabel("first"), Guide.YLabel("second"))


Out[208]:
first -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 second

Guide is the driver for the information about the plot. Guide.XLabel, Guide.YLabel put the labels into axes.

We can also write directly to PDF:


In [209]:
#Gadfly.draw(PDF("stuff.pdf", 10cm, 5cm), p)  # this is fucked up atm

In [210]:
#;open stuff.pdf

DataFrames (Pandas for Julia!)

The DataFrames package is like the pandas!

One can also use RDataSets.

Gadfly has excellent integration with DataFrames!


In [211]:
using RDatasets

In [212]:
irises = dataset("datasets", "iris")


Out[212]:
SepalLengthSepalWidthPetalLengthPetalWidthSpecies
15.13.51.40.2setosa
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
65.43.91.70.4setosa
74.63.41.40.3setosa
85.03.41.50.2setosa
94.42.91.40.2setosa
104.93.11.50.1setosa
115.43.71.50.2setosa
124.83.41.60.2setosa
134.83.01.40.1setosa
144.33.01.10.1setosa
155.84.01.20.2setosa
165.74.41.50.4setosa
175.43.91.30.4setosa
185.13.51.40.3setosa
195.73.81.70.3setosa
205.13.81.50.3setosa
215.43.41.70.2setosa
225.13.71.50.4setosa
234.63.61.00.2setosa
245.13.31.70.5setosa
254.83.41.90.2setosa
265.03.01.60.2setosa
275.03.41.60.4setosa
285.23.51.50.2setosa
295.23.41.40.2setosa
304.73.21.60.2setosa
&vellip&vellip&vellip&vellip&vellip&vellip

In [213]:
head(irises)


Out[213]:
SepalLengthSepalWidthPetalLengthPetalWidthSpecies
15.13.51.40.2setosa
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
65.43.91.70.4setosa

In [214]:
Gadfly.plot(irises, x="SepalLength",y="SepalWidth", Geom.point)


Out[214]:
SepalLength -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0 10.2 10.4 10.6 10.8 11.0 11.2 11.4 11.6 11.8 12.0 0 5 10 15 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 -2.5 0.0 2.5 5.0 7.5 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 SepalWidth

In [216]:
Gadfly.plot(irises, x="SepalLength",y="SepalWidth", color="Species", Geom.point)


Out[216]:
SepalLength -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0 10.2 10.4 10.6 10.8 11.0 11.2 11.4 11.6 11.8 12.0 0 5 10 15 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 setosa versicolor virginica Species -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 -2.5 0.0 2.5 5.0 7.5 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 SepalWidth

Modules

To be added (look at SciPy 2014)


Performance

Performance Tips

Profiling

When profiling run each function once with the correct argument types before timing it, since the first time is run the compilation time will play a large role!


In [222]:
@time cos(10) # run it 2 times ;)


  0.000003 seconds (5 allocations: 176 bytes)
Out[222]:
-0.8390715290764524

Detailed profile can be done with the @profile macro


In [245]:
@profile cos(10)


Out[245]:
-0.8390715290764524

A package named ProfileView.jl gives you a graphical profiling.

Global variables: Don't use them!

Global variables are slow! The main program should be wrapped in a function. Any time you want to use globals, just send them as arguments to functions and return them if necessary.

If you have many variables to pass around, wrap them in a type, e.g. called State.

Type Stability

You should avoid having variables that are changing types! That's why Python is slow..


In [224]:
function sum1(N::Int)
    total = 0 # total is defined as Integer
    for i in 1:N
        total +=i/2
    end
    total
end


function sum2(N::Int)
    total = 0.0 # here total is defined as float from the start
    for i in 1:N
        total +=i/2
    end
    total
end


Out[224]:
sum2 (generic function with 1 method)

In [226]:
sum1(10), sum2(10)


Out[226]:
(27.5,27.5)

In [227]:
N=1000000


Out[227]:
1000000

In [229]:
@time sum1(N)


  0.047829 seconds (2.00 M allocations: 30.518 MB, 31.19% gc time)
Out[229]:
2.5000025e11

In [230]:
@time sum2(N)


  0.000942 seconds (5 allocations: 176 bytes)
Out[230]:
2.5000025e11

They produce the same result, but the time is different.

Exploring what happens beneath the hood

There are 4 functions that can access every step of the compilation process


In [235]:
code_lowered(sum1, (Int,)) # name of func, tuple of tupes of arguments


Out[235]:
1-element Array{Any,1}:
 :($(Expr(:lambda, Any[:N], Any[Any[Any[:N,:Any,0],Any[:total,:Any,2],Any[symbol("#s52"),:Any,2],Any[:i,:Any,18]],Any[],2,Any[]], :(begin  # In[224], line 2:
        total = 0 # In[224], line 3:
        GenSym(0) = (Main.colon)(1,N)
        #s52 = (top(start))(GenSym(0))
        unless (top(!))((top(done))(GenSym(0),#s52)) goto 1
        2: 
        GenSym(1) = (top(next))(GenSym(0),#s52)
        i = (top(getfield))(GenSym(1),1)
        #s52 = (top(getfield))(GenSym(1),2) # In[224], line 4:
        total = total + i / 2
        3: 
        unless (top(!))((top(!))((top(done))(GenSym(0),#s52))) goto 2
        1: 
        0:  # In[224], line 6:
        return total
    end))))

In [234]:
code_lowered(sum2, (Int,))


Out[234]:
1-element Array{Any,1}:
 :($(Expr(:lambda, Any[:N], Any[Any[Any[:N,:Any,0],Any[:total,:Any,2],Any[symbol("#s52"),:Any,2],Any[:i,:Any,18]],Any[],2,Any[]], :(begin  # In[224], line 11:
        total = 0.0 # In[224], line 12:
        GenSym(0) = (Main.colon)(1,N)
        #s52 = (top(start))(GenSym(0))
        unless (top(!))((top(done))(GenSym(0),#s52)) goto 1
        2: 
        GenSym(1) = (top(next))(GenSym(0),#s52)
        i = (top(getfield))(GenSym(1),1)
        #s52 = (top(getfield))(GenSym(1),2) # In[224], line 13:
        total = total + i / 2
        3: 
        unless (top(!))((top(!))((top(done))(GenSym(0),#s52))) goto 2
        1: 
        0:  # In[224], line 15:
        return total
    end))))

Due to the type stability the two code_lowered are different


In [238]:
code_typed(sum1, (Int,)) # check the Union in the result


Out[238]:
1-element Array{Any,1}:
 :($(Expr(:lambda, Any[:N], Any[Any[Any[:N,Int64,0],Any[:total,Any,2],Any[symbol("#s52"),Int64,2],Any[:i,Int64,18]],Any[],Any[UnitRange{Int64},Tuple{Int64,Int64},Int64,Int64],Any[]], :(begin  # In[224], line 2:
        total = 0 # In[224], line 3:
        GenSym(0) = $(Expr(:new, UnitRange{Int64}, 1, :(((top(getfield))(Base.Intrinsics,:select_value)::I)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64)))
        #s52 = (top(getfield))(GenSym(0),:start)::Int64
        unless (Base.box)(Base.Bool,(Base.not_int)(#s52::Int64 === (Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool)) goto 1
        2: 
        GenSym(2) = #s52::Int64
        GenSym(3) = (Base.box)(Base.Int,(Base.add_int)(#s52::Int64,1))
        i = GenSym(2)
        #s52 = GenSym(3) # In[224], line 4:
        total = total::Union{Float64,Int64} + (Base.box)(Base.Float64,(Base.div_float)((Base.box)(Float64,(Base.sitofp)(Float64,i::Int64)),(Base.box)(Float64,(Base.sitofp)(Float64,2))))::Float64
        3: 
        unless (Base.box)(Base.Bool,(Base.not_int)((Base.box)(Base.Bool,(Base.not_int)(#s52::Int64 === (Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool)))) goto 2
        1: 
        0:  # In[224], line 6:
        return total::Union{Float64,Int64}
    end::Union{Float64,Int64}))))

In [239]:
code_typed(sum2, (Int,))


Out[239]:
1-element Array{Any,1}:
 :($(Expr(:lambda, Any[:N], Any[Any[Any[:N,Int64,0],Any[:total,Float64,2],Any[symbol("#s52"),Int64,2],Any[:i,Int64,18]],Any[],Any[UnitRange{Int64},Tuple{Int64,Int64},Int64,Int64],Any[]], :(begin  # In[224], line 11:
        total = 0.0 # In[224], line 12:
        GenSym(0) = $(Expr(:new, UnitRange{Int64}, 1, :(((top(getfield))(Base.Intrinsics,:select_value)::I)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64)))
        #s52 = (top(getfield))(GenSym(0),:start)::Int64
        unless (Base.box)(Base.Bool,(Base.not_int)(#s52::Int64 === (Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool)) goto 1
        2: 
        GenSym(2) = #s52::Int64
        GenSym(3) = (Base.box)(Base.Int,(Base.add_int)(#s52::Int64,1))
        i = GenSym(2)
        #s52 = GenSym(3) # In[224], line 13:
        total = (Base.box)(Base.Float64,(Base.add_float)(total::Float64,(Base.box)(Base.Float64,(Base.div_float)((Base.box)(Float64,(Base.sitofp)(Float64,i::Int64)),(Base.box)(Float64,(Base.sitofp)(Float64,2))))))
        3: 
        unless (Base.box)(Base.Bool,(Base.not_int)((Base.box)(Base.Bool,(Base.not_int)(#s52::Int64 === (Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool)))) goto 2
        1: 
        0:  # In[224], line 15:
        return total::Float64
    end::Float64))))

In [240]:
code_llvm(sum1, (Int,))


define %jl_value_t* @julia_sum1_27369(i64) {
top:
  %1 = alloca [5 x %jl_value_t*], align 8
  %.sub = getelementptr inbounds [5 x %jl_value_t*]* %1, i64 0, i64 0
  %2 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 2
  %3 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 3
  store %jl_value_t* inttoptr (i64 6 to %jl_value_t*), %jl_value_t** %.sub, align 8
  %4 = load %jl_value_t*** @jl_pgcstack, align 8
  %5 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 1
  %.c = bitcast %jl_value_t** %4 to %jl_value_t*
  store %jl_value_t* %.c, %jl_value_t** %5, align 8
  store %jl_value_t** %.sub, %jl_value_t*** @jl_pgcstack, align 8
  store %jl_value_t* null, %jl_value_t** %3, align 8
  %6 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 4
  store %jl_value_t* null, %jl_value_t** %6, align 8
  store %jl_value_t* inttoptr (i64 4562034768 to %jl_value_t*), %jl_value_t** %2, align 8
  %7 = icmp sgt i64 %0, 0
  %8 = select i1 %7, i64 %0, i64 0
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %L3, label %L

L:                                                ; preds = %L, %top
  %10 = phi %jl_value_t* [ %17, %L ], [ inttoptr (i64 4562034768 to %jl_value_t*), %top ]
  %"#s52.0" = phi i64 [ %11, %L ], [ 1, %top ]
  %11 = add i64 %"#s52.0", 1
  store %jl_value_t* %10, %jl_value_t** %3, align 8
  %12 = sitofp i64 %"#s52.0" to double
  %13 = fmul double %12, 5.000000e-01
  %14 = call %jl_value_t* @jl_gc_alloc_1w()
  %15 = getelementptr inbounds %jl_value_t* %14, i64 -1, i32 0
  store %jl_value_t* inttoptr (i64 4562691088 to %jl_value_t*), %jl_value_t** %15, align 8
  %16 = bitcast %jl_value_t* %14 to double*
  store double %13, double* %16, align 16
  store %jl_value_t* %14, %jl_value_t** %6, align 8
  %17 = call %jl_value_t* @jl_apply_generic(%jl_value_t* inttoptr (i64 4570425808 to %jl_value_t*), %jl_value_t** %3, i32 2)
  store %jl_value_t* %17, %jl_value_t** %2, align 8
  %18 = icmp eq i64 %"#s52.0", %8
  br i1 %18, label %L3, label %L

L3:                                               ; preds = %L, %top
  %19 = phi %jl_value_t* [ inttoptr (i64 4562034768 to %jl_value_t*), %top ], [ %17, %L ]
  %20 = load %jl_value_t** %5, align 8
  %21 = getelementptr inbounds %jl_value_t* %20, i64 0, i32 0
  store %jl_value_t** %21, %jl_value_t*** @jl_pgcstack, align 8
  ret %jl_value_t* %19
}

In [242]:
code_native(sum1, (Int,))  # assembly


	.section	__TEXT,__text,regular,pure_instructions
Filename: In[224]
Source line: 2
	pushq	%rbp
	movq	%rsp, %rbp
Source line: 2
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$56, %rsp
	movq	$6, -80(%rbp)
	movabsq	$jl_pgcstack, %rcx
	movq	(%rcx), %rax
	movq	%rax, -72(%rbp)
	leaq	-80(%rbp), %rax
	movq	%rax, (%rcx)
	movq	$0, -56(%rbp)
	movq	$0, -48(%rbp)
	movabsq	$4562034768, %rax       ## imm = 0x10FEB2050
Source line: 2
	movq	%rax, -64(%rbp)
	xorl	%r12d, %r12d
	testq	%rdi, %rdi
	movl	$0, %ecx
Source line: 3
	cmovnsq	%rdi, %rcx
	testq	%rcx, %rcx
	je	L244
	testq	%rdi, %rdi
	cmovnsq	%rdi, %r12
	movl	$1, %ebx
	movabsq	$4562034768, %rax       ## imm = 0x10FEB2050
Source line: 4
	movabsq	$jl_gc_alloc_1w, %r13
	movabsq	$4562691088, %r14       ## imm = 0x10FF52410
	movabsq	$13659316592, %rcx      ## imm = 0x32E28A170
	movsd	(%rcx), %xmm0
	movsd	%xmm0, -88(%rbp)
	movabsq	$jl_apply_generic, %r15
L174:	movq	%rax, -56(%rbp)
	callq	*%r13
	movq	%r14, -8(%rax)
	xorps	%xmm0, %xmm0
	cvtsi2sdq	%rbx, %xmm0
	mulsd	-88(%rbp), %xmm0
	movsd	%xmm0, (%rax)
	movq	%rax, -48(%rbp)
	movabsq	$4570425808, %rdi       ## imm = 0x1106B29D0
Source line: 2
	leaq	-56(%rbp), %rsi
	movl	$2, %edx
Source line: 4
	callq	*%r15
Source line: 3
	incq	%rbx
Source line: 4
	decq	%r12
	movq	%rax, -64(%rbp)
	jne	L174
Source line: 6
L244:	movq	-72(%rbp), %rcx
Source line: 2
	movabsq	$jl_pgcstack, %rdx
Source line: 6
	movq	%rcx, (%rdx)
	addq	$56, %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	ret

Interoperability

Coming from Python, the number of packages available to Julia will seem very small. However there is the possibility of Julia to call packages written in other languages.

Interoperability with Python

Python interoperability is very easy due to the PyCall package!


In [246]:
using PyCall

PyCall has a high-level intereface that transports between Julia and Python trasparently from the users point of view.

For example let's call Python's math module!


In [247]:
@pyimport math

In [248]:
math.sin(math.pi)


Out[248]:
1.2246467991473532e-16

Array objects are automatically converted.. So for numpy


In [249]:
@pyimport numpy.random as nprandom

In [251]:
nr=nprandom.rand(3,4) # this returns a ndarray... but in juliaaa...


Out[251]:
3x4 Array{Float64,2}:
 0.0405775  0.278256  0.371446  0.168714
 0.47408    0.3025    0.281124  0.451177
 0.70342    0.21438   0.218605  0.404222

In [253]:
typeof(nr)  # but within Julia this is an array


Out[253]:
Array{Float64,2}

Defining a Julia function


In [254]:
objective = x -> cos(x) - x  # this is a Julia anonymous function


Out[254]:
(anonymous function)

In [255]:
objective(3)


Out[255]:
-3.989992496600445

We can also pass this Julia (anonymous) function to a Python module:


In [256]:
@pyimport scipy.optimize as so

In [257]:
so.newton(objective,1)


Out[257]:
0.7390851332151607

Differential Equations

Julia has ODE solvers in ODE.jl and Sundials.jl, but we can also use Python solvers.


In [259]:
@pyimport scipy.integrate as integrate
f(x,t)= -x


Out[259]:
f (generic function with 2 methods)

In [261]:
t=[0:0.1:10];

In [262]:
soln = integrate.odeint(f,1,t)


Out[262]:
101x1 Array{Float64,2}:
 1.0        
 0.904837   
 0.818731   
 0.740818   
 0.67032    
 0.606531   
 0.548812   
 0.496585   
 0.449329   
 0.40657    
 0.367879   
 0.332871   
 0.301194   
 ⋮          
 0.000136388
 0.000123409
 0.000111665
 0.000101039
 9.14236e-5 
 8.27231e-5 
 7.4851e-5  
 6.77283e-5 
 6.12835e-5 
 5.54518e-5 
 5.01752e-5 
 4.54009e-5 

Note that PyPlot package provides a higher-level wrapper than PyCall around matplotlib


In [264]:
plot(t, soln)


Out[264]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x32e64d310>

Difference with Python [:obj]

In Python accessing fields and properties of objects is done using the

obj.a  and obj.b()

syntax. However, the syntax obj.b in Julia is restricted to accessing fields of composite types.

Thus to access fields and methods of Python objects via PyCall, it is necessary to use the syntax:

For Fields:   obj[:a]

For Methods:  obj[:a]()

Where here, we use the Julia syntax :a to mean the Julia symbol a.

PyObject - Lower Level

The PyCall interface is built ontop of the interface which transports objects between Python and Julia: PyObject, which wraps the PyObject* in C and represents a reference to a Python object


In [265]:
PyObject(3)


Out[265]:
PyObject 3

In [268]:
xx = PyObject(rand(5,5))


Out[268]:
PyObject array([[ 0.38691497,  0.19097268,  0.65124688,  0.07389289,  0.25604434],
       [ 0.2863791 ,  0.05398261,  0.22311334,  0.91811275,  0.05572879],
       [ 0.07144211,  0.33430555,  0.90846644,  0.96648533,  0.68193779],
       [ 0.17312292,  0.66456728,  0.13342206,  0.16984347,  0.36904861],
       [ 0.72370653,  0.99969921,  0.12774291,  0.4167023 ,  0.53611966]])

In [269]:
typeof(xx)


Out[269]:
PyCall.PyObject

In [270]:
names(xx)


Out[270]:
1-element Array{Symbol,1}:
 :o

In [271]:
xx.o


Out[271]:
Ptr{PyCall.PyObject_struct} @0x000000032e51a990

In [272]:
#xx.shape in python becomes
xx[:shape]


Out[272]:
(5,5)

NB: Julia arrays passed in Python without a copy, but by default Python array is copied when a result is requested in Julia. This can be avoided at a lower level using pycall and PyArray.


Interoperability with C

Within Julia you can call C and Fortran functions in shared libraries, via the ccall function.

To use it we need to specify :

  • the name of the function as a Julia symbol or as a string;
  • and the name of the shared library; given as an ordered pair (tuple)

  • the return type of the function

  • the argument types that the function accepts, as a tuple
  • and the arguments themselves.

An example using the clock function:


In [274]:
t = ccall( (:clock, "libc"), Int32, ())


Out[274]:
91394738

In [275]:
typeof(t)


Out[275]:
Int32

In [277]:
path = ccall( (:getenv, "libc"), Ptr{UInt8}, (Ptr{UInt8},), "PATH")
# (name of function, library), return type, (argument type,), argument)
# Ptr{UInt8} is a pointer to the Uint8 type


Out[277]:
Ptr{UInt8} @0x00007fbb23d09a35

In [278]:
path


Out[278]:
Ptr{UInt8} @0x00007fbb23d09a35

In [279]:
bytestring(path) # create a string from the address of a C string


Out[279]:
"/Applications/Julia-0.4.5.app/Contents/Resources/julia/bin:/Applications/Julia-0.4.5.app/Contents/Resources/julia/bin:/Applications/Julia-0.4.5.app/Contents/Resources/julia/libexec/git-core:/Users/nkarast/anaconda/bin:/Users/nkarast/geant4_workdir/bin/Darwin-clang:/Users/nkarast/Geant-all/Geant4_10_01p02-Install/bin:/usr/local/sbin:/Users/nkarast/.local/bin:/Users/nkarast/root-6.04.14/root-6.04.14-build/bin:/usr/local/bin:/opt/X11/bin:/usr/local/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/openafs/bin:/opt/openafs/sbin:/opt/X11/bin:/Library/TeX/texbin"

Meta-programming

Julia allows to talk in a meta way (one level up) about Julia code, this is to treat code as Julia object!

The basic objects in this approach are the unevaluated symbols!


In [289]:
:s


Out[289]:
:s

In [290]:
typeof(:s)


Out[290]:
Symbol

:s refers to the symbol s and we can evaluate with the eval function


In [291]:
eval(:s) # not yet defined..


LoadError: UndefVarError: s not defined
while loading In[291], in expression starting on line 1

In [292]:
s=3; eval(:s)


Out[292]:
3

The eval function takes an expression and evaluates it, generating the corresponding code.

Everything is a symbol:


In [293]:
:+, :sin


Out[293]:
(:+,:sin)

and symbols can be combined into expressions, that are the basic objects which represent pieces of Julia code.


In [295]:
ex = :(a+b) # this is the expression a+b


Out[295]:
:(a + b)

In [296]:
typeof(ex)


Out[296]:
Expr

Expressions are Julia's objects, so we can find more info about it


In [297]:
names(ex)


Out[297]:
3-element Array{Symbol,1}:
 :head
 :args
 :typ 

In [299]:
ex.args  # ex.<TAB>


Out[299]:
3-element Array{Any,1}:
 :+
 :a
 :b

In [302]:
ex.args[2]


Out[302]:
:a

But expressions can be arbitrary Julia code, that when evaluated will have side effects.

For longer blocks of code quote ... end may be used instead of :(...)


In [303]:
ex2 = 
quote
    y=3
    z=sin(y+1)
end


Out[303]:
quote  # In[303], line 3:
    y = 3 # In[303], line 4:
    z = sin(y + 1)
end

In [304]:
eval(ex2)


Out[304]:
-0.7568024953079282

In [306]:
Meta.show_sexpr(ex2) # this is to show the full info


(:block,
  :( # In[303], line 3:),
  (:(=), :y, 3),
  :( # In[303], line 4:),
  (:(=), :z, (:call, :sin, (:call, :+, :y, 1)))
)

In [307]:
dump(ex2) # to dump the internal of an expression


Expr 
  head: Symbol block
  args: Array(Any,(4,))
    1: LineNumberNode 
      file: Symbol In[303]
      line: Int64 3
    2: Expr 
      head: Symbol =
      args: Array(Any,(2,))
        1: Symbol y
        2: Int64 3
      typ: Any
    3: LineNumberNode 
      file: Symbol In[303]
      line: Int64 4
    4: Expr 
      head: Symbol =
      args: Array(Any,(2,))
        1: Symbol z
        2: Expr 
          head: Symbol call
          args: Array(Any,(2,))
          typ: Any
      typ: Any
  typ: Any

Macros

Using this idea we can write Julia code on the fly from within Julia : programming a program!

The macro name is given to a kind of 'super-function' that takes a piece of code as an argument and returns an altered piece of code.

Macros map a tuple of argument expressions to a returned expression.

Macros are useful to

  • eliminate repetitive code
  • automatically generate complex code that would be painful by hand
  • unroll loops for efficiency
  • inline code for efficiency

Macros are invoked using the @ sign. For example:


In [308]:
@time sin(10)


  0.000003 seconds (5 allocations: 176 bytes)
Out[308]:
-0.5440211108893698

We can defin a macro as follows. The $ sign is used to interpolate the valie of the expression (as in string interpolation)


In [309]:
macro duplicate(ex)
    quote
        $ex
        $ex
    end
end

In [310]:
@duplicate println(sin(10))


-0.5440211108893698
-0.5440211108893698

In [315]:
ex = :(@duplicate println(sin(10))) #to understand what it does...


Out[315]:
:(@duplicate println(sin(10)))

In [313]:
eval(ex)


-0.5440211108893698
-0.5440211108893698

In [314]:
macroexpand(ex)


Out[314]:
quote  # In[309], line 3:
    println(sin(10)) # In[309], line 4:
    println(sin(10))
end

In [316]:
macroexpand(:(@time sine(10)))


Out[316]:
quote  # util.jl, line 153:
    local #113#stats = Base.gc_num() # util.jl, line 154:
    local #115#elapsedtime = Base.time_ns() # util.jl, line 155:
    local #114#val = sine(10) # util.jl, line 156:
    #115#elapsedtime = Base.-(Base.time_ns(),#115#elapsedtime) # util.jl, line 157:
    local #116#diff = Base.GC_Diff(Base.gc_num(),#113#stats) # util.jl, line 158:
    Base.time_print(#115#elapsedtime,#116#diff.allocd,#116#diff.total_time,Base.gc_alloc_count(#116#diff)) # util.jl, line 160:
    #114#val
end

In [ ]: