Julia by Example

44
 Julia by examples  Julia is a high-level, high-perfor mance dynamic pr ogramming language for technical computing. This site is a non ocial series of examples of Julia, for more details see about. Below are a series of examples of common operations in Julia. They assume you already have Julia installed and working the examples are curr ently tested with Julia v!." - latest#. $ello %orld &imple 'unctions &trings Basics &tring( )onverting and formatting &tring *anipulations +rrays rror $andling *ultidimensional +rrays ictionaries oops and *ap  T ypes /nput 0 1utput 2ackages and /ncluding of 'iles 2lotting  T ext2lots 2y2lot %inston 3ad4y ata'rames Hello World  The simplest possible s cript. println("hello world") %ith Julia installed and added to your path this script can be run by julia hello_world.jl , it can also be run from 52 by typing include("hello_world.jl") , that will evaluate all valid expressio ns in that 6le and return the last output. Simple Functions  The example below sh ows two simple functio ns, how to call them and pr int the results. 'urther examples of number formatting are shown below.

description

Learn Julia lang by means of examples

Transcript of Julia by Example

Julia by examples

Julia is a high-level, high-performance dynamic programming language for technical computing. This site is a non official series of examples of Julia, for more details see about.Below are a series of examples of common operations in Julia. They assume you already have Julia installed and working (the examples are currently tested with Julia v0.3 - latest).

Hello World

Simple Functions

Strings Basics

String: Converting and formatting

String Manipulations

Arrays

Error Handling

Multidimensional Arrays

Dictionaries

Loops and Map

Types

Input & Output

Packages and Including of Files

Plotting

TextPlots

PyPlot

Winston

Gadfly

DataFrames

Hello WorldThe simplest possible script.Github Link println("hello world")With Julia installed and added to your path this script can be run by julia hello_world.jl, it can also be run from REPL by typing include("hello_world.jl"), that will evaluate all valid expressions in that file and return the last output.Simple FunctionsThe example below shows two simple functions, how to call them and print the results. Further examples of number formatting are shown below.Github Link # function to calculate the volume of a spherefunction sphere_vol(r)# julia allows Unicode names (in UTF-8 encoding)# so either "pi" or the symbol can be usedreturn 4/3*pi*r^3end

# functions can also be defined more succinctlyquadratic(a, sqr_term, b) = (-b + sqr_term) / 2a

# calculates x for 0 = a*x^2+b*x+c, arguments types can be defined in function definitionsfunction quadratic2(a::Float64, b::Float64, c::Float64)# unlike other languages 2a is equivalent to 2*a# a^2 is used instead of a**2 or pow(a,2)sqr_term = sqrt(b^2-4a*c)r1 = quadratic(a, sqr_term, b)r2 = quadratic(a, -sqr_term, b)# multiple values can be returned from a function using tuples# if the return keyword is omitted, the last term is returnedr1, r2end

vol = sphere_vol(3)# @printf allows number formatting but does not automatically append the \n to statements, see below@printf "volume = %0.3f\n" vol #> volume = 113.097

quad1, quad2 = quadratic2(2.0, -2.0, -12.0)println("result 1: ", quad1)#> result 1: 3.0println("result 2: ", quad2)#> result 2: -2.0Strings BasicsCollection of different string examples (string indexing is the same as array indexing: see below).Github Link # strings are defined with double quotes# like variables, strings can contain any unicode characters1 = "The quick brown fox jumps over the lazy dog ,,"println(s1)#> The quick brown fox jumps over the lazy dog ,,

# println adds a new line to the end of output# print can be used if you dont want that:print("this")#> thisprint(" and")#> andprint(" that.\n")#> that.

# chars are defined with single quotesc1 = 'a'println(c1)#> a# the ascii value of a char can be found with int():println(c1, " ascii value = ", int(c1))#> a ascii value = 97println("int('') == ", int(''))#> int('') == 945

# so be aware thatprintln(int('1') == 1)#> false

# strings can be converted to upper case or lower case:s1_caps = uppercase(s1)s1_lower = lowercase(s1)println(s1_caps, "\n", s1_lower)#> THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG ,,#> the quick brown fox jumps over the lazy dog ,,

# sub strings can be indexed like arrays:# (show prints the raw value)show(s1[11]); println()#> 'b'

# or sub strings can be created:show(s1[1:10]); println()#> "The quick "

# end is used for the end of the array or stringshow(s1[end-10:end]); println()#> "dog ,,"

# julia allows string Interpolation:a = "wolcome"b = "julia"println("$a to $b.")#> wolcome to julia.

# this can extend to evaluate statements:println("1 + 2 = $(1 + 2)")#> 1 + 2 = 3

# strings can also be concatenated using the * operator# using * instead of + isn't intuitive when you start with Julia,# however people think it makes more senses2 = "this" * " and" * " that"println(s2)#> this and that

# as well as the string functions3 = string("this", " and", " that")println(s3)#> this and thatString: Converting and formattingGithub Link # strings can be converted using float and int:e_str1 = "2.718"e = float(e_str1)println(5e)#> 13.5914num_15 = int("15")println(3num_15)#> 45

