1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::ring::*;

use self::submatrix::{AsFirstElement, Submatrix, SubmatrixMut};

use super::*;

pub struct DenseMatrix<R>
    where R: ?Sized + RingBase
{
    data: Box<[R::Element]>,
    col_count: usize
}

impl<R> DenseMatrix<R>
    where R: ?Sized + RingBase
{
    pub fn new(data: Box<[R::Element]>, col_count: usize) -> Self {
        assert!(data.len() % col_count == 0);
        Self { data, col_count }
    }

    pub fn data<'a>(&'a self) -> Submatrix<'a, AsFirstElement<R::Element>, R::Element> {
        Submatrix::<AsFirstElement<_>, _>::new(&self.data, self.row_count(), self.col_count())
    }

    pub fn data_mut<'a>(&'a mut self) -> SubmatrixMut<'a, AsFirstElement<R::Element>, R::Element> {
        let row_count = self.row_count();
        let col_count = self.col_count();
        SubmatrixMut::<AsFirstElement<_>, _>::new(&mut self.data, row_count, col_count)
    }
}

impl<R> DenseMatrix<R>
    where R: ?Sized + RingBase
{
    pub fn at(&self, i: usize, j: usize) -> &R::Element {
        &self.data[i * self.col_count + j]
    }

    pub fn at_mut(&mut self, i: usize, j: usize) -> &mut R::Element {
        &mut self.data[i * self.col_count + j]
    }

    pub fn identity<S>(n: usize, ring: S) -> Self
        where S: RingStore<Type = R>
    {
        let mut result = DenseMatrix {
            data: (0..(n * n)).map(|_| ring.zero()).collect::<Vec<_>>().into_boxed_slice(),
            col_count: n
        };
        for i in 0..n {
            result.data[i * n + i] = ring.one();
        }
        return result;
    }

    pub fn zero<S>(n: usize, m: usize, ring: S) -> Self
        where S: RingStore<Type = R>
    {
        let result = DenseMatrix {
            data: (0..(n * m)).map(|_| ring.zero()).collect::<Vec<_>>().into_boxed_slice(),
            col_count: m
        };
        return result;
    }

    pub fn clone_matrix<S>(&self, ring: S) -> Self
        where S: RingStore<Type = R>
    {
        DenseMatrix {
            col_count: self.col_count,
            data: self.data.iter().map(|x| ring.clone_el(x)).collect::<Vec<_>>().into_boxed_slice()
        }
    }

    pub fn set_row_count<S>(&mut self, new_count: usize, ring: S)
        where S: RingStore<Type = R>
    {
        let mut new_data = std::mem::replace(&mut self.data, Box::new([])).into_vec();
        new_data.resize_with(new_count * self.col_count(), || ring.zero());
        self.data = new_data.into_boxed_slice();
    }
}

impl<R> Matrix<R> for DenseMatrix<R>
    where R: ?Sized + RingBase
{
    fn row_count(&self) -> usize {
        self.data.len() / self.col_count
    }

    fn col_count(&self) -> usize {
        self.col_count
    }

    fn entry_at(&self, i: usize, j: usize) -> &R::Element {
        self.at(i, j)
    }
}

pub struct TransformRows<'a, R>(pub &'a mut DenseMatrix<R>)
    where R: ?Sized + RingBase;

pub struct TransformCols<'a, R>(pub &'a mut DenseMatrix<R>)
    where R: ?Sized + RingBase;

impl<'a, R> TransformTarget<R> for TransformRows<'a, R>
    where R: ?Sized + RingBase
{
    fn transform(&mut self, ring: &R, i: usize, j: usize, transform: &[<R as RingBase>::Element; 4]) {
        let A = &mut *self.0;
        for l in 0..A.col_count() {
            let (new_i, new_j) = (
                ring.add(ring.mul_ref(A.entry_at(i, l), &transform[0]), ring.mul_ref(A.entry_at(j, l), &transform[1])),
                ring.add(ring.mul_ref(A.entry_at(i, l), &transform[2]), ring.mul_ref(A.entry_at(j, l), &transform[3]))
            );
            *A.at_mut(i, l) = new_i;
            *A.at_mut(j, l) = new_j;
        }
    }

    fn subtract(&mut self, ring: &R, src: usize, dst: usize, factor: &<R as RingBase>::Element) {
        let A = &mut *self.0;
        for j in 0..A.col_count() {
            let to_sub = ring.mul_ref(factor, A.entry_at(src, j));
            ring.sub_assign(A.at_mut(dst, j), to_sub);
        }
    }
}

impl<'a, R> TransformTarget<R> for TransformCols<'a, R>
    where R: ?Sized + RingBase
{
    fn transform(&mut self, ring: &R, i: usize, j: usize, transform: &[<R as RingBase>::Element; 4]) {
        let A = &mut *self.0;
        for l in 0..A.row_count() {
            let (new_i, new_j) = (
                ring.add(ring.mul_ref(A.entry_at(l, i), &transform[0]), ring.mul_ref(A.entry_at(l, j), &transform[1])),
                ring.add(ring.mul_ref(A.entry_at(l, i), &transform[2]), ring.mul_ref(A.entry_at(l, j), &transform[3]))
            );
            *A.at_mut(l, i) = new_i;
            *A.at_mut(l, j) = new_j;
        }
    }

    fn subtract(&mut self, ring: &R, src: usize, dst: usize, factor: &<R as RingBase>::Element) {
        let A = &mut *self.0;
        for i in 0..A.row_count() {
            let to_sub = ring.mul_ref(factor, A.entry_at(i, src));
            ring.sub_assign(A.at_mut(i, dst), to_sub);
        }
    }
}