Skip to content

Commit ddcf300

Browse files
authored
Merge branch 'master' into euclid_asm
2 parents b4e7615 + 44181dc commit ddcf300

File tree

52 files changed

+1049
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1049
-215
lines changed

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
dist: trusty
2+
sudo: false
3+
4+
language: node_js
5+
node_js:
6+
- "4"
7+
8+
install:
9+
- npm install gitbook-cli -g
10+
- gitbook install
11+
12+
before_script:
13+
- mkdir -p "${TRAVIS_BUILD_DIR}"/build
14+
15+
script:
16+
- env | sort
17+
- gitbook build . "${TRAVIS_BUILD_DIR}"/build
18+
19+
after_success:
20+
- |
21+
if [[ "${TRAVIS_BRANCH}" == master && "${TRAVIS_PULL_REQUEST}" == false ]]; then
22+
# Commits to master that are not pull requests, that is, only
23+
# actual addition of code to master, should deploy the book to
24+
# the site.
25+
bash "${TRAVIS_BUILD_DIR}"/tools/deploy/update_site_travis.bash
26+
fi

CONTRIBUTORS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ Michal Hanajik
5858
<br>
5959
Bendik Samseth
6060
<br>
61+
mukundan314
62+
<br>
6163
Trashtalk
6264
<br>
6365
Cyrus Burt
66+
<br>
67+
Patrik Tesarik
68+
<br>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and live coded on Twitch: https://www.twitch.tv/simuleios.
1515
If you would like to communicate more directly, please feel free to go to our discord: https://discord.gg/Pr2E9S6.
1616

1717

18-
Note that the this project is essentially a book about algorithms collaboratively written by an online community.
18+
Note that this project is essentially a book about algorithms collaboratively written by an online community.
1919
Fortunately, there are a lot of algorithms out there, which means that there is a lot of content material available.
2020
Unfortunately, this means that we will probably never cover every algorithm ever created and instead need to focus on what the community sees as useful and necessary.
2121
That said, we'll still cover a few algorithms for fun that have very little, if any practical purpose.

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
{
140140
"lang": "x64",
141141
"name": "X86-64 Assembly"
142+
},
143+
{
144+
"lang": "f90",
145+
"name": "Fortran90"
142146
}
143147
]
144148
}

contents/bogo_sort/bogo_sort.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ In code, it looks something like this:
1414

1515
{% method %}
1616
{% sample lang="jl" %}
17-
[import:10-14, lang:"julia"](code/julia/bogo.jl)
17+
[import:12-16, lang:"julia"](code/julia/bogo.jl)
1818
{% sample lang="cs" %}
1919
[import:9-15, lang:"csharp"](code/csharp/BogoSort.cs)
2020
{% sample lang="clj" %}
@@ -36,7 +36,7 @@ In code, it looks something like this:
3636
{% sample lang="rs" %}
3737
[import:16-20, lang:"rust"](code/rust/bogosort.rs)
3838
{% sample lang="swift" %}
39-
[import:25-31, lang:"swift"](code/swift/bogosort.swift)
39+
[import:13-19, lang:"swift"](code/swift/bogosort.swift)
4040
{% sample lang="php" %}
4141
[import:11-16, lang:"php"](code/php/bogo_sort.php)
4242
{% sample lang="nim" %}

contents/bogo_sort/code/c++/bogosort.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void print_range(std::ostream& os, Iter const first, Iter const last) {
2424

2525
if (first != last) {
2626
os << *first;
27-
std::for_each(first + 1, last, [&os] (double d) { os << ", " << *it; });
27+
std::for_each(first + 1, last, [&os] (double d) { os << ", " << d; });
2828
}
2929

3030
os << "}\n";

contents/bogo_sort/code/julia/bogo.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using Random
2+
13
function is_sorted(a::Vector{Float64})
24
for i = 1:length(a)-1
3-
if (a[i+1] > a[i])
5+
if (a[i] > a[i + 1])
46
return false
57
end
68
end
@@ -14,7 +16,7 @@ function bogo_sort(a::Vector{Float64})
1416
end
1517

1618
function main()
17-
a = [1., 3, 2,4]
19+
a = [1, 3, 2, 4]
1820
bogo_sort(a)
1921
println(a)
2022
end

contents/bogo_sort/code/swift/bogosort.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,15 @@ func isSorted(inputArray: [Int]) -> Bool {
66
return false
77
}
88
}
9-
10-
return true
11-
}
12-
13-
func shuffle(inputArray: inout [Int]) -> [Int] {
14-
var shuffledArray = [Int]()
159

16-
for _ in 0..<inputArray.count {
17-
let rand = Int(arc4random_uniform(UInt32(inputArray.count)))
18-
shuffledArray.append(inputArray[rand])
19-
inputArray.remove(at: rand)
20-
}
21-
22-
return shuffledArray
10+
return true
2311
}
2412

