Web-based interface for running Julia (which I'm using right now): install locally on your machine
Free web-based Julia service without installation (Julia may not be up-to-date)
Julia is a new programming language developed from 2009 (public appearance in 2012) by contributers (mostly are from MIT). It is designed for high-performance scientific computing.
It provides an alternative scientific language/platform to Matlab, and very appealing for implementing optimization algorithms: 0. open and free (MIT license)
The core of Julia is written in C/C++ with parsers in Scheme and JIT compilation in the LLVM framework. Most of julia libraries are written in Julia itself.
versioninfo()
Julia Version 0.5.0 Commit 3c9d753 (2016-09-19 18:14 UTC) Platform Info: System: Darwin (x86_64-apple-darwin13.4.0) CPU: Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz WORD_SIZE: 64 BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell) LAPACK: libopenblas64_ LIBM: libopenlibm LLVM: libLLVM-3.7.1 (ORCJIT, haswell)
println("Hello Julia")
Hello Julia
?println
search: println printtable print_with_color print print_joined print_escaped
println(x)
Print (using print
) x
followed by a newline.
a = 3
typeof(a)
Int64
a = "a string"
typeof(a)
String
a = 1e-7
typeof(a)
Float64
1 == true
true
0 == false
true
!true
false
a = -1
if a>0
println("positive")
elseif a<0
println("negative")
else
println("zero")
end
negative
if 1>0
println("true")
end
true
function sphere_vol(r)
return 4/3*pi*r^3
end
WARNING: Method definition sphere_vol(Any) in module Main at In[1]:2 overwritten at In[127]:2.
sphere_vol (generic function with 1 method)
function sphere_vol(r)
4/3*pi*r^3
end
WARNING: Method definition sphere_vol(Any) in module Main at In[127]:2 overwritten at In[128]:2.
sphere_vol (generic function with 1 method)
r = 5
#print("sphere radius=%f, vol=%0.3f\n", r, sphere_vol(r))
@printf "sphere radius=%f, vol=%0.3f\n" r sphere_vol(r)
sphere radius=5.000000, vol=523.599
f(x) = sin(1 / x)
f(1/pi)
WARNING: Method definition f(Any) in module Main at In[16]:2 overwritten at In[130]:1.
1.2246467991473532e-16
quad_root(a,b,c) = ((-b + sqrt(b^2-4a*c))/2a, (-b - sqrt(b^2-4a*c))/2a)
WARNING: Method definition quad_root(Any, Any, Any) in module Main at In[8]:1 overwritten at In[131]:1.
quad_root (generic function with 1 method)
soln = quad_root(2,4,1)
(-0.2928932188134524,-1.7071067811865475)
soln[1]
-0.2928932188134524
soln[end]
-1.7071067811865475
randn(3)
3-element Array{Float64,1}: 0.0630164 -1.16536 0.00876668
map(x -> sin(1 / x), randn(3)) # function without a name, apply a function on the elements of an array
3-element Array{Float64,1}: 0.785616 -0.625924 -0.732419
function f(x, y=2, z=3; a=1, b=.1, c=2) # default values, named arguments
return exp(cos(a * x + b))
end
WARNING: Method definition f(Any) in module Main at In[130]:1 overwritten at In[137]:2. WARNING: Method definition f(Any, Any) in module Main at In[16]:2 overwritten at In[137]:2. WARNING: Method definition f(Any, Any, Any) in module Main at In[16]:2 overwritten at In[137]:2. WARNING: Method definition #f(Array{Any, 1}, Main.#f, Any) in module Main overwritten. WARNING: Method definition #f(Array{Any, 1}, Main.#f, Any, Any) in module Main overwritten. WARNING: Method definition #f(Array{Any, 1}, Main.#f, Any, Any, Any) in module Main overwritten.
f (generic function with 3 methods)
f(1,2,3, c=-5)
1.573962179485784
s = "3.1415"
fval = float(s)
println(5fval)
15.707500000000001
i = parse(Float64,s)
3.1415
c = 'α'
println(c, ", ascii value = ", Int(c))
α, ascii value = 945
str = "This is a string"
show(str[10:13])
" str"
a = "welcome"
b = "julia"
c = 101
println("$a to $b in $c")
welcome to julia in 101
println("1 + 2 = $(1+2)")
1 + 2 = 3
@printf "%d %.1f" c 1.1*c
101 111.1
a = "welcome"
b = "julia"
println(a * b) # string concatenation
welcomejulia
a^3
"welcomewelcomewelcome"
s = "The quick brown fox jumps over the brown dog α,β,γ "
r = search(s, "brown")
11:15
r = replace(s, "brown", "red")
"The quick red fox jumps over the red dog α,β,γ "
r = search(s, r"b[\w]*n")
11:15
r = strip(s)
"The quick brown fox jumps over the brown dog α,β,γ"
r = split("welcome, to, julia", [',', ' '], keep=false)
3-element Array{SubString{String},1}: "welcome" "to" "julia"
?split
search: split splitext splitdir splitdrive rsplit specialized_bitwise_unary
split(string, [chars]; limit=0, keep=true)
Return an array of substrings by splitting the given string on occurrences of the given character delimiters, which may be specified in any of the formats allowed by search
's second argument (i.e. a single character, collection of characters, string, or regular expression). If chars
is omitted, it defaults to the set of all space characters, and keep
is taken to be false
. The two keyword arguments are optional: they are a maximum size for the result and a flag determining whether empty fields should be kept in the result.
a = [1 2 3]
1×3 Array{Int64,2}: 1 2 3
a = [1,2,3]
3-element Array{Int64,1}: 1 2 3
a = [1;2;3]
3-element Array{Int64,1}: 1 2 3
A = [1 2; 3 4]
2×2 Array{Int64,2}: 1 2 3 4
a = Int64[]
push!(a, 4)
push!(a, 5)
push!(a,6)
# show(tmp); println()
show(a)
[4,5,6]
a = [ [1,2,3], [1,2] ]
2-element Array{Array{Int64,1},1}: [1,2,3] [1,2]
a = (Array{Int64, 1})[] # Array of Arrays
push!(a, [1,2,3])
push!(a, [4,5])
2-element Array{Array{Int64,1},1}: [1,2,3] [4,5]
a = [[1,2,3], [4,5]]
2-element Array{Array{Int64,1},1}: [1,2,3] [4,5]
a = [1 2 3]
A = repmat(a, 3, 2)
3×6 Array{Int64,2}: 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
a = ["Hi Im element # $(i+2*(j-1))" for i=1:2, j=1:3]
2×3 Array{String,2}: "Hi Im element # 1" "Hi Im element # 3" "Hi Im element # 5" "Hi Im element # 2" "Hi Im element # 4" "Hi Im element # 6"
a[:,2]
2-element Array{String,1}: "Hi Im element # 3" "Hi Im element # 4"
a[end,:]
3-element Array{String,1}: "Hi Im element # 2" "Hi Im element # 4" "Hi Im element # 6"
a[1:1,:]
1×3 Array{String,2}: "Hi Im element # 1" "Hi Im element # 3" "Hi Im element # 5"
push!([1,2,3], 4, 5, 6)
6-element Array{Int64,1}: 1 2 3 4 5 6
append!([1,2,3], [4,5,6])
6-element Array{Int64,1}: 1 2 3 4 5 6
prepend!([3], [1,2])
3-element Array{Int64,1}: 1 2 3
insert!([1,2,3,4],2,10)
5-element Array{Int64,1}: 1 10 2 3 4
deleteat!([1,2,3,4],2:3)
2-element Array{Int64,1}: 1 4
function foo(arg::Int)
arg_local = arg
arg_local = 2
return (arg_local)
end
arg = 10
out = foo(arg)
(out, arg)
WARNING: Method definition foo(Int64) in module Main at In[76]:2 overwritten at In[172]:2.
(2,10)
function foo!(arg::Array)
arg_local = arg
arg_local[1] = 2
return (arg_local)
end
arg = [10 20]
out = foo!(arg)
(out, arg)
WARNING: Method definition foo!(Array) in module Main at In[77]:2 overwritten at In[173]:2.
( [2 20], [2 20])
A = ones(3)
3-element Array{Float64,1}: 1.0 1.0 1.0
A = ones(3,3)
3×3 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
B = A
A[2,2] = -1
B
3×3 Array{Float64,2}: 1.0 1.0 1.0 1.0 -1.0 1.0 1.0 1.0 1.0
B = copy(A)
A[2,2] = 100
B
3×3 Array{Float64,2}: 1.0 1.0 1.0 1.0 -1.0 1.0 1.0 1.0 1.0
x = "foo", "bar"
typeof(x)
Tuple{String,String}
x = ("foo", "bar")
word1, word2 = x # unpacking
show(word1); println()
show(word2)
"foo" "bar"
x = ("foo", "bar")
x[1]
"foo"
x = ["foo" "bar"]
x[1] = "xxx"
show(x)
String["xxx" "bar"]
d = Dict{Any,Any}(2=>"two", 1=>"one", 4=>"four", 3=>"three")
Dict{Any,Any} with 4 entries: 4 => "four" 2 => "two" 3 => "three" 1 => "one"
# alternatively,
d = Dict(2=>"two", 1=>"one", 4=>"four", 3=>"three")
Dict{Int64,String} with 4 entries: 4 => "four" 2 => "two" 3 => "three" 1 => "one"
show(keys(d)); println()
show(values(d))
[4,2,3,1] String["four","two","three","one"]
for k in sort(collect(keys(d)))
if(k == 1 || k == 4) continue
end
print(k, ": ", d[k], ", ")
end
2: two, 3: three,
show(d[3]); println()
d[3] = "not three"
show(d[3])
"three" "not three"
d[0] = "zero"
"zero"
show(d)
2 in keys(d)
Dict(0=>"zero",4=>"four",2=>"two",3=>"not three",1=>"one")
true
"one" in values(d)
true
animals = ["dog", "cat", "bird"]; # semicolon suppresses output
plurals = [animal * "s" for animal in animals]
3-element Array{String,1}: "dogs" "cats" "birds"
a = [i+j for i=1:2, j=1:3]
2×3 Array{Int64,2}: 2 3 4 3 4 5
Dict("$i" => i for i in 1:3)
Dict{String,Int64} with 3 entries: "1" => 1 "2" => 2 "3" => 3
d = Dict(i => @sprintf("str%d", i) for i=1:10)
Dict{Int64,String} with 10 entries: 7 => "str7" 4 => "str4" 9 => "str9" 10 => "str10" 2 => "str2" 3 => "str3" 5 => "str5" 8 => "str8" 6 => "str6" 1 => "str1"
for i in 1:5 # iteration over a range 1:5, "for i = 1:5" also works
print(i, ", ")
end
1, 2, 3, 4, 5,
for i in collect(1:5) # iteration over an array
print(i, ", ")
end
1, 2, 3, 4, 5,
collect(1:2:10)
5-element Array{Int64,1}: 1 3 5 7 9
a = collect(1:2:10)
while !isempty(a)
print(pop!(a), ", ")
end
9, 7, 5, 3, 1,
countries = ("Japan", "Korea", "China")
cities = ("Tokyo", "Seoul", "Beijing")
for (country, city) in zip(countries, cities) # zip()
println("The capital of $country is $city")
end
The capital of Japan is Tokyo The capital of Korea is Seoul The capital of China is Beijing
countries = ("Japan", "Korea", "China")
cities = ("Tokyo", "Seoul", "Beijing")
for (i, country) in enumerate(countries) # same as above, but with index
city = cities[i]
println("The capital of $country is $city")
end
The capital of Japan is Tokyo The capital of Korea is Seoul The capital of China is Beijing
type Person
name::String
age::Float64
children::Array{String, 1}
# constructor
Person(name::String, age) = new(name, age, [])
Person(name::String, age, children) = new(name, age, children)
end
p1 = Person("Julia", 4)
p2 = Person("Steve", 42, ["Anna", "Bob"])
show(p1); println()
show(p2)
Person("Julia",4.0,String[]) Person("Steve",42.0,String["Anna","Bob"])
people = Person[]
push!(people, Person("Steve", 42))
1-element Array{Person,1}: Person("Steve",42.0,String[])
fname = "benchmarks.csv"
open(fname, "r") do f
for line in eachline(f)
print(line)
end
end
c,fib,0.046968 c,mandel,0.406981 c,parse_int,0.187874 c,pi_sum,46.528101 c,printfd,22.814035 c,quicksort,0.516176 c,rand_mat_mul,236.536026 c,rand_mat_stat,11.954784 fortran,fib,.032889 fortran,mandel,.299340 fortran,parse_int,.917261 fortran,pi_sum,46.140000 fortran,quicksort,.676291 fortran,rand_mat_mul,1119.072000 fortran,rand_mat_stat,13.744000 go,fib,0.10060900000000002 go,mandel,0.404544 go,parse_int,0.708133 go,pi_sum,61.80803100000001 go,quicksort,0.5738630000000001 go,rand_mat_mul,2325.240173 go,rand_mat_stat,106.68269000000001 java,fib,0.042393 java,mandel,0.231601 java,parse_int,1.043216 java,pi_sum,46.616215 java,printfd,119.493350 java,quicksort,0.871698 java,rand_mat_mul,555.709576 java,rand_mat_stat,47.901065 java,sinc_sum,0.155235 javascript,fib,0.175 javascript,mandel,0.632 javascript,parse_int,0.438 javascript,pi_sum,102.1 javascript,quicksort,1.5 javascript,rand_mat_mul,4067 javascript,rand_mat_stat,39.7 julia,fib,0.112414 julia,mandel,0.293425 julia,parse_int,0.362631 julia,pi_sum,49.372756 julia,printfd,45.527162 julia,quicksort,0.640893 julia,rand_mat_mul,261.875685 julia,rand_mat_stat,25.633835 lua,fib,0.112 lua,mandel,0.287 lua,parse_int,1.276 lua,pi_sum,55.045 lua,quicksort,1.217 lua,rand_mat_mul,339.773 lua,rand_mat_stat,51.834 mathematica,fib,7.676 mathematica,mandel,2.54 mathematica,parse_int,3.317 mathematica,pi_sum,70.181 mathematica,quicksort,24.885 mathematica,rand_mat_mul,285.801 mathematica,rand_mat_stat,80.199 matlab,fib,217.85100000 matlab,mandel,20.84800000 matlab,parse_int,296.93900000 matlab,pi_sum,59.23900000 matlab,printfd,2446.41600000 matlab,quicksort,28.09500000 matlab,rand_mat_mul,279.38000000 matlab,rand_mat_stat,125.26700000 octave,fib,458.62197876 octave,mandel,159.23213959 octave,parse_int,1710.93678474 octave,pi_sum,13005.94305992 octave,printfd,2002.40898132 octave,quicksort,911.63611412 octave,rand_mat_mul,295.38583755 octave,rand_mat_stat,429.40402031 python,fib,3.75509262085 python,mandel,4.958152771 python,parse_int,2.30002403259 python,pi_sum,787.712097168 python,quicksort,17.1549320221 python,rand_mat_mul,294.181108475 python,rand_mat_stat,227.633953094 r,fib,26.00000000 r,mandel,22.00000000 r,parse_int,10.00000000 r,pi_sum,770.00000000 r,quicksort,132.00000000 r,rand_mat_mul,452.00000000 r,rand_mat_stat,199.00000000
using DataFrames
df = readtable("benchmarks.csv", names=[:language, :benchmark, :time]) # use of quoted (unevaluated) symbol
language | benchmark | time | |
---|---|---|---|
1 | c | mandel | 0.406981 |
2 | c | parse_int | 0.187874 |
3 | c | pi_sum | 46.528101 |
4 | c | printfd | 22.814035 |
5 | c | quicksort | 0.516176 |
6 | c | rand_mat_mul | 236.536026 |
7 | c | rand_mat_stat | 11.954784 |
8 | fortran | fib | 0.032889 |
9 | fortran | mandel | 0.29934 |
10 | fortran | parse_int | 0.917261 |
11 | fortran | pi_sum | 46.14 |
12 | fortran | quicksort | 0.676291 |
13 | fortran | rand_mat_mul | 1119.072 |
14 | fortran | rand_mat_stat | 13.744 |
15 | go | fib | 0.10060900000000002 |
16 | go | mandel | 0.404544 |
17 | go | parse_int | 0.708133 |
18 | go | pi_sum | 61.80803100000001 |
19 | go | quicksort | 0.5738630000000001 |
20 | go | rand_mat_mul | 2325.240173 |
21 | go | rand_mat_stat | 106.68269000000001 |
22 | java | fib | 0.042393 |
23 | java | mandel | 0.231601 |
24 | java | parse_int | 1.043216 |
25 | java | pi_sum | 46.616215 |
26 | java | printfd | 119.49335 |
27 | java | quicksort | 0.871698 |
28 | java | rand_mat_mul | 555.709576 |
29 | java | rand_mat_stat | 47.901065 |
30 | java | sinc_sum | 0.155235 |
⋮ | ⋮ | ⋮ | ⋮ |
benchmarks = readtable("benchmarks.csv", names=[:language, :benchmark, :time])
cdata = benchmarks[benchmarks[:language].== "c", :]
show(cdata) println("")
benchmarks = join(benchmarks, cdata, on=:benchmark)
show(benchmarks)
benchmarks[:time]./= benchmarks[:time_1]
benchmarks = benchmarks[benchmarks[:language] .== "c", :]
LoadError: syntax: extra token "println" after end of expression while loading In[238], in expression starting on line 3
benchmarks
language | benchmark | time | language_1 | time_1 | |
---|---|---|---|---|---|
1 | c | mandel | 0.406981 | c | 0.406981 |
2 | fortran | mandel | 0.29934 | c | 0.406981 |
3 | go | mandel | 0.404544 | c | 0.406981 |
4 | java | mandel | 0.231601 | c | 0.406981 |
5 | javascript | mandel | 0.632 | c | 0.406981 |
6 | julia | mandel | 0.293425 | c | 0.406981 |
7 | lua | mandel | 0.287 | c | 0.406981 |
8 | mathematica | mandel | 2.54 | c | 0.406981 |
9 | matlab | mandel | 20.848 | c | 0.406981 |
10 | octave | mandel | 159.23213959 | c | 0.406981 |
11 | python | mandel | 4.958152771 | c | 0.406981 |
12 | r | mandel | 22.0 | c | 0.406981 |
13 | c | parse_int | 0.187874 | c | 0.187874 |
14 | fortran | parse_int | 0.917261 | c | 0.187874 |
15 | go | parse_int | 0.708133 | c | 0.187874 |
16 | java | parse_int | 1.043216 | c | 0.187874 |
17 | javascript | parse_int | 0.438 | c | 0.187874 |
18 | julia | parse_int | 0.362631 | c | 0.187874 |
19 | lua | parse_int | 1.276 | c | 0.187874 |
20 | mathematica | parse_int | 3.317 | c | 0.187874 |
21 | matlab | parse_int | 296.939 | c | 0.187874 |
22 | octave | parse_int | 1710.93678474 | c | 0.187874 |
23 | python | parse_int | 2.30002403259 | c | 0.187874 |
24 | r | parse_int | 10.0 | c | 0.187874 |
25 | c | pi_sum | 46.528101 | c | 46.528101 |
26 | fortran | pi_sum | 46.14 | c | 46.528101 |
27 | go | pi_sum | 61.80803100000001 | c | 46.528101 |
28 | java | pi_sum | 46.616215 | c | 46.528101 |
29 | javascript | pi_sum | 102.1 | c | 46.528101 |
30 | julia | pi_sum | 49.372756 | c | 46.528101 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
using Gadfly # ggplot2 style plots
p = plot(benchmarks,
x = :language,
y = :time,
color = :benchmark,
Scale.y_log10,
Guide.ylabel(nothing),
Guide.xlabel(nothing),
Theme(
default_point_size = 1mm,
guide_title_position = :left,
colorkey_swatch_shape = :circle,
minor_label_font = "Georgia",
major_label_font = "Georgia",
),
)
draw(SVG(8inch,8inch/golden), p)
import PyPlot # Julia interface to Matplotlib, a powerful python plotting library
x = linspace(0,2*pi,1000); y = sin(3*x + 4*cos(2*x));
PyPlot.plot(x, y, color="red", linewidth=2.0, linestyle="--")
PyPlot.title("A sinusoidally modulated sinusoid")
PyObject <matplotlib.text.Text object at 0x326033b90>
Pkg.available()
1120-element Array{AbstractString,1}: "AbstractDomains" "AbstractTables" "AbstractTrees" "Accumulo" "ACME" "ActiveAppearanceModels" "Actors" "AffineTransforms" "AMD" "AmplNLWriter" "Anasol" "AndorSIF" "AnsiColor" ⋮ "Xpress" "XSim" "XSV" "YAML" "Yelp" "Yeppp" "YT" "ZChop" "ZipFile" "Zlib" "ZMQ" "ZVSimulator"
Pkg.add("Calculus")
Pkg.update()
INFO: Nothing to be done INFO: Updating METADATA... INFO: Computing changes... INFO: No packages to install, update or remove
using Calculus
derivative(x -> sin(x), 1.0)
0.5403023058631036
import Calculus
Calculus.derivative(x -> sin(x), 1.0)
0.5403023058631036
using Images
using ImageMagick
using QuartzImageIO
const ϕ = golden
function foo(z)
c = (φ-2)+(φ-1)im
max = 80
for n = 1:max
abs(z) ≥ 2 && return n-1
z = z^2 + c
end
return max
end
foo(x, y) = foo(x + y*im)
foo_grid(n) =
broadcast(foo,
linspace(-0.5, 1, n)',
linspace(-1, 0.5, n))
convert(Image, foo_grid(500) * (1/80))
WARNING: Method definition foo(Any) in module Main at In[115]:8 overwritten at In[212]:8. WARNING: Method definition foo(Any, Any) in module Main at In[115]:17 overwritten at In[212]:17. WARNING: Method definition foo_grid(Any) in module Main at In[115]:19 overwritten at In[212]:19.
nheads = @time @parallel (+) for i=1:100_000_000
rand(Int)
end
5.330749 seconds (100.15 M allocations: 1.497 GB, 4.23% gc time)
-6056781706871343244
f(x) = x*x
code_llvm(f, (Float64,))
define double @julia_f_77010(double) #0 { top: %1 = fmul double %0, %0 ret double %1 }
WARNING: Method definition f(Any) in module Main at In[137]:2 overwritten at In[214]:1.
code_native(f, (Float64,))
.section __TEXT,__text,regular,pure_instructions Filename: In[214] pushq %rbp movq %rsp, %rbp Source line: 1 mulsd %xmm0, %xmm0 popq %rbp retq nopw (%rax,%rax)
A = rand(3,2)
B = randn(2,5)
AB = A*B
3×5 Array{Float64,2}: -0.912349 -1.40281 0.29597 -0.628987 -0.491421 -0.72536 -0.878047 0.058601 -0.928543 -0.318243 -0.767678 -1.12623 0.208716 -0.627019 -0.396962
size(AB)
(3,5)
A = rand(3,2)
b = rand(2,1)
Ab = A*b
3×1 Array{Float64,2}: 0.645183 0.928688 1.18238
b'*b
1×1 Array{Float64,2}: 1.40362
dot(b,b)
1.4036239092013683
(b'*b).*A
3×2 Array{Float64,2}: 0.758815 0.23668 0.401091 1.39511 1.06222 0.934759
dot(b,b)*A
3×2 Array{Float64,2}: 0.758815 0.23668 0.401091 1.39511 1.06222 0.934759
A = randn(3,2)
B = randn(3,2)
A .< B
A .== B
A .!= B
A ./ B
A + B
!(A .<= B) # boolean not
~(A .<= B) # bitwise not
float(0b01 $ 0b10) # bitwise xor
float("3.4")
3.4
min(A,B)
3×2 Array{Float64,2}: 0.64041 -1.73591 0.785111 1.42473 -0.766384 0.312189
minimum(A)
0.31218851120315066
square(x) = x^2
square (generic function with 1 method)
@vectorize_1arg Number square
square (generic function with 2 methods)
methods(square)
square(rand(3,4))
3×4 Array{Float64,2}: 0.31573 0.949663 0.00937933 0.00480085 0.962154 0.502913 0.719167 0.381129 0.0470367 0.389741 0.0429033 0.683769
A = randn(3,1) .>= 0
B = randn(3,1) .>= 0
3×1 BitArray{2}: true false false
[A B (A & B)] # bitwise and
3×3 BitArray{2}: true true true false false false false false false
a = rand(Bool)
b = rand(Bool)
[a b (a && b)]
1×3 Array{Bool,2}: false true false
A = rand(5,5)
A[A .< .5] = 0
countnz(A) # not constant time operator
A = sparse(A)
#countnz(A) #works, but not recommended
nnz(A) # constant time operation
A = spzeros(3,3)
A = spones(A) # same structure as the input, but nonzero elem => 1.0
A = speye(Int8, 3,2)
A = spdiagm([1 2 3], [-1 0 1])
full(A)
A = sprand(3,4,0.8)
3×4 sparse matrix with 11 Float64 nonzero entries: [1, 1] = 0.320991 [2, 1] = 0.273994 [3, 1] = 0.298613 [1, 2] = 0.145354 [2, 2] = 0.86788 [3, 2] = 0.768581 [1, 3] = 0.41264 [2, 3] = 0.634764 [3, 3] = 0.169205 [1, 4] = 0.53746 [2, 4] = 0.258654
@printf "%d %f" 3 (2.4*.1)
3 0.240000
@time for i=1:100_000_000
rand(Bool)
end
2.350336 seconds (100.00 M allocations: 1.490 GB, 10.73% gc time)
(http://docs.julialang.org/en/release-0.5/manual/noteworthy-differences/)
Students can present few selected topics about features in Julia. Each presentation should be approx. 25 mins. Discuss with the lecturer if you want to take a part.