Debian Codegraph @

debian/missing-sources/nalgebra/src/debug/random_sdp.rs

raw
1 #[cfg(feature = "arbitrary")]
2 use crate::base::storage::Owned;
3 #[cfg(feature = "arbitrary")]
4 use quickcheck::{Arbitrary, Gen};
5
6 use crate::base::Scalar;
7 use crate::base::allocator::Allocator;
8 use crate::base::dimension::{Dim, Dyn};
9 use crate::base::{DefaultAllocator, OMatrix};
10 use simba::scalar::ComplexField;
11
12 use crate::debug::RandomOrthogonal;
13
14 /// A random, well-conditioned, symmetric definite-positive matrix.
15 #[derive(Clone, Debug)]
16 pub struct RandomSDP<T: Scalar, D: Dim = Dyn>
17 where
18 DefaultAllocator: Allocator<D, D>,
19 {
20 m: OMatrix<T, D, D>,
21 }
22
23 impl<T: ComplexField, D: Dim> RandomSDP<T, D>
24 where
25 DefaultAllocator: Allocator<D, D>,
26 {
27 /// Retrieve the generated matrix.
28 pub fn unwrap(self) -> OMatrix<T, D, D> {
29 self.m
30 }
31
32 /// Creates a new well conditioned symmetric definite-positive matrix from its dimension and a
33 /// random reals generators.
34 pub fn new<Rand: FnMut() -> T>(dim: D, mut rand: Rand) -> Self {
35 let mut m = RandomOrthogonal::new(dim, &mut rand).unwrap();
36 let mt = m.adjoint();
37
38 for i in 0..dim.value() {
39 let mut col = m.column_mut(i);
40 let eigenval = T::one() + T::from_real(rand().modulus());
41 col *= eigenval;
42 }
43
44 RandomSDP { m: m * mt }
45 }
46 }
47
48 #[cfg(feature = "arbitrary")]
49 impl<T: ComplexField + Arbitrary + Send, D: Dim> Arbitrary for RandomSDP<T, D>
50 where
51 DefaultAllocator: Allocator<D, D>,
52 Owned<T, D, D>: Clone + Send,
53 {
54 fn arbitrary(g: &mut Gen) -> Self {
55 let dim = D::try_to_usize().unwrap_or(1 + usize::arbitrary(g) % 50);
56 Self::new(D::from_usize(dim), || T::arbitrary(g))
57 }
58 }