├── Chapter03 ├── 01. Managing streams, and reading and writing files │ ├── iotest.txt │ ├── iotest2.txt │ ├── iotest.jl │ └── commands.txt ├── 08. Running Julia as a background process │ └── commands.txt ├── 05. Working with JSON data │ └── commands.txt ├── 10. Handling Feather data │ └── commands.txt ├── 02. Using IOBuffer to efficiently work with in-memory streams │ └── commands.txt ├── 11. Reading CSV and FWF files │ └── commands.txt ├── 07. Using object serialization in Julia │ └── commands.txt ├── 03. Fetching data from the internet │ └── commands.txt ├── 06. Working with date and time │ └── commands.txt ├── 04. Writing a simple RESTful service │ └── commands.txt └── 09. Reading and writing Microsoft Excel files │ └── commands.txt ├── Chapter01 ├── 06. How to customize Julia on startup │ ├── hello.jl │ ├── startup.jl │ └── commands.txt ├── 07. Setting up Julia to use multiple cores │ ├── hello.jl │ ├── hello2.jl │ └── commands.txt ├── 08. Useful options for interaction with Julia │ ├── example.jl │ └── commands.txt ├── 02. Julia IDEs │ └── SublimeText.txt ├── 05. Running Julia inside the Cloud9 IDE in the AWS cloud │ └── JuliaRunner.run ├── 09. Displaying computation results in Julia │ ├── commands.txt │ ├── display.jl │ └── display2.jl ├── 01. Installing Julia from binaries │ └── commands.txt ├── 10. Managing packages │ └── commands.txt ├── 11. Configuring Julia in Jupyter Notebook │ └── commands.txt ├── 13. Configuring Julia with Jupyter Notebook in Terminal-only cloud environments │ └── commands.txt ├── 03. Julia support for text editors │ └── commands.txt ├── 12. Configuring Julia to work with JupyterLab │ └── commands.txt └── 04. Building Julia from sources on Linux │ └── commands.txt ├── Chapter10 ├── 04. Distributed computing with Julia │ ├── machinefile.txt │ ├── config │ └── commands.txt ├── 01. Multiprocessing in Julia │ └── commands.txt ├── 03. Multithreading in Julia │ └── commands.txt └── 02. Sending parameters to remote Julia processes │ └── commands.txt ├── Chapter04 ├── 10. Understanding broadcasting in Julia │ └── commands.txt ├── 01. Traversing matrices efficiently │ ├── commands.txt │ └── sums.jl ├── 03. Generating full factorial designs │ ├── commands.txt │ └── expand.jl ├── 08. Writing a simple optimization routine │ ├── commands.txt │ └── marquardt.jl ├── 09. Estimating linear regression │ ├── commands.txt │ └── lm.jl ├── 11. Improving code performance using @inbounds │ ├── commands.txt │ └── inbounds.jl ├── 05. Running Monte Carlo simulations │ ├── simwalk.jl │ └── commands.txt ├── 07. Working with complex numbers │ └── commands.txt ├── 12. Creating a matrix from a set of vectors as rows │ └── commands.txt ├── 04. Approximating pi using partial series sums │ └── commands.txt ├── 06. Analyzing a queuing system │ ├── commands.txt │ └── mm1.jl ├── 13. Using array views to avoid memory allocation │ ├── sudoku.jl │ ├── commands.txt │ └── p096_sudoku.txt └── 02. Executing loops efficiently with conditional statements │ └── commands.txt ├── Chapter09 ├── 03. Estimation using maximum likelihood │ ├── commands.txt │ └── opt.jl ├── 02. Optimization using JuMP │ └── commands.txt ├── 04. Complex plotting with Plots.jl │ └── commands.txt ├── 05. Building machine learning models with ScikitLearn.jl │ └── commands.txt └── 01. Working with databases in Julia │ └── commands.txt ├── Chapter02 ├── 07. Working with UTF-8 strings │ ├── hello.txt │ └── commands.txt ├── 04. Parsing Git logs with regular expressions │ ├── commands.txt │ └── parselog.jl ├── 05. Non-standard ways to sort your data │ └── commands.txt ├── 02. Fast matrix multiplication │ ├── commands.txt │ └── fastmatmul.jl ├── 01. Finding the index of a random minimum element in an array │ ├── commands.txt │ └── randargmin2.jl ├── 06. Creating a function preimage - understanding how dictionaries and sets work │ └── commands.txt └── 03. Implementing a custom pseudo-random number generator │ └── commands.txt ├── Chapter06 ├── 05. Understanding the structure of Julia numeric types with introspection │ ├── commands.txt │ └── types.jl ├── 03. Defining your own types – linked list │ ├── commands.txt │ └── ll.jl ├── 07. The efficiency of mutable versus immutable types │ ├── commands.txt │ ├── work.jl │ └── walk.jl ├── 04. Defining primitive types │ ├── commands.txt │ └── argb.jl ├── 08. Ensuring type stability of your code │ ├── quad.jl │ └── commands.txt ├── 06. Using static arrays │ └── commands.txt ├── 01. Metaprogramming │ └── commands.txt └── 02. Macros and generated functions │ └── commands.txt ├── Chapter08 ├── 03. Profiling Julia code │ ├── profiletest.jl │ └── commands.txt ├── 01. Julia development workflow with Revise.jl │ ├── commands.txt │ └── Module1.jl ├── 07. Managing project dependencies │ └── commands.txt ├── 02. Benchmarking code │ └── commands.txt ├── 05. Calling Python from Julia │ └── commands.txt ├── 04. Setting up logging in your code │ └── commands.txt └── 06. Calling R from Julia │ └── commands.txt ├── Chapter07 ├── 02. Investigating the contents of a data frame │ └── commands.txt ├── 01. Converting data between DataFrame and Matrix │ └── commands.txt ├── 10. Creating pivot tables by chaining transformations of data frames │ ├── commands.txt │ └── iris.csv ├── 03. Reading CSV data from the internet │ ├── commands.txt │ └── iris.csv ├── 04. Working with categorical data │ └── commands.txt ├── 06. Split-apply-combine in DataFrames │ ├── commands.txt │ └── iris.csv ├── 08. Comparing data frames for identity │ ├── commands.txt │ └── grades.csv ├── 09. Transforming rows of a DataFrame │ ├── commands.txt │ └── grades.csv ├── 05. Handling missing data │ ├── cor.jl │ ├── commands.txt │ └── grades.csv └── 07. Converting a data frame between wide and narrow formats │ ├── commands.txt │ └── iris.csv ├── Chapter05 ├── 07. Working with NamedTuples │ └── commands.txt ├── 04. Functional programming in Julia │ └── commands.txt ├── 03. Using functions as variables in Julia │ └── commands.txt ├── 02. Using multiple dispatch to handle branching behavior │ └── commands.txt ├── 01. Understanding subtyping in Julia │ └── commands.txt ├── 06. Handling exceptions in Julia │ └── commands.txt └── 05. Scope of variables in Julia │ └── commands.txt ├── LICENSE ├── cookbookconf.jl ├── Julia_Cookbook-Supplement.md └── README.md /Chapter03/01. Managing streams, and reading and writing files/iotest.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 3 -------------------------------------------------------------------------------- /Chapter03/01. Managing streams, and reading and writing files/iotest2.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 4 -------------------------------------------------------------------------------- /Chapter01/06. How to customize Julia on startup/hello.jl: -------------------------------------------------------------------------------- 1 | println("Hello " * join(ARGS, ", ")) 2 | -------------------------------------------------------------------------------- /Chapter01/07. Setting up Julia to use multiple cores/hello.jl: -------------------------------------------------------------------------------- 1 | println("Hello " * join(ARGS, ", ")) 2 | -------------------------------------------------------------------------------- /Chapter01/08. Useful options for interaction with Julia/example.jl: -------------------------------------------------------------------------------- 1 | println("An example was run!") 2 | -------------------------------------------------------------------------------- /Chapter01/07. Setting up Julia to use multiple cores/hello2.jl: -------------------------------------------------------------------------------- 1 | println("Hello " * join(ARGS, ", ")) 2 | sleep(1) 3 | -------------------------------------------------------------------------------- /Chapter10/04. Distributed computing with Julia/machinefile.txt: -------------------------------------------------------------------------------- 1 | 2*ubuntu@127.0.0.1 2 | 1*ubuntu@127.0.0.1 3 | 1*ubuntu@127.0.0.1 4 | -------------------------------------------------------------------------------- /Chapter01/02. Julia IDEs/SublimeText.txt: -------------------------------------------------------------------------------- 1 | { 2 | "cmd": ["ConEmu64", "/cmd", "julia -i", "$file"], 3 | "selector": "source.julia" 4 | } -------------------------------------------------------------------------------- /Chapter01/06. How to customize Julia on startup/startup.jl: -------------------------------------------------------------------------------- 1 | using Random 2 | 3 | ENV["JULIA_EDITOR"] = "vim" 4 | 5 | println("Setup successful") 6 | -------------------------------------------------------------------------------- /Chapter10/04. Distributed computing with Julia/config: -------------------------------------------------------------------------------- 1 | User ubuntu 2 | PubKeyAuthentication yes 3 | StrictHostKeyChecking no 4 | IdentityFile ~/.ssh/cluster -------------------------------------------------------------------------------- /Chapter04/10. Understanding broadcasting in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> x = [1:10;] 2 | julia> s = isodd.(x) .& (rand(length(x)) .< 0.5) 3 | julia> s = @. isodd(x) & ($rand($length(x)) < 0.5) 4 | -------------------------------------------------------------------------------- /Chapter01/06. How to customize Julia on startup/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia -i hello.jl Al Bo Cyd 2 | $ julia -e "println(factorial(10))" 3 | $ julia --banner=no 4 | 5 | 6 | julia> RandomDevice() 7 | -------------------------------------------------------------------------------- /Chapter01/05. Running Julia inside the Cloud9 IDE in the AWS cloud/JuliaRunner.run: -------------------------------------------------------------------------------- 1 | { 2 | "cmd" : ["julia", "$file", "$args"], 3 | "info" : "Started $project_path$file_name", 4 | "selector" : "source.jl" 5 | } -------------------------------------------------------------------------------- /Chapter09/03. Estimation using maximum likelihood/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("opt.jl") 2 | julia> using Random 3 | julia> Random.seed!(1); 4 | julia> x = randn(100); 5 | julia> testoptim(x) 6 | julia> mean(x), std(x)*sqrt(99/100) 7 | -------------------------------------------------------------------------------- /Chapter04/01. Traversing matrices efficiently/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("sums.jl") 2 | julia> using BenchmarkTools 3 | julia> x = rand(10^4, 10^4); 4 | julia> @btime sum_by_row(x) 5 | julia> @btime sum_by_col(x) 6 | julia> @btime sum(x) 7 | -------------------------------------------------------------------------------- /Chapter04/03. Generating full factorial designs/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("expand.jl") 2 | julia> expandgrid(1:2, 'a':'b') 3 | julia> hcat(expandgrid(1:3, [true, false], 'a':'b')...) 4 | 5 | 6 | julia> repeat([1,2], inner=2, outer=3) 7 | -------------------------------------------------------------------------------- /Chapter02/07. Working with UTF-8 strings/hello.txt: -------------------------------------------------------------------------------- 1 | Bulgarian Здравейте 2 | Chinese 你好 3 | English Hello 4 | Greek Χαίρετε 5 | Hindi नमस्ते 6 | Japanese こんにちは 7 | Khmer សួស្តី 8 | Korean 여보세요 9 | Polish cześć 10 | Russian Здравствуйте 11 | -------------------------------------------------------------------------------- /Chapter04/08. Writing a simple optimization routine/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("marquardt.jl") 2 | julia> rosenbrock(x) = 3 | sum([(1-x[i])^2 + 100(x[i+1]-x[i]^2)^2 for i in 1:length(x)-1]) 4 | julia> marquardt(rosenbrock, rand(20)) 5 | -------------------------------------------------------------------------------- /Chapter06/05. Understanding the structure of Julia numeric types with introspection/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("types.jl") 2 | julia> printsubtypes(Number) 3 | julia> supertypes(Bool) 4 | 5 | 6 | julia> typeof(1 + 1im) 7 | julia> typeof(1.0 + 1.0im) 8 | -------------------------------------------------------------------------------- /Chapter08/03. Profiling Julia code/profiletest.jl: -------------------------------------------------------------------------------- 1 | using Statistics 2 | function timeto1(mv) 3 | x = Int[] 4 | while true 5 | push!(x, rand(1:mv)) 6 | 1 in x && return length(x) 7 | end 8 | end 9 | agg(f, mv, rep) = mean(f(mv) for i in 1:rep) -------------------------------------------------------------------------------- /Chapter01/09. Displaying computation results in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Pkg; Pkg.add("PyPlot") 2 | julia> include("display.jl") 3 | 4 | 5 | $ julia display.jl 6 | $ julia display2.jl 7 | 8 | 9 | julia> transpose(1:100) 10 | julia> rand(100, 100); 11 | -------------------------------------------------------------------------------- /Chapter01/09. Displaying computation results in Julia/display.jl: -------------------------------------------------------------------------------- 1 | using PyPlot, Random 2 | 3 | function f() 4 | Random.seed!(1) 5 | r = rand(50) 6 | @show sum(r) 7 | display(transpose(r)) 8 | print(transpose(r)) 9 | plot(r) 10 | end 11 | 12 | f() 13 | -------------------------------------------------------------------------------- /Chapter03/08. Running Julia as a background process/commands.txt: -------------------------------------------------------------------------------- 1 | $ mkfifo pipe 2 | $ ls 3 | $ julia log.txt 2>err.txt & 4 | $ exec 3>pipe 5 | $ echo "1+2" >&3 6 | $ echo "X" >&3 7 | $ echo "exit()" >&3 8 | $ cat log.txt 9 | $ cat err.txt 10 | $ rm pipe log.txt err.txt 11 | -------------------------------------------------------------------------------- /Chapter04/09. Estimating linear regression/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("lm.jl") 2 | julia> using CSV 3 | julia> wages = CSV.read("wages.csv", 4 | allowmissing=:none, 5 | categorical=true); 6 | julia> lm(wages, :LWage, setdiff(names(wages), [:LWage])) 7 | -------------------------------------------------------------------------------- /Chapter01/09. Displaying computation results in Julia/display2.jl: -------------------------------------------------------------------------------- 1 | using PyPlot, Random 2 | 3 | function f() 4 | Random.seed!(1) 5 | r = rand(50) 6 | @show sum(r) 7 | display(transpose(r)) 8 | print(transpose(r)) 9 | plot(r) 10 | show() 11 | end 12 | 13 | f() 14 | 15 | -------------------------------------------------------------------------------- /Chapter06/03. Defining your own types – linked list/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("ll.jl") 2 | julia> charlist = LinkedList(Char) 3 | julia> pushfirst!(charlist, collect("12345")...) 4 | julia> collect(charlist) 5 | julia> charlist[1], charlist[5] 6 | julia> charlist[0] 7 | julia> charlist[6] 8 | julia> charlist[end] 9 | -------------------------------------------------------------------------------- /Chapter01/01. Installing Julia from binaries/commands.txt: -------------------------------------------------------------------------------- 1 | $ sudo apt update 2 | $ sudo apt -y install build-essential 3 | 4 | $ wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.0.1-linux-x86_64.tar.gz 5 | $ tar xvfz julia-1.0.1-linux-x86_64.tar.gz 6 | $ sudo ln -s /home/ubuntu/julia-1.0.1/bin/julia /usr/local/bin/julia 7 | -------------------------------------------------------------------------------- /Chapter01/10. Managing packages/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> status 3 | (v1.0) pkg> add BenchmarkTools 4 | (v1.0) pkg> status 5 | (v1.0) pkg> precompile 6 | 7 | 8 | julia> using BenchmarkTools 9 | julia> @btime rand() 10 | 11 | julia> ] 12 | (v1.0) pkg> add BSON@v0.2.0 13 | (v1.0) pkg> pin BSON 14 | (v1.0) pkg> free BSON 15 | -------------------------------------------------------------------------------- /Chapter07/02. Investigating the contents of a data frame/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames, Random 2 | julia> Random.seed!(1); 3 | julia> df = DataFrame(rand(1000, 100)); 4 | julia> nrow(df), ncol(df), size(df) 5 | julia> describe(df[1:3]) 6 | julia> filter(x -> occursin(r"1[13579]$", String(x[:variable])), 7 | describe(df)) 8 | -------------------------------------------------------------------------------- /Chapter04/11. Improving code performance using @inbounds/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia inbounds.jl 2 | $ julia --check-bounds=yes inbounds.jl 3 | $ julia --check-bounds=no inbounds.jl 4 | 5 | 6 | julia> x = [1,2,3] 7 | julia> x[4] 8 | julia> f(x) = @inbounds x[1] 9 | julia> g(x) = x[1] 10 | julia> @code_native f([1]) 11 | julia> @code_native g([1]) 12 | -------------------------------------------------------------------------------- /Chapter03/01. Managing streams, and reading and writing files/iotest.jl: -------------------------------------------------------------------------------- 1 | a = parse(Float64, readline(stdin)) 2 | b = parse(Float64, readline(stdin)) 3 | println(stdout, "Got values: $a, $b") 4 | if b > a 5 | println(stderr, "Wrong values: ", b, ">", a) 6 | exit(1) 7 | end 8 | println(stdout, "log(", a, "-", b, ")=", log(a-b)) 9 | exit(0) 10 | -------------------------------------------------------------------------------- /Chapter06/07. The efficiency of mutable versus immutable types/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("walk.jl") 2 | julia> using BenchmarkTools 3 | julia> @benchmark simI() 4 | julia> @benchmark simM() 5 | julia> @benchmark simI2() 6 | julia> @benchmark simM2() 7 | 8 | 9 | julia> include("work.jl") 10 | julia> @benchmark worker1() 11 | julia> @benchmark worker2() 12 | -------------------------------------------------------------------------------- /Chapter08/01. Julia development workflow with Revise.jl/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | pkg> add Revise 3 | pkg> add HTTP 4 | 5 | 6 | push!(LOAD_PATH, ".") 7 | using Revise 8 | using Module1 9 | 10 | julia> getcoinprices("2018-06-20", "2018-06-22") 11 | 12 | julia> getcoinprices("2018-06-23", "2018-06-22") 13 | 14 | julia> getcoinprices("2018-06-23", "2018-06-22") -------------------------------------------------------------------------------- /Chapter06/04. Defining primitive types/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("argb.jl") 2 | julia> ARGB(10,11,12,13) 3 | julia> c = ARGB"#12345678" 4 | julia> [f(c) for f in [α, red, green, blue]] 5 | julia> UInt32(c) 6 | julia> String(c) 7 | 8 | julia> zeroalpha(c::ARGB) = ARGB(UInt32(c) & 0x00FFFFFF) 9 | julia> c = ARGB"#12345678" 10 | julia> zeroalpha(c) 11 | julia> @code_native zeroalpha(c) 12 | -------------------------------------------------------------------------------- /Chapter09/03. Estimation using maximum likelihood/opt.jl: -------------------------------------------------------------------------------- 1 | using Optim 2 | using Distributions 3 | 4 | function loglik(x, μ, logσ) 5 | nd = Normal(μ, exp(logσ)) 6 | -sum(logpdf(nd, v) for v in x) 7 | end 8 | 9 | function testoptim(x) 10 | res = optimize(par -> loglik(x, par[1], par[2]), zeros(2)) 11 | display(res) 12 | res.minimizer[1], exp(res.minimizer[2]) 13 | end 14 | -------------------------------------------------------------------------------- /Chapter04/03. Generating full factorial designs/expand.jl: -------------------------------------------------------------------------------- 1 | function expandgrid(levels...) 2 | lengths = length.(levels) 3 | inner = 1 4 | outer = prod(lengths) 5 | grid = [] 6 | for i in 1:length(levels) 7 | outer = div(outer, lengths[i]) 8 | push!(grid, repeat(levels[i], inner=inner, outer=outer)) 9 | inner *= lengths[i] 10 | end 11 | Tuple(grid) 12 | end 13 | -------------------------------------------------------------------------------- /Chapter07/01. Converting data between DataFrame and Matrix/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames 2 | julia> mat = [x*y for x in 1:3, y in 1:4] 3 | julia> df = DataFrame(mat) 4 | julia> df = DataFrame(mat, [:a, :b, :c, :d]) 5 | julia> Matrix(df) 6 | 7 | 8 | julia> vals = [1, 2]; 9 | julia> df = DataFrame(a=vals, b=["x", "y"]) 10 | julia> df.a[1] = 8 11 | julia> vals 12 | 13 | 14 | julia> getindex.(eachcol(df), 2) 15 | -------------------------------------------------------------------------------- /Chapter01/08. Useful options for interaction with Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> x = 10 # just a test command 2 | julia> @edit sin(1.0) 3 | julia> ; 4 | 5 | 6 | # Linux bash 7 | shell> ls 8 | 9 | # Windows cmd 10 | shell> dir 11 | 12 | 13 | julia> include("example.jl") 14 | julia> ? 15 | help?> include 16 | julia> 17 | (reverse-i-search)`x =': x = 10 18 | julia> x = 10 19 | julia> exit() 20 | julia> @which sin(1.0) 21 | -------------------------------------------------------------------------------- /Chapter01/11. Configuring Julia in Jupyter Notebook/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add IJulia 3 | 4 | julia> using IJulia 5 | julia> notebook() 6 | 7 | 8 | $ ~/.julia/packages/Conda/hsaaN/deps/usr/bin/jupyter notebook 9 | 10 | 11 | C:\> %userprofile%\.julia\packages\Conda\hsaaN\deps\usr\Scripts\jupyter-notebook 12 | 13 | 14 | ENV["JUPYTER"] = "[path to your jupyter program]" 15 | using Pkg 16 | Pkg.build("IJulia") 17 | -------------------------------------------------------------------------------- /Chapter06/08. Ensuring type stability of your code/quad.jl: -------------------------------------------------------------------------------- 1 | function quadratic1(a, b, c) 2 | t(s) = (-b + s*sqrt(Δ))/(2a) 3 | a == 0 && error("a must be different than zero") 4 | Δ = Complex(b^2-4*a*c) 5 | t(1), t(-1) 6 | end 7 | 8 | function quadratic2(a, b, c) 9 | Δ = Complex(b^2-4*a*c) 10 | t(s) = (-b + s*sqrt(Δ))/(2a) 11 | a == 0 && error("a must be different than zero") 12 | t(1), t(-1) 13 | end 14 | -------------------------------------------------------------------------------- /Chapter04/05. Running Monte Carlo simulations/simwalk.jl: -------------------------------------------------------------------------------- 1 | using OnlineStats, Random 2 | 3 | function simwalk() 4 | jumps = 0 5 | distance = 0.0 6 | while true 7 | jumps += 1 8 | distance += rand() 9 | distance ≥ 1.0 && return jumps 10 | end 11 | end 12 | 13 | function incremental(n) 14 | s = Mean() 15 | for i in 1:n 16 | fit!(s, simwalk()) 17 | end 18 | value(s) 19 | end 20 | 21 | -------------------------------------------------------------------------------- /Chapter04/01. Traversing matrices efficiently/sums.jl: -------------------------------------------------------------------------------- 1 | function sum_by_col(x) 2 | s = zero(eltype(x)) 3 | for j in 1:size(x, 2) 4 | for i in 1:size(x, 1) 5 | s += x[i, j] 6 | end 7 | end 8 | s 9 | end 10 | 11 | function sum_by_row(x) 12 | s = zero(eltype(x)) 13 | for i in 1:size(x, 1) 14 | for j in 1:size(x, 2) 15 | s += x[i, j] 16 | end 17 | end 18 | s 19 | end 20 | -------------------------------------------------------------------------------- /Chapter06/05. Understanding the structure of Julia numeric types with introspection/types.jl: -------------------------------------------------------------------------------- 1 | function printsubtypes(T, indent=0) 2 | sT = subtypes(T) 3 | println(" "^indent, T, isempty(sT) ? "" : ":") 4 | for S in sT 5 | printsubtypes(S, indent + 1) 6 | end 7 | end 8 | 9 | function supertypes(T) 10 | print(T) 11 | if T != Any 12 | print(" <: ") 13 | S = supertype(T) 14 | supertypes(S) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Chapter01/13. Configuring Julia with Jupyter Notebook in Terminal-only cloud environments/commands.txt: -------------------------------------------------------------------------------- 1 | $ chmod 400 keyfile.pem 2 | 3 | 4 | $ ssh -i path/to/keyfile.pem -L8888:127.0.0.1:8888 ubuntu@[enter_hostname_here] 5 | 6 | $ ~/.julia/packages/Conda/hsaaN/deps/usr/bin/jupyter lab 7 | 8 | C:\> %userprofile%\.julia\packages\Conda\hsaaN\deps\usr\Scripts\jupyter-lab 9 | 10 | 11 | julia> using IJulia 12 | julia> notebook(detached=true) 13 | julia> run(`$(IJulia.notebook_cmd[1]) notebook list`) -------------------------------------------------------------------------------- /Chapter02/04. Parsing Git logs with regular expressions/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("parselog.jl") 2 | julia> ] 3 | (v1.0) pkg> dev DataFrames 4 | julia> gitstats(joinpath(DEPOT_PATH[1], "dev/DataFrames")) 5 | julia> ] 6 | (v1.0) pkg> free DataFrames 7 | 8 | 9 | julia> function f() 10 | for i in 1:2 11 | if i == 1 12 | j = 1 13 | else 14 | println(j) 15 | end 16 | end 17 | end 18 | julia> f() 19 | -------------------------------------------------------------------------------- /Chapter06/07. The efficiency of mutable versus immutable types/work.jl: -------------------------------------------------------------------------------- 1 | struct T1 2 | x::NTuple{1000, Int} 3 | y::Int 4 | end 5 | 6 | mutable struct T2 7 | x::NTuple{1000, Int} 8 | y::Int 9 | end 10 | 11 | function worker1() 12 | p = T1(ntuple(x->1, 1000), 0) 13 | for i in 1:10^6 14 | p = T1(p.x, p.y+1) 15 | end 16 | p 17 | end 18 | 19 | function worker2() 20 | p = T2(ntuple(x->1, 1000), 0) 21 | for i in 1:10^6 22 | p.y += 1 23 | end 24 | p 25 | end 26 | -------------------------------------------------------------------------------- /Chapter07/10. Creating pivot tables by chaining transformations of data frames/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames, DataFramesMeta, CSV, Statistics 2 | julia> df = CSV.read("iris.csv", footerskip=1, 3 | header=["PetalLength", "PetalWidth", 4 | "SepalLength", "SepalWidth", "Class"]); 5 | 6 | 7 | julia> @linq df |> 8 | where(:Class .== "Iris-setosa") |> 9 | by([:PetalLength, :SepalWidth], meanSL = mean(:SepalLength)) |> 10 | unstack(:SepalWidth, :meanSL) 11 | -------------------------------------------------------------------------------- /Chapter01/03. Julia support for text editors/commands.txt: -------------------------------------------------------------------------------- 1 | $ wget -P ~/ https://raw.githubusercontent.com/Naereen/nanorc/master/julia.nanorc 2 | $ echo include \"~/julia.nanorc\" >> ~/.nanorc 3 | 4 | 5 | git clone git://github.com/JuliaEditorSupport/julia-vim.git 6 | mkdir -p ~/.vim 7 | cp -R julia-vim/* ~/.vim 8 | 9 | 10 | wget -P ~/julia-emacs/ https://raw.githubusercontent.com/JuliaEditorSupport/julia-emacs/master/julia-mode.el 11 | echo "(add-to-list 'load-path \"~/julia-emacs\")" >> ~/.emacs 12 | echo "(require 'julia-mode)" >> ~/.emacs -------------------------------------------------------------------------------- /Chapter01/12. Configuring Julia to work with JupyterLab/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add Conda 3 | 4 | julia> using Conda 5 | julia> Conda.add("jupyterlab") 6 | 7 | 8 | $ ~/.julia/packages/Conda/hsaaN/deps/usr/bin/jupyter lab 9 | 10 | 11 | C:\> %userprofile%\.julia\packages\Conda\hsaaN\deps\usr\Scripts\jupyter-lab 12 | 13 | 14 | $ wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh 15 | 16 | $ sudo bash Anaconda3-5.3.0-Linux-x86_64.sh 17 | 18 | $ /home/ubuntu/anaconda3/bin/jupyter lab 19 | 20 | 21 | C:\> C:\ProgramData\Anaconda3\Scripts\jupyter-lab.exe -------------------------------------------------------------------------------- /Chapter04/07. Working with complex numbers/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function juliapoint(z, c) 2 | for n in 1:255 3 | z = z^2 + c 4 | abs2(z) > 4 && return n 5 | end 6 | return 256 7 | end 8 | julia> using PyPlot 9 | julia> xs = -1.4:0.002:1.4; 10 | julia> ys = -1.05:0.002:1.05; 11 | julia> c = -0.4+0.6im; 12 | julia> res = [juliapoint(complex(x, y), c) for y in ys, x in xs]; 13 | julia> imshow(res, extent=[extrema(xs)..., extrema(ys)...], cmap="gray_r") 14 | 15 | 16 | julia> size(res) 17 | julia> [extrema(xs)..., extrema(ys)...] 18 | -------------------------------------------------------------------------------- /Chapter08/07. Managing project dependencies/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | 3 | (v1.0) pkg> generate Project 4 | julia> using StaticArrays 5 | julia> ] 6 | (v1.0) pkg> activate Project 7 | (Project) pkg> add StaticArrays 8 | julia> using StaticArrays 9 | julia> print(read("Project/Project.toml", String)) 10 | julia> exit() 11 | 12 | $ julia 13 | 14 | julia> ] 15 | julia> using StaticArrays 16 | julia> ] 17 | (v1.0) pkg> activate Project 18 | julia> using StaticArrays 19 | 20 | 21 | julia> LOAD_PATH 22 | julia> using Pkg 23 | julia> pop!(LOAD_PATH) 24 | julia> LOAD_PATH 25 | julia> using Statistics 26 | -------------------------------------------------------------------------------- /Chapter08/01. Julia development workflow with Revise.jl/Module1.jl: -------------------------------------------------------------------------------- 1 | module Module1 2 | 3 | using HTTP 4 | using JSON 5 | 6 | export getcoinprices 7 | 8 | function getcoinprices(dateFrom::String,dateTo::String) 9 | # dateFrom > dateTo && return Dict() 10 | url = string("https://api.coindesk.com/v1/bpi/historical/close.json?currency=USD&start=", 11 | dateFrom, "&end=", dateTo) 12 | res = HTTP.request("GET", url ,verbose=0) 13 | dat = JSON.parse(join(readlines(IOBuffer(res.body)), " ")) 14 | haskey(dat, "bpi") ? dat["bpi"] : Dict() 15 | end 16 | 17 | end # module 18 | -------------------------------------------------------------------------------- /Chapter05/07. Working with NamedTuples/commands.txt: -------------------------------------------------------------------------------- 1 | julia> t1 = (1, 2) 2 | julia> t2 = (1.0, 2) 3 | julia> nt1 = (a=1, b=2) 4 | julia> nt2 = (a=1.0, b=2) 5 | julia> t1 isa Tuple{Real, Int} 6 | julia> t2 isa Tuple{Real, Int} 7 | julia> nt1 isa NamedTuple{(:a, :b), Tuple{Real,Int64}} 8 | julia> nt2 isa NamedTuple{(:a, :b), Tuple{Real,Int64}} 9 | julia> nt1 isa NamedTuple{(:a, :b), Tuple{T,Int64}} where T<:Real 10 | julia> nt2 isa NamedTuple{(:a, :b), Tuple{T,Int64}} where T<:Real 11 | 12 | 13 | julia> [t1, t2] 14 | julia> [nt1, nt2] 15 | 16 | 17 | julia> foo(x; y::Integer) = (x, y) 18 | julia> @code_lowered foo(1, y=1) 19 | -------------------------------------------------------------------------------- /Chapter02/05. Non-standard ways to sort your data/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Random, LinearAlgebra 2 | julia> Random.seed!(1); 3 | julia> x = rand(1000, 1000); 4 | julia> x1 = sortslices(x, by=norm, dims=1); 5 | julia> x2 = sortslices(x, lt=(x,y) -> norm(x) < norm(y), dims=1); 6 | julia> x3 = x[sortperm([norm(view(x, i, :)) for i in 1:size(x, 1)]), :]; 7 | julia> issorted(sum(x1.^2, dims=2)) 8 | julia> x1 == x2 == x3 9 | julia> @time x1 = sortslices(x, by=norm, dims=1); 10 | julia> @time x2 = sortslices(x, lt=(x,y) -> norm(x) < norm(y), dims=1); 11 | julia> @time x3 = x[sortperm([norm(view(x, i, :)) for i in 1:size(x, 1)]), :]; 12 | -------------------------------------------------------------------------------- /Chapter07/03. Reading CSV data from the internet/commands.txt: -------------------------------------------------------------------------------- 1 | julia> download("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", 2 | "iris.csv") 3 | julia> isfile("iris.csv") 4 | julia> readline("iris.csv") 5 | julia> using CSV, DataFrames 6 | julia> df = CSV.read("iris.csv", 7 | header=["PetalLength", "PetalWidth", 8 | "SepalLength", "SepalWidth", "Class"]); 9 | julia> describe(df) 10 | julia> tail(df) 11 | julia> eltypes(df) 12 | julia> df = disallowmissing!(df[1:end-1, :]); 13 | julia> eltypes(df) 14 | julia> CSV.write("iris2.csv", df); 15 | -------------------------------------------------------------------------------- /Chapter02/07. Working with UTF-8 strings/commands.txt: -------------------------------------------------------------------------------- 1 | julia> hello = readlines("hello.txt") 2 | julia> hello_dict = Dict(map(x->Pair(x...), split.(hello, ' '))) 3 | julia> chinese = hello_dict["Chinese"] 4 | julia> codeunits(chinese) 5 | julia> ncodeunits(chinese) 6 | julia> collect(chinese) 7 | julia> length(chinese) 8 | julia> isvalid.(chinese, 1:ncodeunits(chinese)) 9 | julia> thisind.(chinese, 0:ncodeunits(chinese)+1) 10 | julia> nextind.(chinese, 0:ncodeunits(chinese)) 11 | julia> prevind.(chinese, 1:ncodeunits(chinese)+1) 12 | 13 | 14 | julia> s=String([0xff, 0xff, 0xff]) 15 | julia> isvalid(s) 16 | julia> collect(s) 17 | julia> s[1] 18 | -------------------------------------------------------------------------------- /Chapter04/08. Writing a simple optimization routine/marquardt.jl: -------------------------------------------------------------------------------- 1 | using ForwardDiff 2 | using LinearAlgebra 3 | 4 | function marquardt(f, x₀; ε=1e-6, maxiter=1000, λ=10.0^4, α=2) 5 | x = x₀ 6 | fx = f(x) 7 | for i in 1:maxiter 8 | g = ForwardDiff.gradient(f, x) 9 | norm(g) ≤ ε && return (x=x, converged=true, iters=i) 10 | x′ = x .- (ForwardDiff.hessian(f, x) + λ*I) \ g 11 | fx′ = f(x′) 12 | if fx′ < fx 13 | λ *= 0.5 14 | fx = fx′ 15 | x = x′ 16 | else 17 | λ *= 2.0 18 | end 19 | end 20 | (x=x, converged=false, iters=maxiter) 21 | end 22 | -------------------------------------------------------------------------------- /Chapter04/11. Improving code performance using @inbounds/inbounds.jl: -------------------------------------------------------------------------------- 1 | using BenchmarkTools 2 | 3 | mode = ["normal", "@inbounds"] 4 | i = 0 5 | for inbounds in ["", "@inbounds"] 6 | global i += 1 7 | eval(Meta.parse("""function f$i(x::AbstractArray{<:Real}) 8 | y = 0 9 | $inbounds for i in eachindex(x) 10 | y += x[i] > 0.5 11 | end 12 | y 13 | end""")) 14 | end 15 | 16 | x = rand(10^7) 17 | for (idx, f) in enumerate([f1, f2]) 18 | println("\n", mode[idx]) 19 | @btime $f($x) 20 | end 21 | -------------------------------------------------------------------------------- /Chapter06/08. Ensuring type stability of your code/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("quad.jl") 2 | julia> using BenchmarkTools 3 | julia> @benchmark quadratic1(1,2,3) 4 | julia> @benchmark quadratic2(1,2,3) 5 | julia> using Test 6 | julia> @inferred quadratic1(1,2,3) 7 | julia> @inferred quadratic2(1,2,3) 8 | julia> @code_warntype quadratic1(1,2,3) 9 | julia> @code_warntype quadratic2(1,2,3) 10 | 11 | 12 | julia> function quadratic3(a, b, c) 13 | t(s,Δ) = (-b + s*sqrt(Δ))/(2a) 14 | a == 0 && error("a must be different than zero") 15 | Δ = Complex(b^2-4*a*c) 16 | t(1,Δ), t(-1,Δ) 17 | end 18 | julia> @benchmark quadratic3(1,2,3) 19 | -------------------------------------------------------------------------------- /Chapter02/02. Fast matrix multiplication/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("fastmatmul.jl") 2 | julia> using BenchmarkTools 3 | julia> A = ones(5, 5000); 4 | julia> B = ones(5000, 5); 5 | julia> @btime *(repeat([A,B], outer=10)...); 6 | julia> @btime fastmatmul(repeat([A,B], outer=10)...); 7 | 8 | 9 | julia> macro fastmatmul(ex::Expr) 10 | ex.head == :call || throw(ArgumentError("expression must be a call")) 11 | ex.args[1] == :(*) || throw(ArgumentError("only multiplication is allowed")) 12 | new_ex = deepcopy(ex) 13 | new_ex.args[1] = :fastmatmul 14 | esc(new_ex) 15 | end 16 | julia> @fastmatmul ones(2,3)*ones(3,4)*ones(4,5) 17 | -------------------------------------------------------------------------------- /Chapter07/04. Working with categorical data/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames 2 | julia> grade_levels = ["F"; [x*y for x in 'D':-1:'A' for y in ["-", "", "+"]]] 3 | julia> using Random 4 | julia> Random.seed!(1); 5 | julia> grades = categorical(rand(grade_levels, 100), ordered=true); 6 | julia> levels!(grades, grade_levels); 7 | julia> df = DataFrame(id=eachindex(grades), grades = grades); 8 | julia> isordered(grades) 9 | julia> levels(grades) 10 | julia> describe(df, stats=:eltype) 11 | julia> filter(x -> x.grades > "A-", df) 12 | 13 | 14 | julia> grades[1:10] .* " grade" 15 | 16 | 17 | julia> x = repeat(["a"^20, "b"^20], 1000); 18 | julia> y = categorical(x); 19 | julia> Base.summarysize(x) 20 | julia> Base.summarysize(y) 21 | -------------------------------------------------------------------------------- /Chapter07/06. Split-apply-combine in DataFrames/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df = CSV.read("iris.csv", footerskip=1, 3 | header=["PetalLength", "PetalWidth", 4 | "SepalLength", "SepalWidth", "Class"]); 5 | julia> describe(df, stats=[:mean, :nmissing]) 6 | julia> using Statistics 7 | julia> by(df, :Class) do x 8 | DataFrame(n = nrow(x), 9 | mean = mean(x.SepalWidth), 10 | std = std(x.SepalWidth)) 11 | end 12 | 13 | 14 | julia> by(df, :Class, x -> describe(x, stats=[:mean, :nunique])) 15 | 16 | 17 | julia> adf = aggregate(df, :Class, maximum); 18 | julia> describe(adf, stats=:mean) 19 | julia> summary(adf) 20 | -------------------------------------------------------------------------------- /Chapter04/12. Creating a matrix from a set of vectors as rows/commands.txt: -------------------------------------------------------------------------------- 1 | julia> input = [[10i+1:10i+5;] for i in 1:3] 2 | julia> output = [10i+j for i in 1:3, j in 1:5] 3 | julia> hcat(input...) 4 | julia> vcat(x...) 5 | julia> reduce(vcat, transpose.(input)) 6 | julia> hcat(transpose.(input)...) 7 | julia> transpose(hcat(input...)) 8 | julia> vcat(reshape.(input, 1, :)...) 9 | 10 | 11 | julia> hcat(input[1]) 12 | julia> x = [1 2; 3 4] 13 | julia> y = transpose(x) 14 | julia> y[1] = 100 15 | julia> x 16 | 17 | 18 | julia> hcat(permutedims.(input)...) 19 | julia> vcat(permutedims.(input)...) 20 | julia> permutedims(hcat(input...)) 21 | julia> [output[i,:] for i in 1:size(output, 1)] 22 | julia> [input[i][j] for i in 1:length(input), j in 1:length(input[1])] 23 | -------------------------------------------------------------------------------- /Chapter05/04. Functional programming in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add UnicodePlots 3 | 4 | 5 | function deriv(f::Function)::Function 6 | h = √eps() 7 | f1(x) = (f(x+h) - f(x))/h 8 | return f1 9 | end 10 | 11 | using UnicodePlots 12 | f(x) = 2x*x + 5x - 4; 13 | x = -5:3; 14 | 15 | plot = lineplot(x, f.(x), width=45, height=15, canvas=DotCanvas, name="f(x)"); 16 | plot = lineplot!(plot, x, deriv(f).(x), name="f'(x)") 17 | 18 | function q_solve(f) 19 | c = f(0) 20 | f1 = deriv(f) 21 | b = f1(0) 22 | a = f(1)-b-c 23 | d = √(b*b-4*a*c) 24 | return ((-b-d)/(2*a),(-b+d)/(2*a)) 25 | end 26 | 27 | julia> q_solve(x -> (x-1)*(x+7)) 28 | 29 | julia> q_solve(x -> x*x + 1) 30 | 31 | julia> q_solve(x -> x*x + 1 + 0im) 32 | -------------------------------------------------------------------------------- /Chapter08/02. Benchmarking code/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function f1(n::Integer) 2 | n > 0 || error("n must be a positive number") 3 | A = rand(10,10) 4 | [A*rand(10) for i in 1:n] 5 | end 6 | julia> @time f1(10^6); 7 | julia> @time f1(10^6); 8 | julia> using BenchmarkTools 9 | julia> @benchmark f1(10^6) 10 | julia> using Random 11 | julia> function f2(n::Integer) 12 | n > 0 || error("n must be a positive number") 13 | A = rand(10,10) 14 | x = rand(10) 15 | [A*rand!(x) for i in 1:n] 16 | end 17 | julia> @benchmark f2(10^6) 18 | 19 | 20 | julia> @which f1(10^6) 21 | julia> @elapsed f1(10^6) 22 | julia> @allocated f1(10^6) 23 | julia> x = 10 24 | julia> @benchmark rand(x) 25 | julia> @benchmark rand($x) 26 | -------------------------------------------------------------------------------- /Chapter07/08. Comparing data frames for identity/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df1 = CSV.read("grades.csv") 3 | 4 | 5 | julia> using Random 6 | julia> Random.seed!(1); 7 | julia> df2 = df1[shuffle(axes(df1, 1)), shuffle(axes(df1, 2))]; 8 | julia> res = join(df1, df2, kind=:outer, 9 | on=union(names(df1), names(df2)), 10 | indicator=:check, validate=(true, true)); 11 | julia> unique(res.check) 12 | julia> res = join(df1[1:end-1,:], df2[2:end,:], kind=:outer, 13 | on=union(names(df1), names(df2)), 14 | indicator=:check, validate=(true, true)); 15 | julia> by(res, :check, nrow) 16 | 17 | 18 | julia> using StatsBase 19 | julia> df_id(df) = countmap(collect(eachrow(df[sort(names(df))]))) 20 | julia> df_id(df1) == df_id(df2) 21 | -------------------------------------------------------------------------------- /Chapter02/01. Finding the index of a random minimum element in an array/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function allargmin(a) 2 | isempty(a) && return Int[] 3 | m = minimum(a) 4 | filter(i -> a[i] == m, eachindex(a)) 5 | end 6 | julia> randargmin1(a) = rand(allargmin(a)) 7 | julia> include("randargmin2.jl") 8 | julia> using StatsBase 9 | julia> x = [1, 2, 3, 1, 2, 3, 1, 1] 10 | julia> countmap([randargmin1(x) for i in 1:10^6]) 11 | julia> countmap([randargmin2(x) for i in 1:10^6]) 12 | julia> x = rand(1:10, 1000); 13 | julia> using BenchmarkTools 14 | julia> @btime randargmin1($x); 15 | julia> @btime randargmin2($x); 16 | 17 | 18 | julia> 0.0 == -0.0, -0.0 < 0.0 19 | julia> isequal(0.0, -0.0), isless(-0.0, 0.0) 20 | julia> NaN == NaN, NaN < NaN 21 | julia> isequal(NaN, NaN), isless(NaN, NaN) 22 | -------------------------------------------------------------------------------- /Chapter02/01. Finding the index of a random minimum element in an array/randargmin2.jl: -------------------------------------------------------------------------------- 1 | function randargmin2(a) 2 | indices = eachindex(a) 3 | y = iterate(indices) 4 | y === nothing && throw(ArgumentError("collection must be non-empty")) 5 | (idx, state) = y 6 | minval = a[idx] 7 | bestidx = idx 8 | bestcount = 1 9 | y = iterate(indices, state) 10 | while y !== nothing 11 | (idx, state) = y 12 | curval = a[idx] 13 | if isless(curval, minval) 14 | minval = curval 15 | bestidx = idx 16 | bestcount = 1 17 | elseif isequal(curval, minval) 18 | bestcount += 1 19 | rand() * bestcount < 1 && (bestidx = idx) 20 | end 21 | y = iterate(indices, state) 22 | end 23 | bestidx 24 | end 25 | -------------------------------------------------------------------------------- /Chapter07/09. Transforming rows of a DataFrame/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df = CSV.read("grades.csv"); 3 | 4 | 5 | julia> function get_grade(final, midterm, takehome) 6 | (ismissing(final) || final < 50) && return "fail" 7 | if final < 75 && coalesce(midterm, 0) < 50 && coalesce(takehome, 0) < 50 8 | "fail" 9 | else 10 | "pass" 11 | end 12 | end 13 | julia> df.grade = get_grade.(df.Final, df.Midterm, df.TakeHome); 14 | julia> df.grade2 = map(eachrow(df)) do r 15 | coalesce(r.Final, 0) < 50 && return "fail" 16 | if r.Final < 75 && coalesce(r.Midterm, 0) < 50 && coalesce(r.TakeHome < 50) 17 | "fail" 18 | else 19 | "pass" 20 | end 21 | end; 22 | julia> df.grade == df.grade2 23 | -------------------------------------------------------------------------------- /Chapter08/05. Calling Python from Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ENV["PYTHON"]="C:\\ProgramData\\Anaconda3\\python.exe" 2 | 3 | julia> ENV["PYTHON"]="~/anaconda3/bin/python" 4 | 5 | 6 | julia> ] 7 | (v1.0) pkg> add PyCall 8 | 9 | 10 | julia> ENV["PYTHON"]="/new/Python/installation/directory/path" 11 | 12 | (v1.0) pkg> build PyCall 13 | 14 | 15 | $ pip install scrapy 16 | 17 | using Conda 18 | Conda.add("scrapy") 19 | 20 | using PyCall 21 | 22 | @pyimport scrapy.selector as ssel 23 | 24 | txt=""" 25 | 26 | My favorite languages 27 | 32 | 33 | """ 34 | 35 | julia> s = ssel.Selector(text=txt) 36 | julia> [strip(e[:extract]()) for e in s[:xpath]("//li/text()")] 37 | 38 | julia> pybuiltin("sorted")([3,2,1]) -------------------------------------------------------------------------------- /Chapter07/05. Handling missing data/cor.jl: -------------------------------------------------------------------------------- 1 | using Statistics 2 | 3 | abstract type CorMethod end 4 | struct CorAll <: CorMethod end 5 | struct CorComplete <: CorMethod end 6 | struct CorPairwise <: CorMethod end 7 | 8 | function Statistics.cor(df::DataFrame; method::CorMethod=CorAll()) 9 | cor1(i, j) = nrow(df2) == 0 ? missing : cor(df2[i], df2[j]) 10 | 11 | function cor2(i, j) 12 | x = dropmissing(DataFrame([df2[i], df2[j]])) 13 | nrow(x) == 0 ? missing : cor(x[1], x[2]) 14 | end 15 | 16 | use_cor = method == CorPairwise() ? cor2 : cor1 17 | df2 = method == CorComplete() ? dropmissing(df) : df 18 | 19 | m = Matrix{Union{Float64, Missing}}(undef, ncol(df), ncol(df)) 20 | for i in 1:ncol(df), j in i:ncol(df) 21 | m[i, j] = use_cor(i, j) 22 | m[j, i] = m[i, j] 23 | end 24 | m 25 | end 26 | -------------------------------------------------------------------------------- /Chapter04/04. Approximating pi using partial series sums/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function our_pi(n, T) 2 | s = one(T) 3 | f = one(T) 4 | for i::T in 1:n 5 | f *= i / (2i+1) 6 | s += f 7 | end 8 | 2s 9 | end 10 | julia> for T in [Float16, Float64, BigFloat] 11 | display([our_pi(2^n, T) for n in 1:10] .- big(π)) 12 | end 13 | julia> our_pi(1000, BigFloat) - pi 14 | julia> setprecision(1000) do 15 | our_pi(1000, BigFloat)-pi 16 | end 17 | julia> our_pi(23, Rational) 18 | julia> our_pi(23, Rational{BigInt}) 19 | 20 | 21 | julia> setprecision(() -> our_pi(1000, BigFloat), 1000) - pi 22 | 23 | 24 | julia> (typeof∘one).([AbstractFloat, Rational]) 25 | julia> typeof(2*one(Rational{Integer})) 26 | julia> typeof(2*one(Rational{Signed})) 27 | julia> typeof(2*one(Rational{Unsigned})) 28 | -------------------------------------------------------------------------------- /Chapter01/07. Setting up Julia to use multiple cores/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia --banner=no -p 2 2 | 3 | 4 | julia> using Distributed 5 | julia> nworkers() 6 | julia> exit() 7 | 8 | 9 | $ julia --banner=no -p auto -L hello.jl 10 | 11 | 12 | julia> exit() 13 | 14 | 15 | $ julia --banner=no -p auto -L hello2.jl 16 | 17 | 18 | julia> exit() 19 | 20 | 21 | # Linux bash 22 | $ export JULIA_NUM_THREADS=`nproc` 23 | $ julia -e "println(Threads.nthreads())" 24 | 25 | 26 | # Windows cmd 27 | C:\> set JULIA_NUM_THREADS=%NUMBER_OF_PROCESSORS% 28 | C:\> julia -e "println(Threads.nthreads())" 29 | 30 | 31 | D:\> julia --banner=no -p 2 -L hello2.jl 32 | 33 | 34 | julia> pwd() 35 | julia> using Distributed 36 | julia> pmap(i -> (i, myid(), pwd()), 1:nworkers()) 37 | julia> cd("C:\\") 38 | julia> pwd() 39 | julia> addprocs(2) 40 | julia> pmap(i -> (i,myid(),pwd()), 1:nworkers()) 41 | -------------------------------------------------------------------------------- /Chapter05/03. Using functions as variables in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | using Random 2 | Random.seed!(0); 3 | 4 | 5 | mutable struct Agent 6 | id::Int 7 | x::Float64 8 | y::Float64 9 | times_moved::Int 10 | end 11 | 12 | function move!(agent::Agent) 13 | angle = rand()*2π 14 | agent.x += cos(angle) 15 | agent.y += sin(angle) 16 | agent.times_moved += 1 17 | end 18 | 19 | pop = Agent.(1:30, 0, 0, 0) 20 | 21 | function step!(pop::Array{Agent,1}) 22 | sort!(pop, by = a -> √(a.x*a.x + a.y*a.y), rev=true) 23 | foreach(i -> (rand() < (1/i)) && move!(pop[i]), 1:length(pop)) 24 | end 25 | 26 | foreach(s -> step!(pop), 1:1000) 27 | 28 | julia> filter(a -> √(a.x*a.x + a.y*a.y) >= 25, pop) 29 | 30 | 31 | julia> pop = fill(Agent(1, 0.0, 0.0, 0), 3) 32 | julia> pop[2].id = 2 33 | julia> pop 34 | 35 | 36 | julia> pop=[Agent(i, 0.0, 0.0, 0) for i in 1:3] -------------------------------------------------------------------------------- /Chapter04/06. Analyzing a queuing system/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia -i mm1.jl 2 | 3 | 4 | julia> ad = Exponential(1.3) 5 | julia> sd = Exponential(0.95) 6 | julia> @time res = [queue1(2^14, 2^12, ad, sd) for i in 1:2^12]; 7 | julia> exact = mm1_exact(ad, sd) 8 | julia> @time println(bootCI(res .- exact, mean, 0.99, 2^14)) 9 | 10 | 11 | function bootCI(data, stat::Function, CI::Float64, reps::Integer) 12 | tmp = similar(data) 13 | boot = [stat(rand!(tmp, data)) for i in 1:reps] 14 | low, high = quantile(boot, [(1-CI)/2, (1+CI)/2]) 15 | (value=stat(data), low=low, high=high) 16 | end 17 | 18 | 19 | using StatsBase, Distributions 20 | 21 | function bayesbootCI(data, stat, CI::Float64, reps::Integer) 22 | d = Dirichlet(length(data), 1) 23 | boot = [stat(data, weights(rand(d))) for i in 1:reps] 24 | low, high = quantile(boot, [(1-CI)/2, (1+CI)/2]) 25 | (value=stat(data), low=low, high=high) 26 | end 27 | -------------------------------------------------------------------------------- /Chapter04/13. Using array views to avoid memory allocation/sudoku.jl: -------------------------------------------------------------------------------- 1 | blockvalid(x, v) = count(isequal(v), x) ≤ 1 2 | 3 | function backtrack!(x) 4 | pos = findfirst(isequal(0), x) 5 | isa(pos, Nothing) && return true 6 | iloc = 3div(pos[1]-1, 3) .+ (1:3) 7 | jloc = 3div(pos[2]-1, 3) .+ (1:3) 8 | for k in 1:9 9 | x[pos] = k 10 | blockvalid(view(x, pos[1], :), k) || continue 11 | blockvalid(view(x, :, pos[2]), k) || continue 12 | blockvalid(view(x, iloc, jloc), k) || continue 13 | backtrack!(x) && return true 14 | end 15 | x[pos] = 0 16 | return false 17 | end 18 | 19 | function ssolve(lines, i) 20 | t = [lines[10i-j][k] - '0' for j in 8:-1:0, k in 1:9] 21 | backtrack!(t) 22 | sum([100, 10, 1] .* t[1, 1:3]) 23 | end 24 | 25 | lines = readlines("p096_sudoku.txt") 26 | @time sum(ssolve(lines, i) for i in 1:50) 27 | @time sum(ssolve(lines, i) for i in 1:50) 28 | -------------------------------------------------------------------------------- /Chapter04/13. Using array views to avoid memory allocation/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia sudoku.jl 2 | 3 | 4 | function backtrack!(x, z, idx) 5 | idx > length(z) && return true 6 | pos = z[idx] 7 | iloc = 3div(pos[1]-1, 3) 8 | jloc = 3div(pos[2]-1, 3) 9 | filled = 0 10 | @inbounds for k in 1:9 11 | filled |= x[pos[1], k] | x[k, pos[2]] 12 | end 13 | @inbounds for k1 in 1:3, k2 in 1:3 14 | filled |= x[iloc+k1, jloc+k2] 15 | end 16 | @inbounds for i in 1:9 17 | k = 1< preimage = Dict{Float64, Set{Tuple{Float64, Float64}}}() 2 | julia> for x in -2.0:2.0, y in -2.0:2.0 3 | k = x / y 4 | v = (x, y) 5 | if haskey(preimage, k) 6 | push!(preimage[k], v) 7 | else 8 | preimage[k] = Set([v]) 9 | end 10 | end 11 | julia> for k in sort!(collect(keys(preimage))) 12 | println(k, ":\t", join(sort!(collect(preimage[k])), ",\t")) 13 | end 14 | 15 | 16 | julia> f1 = NaN 17 | julia> f2 = -NaN 18 | julia> reinterpret(UInt64, f1) 19 | julia> reinterpret(UInt64, f2) 20 | julia> f1 === f2 21 | julia> isequal(f1, f2) 22 | julia> Set([f1, f2]) 23 | julia> Set([0.0, -0.0]) 24 | julia> reinterpret(UInt64, 0.0) 25 | julia> reinterpret(UInt64, -0.0) 26 | julia> 1/0.0 27 | julia> 1/-0.0 28 | julia> unique([-0.0, 0.0, 0.0, -0.0, NaN, -NaN]) 29 | -------------------------------------------------------------------------------- /Chapter03/05. Working with JSON data/commands.txt: -------------------------------------------------------------------------------- 1 | using JSON 2 | 3 | json_txt = """{ 4 | "key":"value", 5 | "number":7, 6 | "array":[1,2,5], 7 | "dict":{"k1":"val1","k2":2} 8 | }"""; 9 | 10 | julia> print(json_txt) 11 | 12 | julia> JSON.parse(json_txt) 13 | 14 | data = Dict{Int64,Union{Int64, String}}(1=>"text", 2=>999); 15 | 16 | julia> print(JSON.json(data)) 17 | 18 | f = open("file.json", "w"); 19 | JSON.print(f,data); 20 | close(f); 21 | 22 | f = open("file.json", "r"); 23 | data2 = JSON.parse(f); 24 | close(f); 25 | 26 | data_copy = JSON.parsefile("file.json") 27 | 28 | julia> a = reshape(collect(1:8), 2, 4) 29 | 30 | julia> b = JSON.json(a) 31 | 32 | julia> JSON.parse(b) 33 | 34 | julia> struct S; x1::Int64; x2::Float64; x3::String; end 35 | 36 | julia> s = S(1, 4.5, "test"); 37 | 38 | julia> println(JSON.json(s)) 39 | 40 | julia> JSON.parse(JSON.json(s)) 41 | -------------------------------------------------------------------------------- /Chapter08/04. Setting up logging in your code/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Logging 2 | julia> function f(x) 3 | y = Set(x) 4 | for v in x 5 | pop!(y, v) 6 | end 7 | end 8 | julia> f([1,2,3]) 9 | julia> f([1,2,1,3]) 10 | julia> function f(x) 11 | y = Set(x) 12 | for v in x 13 | @debug v, y, (v in y) 14 | pop!(y, v) 15 | end 16 | end 17 | julia> old = global_logger(ConsoleLogger(stderr, Logging.Debug)); 18 | julia> f([1,2,1,3]) 19 | julia> function f(x) 20 | y = Set(x) 21 | for v in x 22 | if v in y 23 | @debug v, y, (v in y) 24 | pop!(y, v) 25 | else 26 | @debug "$v not found" 27 | end 28 | end 29 | end 30 | julia> f([1,2,1,3]) 31 | julia> global_logger(old); 32 | julia> f([1,2,1,3]) 33 | 34 | 35 | julia> global_logger(NullLogger()) 36 | julia> @error "Important error" 37 | -------------------------------------------------------------------------------- /Chapter07/07. Converting a data frame between wide and narrow formats/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df = CSV.read("iris.csv", footerskip=1, 3 | header=["PetalLength", "PetalWidth", 4 | "SepalLength", "SepalWidth", "Class"]); 5 | 6 | 7 | julia> df.id = axes(df, 1); 8 | julia> sdf = stack(df) 9 | julia> describe(sdf, stats=[:min, :max]) 10 | julia> udf = unstack(sdf, :variable, :value); 11 | julia> names(udf) 12 | julia> permutecols!(udf, names(df)); 13 | julia> df == udf 14 | julia> using Statistics 15 | julia> agg = by(sdf, [:Class, :variable], 16 | x -> DataFrame(value=mean(x.value), n = nrow(x))) 17 | julia> agg2 = unstack(agg, :Class, :variable, :value); 18 | julia> agg3 = by(df, :Class) do x 19 | DataFrame(PetalLength=mean(x.PetalLength), 20 | PetalWidth=mean(x.PetalWidth), 21 | SepalLength=mean(x.SepalLength), 22 | SepalWidth=mean(x.SepalWidth)); 23 | end; 24 | julia> agg2 == agg3 25 | -------------------------------------------------------------------------------- /Chapter03/10. Handling Feather data/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add DataFrames 3 | (v1.0) pkg> add Feather 4 | (v1.0) pkg> add RCall 5 | (v1.0) pkg> add PyCall 6 | (v1.0) pkg> add Conda 7 | 8 | 9 | using Feather 10 | using DataFrames 11 | 12 | using Random 13 | Random.seed!(0); 14 | 15 | julia> df = DataFrame(x1=[1:3...,missing], 16 | x2=rand(4), 17 | x3=rand(1:10,4)) 18 | 19 | julia> Feather.write("df.dat", df); 20 | 21 | julia> df2 = Feather.read("df.dat") 22 | 23 | julia> isequal(df,df2) 24 | 25 | julia> describe(df)[[1:3..., 5, 7, 8]] 26 | 27 | julia> describe(df2)[[1:3..., 5, 7, 8]] 28 | 29 | using RCall 30 | 31 | julia> $ 32 | 33 | R> library(feather) 34 | R> dfR <- read_feather("df.dat") 35 | 36 | R> dfR 37 | 38 | julia> dfR = @rget dfR 39 | 40 | julia> describe(dfR)[[1:3..., 5, 7, 8]] 41 | 42 | using Conda 43 | Conda.runconda(`install feather-format -c conda-forge -y`) 44 | 45 | using PyCall 46 | @pyimport feather 47 | 48 | julia> dat = feather.read_dataframe("df.dat") 49 | -------------------------------------------------------------------------------- /Chapter04/09. Estimating linear regression/lm.jl: -------------------------------------------------------------------------------- 1 | using DataFrames 2 | 3 | function df2mm(df::DataFrame) 4 | n = size(df, 1) 5 | mm_raw = [fill(1.0, n, 1)] 6 | mm_name = ["const"] 7 | for (name, value) in eachcol(df) 8 | if eltype(value) <: Real 9 | push!(mm_raw, hcat(Float64.(value))) 10 | push!(mm_name, string(name)) 11 | else 12 | uvalue = unique(value) 13 | length(uvalue) == 1 && continue 14 | dvalue = Dict(v=>i for (i, v) in enumerate(uvalue)) 15 | mvalue = zeros(n, length(uvalue)) 16 | for i in 1:n 17 | mvalue[i, dvalue[value[i]]] = 1.0 18 | end 19 | push!(mm_raw, mvalue[:, 2:end]) 20 | append!(mm_name, string.(name, "_", uvalue[2:end])) 21 | end 22 | end 23 | (data=hcat(mm_raw...), names=mm_name) 24 | end 25 | 26 | function lm(df, y, xs) 27 | yv = Float64.(df[y]) 28 | xv, xn = df2mm(df[[xs;]]) 29 | params = (transpose(xv)*xv)\(transpose(xv)*yv) 30 | DataFrame(name = xn, estimate=params) 31 | end 32 | -------------------------------------------------------------------------------- /Chapter08/06. Calling R from Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add RCall 3 | 4 | julia> ENV["R_HOME"]="/new/R/installation/directory/path" 5 | 6 | (v1.0) pkg> build RCall 7 | 8 | (v1.0) pkg> add Ditributions 9 | (v1.0) pkg> add DateFrames 10 | 11 | R> install.packages("ggplot2") 12 | 13 | 14 | using RCall 15 | 16 | using Distributions 17 | using DataFrames 18 | using Random 19 | Random.seed!(0); 20 | dat = rand(MvNormal([1 0.75; 0.75 1]), 1000); 21 | df = DataFrame(permutedims(dat)) 22 | 23 | 24 | julia> @rlibrary ggplot2 25 | julia> ggplot(df,aes(x=:x1,y=:x2)) + geom_point() 26 | 27 | 28 | julia> R"library(ggplot2)" 29 | julia> R"ggplot($df,aes(x=x1,y=x2)) + geom_point()" 30 | 31 | 32 | julia> $ 33 | R> library(ggplot2) 34 | R> ggplot($df,aes(x=x1,y=x2)) + geom_point() 35 | 36 | 37 | julia> @rput df 38 | 39 | julia> R"library(ggplot2)" 40 | julia> R"ggplot(df,aes(x=x1,y=x2)) + geom_point()" 41 | 42 | julia> $ 43 | R> library(ggplot2) 44 | R> ggplot(df,aes(x=x1,y=x2)) + geom_point() 45 | 46 | 47 | julia> using RCall 48 | julia> a = 5; 49 | julia> @rput a 50 | julia> R"b = a*2" 51 | julia> @rget b 52 | julia> b 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter08/03. Profiling Julia code/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add ProfileView 3 | 4 | 5 | julia> include("profiletest.jl"); 6 | julia> @time agg(timeto1, 1000, 10_000); 7 | julia> @time agg(timeto1, 1000, 10_000); 8 | 9 | julia> @allocated agg(timeto1, 1000, 10_000) 10 | 11 | using Profile 12 | using ProfileView 13 | Profile.init(delay=0.0005) 14 | 15 | Profile.clear() 16 | Profile.@profile agg(timeto1, 1000, 10_000); 17 | 18 | julia> Profile.print() 19 | 20 | julia> ProfileView.view() 21 | 22 | function timeto2(mv) 23 | x = Set{Int}() 24 | while true 25 | push!(x, rand(1:mv)) 26 | 1 in x && return length(x) 27 | end 28 | end 29 | 30 | 31 | julia> @time agg(timeto2, 1000, 10_000); 32 | julia> @time agg(timeto2, 1000, 10_000); 33 | julia> @allocated agg(timeto2, 1000, 10_000) 34 | 35 | 36 | Juno.@profiler agg(timeto1, 1000, 10_000); 37 | 38 | julia> function timeto3(mv) 39 | x = BitSet() 40 | while true 41 | push!(x, rand(1:mv)) 42 | 1 in x && return length(x) 43 | end 44 | end 45 | julia> @time agg(timeto3, 1000, 10_000); 46 | julia> @time agg(timeto3, 1000, 10_000); 47 | -------------------------------------------------------------------------------- /Chapter03/02. Using IOBuffer to efficiently work with in-memory streams/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function splitstring(s::AbstractString) 2 | bufs = [IOBuffer() for i in 1:2] 3 | idx = 1 4 | for c in s 5 | write(bufs[idx], c) 6 | idx = 3 - idx 7 | end 8 | @. String(take!(bufs)) 9 | end 10 | julia> s = join('1':'9', "-") 11 | julia> splitstring(s) 12 | 13 | 14 | julia> function splitstring1(s::AbstractString) 15 | bufs = [Char[] for i in 1:2] 16 | idx = 1 17 | for c in s 18 | push!(bufs[idx], c) 19 | idx = 3 - idx 20 | end 21 | join.(bufs) 22 | end 23 | julia> function splitstring2(s::AbstractString) 24 | bufs = ["" for i in 1:2] 25 | idx = 1 26 | for c in s 27 | bufs[idx] *= c 28 | idx = 3 - idx 29 | end 30 | bufs 31 | end 32 | julia> using BenchmarkTools 33 | julia> s = "1"^10^4; 34 | julia> @benchmark splitstring($s) 35 | julia> @benchmark splitstring1($s) 36 | julia> @benchmark splitstring2($s) 37 | -------------------------------------------------------------------------------- /Chapter03/11. Reading CSV and FWF files/commands.txt: -------------------------------------------------------------------------------- 1 | julia> csv = """a,b,c 2 | 11,2,3 3 | 4,555,6 4 | 7,8,9999""" 5 | julia> iocsv = IOBuffer(csv) 6 | julia> fwf = """a b c 7 | 11 2 3 8 | 4 555 66 9 | 7 8 9999""" 10 | julia> iofwf = IOBuffer(fwf) 11 | 12 | 13 | julia> using DelimitedFiles 14 | julia> datacsv, headercsv = readdlm(iocsv, ',', header=true) 15 | julia> headercsv 16 | julia> datacsv 17 | julia> function getsubstring(s::AbstractString, 18 | charfrom::Int, 19 | charto::Int) 20 | SubString(s, nextind.(s, 0, (charfrom, charto))...) 21 | end 22 | julia> function readfwf(io, ranges::AbstractVector{<:Pair}) 23 | datafwf = [] 24 | starts = first.(ranges) 25 | ends = last.(ranges) 26 | while !eof(io) 27 | line = readline(io) 28 | push!(datafwf, getsubstring.(line, starts, ends)) 29 | end 30 | [datafwf[i][j] for i in 1:length(datafwf), j in 1:length(ranges)] 31 | end 32 | julia> datafwf = readfwf(iofwf, [1=>2, 4=>6, 8=>11]) 33 | julia> parse.(Int, datafwf[2:end,:]) 34 | -------------------------------------------------------------------------------- /Chapter05/02. Using multiple dispatch to handle branching behavior/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames 2 | julia> df = DataFrame(s = categorical(["a", "b", "c"]), 3 | n = 1.0:3.0, 4 | f = [sin, cos, missing]) 5 | julia> simpledescribe(v) = "unknown type" 6 | julia> simpledescribe(v::Vector{<:Number}) = "numeric" 7 | julia> simpledescribe(v::CategoricalArray) = "categorical" 8 | julia> simpledisplay(df) = 9 | foreach(x -> println(x[1], ": ", 10 | simpledescribe(x[2])), 11 | eachcol(df)) 12 | julia> simpledisplay(df) 13 | 14 | 15 | julia> methods(simpledescribe) 16 | 17 | 18 | julia> df = DataFrame(x=1:10^6); 19 | julia> function helper(x) 20 | s = zero(eltype(x)) 21 | for v in x 22 | s += v 23 | end 24 | s 25 | end 26 | julia> function fun1(df) 27 | s = zero(eltype(df[1])) 28 | for v in df[1] 29 | s += v 30 | end 31 | s 32 | end 33 | julia> fun2(df) = helper(df[1]) 34 | julia> using BenchmarkTools 35 | julia> @btime fun1(df) 36 | julia> @btime fun2(df) 37 | -------------------------------------------------------------------------------- /Chapter06/04. Defining primitive types/argb.jl: -------------------------------------------------------------------------------- 1 | primitive type ARGB 32 end 2 | 3 | ARGB(c::UInt32) = reinterpret(ARGB, c) 4 | ARGB(c) = ARGB(UInt32(c)) 5 | ARGB(α::UInt8, red::UInt8, green::UInt8, blue::UInt8) = 6 | ARGB(UInt32(α) << 24 + UInt32(red) << 16 + 7 | UInt32(green) << 8 + UInt32(blue)) 8 | ARGB(α, red, green, blue) = ARGB(UInt8(α), UInt8(red), 9 | UInt8(green), UInt8(blue)) 10 | 11 | function ARGB(c::AbstractString) 12 | if !occursin(r"^#[0-9a-fA-F]{8}$", c) 13 | throw(DomainError("wrong color string: $c")) 14 | end 15 | ARGB(parse(UInt32, c[2:end], base=16)) 16 | end 17 | 18 | macro ARGB_str(s) ARGB(s) end 19 | 20 | α(c::ARGB)::UInt8 = (UInt32(c) >> 24) & 0x000000FF 21 | red(c::ARGB)::UInt8 = (UInt32(c) >> 16) & 0x000000FF 22 | green(c::ARGB)::UInt8 = (UInt32(c) >> 8) & 0x000000FF 23 | blue(c::ARGB)::UInt8 = UInt32(c) & 0x000000FF 24 | 25 | Base.UInt32(c::ARGB) = reinterpret(UInt32, c) 26 | convert(UInt32, c::ARGB) = UInt32(c) 27 | convert(ARGB, c::UInt32) = ARGB(c) 28 | Base.String(c::ARGB) = "#" * lpad(string(UInt32(c), base=16), 8, "0") 29 | convert(String, c::ARGB) = String(c) 30 | convert(ARGB, c::AbstractString) = ARGB(c) 31 | -------------------------------------------------------------------------------- /Chapter04/06. Analyzing a queuing system/mm1.jl: -------------------------------------------------------------------------------- 1 | using Distributions, OnlineStats 2 | 3 | function queue1(until::Real, burnin::Real, 4 | ad::Distribution, sd::Distribution) 5 | now, nextArrival, nextDeparture = 0.0, rand(ad), Inf 6 | queue, waits = Float64[], Mean() 7 | while now < until 8 | if nextArrival < nextDeparture 9 | now = nextArrival 10 | if isempty(queue) 11 | nextDeparture = nextArrival + rand(sd) 12 | end 13 | push!(queue, nextArrival) 14 | nextArrival += rand(ad) 15 | else 16 | now = nextDeparture 17 | insystem = nextDeparture - popfirst!(queue) 18 | burnin < now < until && fit!(waits, insystem) 19 | nextDeparture += isempty(queue) ? Inf : rand(sd) 20 | end 21 | end 22 | value(waits) 23 | end 24 | 25 | mm1_exact(ad::Exponential, sd::Exponential) = 1/(1/mean(sd)-1/mean(ad)) 26 | 27 | function bootCI(data, stat::Function, CI::Float64, reps::Integer) 28 | boot = [stat(rand(data, length(data))) for i in 1:reps] 29 | low, high = quantile(boot, [(1-CI)/2, (1+CI)/2]) 30 | (value=stat(data), low=low, high=high) 31 | end 32 | -------------------------------------------------------------------------------- /Chapter07/05. Handling missing data/commands.txt: -------------------------------------------------------------------------------- 1 | julia> download("https://openmv.net/file/class-grades.csv", 2 | "grades.csv") 3 | julia> using CSV, DataFrames, Statistics 4 | julia> df = CSV.read("grades.csv"); 5 | 6 | 7 | julia> summary(df) 8 | julia> describe(df, stats=[:min, :max, :nmissing]) 9 | julia> CSV.validate("grades.csv") 10 | julia> [cor(df[i], df[j]) for i in axes(df, 2), j in axes(df, 2)] 11 | julia> df2 = dropmissing(df); 12 | julia> describe(df2, stats=:nmissing) 13 | julia> [cor(df2[i], df2[j]) for i in axes(df2, 2), j in axes(df2, 2)] 14 | julia> function cor2(x, y) 15 | df = dropmissing(DataFrame([x, y])) 16 | cor(df[1], df[2]) 17 | end 18 | julia> [cor2(df[i], df[j]) for i in axes(df, 2), j in axes(df, 2)] 19 | 20 | 21 | julia> sin(missing) 22 | julia> 1 + missing 23 | julia> s = ["a", "bb", missing, "dddd"] 24 | julia> (x -> isequal(x, missing) ? missing : length(x)).(s) 25 | julia> 1 == missing 26 | julia> missing == missing 27 | julia> 1 < missing 28 | julia> isequal(1, missing) 29 | julia> isequal(missing, missing) 30 | julia> isless(1, missing) 31 | julia> isless(Inf, missing) 32 | 33 | 34 | julia> include("cor.jl"); 35 | julia> cor(df) 36 | julia> cor(df, method=CorComplete()) 37 | julia> cor(df, method=CorPairwise()) 38 | -------------------------------------------------------------------------------- /Chapter06/03. Defining your own types – linked list/ll.jl: -------------------------------------------------------------------------------- 1 | struct ListNode{T} 2 | value::T 3 | next::Union{ListNode{T}, Nothing} 4 | end 5 | 6 | mutable struct LinkedList{T} 7 | head::Union{ListNode{T}, Nothing} 8 | end 9 | 10 | LinkedList(T::Type) = LinkedList{T}(nothing) 11 | 12 | Base.iterate(ll::LinkedList) = ll.head === nothing ? nothing : (ll.head.value, ll.head) 13 | Base.iterate(ll::LinkedList{T}, state::ListNode{T}) where T = 14 | state.next === nothing ? nothing : (state.next.value, state.next) 15 | 16 | function Base.getindex(ll::LinkedList, idx::Integer) 17 | idx < 1 && throw(BoundsError("$idx is less than 1")) 18 | for v in ll 19 | idx -= 1 20 | idx == 0 && return v 21 | end 22 | throw(BoundsError("index beyond end of linked list")) 23 | end 24 | 25 | function Base.pushfirst!(ll::LinkedList{T}, items::T...) where T 26 | for item in reverse(items) 27 | ll.head = ListNode{T}(item, ll.head) 28 | end 29 | ll 30 | end 31 | 32 | Base.show(io::IO, ll::LinkedList{T}) where T = 33 | print(io, "LinkedList{$T}[" * join(ll, ", ") * "]") 34 | 35 | Base.eltype(ll::LinkedList{T}) where T = T 36 | 37 | Base.length(ll::LinkedList) = count(v -> true, ll) 38 | 39 | Base.firstindex(ll::LinkedList) = 1 40 | Base.lastindex(ll::LinkedList) = length(ll) 41 | -------------------------------------------------------------------------------- /Chapter04/05. Running Monte Carlo simulations/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("simwalk.jl") 2 | julia> n = 10^6; 3 | julia> Random.seed!(1); 4 | julia> res1 = mean([simwalk() for i in 1:n]) 5 | julia> res2 = mean(map(x -> simwalk(), 1:n)) 6 | julia> res3 = mean(simwalk() for i in 1:n) 7 | julia> res4 = incremental(n) 8 | julia> n = 10^8; 9 | julia> @time mean([simwalk() for i in 1:n]); 10 | julia> @time mean(map(x -> simwalk(), 1:n)); 11 | julia> @time mean(simwalk() for i in 1:n); 12 | julia> @time incremental(n); 13 | julia> MathConstants.e - incremental(10^9) 14 | 15 | 16 | julia> function simwalk(n) 17 | jumps = 0 18 | for i in 1:n 19 | distance = 0.0 20 | while true 21 | jumps += 1 22 | distance += rand() 23 | distance ≥ 1.0 && break 24 | end 25 | end 26 | jumps / n 27 | end 28 | julia> simwalk(10^6) 29 | julia> @time simwalk(10^8) 30 | julia> function simwalk(n) 31 | jumps = Int32(0) 32 | for i in 1:n 33 | distance = 0.0 34 | while true 35 | jumps += Int32(1) 36 | distance += rand() 37 | distance ≥ 1.0 && break 38 | end 39 | end 40 | jumps / n 41 | end 42 | -------------------------------------------------------------------------------- /Chapter06/06. Using static arrays/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add StaticArrays 3 | (v1.0) pkg> add BenchmarkTools 4 | 5 | 6 | using StaticArrays 7 | using BenchmarkTools 8 | using Random 9 | 10 | function profit(demand, prices_sale, purchases, prices_purchase) 11 | sales = min.(purchases,demand) 12 | sum(sales .* prices_sale .- purchases .* prices_purchase) 13 | end 14 | 15 | Random.seed!(0); 16 | demand, prices_sale, purchases, prices_purchase = 17 | (rand(10).*100, 300:10:390, rand(10).*100, 100:10:190); 18 | 19 | Random.seed!(0); 20 | demand_s, prices_sale_s, purchases_s, prices_purchase_s = 21 | ((@SVector rand(10)).*100, SVector{10}(300:10:390), 22 | (@SVector rand(10)).*100, SVector{10}(100:10:190)); 23 | 24 | julia> @btime profit($demand, $prices_sale, 25 | $purchases, $prices_purchase) 26 | 27 | julia> @btime profit($demand_s, $prices_sale_s, 28 | $purchases_s, $prices_purchase_s) 29 | 30 | 31 | julia> a=rand(5, 5); 32 | julia> b=rand(5, 5); 33 | julia> @btime $a*$b; 34 | julia> as=rand(SMatrix{5,5}); 35 | julia> bs=rand(SMatrix{5,5}); 36 | julia> @btime $as*$bs; 37 | 38 | julia> @time m1 = rand(SMatrix{1,10}); 39 | julia> @time m1 = rand(SMatrix{1,100}); 40 | julia> @time m1 = rand(SMatrix{1,200}); 41 | julia> @time m1 = rand(SMatrix{1,500}); 42 | 43 | julia> @time m1 = rand(SMatrix{1,500}); 44 | -------------------------------------------------------------------------------- /Chapter03/01. Managing streams, and reading and writing files/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia iotest.jl 2 | 3 | 4 | $ more iotest.txt | julia iotest.jl 1> ioout1.txt 2> ioout2.txt 5 | $ more ioout1.txt 6 | $ more ioout2.txt 7 | 8 | 9 | $ more iotest2.txt | julia iotest.jl 1> ioout1.txt 2> ioout2.txt 10 | $ more ioout1.txt 11 | $ more ioout2.txt 12 | 13 | 14 | julia> f = open("my_data.txt", "w") 15 | 16 | julia> write(f, "first line\nsecond line\n") 17 | 18 | julia> println(f, "last line") 19 | julia> close(f) 20 | 21 | 22 | julia> f = open("my_data.txt", "r") 23 | julia> readline(f) 24 | julia> readline(f) 25 | julia> readline(f) 26 | 27 | julia> eof(f) 28 | 29 | julia> readline(f) 30 | 31 | julia> close(f) 32 | 33 | 34 | julia> lines = open("my_data.txt", "r") do f 35 | readlines(f) 36 | end 37 | 38 | 39 | julia> data = open("my_data.txt", "r") do f 40 | read(f) 41 | end 42 | 43 | julia> text = String(data); 44 | 45 | julia> length(data) 46 | 47 | julia> text = read(f, String); 48 | 49 | 50 | julia> ] 51 | 52 | (v1.0) pkg> add StringEncodings 53 | 54 | 55 | using StringEncodings 56 | data = open("my_data.txt", "r") do f 57 | read(f) 58 | end; 59 | txt = StringEncodings.decode(data,"Windows-1250") 60 | 61 | 62 | open("my_data.txt", "w") do f 63 | write(f, "line\nsecond line") 64 | end 65 | -------------------------------------------------------------------------------- /Chapter04/02. Executing loops efficiently with conditional statements/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Random, BenchmarkTools 2 | julia> Random.seed!(1); 3 | julia> x = randn(10^6); 4 | 5 | 6 | julia> @btime sum(v for v in x if v > 0) 7 | julia> function possum1(x) 8 | s = zero(eltype(x)) 9 | for v in x 10 | if v > 0 11 | s += v 12 | end 13 | end 14 | s 15 | end 16 | julia> @btime possum1(x) 17 | julia> function possum2a(x) 18 | s = zero(eltype(x)) 19 | for v in x 20 | s += ifelse(v > 0, v, zero(s)) 21 | end 22 | s 23 | end 24 | julia> @btime possum2a(x) 25 | julia> function possum2b(x) 26 | s = zero(eltype(x)) 27 | @simd for v in x 28 | s += ifelse(v > 0, v, zero(s)) 29 | end 30 | s 31 | end 32 | julia> @btime possum2b(x) 33 | 34 | 35 | julia> function possum2c(x) 36 | s = 0 37 | for v in x 38 | s += ifelse(v > 0, v, 0) 39 | end 40 | s 41 | end 42 | julia> @btime possum2c(x) 43 | 44 | 45 | julia> function possum2d(x::AbstractArray{T}) where T 46 | s = zero(T) 47 | @simd for v in x 48 | s += ifelse(v > 0, v, zero(T)) 49 | end 50 | s 51 | end 52 | julia> @btime possum2d(x) 53 | -------------------------------------------------------------------------------- /Chapter02/03. Implementing a custom pseudo-random number generator/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Random 2 | julia> mutable struct Xorshift <: AbstractRNG 3 | state::UInt64 4 | end 5 | julia> Xorshift() = Xorshift(rand(RandomDevice(), UInt64)) 6 | julia> Random.seed!(r::Xorshift, seed::UInt64 = rand(RandomDevice(), UInt64)) = r.state = seed 7 | julia> function xorshift_rand(r::Xorshift) 8 | state = r.state 9 | state ⊻= state << 13 10 | state ⊻= state >> 7 11 | state ⊻= state << 17 12 | r.state = state 13 | end 14 | julia> const XorshiftSamplers = Union{map(T->Random.SamplerType{T}, 15 | [Bool, UInt32, Int32, UInt64, Int64])...} 16 | julia> Base.rand(r::Xorshift, sampler::XorshiftSamplers) = xorshift_rand(r) % sampler[] 17 | julia> Random.rng_native_52(::Xorshift) = UInt64 18 | julia> using StatsBase 19 | julia> r = Xorshift(0x0139408dcbbf7a44) 20 | julia> countmap(rand(r, 1:10, 10^8)) 21 | julia> const X = zeros(Int, 5, 5) 22 | julia> foreach(i -> X[rand(r, 1:5), rand(r, 1:5)] += 1, 1:10^7) 23 | julia> 25 * X / 10^7 24 | 25 | 26 | julia> rand(Xorshift(), UInt128) 27 | julia> function Base.rand(r::Xorshift, sampler::Random.SamplerType{UInt128}) 28 | r1 = rand(r, UInt64) 29 | r2 = rand(r, UInt64) 30 | (UInt128(r1) << 64) | r2 31 | end 32 | julia> rand(Xorshift(), UInt128) 33 | julia> using BenchmarkTools 34 | julia> r = Xorshift() 35 | julia> @benchmark rand() 36 | julia> @benchmark rand($r) 37 | -------------------------------------------------------------------------------- /Chapter03/07. Using object serialization in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add JLD2 3 | (v1.0) pkg> add FileIO 4 | (v1.0) pkg> add BSON 5 | 6 | 7 | using Serialization 8 | 9 | x = 1:5; 10 | open(f -> serialize(f,x), "x.jls", "w"); 11 | 12 | y = open(deserialize, "x.jls"); 13 | 14 | julia> dump(y) 15 | 16 | open("data.jls", "w") do f 17 | serialize(f,Array{Int8}([1, 2, 4])); 18 | serialize(f,Dict{Int64,String}(1=>"a", 2=>"b")); 19 | end 20 | 21 | julia> f = open("data.jls", "r"); 22 | julia> deserialize(f) 23 | julia> deserialize(f) 24 | julia> close(f) 25 | 26 | 27 | using JLD2 28 | using FileIO 29 | 30 | x1 = 1:5; 31 | x2 = rand(3); 32 | file = File(format"JLD2","myfile.jld2") 33 | save(file, "x1", x1, "x2", x2) 34 | 35 | julia> data = load(file) 36 | 37 | julia> dump(data["x1"]) 38 | 39 | 40 | using BSON 41 | 42 | x = 1:5 43 | d = Dict{Int64, String}(1=>"a", 2=>"b") 44 | e = 5+3im 45 | 46 | f = open("data.bson", "w") 47 | bson(f, Dict("x" => x, "d" => d)) 48 | bson(f, Dict("e" => e)) 49 | close(f) 50 | 51 | julia> f = open("data.bson", "r"); 52 | julia> BSON.load(f) 53 | julia> BSON.load(f) 54 | julia> close(f) 55 | 56 | julia> ] 57 | (v1.0) pkg> add CodecZlib 58 | 59 | using CodecZlib 60 | using Serialization 61 | d=Dict([("txt", collect(1:1000000))]); 62 | open("big2.bin", "w") do f 63 | comp = DeflateCompressorStream(f); 64 | serialize(comp, d); 65 | close(comp); 66 | end 67 | 68 | f=open("big2.bin", "r"); 69 | decomp = DeflateDecompressorStream(f); 70 | d2=deserialize(decomp); 71 | close(f); 72 | -------------------------------------------------------------------------------- /Chapter06/07. The efficiency of mutable versus immutable types/walk.jl: -------------------------------------------------------------------------------- 1 | abstract type AbstractPoint end 2 | 3 | struct PointI <: AbstractPoint 4 | x::Int 5 | y::Int 6 | end 7 | 8 | mutable struct PointM <: AbstractPoint 9 | x::Int 10 | y::Int 11 | end 12 | 13 | PointM(p::PointM) = PointM(p.x, p.y) 14 | 15 | d(p::AbstractPoint) = abs(p.x) + abs(p.y) 16 | move(p::PointI, d::PointI) = PointI(p.x+d.x, p.y+d.y) 17 | move(p::PointM, d::PointM) = (p.x += d.x; p.y += d.y; p) 18 | 19 | function simI() 20 | maxd = 0 21 | x = PointI(0, 0) 22 | @inbounds for i in 1:10^6 23 | x = move(x, PointI(2rand(Bool)-1, 2rand(Bool)-1)) 24 | curd = d(x) 25 | maxd = max(maxd, curd) 26 | end 27 | maxd 28 | end 29 | 30 | function simM() 31 | maxd = 0 32 | x = PointM(0, 0) 33 | m = PointM(0, 0) 34 | @inbounds for i in 1:10^6 35 | m.x, m.y = 2rand(Bool)-1, 2rand(Bool)-1 36 | move(x, m) 37 | curd = d(x) 38 | maxd = max(maxd, curd) 39 | end 40 | maxd 41 | end 42 | 43 | function simI2() 44 | path = PointI[] 45 | x = PointI(0, 0) 46 | @inbounds for i in 1:10^6 47 | push!(path, x) 48 | x = move(x, PointI(2rand(Bool)-1, 2rand(Bool)-1)) 49 | end 50 | path 51 | end 52 | 53 | function simM2() 54 | path = PointM[] 55 | x = PointM(0, 0) 56 | m = PointM(0, 0) 57 | @inbounds for i in 1:10^6 58 | push!(path, PointM(x)) 59 | m.x, m.y = 2rand(Bool)-1, 2rand(Bool)-1 60 | move(x, m) 61 | end 62 | path 63 | end 64 | -------------------------------------------------------------------------------- /Chapter01/04. Building Julia from sources on Linux/commands.txt: -------------------------------------------------------------------------------- 1 | $ sudo apt update 2 | $ sudo apt install --yes build-essential python-minimal gfortran m4 cmake pkg-config libssl-dev 3 | 4 | 5 | $ git clone git://github.com/JuliaLang/julia.git 6 | $ cd julia 7 | $ git checkout v1.0.1 8 | 9 | $ make -j $((`nproc`-1)) 1>build_log.txt 2>build_error.txt 10 | 11 | 12 | $ cd ~ 13 | # Get link from MKL website 14 | $ wget http://registrationcenter-download.intel.com/[go to Intel MKL web site to get link]/l_mkl_2019.0.117.tgz 15 | $ tar zxvf l_mkl_2019.0.117.tgz 16 | $ cd l_mkl_2019.0.117 17 | $ sudo bash install.sh 18 | 19 | cd ~/julia 20 | echo "USEICC = 0" >> Make.user 21 | echo "USEIFC = 0" >> Make.user 22 | echo "USE_INTEL_MKL = 1" >> Make.user 23 | echo "USE_INTEL_LIBM = 0" >> Make.user 24 | source /opt/intel/bin/compilervars.sh intel64 25 | make -j $((`nproc`-1)) 1>build_log.txt 2>build_error.txt 26 | 27 | 28 | $ cd ~ 29 | # Get the link from Intel C++ compilers website 30 | $ wget http://[go to Intel to get link]/parallel_studio_xe_2018_update3_professional_edition.tgz 31 | $ tar zxvf parallel_studio_xe_2018_update3_professional_edition.tgz 32 | $ cd parallel_studio_xe_2018_update3_professional_edition 33 | $ sudo bash install.sh 34 | 35 | cd ~/julia 36 | echo "USEICC = 0" >> Make.user 37 | echo "USEIFC = 0" >> Make.user 38 | echo "USE_INTEL_MKL = 1" >> Make.user 39 | echo "USE_INTEL_LIBM = 1" >> Make.user 40 | source /opt/intel/bin/compilervars.sh intel64 41 | make -j $((`nproc`-1)) 1>build_log.txt 2>build_error.txt 42 | 43 | 44 | $ sudo ln -s /home/ubuntu/julia/usr/bin/julia /usr/local/bin/julia 45 | -------------------------------------------------------------------------------- /Chapter02/04. Parsing Git logs with regular expressions/parselog.jl: -------------------------------------------------------------------------------- 1 | using DataFrames, DataFramesMeta 2 | 3 | function parselog(lines) 4 | author = r"^Author: ([^<]*) <" 5 | insc = r"^.+changed, ([0-9]+) insertion" 6 | delc = r"^.+changed.*, ([0-9]+) deletion" 7 | authordata = DataFrame(author=String[], action=String[], count=Int[]) 8 | curauthor = "" 9 | for line in lines 10 | m = match(author, line) 11 | m === nothing || (curauthor = m[1]) 12 | m = match(insc, line) 13 | m === nothing || push!(authordata, 14 | (curauthor, "insertion", parse(Int, m[1]))) 15 | m = match(delc, line) 16 | m === nothing || push!(authordata, 17 | (curauthor, "deletion", parse(Int, m[1]))) 18 | end 19 | authorstats = @by(authordata, [:author, :action], 20 | count=sum(:count)) 21 | unstack(authorstats, :action, :count) 22 | end 23 | 24 | function gitstats(dir) 25 | if isdir(dir) 26 | println("\nAnalyzing") 27 | cd(dir) do 28 | try 29 | res = read(`git log --stat`, String) 30 | lines = split(res, ['\r', '\n'], keepempty=false) 31 | df = parselog(lines) 32 | df.all = coalesce.(df.deletion, 0) .+ 33 | coalesce.(df.insertion, 0) 34 | display(sort!(df, :all, rev=true)) 35 | catch 36 | error("Running git log failed") 37 | end 38 | end 39 | else 40 | error("$dir is not a directory") 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /Chapter09/02. Optimization using JuMP/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add JuMP 3 | (v1.0) pkg> add Clp 4 | (v1.0) pkg> add Cbc 5 | 6 | (v1.0) pkg> add Gurobi 7 | 8 | using JuMP 9 | using Clp 10 | 11 | 12 | m = Model(solver = ClpSolver()); 13 | 14 | julia> @variable(m, x₁ >= 0) 15 | julia> @variable(m, x₂ >= 0) 16 | 17 | julia> @objective(m, Min, 50x₁ + 70x₂) 18 | 19 | julia> @constraint(m, 200x₁ + 2000x₂ >= 9000); 20 | julia> @constraint(m, 100x₁ + 30x₂ >= 300); 21 | julia> @constraint(m, 9x₁ + 11x₂ >= 60); 22 | 23 | julia> println(m) 24 | 25 | julia> status = solve(m) 26 | 27 | julia> println("Cost: $(getobjectivevalue(m))\nx₁=$(getvalue(x₁))\nx₂=$(getvalue(x₂))") 28 | 29 | 30 | using JuMP 31 | using Cbc 32 | m = Model(solver = CbcSolver()); 33 | @variable(m, x₁ >= 0) 34 | @variable(m, x₂ >= 0, Int) 35 | @objective(m, Min, 50x₁ + 70x₂) 36 | @constraint(m, 200x₁ + 2000x₂ >= 9000) 37 | @constraint(m, 100x₁ + 30x₂ >= 300) 38 | @constraint(m, 9x₁ + 11x₂ >= 60) 39 | 40 | julia> status = solve(m) 41 | julia> println("Cost: $(getobjectivevalue(m))\nx₁=$(getvalue(x₁))\nx₂=$(getvalue(x₂))") 42 | 43 | 44 | julia> status = solve(m) 45 | 46 | $ grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 47 | 48 | using JuMP 49 | using Gurobi 50 | m = Model(solver = GurobiSolver()); 51 | @variable(m, x₁ >= 0) 52 | @variable(m, x₂ >= 0, Int) 53 | @objective(m, Min, 50x₁ + 70x₂) 54 | @constraint(m, 200x₁ + 2000x₂ >= 9000) 55 | @constraint(m, 100x₁ + 30x₂ >= 300) 56 | @constraint(m, 9x₁ + 11x₂ >= 60) 57 | 58 | julia> status = solve(m) 59 | 60 | julia> println("Cost: $(getobjectivevalue(m))\nx₁=$(getvalue(x₁))\nx₂=$(getvalue(x₂))") 61 | -------------------------------------------------------------------------------- /Chapter02/02. Fast matrix multiplication/fastmatmul.jl: -------------------------------------------------------------------------------- 1 | # take a sequence of matrices and multiply them in an optimal order 2 | function fastmatmul(args::AbstractMatrix...) 3 | length(args) ≤ 1 && return *(args...) 4 | sizes = size.(args) 5 | if !all(sizes[i][2] == sizes[i+1][1] for i in 1:length(sizes)-1) 6 | throw(ArgumentError("matrix dimensions mismatch")) 7 | end 8 | partcost = Dict{Tuple{Int,Int}, Tuple{Int, Int}}() 9 | from, to = 1, length(sizes) 10 | solvemul(sizes, partcost, from, to) 11 | domul(args, partcost, from, to) 12 | end 13 | 14 | # find the optimal sequence of multiplications 15 | function solvemul(sizes, partcost, from, to) 16 | if from == to 17 | partcost[(from, to)] = (0, from) 18 | return 19 | end 20 | mincost = typemax(Int) 21 | minj = -1 22 | for j in from:to-1 23 | haskey(partcost, (from, j)) || solvemul(sizes, partcost, from, j) 24 | haskey(partcost, (j+1, to)) || solvemul(sizes, partcost, j+1, to) 25 | curcost = sizes[from][1]*sizes[j][2]*sizes[to][2] + 26 | partcost[(from, j)][1] + partcost[(j+1, to)][1] 27 | if curcost < mincost 28 | minj = j 29 | mincost = curcost 30 | end 31 | end 32 | partcost[(from, to)] = (mincost, minj) 33 | end 34 | 35 | # perform the multiplication given precomputed information 36 | # about the optimal multiplication order 37 | function domul(args, partcost, from, to) 38 | from == to && return args[from] 39 | from+1 == to && return args[from]*args[to] 40 | j = partcost[(from, to)][2] 41 | domul(args, partcost, from, j) * domul(args, partcost, j+1, to) 42 | end 43 | -------------------------------------------------------------------------------- /Chapter10/01. Multiprocessing in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia 2 | 3 | julia> using Distributed 4 | julia> addprocs(1) 5 | 6 | julia> Distributed.myid() 7 | julia> workers() 8 | 9 | julia> res = @spawnat 2 myid() 10 | julia> fetch(res) 11 | 12 | remote_f = function(s::Int=3) 13 | println("Worker $(myid()) will sleep for $s seconds") 14 | sleep(s) 15 | val=rand(1:1000) 16 | println("Completed worker $(myid()) - return $val") 17 | return val 18 | end 19 | 20 | julia> @fetchfrom 2 remote_f(4) 21 | 22 | function run_timeout(timeout::Int, f::Function, params...) 23 | wid = addprocs(1)[1] 24 | result = RemoteChannel(()->Channel{Tuple}(1)); 25 | @spawnat wid put!(result, (f(params...), myid())) 26 | res = nothing 27 | time_elapsed = 0.0 28 | while time_elapsed < timeout && !isready(result) 29 | sleep(0.25) 30 | time_elapsed += 0.25 31 | end 32 | if !isready(result) 33 | println("Not completed! Computation at $wid will be 34 | terminated!") 35 | else 36 | res = take!(result) 37 | end 38 | rmprocs(wid); 39 | return res 40 | end 41 | 42 | julia> run_timeout(3, remote_f, 2) 43 | 44 | julia> run_timeout(3, remote_f, 10) 45 | 46 | julia> workers() 47 | 48 | 49 | $ julia -p 2 50 | 51 | julia> using Distributed 52 | julia> @everywhere function myF2(); println("myF2 ", myid()); end; 53 | julia> @spawnat workers()[end] myF2(); 54 | 55 | 56 | julia> hello() = println("hello"); 57 | julia> @fetchfrom 2 hello() 58 | julia> f_lambda = () -> hello(); 59 | julia> f_lambda() 60 | julia> @fetchfrom 2 f_lambda() 61 | 62 | julia> @everywhere hello() = println("hello") 63 | julia> @fetchfrom 2 f_lambda() 64 | -------------------------------------------------------------------------------- /Chapter09/04. Complex plotting with Plots.jl/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add DataFrames 3 | (v1.0) pkg> add Plots 4 | (v1.0) pkg> add Distributions 5 | (v1.0) pkg> add StatsPlots 6 | (v1.0) pkg> add CSV 7 | 8 | 9 | using Random 10 | using DataFrames 11 | using Plots 12 | using Distributions 13 | using StatPlots 14 | 15 | Random.seed!(0); 16 | df = DataFrame(x1=randn(1000),x2=randn(1000)); 17 | ρ = 0.8 18 | df.x3 = ρ*df.x1 + √(1-ρ*ρ)*df.x2; 19 | 20 | julia> gr() 21 | 22 | p = histogram(df.x1,nbins=25,labels="x1"); 23 | histogram!(p,df.x3,fillalpha=0.5,bar_width=0.3,labels="x3") 24 | 25 | p = histogram(df.x3,normed=true) 26 | plot!(p,Normal(0,1),width=4) 27 | 28 | plot(scatter(df.x1,df.x3,legend=false),histogram2d(df.x1,df.x3),layout=Plots.GridLayout(1, 2),xlabel="x1",ylabel="x3" ) 29 | 30 | corrplot(convert(Array, df), bins=25,labels=["x1","x2","x3"]) 31 | 32 | 33 | using CSV 34 | iris = CSV.read(joinpath(dirname(pathof(DataFrames)),"..","test/data/iris.csv")); 35 | 36 | font_h6 = Plots.font("Helvetica", 6) 37 | plts = Plots.Plot[] 38 | for i in 1:4, j in 1:4 39 | if i == j 40 | push!(plts,histogram(iris[i],group=iris[:Species], 41 | xlabel=names(iris)[j],ylabel="count", 42 | legend=false,fillalpha=0.5, 43 | guidefont=font_h6,tickfont=font_h6)) 44 | else 45 | push!(plts,scatter(iris[j],iris[i], 46 | xlabel=names(iris)[j],ylabel=names(iris)[i], 47 | group=iris[:Species],legend=(i==4&&j==1), 48 | guidefont=font_h6,tickfont=font_h6,legendfont=font_h6, 49 | background_color_legend=RGBA(255,255,255,0.8), 50 | foreground_color_legend=nothing)) 51 | end 52 | end 53 | p = plot(plts...,layout=Plots.GridLayout(4, 4)) 54 | -------------------------------------------------------------------------------- /Chapter03/03. Fetching data from the internet/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add HTTP 3 | (v1.0) pkg> add Gumbo 4 | (v1.0) pkg> add Cascadia 5 | 6 | 7 | using HTTP, Gumbo, Cascadia 8 | 9 | r=HTTP.get("https://github.com/JuliaWeb"); 10 | 11 | page_body = String(r.body); 12 | 13 | h = Gumbo.parsehtml(page_body); 14 | 15 | qs = HTMLElement[] 16 | Cascadia.matchAllInto(sel".d-inline-block.mb-1", h.root,qs); 17 | names_links = Tuple{String, String}[] 18 | for q in qs 19 | name = strip(nodeText(children(q)[1][1])) 20 | link = children(q)[1][1].attributes["href"] 21 | push!(names_links, (name,link)) 22 | end 23 | 24 | julia> names_links 25 | 26 | julia> stats = Dict{String,String}() 27 | julia> @sync for (name, link) in names_links 28 | @async begin 29 | r2=HTTP.get("https://github.com"*link); 30 | h2 = parsehtml(String(r2.body)); 31 | qs2 = HTMLElement[] 32 | Cascadia.matchAllInto(sel".social-count.js-social-count", 33 | h2.root, qs2); 34 | stats[name] = strip(nodeText(qs2[1])) 35 | end 36 | end 37 | 38 | 39 | julia> stats 40 | 41 | julia> stats2 = Dict(key => parse(Int64, stats[key]) for key in keys(stats)) 42 | 43 | julia> m=maximum(values(stats2)) 44 | 45 | 46 | julia> ] 47 | (v1.0) pkg> add PyCall 48 | (v1.0) pkg> add Conda 49 | 50 | using PyCall 51 | using Conda 52 | Conda.add("scrapy") 53 | 54 | @pyimport scrapy.selector as ssel 55 | 56 | julia> s = ssel.Selector(text=page_body) 57 | 58 | julia> elems = s[:xpath]("//a[@itemprop='name codeRepository']") 59 | 60 | julia> strip(elems[1][:xpath]("text()")[1][:extract]()) 61 | 62 | julia> strip(elems[2][:xpath]("text()")[1][:extract]()) 63 | 64 | julia> a = elems[1][:xpath]("@href")[1][:extract]() 65 | 66 | julia> a = elems[2][:xpath]("@href")[1][:extract]() 67 | -------------------------------------------------------------------------------- /Chapter10/03. Multithreading in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | # on Windows 2 | C:\ set JULIA_NUM_THREADS=4 3 | 4 | # on Linux 5 | $ export JULIA_NUM_THREADS=4 6 | 7 | 8 | $ julia 9 | 10 | julia> Threads.nthreads() 11 | 12 | 13 | using DataFrames, BenchmarkTools, Random, Statistics 14 | 15 | Random.seed!(0); 16 | N = 100_000; 17 | const data = DataFrame(rowtype = rand(1:12, N)); 18 | data.x1 = data.rowtype .* randn(N); 19 | 20 | julia> head(data, 5) 21 | 22 | function stats(df) 23 | m = MersenneTwister() 24 | median_val = 0 25 | for i in 1:100 26 | median_val += median(rand(m, df.x1, nrow(df))) 27 | end 28 | return (rowtype=df.rowtype[1], n=nrow(df), 29 | tid=Threads.threadid(), median=median_val / 100) 30 | end 31 | 32 | julia> @time by(data, :rowtype, stats) 33 | 34 | julia> @time by(data, :rowtype, stats); 35 | 36 | function threaded_by(df::DataFrame, groupcol::Symbol, f::Function) 37 | groups = groupby(df, groupcol) 38 | f(view(groups[1], 1:2, :)); #needed for precompilation! 39 | res = Vector{NamedTuple}(undef, length(groups)) 40 | Threads.@threads for g in 1:length(groups) 41 | rv = f(groups[g]) 42 | res[g] = rv 43 | end 44 | DataFrame(rowtype=getfield.(res, groupcol), x1=res) 45 | end 46 | 47 | julia> @time threaded_by(data, :rowtype, stats) 48 | 49 | julia> @time threaded_by(data, :rowtype, stats); 50 | 51 | 52 | julia> Threads.nthreads() 53 | julia> total = 0; 54 | julia> Threads.@threads for i in 1:1_000_000 55 | global total = total + 1 56 | end 57 | julia> total 58 | 59 | julia> total2 = 0; 60 | julia> s = Threads.SpinLock() 61 | julia> Threads.@threads for i in 1:1_000_000 62 | Threads.lock(s) 63 | global total2 = total2 + 1 64 | Threads.unlock(s) 65 | end 66 | julia> total2 67 | -------------------------------------------------------------------------------- /Chapter10/02. Sending parameters to remote Julia processes/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add ParallelDataTransfer 3 | 4 | $ julia -p 4 5 | 6 | julia> using Distributed 7 | julia> nworkers() 8 | 9 | 10 | using Distributed 11 | @everywhere using ParallelDataTransfer 12 | 13 | @everywhere function rule30(ca::Array{Bool}) 14 | lastv = ca[1] 15 | for i in 2:(length(ca)-1) 16 | current = ca[i] 17 | ca[i] = xor(lastv, ca[i] || ca[i+1]) 18 | lastv = current 19 | end 20 | end 21 | 22 | @everywhere function getsetborder(ca::Array{Bool}, 23 | neighbours::Tuple{Int64,Int64}) 24 | ca[1] = (@fetchfrom neighbours[1] caa[end-1]) 25 | ca[end] = (@fetchfrom neighbours[2] caa[2]) 26 | end 27 | 28 | function printsimdist(workers::Array{Int}) 29 | for w in workers 30 | dat = @fetchfrom w caa 31 | for b in dat[2:end-1] 32 | print(b ? "#" : " ") 33 | end 34 | end 35 | println() 36 | end 37 | 38 | function runca(steps::Int, visualize::Bool) 39 | @sync for w in workers() 40 | @async @fetchfrom w fill!(caa, false) 41 | end 42 | @fetchfrom wks[Int(nwks/2)+1] caa[2]=true 43 | visualize && printsimdist(workers()) 44 | for i in 1:steps 45 | @sync for w in workers() 46 | @async @fetchfrom w getsetborder(caa, neighbours) 47 | end 48 | @sync for w in workers() 49 | @async @fetchfrom w rule30(caa) 50 | end 51 | visualize && printsimdist(workers()) 52 | end 53 | end 54 | 55 | wks = workers() 56 | nwks = length(wks) 57 | for i in 1:nwks 58 | sendto(wks[i],neighbours = (i==1 ? wks[nwks] : wks[i-1], 59 | i==nwks ? wks[1] : wks[i+1])) 60 | fetch(@defineat wks[i] const caa = zeros(Bool, 15+2)); 61 | end 62 | 63 | julia> runca(20,true) 64 | -------------------------------------------------------------------------------- /Chapter03/06. Working with date and time/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Dates 2 | 3 | julia> ts = Dates.now() 4 | 5 | julia> typeof(ts) 6 | 7 | julia> Date(2018, 08, 15) 8 | 9 | julia> Date(2018, 8) 10 | 11 | julia> Date(2018) 12 | 13 | julia> DateTime(2018, 8, 15, 18, 22, 55, 123) 14 | 15 | julia> dt1 = Date("2018-08-15T18:22:55.123", DateFormat("y-m-dTH:M:S.s")) 16 | 17 | julia> dtm1 = DateTime("2018-08-15T18:22:55.123", DateFormat("y-m-dTH:M:S.s")) 18 | 19 | julia> dtm2 = DateTime("2018-08-16T19:32:55.223", dateformat"y-m-dTH:M:S.s") 20 | 21 | 22 | julia> delta = dtm2 - dtm1 23 | julia> typeof(delta) 24 | 25 | 26 | julia> Date("2018-03-01") - Date("2018-02-01") 27 | julia> dtm1 + Year(1) + Month(4) + Day(4) + Hour(3) 28 | julia> dtm1 + Millisecond(24*3600*1000) 29 | 30 | 31 | julia> Year(1)*3 + Month(4)/2 + Day(4)*3 + Hour(3) 32 | 33 | julia> Day(4)/2 34 | 35 | julia> Day(4)/8 36 | 37 | julia> Day(4)*0.5 38 | 39 | julia> d1 = Date("2018-08-15") 40 | julia> dump(d1) 41 | 42 | 43 | julia> d1 = Date("2018-08-15") 44 | julia> d1 - Day(d1.instant.periods.value) + Day(1) 45 | 46 | 47 | julia> dt = DateTime("2018-08-15T18:22:55.123") 48 | julia> dump(dt) 49 | 50 | 51 | julia> dt = DateTime("2018-08-15T18:22:55.123"); 52 | julia> dt - Millisecond(dt.instant.periods.value) + Day(1) 53 | 54 | 55 | julia> Date(2008, 1, 31) + Month(1) 56 | julia> Date(2008, 1, 31) + Month(1) - Month(1) 57 | julia> Date(2008, 1, 31) + (Month(1) - Month(1)) 58 | 59 | 60 | julia> ] 61 | (v1.0) pkg> add TimeZones 62 | 63 | using TimeZones 64 | 65 | julia> dtz = parse(ZonedDateTime, "2017-11-14 11:03:53 +0100", 66 | dateformat"yyyy-mm-dd HH:MM:SS zzzzz") 67 | 68 | 69 | julia> dtz = parse(ZonedDateTime, "22/Aug/2018:09:22:07 -0100", 70 | DateFormat("dd/uuu/yyyy:H:M:S zzzzz")) 71 | 72 | julia> dump(dtz) 73 | 74 | 75 | julia> using Dates 76 | julia> dtz2 = dtz + Dates.Year(2) - Dates.Month(1) + Dates.Day(20) 77 | -------------------------------------------------------------------------------- /Chapter03/04. Writing a simple RESTful service/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add JuliaWebAPI 3 | (v1.0) pkg> add ZMQ 4 | (v1.0) pkg> add HTTP 5 | 6 | 7 | julia> using Sockets 8 | 9 | julia> server = Sockets.listen(8080) 10 | 11 | julia> while true 12 | sock = Sockets.accept(server) 13 | data = readline(sock) 14 | print("Got request:\n", data, "\n") 15 | cmd = split(data, " ")[2][2:end] 16 | println(sock, "\nHTTP/1.1 200 OK\nContent-Type: text/html\n") 17 | println(sock, string("", cmd, "=", eval(Meta.parse(cmd)), "")) 18 | close(sock) 19 | end #note that this will block the Julia console 20 | 21 | $ curl http://127.0.0.1:8080/5+9 22 | 23 | 24 | julia> using Sockets 25 | julia> client = Sockets.connect("127.0.0.1", 8080) 26 | 27 | julia> write(client, "GET /3*8\n") 28 | 29 | julia> readlines(client) 30 | 31 | using JuliaWebAPI 32 | using ZMQ 33 | 34 | function testfn(arg1, arg2; optarg1="10", optarg2="20") 35 | println("T: ", arg1, " ", arg2, " ", optarg1, " ", optarg2) 36 | return parse(Int, arg1)+parse(Int, arg2)+parse(Int, optarg1)+parse(Int, optarg2) 37 | end 38 | 39 | 40 | tr = JuliaWebAPI.ZMQTransport("tcp://127.0.0.1:9999", ZMQ.REP, true) 41 | apir = JuliaWebAPI.APIResponder(tr, JuliaWebAPI.JSONMsgFormat()) 42 | 43 | 44 | julia> register(apir, testfn; resp_json=true, 45 | resp_headers=Dict("Content-Type" => "application/json; charset=utf-8")) 46 | 47 | 48 | julia> process(apir) #note that this will block the Julia console 49 | 50 | 51 | using JuliaWebAPI 52 | const apiclnt = JuliaWebAPI.APIInvoker("tcp://127.0.0.1:9999"); 53 | 54 | julia> JuliaWebAPI.run_http(apiclnt, 8888) #note that this will block the Julia console 55 | 56 | 57 | $ curl "http://127.0.0.1:8888/testfn/5/9?optarg1=100&optarg2=1000" 58 | 59 | 60 | julia> process(apir) 61 | 62 | 63 | julia> using HTTP 64 | julia> res = HTTP.get("http://127.0.0.1:8888/testfn/5/9?optarg1=100&optarg2=1000") 65 | -------------------------------------------------------------------------------- /Chapter05/01. Understanding subtyping in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> struct Point{T<:Integer, S<:AbstractString} 2 | pos::Complex{T} 3 | label::S 4 | end 5 | julia> Point(x::T, y::T, label::S) where {T<:Integer, 6 | S<:AbstractString} = 7 | Point{T,S}(Complex(x,y), label) 8 | julia> Point(x, y, label) = Point(promote(Integer.((x,y))...)..., 9 | label) 10 | julia> p1 = Point(1, 0, "1") 11 | julia> p2 = Point(1, 0, SubString("1", 1)) 12 | julia> p3 = Point(true, false, "1") 13 | julia> p4 = Point(2, 0, "2") 14 | julia> [p1, p2, p3, p4] 15 | julia> [p1, p2] 16 | julia> [p1, p3] 17 | julia> [p1, p4] 18 | julia> sumpoint1(v::AbstractVector{Point}) = 19 | Point(sum(p.pos for p in v), "") 20 | julia> sumpoint1([p1, p2]) 21 | julia> sumpoint2(v::AbstractVector{<:Point}) = 22 | Point(sum(p.pos for p in v), "") 23 | julia> sumpoint2([p1, p2]) 24 | julia> foo(p::Point) = "generic definition" 25 | julia> foo(p::Point{Int, <:AbstractString}) = "default Int passed" 26 | julia> foo(p::Point{<:Integer, String}) = "default String passed" 27 | julia> foo(Point(true, true, s"12")) 28 | julia> foo(Point(1, 1, s"12")) 29 | julia> foo(Point(true, true, "12")) 30 | julia> foo(Point(1, 1, "12")) 31 | julia> foo(p::Point{Int, String}) = "most specific method" 32 | julia> foo(Point(1, 1, "12")) 33 | 34 | 35 | julia> push!([p1], p2) 36 | julia> push!(Point[p1], p2) 37 | julia> Int <: Integer 38 | julia> Point{Int, String} <: Point{Integer, String} 39 | julia> Point{Int, String} <: Point{<:Integer, String} 40 | julia> Point{Int, String} <: Point{T, String} where T<:Integer 41 | julia> Point{Int} 42 | julia> Point{<:Signed, String} 43 | julia> Point{Int}{String} 44 | julia> Point{Int, String} 45 | julia> sumpoint1(Point[p1, p2]) 46 | 47 | 48 | julia> methods(foo) 49 | julia> Tuple{Point{Int, String}, Point{Bool, SubString{String}}} <: Tuple{Point{Int}, Point} 50 | julia> sumpoint_tuple(v::Tuple{Vararg{Point}}) = 51 | Point(sum(p.pos for p in v), "") 52 | julia> sumpoint_tuple((p1, p2, p3)) 53 | -------------------------------------------------------------------------------- /Chapter10/04. Distributed computing with Julia/commands.txt: -------------------------------------------------------------------------------- 1 | $ ssh-keygen -P "" -t rsa -f ~/.ssh/cluster 2 | 3 | $ cat ~/.ssh/cluster.pub >> ~/.ssh/authorized_keys 4 | 5 | $ ssh ubuntu@localhost 6 | 7 | $ julia --machine-file machinefile.txt 8 | 9 | julia> using Distributed 10 | 11 | julia> nworkers() 12 | 13 | julia> ] 14 | (v1.0) pkg> add Distributions 15 | (v1.0) pkg> add DataFrames 16 | 17 | using Distributed 18 | 19 | @everywhere using Distributed, Distributions, DataFrames, Random 20 | 21 | @everywhere function sim_inventory(reorder_q::Int64, 22 | reorder_point::Int64; 23 | days = 100, 24 | sd = Normal(20,20^0.5), #daily sales distribution 25 | wh = 0.1, #warehousing costs 26 | p = 4.0, #unit profit (when sold) 27 | d_prob = 0.50, #probability of delivery at given day 28 | k = 60.0, # fixed delivery cost 29 | rng = MersenneTwister(0)) 30 | profit = 0.0 # Cumulated profit 31 | stock = reorder_q 32 | for day in 1:days 33 | if stock < reorder_point && rand(rng)< d_prob # an order arrives 34 | profit -= k # we pay for the delivery 35 | stock += reorder_q 36 | end 37 | sale = max(0, min(Int(round(rand(rng, sd))), stock)) 38 | stock -= sale # decrease stock 39 | profit += p*sale - wh*stock # gain profit 40 | end 41 | return profit / days 42 | end 43 | 44 | julia> sweep = vec(collect(Base.product(10:10:250,10:10:250,20:5:60))) 45 | julia> Random.seed!(0); 46 | julia> Random.shuffle!(sweep); 47 | 48 | const rngs = Dict(i=>MersenneTwister(i) for i in workers()); 49 | 50 | res = @distributed (append!) for s in sweep 51 | rng = deepcopy(rngs[myid()]) 52 | profit = 0.0 53 | for sim in 1:10000 54 | profit += sim_inventory(s[1],s[2],days=s[3], rng=rng) 55 | end 56 | DataFrame(worker=myid(), reorder_q=s[1], reorder_point=s[2], 57 | days=s[3], profit=profit/10000) 58 | end 59 | 60 | julia> res 61 | 62 | julia> sort!(vcat(view.(sort!.(DataFrame.(groupby(res,:days)), 63 | :profit, rev=true), 1)...), 64 | :days) 65 | -------------------------------------------------------------------------------- /Chapter03/09. Reading and writing Microsoft Excel files/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add PyCall 3 | (v1.0) pkg> add Conda 4 | (v1.0) pkg> add DataFrames 5 | (v1.0) pkg> add XLSX 6 | 7 | julia> using Conda 8 | julia> Conda.add("openpyxl") 9 | 10 | 11 | using PyCall 12 | using Dates 13 | using Random 14 | @pyimport openpyxl as xl 15 | 16 | julia> wb = xl.Workbook(); 17 | julia> ws = wb[:active] 18 | 19 | julia> ws[:cell](1, 1, "Data generated on:") 20 | julia> ws[:cell](2, 1, Dates.now()) 21 | 22 | Random.seed!(0) 23 | dat = rand(3, 5) 24 | for i in 1:size(dat)[1] 25 | ws[:append]((dat[i, :]..., )) 26 | end 27 | 28 | wb[:save]("sample1.xlsx") 29 | 30 | wb = xl.load_workbook(filename = "sample1.xlsx") 31 | ws = wb[:active] 32 | 33 | println(ws[:cell](1, 1)[:value], "\n", ws[:cell](2, 1)[:value]) 34 | 35 | julia> typeof(ws[:cell](2, 1)[:value]) 36 | 37 | julia> ws[:cell](20, 221)[:value] == nothing 38 | 39 | julia> using Printf 40 | julia> for row in ws[:rows] 41 | for cell in row 42 | print("|") 43 | if typeof(cell[:value]) <: Number 44 | @printf("%.3f", cell[:value]) 45 | else 46 | show(cell[:value]) 47 | end 48 | end 49 | println("|") 50 | end 51 | 52 | 53 | wb[:close]() 54 | 55 | 56 | using XLSX 57 | 58 | XLSX.openxlsx("sample2.xlsx", mode="w") do xf 59 | sheet = xf[1] 60 | XLSX.rename!(sheet, "SheetName") 61 | sheet["A1"] = "Data generated on:" 62 | sheet["A2"] = Dates.now() 63 | dat = rand(3, 5) 64 | for row in 1:3 65 | for col in 1:5 66 | XLSX.setdata!(sheet, XLSX.CellRef(2+row, col), rand()) 67 | end 68 | end 69 | end 70 | 71 | julia> wb = XLSX.readxlsx("sample2.xlsx") 72 | 73 | julia> sheetname = XLSX.sheetnames(wb)[1] 74 | julia> ws = wb[sheetname] 75 | 76 | julia> dim = ws.dimension 77 | 78 | XLSX.close(wb) 79 | 80 | using DataFrames 81 | df1 = DataFrame(a=[1, 2, 3], b=[4, 5, 6]); 82 | df2 = DataFrame(x1=[1, 2, 3], x2=["A", "B", "C"]); 83 | 84 | XLSX.writetable("sample3.xlsx", 85 | SheetName1=( DataFrames.columns(df1), DataFrames.names(df1) ), 86 | SheetName2=( DataFrames.columns(df2), DataFrames.names(df2) )); 87 | -------------------------------------------------------------------------------- /Chapter06/01. Metaprogramming/commands.txt: -------------------------------------------------------------------------------- 1 | data=""" 2 | id,val,class 3 | 1,4,A 4 | 2,39,B 5 | 3,44,C 6 | """ 7 | 8 | function new_struct(fields::Vector{Tuple{String,DataType}}) 9 | name = "A" * string(hash(fields), base=16) 10 | code = "begin\nstruct $name\n" 11 | for field in fields 12 | code *= field[1]*"::"*string(field[2])*"\n" 13 | end 14 | eval(Meta.parse(code * "end\n$name\nend")) 15 | end 16 | 17 | julia> MyS = new_struct([("a", Int), ("b", String), ("c", Int)]); 18 | julia> dump(MyS) 19 | 20 | function parse_data(data::AbstractString) 21 | lines = filter(x->length(x)>0, strip.(split(data, ('\n', '\r')))) 22 | colnames = string.(split(lines[1], ',')) 23 | row1=split(lines[2], ',') 24 | coltypes = [occursin(r"^-?\d+$", val) ? Int64 : String for val in row1] 25 | (lines[2:end], new_struct(collect(zip(colnames, coltypes)))) 26 | end 27 | 28 | julia> dump(parse_data("col1,col2,col3\nabc,123,123.5")[2]) 29 | 30 | function parse_text(data::AbstractString) 31 | lines, MyStruct = parse_data(data) 32 | res = MyStruct[] 33 | for line in lines 34 | colvals = split(line, ',') 35 | f = (t, v)->t<:Int ? parse(Int, v) : string(v) 36 | vals = f.(MyStruct.types, colvals) 37 | push!(res, Base.invokelatest(MyStruct, vals...)) 38 | end 39 | return res 40 | end 41 | 42 | julia> parse_text(data) 43 | 44 | 45 | function new_struct2(fields::Vector{Tuple{String,DataType}}) 46 | name = "A" * string(hash(fields), base=16) 47 | c = Expr(:block, 48 | Expr(:struct,false,Symbol(name), 49 | Expr(:block, [Expr(:(::), Symbol(f[1]), 50 | f[2]) for f in fields]...)), 51 | Symbol(name)) 52 | eval(c) 53 | end 54 | 55 | julia> MyS2 = new_struct2([("a", Int), ("b", String), ("c", Int)]); 56 | julia> dump(MyS2) 57 | 58 | julia> MyS == MyS2 59 | 60 | 61 | julia> dump(VERSION) 62 | 63 | julia> function f1() 64 | eval(:(g1() = 10)) 65 | g1() 66 | end 67 | julia> function f2() 68 | eval(:(g2() = 10)) 69 | Base.invokelatest(g2) 70 | end 71 | julia> f1() 72 | julia> f2() 73 | -------------------------------------------------------------------------------- /Chapter05/06. Handling exceptions in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | function loglines(filename, lines...) 2 | f = open(filename, "a") 3 | foreach(line -> (println(f, line)),lines) 4 | sqrt(-2) 5 | close(f) 6 | end 7 | 8 | julia> loglines("mylog.txt", "Test log:") 9 | 10 | julia> try 11 | loglines("mylog.txt", "Test log:") 12 | catch e 13 | dump(e) 14 | end 15 | 16 | for i in 1:100_000 17 | try 18 | loglines("mylog2.txt", string(i)) 19 | catch 20 | end 21 | end 22 | 23 | julia> f = open("mylog2.txt") 24 | 25 | julia> close() 26 | 27 | function loglines2(filename, lines...) 28 | f = open(filename, "a") 29 | try 30 | foreach(line -> println(f, line),lines) 31 | sqrt(-2) 32 | finally 33 | close(f) 34 | end 35 | end 36 | 37 | julia> loglines2("mylog3.txt", "Test log:") 38 | 39 | open("mylog3.txt", "w") do f end #make sure the file is empty 40 | for i in 1:100_000 41 | try 42 | loglines2("mylog3.txt", string(i)) 43 | catch e 44 | if !(e isa DomainError) 45 | rethrow(e) 46 | end 47 | end 48 | end 49 | 50 | julia> lines = open("mylog3.txt") do f 51 | readlines(f) 52 | end; 53 | 54 | julia> all([lines[i]==string(i) for i in 1:100_000]) 55 | 56 | 57 | julia> function ff(a) 58 | res = missing 59 | try 60 | res = sqrt(a) 61 | finally 62 | return res 63 | end 64 | end; 65 | 66 | julia> ff(4) 67 | julia> ff(-2) 68 | 69 | function divide(a, b) 70 | b == 0 && @warn "Division by zero" 71 | a/b 72 | end 73 | 74 | julia> divide(3, 5) 75 | julia> divide(3, 0) 76 | 77 | function divide2(a, b) 78 | b == zero(typeof(b)) && @error "Division by zero" 79 | a/b 80 | end 81 | 82 | julia> divide2(3, 0) 83 | 84 | function divide3(a, b) 85 | if b == zero(typeof(b)) 86 | @error "Division by zero" 87 | throw(ErrorException("Division by zero")) 88 | end 89 | a/b 90 | end 91 | 92 | julia> divide3(3, 0) 93 | 94 | julia> subtypes(Exception) 95 | 96 | julia> struct MyException <: Exception 97 | msg::String 98 | end 99 | julia> throw(MyException("something went wrong")) 100 | 101 | 102 | -------------------------------------------------------------------------------- /Chapter06/02. Macros and generated functions/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add BenchmarkTools 3 | 4 | 5 | function fib(n) 6 | n <= 2 ? 1 : fib(n-1) + fib(n-2) 7 | end 8 | 9 | julia> fib(4) 10 | julia> @time fib(40) 11 | 12 | function memoit(f::Function, p) 13 | if !isdefined(Main, :memoit_cache) 14 | global memoit_cache = Dict{Function,Dict{Any,Any}}() 15 | end 16 | c = haskey(memoit_cache, f) ? memoit_cache[f] : memoit_cache[f]=Dict() 17 | haskey(c, p) ? c[p] : c[p] = f(p) 18 | end 19 | 20 | function fib2(n) 21 | n <= 2 ? 1 : memoit(fib2, n-1) + memoit(fib2, n-2) 22 | end 23 | 24 | julia> fib2(4) 25 | julia> @time fib2(40) 26 | 27 | macro memo(e) 28 | println("macro @memo is run: ", e, " ", e.args) 29 | (!(typeof(e) <: Expr) || !(e.head == :call)) && 30 | error("wrong @memo params") 31 | return quote # we handle only functions with a single parameter 32 | memoit($(esc(e.args[1])), $(esc(e.args[2]))) 33 | end 34 | end 35 | 36 | julia> function fib3(n) 37 | n <= 2 ? 1 : (@memo fib3(n-1)) + (@memo fib3(n-2)) 38 | end 39 | 40 | julia> fib3(4) 41 | julia> @time fib3(40) 42 | 43 | 44 | function sumx1(objs...) 45 | isempty(objs) && return 0 46 | total=objs[1].x 47 | for i in 2:length(objs) 48 | total += objs[i].x 49 | end 50 | total 51 | end 52 | 53 | struct A x::Int end 54 | struct B x::Float64 end 55 | 56 | julia> sumx1(A(5), B(7)) 57 | julia> sumx1(A(5), A(17)) 58 | 59 | @generated function sumx2(objs...) 60 | isempty(objs) && return 0 # default where no arguments were given 61 | total = :(objs[1].x) 62 | for i in 2:length(objs) 63 | total = :($total + objs[$i].x) 64 | end 65 | total 66 | end 67 | 68 | julia> sumx2(A(5), B(7)) == sumx1(A(5), B(7)) 69 | julia> sumx2(A(5), A(17)) == sumx1(A(5), A(17)) 70 | 71 | julia> using BenchmarkTools 72 | julia> const valsx = ([A(i) for i=1:10]..., [B(i) for i=1:10]...); 73 | julia> typeof(valsx) 74 | 75 | julia> @btime sumx1(valsx...) 76 | julia> @btime sumx2(valsx...) 77 | 78 | 79 | julia> macro example(v) 80 | :(($v, $(esc(v)))) 81 | end 82 | julia> function f() 83 | x = 1 84 | @example x 85 | end 86 | julia> x = 10 87 | julia> @macroexpand @example x 88 | 89 | 90 | julia> n=5 91 | julia> macroexpand(Main, :(@memo fib3(n-1))) 92 | 93 | julia> @code_lowered sumx2(A(1), B(2)) 94 | 95 | julia> @code_warntype sumx2(A(1), B(2)) 96 | 97 | julia> @code_warntype sumx1(A(1), B(2)) 98 | -------------------------------------------------------------------------------- /cookbookconf.jl: -------------------------------------------------------------------------------- 1 | using Pkg 2 | 3 | function addandpin(spec) 4 | Pkg.add(PackageSpec(; spec...)) 5 | Pkg.pin(spec.name) 6 | end 7 | 8 | # packages without external dependencies 9 | # that have to be manually instlled 10 | 11 | pkg1 = [(name="StatsBase", version="0.26.0"), 12 | (name="TimeZones", version="0.8.1"), 13 | (name="BSON", version="0.2.1"), 14 | (name="Revise", version="0.7.12"), 15 | (name="Distributions", version="0.16.4"), 16 | (name="Clp", version="0.5.0"), 17 | (name="HTTP", version="0.7.1"), 18 | (name="Gumbo", version="0.5.1"), 19 | (name="StringEncodings", version="0.3.1"), 20 | (name="ZMQ", version="1.0.0"), 21 | (name="CodecZlib", version="0.5.0"), 22 | (name="JSON", version="0.19.0"), 23 | (name="BenchmarkTools", version="0.4.1"), 24 | (name="JuliaWebAPI", version="0.5.0"), 25 | (name="FileIO", version="1.0.2"), 26 | (name="ProfileView", version="0.4.0"), 27 | (name="StaticArrays", version="0.8.3"), 28 | (name="ForwardDiff", version="0.9.0"), 29 | (name="Optim", version="0.17.1"), 30 | (name="JuMP", version="0.18.4"), 31 | (name="JLD2", version="0.1.2"), 32 | (name="XLSX", version="0.4.2"), 33 | (name="Cbc", version="0.4.2"), 34 | (name="DataFrames", version="0.14.1"), 35 | (name="CSV", version="0.4.3"), 36 | (name="DataFramesMeta", version="0.4.0"), 37 | (name="Feather", version="0.5.0"), 38 | (name="FreqTables", version="0.3.0"), 39 | (name="OnlineStats", version="0.19.1"), 40 | (name="MySQL", version="0.7.0"), 41 | (name="Cascadia", version="0.4.0"), 42 | (name="UnicodePlots", version="0.3.1"), 43 | (name="ParallelDataTransfer", version="0.5.0")] 44 | 45 | foreach(addandpin, pkg1) 46 | 47 | # packages that depend on Python Anaconda 48 | # whose manual installation is optional 49 | 50 | pkg2 = [(name="Conda", version="1.0.2"), 51 | (name="PyCall", version="1.18.4"), 52 | (name="PyPlot", version="2.6.3"), 53 | (name="Plots", version="0.20.5"), 54 | (name="StatsPlots", version="0.8.1")] 55 | 56 | foreach(addandpin, pkg2) 57 | 58 | # packages that have external dependencies 59 | # that have to be manually installed 60 | 61 | pkg3 = [(name="RCall", version="0.12.1"), 62 | (name="JDBC", version="0.4.0"), 63 | (name="LibPQ", version="0.5.0"), 64 | (name="Gurobi", version="0.5.3")] 65 | 66 | foreach(addandpin, pkg3) 67 | -------------------------------------------------------------------------------- /Chapter09/05. Building machine learning models with ScikitLearn.jl/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add Conda 3 | (v1.0) pkg> add CSV 4 | (v1.0) pkg> add HTTP 5 | (v1.0) pkg> add DataFrames 6 | (v1.0) pkg> add ScikitLearn 7 | (v1.0) pkg> add PyCall 8 | 9 | using Conda 10 | Conda.runconda(`install mlxtend -c conda-forge -y`) 11 | 12 | 13 | using CSV, HTTP, DataFrames, ScikitLearn, Random, PyCall, Statistics 14 | 15 | dat = HTTP.get("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") 16 | buf = IOBuffer(dat.body[1:end-1]) #cut-off trailing new line character 17 | iris = CSV.read(buf;header=false,allowmissing=:none) 18 | names!(iris,Symbol.(["SepalLength","SepalWidth","PetalLength","PetalWidth","Class"])) 19 | 20 | ua = unique(iris[:Class]) 21 | iris[:Class]=[findfirst(==(x), ua) - 1 for x in iris[:Class]] 22 | y = iris[:Class] 23 | X = Matrix(iris[1:4]) 24 | 25 | using ScikitLearn.CrossValidation: train_test_split 26 | Random.seed!(0) 27 | X_train, X_test, y_train, y_test = 28 | train_test_split(X, y,test_size=0.3,random_state=0,stratify=y); 29 | 30 | @sk_import preprocessing : StandardScaler 31 | stdsc = StandardScaler(); 32 | X_train_std = stdsc[:fit_transform](X_train) 33 | X_test_std = stdsc[:transform](X_test) 34 | 35 | @sk_import linear_model: LogisticRegression 36 | logreg = LogisticRegression(fit_intercept=true) 37 | fit!(logreg,X_train_std, y_train) 38 | y_pred = predict(logreg,X_test_std) 39 | 40 | julia> @sk_import metrics : (accuracy_score,confusion_matrix); 41 | julia> accuracy_score(y_test,y_pred) 42 | julia> confusion_matrix(y_test,y_pred) 43 | 44 | @sk_import ensemble : RandomForestClassifier 45 | forest = RandomForestClassifier(n_estimators=100, random_state=0) 46 | fit!(forest,X_train, y_train) 47 | y_pred = predict(forest,X_test) 48 | importances = forest[:feature_importances_] 49 | indices = sortperm(importances,rev=true) 50 | 51 | julia> DataFrame(Name=names(iris)[indices], Importance=importances[indices]) 52 | 53 | julia> confusion_matrix(y_test,y_pred) 54 | 55 | using PyPlot #important! 56 | @pyimport mlxtend.plotting as mlp 57 | fill_vals=Dict{Int,Float64}() 58 | fill_rngs=Dict{Int,Float64}() 59 | for ind in indices[3:end] 60 | fill_vals[ind-1] = mean(iris[names(iris)[ind]]) 61 | fill_rngs[ind-1] = std(iris[names(iris)[ind]]) 62 | end 63 | mlp.plot_decision_regions(X,y,forest,X_highlight=X_test, 64 | feature_index=(indices[1:2].-1), 65 | filler_feature_values=fill_vals,filler_feature_ranges=fill_rngs) 66 | xlabel(names(iris)[indices[1]]) 67 | ylabel(names(iris)[indices[2]]) -------------------------------------------------------------------------------- /Chapter05/05. Scope of variables in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> a, b = 1, 2; 2 | julia> let a=30, b=40 3 | let b=500 4 | println("inner scope $a $b") 5 | end 6 | println("outer scope $a $b") 7 | end 8 | julia> println("global scope $a $b") 9 | 10 | julia> x=5; 11 | julia> let 12 | println(x+1) 13 | end 14 | julia> let 15 | x = x+1 16 | end 17 | julia> let 18 | global x = x+1 19 | end 20 | 21 | #this function will fail when run 22 | function twogram(s::AbstractString) 23 | twograms = String[] 24 | for (i, c) in enumerate(s) 25 | if i == 1 26 | prev = c 27 | else 28 | push!(twograms, string(prev, c)) 29 | prev = c 30 | end 31 | end 32 | twograms 33 | end 34 | 35 | julia> twogram("ABCD") 36 | 37 | function twogram2(s::AbstractString) 38 | twograms = String[] 39 | local prev 40 | for (i, c) in enumerate(s) 41 | if i == 1 42 | prev = c 43 | else 44 | push!(twograms, string(prev, c)) 45 | prev = c 46 | end 47 | end 48 | twograms 49 | end 50 | 51 | julia> twogram2("ABCD") 52 | 53 | julia> s="ABCD"; 54 | julia> twograms = String[]; 55 | julia> local prev 56 | julia> for (i, c) in enumerate(s) 57 | if i == 1 58 | prev = c 59 | else 60 | push!(twograms, string(prev, c)) 61 | prev = c 62 | end 63 | end 64 | 65 | julia> s="ABCD"; 66 | julia> twograms = String[]; 67 | julia> let 68 | local prev 69 | for (i, c) in enumerate(s) 70 | if i == 1 71 | prev = c 72 | else 73 | push!(twograms, string(prev, c)) 74 | prev = c 75 | end 76 | end 77 | twograms 78 | end 79 | 80 | julia> s="ABCD"; 81 | julia> twograms = String[]; 82 | julia> for (i, c) in enumerate(s) 83 | if i == 1 84 | global prev = c 85 | else 86 | push!(twograms, string(prev, c)) 87 | global prev = c 88 | end 89 | end 90 | julia> twograms 91 | 92 | 93 | module B 94 | x = 1 95 | function getxplusone() 96 | return x+1 97 | end 98 | function increasex() 99 | return x+=1 100 | end 101 | function increasexglob() 102 | return global x+=1 103 | end 104 | end 105 | 106 | julia> B.getxplusone() 107 | julia> B.increasex() 108 | julia> B.increasexglob() 109 | 110 | function f() 111 | x = 1 112 | for a in 1:10 113 | x +=1 114 | end 115 | x 116 | end 117 | 118 | julia> f() 119 | 120 | julia> x=1; 121 | julia> for a in 1:10 122 | x +=1 123 | end 124 | 125 | julia> let 126 | x = 1 127 | for a in 1:10 128 | x +=1 129 | end 130 | x 131 | end 132 | 133 | julia> x = 1 134 | julia> for a in 1:10 135 | global x += 1 136 | end 137 | julia> x 138 | 139 | 140 | julia> z=5; 141 | julia> [(x=z+i;x) for i in 1:2] 142 | 143 | julia> z = 5; 144 | julia> [(z=z+i;z) for i in 1:2] 145 | 146 | julia> z = 5; 147 | julia> [(global z=z+i;z) for i in 1:2] 148 | 149 | julia> let state = 0 150 | global counter() = (state += 1) 151 | end; 152 | julia> counter() 153 | julia> counter() 154 | 155 | julia> state 156 | -------------------------------------------------------------------------------- /Chapter07/05. Handling missing data/grades.csv: -------------------------------------------------------------------------------- 1 | Prefix,Assignment,Tutorial,Midterm,TakeHome,Final 2 | 05,57.14,34.09,64.38,51.48,52.50 3 | 08,95.05,105.49,67.50,99.07,68.33 4 | 08,83.70,83.17,30.0,63.15,48.89 5 | 07,81.22,96.06,49.38,105.93,80.56 6 | 08,91.32,93.64,95.0,107.41,73.89 7 | 07,95.0,92.58,93.12,97.78,68.06 8 | 08,95.05,102.99,56.25,99.07,50.0 9 | 07,72.85,86.85,60.0,,56.11 10 | 08,84.26,93.10,47.50,18.52,50.83 11 | 07,90.10,97.55,51.25,88.89,63.61 12 | 07,80.44,90.20,75.0,91.48,39.72 13 | 06,86.26,80.60,74.38,87.59,77.50 14 | 08,97.16,103.71,72.50,93.52,63.33 15 | 07,91.28,83.53,81.25,99.81,92.22 16 | 08,84.80,89.08,44.38,16.91,35.83 17 | 07,93.83,95.43,88.12,80.93,90.0 18 | 08,84.80,89.08,47.50,16.91,53.33 19 | 04,92.01,102.52,38.75,86.11,49.17 20 | 08,55.14,81.85,75.0,56.11,62.50 21 | 08,93.04,82.93,79.38,83.33,91.11 22 | 08,63.40,86.21,63.12,72.78,, 23 | 08,75.27,97.52,63.12,61.11,66.11 24 | 08,63.78,76.21,39.38,42.22,34.44 25 | 07,80.44,90.20,46.25,91.48,72.22 26 | 07,53.36,82.01,74.38,102.59,56.39 27 | 06,91.28,95.24,82.50,97.59,92.78 28 | 08,82.45,86.65,93.12,85.56,89.17 29 | 08,75.27,86.67,69.38,61.11,88.89 30 | 08,91.32,94.89,76.25,107.41,85.56 31 | 07,91.62,65.18,71.88,90.0,45.56 32 | 07,98.58,102.46,67.50,97.59,63.33 33 | 07,86.26,88.57,70.0,87.59,55.0 34 | 08,67.29,95.64,48.12,72.22,43.33 35 | 07,98.58,91.03,101.25,104.26,107.78 36 | 08,85.42,95.67,56.25,103.52,64.72 37 | 05,88.09,63.39,74.38,93.70,50.83 38 | 06,95.05,70.24,52.50,52.41,47.78 39 | 07,89.89,57.97,32.50,85.19,51.67 40 | 06,90.74,89.64,61.25,90.0,, 41 | 07,95.0,94.36,89.38,100.93,85.0 42 | 06,28.14,58.51,72.50,53.70,68.33 43 | 07,95.14,82.67,110.0,89.81,90.83 44 | 07,92.01,112.58,86.25,86.11,83.33 45 | 07,86.26,74.66,85.0,64.07,82.22 46 | 06,57.14,34.09,66.88,51.48,55.83 47 | 07,93.83,57.32,28.12,77.96,45.56 48 | 08,68.95,65.11,44.38,57.41,65.28 49 | 08,85.01,98.47,91.25,83.33,72.22 50 | 08,95.90,99.99,95.62,105.56,102.22 51 | 08,92.46,95.75,61.88,83.33,48.89 52 | 08,96.73,88.11,71.88,97.41,65.56 53 | 08,83.70,83.17,60.62,63.15,57.78 54 | 07,95.14,94.01,99.38,100.0,95.0 55 | 07,98.58,88.30,90.62,100.93,99.17 56 | 08,71.79,102.87,54.37,21.53,36.11 57 | 08,71.79,101.68,75.0,21.53,49.44 58 | 08,87.93,106.53,37.50,97.41,28.06 59 | 08,87.93,108.97,28.75,87.96,47.78 60 | 08,68.95,65.11,40.0,57.41,78.89 61 | 07,72.85,86.85,41.25,60.37,46.67 62 | 08,71.79,102.87,41.88,24.77,, 63 | 08,92.02,97.76,46.25,47.22,60.56 64 | 07,90.33,87.56,68.75,77.96,58.33 65 | 07,95.0,94.36,90.62,100.93,101.11 66 | 07,91.28,108.71,96.25,99.81,88.89 67 | 08,97.0,103.02,93.12,106.48,94.44 68 | 08,93.01,104.18,55.0,96.85,67.22 69 | 08,92.02,100.58,54.37,63.89,63.89 70 | 07,100.83,105.57,101.25,104.44,108.89 71 | 08,80.53,92.80,51.25,72.78,66.67 72 | 08,90.98,97.55,86.25,88.89,90.0 73 | 08,93.59,103.83,92.50,96.85,87.22 74 | 08,97.33,100.42,69.38,102.59,83.06 75 | 07,84.26,91.31,63.12,83.33,75.56 76 | 08,84.26,96.66,52.50,83.33,50.0 77 | 07,93.83,102.19,106.25,94.44,102.78 78 | 08,75.27,86.67,70.0,71.85,80.0 79 | 08,92.02,100.58,73.12,63.89,65.28 80 | 08,97.16,103.71,83.75,95.93,78.89 81 | 08,66.17,93.68,71.88,42.22,61.39 82 | 08,81.22,91.95,79.38,105.93,90.0 83 | 07,74.29,65.70,78.75,103.52,55.0 84 | 08,97.33,106.74,76.88,108.89,83.89 85 | 04,86.86,62.64,92.50,85.19,62.78 86 | 06,95.60,61.40,64.38,99.81,42.78 87 | 04,87.93,99.47,53.12,87.96,61.11 88 | 06,98.49,95.43,42.50,24.77,39.44 89 | 07,74.35,92.93,86.25,78.70,73.89 90 | 07,86.29,88.81,83.12,77.96,75.83 91 | 08,97.0,100.52,64.38,90.74,58.61 92 | 08,97.33,106.74,81.25,108.89,71.11 93 | 08,96.41,103.71,56.25,95.93,66.39 94 | 07,95.60,82.28,76.88,108.33,78.33 95 | 08,87.52,91.58,56.25,71.85,85.0 96 | 08,96.73,103.71,45.0,93.52,61.94 97 | 07,85.34,80.54,41.25,93.70,39.72 98 | 08,89.94,102.77,87.50,90.74,87.78 99 | 07,95.60,76.13,66.25,99.81,85.56 100 | 08,63.40,97.37,73.12,72.78,77.22 -------------------------------------------------------------------------------- /Chapter07/08. Comparing data frames for identity/grades.csv: -------------------------------------------------------------------------------- 1 | Prefix,Assignment,Tutorial,Midterm,TakeHome,Final 2 | 05,57.14,34.09,64.38,51.48,52.50 3 | 08,95.05,105.49,67.50,99.07,68.33 4 | 08,83.70,83.17,30.0,63.15,48.89 5 | 07,81.22,96.06,49.38,105.93,80.56 6 | 08,91.32,93.64,95.0,107.41,73.89 7 | 07,95.0,92.58,93.12,97.78,68.06 8 | 08,95.05,102.99,56.25,99.07,50.0 9 | 07,72.85,86.85,60.0,,56.11 10 | 08,84.26,93.10,47.50,18.52,50.83 11 | 07,90.10,97.55,51.25,88.89,63.61 12 | 07,80.44,90.20,75.0,91.48,39.72 13 | 06,86.26,80.60,74.38,87.59,77.50 14 | 08,97.16,103.71,72.50,93.52,63.33 15 | 07,91.28,83.53,81.25,99.81,92.22 16 | 08,84.80,89.08,44.38,16.91,35.83 17 | 07,93.83,95.43,88.12,80.93,90.0 18 | 08,84.80,89.08,47.50,16.91,53.33 19 | 04,92.01,102.52,38.75,86.11,49.17 20 | 08,55.14,81.85,75.0,56.11,62.50 21 | 08,93.04,82.93,79.38,83.33,91.11 22 | 08,63.40,86.21,63.12,72.78,, 23 | 08,75.27,97.52,63.12,61.11,66.11 24 | 08,63.78,76.21,39.38,42.22,34.44 25 | 07,80.44,90.20,46.25,91.48,72.22 26 | 07,53.36,82.01,74.38,102.59,56.39 27 | 06,91.28,95.24,82.50,97.59,92.78 28 | 08,82.45,86.65,93.12,85.56,89.17 29 | 08,75.27,86.67,69.38,61.11,88.89 30 | 08,91.32,94.89,76.25,107.41,85.56 31 | 07,91.62,65.18,71.88,90.0,45.56 32 | 07,98.58,102.46,67.50,97.59,63.33 33 | 07,86.26,88.57,70.0,87.59,55.0 34 | 08,67.29,95.64,48.12,72.22,43.33 35 | 07,98.58,91.03,101.25,104.26,107.78 36 | 08,85.42,95.67,56.25,103.52,64.72 37 | 05,88.09,63.39,74.38,93.70,50.83 38 | 06,95.05,70.24,52.50,52.41,47.78 39 | 07,89.89,57.97,32.50,85.19,51.67 40 | 06,90.74,89.64,61.25,90.0,, 41 | 07,95.0,94.36,89.38,100.93,85.0 42 | 06,28.14,58.51,72.50,53.70,68.33 43 | 07,95.14,82.67,110.0,89.81,90.83 44 | 07,92.01,112.58,86.25,86.11,83.33 45 | 07,86.26,74.66,85.0,64.07,82.22 46 | 06,57.14,34.09,66.88,51.48,55.83 47 | 07,93.83,57.32,28.12,77.96,45.56 48 | 08,68.95,65.11,44.38,57.41,65.28 49 | 08,85.01,98.47,91.25,83.33,72.22 50 | 08,95.90,99.99,95.62,105.56,102.22 51 | 08,92.46,95.75,61.88,83.33,48.89 52 | 08,96.73,88.11,71.88,97.41,65.56 53 | 08,83.70,83.17,60.62,63.15,57.78 54 | 07,95.14,94.01,99.38,100.0,95.0 55 | 07,98.58,88.30,90.62,100.93,99.17 56 | 08,71.79,102.87,54.37,21.53,36.11 57 | 08,71.79,101.68,75.0,21.53,49.44 58 | 08,87.93,106.53,37.50,97.41,28.06 59 | 08,87.93,108.97,28.75,87.96,47.78 60 | 08,68.95,65.11,40.0,57.41,78.89 61 | 07,72.85,86.85,41.25,60.37,46.67 62 | 08,71.79,102.87,41.88,24.77,, 63 | 08,92.02,97.76,46.25,47.22,60.56 64 | 07,90.33,87.56,68.75,77.96,58.33 65 | 07,95.0,94.36,90.62,100.93,101.11 66 | 07,91.28,108.71,96.25,99.81,88.89 67 | 08,97.0,103.02,93.12,106.48,94.44 68 | 08,93.01,104.18,55.0,96.85,67.22 69 | 08,92.02,100.58,54.37,63.89,63.89 70 | 07,100.83,105.57,101.25,104.44,108.89 71 | 08,80.53,92.80,51.25,72.78,66.67 72 | 08,90.98,97.55,86.25,88.89,90.0 73 | 08,93.59,103.83,92.50,96.85,87.22 74 | 08,97.33,100.42,69.38,102.59,83.06 75 | 07,84.26,91.31,63.12,83.33,75.56 76 | 08,84.26,96.66,52.50,83.33,50.0 77 | 07,93.83,102.19,106.25,94.44,102.78 78 | 08,75.27,86.67,70.0,71.85,80.0 79 | 08,92.02,100.58,73.12,63.89,65.28 80 | 08,97.16,103.71,83.75,95.93,78.89 81 | 08,66.17,93.68,71.88,42.22,61.39 82 | 08,81.22,91.95,79.38,105.93,90.0 83 | 07,74.29,65.70,78.75,103.52,55.0 84 | 08,97.33,106.74,76.88,108.89,83.89 85 | 04,86.86,62.64,92.50,85.19,62.78 86 | 06,95.60,61.40,64.38,99.81,42.78 87 | 04,87.93,99.47,53.12,87.96,61.11 88 | 06,98.49,95.43,42.50,24.77,39.44 89 | 07,74.35,92.93,86.25,78.70,73.89 90 | 07,86.29,88.81,83.12,77.96,75.83 91 | 08,97.0,100.52,64.38,90.74,58.61 92 | 08,97.33,106.74,81.25,108.89,71.11 93 | 08,96.41,103.71,56.25,95.93,66.39 94 | 07,95.60,82.28,76.88,108.33,78.33 95 | 08,87.52,91.58,56.25,71.85,85.0 96 | 08,96.73,103.71,45.0,93.52,61.94 97 | 07,85.34,80.54,41.25,93.70,39.72 98 | 08,89.94,102.77,87.50,90.74,87.78 99 | 07,95.60,76.13,66.25,99.81,85.56 100 | 08,63.40,97.37,73.12,72.78,77.22 -------------------------------------------------------------------------------- /Chapter07/09. Transforming rows of a DataFrame/grades.csv: -------------------------------------------------------------------------------- 1 | Prefix,Assignment,Tutorial,Midterm,TakeHome,Final 2 | 05,57.14,34.09,64.38,51.48,52.50 3 | 08,95.05,105.49,67.50,99.07,68.33 4 | 08,83.70,83.17,30.0,63.15,48.89 5 | 07,81.22,96.06,49.38,105.93,80.56 6 | 08,91.32,93.64,95.0,107.41,73.89 7 | 07,95.0,92.58,93.12,97.78,68.06 8 | 08,95.05,102.99,56.25,99.07,50.0 9 | 07,72.85,86.85,60.0,,56.11 10 | 08,84.26,93.10,47.50,18.52,50.83 11 | 07,90.10,97.55,51.25,88.89,63.61 12 | 07,80.44,90.20,75.0,91.48,39.72 13 | 06,86.26,80.60,74.38,87.59,77.50 14 | 08,97.16,103.71,72.50,93.52,63.33 15 | 07,91.28,83.53,81.25,99.81,92.22 16 | 08,84.80,89.08,44.38,16.91,35.83 17 | 07,93.83,95.43,88.12,80.93,90.0 18 | 08,84.80,89.08,47.50,16.91,53.33 19 | 04,92.01,102.52,38.75,86.11,49.17 20 | 08,55.14,81.85,75.0,56.11,62.50 21 | 08,93.04,82.93,79.38,83.33,91.11 22 | 08,63.40,86.21,63.12,72.78,, 23 | 08,75.27,97.52,63.12,61.11,66.11 24 | 08,63.78,76.21,39.38,42.22,34.44 25 | 07,80.44,90.20,46.25,91.48,72.22 26 | 07,53.36,82.01,74.38,102.59,56.39 27 | 06,91.28,95.24,82.50,97.59,92.78 28 | 08,82.45,86.65,93.12,85.56,89.17 29 | 08,75.27,86.67,69.38,61.11,88.89 30 | 08,91.32,94.89,76.25,107.41,85.56 31 | 07,91.62,65.18,71.88,90.0,45.56 32 | 07,98.58,102.46,67.50,97.59,63.33 33 | 07,86.26,88.57,70.0,87.59,55.0 34 | 08,67.29,95.64,48.12,72.22,43.33 35 | 07,98.58,91.03,101.25,104.26,107.78 36 | 08,85.42,95.67,56.25,103.52,64.72 37 | 05,88.09,63.39,74.38,93.70,50.83 38 | 06,95.05,70.24,52.50,52.41,47.78 39 | 07,89.89,57.97,32.50,85.19,51.67 40 | 06,90.74,89.64,61.25,90.0,, 41 | 07,95.0,94.36,89.38,100.93,85.0 42 | 06,28.14,58.51,72.50,53.70,68.33 43 | 07,95.14,82.67,110.0,89.81,90.83 44 | 07,92.01,112.58,86.25,86.11,83.33 45 | 07,86.26,74.66,85.0,64.07,82.22 46 | 06,57.14,34.09,66.88,51.48,55.83 47 | 07,93.83,57.32,28.12,77.96,45.56 48 | 08,68.95,65.11,44.38,57.41,65.28 49 | 08,85.01,98.47,91.25,83.33,72.22 50 | 08,95.90,99.99,95.62,105.56,102.22 51 | 08,92.46,95.75,61.88,83.33,48.89 52 | 08,96.73,88.11,71.88,97.41,65.56 53 | 08,83.70,83.17,60.62,63.15,57.78 54 | 07,95.14,94.01,99.38,100.0,95.0 55 | 07,98.58,88.30,90.62,100.93,99.17 56 | 08,71.79,102.87,54.37,21.53,36.11 57 | 08,71.79,101.68,75.0,21.53,49.44 58 | 08,87.93,106.53,37.50,97.41,28.06 59 | 08,87.93,108.97,28.75,87.96,47.78 60 | 08,68.95,65.11,40.0,57.41,78.89 61 | 07,72.85,86.85,41.25,60.37,46.67 62 | 08,71.79,102.87,41.88,24.77,, 63 | 08,92.02,97.76,46.25,47.22,60.56 64 | 07,90.33,87.56,68.75,77.96,58.33 65 | 07,95.0,94.36,90.62,100.93,101.11 66 | 07,91.28,108.71,96.25,99.81,88.89 67 | 08,97.0,103.02,93.12,106.48,94.44 68 | 08,93.01,104.18,55.0,96.85,67.22 69 | 08,92.02,100.58,54.37,63.89,63.89 70 | 07,100.83,105.57,101.25,104.44,108.89 71 | 08,80.53,92.80,51.25,72.78,66.67 72 | 08,90.98,97.55,86.25,88.89,90.0 73 | 08,93.59,103.83,92.50,96.85,87.22 74 | 08,97.33,100.42,69.38,102.59,83.06 75 | 07,84.26,91.31,63.12,83.33,75.56 76 | 08,84.26,96.66,52.50,83.33,50.0 77 | 07,93.83,102.19,106.25,94.44,102.78 78 | 08,75.27,86.67,70.0,71.85,80.0 79 | 08,92.02,100.58,73.12,63.89,65.28 80 | 08,97.16,103.71,83.75,95.93,78.89 81 | 08,66.17,93.68,71.88,42.22,61.39 82 | 08,81.22,91.95,79.38,105.93,90.0 83 | 07,74.29,65.70,78.75,103.52,55.0 84 | 08,97.33,106.74,76.88,108.89,83.89 85 | 04,86.86,62.64,92.50,85.19,62.78 86 | 06,95.60,61.40,64.38,99.81,42.78 87 | 04,87.93,99.47,53.12,87.96,61.11 88 | 06,98.49,95.43,42.50,24.77,39.44 89 | 07,74.35,92.93,86.25,78.70,73.89 90 | 07,86.29,88.81,83.12,77.96,75.83 91 | 08,97.0,100.52,64.38,90.74,58.61 92 | 08,97.33,106.74,81.25,108.89,71.11 93 | 08,96.41,103.71,56.25,95.93,66.39 94 | 07,95.60,82.28,76.88,108.33,78.33 95 | 08,87.52,91.58,56.25,71.85,85.0 96 | 08,96.73,103.71,45.0,93.52,61.94 97 | 07,85.34,80.54,41.25,93.70,39.72 98 | 08,89.94,102.77,87.50,90.74,87.78 99 | 07,95.60,76.13,66.25,99.81,85.56 100 | 08,63.40,97.37,73.12,72.78,77.22 -------------------------------------------------------------------------------- /Julia_Cookbook-Supplement.md: -------------------------------------------------------------------------------- 1 | [B. Kamiński, P. Szufel: Julia 1.0 Programming Cookbook](https://www.packtpub.com/application-development/julia-10-programming-cookbook) 2 | ======================================================================================================================================== 3 | 4 | 5 | # Supplement: Julia installation for macOS 6 | 7 | 8 | In this supplement we explain how to use configure Julia environment on macOS. 9 | The discussed examples have been tested with macOS Mojave, version 10.14.2 10 | but Julia is known to work with any macOS version starting from 10.8. 11 | 12 | ### Installing Julia on macOS 13 | 14 | The recommended way to install Julia on macOS is to use the binary release that 15 | is available at [https://julialang.org/downloads/](https://julialang.org/downloads/). 16 | The steps for macOS platform are very similar to the previously presented examples for Linux. 17 | 18 | Julia installer comes as a standard macOS `*.dmg` file. 19 | Hence, you need to simply download it (for example with the Safari browser), 20 | double-click the downloaded file and drag the *Julia-1.0* icon onto the *Applications* icon. 21 | 22 | Once installed you should check what is the actual location of Julia. Use the following command: 23 | 24 | ```bash 25 | $ ls -d /Applications/Julia*/ 26 | ``` 27 | 28 | This will show the location of your Julia installation (for example `/Applications/Julia-1.0.app`). 29 | In the following steps we assume that Julia 1.0 has been installed in the above folder. 30 | 31 | Julia is often used in the command line console (Terminal). It can be started with the following command: 32 | 33 | ```bash 34 | $ /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia 35 | ``` 36 | 37 | Typing such a long command is not convenient. 38 | Hence we recommend creating a symbolic link to the `julia` executable file in the `/usr/local/bin` folder: 39 | 40 | ```bash 41 | $ sudo mkdir /usr/local/bin 42 | $ sudo ln -fs /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia 43 | ``` 44 | 45 | Note that on some macOS installations the folder `/usr/local/bin` does not exist by default and hence we create it. 46 | 47 | Once the above two commands have been run, Julia REPL can be started in the terminal with the command `julia` rather than typing the entire path. 48 | 49 | When installing packages in Julia a complex compilation process takes place. 50 | Hence, compiling some of Julia packages (such as `Cbc.jl`) requires the `Xcode` developer tools. 51 | Their installation can be started by running the command below 52 | (note that this will start the graphical user interface installer): 53 | 54 | ```bash 55 | $ xcode-select --install 56 | ``` 57 | 58 | Additionally, the developer Command Line Tools are also required and can be 59 | installed with the following command (you might need to change the macOS version 60 | number at the end of the `pkg` file name): 61 | 62 | ```bash 63 | $ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / 64 | ``` 65 | 66 | ### Installing Juno IDE on macOS 67 | 68 | Juno is the recommended Julia development environment for macOS (as well as for Linux and Windows platforms). 69 | Once the link to Julia executable (`/usr/local/bin/julia`) is created the installation is straightforward. 70 | Simply go to the Atom website at [https://atom.io/](https://atom.io/), download the installer and double click the `pkg` file. 71 | Start Atom and add the Juno plugin - for details see the Juno paragraph at Julia IDEs recipe in the Chapter 1 of the book. 72 | If you follow the discussed Julia installation steps, Juno will just work out-of-the-box. 73 | 74 | ### Integration of other tools on macOS 75 | 76 | For configuration of other tools including Anaconda Python and Gnu R have a look at 77 | the Linux comments at relevant recipes - there are no differences between Linux and macOS Julia configuration. 78 | -------------------------------------------------------------------------------- /Chapter09/01. Working with databases in Julia/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add MySQL 3 | 4 | 5 | julia> ENV["PATH"]="C:\\Program Files\\PostgreSQL\\10\\lib;"*ENV["PATH"] 6 | julia> ] 7 | (v1.0) pkg> add LibPQ 8 | 9 | 10 | $ java -version 11 | 12 | julia> ] 13 | (v1.0) pkg> add JDBC 14 | 15 | 16 | julia> using DataFrames 17 | julia> using MySQL 18 | julia> conn = MySQL.connect("127.0.0.1", "root", "type_password_here",db="sys") 19 | 20 | julia> MySQL.execute!(conn, "CREATE TABLE mytable (col1 INT AUTO_INCREMENT PRIMARY KEY, col2 VARCHAR(50), col3 INT)") 21 | 22 | julia> st = MySQL.Stmt(conn, "INSERT INTO mytable(col2, col3) VALUES (?,?)"); 23 | julia> MySQL.execute!(st, ["testdata",7]); 24 | julia> MySQL.execute!(st, ["testdata2",8]); 25 | 26 | julia> df = MySQL.query(conn, "SELECT * FROM mytable") |> DataFrame 27 | 28 | julia> res = MySQL.query(conn, "SELECT * FROM mytable") 29 | 30 | julia> MySQL.disconnect(conn) 31 | 32 | 33 | julia> using DataFrames 34 | julia> using LibPQ 35 | julia> conn = LibPQ.Connection("host=localhost dbname=postgres user=postgres password="type_password_here") 36 | 37 | julia> LibPQ.execute(conn, "CREATE TABLE mytable (col1 SERIAL PRIMARY KEY NOT NULL, col2 VARCHAR(50), col3 INT)") 38 | 39 | julia> st = LibPQ.prepare(conn, "INSERT INTO MYTABLE(col2, col3) VALUES (\$1,\$2)") 40 | julia> LibPQ.execute(st,["testdata",7]) 41 | julia> LibPQ.execute(st,["testdata2",8]) 42 | 43 | julia> df = LibPQ.fetch!(DataFrame, LibPQ.execute(conn, "SELECT * FROM mytable")) 44 | 45 | julia> nt = LibPQ.fetch!(NamedTuple, LibPQ.execute(conn, "SELECT * FROM mytable")) 46 | 47 | julia> LibPQ.close(conn) 48 | 49 | 50 | using DataFrames 51 | using JDBC 52 | JDBC.usedriver("ojdbc8.jar") 53 | JDBC.init() 54 | 55 | conn = JDBC.DriverManager.getConnection("jdbc:oracle:thin:@ora.cez1pkekt7fj.us-east-2.rds.amazonaws.com:1521:ORCL",Dict("user"=>"orauser","password"=>"type_password_here")); 56 | 57 | st = JDBC.createStatement(conn); 58 | JDBC.execute(st, "CREATE TABLE mytable (col1 INT GENERATED ALWAYS AS IDENTITY NOT NULL," 59 | * " col2 VARCHAR2(50), col3 INT, CONSTRAINT col1 PRIMARY KEY (col1))"); 60 | 61 | pst = JDBC.prepareStatement(conn, "INSERT INTO mytable (col2, col3) VALUES (:1,:2)") 62 | JDBC.setString(pst,1,"testdata") 63 | JDBC.setInt(pst,2,7) 64 | JDBC.executeUpdate(pst) 65 | JDBC.setString(pst,1,"testdata") 66 | JDBC.setInt(pst,2,8) 67 | JDBC.executeUpdate(pst) 68 | JDBC.commit(conn) 69 | 70 | julia> rs = executeQuery(st, "select * from mytable"); 71 | julia> for r in rs 72 | println(JDBC.getInt(r,1),"|", 73 | JDBC.getString(r,2),"|",JDBC.getInt(r,3)) 74 | end 75 | 76 | julia> close(conn) 77 | 78 | julia> conn = JDBC.Connection("jdbc:oracle:thin:@ora.cez1pkekt7fj.us-east-2.rds.amazonaws.com:1521:ORCL", 79 | props=Dict("user"=>"orauser", 80 | "password"=>"type_password_here")); 81 | julia> csr=JDBC.Cursor(conn); 82 | julia> df = JDBC.load(DataFrame, csr, "select * from mytable") 83 | julia> nt = JDBC.load(NamedTuple, csr, "select * from mytable") 84 | julia> JDBC.close(csr) 85 | 86 | julia> JDBC.close(conn) 87 | 88 | 89 | using JDBC 90 | JDBC.usedriver("postgresql-42.2.4.jar") 91 | JDBC.init() 92 | conn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", 93 | Dict("user"=>"postgres", 94 | "password"=>"type_password_here")) 95 | 96 | 97 | using PyCall 98 | @pyimport elasticsearch 99 | 100 | es = elasticsearch.Elasticsearch() 101 | 102 | julia> es[:info]() 103 | 104 | dat = Dict("col1"=>"some text","col2"=>"more text") 105 | res = es[:index](index="data", doc_type="data", id="1", body=dat) 106 | 107 | q=Dict("query"=>Dict("match"=>Dict("col1"=>Dict("query"=>"some text")))) 108 | 109 | julia> es[:search]("data",body=q)["hits"]["hits"] 110 | -------------------------------------------------------------------------------- /Chapter07/03. Reading CSV data from the internet/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter07/06. Split-apply-combine in DataFrames/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter07/07. Converting a data frame between wide and narrow formats/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter07/10. Creating pivot tables by chaining transformations of data frames/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter04/13. Using array views to avoid memory allocation/p096_sudoku.txt: -------------------------------------------------------------------------------- 1 | Grid 01 2 | 003020600 3 | 900305001 4 | 001806400 5 | 008102900 6 | 700000008 7 | 006708200 8 | 002609500 9 | 800203009 10 | 005010300 11 | Grid 02 12 | 200080300 13 | 060070084 14 | 030500209 15 | 000105408 16 | 000000000 17 | 402706000 18 | 301007040 19 | 720040060 20 | 004010003 21 | Grid 03 22 | 000000907 23 | 000420180 24 | 000705026 25 | 100904000 26 | 050000040 27 | 000507009 28 | 920108000 29 | 034059000 30 | 507000000 31 | Grid 04 32 | 030050040 33 | 008010500 34 | 460000012 35 | 070502080 36 | 000603000 37 | 040109030 38 | 250000098 39 | 001020600 40 | 080060020 41 | Grid 05 42 | 020810740 43 | 700003100 44 | 090002805 45 | 009040087 46 | 400208003 47 | 160030200 48 | 302700060 49 | 005600008 50 | 076051090 51 | Grid 06 52 | 100920000 53 | 524010000 54 | 000000070 55 | 050008102 56 | 000000000 57 | 402700090 58 | 060000000 59 | 000030945 60 | 000071006 61 | Grid 07 62 | 043080250 63 | 600000000 64 | 000001094 65 | 900004070 66 | 000608000 67 | 010200003 68 | 820500000 69 | 000000005 70 | 034090710 71 | Grid 08 72 | 480006902 73 | 002008001 74 | 900370060 75 | 840010200 76 | 003704100 77 | 001060049 78 | 020085007 79 | 700900600 80 | 609200018 81 | Grid 09 82 | 000900002 83 | 050123400 84 | 030000160 85 | 908000000 86 | 070000090 87 | 000000205 88 | 091000050 89 | 007439020 90 | 400007000 91 | Grid 10 92 | 001900003 93 | 900700160 94 | 030005007 95 | 050000009 96 | 004302600 97 | 200000070 98 | 600100030 99 | 042007006 100 | 500006800 101 | Grid 11 102 | 000125400 103 | 008400000 104 | 420800000 105 | 030000095 106 | 060902010 107 | 510000060 108 | 000003049 109 | 000007200 110 | 001298000 111 | Grid 12 112 | 062340750 113 | 100005600 114 | 570000040 115 | 000094800 116 | 400000006 117 | 005830000 118 | 030000091 119 | 006400007 120 | 059083260 121 | Grid 13 122 | 300000000 123 | 005009000 124 | 200504000 125 | 020000700 126 | 160000058 127 | 704310600 128 | 000890100 129 | 000067080 130 | 000005437 131 | Grid 14 132 | 630000000 133 | 000500008 134 | 005674000 135 | 000020000 136 | 003401020 137 | 000000345 138 | 000007004 139 | 080300902 140 | 947100080 141 | Grid 15 142 | 000020040 143 | 008035000 144 | 000070602 145 | 031046970 146 | 200000000 147 | 000501203 148 | 049000730 149 | 000000010 150 | 800004000 151 | Grid 16 152 | 361025900 153 | 080960010 154 | 400000057 155 | 008000471 156 | 000603000 157 | 259000800 158 | 740000005 159 | 020018060 160 | 005470329 161 | Grid 17 162 | 050807020 163 | 600010090 164 | 702540006 165 | 070020301 166 | 504000908 167 | 103080070 168 | 900076205 169 | 060090003 170 | 080103040 171 | Grid 18 172 | 080005000 173 | 000003457 174 | 000070809 175 | 060400903 176 | 007010500 177 | 408007020 178 | 901020000 179 | 842300000 180 | 000100080 181 | Grid 19 182 | 003502900 183 | 000040000 184 | 106000305 185 | 900251008 186 | 070408030 187 | 800763001 188 | 308000104 189 | 000020000 190 | 005104800 191 | Grid 20 192 | 000000000 193 | 009805100 194 | 051907420 195 | 290401065 196 | 000000000 197 | 140508093 198 | 026709580 199 | 005103600 200 | 000000000 201 | Grid 21 202 | 020030090 203 | 000907000 204 | 900208005 205 | 004806500 206 | 607000208 207 | 003102900 208 | 800605007 209 | 000309000 210 | 030020050 211 | Grid 22 212 | 005000006 213 | 070009020 214 | 000500107 215 | 804150000 216 | 000803000 217 | 000092805 218 | 907006000 219 | 030400010 220 | 200000600 221 | Grid 23 222 | 040000050 223 | 001943600 224 | 009000300 225 | 600050002 226 | 103000506 227 | 800020007 228 | 005000200 229 | 002436700 230 | 030000040 231 | Grid 24 232 | 004000000 233 | 000030002 234 | 390700080 235 | 400009001 236 | 209801307 237 | 600200008 238 | 010008053 239 | 900040000 240 | 000000800 241 | Grid 25 242 | 360020089 243 | 000361000 244 | 000000000 245 | 803000602 246 | 400603007 247 | 607000108 248 | 000000000 249 | 000418000 250 | 970030014 251 | Grid 26 252 | 500400060 253 | 009000800 254 | 640020000 255 | 000001008 256 | 208000501 257 | 700500000 258 | 000090084 259 | 003000600 260 | 060003002 261 | Grid 27 262 | 007256400 263 | 400000005 264 | 010030060 265 | 000508000 266 | 008060200 267 | 000107000 268 | 030070090 269 | 200000004 270 | 006312700 271 | Grid 28 272 | 000000000 273 | 079050180 274 | 800000007 275 | 007306800 276 | 450708096 277 | 003502700 278 | 700000005 279 | 016030420 280 | 000000000 281 | Grid 29 282 | 030000080 283 | 009000500 284 | 007509200 285 | 700105008 286 | 020090030 287 | 900402001 288 | 004207100 289 | 002000800 290 | 070000090 291 | Grid 30 292 | 200170603 293 | 050000100 294 | 000006079 295 | 000040700 296 | 000801000 297 | 009050000 298 | 310400000 299 | 005000060 300 | 906037002 301 | Grid 31 302 | 000000080 303 | 800701040 304 | 040020030 305 | 374000900 306 | 000030000 307 | 005000321 308 | 010060050 309 | 050802006 310 | 080000000 311 | Grid 32 312 | 000000085 313 | 000210009 314 | 960080100 315 | 500800016 316 | 000000000 317 | 890006007 318 | 009070052 319 | 300054000 320 | 480000000 321 | Grid 33 322 | 608070502 323 | 050608070 324 | 002000300 325 | 500090006 326 | 040302050 327 | 800050003 328 | 005000200 329 | 010704090 330 | 409060701 331 | Grid 34 332 | 050010040 333 | 107000602 334 | 000905000 335 | 208030501 336 | 040070020 337 | 901080406 338 | 000401000 339 | 304000709 340 | 020060010 341 | Grid 35 342 | 053000790 343 | 009753400 344 | 100000002 345 | 090080010 346 | 000907000 347 | 080030070 348 | 500000003 349 | 007641200 350 | 061000940 351 | Grid 36 352 | 006080300 353 | 049070250 354 | 000405000 355 | 600317004 356 | 007000800 357 | 100826009 358 | 000702000 359 | 075040190 360 | 003090600 361 | Grid 37 362 | 005080700 363 | 700204005 364 | 320000084 365 | 060105040 366 | 008000500 367 | 070803010 368 | 450000091 369 | 600508007 370 | 003010600 371 | Grid 38 372 | 000900800 373 | 128006400 374 | 070800060 375 | 800430007 376 | 500000009 377 | 600079008 378 | 090004010 379 | 003600284 380 | 001007000 381 | Grid 39 382 | 000080000 383 | 270000054 384 | 095000810 385 | 009806400 386 | 020403060 387 | 006905100 388 | 017000620 389 | 460000038 390 | 000090000 391 | Grid 40 392 | 000602000 393 | 400050001 394 | 085010620 395 | 038206710 396 | 000000000 397 | 019407350 398 | 026040530 399 | 900020007 400 | 000809000 401 | Grid 41 402 | 000900002 403 | 050123400 404 | 030000160 405 | 908000000 406 | 070000090 407 | 000000205 408 | 091000050 409 | 007439020 410 | 400007000 411 | Grid 42 412 | 380000000 413 | 000400785 414 | 009020300 415 | 060090000 416 | 800302009 417 | 000040070 418 | 001070500 419 | 495006000 420 | 000000092 421 | Grid 43 422 | 000158000 423 | 002060800 424 | 030000040 425 | 027030510 426 | 000000000 427 | 046080790 428 | 050000080 429 | 004070100 430 | 000325000 431 | Grid 44 432 | 010500200 433 | 900001000 434 | 002008030 435 | 500030007 436 | 008000500 437 | 600080004 438 | 040100700 439 | 000700006 440 | 003004050 441 | Grid 45 442 | 080000040 443 | 000469000 444 | 400000007 445 | 005904600 446 | 070608030 447 | 008502100 448 | 900000005 449 | 000781000 450 | 060000010 451 | Grid 46 452 | 904200007 453 | 010000000 454 | 000706500 455 | 000800090 456 | 020904060 457 | 040002000 458 | 001607000 459 | 000000030 460 | 300005702 461 | Grid 47 462 | 000700800 463 | 006000031 464 | 040002000 465 | 024070000 466 | 010030080 467 | 000060290 468 | 000800070 469 | 860000500 470 | 002006000 471 | Grid 48 472 | 001007090 473 | 590080001 474 | 030000080 475 | 000005800 476 | 050060020 477 | 004100000 478 | 080000030 479 | 100020079 480 | 020700400 481 | Grid 49 482 | 000003017 483 | 015009008 484 | 060000000 485 | 100007000 486 | 009000200 487 | 000500004 488 | 000000020 489 | 500600340 490 | 340200000 491 | Grid 50 492 | 300200000 493 | 000107000 494 | 706030500 495 | 070009080 496 | 900020004 497 | 010800050 498 | 009040301 499 | 000702000 500 | 000008006 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Julia 1.0 Programming Cookbook 5 | 6 | Julia 1.0 Programming Cookbook 7 | 8 | This is the code repository for [Julia 1.0 Programming Cookbook](https://www.packtpub.com/application-development/julia-10-programming-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781788998369 ), published by Packt. 9 | 10 | **Over 100 numerical and distributed computing recipes for your daily data science workflow** 11 | 12 | ## What is this book about? 13 | Julia programming language with its dynamic nature and high-performance provides comparatively minimum time for development of computational models with easy to maintain computational code. Julia 1.0 Programming Cookbook will be your solution based guide as it will take you through different programming aspects with Julia. 14 | 15 | This book covers the following exciting features: 16 | * Boost the code performance using Julia’s unique features 17 | * Organize data in fundamental types of collections: arrays, dictionaries 18 | * Organize data science process within Julia and solve related problems 19 | * Scale Julia computations with cloud computing 20 | * Write data to IO streams with Julia and to handle web transfer 21 | 22 | 23 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1788998367) today! 24 | 25 | https://www.packtpub.com/ 27 | 28 | ## Release history 29 | 30 | * [Version 1.0](https://github.com/PacktPublishing/Julia-1.0-Programming-Cookbook/releases/tag/v1.0): 31 | * An initial release with source codes exactly as in the printed version of the first edition of the book. 32 | * [Version 1.1](https://github.com/PacktPublishing/Julia-1.0-Programming-Cookbook/releases/tag/v1.1): 33 | * The package StatPlots.jl was renamed by its maintainers to StatsPlots.jl; this release updates the codes to reflect this change. 34 | * The `allargmin` function from chapter 2, recipe 1 was fixed to correctly handle the case when it is passed an empty collection. 35 | 36 | ## Book Supplement 37 | Following the feedback of our readers we have created a [Supplement]( https://github.com/PacktPublishing/Julia-1.0-Programming-Cookbook/blob/master/Julia_Cookbook-Supplement.md) that discusses Julia installation and configuration issues related to macOS. 38 | 39 | ## Instructions and Navigations 40 | All of the code is organized into folders. For example, Chapter02. The example code files are organized in folders representing chapters and recipes. For each recipe, there is a commands.txt file that contains commands that should be typed-in by the reader. Every entry in this file is prepended by an appropriate prompt (example, **$**, **julia>**) to make sure that the user knows in which environment the command should be executed (typically the OS shell, the Julia command line). Most recipes also contain additional files, for example, source codes of Julia programs. A full list of files along with their contents is given in the *Getting ready* section of every recipe 41 | 42 | The code will look like the following: 43 | ``` 44 | if isless(curval, minval) 45 | minval = curval 46 | bestidx = idx 47 | bestcount = 1 48 | ``` 49 | 50 | **Following is what you need for this book:** 51 | This book is for developers who would like to enhance their Julia programming skills and would like to get some quick solutions to their common programming problems. Basic Julia programming knowledge is assumed. 52 | 53 | With the following software and hardware list you can run all code files present in the book (Chapter 01-10). 54 | ### Software and Hardware List 55 | In this book we use Julia 1.0.1, however, the presented recipes should work without any problem with any future Julia 1.0.x version. 56 | 57 | The following Julia packages have been used throughout the book. We provide a version number of each package that has been tested with the recipes in the book. Some of the packages on this list are being currently quickly developed and if you use the latest package version in some cases a recipe might not work due to API changes. 58 | 59 | 60 | | Package | Version | External Dependency | 61 | | -------- | -------- | ------------------------------------| 62 | | BSON | v"0.2.1" | none | 63 | | BenchmarkTools |v"0.4.1" | none | 64 | | CSV | v"0.4.3" | none | 65 | | Cascadia |v"0.4.0" | none | 66 | | Cbc | v"0.4.2" | none | 67 | | Clp |v"0.5.0" | none | 68 | | CodecZib | v"0.5.0" | none | 69 | | Conda | v"1.0.2" | optional - external Python Anaconda | 70 | | DataFrames | v"0.14.1" | none | 71 | | DataFramesMeta | v"0.4.0" | none | 72 | | Distributions | v"0.16.4" | none | 73 | | Feather | v"0.5.0" | none | 74 | | FileIO | v"1.0.2" | none | 75 | | ForwardDiff |v"0.9.9" | none | 76 | | FreqTables |v"0.3.0" | none | 77 | | Gumbo | v"0.5.1" | none | 78 | | Gurobi | v"0.5.3" | Gurobi Solver | 79 | | HTTP | v"0.7.1" | none | 80 | | JDBC | v"0.4.0" | Java Runtime Environment | 81 | | JLD2 | v"0.1.2" | none | 82 | | JSON |v"0.19.0" | none | 83 | | JuMP | v"0.18.4" | none | 84 | | JuliaWebAPI |v"0.5.0" | none | 85 | | LibPQ |v"0.5.0" | PostgreSQL libpq library | 86 | | MySQL | v"0.7.0" | none | 87 | | OnlineStats |v"0.19.1" | none | 88 | | Optim | v"0.17.1" | none | 89 | | Plots | v"0.20.5" | optional - external Python Anaconda (with PyPlot plotting backend) | 90 | | ParallelDataTransfer | v"0.5.0" | none | 91 | | ProfileView | v"0.4.0" | none | 92 | | PyCall | v"1.18.4" | optional - external Python Anaconda | 93 | | PyPlot | v"2.6.3" | optional - external Python Anaconda | 94 | | RCall | v"0.12.1" | GNU R | 95 | | Revise |v"0.7.12" | none | 96 | | ScikitLearn | latest available | optional - external Python Anaconda + Anaconda scikit-learn library | 97 | | StatsPlots |v"0.8.1" | optional - external Python Anaconda (with PyPlot plotting backend) | 98 | | StaticArrays |v"0.8.3" | none | 99 | | StatsBase" | v"0.26.0" | none | 100 | | StringEncodings |v"0.3.1" | none | 101 | | TimeZones | v"0.8.1" | none | 102 | | UnicodePlots |v"0.3.1" | none | 103 | | XLSX | v"0.4.2" | none | 104 | | ZMQ | v"1.0.0" | none | 105 | 106 | For each package we provide the information whether it depends on external software installations. Some package have no external dependencies while other require installation of external tools such as GNU R, database drivers or Java virtual machine. Detailed instructions can be found in the recipes that use particular packages (for example, the recipe *Calling R from Julia* in the Chapter 8, *Julia Workflow* shows how to configure RCall while the recipe *Working with databases in Julia* in the Chapter 9, *Data Science* explains the installation process of various database drivers). 107 | 108 | A noteworthy case is the Python dependency. Several important Julia packages depend on Python functionality that is provided via the PyCall.jl package. Note that for those packages (having the status **optional** at our table) you have two options: 109 | 110 | * Use Python Anaconda installed inside of Julia (in this case no installation of external dependencies is required) 111 | * external Python Anaconda installation outside of Julia (the user needs to install and configure Python Anaconda to work with Julia) 112 | 113 | Note that in this book all Python-dependent recipes use Python 3.6.5. In the recipe *Calling Python from Julia* in Chapter 8, *Julia Workflow* we discuss in detail various options for configuring the PyCall.jl package for Julia-to-Python integration. 114 | 115 | 116 | We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://www.packtpub.com/sites/default/files/downloads/9781788998369_ColorImages.pdf). 117 | 118 | ### Related products 119 | * Julia 1.0 Programming - Second Edition [[Packt]](https://www.packtpub.com/application-development/julia-10-programming-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781788999090 ) [[Amazon]](https://www.amazon.com/dp/1788999096) 120 | 121 | * Hands-On Computer Vision with Julia [[Packt]](https://www.packtpub.com/application-development/hands-computer-vision-julia?utm_source=github&utm_medium=repository&utm_campaign=9781788998796 ) [[Amazon]](https://www.amazon.com/dp/B07CSPBV11) 122 | 123 | 124 | ## Get to Know the Author 125 | **Bogumił Kamiński** 126 | ([GitHub](https://github.com/bkamins), [web](http://bogumilkaminski.pl/about/)) is an associate professor and head of the 127 | Decision Support and Analysis Unit at the SGH Warsaw School of Economics, as well as 128 | adjunct professor at the data science laboratory, Ryerson University, Toronto. He is coeditor 129 | of the Central European Journal of Economic Modeling and Econometrics, and of the 130 | Multiple Criteria Decision Making journal. His scientific interests center on operational 131 | research and computational social science. He has authored over 50 research articles on 132 | simulation, optimization, and prediction methods. He also has 15+ years' experience in the 133 | deployment of large-scale advanced analytics solutions for industry and public 134 | administration 135 | 136 | **Przemysław Szufel** 137 | ([GitHub](https://github.com/pszufe), [web](https://szufel.pl/en_aboutme.html)) is an assistant professor in 138 | the Decision Support and Analysis Unit at the SGH Warsaw School of Economics. His 139 | current research focuses on distributed systems and methods for the execution of largescale 140 | simulations for numerical experiments and optimization. He is working on 141 | asynchronous algorithms for the parallel execution of large-scale computations in the cloud 142 | and distributed computational environments. He has authored, and co-authored, several 143 | open source tools for high-performance and numerical simulation 144 | 145 | 146 | ### Suggestions and Feedback 147 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 148 | ### Download a free PDF 149 | 150 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
151 |

https://packt.link/free-ebook/9781788998369

--------------------------------------------------------------------------------