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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use std::fmt::Display;
use std::ops::{Add, Mul, Sub};
use crate::util::{ErrorKind, NmlError};
use rand::Rng;
use crate::util::ErrorKind::{CreateMatrix, InvalidCols, InvalidRows};


/// Nml_Matrix represents a matrix with a given number of rows and columns, the Data is stored in a one dimensional array using row-major-ordering (data[i][j] = data_new[i * m +j], where m is the number of columns)
/// The Library contains a few methods to create matrices with or without data.
#[derive(Debug)]
pub struct NmlMatrix {
    pub num_rows: u32,
    pub num_cols: u32,
    pub data: Vec<f64>,
    pub is_square: bool,
}

impl NmlMatrix {
    ///creates a matrix without data and reserves the capacity for the Data Vector
    pub fn new(num_rows: u32, num_cols: u32) -> Self {
        let data: Vec<f64> = Vec::with_capacity((num_rows * num_cols) as usize);
        Self {
            num_rows,
            num_cols,
            data,
            is_square: num_rows == num_cols,
        }
    }
    ///use a 2d Vector to initialize the matrix. Each Vector in the 2d Vector is a row and the length of these vectors are the columns
    pub fn new_with_2d_vec(num_rows: u32, num_cols: u32, data_2d: &mut Vec<Vec<f64>>) -> Result<Self, NmlError> {
        let rows: u32 = data_2d.len() as u32;
        let mut cols_match = true;
        let mut data: Vec<f64> = Vec::with_capacity((num_rows*num_cols) as usize);
        for element in data_2d {
            if element.len() as u32 != num_cols {
                cols_match = false;
                break;
            }
            data.append(element);
        }
        match cols_match && rows == num_rows{
            true => {Ok(Self{
                num_cols,
                num_rows,
                data,
                is_square: num_rows == num_rows
            })},
            false => {Err(NmlError::new(ErrorKind::CreateMatrix))}
        }
    }

    ///Constructor that uses a vector to initialize the matrix. checks if the entered rows and columns fit the vector size
    pub fn new_with_data(num_rows: u32, num_cols: u32, data: Vec<f64>) -> Result<Self, NmlError> {
        match (num_rows * num_cols) as usize == data.len() {
            false => Err(NmlError::new(ErrorKind::CreateMatrix)),
            true => {
                let is_square = num_rows == num_cols;
                Ok(NmlMatrix {
                    num_rows,
                    num_cols,
                    data,
                    is_square,
                })
            },
        }
    }

    ///Returns a matrix with defined size and random data between minimum and maximum values
    pub fn nml_mat_rnd(num_rows: u32, num_cols: u32, minimum: f64, maximum: f64) -> Self {
        let mut rng = rand::thread_rng();
        let random_numbers: Vec<f64> = (0..100).map(|_| (rng.gen_range(minimum..maximum))).collect();
        Self {
            num_rows,
            num_cols,
            data: random_numbers,
            is_square: num_rows == num_cols,
        }
    }

    ///Creates a square matrix of a given size, where all cells are filled with 0.0
    pub fn nml_mat_sqr(size: u32) -> Self {
        Self {
            num_rows: size,
            num_cols: size,
            data: vec![0.0; (size * size) as usize],
            is_square: true,
        }
    }

