a = [1; 2; 3] println(a) a = [1, 2, 3] a = [1 2 3] b = [4 5 6] A = [1 2 3; 4 5 6] A[1,3] A[1] A[1,1] A[1,2] A[2,1] transpose(A) A' a = [1; 2; 3;] c = [7; 8; 9;] a'*c using LinearAlgebra dot(a, c) Matrix(1.0I, 2, 2) # I는 identity matrix => 단위 행렬. I가 없으면 에러남 Matrix(1.0, 2, 2) zeros(4,1) zeros(2,3) ones(1,3) ones(2,3) B = [1 3 2; 3 2 2; 1 1 1] inv(B) B * inv(B) inv(B)[2,1] a = [1; 2; 3] b = [1.0; 2; 3] d = Array{Float64}(undef, 3) d[1] = 1 d[2] = 2 d[3] = 3 d pairs = Array{Tuple{Int64, Int64}}(undef, 3) pairs pairs2 = (1, 3) pairs[1] pairs[1] = (1,2) pairs[2] = (2,3) pairs[3] = (3,4) pairs # 이거랑 동일함 pairs = [(1,2); (2,3); (3,4)] ijk_array = Array{Tuple{Int64, Int64, Int64}}(undef, 3) ijk_array[1] = (1, 4, 2) a = [10; 20; 30; 40; 50; 60; 70; 80; 90] # range a[1:3] # 1부터 9까지 중 +3 a[1:3:9] a[3] a[2:3:7] a[end-2:end] b = [200; 300; 400] a[2:4] = b a c = collect(1:2:9) println("Hello World") print("hello "); print("world"); print(" Again") println("hello "); println("world"); println(" Again") a = 123.0 println("The value of a = ", a) println("a is $a, and a-10 is $(a-10).") b = [1; 3; 10] println("b is $b") println("the second element of b is $(b[2])") using Printf @printf("The %s of a = %f", "value", a) c = [123.12345; 10.983; 1.092312] for i in 1:length(c) @printf("c[%d] = %7.2f\n", i, c[i]) end str = @sprintf("The %s of a = %f", "value", a) println(str) for i in 1:5 println("This is number $i") end for i in 1:5 if i >= 3 break end println("This is number $i") end my_keys = ["hi", "bye", "cool"] my_values = ["football", "pocketmon","swim"] d = Dict() for i in 1:length(my_keys) d[my_keys[i]] = my_values[i] end d for (key, value) in d println("$key is a $value player") end d["Diego Maradona"] = "football" links = [(1,2), (3,4), (4,2)] link_costs = [5, 13, 8] link_dict = Dict() for i in 1:length(links) link_dict[links[i]] = link_costs[i] end link_dict for (link, cost) in link_dict println("Link $link has cost of $cost") end function f(x,y) return 3x+y end f(1,3) 3*(f(3,2)+f(5,6)) function my_func(n, m) a = zeros(n, 1) b = ones(m, 1) return a,b end x, y = my_func(3,2) x y sqrt(9) sqrt([9 16]) sqrt.([9 16]) myfunc(x) = sin(x) + 3*x myfunc(3) myfunc([5 10]) myfunc.([5 10]) function f(x) retun x+2 end function g(x) return 3x+3 end function f(x) return x+z end function run() z=10 return f(5) end run() function f(x) return x+a end function run() return f(5) end a=10 run() function f2(x) a=0 return x+a end a=5 println(f2(1)) println(a) function f3(x) _a = 0 return x + _a end a = 5 println(f3(1)) println(a) function f4(x, a) return x+a end a = 5 println(f4(1, a)) println(a) a = [1 2 3 4 5] s = 0 for i in 1:length(A) s += a[i] end function my_sum(a) s = 0 for i in 1:5 s += a[i] end return s end a = [1; 2; 3; 4; 5] my_sum(a) rand() rand() rand(5) rand() * 100 rand(1:10) randn(5) function my_randn(n, mu, sigma) return randn(n) .* sigma .+mu end my_randn(10, 50, 3) using Pkg Pkg.add("StatsFuns") Pkg.build("StatsFuns") using StatsFuns mu = 50; sigma = 3; normpdf(mu, sigma, 52) data_file_path = "data.txt" data_file = open(data_file_path) data = readlines(data_file) close(data_file) for line in data println(line) end 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) data = csv_data[1] header = csv_data[2] header data start_node = round.(Int, data[:,1]) end_node = round.(Int, data[:, 2]) link_length = data[:, 3] using PyPlot # 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)