# numbers can be converted to strings and formatted using printf@printf "e = %0.2f\n" e#> 2.718# or to create another string sprintfe_str2 = @sprintf("%0.3f", e)

# to show that the 2 strings are the sameprintln("e_str1 == e_str2: $(e_str1 == e_str2)")#> e_str1 == e_str2: true

# available number format characters are f, e, g, c, s, p, d:# (pi is a predefined constant; however, since its type is # "MathConst" it has to be converted to a float to be formatted)@printf "fix trailing precision: %0.3f\n" float(pi)#> fix trailing precision: 3.142@printf "scientific form: %0.6e\n" 1000pi#> scientific form: 3.141593e+03# g is not implemented yet@printf "a character: %c\n" ''#> a character: @printf "a string: %s\n" "look I'm a string!"#> a string: look I'm a string!@printf "right justify a string: %50s\n" "width 50, text right justified!"#> right justify a string: width 50, text right justified!@printf "a pointer: %p\n" 1e10#> a pointer: 0x00000002540be400@printf "print a integer: %d\n" 1e10#> print an integer: 10000000000String ManipulationsGithub Link s1 = "The quick brown fox jumps over the lazy dog ,,"

# search returns the first index of a chari = search(s1, 'b')println(i)#> 11# the second argument is equivalent to the second argument of split, see below

# or a range if called with another stringr = search(s1, "brown")println(r)#> 11:15

# string replace is done thus:r = replace(s1, "brown", "red")show(r); println()#> "The quick red fox jumps over the lazy dog"

# search and replace can also take a regular expressions by preceding the string with 'r'r = search(s1, r"b[\w]*n")println(r)#> 11:15

# again with a regular expressionr = replace(s1, r"b[\w]*n", "red")show(r); println()#> "The quick red fox jumps over the lazy dog"

# there are also functions for regular expressions that return RegexMatch types# match scans left to right for the first match (specified starting index optional)r = match(r"b[\w]*n", s1)println(r)#> RegexMatch("brown")

# RegexMatch types have a property match that holds the matched stringshow(r.match); println()#> "brown"

# matchall returns a vector with RegexMatches for each matchr = matchall(r"[\w]{4,}", s1)println(r)#> SubString{UTF8String}["quick","brown","jumps","over","lazy"]

# eachmatch returns an iterator over all the matchesr = eachmatch(r"[\w]{4,}", s1)for(i in r) print("\"$(i.match)\" ") endprintln()#> "quick" "brown" "jumps" "over" "lazy"

# a string can be repeated using the repeat function, # or more succinctly with the ^ syntax:r = "hello "^3show(r); println() #> "hello hello hello "

# the strip function works the same as python:# e.g., with one argument it strips the outer whitespacer = strip("hello ")show(r); println() #> "hello"# or with a second argument of an array of chars it strips any of them;r = strip("hello ", ['h', ' '])show(r); println() #> "ello"# (note the array is of chars and not strings)

# similarly split works in basically the same way as python:r = split("hello, there,bob", ',')show(r); println() #> ["hello"," there","bob"]r = split("hello, there,bob", ", ")show(r); println() #> ["hello","there,bob"]r = split("hello, there,bob", [',', ' '], 0, false)show(r); println() #> ["hello","there","bob"]# (the last two arguements are limit and include_empty, see docs)

# the opposite of split: join is simplyr= join([1:10], ", ")println(r) #> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10ArraysGithub Link function printsum(a)# summary generates a summary of an objectprintln(summary(a), ": ", repr(a))end

# arrays can be initialised directly:a1 = [1,2,3]printsum(a1)#> 3-element Array{Int64,1}: [1,2,3]

# or initialised empty:a2 = []printsum(a2)#> 0-element Array{None,1}: None[]

# since this array has no type, functions like push! (see below) don't work# instead arrays can be initialised with a type:a3 = Int64[]printsum(a3)#> 0-element Array{Int64,1}: []

# ranges are different from arrays:a4 = 1:20printsum(a4)#> 20-element UnitRange{Int64}: 1:20

# however they can be used to create arrays thus:a4 = [1:20]printsum(a4)#> 20-element Array{Int64,1}: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

# arrays can also be generated from comprehensions:a5 = [2^i for i = 1:10]printsum(a5)#> 10-element Array{Int64,1}: [2,4,8,16,32,64,128,256,512,1024]

# arrays can be any type, so arrays of arrays can be created:a6 = (Array{Int64, 1})[]printsum(a6)#> 0-element Array{Array{Int64,1},1}: []# (note this is a "jagged array" (i.e., an array of arrays), not a multidimensional array, these are not covered here)

# Julia provided a number of "Dequeue" functions, the most common for appending to the end of arrays is push!# ! at the end of a function name indicates that the first argument is updated.

push!(a1, 4)printsum(a1)#> 4-element Array{Int64,1}: [1,2,3,4]

# push!(a2, 1) would cause error:

push!(a3, 1)printsum(a3) #> 1-element Array{Int64,1}: [1]#> 1-element Array{Int64,1}: [1]

