haar_lib/algebra/
traits.rs1use crate::trait_alias;
3
4pub trait Set {}
6
7pub trait BinaryOp: Set {
9 fn op(self, other: Self) -> Self;
11
12 fn op_assign_r(&mut self, b: Self)
14 where
15 Self: Clone,
16 {
17 *self = Self::op(self.clone(), b);
18 }
19
20 fn op_assign_l(&mut self, b: Self)
22 where
23 Self: Clone,
24 {
25 *self = Self::op(b, self.clone());
26 }
27}
28
29pub trait Identity: Set {
31 fn id() -> Self;
33}
34
35pub trait Inverse: Set {
37 fn inv(self) -> Self;
39}
40
41pub trait Commutative {}
43pub trait Associative {}
45pub trait Idempotence {}
47
48trait_alias!(#[doc = "半群"] Semigroup: BinaryOp + Associative);
49trait_alias!(#[doc = "モノイド"] Monoid: Semigroup + Identity);
50trait_alias!(#[doc = "可換モノイド"] AbelianMonoid: Monoid + Commutative);
51trait_alias!(#[doc = "群"] Group: Monoid + Inverse);
52trait_alias!(#[doc = "可換群"] AbelianGroup: Group + Commutative);
53
54pub trait Times: BinaryOp + Identity + Clone {
56 fn times(self, mut n: u64) -> Self {
60 let mut ret = Self::id();
61 let mut a = self;
62
63 while n > 0 {
64 if n & 1 == 1 {
65 ret = Self::op(ret, a.clone());
66 }
67 a = Self::op(a.clone(), a);
68 n >>= 1;
69 }
70
71 ret
72 }
73}
74impl<A: BinaryOp + Identity + Clone> Times for A {}
75
76pub trait FoldM: Iterator {
78 fn fold_m(self) -> Self::Item
80 where
81 Self: Sized,
82 Self::Item: Monoid,
83 {
84 self.fold(Self::Item::id(), Self::Item::op)
85 }
86}
87
88impl<I> FoldM for I where I: Iterator + ?Sized {}