56*8
x=7
x+1
x^2
typeof(x)
x/3
cos(pi)
typeof(ans)
im^2
typeof(ans)
"Hello" * " world"
typeof(ans)
70//15
true
12 ≠42
2 ∈ [6,3,5]
typeof(ans)
"Like in perl you may write x=$x"
"... but also $(exp(x))"
The type hierarchy for numbers looks like:
One can define functions simply like that:
f(x) = x ^ 3
f(3)
f("ha ")
function f(x::AbstractString)
println("\nnow in f(x::AbstractString) with x=$x of type $(typeof(x))")
"Hello $x"
end
function f(x::AbstractFloat)
println("\nnow in f(x::AbstractFloat) with x=$x of type $(typeof(x))")
x+1.0
end
methods(f)
@show f(2.5);
@show f( Float16(2.5) );
@show f("abc")
@show f("αβγ");
methods(print)
methods(println)
type Point
x::Int
y::Int
name::ASCIIString
end
p = Point(2,3,"Siegfried")
function f(p::Point)
println("\nnow in f(x::Point) with p=$p of type $(typeof(p))")
p.x = p.x + p.y
return p.name
end
methods(f)
f(p)
p
type Line{T}
x0::T
y0::T
x1::T
y1::T
name::ASCIIString
end
L1=Line(23,3,4,44,"Julia")
L2=Line(23.777,3.5,4.1,4.0,"Rolf")
function f{T}(x::Line{T})
println("\nnow in f(x::Line{T}) with x=$x of type $(typeof(x))")
x.name
end
f(L1),f(L2)
A=[1,2,9]
A == 2
A .== 2
A+2.1
B=[1 2 3; 6 7 4; 6 2 1]
C=[ i*j for i in 1:3, j in 1:4 ]
B * A
#B =rand(500,500)
@time eigvals(B)
@time svd(B)
d = Dict( "A"=>6, "B"=> 20)
d["A"]
for (key,value) in d
print("$key ----> $value\n")
end
my_regex = r"[ABC]"
@show ismatch(my_regex,"Foo")
@show my_regex("Bar")
@show my_regex("boo");
my_regex =r"(\d+)g"
match(my_regex,"take 500g sugar")
for i in 1:5
println(i)
end
for i in 1:10
if i % 3 == 0
println(i)
end
end
Lots of packages are available - see http://pkg.julialang.org for a list.
To view all locally installed packages:
Pkg.status()
To add a package (and its dependencies):
Pkg.add("DataFrames")
To update installed packages to the last release:
Pkg.update()
To stay on the top of the master branch:
Pkg.checkout("DataFrames")
#Pkg.generate("someName")
using DataFrames
d = DataFrame(x = 21:30, y = rand(10))
mean(d[:x]), mean(d[:y])
using StatsBase
cov(d[:x], d[:y])
using RDatasets
iris = dataset("datasets", "iris")
@show size(iris)
head(iris)
by(iris, :Species, size)
using DimensionalityReduction
iris_S = convert(Array,DataArray(iris[:,1:4]))
Xpca = pca(iris_S)
using PlotlyJS
using Colors
using Blink
#using Rsvg
#init_notebook()
nms = unique(iris[:Species])
colors = [RGB(0.89, 0.1, 0.1), RGB(0.21, 0.50, 0.72), RGB(0.28, 0.68, 0.3)]
data = GenericTrace[]
for (i, nm) in enumerate(nms)
sc = Xpca.scores[iris[:Species] .== nm, :]
x=sc[:,1]
y=sc[:,2]
trace = scatter(;name=nm, mode="markers",
marker_size=10, marker_color=colors[i], marker_line_width=0,
x=x, y=y)
push!(data, trace)
end
layout = Layout(width=800, height=550, autosize=false, title="Iris dataset",
xaxis=attr(title="PC1"),
yaxis=attr(title="PC2")
)
p1=PlotlyJS.plot(data, layout)
typeof(p1)
#PlotlyJS.savefig(p1, "output_filename.pdf")
function clustering_alpha_shapes()
@eval using DataFrames, RDatasets, Colors
# load data
iris = dataset("datasets", "iris")
nms = unique(iris[:Species])
colors = [RGB(0.89, 0.1, 0.1), RGB(0.21, 0.50, 0.72), RGB(0.28, 0.68, 0.3)]
data = GenericTrace[]
for (i, nm) in enumerate(nms)
df = iris[iris[:Species] .== nm, :]
x=df[:SepalLength]
y=df[:SepalWidth]
z=df[:PetalLength]
trace = scatter3d(;name=nm, mode="markers",
marker_size=3, marker_color=colors[i], marker_line_width=0,
x=x, y=y, z=z)
push!(data, trace)
cluster = mesh3d(;color=colors[i], opacity=0.3, x=x, y=y, z=z)
push!(data, cluster)
end
# notice the nested attrs to create complex JSON objects
layout = Layout(width=800, height=550, autosize=false, title="Iris dataset",
scene=attr(xaxis=attr(gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
showbackground=true,
backgroundcolor="rgb(230, 230,230)"),
yaxis=attr(gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
showbackground=true,
backgroundcolor="rgb(230, 230,230)"),
zaxis=attr(gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
showbackground=true,
backgroundcolor="rgb(230, 230,230)"),
aspectratio=attr(x=1, y=1, z=0.7),
aspectmode = "manual"))
PlotlyJS.plot(data, layout)
end
clustering_alpha_shapes()
using RCall
R"rnorm(10)"
L = 8
@rput L
R"v <- rnorm(L)"
@rget v
v
rcopy(R"t.test(rnorm(1000))")
ccall( (:clock, "libc"), Int32, ())
run(`echo Hello`)
file = "HelloWorld.jl"
run(`cat $file`)
have a look here https://github.com/BioJulia
using Bio.Seq
seq = dna"ACGTTT"
reverse_complement(seq)
seq = dna"ACAGCGTAGCT";
@show approxsearch(seq, dna"AGGG", 0)
@show approxsearch(seq, dna"AGGG", 1)
@show approxsearch(seq, dna"AGGG", 2);
L = 100000000
A = rand(1:1000,L)
B = rand(1:1000,L)
function sum1!(A,B)
for i in 1:length(A)
A[i] = A[i]+B[i]
end
A
end
function sum2!(A,B)
@simd for i in 1:length(A)
@inbounds A[i] = A[i]+B[i]
end
A
end
@time A+=B;
@time sum1!(A,B)
@time sum2!(A,B)
;
procs()
addprocs(2)
procs()
r = remotecall(2, rand, 2, 2)
fetch(r)
#addprocs(fill("ergophobie.molgen.mpg.de",10),dir="/home/arndt",exename="/usr/bin/julia")
nheads() = @parallel (+) for i=1:2000000000
Int(rand(Bool))
end
@time nheads()
using Plots
#plotlyjs()
gr()
#unicodeplots()
Plots.plot(rand(10,5),w=4)
offer python's yield like behavior
function producer(max::Int = 4)
for i in 1:max
produce(i)
end
end
for i in Task(producer)
println(i)
end
for i in Task(() -> producer(2))
println(i)
end