![Page 1: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/1.jpg)
Logo
Introdução à JuliaOficina de Ferramentas
Computacionais
Abel Soares SiqueiraUniversidade Federal do Paraná
30 de Maio de 2015
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 1 / 52
![Page 2: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/2.jpg)
Logo
Introdução
Julia
Alto nível;
Alta performance;
Sintaxe fácil;
Interface com C/Fortran e Python.
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 2 / 52
![Page 3: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/3.jpg)
Logo
Introdução
Mão na massa
$ julia
julia> 2+3
julia> 5*8
julia> 9/2
julia> 2^3
julia> exp(1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 3 / 52
![Page 4: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/4.jpg)
Logo
Introdução
Mão na massa
julia> round(exp(1))
julia> floor(exp(1))
julia> ceil(exp(1))
julia> div(13,4)
julia> rem(13,4)
julia> mod(13,4)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 4 / 52
![Page 5: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/5.jpg)
Logo
Introdução
Hierarquia de tipo
Number
Real Complex
Integer FloatingPoint
Signed Unsigned
Float16
Float32
Float64
Int8
Int16
Int32
Int64
Int128
Uint8
Uint16
Uint32
Uint64
Uint128
Bool
Char
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 5 / 52
![Page 6: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/6.jpg)
Logo
Introdução
Hierarquia de tipo
Number
Real Complex
Integer FloatingPoint
Signed Unsigned
Float16
Float32
Float64
Int8
Int16
Int32
Int64
Int128
Uint8
Uint16
Uint32
Uint64
Uint128
Bool
Char
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 5 / 52
![Page 7: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/7.jpg)
Logo
Introdução
Hierarquia de tipo
Number
Real Complex
Integer FloatingPoint
Signed Unsigned
Float16
Float32
Float64
Int8
Int16
Int32
Int64
Int128
Uint8
Uint16
Uint32
Uint64
Uint128
Bool
Char
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 5 / 52
![Page 8: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/8.jpg)
Logo
Introdução
Hierarquia de tipo
Number
Real Complex
Integer FloatingPoint
Signed Unsigned
Float16
Float32
Float64
Int8
Int16
Int32
Int64
Int128
Uint8
Uint16
Uint32
Uint64
Uint128
Bool
Char
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 5 / 52
![Page 9: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/9.jpg)
Logo
Introdução
Hierarquia de tipo
Number
Real Complex
Integer FloatingPoint
Signed Unsigned
Float16
Float32
Float64
Int8
Int16
Int32
Int64
Int128
Uint8
Uint16
Uint32
Uint64
Uint128
Bool
Char
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 5 / 52
![Page 10: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/10.jpg)
Logo
Introdução
julia> round(2.3)
julia> iround(2.3)
julia> # Na v0.4 iround não existe mais
julia> # Use round(Int, 2.3)
julia> typeof(2)
julia> typeof(2.0)
julia> 2 == 2.0
julia> isapprox(exp(1), 2.71828)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 6 / 52
![Page 11: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/11.jpg)
Logo
Introdução
HELP
julia> help(exp)
julia> ?exp
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 7 / 52
![Page 12: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/12.jpg)
Logo
Introdução
test1.jl2+3
println (3+4)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 8 / 52
![Page 13: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/13.jpg)
Logo
Introdução
$ julia test1.jl
julia> include("test1.jl")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 9 / 52
![Page 14: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/14.jpg)
Logo
Vetores e matrizes
julia> rand(3)
julia> A = rand(3,3)
julia> e = ones(3)
julia> b = A*e
julia> x = A\b
julia> norm(x-e)
julia> x[1]
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 10 / 52
![Page 15: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/15.jpg)
Logo
Vetores e matrizes
julia> rand(3)
julia> A = rand(3,3)
julia> e = ones(3)
julia> b = A*e
julia> x = A\b
julia> norm(x-e)
julia> x[1]
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 10 / 52
![Page 16: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/16.jpg)
Logo
Vetores e matrizes
julia> rand(3)
julia> A = rand(3,3)
julia> e = ones(3)
julia> b = A*e
julia> x = A\b
julia> norm(x-e)
julia> x[1]
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 10 / 52
![Page 17: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/17.jpg)
Logo
Vetores e matrizes
julia> rand(3)
julia> A = rand(3,3)
julia> e = ones(3)
julia> b = A*e
julia> x = A\b
julia> norm(x-e)
julia> x[1]
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 10 / 52
![Page 18: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/18.jpg)
Logo
Vetores e matrizes
julia> A = rand(3,5)
julia> A[2,3] = 0.0
julia> A
julia> (m,n) = size(A) # Ou m,n = size(A)
julia> A’*A
julia> ones(3,5)
julia> zeros(3,5)
julia> eye(3,5)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 11 / 52
![Page 19: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/19.jpg)
Logo
Vetores e matrizes
julia> A = rand(3,5)
julia> A[2,3] = 0.0
julia> A
julia> (m,n) = size(A) # Ou m,n = size(A)
julia> A’*A
julia> ones(3,5)
julia> zeros(3,5)
julia> eye(3,5)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 11 / 52
![Page 20: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/20.jpg)
Logo
Vetores e matrizes
julia> A = rand(3,5)
julia> A[2,3] = 0.0
julia> A
julia> (m,n) = size(A) # Ou m,n = size(A)
julia> A’*A
julia> ones(3,5)
julia> zeros(3,5)
julia> eye(3,5)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 11 / 52
![Page 21: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/21.jpg)
Logo
Vetores e matrizes
julia> (L,U,P) = lu(A)
julia> norm(L*U-A[P,:])
julia> G = chol(A*A’)
julia> norm(G’*G-A*A’)
julia> (U,S,V) = svd(A)
julia> norm(U*diagm(S)*V’-A)
julia> (Q,R) = qr(A)
julia> norm(Q*R-A)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 12 / 52
![Page 22: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/22.jpg)
Logo
Vetores e matrizes
julia> (L,U,P) = lu(A)
julia> norm(L*U-A[P,:])
julia> G = chol(A*A’)
julia> norm(G’*G-A*A’)
julia> (U,S,V) = svd(A)
julia> norm(U*diagm(S)*V’-A)
julia> (Q,R) = qr(A)
julia> norm(Q*R-A)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 12 / 52
![Page 23: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/23.jpg)
Logo
Vetores e matrizes
julia> (L,U,P) = lu(A)
julia> norm(L*U-A[P,:])
julia> G = chol(A*A’)
julia> norm(G’*G-A*A’)
julia> (U,S,V) = svd(A)
julia> norm(U*diagm(S)*V’-A)
julia> (Q,R) = qr(A)
julia> norm(Q*R-A)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 12 / 52
![Page 24: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/24.jpg)
Logo
Vetores e matrizes
julia> (L,U,P) = lu(A)
julia> norm(L*U-A[P,:])
julia> G = chol(A*A’)
julia> norm(G’*G-A*A’)
julia> (U,S,V) = svd(A)
julia> norm(U*diagm(S)*V’-A)
julia> (Q,R) = qr(A)
julia> norm(Q*R-A)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 12 / 52
![Page 25: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/25.jpg)
Logo
Vetores e matrizes
Cuidado com matrizes disfarçadas
julia> [1;2;3] # Array
julia> [1,2,3] # Array
julia> [1 2 3] # Array 1x3
julia> [1 2 3]’ # Array 3x1
julia> [1;2;3] == [1,2,3]
julia> [1 2 3]’ == [1,2,3]
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 13 / 52
![Page 26: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/26.jpg)
Logo
Vetores e matrizes
Cuidado com matrizes disfarçadas
julia> [1;2;3] # Array
julia> [1,2,3] # Array
julia> [1 2 3] # Array 1x3
julia> [1 2 3]’ # Array 3x1
julia> [1;2;3] == [1,2,3]
julia> [1 2 3]’ == [1,2,3]
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 13 / 52
![Page 27: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/27.jpg)
Logo
Controle de fluxo
if, elseif, else
if ALGO
CMDs
elseif OUTRO ALGO
CMDs
else
CMDs
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 14 / 52
![Page 28: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/28.jpg)
Logo
Controle de fluxo
ifelse.jlif x > 0
println (" positivo ")
elseif x == 0
println ("zero")
else
println (" negativo ")
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 15 / 52
![Page 29: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/29.jpg)
Logo
Controle de fluxo
julia> x = 1
julia> include("ifelse.jl")
julia> x = 0
julia> include("ifelse.jl")
julia> x = -1
julia> include("ifelse.jl")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 16 / 52
![Page 30: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/30.jpg)
Logo
Controle de fluxo
while
while ALGO
CMDs
end
while.jlwhile n != 1
println ("n = ", n)
if n % 2 == 0
n = n/2
else
n = 3*n+1
end
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 17 / 52
![Page 31: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/31.jpg)
Logo
Controle de fluxo
julia> n = 3
julia> include("while.jl")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 18 / 52
![Page 32: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/32.jpg)
Logo
Controle de fluxo
for
for VAR in RANGE
CMDs
end
for VAR = RANGE
CMDs
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 19 / 52
![Page 33: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/33.jpg)
Logo
Controle de fluxo
for.jlfor n in [1 10 100 200]
println (" log10($n) = ", log10(n))
end
for i = 1:10
println ("$i^2 = $(i^2)")
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 20 / 52
![Page 34: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/34.jpg)
Logo
Controle de fluxo
julia> include("for.jl")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 21 / 52
![Page 35: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/35.jpg)
Logo
Controle de fluxo
julia> for i = 2:3:15
println("$i")
end
julia> typeof(1:10)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 22 / 52
![Page 36: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/36.jpg)
Logo
Controle de fluxo
Comparações curtas
julia> 2 > 0
julia> 2 > 0 && println("ok")
julia> 2 < 0 && println("ok")
julia> error()
julia> 2 > 0 && error()
julia> 2 > 0 || println("ok")
julia> 2 < 0 || println("ok")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 23 / 52
![Page 37: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/37.jpg)
Logo
Controle de fluxo
Comparações curtas
julia> 1 > 0 ? println("ok") : println("not")
julia> 1 < 0 ? println("ok") : println("not")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 24 / 52
![Page 38: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/38.jpg)
Logo
Funções
ax2 + bx+ c = 0
x =−b±
√b2 − 4ac
2a
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 25 / 52
![Page 39: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/39.jpg)
Logo
Funções
bhas.jlfunction bhaskara(a, b, c)
D = b^2 - 4*a*c;
D = sqrt(D);
return (-b+D)/(2*a), (-b-D)/(2*a)
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 26 / 52
![Page 40: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/40.jpg)
Logo
Funções
julia> include("bhas.jl")
julia> bhaskara(1,5,6)
julia> r = bhaskara(1,5,6)
julia> r[1]
julia> r1,r2 = bhaskara(1,5,6)
julia> r1
julia> bhaskara(1,0,1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 27 / 52
![Page 41: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/41.jpg)
Logo
Funções
julia> include("bhas.jl")
julia> bhaskara(1,5,6)
julia> r = bhaskara(1,5,6)
julia> r[1]
julia> r1,r2 = bhaskara(1,5,6)
julia> r1
julia> bhaskara(1,0,1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 27 / 52
![Page 42: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/42.jpg)
Logo
Funções
julia> include("bhas.jl")
julia> bhaskara(1,5,6)
julia> r = bhaskara(1,5,6)
julia> r[1]
julia> r1,r2 = bhaskara(1,5,6)
julia> r1
julia> bhaskara(1,0,1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 27 / 52
![Page 43: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/43.jpg)
Logo
Funções
julia> include("bhas.jl")
julia> bhaskara(1,5,6)
julia> r = bhaskara(1,5,6)
julia> r[1]
julia> r1,r2 = bhaskara(1,5,6)
julia> r1
julia> bhaskara(1,0,1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 27 / 52
![Page 44: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/44.jpg)
Logo
Funções
bhas.jlfunction bhaskara(a, b, c)
D = b^2 - 4*a*c;
if D >= 0
D = sqrt(D);
else
D = im*sqrt(-D);
end
return (-b+D)/(2*a), (-b-D)/(2*a)
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 28 / 52
![Page 45: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/45.jpg)
Logo
Funções
julia> include("bhas.jl")
julia> bhaskara(1,0,-1)
julia> bhaskara(1,0,1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 29 / 52
![Page 46: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/46.jpg)
Logo
Funções
bhas.jlfunction bhaskara(a, b, c)
D = b^2 - 4*a*c;
D = D >= 0 ? sqrt(D) : im*sqrt(-D)
return (-b+D)/(2*a), (-b-D)/(2*a)
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 30 / 52
![Page 47: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/47.jpg)
Logo
Funções
‖A‖F =
√√√√ m∑i=1
n∑j=1
a2i,j
normFfunction normF(A:: Matrix)
(m,n) = size(A);
s = 0.0
for i = 1:m
for j = 1:n
s += A[i,j]^2
end
end
return sqrt(s)
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 31 / 52
![Page 48: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/48.jpg)
Logo
Funções
Exemplo: calculando a raiz de a > 1
f(x) = x2 − a
√a
x y
x+ y
2
x y
x+ y
2
xy
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 32 / 52
![Page 49: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/49.jpg)
Logo
Funções
Exemplo: calculando a raiz de a > 1
f(x) = x2 − a
√a
x y
x+ y
2
x y
x+ y
2
xy
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 32 / 52
![Page 50: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/50.jpg)
Logo
Funções
Exemplo: calculando a raiz de a > 1
f(x) = x2 − a
√a
x y
x+ y
2
x y
x+ y
2
xy
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 32 / 52
![Page 51: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/51.jpg)
Logo
Funções
Exemplo: calculando a raiz de a > 1
f(x) = x2 − a
√a
x y
x+ y
2
x y
x+ y
2
xy
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 32 / 52
![Page 52: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/52.jpg)
Logo
Funções
Exemplo: calculando a raiz de a > 1
f(x) = x2 − a
√a
x y
x+ y
2
x y
x+ y
2
xy
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 32 / 52
![Page 53: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/53.jpg)
Logo
Funções
bissec.jl - v1function bissec (a)
x = 1
y = a
m = (x+y)/2
while abs(m^2-a) > 1e-4
if m^2 > a
y = m
else
x = m
end
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 54: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/54.jpg)
Logo
Funções
bissec.jl - v2function bissec (a)
a < 1 && error ("Raiz de numero negativo nao existe nos reais ")
x, y = 1, a
m = (x+y)/2
while abs(m^2-a) > 1e-4
m^2 > a ? (y = m) : (x = m)
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 55: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/55.jpg)
Logo
Funções
bissec.jl - v3function bissec (a, tol , x, y)
a < 1 && error ("Raiz de numero negativo nao existe nos reais ")
m = (x+y)/2
while abs(m^2-a) > tol
m^2 > a ? (y = m) : (x = m)
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 56: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/56.jpg)
Logo
Funções
bissec.jl - v4function bissec (a, tol = 1e-6, x = 1, y = a)
a < 1 && error ("Raiz de numero negativo nao existe nos reais ")
m = (x+y)/2
while abs(m^2-a) > tol
m^2 > a ? (y = m) : (x = m)
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 57: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/57.jpg)
Logo
Funções
bissec.jl - v5function bissec (a; tol = 1e-6, x = 1, y = a)
a < 1 && error ("Raiz de numero negativo nao existe nos reais ")
m = (x+y)/2
while abs(m^2-a) > tol
m^2 > a ? (y = m) : (x = m)
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 58: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/58.jpg)
Logo
Funções
bissec.jl - v6function bissec (a:: Float64; tol:: Float64 = 1e-6, x:: Float64 = 1,
y:: Float64 = a)
a < 1 && error ("Raiz de numero negativo nao existe nos reais ")
m = (x+y)/2
while abs(m^2-a) > tol
m^2 > a ? (y = m) : (x = m)
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 59: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/59.jpg)
Logo
Funções
bissec.jl - v7function bissec (a:: Number; tol:: Number = 1e-6, x:: Number = 1,
y:: Number = a)
a < 1 && error ("Raiz de numero negativo nao existe nos reais ")
m = (x+y)/2
while abs(m^2-a) > tol
m^2 > a ? (y = m) : (x = m)
m = (x+y)/2
end
return m
end
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 33 / 52
![Page 60: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/60.jpg)
Logo
Funções
julia> f(x) = x^2
julia> f(1)
julia> f(2)
julia> f(-1)
julia> g = x->x^3 - x
julia> g(1)
julia> g(2)
julia> g(-1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 34 / 52
![Page 61: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/61.jpg)
Logo
Funções
julia> f(x) = x^2
julia> f(1)
julia> f(2)
julia> f(-1)
julia> g = x->x^3 - x
julia> g(1)
julia> g(2)
julia> g(-1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 34 / 52
![Page 62: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/62.jpg)
Logo
Funções
julia> f(x) = x^2
julia> t = [1,2,3]
julia> f(t) # Erro
julia> map(f, t)
julia> f(x) = x.^2
julia> f(t) # Ok
julia> t.^3
julia> exp(t).*t
julia> 1./t
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 35 / 52
![Page 63: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/63.jpg)
Logo
Funções
julia> f(x) = x^2
julia> t = [1,2,3]
julia> f(t) # Erro
julia> map(f, t)
julia> f(x) = x.^2
julia> f(t) # Ok
julia> t.^3
julia> exp(t).*t
julia> 1./t
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 35 / 52
![Page 64: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/64.jpg)
Logo
Funções
julia> h(x,y) = x^2-y^2
julia> h(2,1)
julia> f(x) = h(x,2)
julia> q(x) = 0.5*dot(x,x)
julia> q(ones(10))
julia> D(x,f,h) = (f(x+h)-f(x)/h)
julia> D(1,x->x^2,0.1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 36 / 52
![Page 65: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/65.jpg)
Logo
Funções
julia> h(x,y) = x^2-y^2
julia> h(2,1)
julia> f(x) = h(x,2)
julia> q(x) = 0.5*dot(x,x)
julia> q(ones(10))
julia> D(x,f,h) = (f(x+h)-f(x)/h)
julia> D(1,x->x^2,0.1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 36 / 52
![Page 66: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/66.jpg)
Logo
Funções
julia> h(x,y) = x^2-y^2
julia> h(2,1)
julia> f(x) = h(x,2)
julia> q(x) = 0.5*dot(x,x)
julia> q(ones(10))
julia> D(x,f,h) = (f(x+h)-f(x)/h)
julia> D(1,x->x^2,0.1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 36 / 52
![Page 67: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/67.jpg)
Logo
Funções
julia> h(x,y) = x^2-y^2
julia> h(2,1)
julia> f(x) = h(x,2)
julia> q(x) = 0.5*dot(x,x)
julia> q(ones(10))
julia> D(x,f,h) = (f(x+h)-f(x)/h)
julia> D(1,x->x^2,0.1)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 36 / 52
![Page 68: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/68.jpg)
Logo
Funções
julia> # f dependendo dela mesmo?
julia> f(x) = f(x-1)*x # Loop infinito ou erro
julia> f(x) = x > 0 ? x^2+1 : 2*x+1
julia> fat(x::Integer) = x > 1 ? fat(x-1)*x : 1
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 37 / 52
![Page 69: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/69.jpg)
Logo
Funções
julia> # f dependendo dela mesmo?
julia> f(x) = f(x-1)*x # Loop infinito ou erro
julia> f(x) = x > 0 ? x^2+1 : 2*x+1
julia> fat(x::Integer) = x > 1 ? fat(x-1)*x : 1
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 37 / 52
![Page 70: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/70.jpg)
Logo
Funções
julia> function foo(a::Float64, v::Array{Float64})
a = 2
v[1] = 0.0
return a, v
end
julia> a = 3
julia> v = rand(3)
julia> b, w = foo(a,v)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 38 / 52
![Page 71: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/71.jpg)
Logo
Funções
julia> function foo(a::Float64, v::Array{Float64})
a = 2
w = copy(v)
w[1] = 0.0
return a, w
end
julia> a = 3
julia> v = rand(3)
julia> b, w = foo(a,v)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 39 / 52
![Page 72: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/72.jpg)
Logo
Funções
julia> function bar(x::Int)
return x+1
end
julia> function bar(x::FloatingPoint)
return 1/x
end
julia> bar(2)
julia> bar(2.0)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 40 / 52
![Page 73: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/73.jpg)
Logo
Funções
Interação com C e Fortran
ccall( (FUNCAO, BIBLIOTECA), RETURN, (TIPOS, DE,
ENTRADA), ENTRADAS)
A biblioteca tem que ser dinâmica (shared);
Os tipos estão numa tupla;
Existem os tipos Cint = Int32, Cfloat = Float32 e
Cdouble = Float64.
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 41 / 52
![Page 74: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/74.jpg)
Logo
Funções
Interação com C e Fortran
ccode/dot.cdouble dotC (int n, double *x, double *y) {
int i;
double s = 0.0;
for (i = 0; i < n; i++)
s += x[i]*y[i];
return s;
}
ccall( ("dotC", -), Cdouble , (Cint , Ptr{Cdouble}, Ptr{Cdouble }), n
, x, y)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 42 / 52
![Page 75: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/75.jpg)
Logo
Funções
Interação com C e Fortran
$ gcc -c -o dot.o dot.c -fPIC
$ ld -shared -o libtestC.so dot.o
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 43 / 52
![Page 76: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/76.jpg)
Logo
Funções
Interação com C e Fortran
testC.jln = 100
s = 0.0
for i = 1:100
v = rand(n)
w = rand(n)
d = ccall ((" dotC", "ccode/libtestC.so"), Cdouble ,
(Cint , Ptr{Cdouble}, Ptr{Cdouble }), n, v, w)
s += abs(d-dot(v,w))
end
println ("Erro = $s")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 44 / 52
![Page 77: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/77.jpg)
Logo
Funções
Interação com C e Fortran
fcode/dot.fsubroutine dotF (N, X, Y, D)
integer N
double precision X(N), Y(N)
double precision D
integer i
D = 0.0D0
do i = 1,N
D = D + X(i)*Y(i)
end do
end subroutine dotF
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 45 / 52
![Page 78: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/78.jpg)
Logo
Funções
n = 100
d = [0.0] #Um vetor
ccall( ("dotf_", -), Void , (Ptr{Int32}, Ptr{Float64},
Ptr{Float64}, Ptr{Float64 }), Int32[n], x, y, d)
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 46 / 52
![Page 79: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/79.jpg)
Logo
Funções
Interação com C e Fortran
$ gfortran -c -o dot.o dot.f -fPIC
$ ld -shared -o libtestF.so dot.o -gfortran
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 47 / 52
![Page 80: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/80.jpg)
Logo
Funções
Interação com C e Fortran
testF.jln = 100
s = 0.0
for i = 1:100
v = rand(n)
w = rand(n)
d = [0.0]
ccall ((" dotf_", "fcode/libtestF.so"), Void , (Ptr{Int32}, Ptr{
Float64},
Ptr{Float64}, Ptr{Float64 }), Int32[n], v, w, d)
s += abs(d[1]-dot(v,w))
end
println ("Erro = $s")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 48 / 52
![Page 81: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/81.jpg)
Logo
Plots
Não existe pacote padrão;
Algumas opções são PyPlot, Gadfly e Winston;
Podemos instalar o pacote direto do terminal do Julia;
Como é tudo novo, e existem muitas dependências, não há
garantia que os pacotes estão funcionando.
Normalmente esses problemas são reportados e alguma solução
(às vezes temporária) é apresentada.
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 49 / 52
![Page 82: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/82.jpg)
Logo
Plots
julia> Pkg.update()
julia> Pkg.add("PyPlot")
julia> using PyPlot
julia> plot([1,2,3,4,5],[3,2,5,1,4])
julia> plot([1,2,3,4,5],[3,2,5,1,4],".")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 50 / 52
![Page 83: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/83.jpg)
Logo
Plots
julia> plot([1,2,3,4,5],[3,2,5,1,4])
julia> axis([0,6,0,6])
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 84: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/84.jpg)
Logo
Plots
julia> plot([1,2,3,4,5],[3,2,5,1,4],".")
julia> axis([0,6,0,6])
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 85: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/85.jpg)
Logo
Plots
julia> x = linspace(0,1,100);
julia> plot(x,x.^2)
julia> xlabel("Coiso"); ylabel("Outro coiso")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 86: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/86.jpg)
Logo
Plots
julia> x = linspace(0,1,100);
julia> plot(x, x.^3-x, "r", x, 4*x.*(1-x), "b--")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 87: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/87.jpg)
Logo
Plots
julia> x = linspace(0,1,100);
julia> plot(cos(2*pi*x), sin(2*pi*x), "o")
julia> title("Círculo")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 88: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/88.jpg)
Logo
Plots
julia> x = linspace(0,1,100);
julia> plot(x, x.^2, "r")
julia> plot(x, 1-x.^2, color="blue", linewidth=2.0, linestyle="--")
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 89: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/89.jpg)
Logo
Plots
julia> x = linspace(0,1,100);
julia> plot(x, x.^2.*sin(4*pi*x))
julia> plot(x, x.^2, "r–", x, -x.^2, "r–")
julia> title(L"Plot of $f(x) = x^2\sin(x)$")Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 51 / 52
![Page 90: Introdução à Julia - abelsiqueira.github.io · Introdução Hierarquia de tipo Number Real Complex Integer FloatingPoint Signed Unsigned Float16 Float32 Float64 Int8 Int16 Int32](https://reader035.vdocuments.mx/reader035/viewer/2022070819/5f1bd1b953d96d737e415ce1/html5/thumbnails/90.jpg)
Logo
Plots
Obrigado
Esta apresentação está licenciada com uma Licença Creative
Commons Atribuição-CompartilhaIgual 4.0 Internacional.
Abel Soares Siqueira Introdução à Julia 30 de Maio de 2015 52 / 52