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
use std::ops::Mul;

use crate::general::{Field, MultiplicativeGroup, MultiplicativeMonoid};
use crate::linear::FiniteDimVectorSpace;

/// The space of all matrices.
pub trait Matrix:
    Sized + Clone + Mul<<Self as Matrix>::Row, Output = <Self as Matrix>::Column>
{
    /// The underlying field.
    type Field: Field;

    /// The type of rows of this matrix.
    type Row: FiniteDimVectorSpace<Field = Self::Field>;

    /// The type of columns of this matrix.
    type Column: FiniteDimVectorSpace<Field = Self::Field>;

    /// The type of the transposed matrix.
    type Transpose: Matrix<Field = Self::Field, Row = Self::Column, Column = Self::Row>;

    /// The number of rows of this matrix.
    fn nrows(&self) -> usize;

    /// The number of columns of this matrix.
    fn ncolumns(&self) -> usize;

    /// The i-th row of this matrix.
    fn row(&self, i: usize) -> Self::Row;

    /// The i-th column of this matrix.
    fn column(&self, i: usize) -> Self::Column;

    /// Gets the component at row `i` and column `j` of this matrix without bound checking.
    unsafe fn get_unchecked(&self, i: usize, j: usize) -> Self::Field;

    /// Gets the component at row `i` and column `j` of this matrix.
    fn get(&self, i: usize, j: usize) -> Self::Field {
        assert!(
            i < self.nrows() && j < self.ncolumns(),
            "Matrix indexing: index out of bounds."
        );

        unsafe { self.get_unchecked(i, j) }
    }

    /// Transposes this matrix.
    fn transpose(&self) -> Self::Transpose;
}

/// The space of all matrices that are stable under modifications of its components, rows and columns.
pub trait MatrixMut: Matrix {
    /// Sets the i-th row of this matrix.
    #[inline]
    fn set_row(&self, i: usize, row: &Self::Row) -> Self {
        let mut res = self.clone();
        res.set_row_mut(i, row);
        res
    }

    /// In-place sets the i-th row of this matrix.
    fn set_row_mut(&mut self, i: usize, row: &Self::Row);

    /// Sets the i-th col of this matrix.
    #[inline]
    fn set_column(&self, i: usize, col: &Self::Column) -> Self {
        let mut res = self.clone();
        res.set_column_mut(i, col);
        res
    }

    /// In-place sets the i-th col of this matrix.
    fn set_column_mut(&mut self, i: usize, col: &Self::Column);

    /// Sets the component at row `i` and column `j` of this matrix without bound checking.
    unsafe fn set_unchecked(&mut self, i: usize, j: usize, val: Self::Field);

    /// Sets the component at row `i` and column `j` of this matrix.
    fn set(&mut self, i: usize, j: usize, val: Self::Field) {
        assert!(
            i < self.nrows() && j < self.ncolumns(),
            "Matrix indexing: index out of bounds."
        );

        unsafe { self.set_unchecked(i, j, val) }
    }
}

/// The monoid of all square matrices, including non-inversible ones.
pub trait SquareMatrix:
    Matrix<
        Row = <Self as SquareMatrix>::Vector,
        Column = <Self as SquareMatrix>::Vector,
        Transpose = Self,
    > + MultiplicativeMonoid
{
    /// The type of rows, column, and diagonal of this matrix.
    type Vector: FiniteDimVectorSpace<Field = Self::Field>;

    /// The diagonal of this matrix.
    fn diagonal(&self) -> Self::Vector;

    /// The determinant of this matrix.
    fn determinant(&self) -> Self::Field;

    // FIXME: add an epsilon value (as for try_normalize)?
    /// Attempts to two_sided_inverse `self`.
    #[inline]
    fn try_inverse(&self) -> Option<Self>;

    /// The number of rows or column of this matrix.
    #[inline]
    fn dimension(&self) -> usize {
        self.nrows()
    }

    /// In-place transposition.
    #[inline]
    fn transpose_mut(&mut self) {
        *self = self.transpose()
    }
}

/// The monoid of all mutable square matrices that are stable under modification of its diagonal.
pub trait SquareMatrixMut:
    SquareMatrix
    + MatrixMut<
        Row = <Self as SquareMatrix>::Vector,
        Column = <Self as SquareMatrix>::Vector,
        Transpose = Self,
    >
{
    /// Constructs a new diagonal matrix.
    fn from_diagonal(diag: &Self::Vector) -> Self;

    /// Sets the matrix diagonal.
    #[inline]
    fn set_diagonal(&self, diag: &Self::Vector) -> Self {
        let mut res = self.clone();
        res.set_diagonal_mut(diag);
        res
    }

    /// In-place sets the matrix diagonal.
    fn set_diagonal_mut(&mut self, diag: &Self::Vector);
}

/// The group of inversible matrix. Commonly known as the General Linear group `GL(n)` by
/// algebraists.
pub trait InversibleSquareMatrix: SquareMatrix + MultiplicativeGroup {}

// Add marker traits for symmetric-, SDP-ness, etc.