Skip to content

Commit 657eb6e

Browse files
Merge pull request #173 from ThibFrgsGmz/feat/idiomatic_tilde_matrix
(feat) make `tilde_matrix` (skew-symmetric matrix) more idiomatic
2 parents d1040d2 + 949cf0d commit 657eb6e

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/utils.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,41 @@ use crate::linalg::{
2222
};
2323
use nalgebra::Complex;
2424

25-
/// Returns the tilde matrix from the provided Vector3.
25+
/// Returns the skew-symmetric matrix (also known as the tilde matrix)
26+
/// corresponding to the provided 3D vector.
27+
///
28+
/// The skew-symmetric matrix of a vector `v` is defined as:
29+
///
30+
/// ```plaintext
31+
/// 0 -v.z v.y
32+
/// v.z 0 -v.x
33+
/// -v.y v.x 0
34+
/// ```
35+
///
36+
/// This matrix has the property that for any vector `w`, the cross product `v x w`
37+
/// can be computed as the matrix product of the skew-symmetric matrix of `v` and `w`.
2638
pub fn tilde_matrix(v: &Vector3<f64>) -> Matrix3<f64> {
27-
Matrix3::new(
28-
0.0,
29-
-v[(2, 0)],
30-
v[(1, 0)],
31-
v[(2, 0)],
32-
0.0,
33-
-v[(0, 0)],
34-
-v[(1, 0)],
35-
v[(0, 0)],
36-
0.0,
37-
)
39+
Matrix3::new(0.0, -v.z, v.y, v.z, 0.0, -v.x, -v.y, v.x, 0.0)
40+
}
41+
42+
#[test]
43+
fn test_tilde_matrix() {
44+
let vec = Vector3::new(1.0, 2.0, 3.0);
45+
let rslt = Matrix3::new(0.0, -3.0, 2.0, 3.0, 0.0, -1.0, -2.0, 1.0, 0.0);
46+
assert_eq!(tilde_matrix(&vec), rslt);
47+
48+
let v = Vector3::new(1.0, 2.0, 3.0);
49+
let m = tilde_matrix(&v);
50+
51+
assert_eq!(m[(0, 0)], 0.0);
52+
assert_eq!(m[(0, 1)], -v.z);
53+
assert_eq!(m[(0, 2)], v.y);
54+
assert_eq!(m[(1, 0)], v.z);
55+
assert_eq!(m[(1, 1)], 0.0);
56+
assert_eq!(m[(1, 2)], -v.x);
57+
assert_eq!(m[(2, 0)], -v.y);
58+
assert_eq!(m[(2, 1)], v.x);
59+
assert_eq!(m[(2, 2)], 0.0);
3860
}
3961

4062
/// Checks if the provided 3x3 matrix is diagonal.
@@ -688,13 +710,6 @@ pub fn spherical_to_cartesian(range_ρ: f64, θ: f64, φ: f64) -> Vector3<f64> {
688710
}
689711
}
690712

691-
#[test]
692-
fn test_tilde_matrix() {
693-
let vec = Vector3::new(1.0, 2.0, 3.0);
694-
let rslt = Matrix3::new(0.0, -3.0, 2.0, 3.0, 0.0, -1.0, -2.0, 1.0, 0.0);
695-
assert_eq!(tilde_matrix(&vec), rslt);
696-
}
697-
698713
#[rustfmt::skip]
699714
#[test]
700715
fn test_diagonality() {

0 commit comments

Comments
 (0)