push!(a6, [1,2,3])printsum(a6)#> 1-element Array{Array{Int64,1},1}: [[1,2,3]]

# using repeat() to create arrays#you must use the keywords "inner" and "outer"# all arguments must be arrays (not ranges)a7 = repeat(a1,inner=[2],outer=[1])printsum(a7)#> 8-element Array{Int64,1}: [1,1,2,2,3,3,4,4]a8 = repeat([4:-1:1],inner=[1],outer=[2])printsum(a8)#> 8-element Array{Int64,1}: [4,3,2,1,4,3,2,1]Error HandlingGithub Link a=[]# try, catch can be used to deal with errors as with many other languagestrypush!(a,1)catch errshowerror(STDOUT, err, backtrace());println()endprintln("Continuing after error")Multidimensional ArraysJulia has very good multidimensional array capabilities. Check out the manual.Github Link # repeat can be useful to expand a grid# as in R's expand.grid() function:

m1 = hcat(repeat([1:2],inner=[1],outer=[3*2]),repeat([1:3],inner=[2],outer=[2]),repeat([1:4],inner=[3],outer=[1]))printsum(m1)#> 12x3 Array{Int64,2}: [1 1 1#> 2 1 1#> 1 2 1#> 2 2 2#> 1 3 2#> 2 3 2#> 1 1 3#> 2 1 3#> 1 2 3#> 2 2 4#> 1 3 4#> 2 3 4]

# for simple repetitions of arrays,# use repmatm2 = repmat(m1,1,2) # replicate a9 once into dim1 and twice into dim2println("size: ", size(m2))#> size: (12,6)

m3 = repmat(m1,2,1) # replicate a9 twice into dim1 and once into dim2println("size: ", size(m3))#> size: (24,3)

# Julia comprehensions are another way to easily create # multidimensional arrays

m4 = [i+j+k for i=1:2, j=1:3, k=1:2] # creates a 2x3x2 array of Int64m5 = ["Hi Im # $(i+2*(j-1 + 3*(k-1)))" for i=1:2, j=1:3, k=1:2] # expressions are very flexible# you can specify the type of the array by just # placing it in front of the expressionm5 = ASCIIString["Hi Im element # $(i+2*(j-1 + 3*(k-1)))" for i=1:2, j=1:3, k=1:2]printsum(m5)#> 2x3x2 Array{ASCIIString,3}: ASCIIString["Hi Im element # 1" "Hi Im element # 3" "Hi Im element # 5"#> "Hi Im element # 2" "Hi Im element # 4" "Hi Im element # 6"]#> #> ASCIIString["Hi Im element # 7" "Hi Im element # 9" "Hi Im element # 11"#> "Hi Im element # 8" "Hi Im element # 10" "Hi Im element # 12"]

# Array reductions# many functions in Julia have an array method# to be applied to specific dimensions of an array:

sum(m4,3) # takes the sum over the third dimensionsum(m4,(1,3)) # sum over first and third dim

maximum(m4,2) # find the max elt along dim 2findmax(m4,3) # find the max elt and its index along dim 2 (available only in very recent Julia versions)

# Broadcasting# when you combine arrays of different sizes in an operation,# an attempt is made to "spread" or "broadcast" the smaller array# so that the sizes match up. broadcast operators are preceded by a dot:

m4 .+ 3 # add 3 to all elementsm4 .+ [1:2] # adds vector [1,2] to all elements along first dim

# slices and viewsm4[:,:,1] # holds dim 3 fixed and displays the resulting viewm4[:,2,:] # that's a 2x1x2 array. not very intuititive to look at

# get rid of dimensions with size 1:squeeze(m4[:,2,:],2) # that's better

# assign new values to a certain viewm4[:,:,1] = rand(1:6,2,3)printsum(m4)#> 2x3x2 Array{Int64,3}: [3 5 2#> 2 2 2]#> #> [4 5 6#> 5 6 7]

# (for more examples of try, catch see Error Handling above)try# this will cause an error, you have to assign the correct typem4[:,:,1] = rand(2,3)catch errprintln(err)end#> InexactError()

try# this will cause an error, you have to assign the right shapem4[:,:,1] = rand(1:6,3,2)catch errprintln(err)end#> DimensionMismatch("tried to assign 3x2 array to 2x3x1 destination")DictionariesJulia uses Dicts as associative collections. Usage is very like python except for the rather odd => definition syntax.Github Link # dicts can be initialised directly:a1 = {1=>"one", 2=>"two"}printsum(a1) #> Dict{Any,Any}: {2=>"two",1=>"one"}

# then added to:a1[3]="three"printsum(a1) #> Dict{Any,Any}: {2=>"two",3=>"three",1=>"one"}# (note dicts cannot be assumed to keep their original order)

# dicts may also be created with the type explicitly seta2 = Dict{Int64, String}()a2[0]="zero"

