Skip to content

Commit

Permalink
Merging from origin, again
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegillet committed Jul 3, 2018
2 parents f7a5306 + 16ca5bf commit 5cba46c
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 49 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ indent_size = 4
indent_style = space
indent_size = 2

# Matlab
[*.m]
indent_style = space
indent_size = 4

# Markdown
[*.md]
indent_style = space
Expand All @@ -85,6 +90,11 @@ indent_size = 2
indent_style = space
indent_size = 4

# Racket
[*.rkt]
indent_style = space
indent_size = 2

# Rust
[*.rs]
indent_style = space
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Chinmaya Mahesh
Unlambder
Kjetil Johannessen
CDsigma
DominikRafacz
12 changes: 6 additions & 6 deletions chapters/FFT/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Truth be told, I didn't understand it fully until I discretized real and frequen

In principle, the Discrete Fourier Transform (DFT) is simply the Fourier transform with summations instead of integrals:

$$X_k = \sum_{n=0}^{N-1} x_n \cdot e^{-2 \pi k n / N}$$
$$X_k = \sum_{n=0}^{N-1} x_n \cdot e^{-2 \pi i k n / N}$$

and

$$x_n = \frac{1}{N} \sum_{k=0}^{N-1} X_k \cdot e^{2 \pi k n / N}$$
$$x_n = \frac{1}{N} \sum_{k=0}^{N-1} X_k \cdot e^{2 \pi i k n / N}$$

Where $$X_n$$ and $$x_n$$ are sequences of $$N$$ numbers in frequency and real space, respectively.
In principle, this is no easier to understand than the previous case!
Expand All @@ -72,7 +72,7 @@ For some reason, though, putting code to this transformation really helped me fi
{% sample lang="jl" %}
[import:2-11, lang:"julia"](code/julia/fft.jl)
{% sample lang="c" %}
[import:7-19, lang:"c_cpp"](code/c/fft.c)
[import:8-19, lang:"c_cpp"](code/c/fft.c)
{% sample lang="cpp" %}
[import:2-11, lang:"julia"](code/julia/fft.jl)
{% sample lang="hs" %}
Expand Down Expand Up @@ -117,7 +117,7 @@ In the end, the code looks like:
{% sample lang="jl" %}
[import:14-31, lang:"julia"](code/julia/fft.jl)
{% sample lang="c" %}
[import:21-40, lang:"c_cpp"](code/c/fft.c)
[import:20-39, lang:"c_cpp"](code/c/fft.c)
{% sample lang="cpp" %}
[import:27-57, lang:"c_cpp"](code/c++/fft.cpp)
{% sample lang="hs" %}
Expand All @@ -138,12 +138,12 @@ Butterfly Diagrams show where each element in the array goes before, during, and
As mentioned, the FFT must perform a DFT.
This means that even though we need to be careful about how we add elements together, we are still ultimately performing the following operation:

$$X_k = \sum_{n=0}^{N-1} x_n \cdot e^{-2 \pi k n / N}$$
$$X_k = \sum_{n=0}^{N-1} x_n \cdot e^{-2 \pi i k n / N}$$

However, after shuffling the initial array (by bit reversing or recursive subdivision), we perform the matrix multiplication of the $$e^{-2 \pi k n / N}$$ terms in pieces.
Basically, we split the array into a series of omega values:

$$\omega_N^k = e^{-2 \pi k / N}$$
$$\omega_N^k = e^{-2 \pi i k / N}$$

And at each step, we use the appropriate term.
For example, imagine we need to perform an FFT of an array of only 2 elements.
Expand Down
26 changes: 0 additions & 26 deletions chapters/differential_equations.md

This file was deleted.

35 changes: 35 additions & 0 deletions chapters/monte_carlo/code/java/MonteCarlo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//submitted by DominikRafacz
import java.util.Random;

public class MonteCarlo {

public static void main(String[] args) {
monteCarlo(10_000_000);
}

//function to check whether point (x,y) is in unit circle
private static boolean inCircle(double x, double y) {
return x * x + y * y < 1;
}

//function to calculate estimation of pi
public static void monteCarlo(int samples) {
int piCount = 0;

Random random = new Random();

for (int i = 0; i < samples; i++) {
double x = random.nextDouble();
double y = random.nextDouble();
if (inCircle(x, y)) {
piCount++;
}
}

double estimation = 4.0 * piCount / samples;

System.out.println("Estimated pi value: " + estimation);
System.out.printf("Percent error: %.4f%%",
100 * (Math.PI - estimation) / Math.PI);
}
}
5 changes: 5 additions & 0 deletions chapters/monte_carlo/monte_carlo.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ each point is tested to see whether it's in the circle or not:
[import:2-5, lang:"d"](code/d/monte_carlo.d)
{% sample lang="go" %}
[import:12-14, lang:"golang"](code/go/monteCarlo.go)
{% sample lang="java" %}
[import:11-13, lang:"java"](code/java/MonteCarlo.java)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down Expand Up @@ -108,6 +110,9 @@ Feel free to submit your version via pull request, and thanks for reading!
{%sample lang="go" %}
### Go
[import, lang:"golang"](code/go/monteCarlo.go)
{% sample lang="java" %}
### Java
[import, lang:"java"](code/java/MonteCarlo.java)
{% endmethod %}


