1#[cfg(not(feature = "std"))]
4use simba::scalar::{ComplexField, RealField};
5
6macro_rules! impl_svd2 {
8 ($Svd2:ident, $Mat2:ty, $Vec2:ty, $Real:ty $(, #[$attr:meta])*) => {
9 #[doc = concat!("The SVD of a 2x2 matrix (", stringify!($Real), " precision).")]
10 #[derive(Clone, Copy, Debug, PartialEq)]
11 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12 $(#[$attr])*
13 pub struct $Svd2 {
14 pub u: $Mat2,
16 pub s: $Vec2,
18 pub vt: $Mat2,
20 }
21
22 impl $Svd2 {
23 #[inline]
25 pub fn new(u: $Mat2, s: $Vec2, vt: $Mat2) -> Self {
26 Self { u, s, vt }
27 }
28
29 #[inline]
38 pub fn from_matrix(m: $Mat2) -> Self {
39 let e = (m.col(0).x + m.col(1).y) * 0.5;
40 let f = (m.col(0).x - m.col(1).y) * 0.5;
41 let g = (m.col(0).y + m.col(1).x) * 0.5;
42 let h = (m.col(0).y - m.col(1).x) * 0.5;
43 let q = (e * e + h * h).sqrt();
44 let r = (f * f + g * g).sqrt();
45
46 let sx = q + r;
49 let sy = q - r;
50 let sy_sign: $Real = if sy < 0.0 { -1.0 } else { 1.0 };
51 let singular_values = <$Vec2>::new(sx, sy * sy_sign);
52
53 let a2 = h.atan2(e);
54 let a1 = if r < 1e-10 { a2 } else { g.atan2(f) };
57 let theta = (a2 - a1) * 0.5;
58 let phi = (a2 + a1) * 0.5;
59 let st = theta.sin();
60 let ct = theta.cos();
61 let sp = phi.sin();
62 let cp = phi.cos();
63
64 let u = <$Mat2>::from_cols(<$Vec2>::new(cp, sp), <$Vec2>::new(-sp, cp));
65 let vt = <$Mat2>::from_cols(
66 <$Vec2>::new(ct, st * sy_sign),
67 <$Vec2>::new(-st, ct * sy_sign),
68 );
69
70 Self {
71 u,
72 s: singular_values,
73 vt,
74 }
75 }
76
77 #[inline]
81 pub fn recompose(&self) -> $Mat2 {
82 let u_s = <$Mat2>::from_cols(self.u.col(0) * self.s.x, self.u.col(1) * self.s.y);
83 u_s * self.vt.transpose()
84 }
85
86 }
87 };
88}
89
90impl_svd2!(Svd2, glam::Mat2, glam::Vec2, f32);
91impl_svd2!(DSvd2, glam::DMat2, glam::DVec2, f64);
92
93impl From<Svd2> for DSvd2 {
95 #[inline]
96 fn from(svd: Svd2) -> Self {
97 Self {
98 u: svd.u.as_dmat2(),
99 s: svd.s.as_dvec2(),
100 vt: svd.vt.as_dmat2(),
101 }
102 }
103}
104
105impl From<DSvd2> for Svd2 {
106 #[inline]
107 fn from(svd: DSvd2) -> Self {
108 Self {
109 u: svd.u.as_mat2(),
110 s: svd.s.as_vec2(),
111 vt: svd.vt.as_mat2(),
112 }
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119 use approx::assert_relative_eq;
120
121 #[test]
122 fn svd_2x2_identity() {
123 let mat = glam::Mat2::IDENTITY;
124 let svd = Svd2::from_matrix(mat);
125
126 assert_relative_eq!(svd.s, glam::Vec2::new(1.0, 1.0), epsilon = 1e-6);
127 assert_relative_eq!(svd.recompose(), mat, epsilon = 1e-6);
128 }
129
130 #[test]
131 fn svd_2x2_diagonal() {
132 let mat = glam::Mat2::from_diagonal(glam::Vec2::new(3.0, 2.0));
133 let svd = Svd2::from_matrix(mat);
134
135 assert_relative_eq!(svd.s, glam::Vec2::new(3.0, 2.0), epsilon = 1e-6);
136 assert_relative_eq!(svd.recompose(), mat, epsilon = 1e-6);
137 }
138
139 #[test]
140 fn svd_2x2_general() {
141 let mat = glam::Mat2::from_cols_array_2d(&[[4.0, 3.0], [2.0, 1.0]]);
142 let svd = Svd2::from_matrix(mat);
143
144 assert_relative_eq!(svd.recompose(), mat, epsilon = 1e-5);
146
147 assert!(svd.s.x >= svd.s.y);
149 assert!(svd.s.y >= 0.0);
150
151 assert_relative_eq!(
153 svd.u * svd.u.transpose(),
154 glam::Mat2::IDENTITY,
155 epsilon = 1e-6
156 );
157 assert_relative_eq!(
158 svd.vt * svd.vt.transpose(),
159 glam::Mat2::IDENTITY,
160 epsilon = 1e-6
161 );
162 }
163
164 #[test]
165 fn svd_2x2_rotation() {
166 let angle = 0.5_f32;
167 let mat = glam::Mat2::from_angle(angle);
168 let svd = Svd2::from_matrix(mat);
169
170 assert_relative_eq!(svd.s, glam::Vec2::new(1.0, 1.0), epsilon = 1e-6);
172 assert_relative_eq!(svd.recompose(), mat, epsilon = 1e-6);
173 }
174
175 #[test]
176 fn svd_2x2_f64() {
177 let mat = glam::DMat2::from_cols_array_2d(&[[4.0, 3.0], [2.0, 1.0]]);
178 let svd = DSvd2::from_matrix(mat);
179
180 assert_relative_eq!(svd.recompose(), mat, epsilon = 1e-10);
181 }
182}