## We can interact with Julia inside of Visual Studio Code!
# This is a block... if I press option-enter (on Mac) or alt-enter (Windows/Linux)
# then it will run everything in the block.
x = 5
y = 6
x*y
30
## You can define functions in blocks.
function squared(x)
return x*x
end
function squared(x::Int)
return x+1
end
squared(x)*squared(y)
42
## Using Matrices and Arrays
A = rand(10,12)
10×12 Matrix{Float64}: 0.121631 0.265558 0.695218 0.433292 … 0.0323556 0.521357 0.61474 0.238716 0.504298 0.529062 0.667526 0.517237 0.503166 0.905959 0.268419 0.827511 0.245653 0.988945 0.185714 0.598201 0.350977 0.638734 0.543971 0.972842 0.287848 0.486156 0.868915 0.0802408 0.461994 0.353189 0.640837 0.56258 0.129159 0.973258 0.286063 0.51381 0.313473 0.797174 0.0808024 … 0.013043 0.371691 0.167303 0.443006 0.553918 0.520295 0.262374 0.979852 0.375907 0.708854 0.502522 0.707348 0.962172 0.509297 0.72498 0.346111 0.863141 0.955108 0.189804 0.432468 0.195528 0.710319 0.0179187 0.934372 0.172979 0.533152 0.672976 0.280835 0.80828 0.252692 0.571847
##
B = A'*A
12×12 Matrix{Float64}: 2.42018 1.92085 2.84731 1.57028 … 2.25464 2.1708 1.941 2.39374 1.92085 2.65597 3.06981 2.34309 2.43971 2.3586 2.41131 2.59311 2.84731 3.06981 4.65257 2.5024 3.2172 2.9664 3.24734 3.37588 1.57028 2.34309 2.5024 2.46255 2.07619 1.7488 2.33042 2.38463 2.94641 2.97822 4.0687 2.81297 3.36031 3.10772 3.07092 3.88317 3.06346 3.04268 4.11457 2.76038 … 3.13792 2.79789 3.07304 3.44457 2.51625 2.83985 4.16362 2.55592 2.82052 2.75211 3.06187 3.28986 1.95661 1.69378 2.74179 1.17263 1.93408 1.75736 1.78144 1.7715 2.25464 2.43971 3.2172 2.07619 3.10547 2.66442 2.06202 3.25459 2.1708 2.3586 2.9664 1.7488 2.66442 3.19985 1.77743 3.07805 1.941 2.41131 3.24734 2.33042 … 2.06202 1.77743 3.04851 2.12307 2.39374 2.59311 3.37588 2.38463 3.25459 3.07805 2.12307 3.88566
##
b = randn(12)
c = B\b # solving a system is built in...
12-element Vector{Float64}: 1.7364355310364702e15 -1.0086469324880208e14 2.8758303294343365e15 1.209055894057744e15 -4.28040198558911e14 -1.417361091360047e15 -2.3784458715732855e15 -7.688831523450749e14 -1.8202914235445155e15 -2.9987459990328656e14 -1.7784221021427417e12 1.5687582161504622e15
## rand vs. randn
# Let's use plots to show the difference between rand and randn.
using Plots
x = rand(100000)
y = randn(100000)
plot(scatter(x),scatter(y))
## Let's look at a histogram of x
histogram(x)
##
histogram(y)
## Show them side-by-side
plot(
histogram(x,title="samples of rand", xlabel="value", ylabel="count"),
histogram(y,title="samples of randn", xlabel="value", ylabel="count")
)
## to save a figure, use savefig
savefig("myfig.pdf")
"/Users/dgleich/Dropbox/courses/cs515-2023/web/input/julia/myfig.pdf"
##
rank(B)
UndefVarError: `rank` not defined Stacktrace: [1] top-level scope @ In[11]:2
## When you need that...
using LinearAlgebra # add all linear algebra examples ...
##
rank(B)
10
## Let's look at a more interesting example.
# Our goal is to take as input a set of vectors as
# column of a matrix and return true if
# any two of those vectors are orthogonal.
function orthogonal_vectors(X::Matrix, Y::Matrix)
for i in 1:size(X,2) # for each column of x
for j in 1:size(Y,2) # for each column of y
xi = @view X[:,i] # get the ith column of x
yj = @view Y[:,j] # get the jth column of Y
v = abs(xi'*yj)/(norm(xi)*norm(yj))
if v <= eps(1.0)*sqrt(size(X,1))
return true
end
end
end
return false
end
orthogonal_vectors(randn(10,100),randn(10,100))
false
##
orthogonal_vectors(rand(-1:1, 10,100),rand(-1:1,10,100))
true