Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a18e7cc
Adding flood fill in Coconut
Amaras Aug 5, 2020
7d2739b
Correcting copy-paste fails
Amaras Aug 5, 2020
5da3ae8
Flood Fill in C (#741)
Gathros Aug 6, 2020
d21dacd
Huffman Encoding Scratch Implementation (#682)
dovisutu Sep 10, 2020
aa08db0
Update monte_carlo.py (#751)
DougBR Sep 10, 2020
a4dbc2a
Fixed infinite loop issue in "euclidean_example.py" (#749)
valdaarhun Sep 11, 2020
9dcc06f
fix both error of issue 739 (#756)
Oct 12, 2020
627aed9
Add Flood Fill in Python (#753)
lazyprop Oct 12, 2020
fee7e83
Remove color function from flood fill because it is redundant (#766)
leios Oct 15, 2020
85f8e9a
Added Bogosort in Coconut (#735)
Amaras Oct 15, 2020
18944ea
Added Bubblesort in Coconut (#736)
Amaras Oct 15, 2020
f1aa057
Add Scheme alongside Racket in EditorConfig (#779)
berquist Nov 4, 2020
7bc0e22
Added CSS for printing (#777)
0xJonas Nov 5, 2020
91d5625
Added Cooley-Tukey in Lisp
0xJonas Nov 11, 2020
5a6ebd0
Make Euclid's algorithm in Nim more idiomatic (#784)
berquist Nov 11, 2020
153317d
Make Verlet integration in Nim more idiomatic (#782)
berquist Nov 11, 2020
0ca5ac6
Make Bogosort in Nim more idiomatic (#783)
berquist Nov 11, 2020
7ada7da
Formatting and refactoring
0xJonas Dec 27, 2020
c72caaf
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
70822e9
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
9d0a5bb
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
2793edf
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
8426216
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
e92bcb7
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
e631b57
Updated line number
0xJonas Dec 28, 2020
02dd353
Merge pull request #786 from 0xJonas/cooley-tukey-lisp
Trashtalk217 Jan 1, 2021
f9a6eda
Add Computus in Nim (#780)
berquist Jan 2, 2021
b49f034
Domain Coloring in Python using Matplotlib (#788)
0xJonas Jan 4, 2021
9d7349a
adding gif to show people how to change languages (#797)
leios Jan 25, 2021
5be74ec
Adding flood fill in Coconut
Amaras Aug 5, 2020
1949a10
Correcting copy-paste fails
Amaras Aug 5, 2020
a5c0f6b
Addressed part of Trashtalk's review
Amaras Feb 4, 2021
db00314
Merge remote-tracking branch 'origin/flood_fill_coconut' into flood_f…
Amaras Feb 4, 2021
f312994
Got closer to Julia's code logic + reworked the line numbers
Amaras Feb 4, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Formatting and refactoring
  • Loading branch information
0xJonas committed Dec 27, 2020
commit 7ada7da6a663f8c79afcdef840fcdcb51ef1fa01
92 changes: 51 additions & 41 deletions contents/cooley_tukey/code/clisp/fft.lisp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

(defun coefficient (time-index freq-index dft-len)
"Calculates a single twiddle factor for the Fourier Transform."
(exp (- (/ (* #c( 0 1) 2.0 pi time-index freq-index)
(exp (- (/ (* #c(0 1) 2.0 pi time-index freq-index)
dft-len))))

(defun dft (data)
Expand All @@ -14,38 +14,46 @@
(defun merge-sub-ffts (evens odds)
"Combines the FFTs of the even and odd indices"
(let* ((fft-length (+ (length evens) (length odds)))
(indices (loop for i from 0 below (length odds) collect i))
(odd-terms (mapcar #'(lambda (v i) (* v (coefficient 1.0 i fft-length)))
odds
indices)))
;; Calculate coefficients for the odd indices.
(twiddle-factors (loop for i from 0 below (length odds)
collect (coefficient 1.0 i fft-length)))
;; Multiply values with coefficients
(odd-terms (mapcar #'* odds twiddle-factors)))
;; Combine the two FFTs
(concatenate 'list
(mapcar #'+ evens odd-terms)
(mapcar #'- evens odd-terms))))

(defun cooley-tukey-rec (data)
"Performs the DFT using the recursive Cooley-Tukey method."
"Performs the Fourier Transform using the recursive Cooley-Tukey method."
(if (<= (length data) 1)
data
(loop for i from 0 below (length data)
if (evenp i)
collect (elt data i) into evens
else
collect (elt data i) into odds
finally
(return (merge-sub-ffts (cooley-tukey-rec evens)
(cooley-tukey-rec odds))))))
data
(loop
for i from 0 below (length data)
;; Split input into even and odd indices into two smaller lists.
if (evenp i)
collect (elt data i) into evens
else
collect (elt data i) into odds
finally
;; Calculate the Fourier Transform for the two smaller lists and
;; combine them into the Fourier Transform for the full input.
(return (merge-sub-ffts (cooley-tukey-rec evens)
(cooley-tukey-rec odds))))))

(defun reverse-bits (value num-bits)
"Reverses the bits of a value"
(if (= num-bits 1)
value
(let* ((num-low-bits (floor (/ num-bits 2)))
(num-high-bits (- num-bits num-low-bits))
(bit-mask (- (expt 2 num-low-bits) 1))
(lower-half (logand value bit-mask))
(upper-half (ash value (- num-low-bits))))
(logior (ash (reverse-bits lower-half num-low-bits) num-high-bits)
(reverse-bits upper-half num-high-bits)))))
value
;; Split bits into two parts
(let* ((num-low-bits (floor (/ num-bits 2)))
(num-high-bits (- num-bits num-low-bits))
(bit-mask (- (expt 2 num-low-bits) 1))
(lower-half (logand value bit-mask))
(upper-half (ash value (- num-low-bits))))
;; Reverse the bits of each part, then swap the results.
(logior (ash (reverse-bits lower-half num-low-bits) num-high-bits)
(reverse-bits upper-half num-high-bits)))))

(defun bit-shuffle-indices (data)
"Rearanges the elements in a list according to their bit-reversed indices."
Expand All @@ -61,27 +69,29 @@
(defun butterfly-group (data start stride)
"Calculates a single group of butterflies."
(dotimes (i stride)
(let* ((a-index (+ start i))
(b-index (+ start i stride))
(a (elt data a-index))
(b (elt data b-index))
;; Take two elements which are stride apart and perform a butterfly on them.
(let* ((first-elt-index (+ start i))
(second-elt-index (+ start i stride))
(first-elt (elt data first-elt-index))
(second-elt (elt data second-elt-index))
(coeff (coefficient 1.0 i (* 2 stride))))
(multiple-value-bind (sum difference) (butterfly a b coeff)
(setf (elt data a-index) sum)
(setf (elt data b-index) difference)))))

(defun cooley-tukey-tailrec (data stride)
"Actual iterative implementation of the Cooley-Tukey method."
(if (>= stride (length data))
data
(progn
(loop for i from 0 below (length data) by (* 2 stride) do
(butterfly-group data i stride))
(cooley-tukey-tailrec data (* 2 stride)))))
(multiple-value-bind (sum difference) (butterfly first-elt second-elt coeff)
;; Write results back into the list.
(setf (elt data first-elt-index) sum)
(setf (elt data second-elt-index) difference)))))

(defun cooley-tukey-iter (data)
"Performs the DFT using the iterative Cooley-Tukey method."
(cooley-tukey-tailrec (bit-shuffle-indices data) 1))
"Performs the Fourier Transform using the iterative Cooley-Tukey method."
(loop
;; Bit-shuffle indices.
with shuffled-data = (bit-shuffle-indices data)
for stride = 1 then (* 2 stride)
while (< stride (length shuffled-data))
do
;; Compute butterfly groups for the current stride.
(loop for i from 0 below (length shuffled-data) by (* 2 stride) do
(butterfly-group shuffled-data i stride))
finally (return shuffled-data)))

(defun approx-eql (list1 list2)
(let ((diffs (mapcar #'(lambda (e1 e2) (abs (- e1 e2)))
Expand Down