# dicts, like arrays, may also be created from comprehensionsa3 = {i => @sprintf("%d", i) for i = 1:10}printsum(a3)#> Dict{Any,Any}: {5=>"5",4=>"4",6=>"6",7=>"7",2=>"2",10=>"10",9=>"9",8=>"8",3=>"3",1=>"1"}

# as you would expect, Julia comes with all the normal helper functions# for dicts, e.g., (haskey)[http://docs.julialang.org/en/latest/stdlib/base/#Base.haskey]println(haskey(a1,1)) #> true

# which is equivalent toprintln(1 in keys(a1)) #> true# where keys creates an iterator over the keys of the dictionary

# similar to keys, (values)[http://docs.julialang.org/en/latest/stdlib/base/#Base.values] get iterators over the dict's values:printsum(values(a1)) #> ValueIterator{Dict{Any,Any}}: ValueIterator{Dict{Any,Any}}({2=>"two",3=>"three",1=>"one"})

# use collect to get an array:printsum(collect(values(a1)))#> 3-element Array{Any,1}: {"two","three","one"}Loops and MapFor loops can be defined in a number of ways.Github Link for i in 1:5print(i, ", ")end#> 1, 2, 3, 4, 5, # In loop definitions "in" is equivilent to "=" (AFAIK, the two are interchangable in this context)for i = 1:5print(i, ", ")endprintln() #> 1, 2, 3, 4, 5,

# arrays can also be looped over directly:a1 = [1,2,3,4]for i in a1print(i, ", ")endprintln() #> 1, 2, 3, 4,

# continue and break work in the same way as pythona2 = [1:20]for i in a2if i % 2 != 0continueendprint(i, ", ")if i >= 8breakendendprintln() #> 2, 4, 6, 8,

# if the array is being manipulated during evaluation a while loop shoud be used# pop removes the last element from an arraywhile !isempty(a1)print(pop!(a1), ", ")endprintln() #> 4, 3, 2, 1,

d1 = {1=>"one", 2=>"two", 3=>"three"}# dicts may be looped through using the keys function:for k in sort(collect(keys(d1)))print(k, ": ", d1[k], ", ")endprintln() #> 1: one, 2: two, 3: three,

# like python enumerate can be used to get both the index and value in a loopa3 = ["one", "two", "three"]for (i, v) in enumerate(a3)print(i, ": ", v, ", ")endprintln() #> 1: one, 2: two, 3: three,

# (note enumerate starts from 1 since Julia arrays are 1 indexed unlike python)

# map works as you might expect performing the given function on each member of an array or iter much like comprehensionsa4 = map((x) -> x^2, [1, 2, 3, 7])printsum(a4) #> 4-element Array{Int64,1}: [1,4,9,49]TypesTypes are a key way of structuring data within Julia.Github Link # Type Definitions are probably most similar to tyepdefs in c?# a simple type with no special constructor functions might look like thistype Personname::Stringmale::Boolage::Float64children::Intend

p = Person("Julia", false, 4, 0)printsum(p)#> Person: Person("Julia",false,4.0,0)

people = Person[]push!(people, Person("Steve", true, 42, 0))push!(people, Person("Jade", false, 17, 3))printsum(people)#> 2-element Array{Person,1}: [Person("Steve",true,42.0,0),Person("Jade",false,17.0,3)]

# types may also contains arrays and dicts# constructor functions can be defined to easily create objectstype Familyname::Stringmembers::Array{String, 1}extended::Bool# constructor that takes one argument and generates a default# for the other two valuesFamily(name::String) = new(name, String[], false)# constructor that takes two arguements and infers the thirdFamily(name::String, members) = new(name, members, length(members) > 3)end

fam1 = Family("blogs")println(fam1)#> Family("blogs",String[],false)fam2 = Family("jones", ["anna", "bob", "charlie", "dick"])println(fam2)#> Family("jones",String["anna","bob","charlie","dick"],true)Input & OutputThe basic syntax for reading and writing files in Julia is quite similar to python.The simple.dat file used in this example is available from github.Github Link fname = "simple.dat"# using do means the file is closed automatically# in the same way "with" does in pythonopen(fname,"r") do ffor line in eachline(f)print(line)endend#> this is a simple file containing#> text and numbers:#> 43.3#> 17

f = open(fname,"r")showall(readlines(f))#> Union(ASCIIString,UTF8String)["this is a simple file containing\n","text and numbers:\n","43.3\n","17\n"]close(f)

f = open(fname,"r")fstring = readall(f)close(f)println(summary(fstring))#> ASCIIStringprint(fstring)#> this is a simple file containing#> text and numbers:#> 43.3#> 17

outfile = "outfile.dat"# writing to files is very similar:f = open(outfile, "w")# both print and println can be used as usual but with f as their first arugmentprintln(f, "some content")print(f, "more content")print(f, " more on the same line")close(f)

# we can then check the content of the file written# "do" above just creates an anonymous function and passes it to open# we can use the same logic to pass readall and thereby succinctly# open, read and close a file in one lineoutfile_content = open(readall, outfile, "r")println(repr(outfile_content))#> "some content\nmore content more on the same line"Packages and Including of FilesPackages extend the functionality of Julia's standard library. Github Link # You might not want to run this file completely, as the Pkg-commands can take a# long time to complete.