    ///creates a identity matrix with the given size
    pub fn nml_mat_eye(size: u32) -> Self {
        let mut data: Vec<f64> = vec![0.0; (size * size) as usize];
        for i in 0..size {
            data[(i * size + i) as usize] = 1.0;
        }
        Self {
            num_rows: size,
            num_cols: size,
            data,
            is_square: true,
        }
    }
    ///Creates a new matrix which is a copy of a given matrix. Uses only the reference to the matrix, so that the original matrix is not moved
    pub fn nml_mat_cp(matrix: &NmlMatrix) -> Self {
        Self {
            num_rows: matrix.num_rows,
            num_cols: matrix.num_cols,
            data: matrix.data.clone(),
            is_square: matrix.is_square,
        }
    }
    ///Unimplemented method that should read in a matrix from a file
    pub fn nml_mat_from_file() -> Self {
        unimplemented!("Not implemented yet")
    }
    ///Checks if two matrices are equal by checking their dimensions and after that every cell. Method is also used for implementation of trait PatialEq
    pub fn equality(self: &Self, matrix: &NmlMatrix) -> bool {
        if self.num_rows != matrix.num_rows || self.num_cols != matrix.num_cols {
            return false;
        }
        for i in 0..self.num_rows {
            for j in 0..self.num_cols {
                if self.data[(i * self.num_cols + j) as usize] != matrix.data[(i * matrix.num_cols + j) as usize] {
                    return false;
                }
            }
        }
        true
    }
    ///Checks if two matrices are equal with a given tolerance
    pub fn equality_in_tolerance(self: &Self, matrix: NmlMatrix, tolerance: f64) -> bool {
        if self.num_rows != matrix.num_rows || self.num_cols != matrix.num_cols {
            return false;
        }
        for i in 0..self.num_rows {
            for j in 0..self.num_cols {
                if (self.data[(i * self.num_cols + j) as usize] - matrix.data[(i * matrix.num_cols + j) as usize]).abs() > tolerance {
                    return false;
                }
            }
        }
        true
    }
    ///Returns the value a specified at A[i,j]
    pub fn at(self: &Self, row: u32, col: u32) -> Result<f64, NmlError> {
        match row < self.num_rows as u32 && col < self.num_cols as u32 {
            false => Err(NmlError::new(InvalidRows)),
            true => Ok(self.data[(row * self.num_cols as u32 + col) as usize]),
        }
    }
    ///Returns a result with a specified Column of a matrix, which in itself also is a matrix. If the specified matrix is not in the matrix the result will contain an error
    pub fn get_column(self: &Self, column: u32) -> Result<Self, NmlError> {
        match column < self.num_cols {
            false => Err(NmlError::new(InvalidCols)),
            true => {
                let mut data: Vec<f64> = Vec::with_capacity(self.num_rows as usize);
                for i in 0..self.num_rows {
                    data.push(self.data[(i * self.num_rows + column) as usize]);
                }
                Ok(Self {
                    num_cols: 1,
                    num_rows: self.num_rows,
                    data,
                    is_square: false
                })
            },
        }
    }

    ///Returns a result which either contains a row of a matrix (which is also a matrix) or a error
    pub fn get_row(self: &Self, row: u32) -> Result<Self, NmlError> {
        match row < self.num_rows {
            true => {
                let data: Vec<f64> = self.data[(row * self.num_cols) as usize..(row * self.num_cols + self.num_cols) as usize].to_vec().clone();
                Ok(Self {
                    num_cols: self.num_cols,
                    num_rows: 1,
                    data,
                    is_square: false
                })
            },
            false => Err(NmlError::new(InvalidRows)),
        }
    }
    /// Method sets the value of a given cell in the matrix through a mutable reference
    pub fn set_value(self: &mut Self, row: u32, col: u32, data: f64) -> Result<(), NmlError> {
        let valid_tuple: (bool, bool) = (row < self.num_rows, col < self.num_cols);
        match valid_tuple {
            (false, _) => Err(NmlError::new(InvalidRows)),
            (_, false) => Err(NmlError::new(InvalidCols)),
            (true, true) => {
                self.data[(row * self.num_cols + col) as usize] = data;
                Ok(())
            },
        }
    }
    /// Method sets the values of all cells ro a given value
    pub fn set_all_values(self: &mut Self, value: f64) {
        for i in 0..self.num_rows {
            for j in 0..self.num_cols {
                self.data[(i * self.num_cols + j) as usize] = value;
            }
        }
    }
    ///checks if the matrix is square and sets the diagonal values to a given value
    pub fn set_dig_values(self: &mut Self, value: f64) -> Result<(), NmlError> {
        if self.is_square == true {
            for i in 0..self.num_rows {
                self.data[(i * self.num_cols + i) as usize] = value;
            }
        }
        match self.is_square {
            true => Ok(()),
            false => Err(NmlError::new(ErrorKind::MatrixNotSquare)),
        }
    }
    ///multiplies a given row with a given scalar in place. If the row-index is not in the matrix the returned Result will contain an error
    pub fn multiply_row_scalar(self: &mut Self, row: u32, scalar: f64) -> Result<(), NmlError> {
        match row < self.num_rows {
            false => Err(NmlError::new(InvalidRows)),
            true => {
                for i in 0..self.num_cols {
                    self.data[(row * self.num_cols + i) as usize] *= scalar;
                }
                Ok(())
            },
        }
    }
    ///multiplies a given column with a given scalar in place. If the row-index is not in the matrix the returned Result will contain an error
    pub fn multiply_col_scalar(self: &mut Self, col: u32, scalar: f64) -> Result<(), NmlError> {
        match col < self.num_cols {
            false => Err(NmlError::new(InvalidCols)),
            true => {
                for i in 0..self.num_rows {
                    self.data[(i * self.num_cols + col) as usize] *= scalar;
                }
                Ok(())
            }
        }
    }
    ///multiplies the matrix in place with a given scalar
    pub fn multiply_matrix_scalar(self: &mut Self, scalar: f64) {
        for i in 0..self.data.len() {
            self.data[i] *= scalar;
        }
    }

