2017-08-03 01:37:44 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::storage::Owned;
|
2017-08-03 01:37:44 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2018-05-19 23:15:15 +08:00
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::Allocator;
|
|
|
|
use crate::base::dimension::{Dim, Dynamic, U2};
|
2020-03-21 19:16:46 +08:00
|
|
|
use crate::base::Scalar;
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::{DefaultAllocator, MatrixN};
|
|
|
|
use crate::linalg::givens::GivensRotation;
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::ComplexField;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
/// A random orthogonal matrix.
|
|
|
|
#[derive(Clone, Debug)]
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct RandomOrthogonal<N: Scalar, D: Dim = Dynamic>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
|
|
|
m: MatrixN<N, D>,
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-25 18:19:36 +08:00
|
|
|
impl<N: ComplexField, D: Dim> RandomOrthogonal<N, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
/// Retrieve the generated matrix.
|
|
|
|
pub fn unwrap(self) -> MatrixN<N, D> {
|
|
|
|
self.m
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new random orthogonal matrix from its dimension and a random reals generators.
|
|
|
|
pub fn new<Rand: FnMut() -> N>(dim: D, mut rand: Rand) -> Self {
|
|
|
|
let mut res = MatrixN::identity_generic(dim, dim);
|
|
|
|
|
2019-03-23 18:46:56 +08:00
|
|
|
// Create an orthogonal matrix by composing random Givens rotations rotations.
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..dim.value() - 1 {
|
2019-03-23 18:46:56 +08:00
|
|
|
let rot = GivensRotation::new(rand(), rand()).0;
|
2017-08-03 01:37:44 +08:00
|
|
|
rot.rotate(&mut res.fixed_rows_mut::<U2>(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomOrthogonal { m: res }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "arbitrary")]
|
2019-03-25 18:19:36 +08:00
|
|
|
impl<N: ComplexField + Arbitrary + Send, D: Dim> Arbitrary for RandomOrthogonal<N, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
|
|
|
Owned<N, D, D>: Clone + Send,
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
2018-12-29 21:22:24 +08:00
|
|
|
use rand::Rng;
|
2017-08-03 01:37:44 +08:00
|
|
|
let dim = D::try_to_usize().unwrap_or(g.gen_range(1, 50));
|
|
|
|
Self::new(D::from_usize(dim), || N::arbitrary(g))
|
|
|
|
}
|
|
|
|
}
|