Spring 2025
x = [1,2,3];
y = [4,5,6];
z = []
for idx in 1:length(x)
push!(z, x[idx]*y[idx]);
end
println("z = $z");
x = [1,2,3];
y = [4,5,6];
z = x .* y;
println("z = $z");
function unsquare(x)
result = similar(x)
for idx in 1:length(x)
result[idx] = √( x[idx]^2 )
end
return result
end
x = 1:100000
@time unsquare(x);
@time .√( x.^2 );
estimate(n) = 4*sum( (rand(n).^2 .+ rand(n).^2) .< 1 ) / n estimate(100) est = estimate(10000000) abs(est - π)
string([1,2,3]) string.([1,2,3]) string([1,2,3], ["a", "b", "c"]) string.([1,2,3], ["a", "b", "c"])
fib(n) = n ≤ 2 ? one(n) : fib(n-1) + fib(n-2) # Fibonacci number dist(X,Y) = .√sum((X-Y).^2, dims=2) # Distance between every row vector betwen X and Y norm(X) = X./sum(X, dims=2) # Normalize each row vector in a matrix avglag(X) = sum(X[2:end] - X[1:end-1], dims=1) / (size(X)[1]-1) # Average lag between numbers in a list # Here's a cooler estimator for pi (Bailey-Borwein-Plouffe) bppEstimate(n) = sum([(1/16^k) * sum([4,-2,-1,-1] ./ (8*k .+ [1,4,5,6])) for k in 0:n])
using Pkg; Pkg.add(["DataFrames", "CSV", "DelimitedFiles", "HTTP"]); using DataFrames, CSV, DelimitedFiles, HTTP;
foo.csv:foo = DataFrame(CSV.File("foo.csv"))
# Make a function to simplify this:
read_remote_csv(url) = DataFrame(CSV.File(HTTP.get(url).body));
df = read_remote_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/refs/heads/master/iris.csv")
show(first(df, 5)) names(df) first(df.sepal_length, 7)
PyCall that let’s you call Python directlyusing Pkg
Pkg.add("PyCall") # Install, just once
CondaPkg.build("PyCall")
using Conda
Conda.add(["numpy", "pandas", "scipy", "matplotlib"])
ENV["PYTHON"] = "/opt/local/bin/python"
Pkg.build("PyCall")
pyimportnp = pyimport("numpy");
x = np.random.normal(size=10, scale=2.5);
println("x = $x");
pd = pyimport("pandas");
foo = pd.read_csv("foo.csv")
foo["Name"]
RCall that let’s you use an R environment from inside Juliausing Pkg
Pkg.add("RCall") # Install, just once
using RCall # Whenever you want to use R
https://juliainterop.github.io/RCall.jl/stable/gettingstarted/
$@rputx = 3.1415; @rput x;
@rget@rget y;
println("y = $y");
x = R"rnorm(10)"
z = rcopy(R"rnorm(10)")
rdf = robject(df);
@rput rdf;
R"""
library(ggplot2)
ggplot(rdf, aes(x=sepal_length, y=sepal_width, fill=species)) +
geom_point(size=4, shape=21, color="black") +
xlab("Sepal Length of Iris") + ylab("Sepal Width of Iris")
"""
@rimport base as rbase rbase.sum(df.sepal_length) rcall(:summary, rdf)