-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathvector_cross_product.rs
98 lines (80 loc) · 3.13 KB
/
vector_cross_product.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/// Cross Product and Magnitude Calculation
///
/// This program defines functions to calculate the cross product of two 3D vectors
/// and the magnitude of a vector from its direction ratios. The main purpose is
/// to demonstrate the mathematical concepts and provide test cases for the functions.
///
/// Time Complexity:
/// - Calculating the cross product and magnitude of a vector each takes O(1) time
/// since we are working with fixed-size arrays and performing a fixed number of
/// mathematical operations.
/// Function to calculate the cross product of two vectors
pub fn cross_product(vec1: [f64; 3], vec2: [f64; 3]) -> [f64; 3] {
let x = vec1[1] * vec2[2] - vec1[2] * vec2[1];
let y = -(vec1[0] * vec2[2] - vec1[2] * vec2[0]);
let z = vec1[0] * vec2[1] - vec1[1] * vec2[0];
[x, y, z]
}
/// Function to calculate the magnitude of a vector
pub fn vector_magnitude(vec: [f64; 3]) -> f64 {
(vec[0].powi(2) + vec[1].powi(2) + vec[2].powi(2)).sqrt()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cross_product_and_magnitude_1() {
// Test case with non-trivial vectors
let vec1 = [1.0, 2.0, 3.0];
let vec2 = [4.0, 5.0, 6.0];
let cross_product = cross_product(vec1, vec2);
let magnitude = vector_magnitude(cross_product);
// Check the expected results with a tolerance for floating-point comparisons
assert_eq!(cross_product, [-3.0, 6.0, -3.0]);
assert!((magnitude - 7.34847).abs() < 1e-5);
}
#[test]
fn test_cross_product_and_magnitude_2() {
// Test case with orthogonal vectors
let vec1 = [1.0, 0.0, 0.0];
let vec2 = [0.0, 1.0, 0.0];
let cross_product = cross_product(vec1, vec2);
let magnitude = vector_magnitude(cross_product);
// Check the expected results
assert_eq!(cross_product, [0.0, 0.0, 1.0]);
assert_eq!(magnitude, 1.0);
}
#[test]
fn test_cross_product_and_magnitude_3() {
// Test case with vectors along the axes
let vec1 = [2.0, 0.0, 0.0];
let vec2 = [0.0, 3.0, 0.0];
let cross_product = cross_product(vec1, vec2);
let magnitude = vector_magnitude(cross_product);
// Check the expected results
assert_eq!(cross_product, [0.0, 0.0, 6.0]);
assert_eq!(magnitude, 6.0);
}
#[test]
fn test_cross_product_and_magnitude_4() {
// Test case with parallel vectors
let vec1 = [1.0, 2.0, 3.0];
let vec2 = [2.0, 4.0, 6.0];
let cross_product = cross_product(vec1, vec2);
let magnitude = vector_magnitude(cross_product);
// Check the expected results
assert_eq!(cross_product, [0.0, 0.0, 0.0]);
assert_eq!(magnitude, 0.0);
}
#[test]
fn test_cross_product_and_magnitude_5() {
// Test case with zero vectors
let vec1 = [0.0, 0.0, 0.0];
let vec2 = [0.0, 0.0, 0.0];
let cross_product = cross_product(vec1, vec2);
let magnitude = vector_magnitude(cross_product);
// Check the expected results
assert_eq!(cross_product, [0.0, 0.0, 0.0]);
assert_eq!(magnitude, 0.0);
}
}