Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Euclidean Distance #610

Merged
merged 10 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
* Compression
* [Run Length Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/compression/run_length_encoding.rs)
* Conversions
* [Binary to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs)
* [Binary to Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs)
* [Decimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
* [Hexadecimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
* [Octal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
* [Octal to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
* [Binary To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs)
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs)
* [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
* [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
* Data Structures
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
Expand Down Expand Up @@ -162,6 +162,7 @@
* [Cross Entropy Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/math/cross_entropy_loss.rs)
* [Doomsday](https://github.com/TheAlgorithms/Rust/blob/master/src/math/doomsday.rs)
* [Elliptic Curve](https://github.com/TheAlgorithms/Rust/blob/master/src/math/elliptic_curve.rs)
* [Euclidean Distance](https://github.com/TheAlgorithms/Rust/blob/master/src/math/euclidean_distance.rs)
* [Exponential Linear Unit](https://github.com/TheAlgorithms/Rust/blob/master/src/math/exponential_linear_unit.rs)
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/math/extended_euclidean_algorithm.rs)
* [Factors](https://github.com/TheAlgorithms/Rust/blob/master/src/math/factors.rs)
Expand Down
41 changes: 41 additions & 0 deletions src/math/euclidean_distance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Author : cyrixninja
// Calculate the Euclidean distance between two vectors
// Wikipedia : https://en.wikipedia.org/wiki/Euclidean_distance

pub fn euclidean_distance(vector_1: &Vector, vector_2: &Vector) -> f64 {
// Calculate the Euclidean distance using the provided vectors.
let squared_sum: f64 = vector_1
.iter()
.zip(vector_2.iter())
.map(|(&a, &b)| (a - b).powi(2))
.sum();

squared_sum.sqrt()
}

type Vector = Vec<f64>;

#[cfg(test)]
mod tests {
use super::*;

// Define a test function for the euclidean_distance function.
#[test]
fn test_euclidean_distance() {
// First test case: 2D vectors
let vec1_2d = vec![1.0, 2.0];
let vec2_2d = vec![4.0, 6.0];

// Calculate the Euclidean distance
let result_2d = euclidean_distance(&vec1_2d, &vec2_2d);
assert_eq!(result_2d, 5.0);

// Second test case: 4D vectors
let vec1_4d = vec![1.0, 2.0, 3.0, 4.0];
let vec2_4d = vec![5.0, 6.0, 7.0, 8.0];

// Calculate the Euclidean distance
let result_4d = euclidean_distance(&vec1_4d, &vec2_4d);
assert_eq!(result_4d, 8.0);
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod collatz_sequence;
mod cross_entropy_loss;
mod doomsday;
mod elliptic_curve;
mod euclidean_distance;
mod exponential_linear_unit;
mod extended_euclidean_algorithm;
mod factors;
Expand Down Expand Up @@ -85,6 +86,7 @@ pub use self::collatz_sequence::sequence;
pub use self::cross_entropy_loss::cross_entropy_loss;
pub use self::doomsday::get_week_day;
pub use self::elliptic_curve::EllipticCurve;
pub use self::euclidean_distance::euclidean_distance;
pub use self::exponential_linear_unit::exponential_linear_unit;
pub use self::extended_euclidean_algorithm::extended_euclidean_algorithm;
pub use self::factors::factors;
Expand Down
Loading