pub struct ModuleString<R, T: Hash + Eq, A: ?Sized> { /* private fields */ }
Expand description
Creates free-arithmetic constructions based upon order-invariant addition of terms of type C
with
multiplication with scalar coefficients of type R
§Basic Construction
Given type parameters R
, T
, and A
, the construction goes as follows:
- Internally, each instance contains a map from instances of
T
to coefficients fromR
- Addition is then implemented by merging the arguments’ HashMaps where repeated terms have their coefficients added together
- Multiplication with
R
values is applied by each term’s coefficient with the given scalar - Multiplication between terms is performed by multiplying the coefficients and apply the rule
defined by type
A
- Then, full multiplication is defined by distributing over each pair of terms and summing
With this system, using different A
rules and T
/R
types, a number of variants can be defined:
FreeModule<R,T>
defines a vector-space overR
using the default addition andR
-multiplication without having a multiplication ruleFreeAlgebra<R,T>
is aFreeModule<R,T>
but where the multiplication rule between terms is built as with aFreeMonoid<T>
MonoidRing<R,T>
is aFreeModule<R,T>
but where the multiplication rule for terms simply uses the intrinsic multiplication ofT
§Other impls
In addition to the basic arithmetic operations, ModuleString implements a number of other notable traits depending on the type arguments:
- Sub, SubAssign, Neg, etc. These apply by negating each coefficient whenever
R
is negatable - Index gets coefficient references from a term reference
- MulAssociative and MulCommutative whenever
R
is andA
implements AssociativeMonoidRule or CommutativeMonoidRule - Distributive whenever
R
is - Extend, FromIterator, and Sum all are implemented using repeated addition.
§A note on IterMut
The method [ModuleString.iter_mut()] will give an valid iterator over mutable references to each
element of the object, but it is of note that because of the nature of some of the AlgebraRule
’s,
the method will reallocate internal storage and iterate through every element,
even if dropped.
The reason for this is that if it did not, it would be possible to modify a ModuleString into an invalid state. Hence, to mitigate this, the iterator must remultiply every element again as it iterates over the references.
Implementations§
Source§impl<T: Hash + Eq, R, A: ?Sized> ModuleString<R, T, A>
impl<T: Hash + Eq, R, A: ?Sized> ModuleString<R, T, A>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of terms in this module element
§Examples
use maths_traits::algebra::Zero;
use free_algebra::FreeModule;
let p = FreeModule::zero();
let q = &p + (3.5, 'x');
let r = &q + (2.0, 'y');
let s = &r + (2.0, 'x'); //adds to first term without increasing length
assert_eq!(p.len(), 0);
assert_eq!(q.len(), 1);
assert_eq!(r.len(), 2);
assert_eq!(s.len(), 2);
Sourcepub fn get_ref<Q: Hash + Eq>(&self, t: &Q) -> Option<&R>where
T: Borrow<Q>,
pub fn get_ref<Q: Hash + Eq>(&self, t: &Q) -> Option<&R>where
T: Borrow<Q>,
Retrieves a reference to the coefficient of a term, if it is non-zero
§Examples
use maths_traits::algebra::Zero;
use free_algebra::FreeModule;
let p = FreeModule::zero() + (3.5, 'x') + (2.0, 'y') + (1.0, 'z');
assert_eq!(p.get_ref(&'x'), Some(&3.5));
assert_eq!(p.get_ref(&'y'), Some(&2.0));
assert_eq!(p.get_ref(&'z'), Some(&1.0));
assert_eq!(p.get_ref(&'w'), None);
Sourcepub fn get<Q: Hash + Eq>(&self, t: &Q) -> R
pub fn get<Q: Hash + Eq>(&self, t: &Q) -> R
Clones a the coefficient of a term or returns zero if it doesn’t exist
§Examples
use maths_traits::algebra::Zero;
use free_algebra::FreeModule;
let p = FreeModule::zero() + (3.5, 'x') + (2.0, 'y') + (1.0, 'z');
assert_eq!(p.get(&'x'), 3.5);
assert_eq!(p.get(&'y'), 2.0);
assert_eq!(p.get(&'z'), 1.0);
assert_eq!(p.get(&'w'), 0.0);
Sourcepub fn iter<'a>(&'a self) -> Iter<'a, R, T>
pub fn iter<'a>(&'a self) -> Iter<'a, R, T>
Produces an iterator over references to the terms and references in this element
Sourcepub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, R, T, A> ⓘwhere
R: AddAssign,
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, R, T, A> ⓘwhere
R: AddAssign,
Produces an iterator over mutable references to the terms and references in this element
§Examples
use maths_traits::algebra::Zero;
use free_algebra::FreeModule;
let mut p = FreeModule::zero() + (3.5, 'x') + (2.0, 'y') + (1.0, 'z');
for (_, t) in p.iter_mut() {
*t = 'a';
}
assert_eq!(p.get(&'x'), 0.0);
assert_eq!(p.get(&'y'), 0.0);
assert_eq!(p.get(&'z'), 0.0);
assert_eq!(p.get(&'a'), 6.5);
Sourcepub fn commutator(self, rhs: Self) -> Self
pub fn commutator(self, rhs: Self) -> Self
Computes the algebraic commutator [a,b] = a*b - b*a
§Examples
use maths_traits::algebra::One;
use free_algebra::{FreeAlgebra, FreeMonoid};
let one = FreeMonoid::<char>::one();
let x:FreeMonoid<_> = 'x'.into();
let y:FreeMonoid<_> = 'y'.into();
let p:FreeAlgebra<f32,_> = FreeAlgebra::one() + &x;
let q:FreeAlgebra<f32,_> = FreeAlgebra::one() + &y;
let commutator = p.commutator(q);
assert_eq!(commutator.get(&one), 0.0);
assert_eq!(commutator.get(&x), 0.0);
assert_eq!(commutator.get(&y), 0.0);
assert_eq!(commutator.get(&(&x*&y)), 1.0);
assert_eq!(commutator.get(&(&y*&x)), -1.0);
A property of significance for this product is that when the arguments commute, the
output is always 0
.
use maths_traits::algebra::{One, Zero};
use free_algebra::{FreeAlgebra, FreeMonoid};
let p = FreeAlgebra::<f32,_>::one() + FreeMonoid::from('x');
let commutator = p.clone().commutator(p);
assert!(commutator.is_zero());
Trait Implementations§
Source§impl<'a, RHS, T, R, A> Add<RHS> for &'a ModuleString<R, T, A>
impl<'a, RHS, T, R, A> Add<RHS> for &'a ModuleString<R, T, A>
Source§impl<RHS, T, R, A> Add<RHS> for ModuleString<R, T, A>
impl<RHS, T, R, A> Add<RHS> for ModuleString<R, T, A>
Source§impl<'a, T, R, A> AddAssign<&'a ModuleString<R, T, A>> for ModuleString<R, T, A>
impl<'a, T, R, A> AddAssign<&'a ModuleString<R, T, A>> for ModuleString<R, T, A>
Source§fn add_assign(&mut self, rhs: &'a Self)
fn add_assign(&mut self, rhs: &'a Self)
+=
operation. Read moreSource§impl<'a, T, R, A> AddAssign<&'a T> for ModuleString<R, T, A>
impl<'a, T, R, A> AddAssign<&'a T> for ModuleString<R, T, A>
Source§fn add_assign(&mut self, rhs: &'a T)
fn add_assign(&mut self, rhs: &'a T)
+=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign, A: ?Sized> AddAssign<(R, T)> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> AddAssign<(R, T)> for ModuleString<R, T, A>
Source§fn add_assign(&mut self, rhs: (R, T))
fn add_assign(&mut self, rhs: (R, T))
+=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign + One, A: ?Sized> AddAssign<T> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign + One, A: ?Sized> AddAssign<T> for ModuleString<R, T, A>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign, A: ?Sized> AddAssign for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> AddAssign for ModuleString<R, T, A>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl<R, T, A: ?Sized> Clone for ModuleString<R, T, A>
impl<R, T, A: ?Sized> Clone for ModuleString<R, T, A>
Source§impl<R, T, A: ?Sized> Debug for ModuleString<R, T, A>
impl<R, T, A: ?Sized> Debug for ModuleString<R, T, A>
Source§impl<R: Display, T: Hash + Eq + Display, A: ?Sized> Display for ModuleString<R, T, A>
Formats the ModuleString as a sequence of factors separated by +
’s
impl<R: Display, T: Hash + Eq + Display, A: ?Sized> Display for ModuleString<R, T, A>
Formats the ModuleString as a sequence of factors separated by +
’s
§Examples
use maths_traits::algebra::{One, Zero};
use free_algebra::{FreeAlgebra, FreeMonoid};
let one = FreeMonoid::one();
let x:FreeMonoid<_> = 'x'.into();
let y:FreeMonoid<_> = 'y'.into();
let mut p = FreeAlgebra::zero();
assert_eq!(format!("{}", p), "0");
p += (1.0, one.clone());
assert_eq!(format!("{}", p), "1");
p += (2.5, one);
assert_eq!(format!("{}", p), "3.5");
p += x;
assert!(format!("{}", p) == "(3.5 + x)" || format!("{}", p) == "(x + 3.5)");
p *= (1.0, y);
assert!(format!("{}", p) == "(3.5*y + x*y)" || format!("{}", p) == "(x*y + 3.5*y)");
assert!(format!("{:#}", p) == "(3.5y + xy)" || format!("{:#}", p) == "(xy + 3.5y)");
Source§impl<'a, RHS, T, R, A> Div<RHS> for &'a ModuleString<R, T, A>
impl<'a, RHS, T, R, A> Div<RHS> for &'a ModuleString<R, T, A>
Source§impl<RHS, T, R, A> Div<RHS> for ModuleString<R, T, A>
impl<RHS, T, R, A> Div<RHS> for ModuleString<R, T, A>
Source§impl<'a, T, R, A> DivAssign<&'a R> for ModuleString<R, T, A>
impl<'a, T, R, A> DivAssign<&'a R> for ModuleString<R, T, A>
Source§fn div_assign(&mut self, rhs: &'a R)
fn div_assign(&mut self, rhs: &'a R)
/=
operation. Read moreSource§impl<T: Hash + Eq, R: DivAssign + Clone, A: ?Sized> DivAssign<R> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: DivAssign + Clone, A: ?Sized> DivAssign<R> for ModuleString<R, T, A>
Source§fn div_assign(&mut self, rhs: R)
fn div_assign(&mut self, rhs: R)
/=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign, A: ?Sized> Extend<(R, T)> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> Extend<(R, T)> for ModuleString<R, T, A>
Source§fn extend<I: IntoIterator<Item = (R, T)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (R, T)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T: Hash + Eq, R: AddAssign, A: ?Sized> FromIterator<(R, T)> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> FromIterator<(R, T)> for ModuleString<R, T, A>
Source§impl<T: Hash + Eq, R: AddAssign, A: ?Sized> FromIterator<ModuleString<R, T, A>> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> FromIterator<ModuleString<R, T, A>> for ModuleString<R, T, A>
Source§fn from_iter<I: IntoIterator<Item = Self>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = Self>>(iter: I) -> Self
Source§impl<T: Hash + Eq, R: AddAssign + One, A: ?Sized> FromIterator<T> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign + One, A: ?Sized> FromIterator<T> for ModuleString<R, T, A>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<T: Hash + Eq, R, A: ?Sized> IntoIterator for ModuleString<R, T, A>
impl<T: Hash + Eq, R, A: ?Sized> IntoIterator for ModuleString<R, T, A>
Source§impl<'a, RHS, T, R, A> Mul<RHS> for &'a ModuleString<R, T, A>
impl<'a, RHS, T, R, A> Mul<RHS> for &'a ModuleString<R, T, A>
Source§impl<RHS, T, R, A> Mul<RHS> for ModuleString<R, T, A>
impl<RHS, T, R, A> Mul<RHS> for ModuleString<R, T, A>
Source§impl<'a, T, R, A> MulAssign<&'a R> for ModuleString<R, T, A>
impl<'a, T, R, A> MulAssign<&'a R> for ModuleString<R, T, A>
Source§fn mul_assign(&mut self, rhs: &'a R)
fn mul_assign(&mut self, rhs: &'a R)
*=
operation. Read moreSource§impl<T: Hash + Eq + Clone, R: MulMagma + AddMagma, A: ?Sized + AlgebraRule<R, T>> MulAssign<(R, T)> for ModuleString<R, T, A>
impl<T: Hash + Eq + Clone, R: MulMagma + AddMagma, A: ?Sized + AlgebraRule<R, T>> MulAssign<(R, T)> for ModuleString<R, T, A>
Source§fn mul_assign(&mut self, (r1, t1): (R, T))
fn mul_assign(&mut self, (r1, t1): (R, T))
*=
operation. Read moreSource§impl<T: Hash + Eq, R: MulAssign + Clone, A: ?Sized> MulAssign<R> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: MulAssign + Clone, A: ?Sized> MulAssign<R> for ModuleString<R, T, A>
Source§fn mul_assign(&mut self, rhs: R)
fn mul_assign(&mut self, rhs: R)
*=
operation. Read moreSource§impl<T: Hash + Eq + Clone, R: Semiring, A: ?Sized + AlgebraRule<R, T>> MulAssign for ModuleString<R, T, A>
impl<T: Hash + Eq + Clone, R: Semiring, A: ?Sized + AlgebraRule<R, T>> MulAssign for ModuleString<R, T, A>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moreSource§impl<'a, T: Hash + Eq, R: Neg, A: ?Sized> Neg for &'a ModuleString<R, T, A>where
ModuleString<R, T, A>: Clone,
impl<'a, T: Hash + Eq, R: Neg, A: ?Sized> Neg for &'a ModuleString<R, T, A>where
ModuleString<R, T, A>: Clone,
Source§impl<T: Clone + Hash + Eq, R: PartialEq + UnitalSemiring, A: UnitalAlgebraRule<R, T> + ?Sized> One for ModuleString<R, T, A>
impl<T: Clone + Hash + Eq, R: PartialEq + UnitalSemiring, A: UnitalAlgebraRule<R, T> + ?Sized> One for ModuleString<R, T, A>
Source§impl<R, T, A: ?Sized> PartialEq for ModuleString<R, T, A>
impl<R, T, A: ?Sized> PartialEq for ModuleString<R, T, A>
Source§impl<Z, R, T, A> Pow<Z> for ModuleString<R, T, A>where
Z: Natural,
T: Hash + Eq + Clone,
R: PartialEq + UnitalSemiring,
A: ?Sized + UnitalAlgebraRule<R, T> + AssociativeAlgebraRule<R, T>,
impl<Z, R, T, A> Pow<Z> for ModuleString<R, T, A>where
Z: Natural,
T: Hash + Eq + Clone,
R: PartialEq + UnitalSemiring,
A: ?Sized + UnitalAlgebraRule<R, T> + AssociativeAlgebraRule<R, T>,
Source§impl<'a, RHS, T, R, A> Sub<RHS> for &'a ModuleString<R, T, A>
impl<'a, RHS, T, R, A> Sub<RHS> for &'a ModuleString<R, T, A>
Source§impl<RHS, T, R, A> Sub<RHS> for ModuleString<R, T, A>
impl<RHS, T, R, A> Sub<RHS> for ModuleString<R, T, A>
Source§impl<'a, T, R, A> SubAssign<&'a ModuleString<R, T, A>> for ModuleString<R, T, A>
impl<'a, T, R, A> SubAssign<&'a ModuleString<R, T, A>> for ModuleString<R, T, A>
Source§fn sub_assign(&mut self, rhs: &'a Self)
fn sub_assign(&mut self, rhs: &'a Self)
-=
operation. Read moreSource§impl<'a, T, R, A> SubAssign<&'a T> for ModuleString<R, T, A>
impl<'a, T, R, A> SubAssign<&'a T> for ModuleString<R, T, A>
Source§fn sub_assign(&mut self, rhs: &'a T)
fn sub_assign(&mut self, rhs: &'a T)
-=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign + Neg<Output = R>, A: ?Sized> SubAssign<(R, T)> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign + Neg<Output = R>, A: ?Sized> SubAssign<(R, T)> for ModuleString<R, T, A>
Source§fn sub_assign(&mut self, rhs: (R, T))
fn sub_assign(&mut self, rhs: (R, T))
-=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign + Neg<Output = R> + One, A: ?Sized> SubAssign<T> for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign + Neg<Output = R> + One, A: ?Sized> SubAssign<T> for ModuleString<R, T, A>
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-=
operation. Read moreSource§impl<T: Hash + Eq, R: AddAssign + Neg<Output = R>, A: ?Sized> SubAssign for ModuleString<R, T, A>
impl<T: Hash + Eq, R: AddAssign + Neg<Output = R>, A: ?Sized> SubAssign for ModuleString<R, T, A>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more