Skip to content

Commit 94a96ae

Browse files
authored
Merge branch 'master' into simplify-rust-impls
2 parents b8df0bb + 6d718f7 commit 94a96ae

File tree

8 files changed

+152
-12
lines changed

8 files changed

+152
-12
lines changed

contents/bogo_sort/code/rust/bogosort.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ extern crate rand;
44

55
use rand::{thread_rng, Rng};
66

7-
fn is_sorted(arr : &[i32]) -> bool {
7+
fn is_sorted(arr: &[i32]) -> bool {
88
for i in 1..arr.len() {
9-
if arr[i-1] > arr[i] {
9+
if arr[i - 1] > arr[i] {
1010
return false;
1111
}
1212
}
1313
true
1414
}
1515

16-
fn bogo_sort(arr : &mut [i32]) {
16+
fn bogo_sort(arr: &mut [i32]) {
1717
while !is_sorted(arr) {
1818
thread_rng().shuffle(arr);
1919
}

contents/bubble_sort/code/rust/bubble_sort.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate rand; // External crate that provides random number generation tools
22

3-
use rand::{thread_rng, Rng}; // Used for random number generation
43
use rand::distributions::Uniform; // Used for a uniform distribution
4+
use rand::{thread_rng, Rng}; // Used for random number generation
55

66
fn bubble_sort(a: &mut [u32]) {
77
let n = a.len();
@@ -24,4 +24,4 @@ fn main() {
2424
println!("Before sorting: {:?}", rand_vec);
2525
bubble_sort(&mut rand_vec);
2626
println!("After sorting: {:?}", rand_vec);
27-
}
27+
}

contents/euclidean_algorithm/code/rust/euclidean_example.rs

-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ fn main() {
3232
println!("{}", chk1);
3333
println!("{}", chk2);
3434
}
35-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <iomanip>
4+
#include <iostream>
5+
#include <vector>
6+
7+
8+
void gaussianElimination(std::vector<std::vector<double> > &eqns) {
9+
// 'eqns' is the matrix, 'rows' is no. of vars
10+
int rows = eqns.size(), cols = eqns[0].size();
11+
12+
for (int i = 0; i < rows - 1; i++) {
13+
int pivot = i;
14+
15+
for (int j = i + 1; j < rows; j++) {
16+
if (fabs(eqns[j][i]) > fabs(eqns[pivot][i])) pivot = j;
17+
}
18+
19+
if (eqns[pivot][i] == 0.0)
20+
continue; // But continuing to simplify the matrix as much as possible
21+
22+
if (i != pivot) // Swapping the rows if new row with higher maxVals is found
23+
std::swap(eqns[pivot], eqns[i]); // C++ swap function
24+
25+
for (int j = i + 1; j < rows; j++) {
26+
double scale = eqns[j][i] / eqns[i][i];
27+
28+
for (int k = i + 1; k < cols; k++) // k doesn't start at 0, since
29+
eqns[j][k] -= scale * eqns[i][k]; // values before from 0 to i
30+
// are already 0
31+
eqns[j][i] = 0.0;
32+
}
33+
}
34+
}
35+
36+
void gaussJordan(std::vector<std::vector<double> > &eqns) {
37+
// 'eqns' is the (Row-echelon) matrix, 'rows' is no. of vars
38+
int rows = eqns.size();
39+
40+
for (int i = rows - 1; i >= 0; i--) {
41+
42+
if (eqns[i][i] != 0) {
43+
44+
eqns[i][rows] /= eqns[i][i];
45+
eqns[i][i] = 1; // We know that the only entry in this row is 1
46+
47+
// subtracting rows from below
48+
for (int j = i - 1; j >= 0; j--) {
49+
eqns[j][rows] -= eqns[j][i] * eqns[i][rows];
50+
eqns[j][i] = 0; // We also set all the other values in row to 0 directly
51+
}
52+
}
53+
}
54+
}
55+
56+
std::vector<double> backSubs(const std::vector<std::vector<double> > &eqns) {
57+
// 'eqns' is matrix, 'rows' is no. of variables
58+
int rows = eqns.size();
59+
60+
std::vector<double> ans(rows);
61+
for (int i = rows - 1; i >= 0; i--) {
62+
double sum = 0.0;
63+
64+
for (int j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j];
65+
66+
if (eqns[i][i] != 0)
67+
ans[i] = (eqns[i][rows] - sum) / eqns[i][i];
68+
else
69+
return std::vector<double>(0);
70+
}
71+
return ans;
72+
}
73+
74+
75+
void printMatrix(const std::vector<std::vector<double> > &matrix) {
76+
for (int row = 0; row < matrix.size(); row++) {
77+
std::cout << "[";
78+
79+
for (int col = 0; col < matrix[row].size() - 1; col++)
80+
std::cout << std::setw(8) << std::fixed << std::setprecision(3)
81+
<< matrix[row][col];
82+
83+
std::cout << " |" << std::setw(8) << std::fixed << std::setprecision(3)
84+
<< matrix[row].back() << " ]" << std::endl;
85+
}
86+
}
87+
88+
89+
int main() {
90+
std::vector<std::vector<double> > equations{
91+
{2, 3, 4, 6},
92+
{1, 2, 3, 4},
93+
{3, -4, 0, 10}};
94+
95+
std::cout << "Initial matrix:" << std::endl;
96+
printMatrix(equations);
97+
std::cout << std::endl;
98+
99+
gaussianElimination(equations);
100+
std::cout << "Matrix after gaussian elimination:" << std::endl;
101+
printMatrix(equations);
102+
std::cout << std::endl;
103+
104+
std::vector<double> ans = backSubs(equations);
105+
std::cout << "Solution from backsubstitution" << std::endl;
106+
std::cout << "x = " << ans[0] << ", y = " << ans[1] << ", z = " << ans[2]
107+
<< std::endl
108+
<< std::endl;
109+
110+
gaussJordan(equations);
111+
std::cout << "Matrix after Gauss Jordan:" << std::endl;
112+
printMatrix(equations);
113+
std::cout << std::endl;
114+
}

contents/gaussian_elimination/gaussian_elimination.md

+12
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ In code, this process might look like this:
314314
{% sample lang="c" %}
315315
[import:5-13, lang:"c"](code/c/gaussian_elimination.c)
316316
[import:19-34, lang:"c"](code/c/gaussian_elimination.c)
317+
{% sample lang="cpp" %}
318+
[import:13-23, lang:"cpp"](code/c++/gaussian_elimination.cpp)
317319
{% sample lang="hs" %}
318320
[import:10-17, lang:"haskell"](code/haskell/gaussianElimination.hs)
319321
[import:44-46, lang:"haskell"](code/haskell/gaussianElimination.hs)
@@ -384,6 +386,8 @@ Here is what it might look like in code:
384386
[import:32-40, lang:"java"](code/java/GaussianElimination.java)
385387
{% sample lang="c" %}
386388
[import:36-41, lang:"c"](code/c/gaussian_elimination.c)
389+
{% sample lang="cpp" %}
390+
[import:25-32, lang:"cpp"](code/c++/gaussian_elimination.cpp)
387391
{% sample lang="hs" %}
388392
[import:19-33, lang:"haskell"](code/haskell/gaussianElimination.hs)
389393
[import:42-42, lang:"haskell"](code/haskell/gaussianElimination.hs)
@@ -403,6 +407,8 @@ When we put everything together, it looks like this:
403407
[import:1-45, lang:"julia"](code/julia/gaussian_elimination.jl)
404408
{% sample lang="c" %}
405409
[import:15-48, lang:"c"](code/c/gaussian_elimination.c)
410+
{% sample lang="cpp" %}
411+
[import:8-34, lang:"cpp"](code/c++/gaussian_elimination.cpp)
406412
{% sample lang="rs" %}
407413
[import:41-78, lang:"rust"](code/rust/gaussian_elimination.rs)
408414
{% sample lang="hs" %}
@@ -440,6 +446,8 @@ Here it is in code:
440446
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
441447
{% sample lang="c" %}
442448
[import:64-82, lang:"c"](code/c/gaussian_elimination.c)
449+
{% sample lang="cpp" %}
450+
[import:36-54, lang:"cpp"](code/c++/gaussian_elimination.cpp)
443451
{% sample lang="rs" %}
444452
This code does not exist yet in rust, so here's Julia code (sorry for the inconvenience)
445453
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
@@ -481,6 +489,8 @@ In code, it looks like this:
481489
[import:47-64, lang:"julia"](code/julia/gaussian_elimination.jl)
482490
{% sample lang="c" %}
483491
[import:50-62, lang:"c"](code/c/gaussian_elimination.c)
492+
{% sample lang="cpp" %}
493+
[import:56-72, lang:"cpp"](code/c++/gaussian_elimination.cpp)
484494
{% sample lang="rs" %}
485495
[import:79-94, lang:"rust"](code/rust/gaussian_elimination.rs)
486496
{% sample lang="hs" %}
@@ -547,6 +557,8 @@ Here's a video describing Gaussian elimination:
547557
[import, lang:"julia"](code/julia/gaussian_elimination.jl)
548558
{% sample lang="c" %}
549559
[import, lang:"c"](code/c/gaussian_elimination.c)
560+
{% sample lang="cpp" %}
561+
[import, lang:"cpp"](code/c++/gaussian_elimination.cpp)
550562
{% sample lang="rs" %}
551563
[import, lang:"rust"](code/rust/gaussian_elimination.rs)
552564
{% sample lang="hs" %}

contents/monte_carlo_integration/code/rust/monte_carlo.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ fn monte_carlo(n: i64) -> f64 {
2626
fn main() {
2727
let pi_estimate = monte_carlo(10000000);
2828

29-
println!("Percent error is {:.3}%", (100.0 * (pi_estimate - PI).abs() / PI));
29+
println!(
30+
"Percent error is {:.3}%",
31+
(100.0 * (pi_estimate - PI).abs() / PI)
32+
);
3033
}

contents/tree_traversal/code/rust/tree.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn dfs_stack(n: &Node) {
4747
}
4848
}
4949

50-
fn bfs_queue(n: &Node){
50+
fn bfs_queue(n: &Node) {
5151
let mut queue = VecDeque::new();
5252
queue.push_back(n);
5353

@@ -59,14 +59,20 @@ fn bfs_queue(n: &Node){
5959

6060
fn create_tree(num_row: u64, num_child: u64) -> Node {
6161
if num_row == 0 {
62-
return Node { children: vec![], value: 0 };
62+
return Node {
63+
children: vec![],
64+
value: 0,
65+
};
6366
}
6467

6568
let children = (0..num_child)
6669
.map(|_| create_tree(num_row - 1, num_child))
6770
.collect();
6871

69-
Node { children, value: num_row }
72+
Node {
73+
children,
74+
value: num_row,
75+
}
7076
}
7177

7278
fn main() {

contents/verlet_integration/code/rust/verlet.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ fn main() {
5050
let (time_vv, vel_vv) = velocity_verlet(5.0, -10.0, 0.01);
5151

5252
println!("Time for original Verlet integration: {}", time_v);
53-
println!("Time and velocity for Stormer Verlet integration: {}, {}", time_sv, vel_sv);
54-
println!("Time and velocity for velocity Verlet integration: {}, {}", time_vv, vel_vv);
53+
println!(
54+
"Time and velocity for Stormer Verlet integration: {}, {}",
55+
time_sv, vel_sv
56+
);
57+
println!(
58+
"Time and velocity for velocity Verlet integration: {}, {}",
59+
time_vv, vel_vv
60+
);
5561
}

0 commit comments

Comments
 (0)