# list all available packages:Pkg.available()

# install one package (e.g. Calculus) and all its dependencies:Pkg.add("Calculus")

# to list all installed packagesPkg.installed()

# to update all packages to their newest versionPkg.update()

# to use a package:using Calculus# will import all functions of that package into the current namespace, so that# it is possible to callderivative(x -> sin(x), 1.0)# without specifing the package it is included in.

import Calculus# will enable you to specify which package the function is called fromCalculus.derivative(x -> cos(x), 1.0)

# Using `import` is especially useful if there are conflicts in function/type-names# between packages.# Example:# Winston as well as Gadfly provide a plot() function (see below).Pkg.add("Winston")Pkg.add("Gadfly")

# If you were to "import" both of the packages with `using`, there would be a conflict.# That can be prevented by using `import`, as follows:import Winstonimport Gadfly

Winston.plot(rand(4))Gadfly.plot(x=[1:10], y=rand(10))PlottingPlotting in Julia is only possible with additional Packages. Examples of different packages are given below.TextPlotsTextPlots Packge Page.TextPlots is an extemely simple plotting library which generates terminal plots using Braille characters.It is well documented and requires no other packages or external libraries; making it the simplest plotting option for Julia.Github Link using TextPlots

plot(x -> cos(x), -1:1)#> cos(x)#> 1 #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> 0.54030 #> -1 1

plot([x -> cos(x), x -> cos(x + pi)], 0:5)#> cos(x), cos(x + pi)#> 1 #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> -1 #> 0 5

plot([1, 3, 5, 7], [13, 11, 9, 7])#> scatter plot#> 13 #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> 7 #> 1 7PyPlotPyPlot Package Page.PyPlot needs Python and matplotlib installed matplotlib.pyplot docs.Github Link # PyPlot: needs Python and matplotlib installed# matplotlib.pyplot docsimport PyPlot# plot 5 random numbers in [0,1], PyPlot.plot creates a new figurePyPlot.plot(rand(5))# labeling the axes, creating a title:PyPlot.xlabel("x-axis")PyPlot.ylabel("y-axis")PyPlot.title("Random")# creating the legend:l = ["random data"]# legend takes an array of strings, but since strings can be indexed to get# chars (e.g. l[1] == 'r') the brackets are necessaryPyPlot.legend(l)

# create a new figure:f = PyPlot.figure(2)# generating data:y1 = randn(10)y2 = randn(10)x = 1:10 # PyPlot can handle ranges as well as arrays# setup a grid for plotting:PyPlot.subplot2grid((3, 1), (0, 0), rowspan=2)# options for plotting are passed as keyword-arguments:PyPlot.plot(x, y1, c="red", marker=".", linestyle="None")# a second plot will plot in an existing figure/subplotPyPlot.plot(x, y2)# add text to the plot:PyPlot.text(x[4]+.1, y1[4], "value #4", verticalalignment="center")# PyPlot will adjust the axes automatically, but xlim and ylim can explicitly# change the smallest and largest value displayed on either axisPyPlot.xlim([0, 11])PyPlot.legend(["dots", "line"], loc="upper left")

# change into the other subplot:PyPlot.subplot2grid((3, 1), (2, 0), rowspan=2)# plot some more data into the lower subplotPyPlot.plot(x, y1-y2)PyPlot.xlim([0, 11])PyPlot.ylim([-3, 3])# change the ticks used on the y-axisPyPlot.yticks([-3, 0, 3])# save the current figurePyPlot.savefig("pyplot.png")pyplot.pngWinstonWinston Package PageMatlab-like plotting. Installed via Pkg.add("Winston")Github Link using Winston

# optionally call figure prior to plotting to set the sizefigure(width=600, height=400)# plot some datapl = plot(cumsum(rand(500) .- 0.5), "r", cumsum(rand(500) .- 0.5), "b")# display the plot (not done automatically!)display(pl)

# by default display will not wait and the plot will vanish as soon as it appears# using readline is a blunt wait to allow the user to choose when to continueprintln("Press enter to continue: ")readline(STDIN)

# save the current figuresavefig("winston.svg")# .eps, .pdf, & .png are also supported# we used svg here because it respects the width and height specified above

# TODO: needs more expansive explanationwinston.svgGadflyGadfly Package Pageggplot2-like plotting. Installed via Pkg.add("Gadfly")Github Link using Gadfly

# plot some datapl = @plot(cos(x)/x, 5, 25)# and save it to a filedraw(PNG("gadfly.png", 300, 100), pl)

# TODO: needs more expansive explanationgadfly.pngDataFramesThe DataFrames.jl package provides tool for working with tabular data.The iris.csv file used in this example is available from github.Github Link using DataFramesshowln(x) = (show(x); println())# TODO: needs more links to docs.

# A DataFrame is an in-memory databasedf = DataFrame(A = [1, 2], B = [e, pi], C = ["xx", "xy"])showln(df)#> 2x3 DataFrame#> |-------|---|---------|------|#> | Row # | A | B | C |#> | 1 | 1 | 2.71828 | "xx" |#> | 2 | 2 | 3.14159 | "xy" |

