Struct InversionPopulation

Source
pub struct InversionPopulation<T> {
    pub population: Vec<Inversion<T>>,
    pub size: usize,
}
Expand description

Population of Inversion objects. Includes initilializers and transformation tools.

Fields§

§population: Vec<Inversion<T>>§size: usize

Implementations§

Source§

impl<T> InversionPopulation<T>

Source

pub fn from_vec(vec: &Vec<Vec<T>>) -> Result<InversionPopulation<T>, Error>

Creates an InversionPopulationbased on a given matrix.

§Example
use permu_rs::inversion::InversionPopulation;
let pop: Vec<Vec<u16>> = vec![vec![0,2,0,0], vec![1,2,0,0], vec![0,0,0,0]];
let pop = InversionPopulation::from_vec(&pop).unwrap();

println!("{}", pop);

// Now, the seond vector contais one item less 
let pop: Vec<Vec<u16>> = vec![vec![0,2,0,0], vec![1,0,0], vec![0,0,0,0]];
let pop = InversionPopulation::from_vec(&pop); // This should return a LengthError
assert!(pop.is_err());
Source

pub fn zeros(size: usize, length: usize) -> InversionPopulation<T>

Creates a InversionPopulation of the size given with Inversions of length specified, filled with 0s. This population represents a population of identity permutations.

§Example
use permu_rs::*;
use permutation::{Permutation, PermuPopulation};
use inversion::{Inversion, InversionPopulation};

let (size, length) = (20,10);
let identity = PermuPopulation::from_vec(vec![Permutation::<u8>::identity(length);size]);
let inversions = InversionPopulation::<u8>::zeros(size,length-1);
let mut permus = PermuPopulation::<u8>::zeros(size, length);

inversions.to_permus(&mut permus);
assert_eq!(identity, permus);

println!("Zeros or identity population\n{}", inversions);
Source

pub fn to_permus(&self, permu_pop: &mut PermuPopulation<T>) -> Result<(), Error>

Transforms the Inversion to its Permutation representation. Fills a given PermuPopulation based on the Inversions from the InversionPopulation. The Inversion -> Permutation transformation is done respecting the positions in the population.

§Errors

Returns a LengthError if the size of both Populations are not equal.

§Panics

The mothod will panic if a Inversion of the InversionPopulation has not a Permutation representation.

§Example
use permu_rs::*;
let (size, length) = (5, 10);

let mut out_pop = permutation::PermuPopulation::<u8>::zeros(size, length); // Output permutation

let identity_pop = permutation::PermuPopulation::<u8>::identity(size, length);
let inversions = inversion::InversionPopulation::<u8>:: zeros(size, length-1);

inversions.to_permus(&mut out_pop);

assert_eq!(out_pop, identity_pop);

println!("{}\n", inversions);
println!("{}", out_pop);
Source

pub fn from_permus( permu_pop: &PermuPopulation<T>, inversions: &mut InversionPopulation<T>, ) -> Result<(), Error>

Fills a given InversionPopulation with the inversion representations from a PermuPopulation. The transformation is done respecting the positions inside the PermuPopulation.

§Errors

Returns a LengthError if the size of both populations are not equal or if the length of the permutations is not equal to the length of the inversion vectors + 1.

§Panics

The function panics if the internal Inversion::from_permu returns an Error. This will happen if a type conversion error occurs.

§Example
use permu_rs::permutation::{Permutation, PermuPopulation};
use permu_rs::inversion::{Inversion, InversionPopulation};

let (size, length) = (5, 4);

let mut population = vec![Inversion::<u16>::from_vec(vec![1,0,0]); size];
let mut inversions = InversionPopulation{ population, size };

let inversion_ok = InversionPopulation::<u16>::zeros(size, length-1); // Correct result
let permus = PermuPopulation::<u16>::identity(size, length);

InversionPopulation::from_permus(&permus, &mut inversions);
assert_eq!(inversion_ok, inversions);

println!("{}\n", permus);
println!("{}", inversions);

Trait Implementations§

Source§

impl<T: Clone> Clone for InversionPopulation<T>

Source§

fn clone(&self) -> InversionPopulation<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for InversionPopulation<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for InversionPopulation<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq> PartialEq for InversionPopulation<T>

Source§

fn eq(&self, other: &InversionPopulation<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Population<T> for InversionPopulation<T>

Source§

fn learn(&self) -> Distribution

Implementation of learn method for InversionPopulation.

§Example
use permu_rs::{Population, Distribution};
use permu_rs::inversion::{InversionPopulation, Inversion};
use InversionPopulation as invpop;
use Inversion as inv;
 
let pop: Vec<Vec<u8>> = vec![vec![2,1,0], vec![1,0,0], vec![0,0,0]];
let pop = InversionPopulation::from_vec(&pop).unwrap();

let target = vec![vec![1,1,1,0],vec![2,1,0,0],vec![3,0,0,0]];
let target = Distribution::InversionDistribution(target, false);

let distr = pop.learn();

assert_eq!(target, distr);
Source§

fn sample(&mut self, distr: &mut Distribution) -> Result<(), Error>

Implementation of sample method for PermuPopulation.

§Errors

Returns a LengthError if the length of the output population’s Inversion’s length is not equal to its population Inversions’s. Returns an IncorrectDistrType error if the given distribution is not InversionPopulation.

§Example
use permu_rs::{Population, Distribution};
use permu_rs::inversion::InversionPopulation;
 
// Initialize a custom distribution
let distr = vec![vec![1,1,1,0],vec![2,1,0,0],vec![3,0,0,0]];
let mut distr = Distribution::InversionDistribution(distr, false);
println!("Original distr:\n{}", distr);
// Init output population
let mut out = InversionPopulation::<u8>::zeros(10, 3); 
// Sample distribution
out.sample(&mut distr).unwrap();

// Now the original distribution has been changed in order to soften it
println!("Now distr:\n{}", distr);
println!("Out:\n{}", out); // Sampled population
Source§

fn to_permus(&self, permus: &mut PermuPopulation<T>) -> Result<(), Error>

Fills the given PermuPopulation with the permutation vector representation of the current population .
Source§

fn from_permus(&mut self, permus: &PermuPopulation<T>) -> Result<(), Error>

Maps a given PermuPopulation into the current Population’s representation.
Source§

impl<T> StructuralPartialEq for InversionPopulation<T>

Auto Trait Implementations§

§

impl<T> Freeze for InversionPopulation<T>

§

impl<T> RefUnwindSafe for InversionPopulation<T>
where T: RefUnwindSafe,

§

impl<T> Send for InversionPopulation<T>
where T: Send,

§

impl<T> Sync for InversionPopulation<T>
where T: Sync,

§

impl<T> Unpin for InversionPopulation<T>
where T: Unpin,

§

impl<T> UnwindSafe for InversionPopulation<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.