    /// row_1 ist multiplied with scalar_1, this is analog for 2. row_1 will be modified with the solution (row_1 = row_1 * scalar + row_2 * scalar_2)
    pub fn add_rows(self: &mut Self, row_1: u32, scalar_1: f64, row_2: u32, scalar_2: f64) -> Result<(), NmlError> {
        match row_1 < self.num_rows && row_2 < self.num_rows {
            false => Err(NmlError::new(InvalidRows)),
            true => {
                for i in 0..self.num_cols {
                    let value = self.data[(row_1 * self.num_cols + i) as usize];
                    self.data[(row_1 * self.num_cols + i) as usize] = value * scalar_1 + self.data[(row_2 * self.num_cols + i) as usize] * scalar_2;
                }
                Ok(())
            }
        }
    }
    ///Method that swaps two given rows of a matrix object in place. Returns either nothing or an NmlError if the specified rows are not in the matrix
    pub fn swap_rows(self: &mut Self, row_1: u32, row_2: u32) -> Result<(), NmlError> {
        match row_1 < self.num_rows && row_2 < self.num_rows {
            false => Err(NmlError::new(InvalidRows)),
            true => {
                for i in 0..self.num_cols {
                    let temp = self.data[(row_1 * self.num_cols + i) as usize];
                    self.data[(row_1 * self.num_cols + i) as usize] = self.data[(row_2 * self.num_cols + i) as usize];
                    self.data[(row_2 * self.num_cols + i) as usize] = temp;
                }
                Ok(())
            }
        }
    }
    ///Method that swaps two given rows of a matrix object in place. Returns either nothing or an NmlError if the specified rows are not in the matrix
    pub fn swap_columns(self: &mut Self, col_1: u32, col_2: u32) -> Result<(), NmlError> {
        match col_1 < self.num_cols && col_2 < self.num_cols {
            false => Err(NmlError::new(InvalidCols)),
            true => {
                for i in 0..self.num_rows {
                    let temp = self.data[(i * self.num_cols + col_1) as usize];
                    self.data[(i * self.num_cols + col_1) as usize] = self.data[(i * self.num_cols + col_2) as usize];
                    self.data[(i * self.num_cols + col_2) as usize] = temp;
                }
                Ok(())
            }
        }
    }
    ///Tries to remove a column of a matrix and returns the rest of the matrix as a now on. Does not move the original matrix.
    ///If the column is not in the original matrix the result will return an error
    pub fn remove_column(self: &Self, col: u32) -> Result<Self, NmlError> {
        match col < self.num_cols {
            false => Err(NmlError::new(InvalidCols)),
            true => {
                let mut data: Vec<f64> = Vec::with_capacity(self.data.len());
                let indexes: Vec<usize> = (col as usize..self.data.len()).step_by(self.num_cols as usize).collect();
                for i in 0..self.data.len() {
                    if !indexes.contains(&i) {
                        data.push(self.data[i]);
                    }
                }
                Ok(
                    Self {
                        num_cols: 1,
                        num_rows: self.num_rows,
                        data,
                        is_square: false
                    }
                )
            }
        }
    }
    ///Tries to remove a column of a matrix and returns the rest of the matrix as a now on. Does not move the original matrix.
    ///If the column is not in the original matrix the result will return an error
    pub fn remove_row(self: &Self, row: u32) -> Result<Self, NmlError> {
        match row < self.num_rows {
            false => Err(NmlError::new(InvalidRows)),
            true => {
                let data: Vec<f64> = self.data[((row + 1) * self.num_cols) as usize..self.data.len()].to_vec();
                Ok(Self {
                    num_cols: self.num_cols,
                    num_rows: 1,
                    data,
                    is_square: false,
                })
            }
        }
    }