Expand Down
6 changes: 3 additions & 3 deletions chapters/sorting_searching/bogo/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ imagine you have an array of $$n$$ elements that you want sorted.
One way to do it is to shuffle the array at random and hope that all the elements will be magically in order after shuffling.
If they are not in order, just shuffle everything again.
And then again. And again.
In the best case, this algorithm runs with a complexity of $$\Omega(1)$$, and in the worst, $$\mathcal{O}(\infty)$$.
In the best case, this algorithm runs with a complexity of $$\Omega(n)$$, and in the worst, $$\mathcal{O}(\infty)$$.

In code, it looks something like this:

Expand All @@ -18,9 +18,9 @@ In code, it looks something like this:
{% sample lang="cs" %}
[import:9-15, lang:"csharp"](code/cs/BogoSort.cs)
{% sample lang="clj" %}
[import:2-10, lang:"clojure"](code/clojure/bogo.clj)
[import:2-11, lang:"clojure"](code/clojure/bogo.clj)
{% sample lang="c" %}
[import:4-27, lang:"c_cpp"](code/c/bogo_sort.c)
[import:4-29, lang:"c_cpp"](code/c/bogo_sort.c)
{% sample lang="java" %}
[import:2-17, lang:"java"](code/java/bogo.java)
{% sample lang="js" %}
Expand Down
2 changes: 1 addition & 1 deletion chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
{% sample lang="cs" %}
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
{% sample lang="c" %}
[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c)
[import:4-22, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="java" %}
[import:2-12, lang:"java"](code/java/bubble.java)
{% sample lang="js" %}
Expand Down
39 changes: 34 additions & 5 deletions chapters/tree_traversal/code/python/Tree_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Node:

def __init__(self):
self.data = None
self.children = []


def create_tree(node, num_row, num_child):
node.data = num_row

Expand All @@ -14,12 +14,37 @@ def create_tree(node, num_row, num_child):

return node


def DFS_recursive(node):
if len(node.children) > 0:
if node.data != None:
print(node.data)

for child in node.children:
DFS_recursive(child)


def DFS_recursive_postorder(node):
for child in node.children:
DFS_recursive(child)

if node.data != None:
print(node.data)

for child in node.children:
DFS_recursive(child)

# This assumes only 2 children, but accounts for other possibilities
def DFS_recursive_inorder_btree(node):
if (len(node.children) == 2):
DFS_recursive_inorder_btree(node.children[1])
print(node.data)
DFS_recursive_inorder_btree(node.children[2])
elif (len(node.children) == 1):
DFS_recursive_inorder_btree(node.children[1])
print(node.data)
elif (len(node.children) == 0):
print(node.data)
else:
print("Not a binary tree!")


def DFS_stack(node):
stack = []
Expand All @@ -34,6 +59,7 @@ def DFS_stack(node):
for child in temp.children:
stack.append(child)


def BFS_queue(node):
queue = []
queue.append(node)
Expand All @@ -47,6 +73,7 @@ def BFS_queue(node):
for child in temp.children:
queue.append(child)


def main():
tree = create_tree(Node(), 3, 3)

Expand All @@ -59,5 +86,7 @@ def main():
print("Queue:")
BFS_queue(tree)

main()

if __name__ == '__main__':
main()

14 changes: 6 additions & 8 deletions chapters/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
This has not been implemented in your chosen language, so here is the Julia code
[import:3-7, lang:"julia"](code/julia/Tree.jl)
{% sample lang="py" %}
[import:1-5, lang:"python"](code/python/Tree_example.py)
[import:1-4, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:3-7, lang:"julia"](code/julia/Tree.jl)
Expand All @@ -43,7 +43,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
{% sample lang="js" %}
[import:12-15, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:7-15, lang:"python"](code/python/Tree_example.py)
[import:18-23, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:9-16, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -79,8 +79,7 @@ This has not been implemented in your chosen language, so here is the Julia code
{% sample lang="js" %}
[import:17-20, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
[import:26-31, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -112,8 +111,7 @@ This has not been implemented in your chosen language, so here is the Julia code
{% sample lang="js" %}
[import:22-34, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
[import:34-46, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -154,7 +152,7 @@ In code, it looks like this:
{% sample lang="js" %}
[import:36-43, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:24-35, lang:"python"](code/python/Tree_example.py)
[import:49-60, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:45-56, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -187,7 +185,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
{% sample lang="js" %}
[import:45-52, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:37-48, lang:"python"](code/python/Tree_example.py)
[import:63-74, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:58-69, lang:"julia"](code/julia/Tree.jl)
Expand Down

0 comments on commit 5cba46c

Please sign in to comment.