haar_lib/algebra/
matrix.rs

1//! 行列の代数的構造
2
3pub use crate::algebra::traits::*;
4use crate::{algebra::semiring::*, impl_algebra, linalg::matrix::MatrixOnSemiring};
5
6/// 行列の加法
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub struct SumMatrix<R> {
9    /// 行列の要素の環
10    pub semiring: R,
11    /// 行数
12    pub h: usize,
13    /// 列数
14    pub w: usize,
15}
16impl<R> SumMatrix<R> {
17    /// [`SumMatrix`]を返す。
18    pub fn new(semiring: R, h: usize, w: usize) -> Self {
19        Self { semiring, h, w }
20    }
21}
22impl<R: Semiring + Clone + PartialEq> Additive for SumMatrix<R>
23where
24    R::Element: Copy + PartialEq,
25{
26    fn times(&self, a: Self::Element, n: u64) -> Self::Element {
27        a.times(n)
28    }
29}
30impl_algebra!(
31    {R: Semiring + Clone + PartialEq} SumMatrix<R> where {R::Element: Copy + PartialEq};
32    set: MatrixOnSemiring<R>;
33    op: |_, a: Self::Element, b: Self::Element| a + b;
34    id: |s: &Self| MatrixOnSemiring::zero(s.semiring.clone(), s.h, s.w);
35    assoc;
36    commu;
37);
38impl_algebra!(
39    {R: Ring + Clone + PartialEq} SumMatrix<R> where {R::Element: Copy + PartialEq};
40    inv: |_, a: Self::Element| -a;
41);
42
43/// 行列の乗法
44#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
45pub struct ProdMatrix<R> {
46    /// 行列の要素の環
47    pub semiring: R,
48    /// 行列の大きさ
49    pub n: usize,
50}
51impl<R> ProdMatrix<R> {
52    /// [`ProdMatrix`]を返す。
53    pub fn new(semiring: R, n: usize) -> Self {
54        Self { semiring, n }
55    }
56}
57impl<R: Semiring + Clone + PartialEq> Multiplicative for ProdMatrix<R> where
58    R::Element: Copy + PartialEq
59{
60}
61impl_algebra!(
62    {R: Semiring + Clone + PartialEq} ProdMatrix<R> where {R::Element: Copy + PartialEq};
63    set: MatrixOnSemiring<R>;
64    op: |_, a: Self::Element, b: Self::Element| a * b;
65    id: |s: &Self| MatrixOnSemiring::unit(s.semiring.clone(), s.n);
66    assoc;
67);