# The columns of a DataFrame can be indexed using numbers or namesshowln(df[1])#> [1,2]showln(df[:A])#> [1,2]

showln(df[2])#> [2.718281828459045,3.141592653589793]showln(df[:B])#> [2.718281828459045,3.141592653589793]

showln(df[3])#> ASCIIString["xx","xy"]showln(df[:C])#> ASCIIString["xx","xy"]

# The rows of a DataFrame can be indexed only by using numbersshowln(df[1, :])#> 1x3 DataFrame#> |-------|---|---------|------|#> | Row # | A | B | C |#> | 1 | 1 | 2.71828 | "xx" |showln(df[1:2, :])#> 2x3 DataFrame#> |-------|---|---------|------|#> | Row # | A | B | C |#> | 1 | 1 | 2.71828 | "xx" |#> | 2 | 2 | 3.14159 | "xy" |

# DataFrames can be loaded from CSV files using readtable()iris = readtable("iris.csv")

# Check the names and element types of the columns of our new DataFrameshowln(names(iris))#> [:SepalLength,:SepalWidth,:PetalLength,:PetalWidth,:Species]showln(eltypes(iris))#> Type[Float64,Float64,Float64,Float64,UTF8String]

# Subset the DataFrame to only include rows for one speciesshowln(iris[iris[:Species] .== "setosa", :])#> 50x5 DataFrame#> |-------|-------------|------------|-------------|------------|----------|#> | Row # | SepalLength | SepalWidth | PetalLength | PetalWidth | Species |#> | 1 | 5.1 | 3.5 | 1.4 | 0.2 | "setosa" |#> | 2 | 4.9 | 3.0 | 1.4 | 0.2 | "setosa" |#> | 3 | 4.7 | 3.2 | 1.3 | 0.2 | "setosa" |#> | 4 | 4.6 | 3.1 | 1.5 | 0.2 | "setosa" |#> | 5 | 5.0 | 3.6 | 1.4 | 0.2 | "setosa" |#> | 6 | 5.4 | 3.9 | 1.7 | 0.4 | "setosa" |#> | 7 | 4.6 | 3.4 | 1.4 | 0.3 | "setosa" |#> | 8 | 5.0 | 3.4 | 1.5 | 0.2 | "setosa" |#> | 9 | 4.4 | 2.9 | 1.4 | 0.2 | "setosa" |#> #> | 41 | 5.0 | 3.5 | 1.3 | 0.3 | "setosa" |#> | 42 | 4.5 | 2.3 | 1.3 | 0.3 | "setosa" |#> | 43 | 4.4 | 3.2 | 1.3 | 0.2 | "setosa" |#> | 44 | 5.0 | 3.5 | 1.6 | 0.6 | "setosa" |#> | 45 | 5.1 | 3.8 | 1.9 | 0.4 | "setosa" |#> | 46 | 4.8 | 3.0 | 1.4 | 0.3 | "setosa" |#> | 47 | 5.1 | 3.8 | 1.6 | 0.2 | "setosa" |#> | 48 | 4.6 | 3.2 | 1.4 | 0.2 | "setosa" |#> | 49 | 5.3 | 3.7 | 1.5 | 0.2 | "setosa" |#> | 50 | 5.0 | 3.3 | 1.4 | 0.2 | "setosa" |

# Count the number of rows for each speciesshowln(by(iris, :Species, df -> size(df, 1)))#> 3x2 DataFrame#> |-------|--------------|----|#> | Row # | Species | x1 |#> | 1 | "setosa" | 50 |#> | 2 | "versicolor" | 50 |#> | 3 | "virginica" | 50 |

# Discretize entire columns at a timeiris[:SepalLength] = iround(iris[:SepalLength])iris[:SepalWidth] = iround(iris[:SepalWidth])

# Tabulate data according to discretized columns to see "clusters"tabulated = by(iris,[:Species, :SepalLength, :SepalWidth],df -> size(df, 1))showln(tabulated)#> 17x4 DataFrame#> |-------|--------------|-------------|------------|----|#> | Row # | Species | SepalLength | SepalWidth | x1 |#> | 1 | "setosa" | 4 | 3 | 4 |#> | 2 | "setosa" | 5 | 2 | 1 |#> | 3 | "setosa" | 5 | 3 | 23 |#> | 4 | "setosa" | 5 | 4 | 17 |#> | 5 | "setosa" | 6 | 4 | 5 |#> | 6 | "versicolor" | 5 | 2 | 3 |#> | 7 | "versicolor" | 5 | 3 | 3 |#> | 8 | "versicolor" | 6 | 2 | 6 |#> | 9 | "versicolor" | 6 | 3 | 29 |#> | 10 | "versicolor" | 7 | 3 | 9 |#> | 11 | "virginica" | 5 | 3 | 1 |#> | 12 | "virginica" | 6 | 2 | 1 |#> | 13 | "virginica" | 6 | 3 | 22 |#> | 14 | "virginica" | 7 | 3 | 19 |#> | 15 | "virginica" | 7 | 4 | 1 |#> | 16 | "virginica" | 8 | 3 | 4 |#> | 17 | "virginica" | 8 | 4 | 2 |