    pub fn get_sub_mtr(self: &Self, row_start: u32, row_end: u32, col_start: u32, col_end: u32) -> Result<Self, NmlError> {
        match row_start < self.num_rows && row_end < self.num_rows && col_start < self.num_cols && col_end < self.num_cols {
            false => Err(NmlError::new(InvalidRows)),
            true => {
                let mut data: Vec<f64> = Vec::new();
                for i in row_start - 1..row_end {
                    for j in col_start - 1..col_end {
                        data.push(self.data[(i * self.num_cols + j) as usize]);
                    }
                }
                Ok(Self {
                    num_rows: row_end - row_start,
                    num_cols: col_end - col_start,
                    data,
                    is_square: false,
                })
            }
        }
    }

    ///Computes the transpose matrix b' of a matrix b. This is achieved by going from row-major-ordering to a more efficient storage. The input matrix will not be modified or moved.
    pub fn transpose(self: &Self) -> Self{
        let mut data: Vec<f64> = Vec::with_capacity(self.data.len());
        for i in 0..self.num_cols {
            for j in 0..self.num_rows {
                data.push(self.data[(i + j*self.num_cols) as usize]);
            }
        }
        Self {
            num_rows: self.num_cols,
            num_cols: self.num_rows,
            data,
            is_square: self.is_square,
        }
    }
    ///The naive matrix multiplication algorihm applied with the transponse of the one matrix
    pub fn mul_transpose(self: &Self, other: &Self) -> Result<Self, NmlError> {
        match self.num_cols == other.num_rows {
            false => Err(NmlError::new(InvalidCols)),
            true => {
                let m: u32 = self.num_rows;
                let n: u32 = self.num_cols;
                let p: u32 = other.num_cols;
                let transpose: NmlMatrix = other.transpose();
                let mut data: Vec<f64> = Vec::new();
                for i in 0..m {
                    for j in 0..p {
                        data.insert((i * p + j) as usize, 0.0);
                        for k in 0..n {
                            data[(i*p+j) as usize] += self.data[(i * n + k) as usize] * transpose.data[(p * k + j) as usize];
                        }
                    }
                }
                Ok(Self{
                    num_rows: self.num_rows,
                    num_cols: other.num_cols,
                    data,
                    is_square: self.num_rows == other.num_cols
                })
            }
        }
    }
    ///The naive matrix multiplication algorithm. It iterates trough all values of both matrices. These matrices are not moved or modified
    pub fn mul_naive(self: &Self, other: &Self) -> Result<Self,NmlError> {
        let m = self.num_rows;
        let n_1 = self.num_cols;
        let n_2 = other.num_rows;
        let p = other.num_cols;
        match n_1 == n_2 {
            false => {Err(NmlError::new(CreateMatrix))},
            true => {
                let mut data = Vec::with_capacity((m*p) as usize);
                for i in 0..m {
                    for j in 0..p {
                        data.insert((i * p + j) as usize, 0.0);
                        for k in 0..n_1 {
                            data[(i*p+j) as usize] += self.data[(i * n_1 + k) as usize] * other.data[(p * k + j) as usize];
                        }
                    }
                }
                Ok(Self{
                    num_rows: m,
                    num_cols: p,
                    data,
                    is_square: m == p,
                })
            }
        }
    }

}
impl Sub for NmlMatrix{
    type Output = Result<Self, NmlError>;

