Skip to main content

PopulationMatrix

Struct PopulationMatrix 

Source
pub struct PopulationMatrix { /* private fields */ }
Expand description

This struct represents the likelihood of different lifestages of an organism to survive, grow, and reproduce to the next lifestage (or stay in the same lifestage) by storing decimal (f64) values in a matrix (a vector of vectors, denoted Vec<Vec<f64>>). Each sub-vector (row) represents a lifestage, and each item (column) represents the yearly proportion of individuals recruited to that lifestage. The first row typically represents newborns, seedlings, etc.

§Examples

Index [1][1] is the number of lifestage 1 individuals that were in lifestage 1 the previous year.

Index [1][2] is the number of lifestage 1 indidviduals that were in lifestage 2 the year before.

Indeg [2][1] is the number of lifestage 2 individuals who were lifetage 1 individuals the previous year.

…etc…

An example matrix could look like this: [0][0][0.1][0.2] [0.6][0][0][0] [0][0.8][0][0] [0][0][0.8][0.94]

Implementations§

Source§

impl PopulationMatrix

Source

pub fn build(input: Vec<Vec<f64>>) -> Result<PopulationMatrix, &'static str>

This function builds a Population Matrix from a square vector of vectors (Vec<Vec>), ensuring that it contains a consistent number of lifestages across all inputted Lifestage Survival Vectors and in the number of inputted Lifestage Survival Vectors. If these conditions are not met, it will return an error message.

Source

pub fn get_lifestage_count(&self) -> u8

Returns the number of listages represented in the Population Matrix, useful for calculations requiring matching numbers of lifestages.

Source

pub fn get_matrix(&self) -> &Vec<Vec<f64>>

Returns the full matrix of matrices stored in the Population Matrix.

Source

pub fn project_vector( &self, vector: &PopulationVector, ) -> Result<PopulationVector, &'static str>

Given an input of a PopulationMatrix and a PopulationVector with the same number of items in their matrix and vector values respectively, the function will return a PopulationMatrix. ##Use This function takes a population matrix (formatted as a PopulationMatrix struct) and a population vector (formatted as a PopulationVector struct) as an imput, calculating the sum of the components of each column of the vector within the population matrix and muliplying it by the value of the population vector at the same index. This is essentially a column-wise matrix-vector product. It then returns a new PopulationVector.

This can be visually thought of as such:

[ 40] [0 , 0 , 0.1 ] [ 10] [ 20] x [0.6, 0.8, 0 ] = [ 40] [100] [0 , 0.8, 0.95] [111]

This calculation is common in Population Variability Analysis (PVA) wherein each row (LifeStageSurvivalVector) represents the probability of recruitment into that life stage over the course of a year. By multiplying a matrix of these probabilities by a vector containing the current population, a researcher can estimate the following year’s population.

§Errors

This function will return an Err(‘static str’) if the number of rows or items within rows in the matrix is not equal to the number of items in the population vector.

Although this is theoretically impossible, the program could also panic if it recieves an out-of-bounds index request for the population vector. However, the function checks for this earlier in order to return a useful error code and prevent other mistakes, so should never occur.

§Examples
use ecolysis_core::populations::population_level_simulation::{PopulationMatrix, PopulationVector}; // import relevant structs
let popvector = PopulationVector::new(vec![150.0, 200.0, 33.0]); // create a population vector type
let popmatrix = PopulationMatrix::build(vec![
vec![0.25, 0.001, 0.75],
vec![0.3, 0.4346, 0.002],
vec![0.98, 0.66, 0.161]
]).unwrap(); // create a Population Matrix type
let new_popvector = popmatrix.project_vector(&popvector); // project the vector by the matrix
println!("{:?}", new_popvector.unwrap().get_vector()); // print results

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V