Julia

Julia je novi jezik, nastao 2012. godine. Julia je dinamički jezik koji se može kompajlirati te dobiti vrlo dobre performanse (često u rangu C-a). Ima sličnosti s Pythonom, ali i s Lispom. Prvenstvena namjena mu je znanstveno računanje, ali dizajniran je tako da i u domeni nespecijaliziranih programskih jezika može naći svoje mjesto.

In [1]:
VERSION
Out[1]:
v"0.5.0"
In [2]:
f(x, y) = 1 + 2x*y^2
Out[2]:
f (generic function with 1 method)
In [3]:
f(1,4)
Out[3]:
33
In [4]:
α=3
Out[4]:
3
In [5]:
println("α=", α)
α=3
In [6]:
μ=3
println("Sinus od  je $(sin(μ))")
Sinus od 3 je 0.1411200080598672
In [7]:
10^19
Out[7]:
-8446744073709551616
In [8]:
BigInt(10)^19
Out[8]:
10000000000000000000
In [9]:
a = Int8(1)
b = Int8(2)
a + b
Out[9]:
3
In [10]:
typeof(ans)
Out[10]:
Int8
In [11]:
typemax(Int64)
Out[11]:
9223372036854775807
In [12]:
c = 1+3.5im
Out[12]:
1.0 + 3.5im
In [13]:
c^2
Out[13]:
-11.25 + 7.0im
In [14]:
c.re, c.im
Out[14]:
(1.0,3.5)
In [15]:
3//4
Out[15]:
3//4
In [16]:
typeof(ans)
Out[16]:
Rational{Int64}
In [17]:
//(3, 4)
Out[17]:
3//4
In [18]:
//
Out[18]:
// (generic function with 7 methods)
In [19]:
methods(//)
Out[19]:
7 methods for generic function //:
In [20]:
l = [3, 4, 5]
Out[20]:
3-element Array{Int64,1}:
 3
 4
 5
In [21]:
l[1]
Out[21]:
3
In [22]:
l[1:2]
Out[22]:
2-element Array{Int64,1}:
 3
 4
In [23]:
l[2:end]
Out[23]:
2-element Array{Int64,1}:
 4
 5
In [24]:
l[1:end-1]
Out[24]:
2-element Array{Int64,1}:
 3
 4
In [25]:
a = [1.1, 2.2, 3.3]
b = [4.4, 5.5, 6.6]
Out[25]:
3-element Array{Float64,1}:
 4.4
 5.5
 6.6
In [26]:
dot(a,b)
Out[26]:
38.72
In [27]:
(a,b)
Out[27]:
38.72
In [28]:
a.*b
Out[28]:
3-element Array{Float64,1}:
  4.84
 12.1 
 21.78
In [29]:
norm(a)
Out[29]:
4.115823125451335
In [30]:
a⋅b
Out[30]:
38.72
In [31]:
a×b
Out[31]:
3-element Array{Float64,1}:
 -3.63
  7.26
 -3.63
In [32]:
Ψ = norm
Out[32]:
norm (generic function with 9 methods)
In [33]:
Ψ(a)
Out[33]:
4.115823125451335
In [34]:
?norm
search: norm normpath normalize normalize! normalize_string vecnorm issubnormal

Out[34]:
norm(A, [p])

Compute the p-norm of a vector or the operator norm of a matrix A, defaulting to the p=2-norm.

For vectors, p can assume any numeric value (even though not all values produce a mathematically valid vector norm). In particular, norm(A, Inf) returns the largest value in abs(A), whereas norm(A, -Inf) returns the smallest.

For matrices, the matrix norm induced by the vector p-norm is used, where valid values of p are 1, 2, or Inf. (Note that for sparse matrices, p=2 is currently not implemented.) Use vecnorm to compute the Frobenius norm.

In [35]:
suma = 0
for i = 1:10
    suma += i
end
println("Suma je $suma")
Suma je 55
In [36]:
kvadrati = [i^2 for i in [1:2:10; 7]]
Out[36]:
6-element Array{Int64,1}:
  1
  9
 25
 49
 81
 49
In [37]:
M = [2 1; 1 1]
Out[37]:
2×2 Array{Int64,2}:
 2  1
 1  1
In [38]:
M[:,1]
Out[38]:
2-element Array{Int64,1}:
 2
 1
In [39]:
rand()
Out[39]:
0.194052272230967
In [40]:
v = [1, 2]
Out[40]:
2-element Array{Int64,1}:
 1
 2
In [41]:
M*v
Out[41]:
2-element Array{Int64,1}:
 4
 3
In [42]:
dot(v,v)
Out[42]:
5
In [43]:
M = rand(100, 100)
Out[43]:
100×100 Array{Float64,2}:
 0.823685  0.959533   0.914166   …  0.634468   0.55875    0.959321 
 0.888054  0.517168   0.98111       0.767699   0.0826222  0.313916 
 0.526687  0.452331   0.0186533     0.0750501  0.950221   0.484281 
 0.905193  0.477754   0.666308      0.119242   0.748496   0.510197 
 0.552254  0.845979   0.0635522     0.206468   0.405423   0.0730754
 0.705777  0.759756   0.78426    …  0.64529    0.586206   0.902311 
 0.418558  0.349762   0.314765      0.442339   0.635466   0.157412 
 0.284421  0.396884   0.878606      0.218935   0.503469   0.309289 
 0.985913  0.545452   0.405668      0.494811   0.297475   0.961027 
 0.852446  0.388494   0.671127      0.275561   0.526394   0.730511 
 0.109036  0.497742   0.316859   …  0.847041   0.435709   0.434643 
 0.14566   0.0323608  0.874718      0.686789   0.065643   0.880529 
 0.439728  0.192207   0.0268152     0.473984   0.741708   0.937173 
 ⋮                               ⋱                                 
 0.994145  0.799665   0.13128       0.946506   0.79288    0.639039 
 0.752141  0.707233   0.288864      0.691256   0.343201   0.0835424
 0.788981  0.0655596  0.262611   …  0.163575   0.36919    0.369076 
 0.203687  0.804314   0.487234      0.342588   0.184886   0.262782 
 0.449541  0.652579   0.0511055     0.165664   0.889631   0.408358 
 0.8354    0.584587   0.249332      0.380268   0.346006   0.719066 
 0.863734  0.436482   0.242159      0.979589   0.22725    0.840324 
 0.720361  0.284384   0.511806   …  0.761623   0.771165   0.849925 
 0.592886  0.905295   0.412045      0.360163   0.890909   0.644862 
 0.133634  0.384939   0.083474      0.528835   0.0508042  0.145624 
 0.97755   0.418905   0.169726      0.101553   0.657945   0.5839   
 0.381275  0.711899   0.174598      0.927804   0.690078   0.249492 
In [44]:
@time lamb, vv = eig(M)
  0.660276 seconds (405.51 k allocations: 17.288 MB, 1.26% gc time)
Out[44]:
(Complex{Float64}[49.7051+0.0im,2.80627+0.901813im,2.80627-0.901813im,1.32752+2.54612im,1.32752-2.54612im,2.90142+0.0im,-2.91262+0.0524949im,-2.91262-0.0524949im,1.9868+1.79619im,1.9868-1.79619im  …  -0.794771-0.174447im,0.644836+0.419374im,0.644836-0.419374im,0.214759+0.0im,-0.577777+0.495315im,-0.577777-0.495315im,0.282011+0.582889im,0.282011-0.582889im,0.194124+0.168054im,0.194124-0.168054im],
Complex{Float64}[-0.0986176+0.0im 0.055689-0.0109681im … -0.00190763+0.0860034im -0.00190763-0.0860034im; -0.097162+0.0im 0.00428767+0.00225751im … 0.017157-0.00893366im 0.017157+0.00893366im; … ; -0.0991484+0.0im -0.038184+0.0420319im … -0.0170382+0.0517033im -0.0170382-0.0517033im; -0.110852+0.0im 0.0604364+0.101726im … -0.00615236-0.136541im -0.00615236+0.136541im])
In [45]:
lu(M)
Out[45]:
(
[1.0 0.0 … 0.0 0.0; 0.205062 1.0 … 0.0 0.0; … ; 0.15923 -0.00455682 … 1.0 0.0; 0.996428 -0.45928 … 0.0728867 1.0],

[0.994155 0.743064 … 0.196954 0.523056; 0.0 0.826546 … 0.361956 0.753536; … ; 0.0 0.0 … -5.56871 -1.357; 0.0 0.0 … 0.0 -0.714286],

Int32[21,64,81,17,79,58,30,16,41,19  …  32,82,51,80,36,2,100,22,73,50])
In [46]:
norm(M)
Out[46]:
49.90371178351387
In [47]:
function normakvadrata(M)
    norm(M^2)
end
Out[47]:
normakvadrata (generic function with 1 method)
In [48]:
normakvadrata(M)
Out[48]:
2480.458037303798
In [49]:
Base.:+(s1::AbstractString, s2::AbstractString) = string(s1, s2)
In [50]:
"Prvi" + " drugi"
Out[50]:
"Prvi drugi"
In [51]:
"Vrijednost od x je " + 3
MethodError: no method matching +(::String, ::Int64)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:138
  +(!Matched::Complex{Bool}, ::Real) at complex.jl:151
  +(!Matched::Char, ::Integer) at char.jl:40
  ...
In [52]:
Base.:+(s::AbstractString, x::Number) = s + "$(2x)"
In [53]:
"Vrijednost od x je " + 3
Out[53]:
"Vrijednost od x je 6"
In [54]:
immutable Vector2D
    x::Float64
    y::Float64
end
In [55]:
v = Vector2D(3, 4);
w = Vector2D(5, 6);
In [56]:
v + w
MethodError: no method matching +(::Vector2D, ::Vector2D)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:138
In [57]:
Base.:+(v::Vector2D, w::Vector2D) = Vector2D(v.x+w.x, v.y+w.y)
In [58]:
v+w
Out[58]:
Vector2D(8.0,10.0)
In [59]:
Base.:*(v::Vector2D, α::Number) = Vector2D(v.x*α, v.y*α)
Base.:*(α::Number, v::Vector2D) = Vector2D(v.x*α, v.y*α)
In [60]:
v * 3.5
Out[60]:
Vector2D(10.5,14.0)
In [61]:
3.5 * v
Out[61]:
Vector2D(10.5,14.0)
In [62]:
methods(+)
Out[62]:
166 methods for generic function +:
In [63]:
function sum1(N::Int)
    total = 0
    
    for i in 1:N
        total += i/2
    end
    
    total
end

function sum2(N::Int)
    total = 0.0
    
    for i in 1:N
        total += i/2
    end
    
    total
end
Out[63]:
sum2 (generic function with 1 method)
In [64]:
sum1(10), sum2(10)
Out[64]:
(27.5,27.5)
In [65]:
N = 10000000

@time sum1(N)
@time sum2(N)
  0.267848 seconds (30.00 M allocations: 457.764 MB, 15.53% gc time)
  0.010801 seconds (5 allocations: 176 bytes)
Out[65]:
2.50000025e13
In [66]:
code_typed(sum2, (Int,))
Out[66]:
1-element Array{Any,1}:
 LambdaInfo for sum2(::Int64)
In [67]:
code_llvm(sum2, (Int, ))
define double @julia_sum2_72290(i64) #0 {
top:
  %1 = icmp slt i64 %0, 1
  br i1 %1, label %L2, label %if.preheader

if.preheader:                                     ; preds = %top
  br label %if

L2.loopexit:                                      ; preds = %if
  br label %L2

L2:                                               ; preds = %L2.loopexit, %top
  %total.0.lcssa = phi double [ 0.000000e+00, %top ], [ %5, %L2.loopexit ]
  ret double %total.0.lcssa

if:                                               ; preds = %if.preheader, %if
  %total.04 = phi double [ %5, %if ], [ 0.000000e+00, %if.preheader ]
  %"#temp#.03" = phi i64 [ %2, %if ], [ 1, %if.preheader ]
  %2 = add i64 %"#temp#.03", 1
  %3 = sitofp i64 %"#temp#.03" to double
  %4 = fmul double %3, 5.000000e-01
  %5 = fadd double %total.04, %4
  %6 = icmp eq i64 %"#temp#.03", %0
  br i1 %6, label %L2.loopexit, label %if
}
In [68]:
code_native(sum2, (Int,))
	.text