    fn sub(self, rhs: Self) -> Self::Output {
        match self.num_rows == rhs.num_rows && self.num_cols == rhs.num_cols {
            false => Err(NmlError::new(CreateMatrix)),
            true => {
                let mut data: Vec<f64> = Vec::new();
                for i in 0..self.data.len() -1 {
                    data.push(self.data[i] - rhs.data[i]);
                }
                Ok(Self{
                    num_rows: self.num_rows,
                    num_cols: self.num_cols,
                    data,
                    is_square: self.is_square
                })
            }
        }
    }
}

impl Sub for &NmlMatrix {
    type Output = Result<NmlMatrix, NmlError>;

    fn sub(self, rhs: Self) -> Self::Output {
        match self.num_rows == rhs.num_rows && self.num_cols == rhs.num_cols {
            false => Err(NmlError::new(CreateMatrix)),
            true => {
                let mut data: Vec<f64> = Vec::new();
                for i in 0..self.data.len() -1 {
                    data.push(self.data[i] - rhs.data[i]);
                }
                Ok(NmlMatrix{
                    num_rows: self.num_rows,
                    num_cols: self.num_cols,
                    data,
                    is_square: self.is_square
                })
            }
        }
    }
}

impl Display for NmlMatrix {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut output = String::new();
        for i in 0..self.num_rows {
            for j in 0..self.num_cols {
                output.push_str(&self.data[(i * self.num_cols + j) as usize].to_string());
                output.push_str(" ");
            }
            output.push_str("\n");
        }
        write!(f, "{}", output)
    }
}

impl Eq for NmlMatrix {}
impl PartialEq for NmlMatrix {
    fn eq(&self, other: &Self) -> bool {
        self.equality(other)
    }
}

impl Add for NmlMatrix {
    type Output = Result<Self, NmlError>;

    fn add(self, rhs: Self) -> Self::Output {
        match self.num_rows == rhs.num_rows && self.num_cols == rhs.num_cols{
            false => Err(NmlError::new(CreateMatrix)),
            true => {
                let mut data: Vec<f64> = Vec::new();
                for i in 0..self.data.len() {
                    data.push(self.data[i] + rhs.data[i]);
                }
                Ok(Self{
                    num_cols: self.num_cols,
                    num_rows: self.num_rows,
                    data,
                    is_square: self.is_square
                })
            }
        }
    }
}

impl Add for &NmlMatrix {
    type Output = Result<NmlMatrix, NmlError>;

    fn add(self, rhs: Self) -> Self::Output {
        match self.num_rows == rhs.num_rows && self.num_cols == rhs.num_cols{
            false => Err(NmlError::new(CreateMatrix)),
            true => {
                let mut data: Vec<f64> = Vec::new();
                for i in 0..self.data.len() {
                    data.push(self.data[i] + rhs.data[i]);
                }
                Ok(NmlMatrix{
                    num_cols: self.num_cols,
                    num_rows: self.num_rows,
                    data,
                    is_square: self.is_square
                })
            }
        }
    }
}

impl Mul for NmlMatrix {
    type Output = Result<NmlMatrix, NmlError>;

    fn mul(self, rhs: Self) -> Self::Output {
        return self.mul_naive(&rhs);
    }
}