Skip to content

Commit 5f65836

Browse files
authored
Update all julia code to V1.0 (#389)
* adding changes to update all julia code to V1.0 * adding more points to graham.jl
1 parent a1a6dfa commit 5f65836

File tree

9 files changed

+39
-30
lines changed

9 files changed

+39
-30
lines changed

contents/cooley_tukey/code/julia/fft.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using FFTW
2+
13
#simple DFT function
24
function DFT(x)
35
N = length(x)
@@ -24,8 +26,8 @@ function cooley_tukey(x)
2426
n = 0:N-1
2527
half = div(N,2)
2628
factor = exp.(-2im*pi*n/N)
27-
return vcat(x_odd + x_even .* factor[1:half],
28-
x_odd - x_even .* factor[1:half])
29+
return vcat(x_odd .+ x_even .* factor[1:half],
30+
x_odd .- x_even .* factor[1:half])
2931

3032
end
3133

@@ -37,7 +39,7 @@ function bitreverse(a::Array)
3739

3840
bit_indices = []
3941
for i = 1:length(indices)
40-
push!(bit_indices, bits(indices[i]))
42+
push!(bit_indices, bitstring(indices[i]))
4143
end
4244

4345
# Now stripping the unnecessary numbers
@@ -50,11 +52,11 @@ function bitreverse(a::Array)
5052
bit_indices[i] = reverse(bit_indices[i])
5153
end
5254

53-
#replacing indices
55+
# Replacing indices
5456
for i = 1:length(indices)
5557
indices[i] = 0
5658
for j = 1:digits
57-
indices[i] += 2^(j-1) * parse(string(bit_indices[i][end-j]))
59+
indices[i] += 2^(j-1) * parse(Int, string(bit_indices[i][end-j]))
5860
end
5961
indices[i] += 1
6062
end

contents/cooley_tukey/cooley_tukey.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ For some reason, though, putting code to this transformation really helped me fi
7070

7171
{% method %}
7272
{% sample lang="jl" %}
73-
[import:2-11, lang:"julia"](code/julia/fft.jl)
73+
[import:4-13, lang:"julia"](code/julia/fft.jl)
7474
{% sample lang="c" %}
7575
[import:8-19, lang:"c_cpp"](code/c/fft.c)
7676
{% sample lang="cpp" %}
7777
[import:23-33, lang:"c_cpp"](code/c++/fft.cpp)
7878
{% sample lang="hs" %}
79-
[import:2-11, lang:"julia"](code/julia/fft.jl)
79+
[import:4-13, lang:"julia"](code/julia/fft.jl)
8080
{% sample lang="py" %}
8181
[import:5-11, lang:"python"](code/python/fft.py)
8282
{% sample lang="scratch" %}
83-
[import:2-11, lang:"julia"](code/julia/fft.jl)
83+
[import:4-13, lang:"julia"](code/julia/fft.jl)
8484
{% endmethod %}
8585

8686
In this function, we define `n` to be a set of integers from $$0 \rightarrow N-1$$ and arrange them to be a column.
@@ -115,7 +115,7 @@ With recursion, we can reduce the complexity to $$\sim \mathcal{O}(n \log n)$$,
115115
In the end, the code looks like:
116116
{% method %}
117117
{% sample lang="jl" %}
118-
[import:14-31, lang:"julia"](code/julia/fft.jl)
118+
[import:16-32, lang:"julia"](code/julia/fft.jl)
119119
{% sample lang="c" %}
120120
[import:20-39, lang:"c_cpp"](code/c/fft.c)
121121
{% sample lang="cpp" %}
@@ -125,7 +125,7 @@ In the end, the code looks like:
125125
{% sample lang="py" %}
126126
[import:13-24, lang:"python"](code/python/fft.py)
127127
{% sample lang="scratch" %}
128-
[import:14-31, lang:"julia"](code/julia/fft.jl)
128+
[import:16-32, lang:"julia"](code/julia/fft.jl)
129129
{% endmethod %}
130130

131131
As a side note, we are enforcing that the array must be a power of 2 for the operation to work.

contents/gaussian_elimination/code/julia/gaussian_elimination.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function gaussian_elimination(A::Array{Float64,2})
1010
for col = 1:(cols-1)
1111

1212
# Step 1: finding the maximum element for each column
13-
max_index = indmax(abs.(A[row:end,col])) + row-1
13+
max_index = argmax(abs.(A[row:end,col])) + row-1
1414

1515
# Check to make sure matrix is good!
1616
if (A[max_index, col] == 0)
@@ -50,7 +50,7 @@ function back_substitution(A::Array{Float64,2})
5050
cols = size(A,2)
5151

5252
# Creating the solution Vector
53-
soln = Vector{Float64}(rows)
53+
soln = zeros(rows)
5454

5555
for i = rows:-1:1
5656
sum = 0.0

contents/gaussian_elimination/gaussian_elimination.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ In code, this involves keeping a rolling sum of all the values we substitute in
420420

421421
{% method %}
422422
{% sample lang="jl" %}
423-
[import:47-67, lang:"julia"](code/julia/gaussian_elimination.jl)
423+
[import:47-64, lang:"julia"](code/julia/gaussian_elimination.jl)
424424
{% sample lang="c" %}
425425
[import:50-62, lang:"c_cpp"](code/c/gaussian_elimination.c)
426426
{% sample lang="rs" %}

contents/graham_scan/code/julia/graham.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Point
1+
struct Point
22
x::Float64
33
y::Float64
44
end
@@ -14,8 +14,8 @@ function graham_scan(points::Vector{Point})
1414
sort!(points, by = item -> item.y)
1515

1616
# Sort all other points according to angle with that point
17-
other_points = sort(points[2:end], by = item -> atan2(item.y - points[1].y,
18-
item.x - points[1].x))
17+
other_points = sort(points[2:end], by = item -> atan(item.y - points[1].y,
18+
item.x - points[1].x))
1919

2020
# Place points sorted by angle back into points vector
2121
for i in 1:length(other_points)
@@ -46,7 +46,11 @@ end
4646

4747
function main()
4848
# This hull is just a simple test so we know what the output should be
49-
points = [Point(2,1.9), Point(1, 1), Point(2, 4), Point(3, 1), Point(2, 0)]
49+
points = [
50+
Point(-5.,2), Point(5,7), Point(-6,-12), Point(-14,-14), Point(9,9),
51+
Point(-1,-1), Point(-10,11), Point(-6,15), Point(-6,-8), Point(15,-9),
52+
Point(7,-7), Point(-2,-9), Point(6,-5), Point(0,14), Point(2,8)
53+
]
5054
hull = graham_scan(points)
5155
println(hull)
5256
end

contents/split-operator_method/code/julia/split_op.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#
77
#------------------------------------------------------------------------------#
88

9+
using FFTW
10+
911
struct Param
1012
xmax::Float64
1113
res::Int64
@@ -37,17 +39,17 @@ mutable struct Operators
3739
K::Vector{Complex{Float64}}
3840
wfc::Vector{Complex{Float64}}
3941

40-
Operators(res) = new(Vector{Complex{Float64}}(res),
41-
Vector{Complex{Float64}}(res),
42-
Vector{Complex{Float64}}(res),
43-
Vector{Complex{Float64}}(res))
42+
Operators(res) = new(zeros(res),
43+
zeros(res),
44+
zeros(res),
45+
zeros(res))
4446
end
4547

4648
# Function to initialize the wfc and potential
4749
function init(par::Param, voffset::Float64, wfcoffset::Float64)
4850
opr = Operators(length(par.x))
49-
opr.V = 0.5 * (par.x - voffset).^2
50-
opr.wfc = exp.(-(par.x - wfcoffset).^2/2)
51+
opr.V = 0.5 * (par.x .- voffset).^2
52+
opr.wfc = exp.(-(par.x .- wfcoffset).^2/2)
5153
if (par.im_time)
5254
opr.K = exp.(-0.5*par.k.^2*par.dt)
5355
opr.R = exp.(-0.5*opr.V*par.dt)
@@ -93,7 +95,8 @@ function split_op(par::Param, opr::Operators)
9395
# Outputting data to file. Plotting can also be done in a similar way
9496
# This is set to output exactly 100 files, no matter how many timesteps
9597
if ((i-1) % div(par.timesteps, 100) == 0)
96-
outfile = open("output" *string(lpad(i-1, 5, 0))* ".dat","w")
98+
outfile = open("output" * string(lpad(string(i-1), 5, string(0)))
99+
* ".dat","w")
97100

98101
# Outputting for gnuplot. Any plotter will do.
99102
for j = 1:length(density)

contents/split-operator_method/split-operator_method.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Regardless, we first need to set all the initial parameters, including the initi
9898

9999
{% method %}
100100
{% sample lang="jl" %}
101-
[import:9-32, lang:"julia"](code/julia/split_op.jl)
101+
[import:11-34, lang:"julia"](code/julia/split_op.jl)
102102
{% sample lang="c" %}
103103
[import:10-20, lang:"c_cpp"](code/c/split_op.c)
104104
[import:51-72, lang:"c_cpp"](code/c/split_op.c)
@@ -115,7 +115,7 @@ Afterwards, we turn them into operators:
115115

116116
{% method %}
117117
{% sample lang="jl" %}
118-
[import:34-60, lang:"julia"](code/julia/split_op.jl)
118+
[import:36-62, lang:"julia"](code/julia/split_op.jl)
119119
{% sample lang="c" %}
120120
[import:22-28, lang:"c_cpp"](code/c/split_op.c)
121121
[import:74-95, lang:"c_cpp"](code/c/split_op.c)
@@ -133,7 +133,7 @@ The final step is to do the iteration, itself.
133133

134134
{% method %}
135135
{% sample lang="jl" %}
136-
[import:63-109, lang:"julia"](code/julia/split_op.jl)
136+
[import:65-112, lang:"julia"](code/julia/split_op.jl)
137137
{% sample lang="c" %}
138138
[import:97-145, lang:"c_cpp"](code/c/split_op.c)
139139
{% sample lang="py" %}

contents/stable_marriage_problem/code/julia/stable_marriage.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Submitted by Gustorn
1+
using Random
22

33
const mnames = ["A", "B", "C", "D"]
44
const wnames = ["E", "F", "G", "H"]

contents/tree_traversal/code/julia/Tree.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function DFS_recursive_inorder_btree(n::Node)
4343
end
4444

4545
function DFS_stack(n::Node)
46-
s = Stack(Node)
46+
s = Stack{Node}()
4747
push!(s, n)
4848

4949
while(length(s) > 0)
@@ -56,7 +56,7 @@ function DFS_stack(n::Node)
5656
end
5757

5858
function BFS_queue(n::Node)
59-
q = Queue(Node)
59+
q = Queue{Node}()
6060
enqueue!(q, n)
6161

6262
while(length(q) > 0)

0 commit comments

Comments
 (0)