julia programming language

Learn from examples

1. Program to output a string, integer, and floating point exjl1.jl source code # exjl1.jl various ways to print string, integer, float or double using Printf # only needed for @printf println("julia exjl1.jl running") print("julia exjl1") print(".jl") print(" running") println(" multiple print on one line") cstr = "a string" @printf "cstr = %s \n" cstr bint = 9999 print("bint = ") print(bint) println(" ") @printf "bint = %d \n" bint aflt = 1.5 print("aflt = ") print(aflt) println(" ") @printf "aflt = %7.4f \n" aflt bigflt = 123456789.987654321e+249 @printf "bigflt = %22.18e \n" bigflt println(1.5, " ", 2, " abc") println(" ") println("exjl1.jl finished") # end exjl1.jl Output from execution: julia exjl1.jl running julia exjl1.jl running multiple print on one line cstr = a string bint = 9999 bint = 9999 aflt = 1.5 aflt = 1.5000 bigflt = 1.234567899876543095e+257 1.5 2 abc exjl1.jl finished 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: julia exjl1.jl Linux: julia exjl1.jl MacOSX julia exjl1.jl 3. You must be able to declare variables and arrays and matrix of various types. exjl3.jl source code # exjl3.jl declare and print variable, vector, matrix using Printf # only needed if @printf will be used println("julia exjl3.jl running") s1 = "any string" println(s1) s2 = "more" println(s2) s3 = s1*s2 # concatenation println(s3) s4 = s1*" "*s2 # space between strings println(s4) i = 13 # integer Int print("i = ") print(i) # print can print everything, even arrays println(" ") # no line feed in just print @printf "i = %d \n" i # "C" format and list x = 0.0 # double Float64 @printf "x=%f \n" x for i in 1:4 global x x = x + 1.0 end @printf "x=%f \n" x v1 = [ 1 2 3 4 ] # row vector no comma print("v1=") print(v1) print(" row vector"); println(" ") @printf "v1[3]=%d \n" v1[3] v2 = [1, 2, 3, 4] # typical row vector print("v2=") print(v2) print(" with , row vector"); println(" ") @printf "v2[3]=%d \n" v2[3] v3 = [ 1 2 3 4 ]' # column vector "'" print("v3=") print(v3) print(" with ' column vector"); println(" ") @printf "v3[3]=%d \n" v3[3] # matrix A = Array{Float64,2}(undef, 2, 3) println("A created ") print("A=") print(A) println(" ") A0 = zeros(2, 3) print("A0=") print(A0) println(" ") A1 = ones(2, 3) print("A1=") print(A1) println(" ") B = [1.0 2.0 3.0 ; 4.0 5.0 6.0] # no commas! ; or new line print("B=") print(B) println(" ") nrow = size(B,1) print("nrow=size(B,1)=") println(nrow) ncol = size(B,2) print("ncol=size(B,2)=") println(ncol) println(" ") println("exjl3.jl finished") # end exjl3.jl Execution output: julia exjl3.jl running any string more any stringmore any string more i = 13 i = 13 x=0.000000 x=4.000000 v1=[1 2 3 4] row vector v1[3]=3 v2=[1, 2, 3, 4] with , row vector v2[3]=3 v3=[1; 2; 3; 4] with ' column vector v3[3]=3 A created A=[4.24399e-314 3.31031e-312 -1.10215e213; 3.22543e-312 3.35275e-312 2.3178e-313] A0=[0.0 0.0 0.0; 0.0 0.0 0.0] A1=[1.0 1.0 1.0; 1.0 1.0 1.0] B=[1.0 2.0 3.0; 4.0 5.0 6.0] nrow=size(B,1)=2 ncol=size(B,2)=3 exjl3.jl finished 4. You need to be able to have loops, iteration statements exjl4.jl source code # exjl4.jl example loops and iteration using Printf println("julia exjl4.jl running") for i in 1:5 print(i, ", ") end println() # 1, 2, 3, 4, 5, for i = 1:5 # both "in" and "=" work print(i, ", ") end println() # 1, 2, 3, 4, 5, arr = [2,4,6,8] for i in 1:4 # array index starts at 1 like Fortran, Matlab print(i, ", ") end println() # 2, 4, 6, 8 # arrays can also be looped over directly: a1 = [2,4,6,8] for i in a1 print(i, ", ") end println() # 2, 4, 6, 8, # continue and break work in loops a2 = collect(1:20) for i in a2 if i % 2 != 0 continue end print(i, ", ") if i >= 8 break end end println() # 2, 4, 6, 8, d1 = Dict(1=>"one", 2=>"two", 3=>"three") # dicts may be looped through using the keys function: for k in sort(collect(keys(d1))) print(k, ": ", d1[k], ", ") end println() # 1: one, 2: two, 3: three, # enumerate can be used to get both the index and value in a loop a3 = ["one", "two", "three"] for (i, v) in enumerate(a3) print(i, ": ", v, ", ") end println() # 1: one, 2: two, 3: three, # map works as you might expect performing the given function on each member of an array or iter much like comprehensions a4 = map((x) -> x^2, [1, 2, 3, 7]) print(a4) # 4-element Array{Int64,1}: [1,4,9,49] println(" ") println("exjl4.jl finished") # end exjl4.jl Execution output: julia exjl4.jl running 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 2, 4, 6, 8, 2, 4, 6, 8, 1: one, 2: two, 3: three, 1: one, 2: two, 3: three, [1, 4, 9, 49] exjl4.jl finished 5. You need if then else conditional statements exjl5.jl source code # exjl5.jl example if then else using Printf println("julia exjl5.jl running") # < > <= >= == != && || function test(x, y) if x < y println("x is less than y") elseif x > y println("x is greater than y") elseif x <= y || x != y println("x is greater than y") elseif x >= y && x == Y println("x is greater than y") elseif x == y println("x is greater than y") elseif x != y println("x is greater than y") else println("x is equal to y") end end test(1, 2) test(2, 1) test(1, 1) println("exjl5.jl finished") # end exjl5.jl Execution output: julia exjl5.jl running x is less than y x is greater than y x is greater than y exjl5.jl finished 6. You need to be able to create functions, procedures, subroutines. exjl6.jl source code # exjl6.jl example create and use functions and procedures using Printf function anyarg(a1, a2, a3, a4) # procedure because return nothing print("a1 = ") print(a1) println(" ") print("a2 = ") print(a2) println(" ") print("a3 = ") print(a3) println(" ") print("a4 = ") print(a4) println(" ") return nothing end # anyarg function mulany(A, B) # A and B of some type C = A*B return C end # mulany function targs(A::String, B::Float64, C::Int) println("targs A = "*A) @printf "B = %f \n" B @printf "C = %d \n" C return nothing end # targs println("julia exjl6.jl running") # functions before first executable i = 13 s1 = "my string" x = 3.5 M1 = [ 1.0 2.0; 3.0 4.0 ] M2 = Array{Float64, 2}(undef,2,2) M2[1,1] = 1.1 M2[1,2] = 2.2 M2[2,1] = 3.3 M2[2,2] = 4.4 println("anyarg, no type checking") anyarg(i, s1, x, M1) println(" ") anyarg(M1, x, i, s1) # with no type checking println(" ") Q = mulany(i, 2) print("Q = mulany(i, 2) =") print(Q) println(" ") Q = mulany(s1, "more") print("Q = mulany(s1, more ) =") print(Q) println(" ") Q = mulany(x, 2.0) print("Q = mulany(x, 2.0) =") print(Q) println(" ") Q = mulany(M1, M2) print("Q = mulany(M1, M2) =") print(Q) println(" ") println(" ") targs(s1, x, i) # must agree with type println(" ") println("exjl6.jl finished") # end exjl6.jl Execution output: julia exjl6.jl running anyarg, no type checking a1 = 13 a2 = my string a3 = 3.5 a4 = [1.0 2.0; 3.0 4.0] a1 = [1.0 2.0; 3.0 4.0] a2 = 3.5 a3 = 13 a4 = my string Q = mulany(i, 2) =26 Q = mulany(s1, more ) =my stringmore Q = mulany(x, 2.0) =7.0 Q = mulany(M1, M2) =[7.7 11.0; 16.5 24.2] targs A = my string B = 3.500000 C = 13 exjl6.jl finished 7. You need to be able to read and write files in various formats. exjl7.jl source code # exjl7.jl example read and write files using Printf println("julia exjl7.jl running") println("open txt_jl.dat for writing") fout = open("txt_jl.dat","w") println(fout, "line 1") println(fout, "line 2") println(fout, "last line") close(fout) println("file txt_jl.dat written") println(" ") println("open txt_jl.dat for reading") open("txt_jl.dat") do f for ln in eachline(f) print("line= ") println(ln) end end println("read and printed txt_jl.dat") println("convert string to Float64 or Int") x = parse(Float64,"13.54") @printf "x= %f \n" x i = parse(Int,"135") @printf "i= %d \n" i println("convert Float64 and Int to string") x = 13.54; xstr = @sprintf("%f",x) @printf "xstr = %s \n" xstr i = 135 istr = @sprintf("%d",i) @printf "istr = %s \n" istr s="1.0 2.5 3.7 4.5" print("s=") println(s) sv = split(s) print("split(s)=") println(sv) n=size(sv,1) print("n=size(sv,1)=") println(n) @printf "n=%s \n" n for k in 1:n println(sv[k]) end print("sv[1]=") println(sv[1]) print("sv[2]=") println(sv[2]) print("sv[3]=") println(sv[3]) print("sv[4]=") println(sv[4]) print("sv[1]*sv[2]*sv[3]*sv[4]=") println(sv[1]*sv[2]*sv[3]*sv[4]) print("sv[1]*space*sv[2]*space*sv[3]*space*sv[4]=") svs = sv[1]*" "*sv[2]*" "*sv[3]*" "*sv[4] println(svs) println("exjl7.jl finished") # end exjl7.jl Execution output: julia exjl7.jl running open txt_jl.dat for writing file txt_jl.dat written open txt_jl.dat for reading line= line 1 line= line 2 line= last line read and printed txt_jl.dat convert string to Float64 or Int x= 13.540000 i= 135 convert Float64 and Int to string xstr = 13.540000 istr = 135 s=1.0 2.5 3.7 4.5 split(s)=SubString{String}["1.0", "2.5", "3.7", "4.5"] n=size(sv,1)=4 n=4 1.0 2.5 3.7 4.5 sv[1]=1.0 sv[2]=2.5 sv[3]=3.7 sv[4]=4.5 sv[1]*sv[2]*sv[3]*sv[4]=1.02.53.74.5 sv[1]*space*sv[2]*space*sv[3]*space*sv[4]=1.0 2.5 3.7 4.5 exjl7.jl finished 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, operating system commands, header files, etc. exjl8.jl source code ??? Execution output: ??? 9. extra examples: math, complex, matrix function, numerical integration, numerical derivatives, partial differential equations math_funct.jl source code math_funct_jl.out output test_complex.jl source code test_complex_jl.out output matrix.jl source code matrix_jl.out output test_gaulegf.jl source code test_gaulegf_jl.out output test_nuderiv.jl source code test_nuderiv_jl.out output pde22_eq.jl source code pde22_eq_jl.out output pde22_eq_jl.dat data output pde22_eq_jl.sh run plot pde22_eq_jl.plot plot data pde22_eq_jl.png output