2513
func bogoSort(sortArray: inout [Int]) -> [Int] {
2614
while(!isSorted(inputArray: sortArray)) {
27-
sortArray = shuffle(inputArray: &sortArray)
15+
sortArray.shuffle()
2816
}
29-
17+
3018
return sortArray
3119
}
3220

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
4848
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp)
4949
{% sample lang="nim" %}
5050
[import:5-9, lang:"nim"](code/nim/bubble_sort.nim)
51+
{% sample lang="f90" %}
52+
[import:19-40, lang:"fortran"](code/fortran/bubble.f90)
5153
{% endmethod %}
5254

5355
... And that's it for the simplest bubble sort method.
@@ -101,6 +103,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
101103
[import, lang:"lisp"](code/lisp/bubble_sort.lisp)
102104
{% sample lang="nim" %}
103105
[import, lang:"nim"](code/nim/bubble_sort.nim)
106+
{% sample lang="f90" %}
107+
[import, lang:"fortran"](code/fortran/bubble.f90)
104108
{% endmethod %}
105109

106110
<script>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
PROGRAM main
2+
3+
IMPLICIT NONE
4+
REAL(8), DIMENSION(10) :: A
5+
6+
A = (/ 1d0, 3d0, 2d0, 4d0, 5d0, 10d0, 50d0, 7d0, 1.5d0, 0.3d0 /)
7+
8+
WRITE(*,*) 'Input vector'
9+
WRITE(*,'( F6.2 )') A
10+
WRITE(*,*) ' '
11+
12+
CALL bubblesort(A)
13+
14+
WRITE(*,*) 'Output vector'
15+
WRITE(*,'(F6.2)') A
16+
17+
CONTAINS
18+
19+
SUBROUTINE bubblesort(array)
20+
21+
IMPLICIT NONE
22+
INTEGER :: array_length, i, j, n
23+
REAL(8) :: tmp
24+
REAL(8), DIMENSION(:), INTENT(INOUT) :: array
25+
26+
array_length = size(array)
27+
n = array_length
28+
29+
DO i=1, n
30+
DO j=1, n-1
31+
IF ( array(j) > array(j+1) ) THEN
32+
33+
tmp = array(j+1)
34+
array(j+1) = array(j)
35+
array(j) = tmp
36+
37+
END IF
38+
END DO
39+
END DO
40+
END SUBROUTINE bubblesort
41+
42+
END PROGRAM main
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
(ns fft.core
2+
(:require [complex.core :as c]))
3+
;; complex is a jar for complex numbers
4+
;; https://github.com/alanforr/complex
5+
;; add [complex "0.1.11"] to :dependencies in your project.clj
6+
;; and run lein repl or lein deps in the terminal
7+
(defn matrix-mult
8+
"take a matrix m and a vector v which length is number of columns
9+
,return a vector of applying dot-product between v and each row of
10+
m. the returned vector's length is the number of rows of m"
11+
[m v]
12+
(mapv (comp (partial apply c/+)
13+
(partial map c/* v))
14+
m))
15+
(defn dft
16+
"take a vector of real numbers and return a vector of frequency
17+
space"
18+
[vx]
19+
(let [len (count vx)]
20+
(matrix-mult
21+
(partition len
22+
(for [n (range len)
23+
k (range len)]
24+
;; expresion below is
25+
;; e^(n*k*2*pi*(1/len)*(-i))
26+
(c/exp (c/* n k
27+
2 Math/PI
28+
(/ len)
29+
(c/complex 0 -1)))))
30+
vx)))
31+
(defn fft [vx]
32+
(let [len (count vx)]
33+
(if (= len 1)
34+
vx
35+
;;else
36+
(let [;; take values of vx in the even indices
37+
even-indices (keep-indexed #(if (even? %1) %2) vx)
38+
;; take values in the odd indices
39+
odd-indices (keep-indexed #(if (odd? %1) %2) vx)
40+
;; recursion
41+
even-fft (fft even-indices)
42+
odd-fft (fft odd-indices)
43+
;; make a sequence of e^(-2pi*i*k/N) where N is the length
44+
;; vx and k range from 0 to N/2
45+
omegas-half (map
46+
(comp c/exp
47+
(partial c/*
48+
(/ len)
49+
2 Math/PI
50+
(c/complex 0 -1)))
51+
(range 0 (quot len 2)))
52+
;; take the negative of the first sequence because
53+
;; e^(-2pi*i*(k+N/2)/N=-e^(-2pi*i*k/N) where k ranges from
54+
;; 0 to N/2
55+
omegas-2half (map c/- omegas-half)
56+
mult-add (partial map #(c/+ %3 (c/* %1 %2)))]
57+
(concat (mult-add omegas-half odd-fft even-fft)
58+
(mult-add omegas-2half odd-fft even-fft))))))
59+
(defn -main [& args]
60+
(let [vx [0 1 2 3]
61+
len (count vx)
62+
;; calculate the next power of 2 after len
63+
;; the reason behind this is to fill them with zeros for fft
64+
next-len (->>
65+
[len 2]
66+
(map #(Math/log %))
67+
(apply /)
68+
Math/ceil
69+
(Math/pow 2)
70+
int)
71+
;; add zeros at the end of vx
72+
complete-vx (into vx (repeat (- next-len len) 0))
73+
fft-cvx (fft complete-vx)
74+
dft-cvx (dft complete-vx)
75+
diffv (mapv c/- fft-cvx dft-cvx)]
76+
(println "vx:" vx)
77+
(println "complete-vx:" complete-vx)
78+
(println "result from fft:" (map c/stringify fft-cvx))
79+
(println "result from dft:" (map c/stringify dft-cvx))
80+
(println "difference: " (map c/stringify diffv))))

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/code/python/fft.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
from cmath import exp, pi
33
from math import log2
44

5+
def dft(X):
6+
N = len(X)
7+
temp = [0]*N
8+
for i in range(N):
9+
for k in range(N):
10+
temp[i] += X[k] * exp(-2.0j*pi*i*k/N)
11+
return temp
12+
513
def cooley_tukey(X):
614
N = len(X)
715
if N <= 1:
@@ -47,5 +55,7 @@ def iterative_cooley_tukey(X):
4755

4856
Y = cooley_tukey(X)
4957
Z = iterative_cooley_tukey(X)
58+
T = dft(X)
5059

5160
print(all(abs([Y[i] - Z[i] for i in range(64)][j]) < 1 for j in range(64)))
61+
print(all(abs([Y[i] - T[i] for i in range(64)][j]) < 1 for j in range(64)))

contents/cooley_tukey/cooley_tukey.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@ 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)
76+
{% sample lang="clj" %}
77+
[import:15-30, lang:"clojure"](code/clojure/fft.clj)
7678
{% sample lang="cpp" %}
77-
[import:2-11, lang:"julia"](code/julia/fft.jl)
79+
[import:23-33, lang:"c_cpp"](code/c++/fft.cpp)
7880
{% sample lang="hs" %}
79-
[import:2-11, lang:"julia"](code/julia/fft.jl)
81+
[import:4-13, lang:"julia"](code/julia/fft.jl)
8082
{% sample lang="py" %}
81-
[import:2-11, lang:"julia"](code/julia/fft.jl)
83+
[import:5-11, lang:"python"](code/python/fft.py)
8284
{% sample lang="scratch" %}
83-
[import:2-11, lang:"julia"](code/julia/fft.jl)
85+
[import:4-13, lang:"julia"](code/julia/fft.jl)
8486
{% endmethod %}
8587

8688
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,17 +117,19 @@ With recursion, we can reduce the complexity to $$\sim \mathcal{O}(n \log n)$$,
115117
In the end, the code looks like:
116118
{% method %}
117119
{% sample lang="jl" %}
118-
[import:14-31, lang:"julia"](code/julia/fft.jl)
120+
[import:16-32, lang:"julia"](code/julia/fft.jl)
119121
{% sample lang="c" %}
120122
[import:20-39, lang:"c_cpp"](code/c/fft.c)
123+
{% sample lang="clj" %}
124+
[import:31-58, lang:"clojure"](code/clojure/fft.clj)
121125
{% sample lang="cpp" %}
122-
[import:35-66, lang:"c_cpp"](code/c++/fft.cpp)
126+
[import:36-66, lang:"c_cpp"](code/c++/fft.cpp)
123127
{% sample lang="hs" %}
124128
[import:6-19, lang:"haskell"](code/haskell/fft.hs)
125129
{% sample lang="py" %}
126-
[import:5-16, lang:"python"](code/python/fft.py)
130+
[import:13-24, lang:"python"](code/python/fft.py)
127131
{% sample lang="scratch" %}
128-
[import:14-31, lang:"julia"](code/julia/fft.jl)
132+
[import:16-32, lang:"julia"](code/julia/fft.jl)
129133
{% endmethod %}
130134

131135
As a side note, we are enforcing that the array must be a power of 2 for the operation to work.
@@ -222,6 +226,8 @@ Note: I implemented this in Julia because the code seems more straightforward in
222226
[import, lang:"julia"](code/julia/fft.jl)
223227
{% sample lang="c" %}
224228
[import, lang:"c_cpp"](code/c/fft.c)
229+
{% sample lang="clj" %}
230+
[import, lang:"clojure"](code/clojure/fft.clj)
225231
{% sample lang="cpp" %}
226232
[import, lang:"c_cpp"](code/c++/fft.cpp)
227233
{% sample lang="hs" %}

0 commit comments

Comments
 (0)