Filename: In[63]
	pushq	%rbp
	movq	%rsp, %rbp
	xorpd	%xmm0, %xmm0
	xorl	%eax, %eax
Source line: 14
	testq	%rdi, %rdi
	jle	L56
	movabsq	$140600821612904, %rcx  # imm = 0x7FE02E070168
	movsd	(%rcx), %xmm1           # xmm1 = mem[0],zero
	nopl	(%rax)
Source line: 15
L32:
	incq	%rax
	xorps	%xmm2, %xmm2
	cvtsi2sdq	%rax, %xmm2
	mulsd	%xmm1, %xmm2
	addsd	%xmm2, %xmm0
Source line: 14
	cmpq	%rax, %rdi
	jne	L32
Source line: 18
L56:
	popq	%rbp
	retq
	nopw	(%rax,%rax)
In [69]:
using PyCall
@pyimport numpy.random as nprandom
nprandom.rand(3,4)
Out[69]:
3×4 Array{Float64,2}:
 0.641178   0.344351  0.386636  0.794118 
 0.0545324  0.969383  0.110949  0.453816 
 0.734294   0.963323  0.603344  0.0206484
In [70]:
objective = x -> cos(x) - x #ekvivalent lambda funkcijama u Pythonu
Out[70]:
(::#3) (generic function with 1 method)
In [71]:
objective(1)
Out[71]:
-0.45969769413186023
In [72]:
t = ccall( (:clock, "libc"), Int32, ())
Out[72]:
19913557
In [73]:
function es(n::Int64)
    l = ones(Bool, n)
    l[1] = false
    for i in 2:int64(sqrt(n))
        if l[i]
            for j in (i*i):i:n
                l[j] = false
            end
        end
    end
    return filter(x -> l[x],1:n)
end
Out[73]:
es (generic function with 1 method)