pub struct MonoidalString<C, M: ?Sized> { /* private fields */ }Expand description
Creates free-arithmetic constructions based upon free-multiplication of letters of type C
that uses a Vec<C> internally
§Basic Construction
Given type parameters C and M, the construction goes as follows:
- Internally, each instance contains a list of values of type
C - By default, multiplication is given by concatenating the internal
Vec’s of each argument - Then, with the above as a base, the given type
Mcan modify that operation by applying a multiplication rule to the internal list every time anotherCis appended to it
With this system, using different M rules and C types, a number of variants can be defined:
FreeMonoid<C>is created by using the default multiplication with anyCFreeGroup<C>is made by wrapping it in aFreeInv<C>to allow eachCto be inverted and by and modifyingMto make inverses cancelFreePowMonoid<C,P>is constructed by wrapping eachCin aFreePow<C>to raise each element to a symbolic powerPand by havingMcombine any adjacent elements with equal base by adding their exponents
§Other impls
In addition to the basic multiplication traits, MonoidalString implements a number of other notable traits depending on the type arguments:
- Div, DivAssign, Inv, etc. These apply whenever
Mimplements - Index wraps the internal list’s implementation
InvMonoidRule<C>to provide a way to invertCelements - MulAssociative and MulCommutative whenever
Mimplements AssociativeMonoidRule or CommutativeMonoidRule - PartialEq with any type that implements
Borrow<[C]>. This is in order to make it possible to test equality with other structures (likeVec<C>and[C]) that are lists ofC’s - PartialOrd and Ord implement lexicographic ordering
- Extend, FromIterator, and Product all are implemented by applying the multiplication
rule to an Iterator of
C’s.
§A note on IterMut
The method [MonoidalString.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 MonoidRule’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 MonoidalString into an invalid state. Hence, to mitigate this, the iterator must remultiply every element again as it iterates over the references.
Implementations§
Source§impl<C, M: ?Sized> MonoidalString<C, M>
impl<C, M: ?Sized> MonoidalString<C, M>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of letters in this monoidal-string
§Examples
use maths_traits::algebra::One;
use free_algebra::FreeMonoid;
let x = FreeMonoid::one();
let y = x.clone() * 'a' * 'a';
assert_eq!(x.len(), 0);
assert_eq!(y.len(), 2);
Sourcepub fn iter(&self) -> Iter<'_, C>
pub fn iter(&self) -> Iter<'_, C>
Produces an iterator over references to the letters in this element
§Examples
use maths_traits::algebra::One;
use free_algebra::FreeMonoid;
let x = FreeMonoid::one() * 'a' * 'b' * 'c';
let mut iter = x.iter();
assert_eq!(iter.next(), Some(&'a'));
assert_eq!(iter.next(), Some(&'b'));
assert_eq!(iter.next(), Some(&'c'));
assert_eq!(iter.next(), None);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, C, M> ⓘwhere
M: MonoidRule<C>,
pub fn iter_mut(&mut self) -> IterMut<'_, C, M> ⓘwhere
M: MonoidRule<C>,
Produces an iterator over mutable references to the letters in this element
Note that the potential for modification does mean that the element needs to be remultiplied as letters are changed, so a potential reallocation of space may occur.
§Examples
use free_algebra::{FreePow, FreePowMonoid};
let x = FreePow('a',1) * FreePow('b',1) * FreePow('c',1);
let mut y = x.clone();
for FreePow(c,p) in y.iter_mut() {
*c = 'a';
}
assert_eq!(x, [FreePow('a',1), FreePow('b',1), FreePow('c',1)]);
assert_eq!(y, [FreePow('a',3)]);
Sourcepub fn reverse(self) -> Selfwhere
Self: Product<C>,
pub fn reverse(self) -> Selfwhere
Self: Product<C>,
Reverses the letters in this element and remultiplies
§Examples
use maths_traits::algebra::One;
use free_algebra::FreeMonoid;
let x = FreeMonoid::one() * 'a' * 'b' * 'c';
let y = x.clone().reverse();
assert_eq!(x, ['a', 'b', 'c']);
assert_eq!(y, ['c', 'b', 'a']);
Sourcepub fn commutator(self, rhs: Self) -> Self
pub fn commutator(self, rhs: Self) -> Self
Computes the multiplicative commutator [a,b] = a⁻¹b⁻¹ab
§Examples
use free_algebra::FreeGroup;
use free_algebra::FreeInv::*;
let x:FreeGroup<_> = Id('a').into();
let y:FreeGroup<_> = Id('b').into();
assert_eq!(x, [Id('a')]);
assert_eq!(y, [Id('b')]);
assert_eq!(x.commutator(y), [Inv('a'), Inv('b'), Id('a'), Id('b')]);
A property of significance for this product is that when the arguments commute, the
output is always 1.
use maths_traits::algebra::One;
use free_algebra::{FreePowMonoid, FreePow};
let x:FreePowMonoid<_,_> = FreePow('a', 2).into();
let y:FreePowMonoid<_,_> = FreePow('a', -24).into();
assert!(x.commutator(y).is_one());
Trait Implementations§
Source§impl<C, M: ?Sized> Default for MonoidalString<C, M>
impl<C, M: ?Sized> Default for MonoidalString<C, M>
Source§impl<C: Display, M: ?Sized> Display for MonoidalString<C, M>
Formats the MonoidalString as a sequence of factors separated by *’s
impl<C: Display, M: ?Sized> Display for MonoidalString<C, M>
Formats the MonoidalString as a sequence of factors separated by *’s
§Examples
use maths_traits::algebra::One;
use free_algebra::FreeMonoid;
let x = FreeMonoid::one() * 'a' * 'b' * 'c';
assert_eq!(format!("{}", x), "a*b*c");
use maths_traits::algebra::*;
use free_algebra::FreeInv::*;
let y = Id('a') * Inv('b') * Inv('a') * Id('c');
assert_eq!(format!("{}", y), "a*b⁻¹*a⁻¹*c");Note that if the “alternate” flag # is used, then the *’s will be dropped.
use maths_traits::algebra::One;
use free_algebra::FreeMonoid;
let x = FreeMonoid::one() * 'a' * 'b' * 'c';
assert_eq!(format!("{:#}", x), "abc");
use maths_traits::algebra::*;
use free_algebra::FreeInv::*;
let y = Id('a') * Inv('b') * Inv('a') * Id('c');
assert_eq!(format!("{:#}", y), "ab⁻¹a⁻¹c");Source§impl<C: Eq, P: Add<Output = P> + Neg<Output = P> + Zero> Div<MonoidalString<FreePow<C, P>, PowRule>> for FreePow<C, P>
impl<C: Eq, P: Add<Output = P> + Neg<Output = P> + Zero> Div<MonoidalString<FreePow<C, P>, PowRule>> for FreePow<C, P>
Source§type Output = MonoidalString<FreePow<C, P>, PowRule>
type Output = MonoidalString<FreePow<C, P>, PowRule>
/ operator.Source§fn div(self, rhs: FreePowMonoid<C, P>) -> FreePowMonoid<C, P>
fn div(self, rhs: FreePowMonoid<C, P>) -> FreePowMonoid<C, P>
/ operation. Read moreSource§impl<'a, RHS, C, M> Div<RHS> for &'a MonoidalString<C, M>
impl<'a, RHS, C, M> Div<RHS> for &'a MonoidalString<C, M>
Source§impl<RHS, C, M> Div<RHS> for MonoidalString<C, M>
impl<RHS, C, M> Div<RHS> for MonoidalString<C, M>
Source§impl<'a, C, M> DivAssign<&'a C> for MonoidalString<C, M>
impl<'a, C, M> DivAssign<&'a C> for MonoidalString<C, M>
Source§fn div_assign(&mut self, rhs: &'a C)
fn div_assign(&mut self, rhs: &'a C)
/= operation. Read moreSource§impl<'a, C, M> DivAssign<&'a MonoidalString<C, M>> for MonoidalString<C, M>
impl<'a, C, M> DivAssign<&'a MonoidalString<C, M>> for MonoidalString<C, M>
Source§fn div_assign(&mut self, rhs: &'a Self)
fn div_assign(&mut self, rhs: &'a Self)
/= operation. Read moreSource§impl<C, M: InvMonoidRule<C> + ?Sized> DivAssign<C> for MonoidalString<C, M>
impl<C, M: InvMonoidRule<C> + ?Sized> DivAssign<C> for MonoidalString<C, M>
Source§fn div_assign(&mut self, rhs: C)
fn div_assign(&mut self, rhs: C)
/= operation. Read moreSource§impl<C, M: InvMonoidRule<C> + ?Sized> DivAssign for MonoidalString<C, M>
impl<C, M: InvMonoidRule<C> + ?Sized> DivAssign for MonoidalString<C, M>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<C, M: MonoidRule<C> + ?Sized> Extend<C> for MonoidalString<C, M>
impl<C, M: MonoidRule<C> + ?Sized> Extend<C> for MonoidalString<C, M>
Source§fn extend<I: IntoIterator<Item = C>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = C>>(&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<C, M: ?Sized> From<C> for MonoidalString<C, M>
impl<C, M: ?Sized> From<C> for MonoidalString<C, M>
Source§impl<C, M: ?Sized, T> FromIterator<T> for MonoidalString<C, M>where
Self: Product<T>,
impl<C, M: ?Sized, T> FromIterator<T> for MonoidalString<C, M>where
Self: Product<T>,
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<C, M: ?Sized, I> Index<I> for MonoidalString<C, M>
impl<C, M: ?Sized, I> Index<I> for MonoidalString<C, M>
Source§impl<C, M: ?Sized> IntoIterator for MonoidalString<C, M>
impl<C, M: ?Sized> IntoIterator for MonoidalString<C, M>
Source§impl<'a, C, M: InvMonoidRule<C> + ?Sized> Inv for &'a MonoidalString<C, M>where
MonoidalString<C, M>: Clone,
impl<'a, C, M: InvMonoidRule<C> + ?Sized> Inv for &'a MonoidalString<C, M>where
MonoidalString<C, M>: Clone,
Source§impl<C, M: InvMonoidRule<C> + ?Sized> Inv for MonoidalString<C, M>
impl<C, M: InvMonoidRule<C> + ?Sized> Inv for MonoidalString<C, M>
Source§impl<C: Eq, P: Add<Output = P>> Mul<MonoidalString<FreePow<C, P>, PowRule>> for FreePow<C, P>
impl<C: Eq, P: Add<Output = P>> Mul<MonoidalString<FreePow<C, P>, PowRule>> for FreePow<C, P>
Source§type Output = MonoidalString<FreePow<C, P>, PowRule>
type Output = MonoidalString<FreePow<C, P>, PowRule>
* operator.Source§fn mul(self, rhs: FreePowMonoid<C, P>) -> FreePowMonoid<C, P>
fn mul(self, rhs: FreePowMonoid<C, P>) -> FreePowMonoid<C, P>
* operation. Read moreSource§impl<'a, RHS, C, M> Mul<RHS> for &'a MonoidalString<C, M>
impl<'a, RHS, C, M> Mul<RHS> for &'a MonoidalString<C, M>
Source§impl<RHS, C, M> Mul<RHS> for MonoidalString<C, M>
impl<RHS, C, M> Mul<RHS> for MonoidalString<C, M>
Source§impl<'a, C, M> MulAssign<&'a C> for MonoidalString<C, M>
impl<'a, C, M> MulAssign<&'a C> for MonoidalString<C, M>
Source§fn mul_assign(&mut self, rhs: &'a C)
fn mul_assign(&mut self, rhs: &'a C)
*= operation. Read moreSource§impl<'a, C, M> MulAssign<&'a MonoidalString<C, M>> for MonoidalString<C, M>
impl<'a, C, M> MulAssign<&'a MonoidalString<C, M>> for MonoidalString<C, M>
Source§fn mul_assign(&mut self, rhs: &'a Self)
fn mul_assign(&mut self, rhs: &'a Self)
*= operation. Read moreSource§impl<C, M: MonoidRule<C> + ?Sized> MulAssign<C> for MonoidalString<C, M>
impl<C, M: MonoidRule<C> + ?Sized> MulAssign<C> for MonoidalString<C, M>
Source§fn mul_assign(&mut self, rhs: C)
fn mul_assign(&mut self, rhs: C)
*= operation. Read moreSource§impl<C, M: MonoidRule<C> + ?Sized> MulAssign for MonoidalString<C, M>
impl<C, M: MonoidRule<C> + ?Sized> MulAssign for MonoidalString<C, M>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read more