Skip to content

Commit a47bdee

Browse files
authored
Merge pull request algorithm-archivists#113 from leios/smallscale_updates
adding smallscale changes to graham implementation, fft, and thomas.
2 parents 81cb3be + ed9bd18 commit a47bdee

File tree

3 files changed

+7
-28
lines changed
  • chapters

3 files changed

+7
-28
lines changed

chapters/FFT/code/julia/fft.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function DFT(x)
55
# We want two vectors here for real space (n) and frequency space (k)
66
n = 0:N-1
77
k = n'
8-
transform_matrix = exp.(-2im * pi *n *k / N)
8+
transform_matrix = exp.(-2im*pi*n*k/N)
99
return transform_matrix*x
1010

1111
end
@@ -22,7 +22,6 @@ function cooley_tukey(x)
2222
x_even = x[2]
2323
end
2424
n = 0:N-1
25-
#n = n'
2625
half = div(N,2)
2726
factor = exp.(-2im*pi*n/N)
2827
return vcat(x_odd + x_even .* factor[1:half],

chapters/computational_geometry/gift_wrapping/graham_scan/code/julia/graham.jl

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@ function dist(point1::Point, point2::Point)
77
return sqrt((point1.x - point2.x)^2 + (point1.y - point2.y)^2)
88
end
99

10-
function graham_angle(point1::Point, point2::Point, point3::Point)
11-
# Find distances between all points
12-
a = dist(point3, point2)
13-
b = dist(point3, point1)
14-
c = dist(point1, point2)
15-
16-
ret_angle = acos((b*b - a*a - c*c)/(2*a*c))
17-
18-
if(sign(point1.x - point2.x) != sign(point1.x - point3.x))
19-
ret_angle += 0.5*pi
20-
end
21-
22-
if (isnan(ret_angle))
23-
exit(1)
24-
end
25-
26-
return ret_angle
27-
28-
end
29-
3010
function ccw(a::Point, b::Point, c::Point)
3111
return ((b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x))
3212
end
@@ -38,7 +18,8 @@ function graham_scan(points::Vector{Point})
3818
sort!(points, by = item -> item.y)
3919

4020
# Sort all other points according to angle with that point
41-
other_points = sort(points[2:end], by = item -> graham_angle(Point(points[1].x - 1,points[1].y), points[1], item))
21+
other_points = sort(points[2:end], by = item -> atan2(item.y - points[1].y,
22+
item.x - points[1].x))
4223

4324
# Place points sorted by angle back into points vector
4425
for i in 1:length(other_points)
@@ -69,6 +50,7 @@ function graham_scan(points::Vector{Point})
6950
end
7051

7152
function main()
53+
# This hull is just a simple test so we know what the output should be
7254
points = [Point(2,1.9), Point(1, 1), Point(2, 4), Point(3, 1), Point(2, 0)]
7355
hull = graham_scan(points)
7456
println(hull)

chapters/matrix_methods/thomas/thomas.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
##### Dependencies
2-
* [Gaussian Elimination](gaussian_elimination.md)
3-
41
# Thomas Algorithm
52

6-
As alluded to in the Gaussian Elimination Chapter, the Thomas Algorithm (or TDMA -- Tri-Diagonal Matrix Algorithm) allows for programmers to **massively** cut the computational cost of their code from $$\sim O(n^3) \rightarrow \sim O(n)$$! This is done by exploiting a particular case of Gaussian Elimination, particularly the case where our matrix looks like:
3+
As alluded to in the [Gaussian Elimination chapter](../gaussian_elimination/gaussian_elimination.md), the Thomas Algorithm (or TDMA -- Tri-Diagonal Matrix Algorithm) allows for programmers to **massively** cut the computational cost of their code from $$\sim O(n^3) \rightarrow \sim O(n)$$! This is done by exploiting a particular case of Gaussian Elimination, particularly the case where our matrix looks like:
74

85
$$
96
\left[
@@ -19,7 +16,8 @@ $$
1916

2017
By this, I mean that our matrix is *Tri-Diagonal* (excluding the right-hand side of our system of equations, of course!). Now, at first, it might not be obvious how this helps; however, we may divide this array into separate vectors corresponding to $$a$$, $$b$$, $$c$$, and $$d$$ and then solve for $$x$$ with back-substitution, like before.
2118

22-
In particular, we need to find an optimal scale factor for each row and use that. What is the scale factor? Well, it is the diagonal $$-$$ the multiplicative sum of the off-diagonal elements. In the end, we will update $$c$$ and $$d$$ to be$$c'$$ and $$d'$$ like so:
19+
In particular, we need to find an optimal scale factor for each row and use that. What is the scale factor? Well, it is the diagonal $$-$$ the multiplicative sum of the off-diagonal elements.
20+
In the end, we will update $$c$$ and $$d$$ to be $$c'$$ and $$d'$$ like so:
2321

2422
$$
2523
\begin{align}

0 commit comments

Comments
 (0)