a = [1; 2; 3]
3-element Array{Int64,1}: 1 2 3
println(a)
[1, 2, 3]
a = [1, 2, 3]
3-element Array{Int64,1}: 1 2 3
a = [1 2 3]
1×3 Array{Int64,2}: 1 2 3
b = [4 5 6]
1×3 Array{Int64,2}: 4 5 6
A = [1 2 3; 4 5 6]
2×3 Array{Int64,2}: 1 2 3 4 5 6
A[1,3]
3
A[1]
1
A[1,1]
1
A[1,2]
2
A[2,1]
4
transpose(A)
3×2 LinearAlgebra.Transpose{Int64,Array{Int64,2}}: 1 4 2 5 3 6
A'
3×2 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}: 1 4 2 5 3 6
a = [1; 2; 3;]
c = [7; 8; 9;]
3-element Array{Int64,1}: 7 8 9
a'*c
50
using LinearAlgebra
dot(a, c)
50
Matrix(1.0I, 2, 2)
# I는 identity matrix => 단위 행렬. I가 없으면 에러남
2×2 Array{Float64,2}: 1.0 0.0 0.0 1.0
Matrix(1.0, 2, 2)
MethodError: no method matching Array{T,2} where T(::Float64, ::Int64, ::Int64) Closest candidates are: Array{T,2} where T(!Matched::UndefInitializer, ::Integer, ::Integer) at sysimg.jl:152 Array{T,2} where T(!Matched::UniformScaling, ::Integer, ::Integer) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/uniformscaling.jl:341 Stacktrace: [1] top-level scope at In[20]:1
zeros(4,1)
4×1 Array{Float64,2}: 0.0 0.0 0.0 0.0
zeros(2,3)
2×3 Array{Float64,2}: 0.0 0.0 0.0 0.0 0.0 0.0
ones(1,3)
1×3 Array{Float64,2}: 1.0 1.0 1.0
ones(2,3)
2×3 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0
B = [1 3 2; 3 2 2; 1 1 1]
3×3 Array{Int64,2}: 1 3 2 3 2 2 1 1 1
inv(B)
3×3 Array{Float64,2}: 1.11022e-16 1.0 -2.0 1.0 1.0 -4.0 -1.0 -2.0 7.0
B * inv(B)
3×3 Array{Float64,2}: 1.0 0.0 0.0 0.0 1.0 0.0 -2.22045e-16 0.0 1.0
inv(B)[2,1]
1.0000000000000004
a = [1; 2; 3]
3-element Array{Int64,1}: 1 2 3
b = [1.0; 2; 3]
3-element Array{Float64,1}: 1.0 2.0 3.0
d = Array{Float64}(undef, 3)
3-element Array{Float64,1}: 2.3064115165e-314 2.3064115323e-314 2.3091702605e-314
d[1] = 1
1
d[2] = 2
2
d[3] = 3
3
d
3-element Array{Float64,1}: 1.0 2.0 3.0
pairs = Array{Tuple{Int64, Int64}}(undef, 3)
3-element Array{Tuple{Int64,Int64},1}: (4671453744, 4671453888) (4671453936, 4671453984) (4671454032, 4678541088)
pairs
3-element Array{Tuple{Int64,Int64},1}: (4671453744, 4671453888) (4671453936, 4671453984) (4671454032, 4678541088)
pairs2 = (1, 3)
(1, 3)
pairs[1]
(4671453744, 4671453888)
pairs[1] = (1,2)
(1, 2)
pairs[2] = (2,3)
pairs[3] = (3,4)
(3, 4)
pairs
3-element Array{Tuple{Int64,Int64},1}: (1, 2) (2, 3) (3, 4)
# 이거랑 동일함
pairs = [(1,2); (2,3); (3,4)]
3-element Array{Tuple{Int64,Int64},1}: (1, 2) (2, 3) (3, 4)
ijk_array = Array{Tuple{Int64, Int64, Int64}}(undef, 3)
3-element Array{Tuple{Int64,Int64,Int64},1}: (4662978080, 4669435552, 4669435392) (1, 4669435632, 4669435712) (4669435792, 4, 4)
ijk_array[1] = (1, 4, 2)
(1, 4, 2)
a = [10; 20; 30; 40; 50; 60; 70; 80; 90]
9-element Array{Int64,1}: 10 20 30 40 50 60 70 80 90
# range
a[1:3]
3-element Array{Int64,1}: 10 20 30
# 1부터 9까지 중 +3
a[1:3:9]
3-element Array{Int64,1}: 10 40 70
a[3]
30
a[2:3:7]
2-element Array{Int64,1}: 20 50
a[end-2:end]
3-element Array{Int64,1}: 70 80 90
b = [200; 300; 400]
3-element Array{Int64,1}: 200 300 400
a[2:4] = b
3-element Array{Int64,1}: 200 300 400
a
9-element Array{Int64,1}: 10 200 300 400 50 60 70 80 90
c = collect(1:2:9)
5-element Array{Int64,1}: 1 3 5 7 9
println("Hello World")
Hello World
print("hello "); print("world"); print(" Again")
hello world Again
println("hello "); println("world"); println(" Again")
hello world Again
a = 123.0
123.0
$
변수 로 사용 가능. 혹은 $
(연산)println("The value of a = ", a)
The value of a = 123.0
println("a is $a, and a-10 is $(a-10).")
a is 123.0, and a-10 is 113.0.
b = [1; 3; 10]
3-element Array{Int64,1}: 1 3 10
println("b is $b")
b is [1, 3, 10]
println("the second element of b is $(b[2])")
the second element of b is 3
using Printf
@printf("The %s of a = %f", "value", a)
The value of a = 123.000000
c = [123.12345; 10.983; 1.092312]
3-element Array{Float64,1}: 123.12345 10.983 1.092312
for i in 1:length(c)
@printf("c[%d] = %7.2f\n", i, c[i])
end
c[1] = 123.12 c[2] = 10.98 c[3] = 1.09
str = @sprintf("The %s of a = %f", "value", a)
"The value of a = 123.000000"
println(str)
The value of a = 123.000000
for i in 1:5
println("This is number $i")
end
This is number 1 This is number 2 This is number 3 This is number 4 This is number 5
for i in I
#do something here for each i
end
for i in 1:5
if i >= 3
break
end
println("This is number $i")
end
This is number 1 This is number 2
my_keys = ["hi", "bye", "cool"]
my_values = ["football", "pocketmon","swim"]
3-element Array{String,1}: "football" "pocketmon" "swim"
d = Dict()
for i in 1:length(my_keys)
d[my_keys[i]] = my_values[i]
end
d
Dict{Any,Any} with 3 entries: "bye" => "pocketmon" "hi" => "football" "cool" => "swim"
for (key, value) in d
println("$key is a $value player")
end
bye is a pocketmon player hi is a football player cool is a swim player
d["Diego Maradona"] = "football"
"football"
links = [(1,2), (3,4), (4,2)]
3-element Array{Tuple{Int64,Int64},1}: (1, 2) (3, 4) (4, 2)
link_costs = [5, 13, 8]
3-element Array{Int64,1}: 5 13 8
link_dict = Dict()
Dict{Any,Any} with 0 entries
for i in 1:length(links)
link_dict[links[i]] = link_costs[i]
end
link_dict
Dict{Any,Any} with 3 entries: (1, 2) => 5 (4, 2) => 8 (3, 4) => 13
for (link, cost) in link_dict
println("Link $link has cost of $cost")
end
Link (1, 2) has cost of 5 Link (4, 2) has cost of 8 Link (3, 4) has cost of 13
function f(x,y)
return 3x+y
end
f (generic function with 1 method)
f(1,3)
6
3*(f(3,2)+f(5,6))
96
function my_func(n, m)
a = zeros(n, 1)
b = ones(m, 1)
return a,b
end
my_func (generic function with 1 method)
x, y = my_func(3,2)
([0.0; 0.0; 0.0], [1.0; 1.0])
x
3×1 Array{Float64,2}: 0.0 0.0 0.0
y
2×1 Array{Float64,2}: 1.0 1.0
sqrt(9)
3.0
sqrt([9 16])
DimensionMismatch("matrix is not square: dimensions are (1, 2)") Stacktrace: [1] sqrt(::Array{Int64,2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/LinearAlgebra.jl:214 [2] top-level scope at In[92]:1
sqrt.([9 16])
1×2 Array{Float64,2}: 3.0 4.0
myfunc(x) = sin(x) + 3*x
myfunc (generic function with 1 method)
myfunc(3)
9.141120008059866
myfunc([5 10])
DimensionMismatch("matrix is not square: dimensions are (1, 2)") Stacktrace: [1] exp!(::Array{Complex{Float64},2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/LinearAlgebra.jl:214 [2] sin(::Array{Int64,2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/dense.jl:792 [3] myfunc(::Array{Int64,2}) at ./In[94]:1 [4] top-level scope at In[96]:1
myfunc.([5 10])
1×2 Array{Float64,2}: 14.0411 29.456
function f(x)
retun x+2
end
syntax: extra token "x" after end of expression
function g(x)
return 3x+3
end
g (generic function with 1 method)
function f(x)
return x+z
end
function run()
z=10
return f(5)
end
run (generic function with 1 method)
run()
UndefVarError: z not defined Stacktrace: [1] f(::Int64) at ./In[100]:2 [2] run() at ./In[100]:7 [3] top-level scope at In[101]:1
function f(x)
return x+a
end
function run()
return f(5)
end
a=10
run()
15
function f2(x)
a=0
return x+a
end
a=5
5
println(f2(1))
1
println(a)
5
function f3(x)
_a = 0
return x + _a
end
f3 (generic function with 1 method)
a = 5
println(f3(1))
println(a)
1 5
function f4(x, a)
return x+a
end
a = 5
println(f4(1, a))
println(a)
6 5
a = [1 2 3 4 5]
s = 0
for i in 1:length(A)
s += a[i]
end
BoundsError: attempt to access 1×5 Array{Int64,2} at index [6] Stacktrace: [1] getindex(::Array{Int64,2}, ::Int64) at ./array.jl:729 [2] top-level scope at ./In[109]:4
function my_sum(a)
s = 0
for i in 1:5
s += a[i]
end
return s
end
a = [1; 2; 3; 4; 5]
5-element Array{Int64,1}: 1 2 3 4 5
my_sum(a)
15
rand()
0.9266495281538778
rand()
0.8304477728220296
rand(5)
5-element Array{Float64,1}: 0.8324770166728122 0.2662287032474082 0.6933367170942821 0.2955082361684169 0.577475034743496
rand() * 100
93.7998436597625
rand(1:10)
4
randn(5)
5-element Array{Float64,1}: -1.2142547850901897 -1.991431544403744 -1.3196835160607816 1.1693537449632032 0.28594133262787147
function my_randn(n, mu, sigma)
return randn(n) .* sigma .+mu
end
my_randn (generic function with 1 method)
my_randn(10, 50, 3)
10-element Array{Float64,1}: 48.96424536283605 45.82637579794923 52.22822347403981 49.35333388905027 48.71153119242729 52.760595462606034 46.632272155091094 52.03787969109315 53.18965634376912 55.264539739277296
using Pkg
Pkg.add("StatsFuns")
Updating registry at `~/.julia/registries/General` Updating git-repo `https://github.com/JuliaRegistries/General.git` Resolving package versions... Updating `~/.julia/environments/v1.1/Project.toml` [no changes] Updating `~/.julia/environments/v1.1/Manifest.toml` [no changes]
Pkg.build("StatsFuns")
Building SpecialFunctions → `~/.julia/packages/SpecialFunctions/ne2iw/deps/build.log` Building Rmath ───────────→ `~/.julia/packages/Rmath/BoBag/deps/build.log`
using StatsFuns
┌ Info: Precompiling StatsFuns [4c63d2b9-4356-54db-8cca-17b64c39e42c] └ @ Base loading.jl:1186 ERROR: LoadError: SystemError: opening file "/Users/byeon/.julia/compiled/v1.1/SpecialFunctions/78gOt.ji": Permission denied Stacktrace: [1] #systemerror#43(::Nothing, ::Function, ::String, ::Bool) at ./error.jl:134 [2] systemerror at ./error.jl:134 [inlined] [3] #open#309(::Bool, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Function, ::String) at ./iostream.jl:289 [4] #open at ./none:0 [inlined] [5] open(::String, ::String) at ./iostream.jl:345 [6] stale_cachefile(::String, ::String) at ./loading.jl:1321 [7] _require_search_from_serialized(::Base.PkgId, ::String) at ./loading.jl:693 [8] _require(::Base.PkgId) at ./loading.jl:937 [9] require(::Base.PkgId) at ./loading.jl:858 [10] require(::Module, ::Symbol) at ./loading.jl:853 [11] include at ./boot.jl:326 [inlined] [12] include_relative(::Module, ::String) at ./loading.jl:1038 [13] include(::Module, ::String) at ./sysimg.jl:29 [14] top-level scope at none:2 [15] eval at ./boot.jl:328 [inlined] [16] eval(::Expr) at ./client.jl:404 [17] top-level scope at ./none:3 in expression starting at /Users/byeon/.julia/packages/StatsFuns/f6KKB/src/StatsFuns.jl:6
Failed to precompile StatsFuns [4c63d2b9-4356-54db-8cca-17b64c39e42c] to /Users/byeon/.julia/compiled/v1.1/StatsFuns/530lR.ji. Stacktrace: [1] error(::String) at ./error.jl:33 [2] compilecache(::Base.PkgId, ::String) at ./loading.jl:1197 [3] _require(::Base.PkgId) at ./loading.jl:960 [4] require(::Base.PkgId) at ./loading.jl:858 [5] require(::Module, ::Symbol) at ./loading.jl:853 [6] top-level scope at In[123]:1
mu = 50; sigma = 3;
normpdf(mu, sigma, 52)
UndefVarError: normpdf not defined Stacktrace: [1] top-level scope at In[125]:1
data_file_path = "data.txt"
"data.txt"
data_file = open(data_file_path)
IOStream(<file data.txt>)
data = readlines(data_file)
3-element Array{String,1}: "This is the first line" "this is the second" "thie is the third"
close(data_file)
for line in data
println(line)
end
This is the first line this is the second thie is the third
output_file_path = "results1.txt"
output_file = open(output_file_path, "w")
print(output_file, "Magic Johnson")
println(output_file, "is a basketball player")
println(output_file, "Michael jordan is also a basketball palyer")
close(output_file)
using DelimitedFiles
csv_file_name = "data.csv"
csv_data = readdlm(csv_file_name, ',', header=true)
(Any[1 2 2 ""; 1 3 4.5 ""; … ; 2 4 3 ""; 3 4 5 ""], AbstractString["start node" " end node link length" "" ""])
data = csv_data[1]
header = csv_data[2]
1×4 Array{AbstractString,2}: "start node" " end node link length" "" ""
header
1×4 Array{AbstractString,2}: "start node" " end node link length" "" ""
data
5×4 Array{Any,2}: 1 2 2 "" 1 3 4.5 "" 2 3 6 "" 2 4 3 "" 3 4 5 ""
start_node = round.(Int, data[:,1])
5-element Array{Int64,1}: 1 1 2 2 3
end_node = round.(Int, data[:, 2])
5-element Array{Int64,1}: 2 3 3 4 4
link_length = data[:, 3]
5-element Array{Any,1}: 2 4.5 6 3 5
using PyPlot
┌ Info: Recompiling stale cache file /Users/byeon/.julia/compiled/v1.1/PyPlot/oatAj.ji for PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee] └ @ Base loading.jl:1184
# Preparing a figure object
fig = figure()
# Data
x = range(0, stop=2*pi, length=1000)
y = sin.(3*x)
# Plotting with linewidth and linestyle specified
plot(x, y, color="blue", linewidth=2.0, linestyle="--")
# Labeling the axes
xlabel(L"value of $x$")
ylabel(L"\sin(3x)")
# Title
title("Test plotting")
# Save the figure as PNG and PDF
savefig("plot1.png")
savefig("plot1.pdf")
# Close the figure object
close(fig)
lower_bound = [4.0, 4.2, 4.4, 4.8, 4.9, 4.95, 4.99, 5.00]
upper_bound = [5.4, 5.3, 5.3, 5.2, 5.2, 5.15, 5.10, 5.05]
iter = 1:8
# Creating a new figure object
fig = figure()
# Plotting two datasets
plot(iter, lower_bound, color="red", linewidth=2.0, linestyle="-",
marker="o", label=L"Lower Bound $Z^k_L$")
plot(iter, upper_bound, color="blue", linewidth=2.0, linestyle="-.",
marker="D", label=L"Upper Bound $Z^k_U$")
# Labeling axes
xlabel(L"iteration clock $k$", fontsize="xx-large")
ylabel("objective function value", fontsize="xx-large")
# Putting the legend and determining the location
legend(loc="upper right", fontsize="x-large")
# Add grid lines
grid(color="#DDDDDD", linestyle="-", linewidth=1.0)
tick_params(axis="both", which="major", labelsize="x-large")
# Title
title("Lower and Upper Bounds")
# Save the figure as PNG and PDF
savefig("plot2.png")
savefig("plot2.pdf")
# Closing the figure object
close(fig)
# Data
data = randn(100) # Some Random Data
nbins = 10 # Number of bins
# Creating a new figure object
fig = figure()
# Histogram
plt[:hist](data, nbins)
# Title
title("Histogram")
# Save the figure as PNG and PDF
savefig("plot3.png")
savefig("plot3.pdf")
# Closing the figure object
close(fig)
┌ Warning: `getindex(o::PyObject, s::Symbol)` is deprecated in favor of dot overloading (`getproperty`) so elements should now be accessed as e.g. `o.s` instead of `o[:s]`. │ caller = top-level scope at In[150]:7 └ @ Core In[150]:7