WeightedSum

pub struct WeightedSum<T>
where T: Atomic,
{ weight_map: BTreeMap<T, Fraction>, }
Expand description

Weighted sum of Atomic values

A weighted sum is an expression of the form c_1 * v_1 + ... + c_n * v_n where v_1, ..., v_n are atomic values and c_1, ..., c_n are real valued coefficients to these variables.

Fields§

§weight_map: BTreeMap<T, Fraction>

Map of atomic values and their weights

Implementations§

Source§

impl<T: Atomic> WeightedSum<T>

Source

pub fn is_zero(&self) -> bool

Check if the weighted sum is empty / equal to 0

Source

pub fn is_integer_form(&self) -> bool

Check whether all coefficients are already integers

This checks whether all coefficients in the weighted sum are integers, i.e., they can be converted to integer values without a loss in precision.

§Example
use taco_threshold_automaton::{
    expressions::{Variable, fraction::Fraction},
    lia_threshold_automaton::integer_thresholds::WeightedSum
};

let ws : WeightedSum<Variable> = WeightedSum::new([
    (Variable::new("var2"), 1),
]);
assert!(ws.is_integer_form());

let ws : WeightedSum<Variable> = WeightedSum::new([
    (Variable::new("var2"), Fraction::new(1, 2, false)),
]);
assert!(!ws.is_integer_form());
Source

pub fn new<F, I, V>(weight_map: I) -> Self
where F: Into<Fraction>, V: Into<T>, I: IntoIterator<Item = (V, F)>,

Create a new WeightedSum

Creates a new WeightedSum, filtering all components with a factor of 0

Source

pub fn new_empty() -> Self

Create a new empty WeightedSum

Source

pub fn get_factor(&self, v: &T) -> Option<&Fraction>

Get the factor for v if it exists

§Example
use taco_threshold_automaton::{
    expressions::{Variable, fraction::Fraction},
    lia_threshold_automaton::integer_thresholds::WeightedSum
};

let ws : WeightedSum<Variable> = WeightedSum::new([
    (Variable::new("var1"), 1),
]);
assert_eq!(ws.get_factor(&Variable::new("var1")), Some(&Fraction::from(1)));
Source

pub fn get_lcm_of_denominators(&self) -> u32

Get the least common multiple across all denominators of the coefficients

The least common multiple is computed across all denominators of the coefficients in the WeightedSum.

This can be useful for scaling such that all coefficients are integers.

§Example
use taco_threshold_automaton::{
    expressions::{Variable, fraction::Fraction},
    lia_threshold_automaton::integer_thresholds::WeightedSum
};

let ws : WeightedSum<Variable> = WeightedSum::new([
    (Variable::new("var1"), 1),
]);
assert_eq!(ws.get_lcm_of_denominators(), 1);

let ws : WeightedSum<Variable> = WeightedSum::new([
    (Variable::new("var1"), Fraction::from(1)),
    (Variable::new("var2"), Fraction::new(1, 2, false)),
]);
assert_eq!(ws.get_lcm_of_denominators(), 2);
Source

pub fn scale(&mut self, factor: Fraction)

Scale the weighted sum by a factor

§Example
use taco_threshold_automaton::{
    expressions::Variable,
    lia_threshold_automaton::integer_thresholds::WeightedSum
};

let mut ws : WeightedSum<Variable> = WeightedSum::new([
    (Variable::new("var2"), 1),
    (Variable::new("var2"), 3),
]);
let scaled_ws = WeightedSum::new([
    (Variable::new("var2"), 3),
    (Variable::new("var2"), 9),
]);

ws.scale(3.into());

assert_eq!(ws, scaled_ws);
Source

pub fn contains(&self, t: &T) -> bool

Check whether t is part of the WeightedSum (and the factor is not 0)

§Example
use taco_threshold_automaton::{
    expressions::Variable,
    lia_threshold_automaton::integer_thresholds::WeightedSum
};

let ws = WeightedSum::new([
    (Variable::new("var"), 1),
    (Variable::new("zvar"), 0),
]);

assert!(ws.contains(&Variable::new("var")));
assert!(!ws.contains(&Variable::new("unknown")));
assert!(!ws.contains(&Variable::new("zvar")));
Source

fn get_integer_expression<S>(&self) -> IntegerExpression<S>
where T: Into<IntegerExpression<S>>, S: Atomic,

Encode the weighted sum into an IntegerExpression

This method will return the WeightedSum encoded as an IntegerExpression. In case the weighted sum contains a fraction that is not in an integer form this function will encode the fraction without panicking or returning an error

Source

pub fn get_atoms_appearing(&self) -> impl Iterator<Item = &T>

Returns an iterator over all atoms in the weighted sum

Trait Implementations§

Source§

impl<T> Clone for WeightedSum<T>
where T: Atomic + Clone,

Source§

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

Returns a duplicate 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 for WeightedSum<T>
where T: Atomic + Debug,

Source§

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

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

impl<T: Atomic> Display for WeightedSum<T>

Source§

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

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

impl<T, F, I> From<I> for WeightedSum<T>
where T: Atomic, F: Into<Fraction> + Clone, I: IntoIterator<Item = (T, F)>,

Source§

fn from(value: I) -> Self

Converts to this type from the input type.
Source§

impl<T> Hash for WeightedSum<T>
where T: Atomic + Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Atomic> IntoIterator for &'a WeightedSum<T>

Source§

type Item = (&'a T, &'a Fraction)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, Fraction>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, S> IntoNoDivBooleanExpr<S> for WeightedSum<T>

Source§

fn get_scaled_integer_expression( &self, scaling_factor: u32, ) -> IntegerExpression<S>

Encode the object into an IntegerExpression without divisions appearing Read more
Source§

fn get_lcm_of_denominators(&self) -> u32

Get the lcm across all denominators in the object Read more
Source§

fn encode_comparison_to_boolean_expression<Q>( &self, comparison_op: ComparisonOp, other: &Q, ) -> BooleanExpression<T>

Encode the comparison of two expressions into a boolean expression with a that does not contain any real numbers. Read more
Source§

impl<T> Ord for WeightedSum<T>
where T: Atomic + Ord,

Source§

fn cmp(&self, other: &WeightedSum<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for WeightedSum<T>
where T: Atomic + PartialEq,

Source§

fn eq(&self, other: &WeightedSum<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> PartialOrd for WeightedSum<T>
where T: Atomic + PartialOrd,

Source§

fn partial_cmp(&self, other: &WeightedSum<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Eq for WeightedSum<T>
where T: Atomic + Eq,

Source§

impl<T> StructuralPartialEq for WeightedSum<T>
where T: Atomic,

Auto Trait Implementations§

§

impl<T> Freeze for WeightedSum<T>

§

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

§

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

§

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

§

impl<T> Unpin for WeightedSum<T>

§

impl<T> UnwindSafe for WeightedSum<T>
where T: RefUnwindSafe,

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.