diff --git a/DIRECTORY.md b/DIRECTORY.md index fddaca51318..3368543e38a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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) diff --git a/src/math/euclidean_distance.rs b/src/math/euclidean_distance.rs new file mode 100644 index 00000000000..0be7459f042 --- /dev/null +++ b/src/math/euclidean_distance.rs @@ -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; + +#[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); + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 18326d7f2bb..6d8be370875 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -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; @@ -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;