[−][src]Struct free_algebra::module::ModuleString
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.
Methods
impl<T: Hash + Eq, R, A: ?Sized> ModuleString<R, T, A>
[src]
pub fn len(&self) -> usize
[src]
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);
pub fn get_ref<Q: Hash + Eq>(&self, t: &Q) -> Option<&R> where
T: Borrow<Q>,
[src]
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);
pub fn get<Q: Hash + Eq>(&self, t: &Q) -> R where
T: Borrow<Q>,
R: Zero + Clone,
[src]
T: Borrow<Q>,
R: Zero + Clone,
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);
pub fn iter<'a>(&'a self) -> Iter<'a, R, T>
[src]
Produces an iterator over references to the terms and references in this element
ⓘImportant traits for IterMut<'a, R, T, A>pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, R, T, A> where
R: AddAssign,
[src]
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);
pub fn commutator(self, rhs: Self) -> Self where
Self: MulMagma + Sub<Output = Self>,
[src]
Self: MulMagma + Sub<Output = 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
impl<T: Hash + Eq, R: One, A: ?Sized> From<T> for ModuleString<R, T, A>
[src]
impl<T: Hash + Eq, R: One, A: ?Sized> From<(R, T)> for ModuleString<R, T, A>
[src]
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> Extend<(R, T)> for ModuleString<R, T, A>
[src]
fn extend<I: IntoIterator<Item = (R, T)>>(&mut self, iter: I)
[src]
impl<T: Hash + Eq, R, A: ?Sized> IntoIterator for ModuleString<R, T, A>
[src]
type Item = (R, T)
The type of the elements being iterated over.
type IntoIter = IntoIter<R, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<R, T>
[src]
impl<R, T: Hash + Eq, A: ?Sized> Clone for ModuleString<R, T, A> where
R: Clone,
T: Clone,
[src]
R: Clone,
T: Clone,
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, other: &Self)
[src]
impl<R, T: Hash + Eq, A: ?Sized> Default for ModuleString<R, T, A>
[src]
impl<R, T: Hash + Eq, A: ?Sized> Eq for ModuleString<R, T, A> where
R: Eq,
T: Eq,
[src]
R: Eq,
T: Eq,
impl<R, T: Hash + Eq, A: ?Sized> PartialEq<ModuleString<R, T, A>> for ModuleString<R, T, A> where
R: PartialEq,
T: PartialEq,
[src]
R: PartialEq,
T: PartialEq,
impl<R: Display, T: Hash + Eq + Display, A: ?Sized> Display for ModuleString<R, T, A>
[src]
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)");
impl<R, T: Hash + Eq, A: ?Sized> Debug for ModuleString<R, T, A> where
R: Debug,
T: Debug,
[src]
R: Debug,
T: Debug,
impl<RHS, T, R, A> Div<RHS> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: DivAssign<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
Self: DivAssign<RHS>,
type Output = Self
The resulting type after applying the /
operator.
fn div(self, rhs: RHS) -> Self
[src]
impl<'a, RHS, T, R, A> Div<RHS> for &'a ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Div<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Div<RHS>,
type Output = <ModuleString<R, T, A> as Div<RHS>>::Output
The resulting type after applying the /
operator.
fn div(self, rhs: RHS) -> Self::Output
[src]
impl<RHS, T, R, A> Sub<RHS> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: SubAssign<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
Self: SubAssign<RHS>,
type Output = Self
The resulting type after applying the -
operator.
fn sub(self, rhs: RHS) -> Self
[src]
impl<'a, RHS, T, R, A> Sub<RHS> for &'a ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Sub<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Sub<RHS>,
type Output = <ModuleString<R, T, A> as Sub<RHS>>::Output
The resulting type after applying the -
operator.
fn sub(self, rhs: RHS) -> Self::Output
[src]
impl<RHS, T, R, A> Add<RHS> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: AddAssign<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
Self: AddAssign<RHS>,
type Output = Self
The resulting type after applying the +
operator.
fn add(self, rhs: RHS) -> Self
[src]
impl<'a, RHS, T, R, A> Add<RHS> for &'a ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Add<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Add<RHS>,
type Output = <ModuleString<R, T, A> as Add<RHS>>::Output
The resulting type after applying the +
operator.
fn add(self, rhs: RHS) -> Self::Output
[src]
impl<RHS, T, R, A> Mul<RHS> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: MulAssign<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
Self: MulAssign<RHS>,
type Output = Self
The resulting type after applying the *
operator.
fn mul(self, rhs: RHS) -> Self
[src]
impl<'a, RHS, T, R, A> Mul<RHS> for &'a ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Mul<RHS>,
[src]
T: Hash + Eq,
A: ?Sized,
ModuleString<R, T, A>: Clone + Mul<RHS>,
type Output = <ModuleString<R, T, A> as Mul<RHS>>::Output
The resulting type after applying the *
operator.
fn mul(self, rhs: RHS) -> Self::Output
[src]
impl<T: Hash + Eq, R: Neg, A: ?Sized> Neg for ModuleString<R, T, A>
[src]
type Output = ModuleString<R::Output, T, A>
The resulting type after applying the -
operator.
fn neg(self) -> Self::Output
[src]
impl<'a, T: Hash + Eq, R: Neg, A: ?Sized> Neg for &'a ModuleString<R, T, A> where
ModuleString<R, T, A>: Clone,
[src]
ModuleString<R, T, A>: Clone,
type Output = ModuleString<R::Output, T, A>
The resulting type after applying the -
operator.
fn neg(self) -> Self::Output
[src]
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> AddAssign<ModuleString<R, T, A>> for ModuleString<R, T, A>
[src]
fn add_assign(&mut self, rhs: Self)
[src]
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> AddAssign<(R, T)> for ModuleString<R, T, A>
[src]
fn add_assign(&mut self, rhs: (R, T))
[src]
impl<T: Hash + Eq, R: AddAssign + One, A: ?Sized> AddAssign<T> for ModuleString<R, T, A>
[src]
fn add_assign(&mut self, rhs: T)
[src]
impl<'a, T, R, A> AddAssign<&'a ModuleString<R, T, A>> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: AddAssign<Self>,
Self: Clone,
[src]
T: Hash + Eq,
A: ?Sized,
Self: AddAssign<Self>,
Self: Clone,
fn add_assign(&mut self, rhs: &'a Self)
[src]
impl<'a, T, R, A> AddAssign<&'a T> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: AddAssign<T>,
T: Clone,
[src]
T: Hash + Eq,
A: ?Sized,
Self: AddAssign<T>,
T: Clone,
fn add_assign(&mut self, rhs: &'a T)
[src]
impl<T: Hash + Eq, R: AddAssign + Neg<Output = R>, A: ?Sized> SubAssign<ModuleString<R, T, A>> for ModuleString<R, T, A>
[src]
fn sub_assign(&mut self, rhs: Self)
[src]
impl<T: Hash + Eq, R: AddAssign + Neg<Output = R>, A: ?Sized> SubAssign<(R, T)> for ModuleString<R, T, A>
[src]
fn sub_assign(&mut self, rhs: (R, T))
[src]
impl<T: Hash + Eq, R: AddAssign + Neg<Output = R> + One, A: ?Sized> SubAssign<T> for ModuleString<R, T, A>
[src]
fn sub_assign(&mut self, rhs: T)
[src]
impl<'a, T, R, A> SubAssign<&'a ModuleString<R, T, A>> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: SubAssign<Self>,
Self: Clone,
[src]
T: Hash + Eq,
A: ?Sized,
Self: SubAssign<Self>,
Self: Clone,
fn sub_assign(&mut self, rhs: &'a Self)
[src]
impl<'a, T, R, A> SubAssign<&'a T> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: SubAssign<T>,
T: Clone,
[src]
T: Hash + Eq,
A: ?Sized,
Self: SubAssign<T>,
T: Clone,
fn sub_assign(&mut self, rhs: &'a T)
[src]
impl<T: Hash + Eq, R: MulAssign + Clone, A: ?Sized> MulAssign<R> for ModuleString<R, T, A>
[src]
fn mul_assign(&mut self, rhs: R)
[src]
impl<'a, T, R, A> MulAssign<&'a R> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: MulAssign<R>,
R: Clone,
[src]
T: Hash + Eq,
A: ?Sized,
Self: MulAssign<R>,
R: Clone,
fn mul_assign(&mut self, rhs: &'a R)
[src]
impl<T: Hash + Eq + Clone, R: MulMagma + AddMagma, A: ?Sized + AlgebraRule<R, T>> MulAssign<(R, T)> for ModuleString<R, T, A>
[src]
fn mul_assign(&mut self, (r1, t1): (R, T))
[src]
impl<T: Hash + Eq + Clone, R: Semiring, A: ?Sized + AlgebraRule<R, T>> MulAssign<ModuleString<R, T, A>> for ModuleString<R, T, A>
[src]
fn mul_assign(&mut self, rhs: Self)
[src]
impl<T: Hash + Eq, R: DivAssign + Clone, A: ?Sized> DivAssign<R> for ModuleString<R, T, A>
[src]
fn div_assign(&mut self, rhs: R)
[src]
impl<'a, T, R, A> DivAssign<&'a R> for ModuleString<R, T, A> where
T: Hash + Eq,
A: ?Sized,
Self: DivAssign<R>,
R: Clone,
[src]
T: Hash + Eq,
A: ?Sized,
Self: DivAssign<R>,
R: Clone,
fn div_assign(&mut self, rhs: &'a R)
[src]
impl<T: Hash + Eq, R, A: ?Sized, I> Index<I> for ModuleString<R, T, A> where
HashMap<T, R>: Index<I>,
[src]
HashMap<T, R>: Index<I>,
type Output = <HashMap<T, R> as Index<I>>::Output
The returned type after indexing.
fn index(&self, i: I) -> &Self::Output
[src]
impl<R, T: Hash + Eq, A: ?Sized> Hash for ModuleString<R, T, A> where
HashMap<T, R>: Hash,
[src]
HashMap<T, R>: Hash,
fn hash<__HRTA>(&self, __state: &mut __HRTA) where
__HRTA: Hasher,
[src]
__HRTA: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> FromIterator<(R, T)> for ModuleString<R, T, A>
[src]
fn from_iter<I: IntoIterator<Item = (R, T)>>(iter: I) -> Self
[src]
impl<T: Hash + Eq, R: AddAssign + One, A: ?Sized> FromIterator<T> for ModuleString<R, T, A>
[src]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
[src]
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> FromIterator<ModuleString<R, T, A>> for ModuleString<R, T, A>
[src]
fn from_iter<I: IntoIterator<Item = Self>>(iter: I) -> Self
[src]
impl<T: Hash + Eq, R: AddAssign, A: ?Sized, K> Sum<K> for ModuleString<R, T, A> where
Self: FromIterator<K>,
[src]
Self: FromIterator<K>,
impl<T: Hash + Eq, R, A: ?Sized, K> Product<K> for ModuleString<R, T, A> where
Self: Mul<K, Output = Self> + One,
[src]
Self: Mul<K, Output = Self> + One,
impl<T: Hash + Eq, R: AddAssign, A: ?Sized> Zero for ModuleString<R, T, A>
[src]
impl<T: Clone + Hash + Eq, R: PartialEq + UnitalSemiring, A: UnitalAlgebraRule<R, T> + ?Sized> One for ModuleString<R, T, A>
[src]
impl<Z, R, T, A: ?Sized> Pow<Z> for ModuleString<R, T, A> where
Z: Natural,
T: Hash + Eq + Clone,
R: PartialEq + UnitalSemiring,
A: UnitalAlgebraRule<R, T> + AssociativeAlgebraRule<R, T>,
[src]
Z: Natural,
T: Hash + Eq + Clone,
R: PartialEq + UnitalSemiring,
A: UnitalAlgebraRule<R, T> + AssociativeAlgebraRule<R, T>,
impl<T: Hash + Eq, R: AddAssociative, A: ?Sized> AddAssociative for ModuleString<R, T, A>
[src]
impl<T: Hash + Eq, R: AddCommutative, A: ?Sized> AddCommutative for ModuleString<R, T, A>
[src]
impl<T: Hash + Eq, R: MulAssociative, A: ?Sized + AssociativeAlgebraRule<R, T>> MulAssociative for ModuleString<R, T, A>
[src]
impl<T: Hash + Eq, R: MulCommutative, A: ?Sized + CommutativeAlgebraRule<R, T>> MulCommutative for ModuleString<R, T, A>
[src]
impl<T: Hash + Eq, R: Distributive, A: ?Sized> Distributive<ModuleString<R, T, A>> for ModuleString<R, T, A>
[src]
Auto Trait Implementations
impl<R, T, A: ?Sized> Send for ModuleString<R, T, A> where
A: Send,
R: Send,
T: Send,
A: Send,
R: Send,
T: Send,
impl<R, T, A: ?Sized> Sync for ModuleString<R, T, A> where
A: Sync,
R: Sync,
T: Sync,
A: Sync,
R: Sync,
T: Sync,
impl<R, T, A: ?Sized> Unpin for ModuleString<R, T, A> where
R: Unpin,
T: Unpin,
R: Unpin,
T: Unpin,
impl<R, T, A: ?Sized> UnwindSafe for ModuleString<R, T, A> where
A: UnwindSafe,
R: UnwindSafe,
T: UnwindSafe,
A: UnwindSafe,
R: UnwindSafe,
T: UnwindSafe,
impl<R, T, A: ?Sized> RefUnwindSafe for ModuleString<R, T, A> where
A: RefUnwindSafe,
R: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
R: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> From<!> for T
[src]
impl<I> IntoIterator for I where
I: Iterator,
[src]
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<G> MulN for G where
G: AddSemigroup + Zero,
[src]
G: AddSemigroup + Zero,
impl<G> MulZ for G where
G: Negatable + AddMonoid,
[src]
G: Negatable + AddMonoid,
fn mul_z<N>(self, n: N) -> Self where
N: IntegerSubset,
[src]
N: IntegerSubset,
impl<G> PowN for G where
G: MulSemigroup + One,
[src]
G: MulSemigroup + One,