Source Examples

quine.jl bubblesort.jl

enum.jl

modint.jl

queens.jl

preduce.jl

time.jl

ndgrid.jl

lru_test.jl

staged.jl

plife.jl

quaternion.jl

wordcount.jl

lru.jl

typetree.jl

hpl.jl

Julia is a high-level, high-performance dynamic programming language for technical computing. This site is a non official series of examples of Julia, for more details see about.quine.jlThis script prints a string identical to it's own source code, see here.github.com/karbarcca/Rosetta-Julia/blob/master/src/Quine.jl: In Julia, $x in a string literal interpolates the value of the variable into the string. $(expression) evaluates the expression and interpolates the result into the string. Normally, the string value "hi\tworld" would be inserted without quotation marks and with a literal tabThe repr function returns a string value that contains quotation marks and in which the literal tab is replaced by the characters \t. When the result of the repr function is interpolated, the result is what you would type into your code to create that string literal.Github Link x="println(\"x=\$(repr(x))\\n\$x\")"println("x=$(repr(x))\n$x")bubblesort.jlGithub Link import Base.Sortimmutable BubbleSortAlg 0print(io, "$(x.v*10^r) $(prefix)seconds")elseprint(io, "$(x.v/10^-r) $(prefix)seconds")endend

convert{p,q}(::Type{TimeDelta{p}}, x::TimeDelta{q}) =TimeDelta{p}(p Array(T, sz))s = 1for i=1:na = out[i]::Arrayv = vs[i]snext = s*size(a,i)ndgrid_fill(a, v, s, snext)s = snextendoutend

meshgrid(v::AbstractVector) = meshgrid(v, v)

function meshgrid{T}(vx::AbstractVector{T}, vy::AbstractVector{T})m, n = length(vy), length(vx)vx = reshape(vx, 1, n)vy = reshape(vy, m, 1)(repmat(vx, m, 1), repmat(vy, 1, n))end

function meshgrid{T}(vx::AbstractVector{T}, vy::AbstractVector{T},vz::AbstractVector{T})m, n, o = length(vy), length(vx), length(vz)vx = reshape(vx, 1, n, 1)vy = reshape(vy, m, 1, 1)vz = reshape(vz, 1, 1, o)om = ones(Int, m)on = ones(Int, n)oo = ones(Int, o)(vx[om, :, oo], vy[:, on, oo], vz[om, on, :])endlru_test.jlGithub Link using LRUExample

TestLRU = LRUExample.UnboundedLRU{ASCIIString, ASCIIString}()TestBLRU = LRUExample.BoundedLRU{ASCIIString, ASCIIString}(1000)

get_str(i) = ascii(vcat(map(x->[x>>4; x&0x0F], reinterpret(Uint8, [int32(i)]))...))

