nalgebra/src/debug/random_sdp.rs

58 lines
1.6 KiB
Rust
Raw Normal View History

#[cfg(feature = "arbitrary")]
2019-03-23 21:29:07 +08:00
use crate::base::storage::Owned;
#[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen};
2019-03-25 18:19:36 +08:00
use alga::general::ComplexField;
2019-03-23 21:29:07 +08:00
use crate::base::Scalar;
use crate::base::allocator::Allocator;
use crate::base::dimension::{Dim, Dynamic};
use crate::base::{DefaultAllocator, MatrixN};
2019-03-23 21:29:07 +08:00
use crate::debug::RandomOrthogonal;
/// A random, well-conditioned, symmetric definite-positive matrix.
#[derive(Clone, Debug)]
pub struct RandomSDP<N: Scalar, D: Dim = Dynamic>
2018-10-22 13:00:10 +08:00
where DefaultAllocator: Allocator<N, D, D>
2018-02-02 19:26:35 +08:00
{
m: MatrixN<N, D>,
}
2019-03-25 18:19:36 +08:00
impl<N: ComplexField, D: Dim> RandomSDP<N, D>
2018-10-22 13:00:10 +08:00
where DefaultAllocator: Allocator<N, D, D>
2018-02-02 19:26:35 +08:00
{
/// Retrieve the generated matrix.
pub fn unwrap(self) -> MatrixN<N, D> {
self.m
}
/// Creates a new well conditioned symmetric definite-positive matrix from its dimension and a
/// random reals generators.
pub fn new<Rand: FnMut() -> N>(dim: D, mut rand: Rand) -> Self {
let mut m = RandomOrthogonal::new(dim, || rand()).unwrap();
let mt = m.adjoint();
2018-02-02 19:26:35 +08:00
for i in 0..dim.value() {
let mut col = m.column_mut(i);
let eigenval = N::one() + N::from_real(rand().modulus());
col *= eigenval;
}
RandomSDP { m: m * mt }
}
}
#[cfg(feature = "arbitrary")]
2019-03-25 18:19:36 +08:00
impl<N: ComplexField + Arbitrary + Send, D: Dim> Arbitrary for RandomSDP<N, D>
2018-02-02 19:26:35 +08:00
where
DefaultAllocator: Allocator<N, D, D>,
Owned<N, D, D>: Clone + Send,
{
fn arbitrary<G: Gen>(g: &mut G) -> Self {
use rand::Rng;
let dim = D::try_to_usize().unwrap_or(g.gen_range(1, 50));
Self::new(D::from_usize(dim), || N::arbitrary(g))
}
}