Skip to content

Commit

Permalink
Add Euclidean Distance (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrixninja authored Nov 13, 2023
1 parent 0c2f4d9 commit ed80173
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
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

0 comments on commit ed80173

Please sign in to comment.