isbounded{Ln==:maxsize, L.names))isbounded{L= lru.maxsizetailstr = get_str(i-lru.maxsize+1)@assert lru.q[end].v == tailstrendend#println("pass")

#print(" Lookup, random access: ")for i in 1:nstr = get_str(rand(1:n))if haskey(lru, str) # the bounded LRUs can have cache missesblah = lru[str]@assert lru.q[1].v == blahendend#println("pass")endempty!(lru)endend

lrutest()staged.jlGithub Link function add_method(gf, an, at, body)argexs = { Expr(symbol("::"), an[i], at[i]) for i=1:length(an) }def = quotelet __F__=($gf)function __F__($(argexs...))$bodyendendendeval(def)end

macro staged(fdef)if !isa(fdef,Expr) || !is(fdef.head,:function)error("@staged: expected method definition")endfname = fdef.args[1].args[1]argspec = fdef.args[1].args[2:end]argnames = map(x->(isa(x,Expr) ? x.args[1] : x), argspec)qargnames = map(x->Expr(:quote,x), argnames)fbody = fdef.args[2]@gensym gengf argtypes expander genbodyquotelet ($gengf)global ($fname) # should be "outer"local ($expander)function ($expander)($(argnames...))$fbodyend($gengf)() = 0 # should be initially empty GFfunction ($fname)($(argspec...))($argtypes) = typeof(tuple($(argnames...)))if !method_exists($gengf, $argtypes)($genbody) = apply(($expander), ($argtypes))add_method($gengf, {$(qargnames...)},$argtypes, $genbody)endreturn ($gengf)($(argnames...))endendendend

# example

@staged function nloops(dims::Tuple)names = map(x->gensym(), dims)ex = quoteprintln([$(names...)])endfor i = 1:length(dims)ex = quotefor $(names[i]) in dims[$i]$exendendendexendplife.jlGithub Link function life_rule(old)m, n = size(old)new = similar(old, m-2, n-2)for j = 2:n-1for i = 2:m-1nc = +(old[i-1,j-1], old[i-1,j], old[i-1,j+1],old[i ,j-1], old[i ,j+1],old[i+1,j-1], old[i+1,j], old[i+1,j+1])

new[i-1,j-1] = (nc == 3 ? 1 :nc == 2 ? old[i,j] :0)endendnewend

function life_step(d)DArray(size(d),procs(d)) do I# fetch neighborhood - toroidal boundariestop = mod(first(I[1])-2,size(d,1))+1bot = mod( last(I[1]) ,size(d,1))+1left = mod(first(I[2])-2,size(d,2))+1right = mod( last(I[2]) ,size(d,2))+1

old = Array(Bool, length(I[1])+2, length(I[2])+2)@sync begin@async old[1 , 1 ] = d[top , left] # left side@async old[2:end-1, 1 ] = d[I[1], left]@async old[end , 1 ] = d[bot , left]@async old[1 , 2:end-1] = d[top , I[2]]@async old[2:end-1, 2:end-1] = d[I[1], I[2]] # middle@async old[end , 2:end-1] = d[bot , I[2]]@async old[1 , end ] = d[top , right] # right side@async old[2:end-1, end ] = d[I[1], right]@async old[end , end ] = d[bot , right]end

life_rule(old)endend

function plife(m, n)w = Window("parallel life", n, m)c = Canvas(w)pack(c)done = falsec.mouse.button1press = (c,x,y)->(done=true)cr = getgc(c)

grid = DArray(I->convert(Array{Bool,2},randbool(map(length,I))), (m, n), workers())

last = time(); f = 1while !done@async beginimg = convert(Array{Uint32,2},grid) .* 0x00ffffffset_source_surface(cr, CairoRGBSurface(img), 0, 0)paint(cr)reveal(c)endt = time()if t-last > 2println("$(f/(t-last)) FPS")last = t; f = 0endgrid = life_step(grid)f += 1sleep(0.01)endendquaternion.jlGithub Link module Quaternions

import Base: convert, promote_rule, show, real, imag, conj, abs, abs2, inv, +, -, /, *

immutable Quaternion{T ...(define text)...# julia> counts=parallel_wordcount(text)## Or to run on a group of files, writing results to an output file:# julia -p # julia> require("/examples/wordcount.jl")# julia> wordcount_files("/tmp/output.txt", "/tmp/input1.txt","/tmp/input2.txt",...)

# "Map" function.# Takes a string. Returns a Dict with the number of times each word # appears in that string.function wordcount(text)words=split(text,[' ','\n','\t','-','.',',',':',';'],false)counts=Dict()for w = wordscounts[w]=get(counts,w,0)+1endreturn countsend

# "Reduce" function.# Takes a collection of Dicts in the format returned by wordcount()# Returns a Dict in which words that appear in multiple inputs# have their totals added together.function wcreduce(wcs)counts=Dict()for c in wcs, (k,v) in ccounts[k] = get(counts,k,0)+vendreturn countsend

# Splits input string into nprocs() equal-sized chunks (last one rounds up), # and @spawns wordcount() for each chunk to run in parallel. Then fetch()s# results and performs wcreduce().function parallel_wordcount(text)lines=split(text,'\n',false)np=nprocs()unitsize=ceil(length(lines)/np)wcounts={}rrefs={}# spawn procsfor i=1:npfirst=unitsize*(i-1)+1last=unitsize*iif last>length(lines)last=length(lines)endsubtext=join(lines[int(first):int(last)],"\n")push!(rrefs, @spawn wordcount( subtext ) )end# fetch resultswhile length(rrefs)>0push!(wcounts,fetch(pop!(rrefs)))end# reducecount=wcreduce(wcounts)return countend

# Takes the name of a result file, and a list of input file names.# Combines the contents of all files, then performs a parallel_wordcount# on the resulting string. Writes the results to result_file.function wordcount_files(result_file,inputs...)text = ""for file in inputsopen(file) do ftext *= readall(f)endendwc = parallel_wordcount(text)open(result_file,"w") do ffor (k,v) in wcprintln(f, k,"=",v)endendendlru.jlGithub Link module LRUExample# An LRU (Least Recently Used) cache is an associative data structure which# maintains its contents in an order such that the most recently used item# is at the beginning of the structure, and the least recently used at the end.## This file specifies two types of LRU caches, both with and without a size# limit. BoundedLRU has a limit and evicts the LRU item if a new item is added# after that bound is reached. UnboundedLRU does not have a maximum size, but# can be used as a basis for more complex LRUs.## LRUs should follow the interfaces for general collections, indexable# collections, and associative collections.

# The standard implementation of an LRU backs a hash table with a doubly-linked# list for O(1) operations when reordering on access and eviction. The Julia# implementation instead backs the table with a Vector. For moderately-sized# collections, the difference in performance is small, and this implmentation# is simpler and easier to understand.

import Base.isempty, Base.length, Base.sizeofimport Base.start, Base.next, Base.doneimport Base.haskey, Base.getimport Base.setindex!, Base.getindex, Base.delete!, Base.empty!import Base.show

abstract LRU{K,V}