moonalloy 0.3.2

The oxidized scientific computing library for the 21st century
Documentation
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! Matrix - An implementation of a mathematical Matrix
//!
//! This module contains structures and functions for manipulating matrices in Linear Algebra.
//! All of the basics of matrix arithmetic.

use crate::Array;

use std::alloc::{alloc, Layout};
use std::fmt::*;
use std::ops::{Add, Deref, DerefMut, Index, IndexMut, Mul, Neg, Sub};

/// A representation of a mathematical matrix
#[derive(Debug, Clone)]
#[repr(C)]
pub struct Matrix {
    /// Number of rows in the matrix
    rows: usize,
    /// Number of columns in the matrix
    cols: usize,
    /// Elements of the matrix as a raw pointer of Arrays
    arrays: *mut Array,
}

impl Matrix {
    /// Checks that the slice of Arrays can be converted to a valid matrix.
    ///
    /// # Arguments
    ///
    /// * `slice` - a mutable slice of Arrays
    fn is_valid_slice(slice: &mut [Array]) -> bool {
        let len = slice[0].len();
        for i in 1..slice.len() {
            assert!(len == slice[i].len());
        }

        true
    }

    /// Returns a new matrix from a mutable slice of Arrays.
    ///
    /// # Arguments
    ///
    /// * `slice` - a mutable slice of Arrays
    ///
    /// # Panics
    ///
    /// The slice must adhere to the `n`x`m` form of a matrix otherwise
    /// the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix
    /// use moonalloy::linalg::matrix::Matrix;
    /// let mat = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0)]);
    /// ```
    pub fn new(slice: &mut [Array]) -> Matrix {
        assert!(Matrix::is_valid_slice(slice));
        Matrix {
            rows: slice.len(),
            cols: slice[0].len(),
            arrays: slice.as_mut_ptr(),
        }
    }

    /// Returns a new matrix where all the elements have the same value
    ///
    /// # Arguments
    ///
    /// * `val` - the value for all the elements in the matrix.
    /// * `rows` - the number of rows in the new matrix.
    /// * `cols` - the number of columns in the new matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix where all elements have the value 3.0
    /// use moonalloy::linalg::matrix::Matrix;
    /// let mat = Matrix::of(3.0, 2, 2);
    ///
    /// assert_eq(Matrix::new(&mut [Array::from(&mut [3.0, 3.0]), Array::from(&mut [3.0, 3.0)]), mat);
    /// ```
    fn of(val: f64, rows: usize, cols: usize) -> Matrix {
        let mat_slice = unsafe {
            let layout = Layout::array::<Array>(rows).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, rows)
        };

        for i in 0..rows {
            mat_slice[i] = Array::of(val, cols);
        }

        Matrix {
            rows,
            cols,
            arrays: mat_slice.as_mut_ptr(),
        }
    }

    /// Returns a new matrix where all the elements have the value of 0.0
    ///
    /// # Arguments
    ///
    /// * `rows` - the number of rows in the new matrix.
    /// * `cols` - the number of columns in the new matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix where all elements have the value 0.0
    /// use moonalloy::linalg::matrix::Matrix;
    /// let mat = Matrix::zeros(2, 2);
    ///
    /// assert_eq(Matrix::new(&mut [Array::from(&mut [0.0, 0.0]), Array::from(&mut [0.0, 0.0)]), mat);
    /// ```
    pub fn zeros(rows: usize, cols: usize) -> Matrix {
        Matrix::of(0.0, rows, cols)
    }

    /// Returns a new matrix where all the elements have the value of 1.0
    ///
    /// # Arguments
    ///
    /// * `rows` - the number of rows in the new matrix.
    /// * `cols` - the number of columns in the new matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix where all elements have the value 1.0
    /// use moonalloy::linalg::matrix::Matrix;
    /// let mat = Matrix::ones(2, 2);
    ///
    /// assert_eq(Matrix::new(&mut [Array::from(&mut [1.0, 1.0]), Array::from(&mut [1.0, 1.0)]), mat);
    /// ```
    pub fn ones(rows: usize, cols: usize) -> Matrix {
        Matrix::of(1.0, rows, cols)
    }

    /// Returns an `n`x`n` identity matrix.
    ///
    /// # Arguments
    ///
    /// * `len` - the rank of the identity matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 identity matrix
    /// use moonalloy::linalg::matrix::Matrix;
    /// let mat = Matrix::identity(2);
    ///
    /// assert_eq(Matrix::new(&mut [Array::from(&mut [1.0, 0.0]), Array::from(&mut [0.0, 1.0)]), mat);
    /// ```
    pub fn identity(len: usize) -> Matrix {
        let mat = Matrix::zeros(len, len);

        let mat_slice = unsafe { std::slice::from_raw_parts_mut(mat.arrays, len) };

        for i in 0..len {
            let slice = &mut mat_slice[i];

            slice[i] = 1.0;
        }

        mat
    }

    /// Returns a string representation of a matrix.
    pub fn to_string(&self) -> String {
        let array_slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        let mut result = String::from("Matrix: \n[");

        for (i, arr) in array_slice.iter().enumerate() {
            let slice = arr.as_slice();

            result.push_str(format!("{:?}", slice).as_str());
            if i < arr.len() - 1 {
                result.push_str(", \n ")
            }
        }

        result.push(']');

        result
    }

    /// Adds two matrices without modifying the originals.
    ///
    /// # Arguments
    ///
    /// * `other` - the other matrix to add.
    ///
    /// # Panics
    ///
    /// The two matrices must have the same dimensions in order to add them
    /// together. If not, the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create to matrices `a` and `b` and add them.
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    /// let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
    ///
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [3.0, 5.0]), Array::from(&mut [8.0, 13.0])]), a.plus(&b));
    /// // Use the `+`-operator as a shorthand for this.
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [3.0, 5.0]), Array::from(&mut [8.0, 13.0])]), a + b);
    /// ```
    pub fn plus(&self, other: &Matrix) -> Matrix {
        assert!(
            self.cols == other.cols,
            "ERROR - Matrix addition: Columns differ in dimensions."
        );
        assert!(
            self.rows == other.rows,
            "ERROR - Matrix addition: Rows differ in dimensions."
        );

        let result = unsafe {
            let layout = Layout::array::<Array>(self.rows).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, self.rows)
        };

        let mat_slice1 = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        let mat_slice2 = unsafe { std::slice::from_raw_parts_mut(other.arrays, other.rows) };

        for i in 0..self.rows {
            result[i] = mat_slice1[i].plus(&mat_slice2[i]);
        }

        Matrix {
            rows: self.rows,
            cols: self.cols,
            arrays: result.as_mut_ptr(),
        }
    }

    /// Multiply every element in a matrix with a scalar value without modifying the original.
    ///
    /// # Arguments
    ///
    /// * `scal` - the scalar value to multiply with.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a matrix `a` and multiply all elements with -1.0.
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    ///
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [-1.0, -2.0]), Array::from(&mut [-3.0, -5.0])]), a.scalar(-1.0));
    /// // Use the unary `-`-operator as a shorthand for multiplication with -1.0.
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [-1.0, -2.0]), Array::from(&mut [-3.0, -5.0])]), -a);
    /// ```
    pub fn scalar(&self, scal: f64) -> Matrix {
        let result = unsafe {
            let layout = Layout::array::<Array>(self.rows).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, self.rows)
        };

        let slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        for i in 0..self.rows {
            result[i] = slice[i].scalar_mult(scal);
        }

        Matrix {
            rows: self.rows,
            cols: self.cols,
            arrays: result.as_mut_ptr(),
        }
    }

    /// Subtracts two matrices without modifying the originals.
    ///
    /// # Arguments
    ///
    /// * `other` - the other matrix to subtract.
    ///
    /// # Panics
    ///
    /// The two matrices must have the same dimensions in order to subtract them.
    /// If not, the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create two matrices `a` and `b` and subtract them.
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    /// let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
    ///
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [-1.0, -1.0]), Array::from(&mut [-2.0, -3.0])]), a.minus(&b));
    /// // Use the `+`-operator as a shorthand for this.
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [-1.0, -1.0]), Array::from(&mut [-2.0, -3.0])]), a - b);
    /// ```
    pub fn minus(&self, other: &Matrix) -> Matrix {
        assert!(
            self.cols == other.cols,
            "ERROR - Matrix subtraction: Columns differ in dimensions."
        );
        assert!(
            self.rows == other.rows,
            "ERROR - Matrix subtraction: Rows differ in dimensions."
        );

        let result = unsafe {
            let layout = Layout::array::<Array>(self.rows).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, self.rows)
        };

        let mat_slice1 = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        let mat_slice2 = unsafe { std::slice::from_raw_parts_mut(other.arrays, other.rows) };

        for i in 0..self.rows {
            result[i] = mat_slice1[i].minus(&mat_slice2[i]);
        }

        Matrix {
            rows: self.rows,
            cols: self.cols,
            arrays: result.as_mut_ptr(),
        }
    }

    /// Multiplies two matrices element by element without modifying the originals.
    ///
    /// # Arguments
    ///
    /// * `other` - the other matrix to perform element-wise multiplication with.
    ///
    /// # Panics
    ///
    /// The two matrices must have the same dimensions in order to multiply them.
    /// If not, the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create two matrices `a` and `b` and perform element-wise multiplication.
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    /// let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
    ///
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [2.0, 6.0]), Array::from(&mut [15.0, 40.0])]), a.elem_mult(&b));
    /// // Use the `*`-operator as a shorthand for this.
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [2.0, 6.0]), Array::from(&mut [15.0, 40.0])]), a * b);
    /// ```
    pub fn elem_mult(&self, other: &Matrix) -> Matrix {
        assert!(
            self.cols == other.cols,
            "ERROR - Matrix element-wise multiplication: Columns differ in dimensions."
        );
        assert!(
            self.rows == other.rows,
            "ERROR - Matrix element-wise multiplication: Rows differ in dimensions."
        );

        let result = unsafe {
            let layout = Layout::array::<Array>(self.rows).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, self.rows)
        };

        let mat_slice1 = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        let mat_slice2 = unsafe { std::slice::from_raw_parts_mut(other.arrays, other.rows) };

        for i in 0..self.rows {
            result[i] = mat_slice1[i].mult(&mat_slice2[i]);
        }

        Matrix {
            rows: self.rows,
            cols: self.cols,
            arrays: result.as_mut_ptr(),
        }
    }

    /// Returns a transpose of a matrix without modifying the original.
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a matrix `a` and transpose it.
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    ///
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [1.0, 3.0]), Array::from(&mut [2.0, 5.0])]), a.transpose());
    /// ```
    pub fn transpose(&self) -> Matrix {
        let result = unsafe {
            let layout = Layout::array::<Array>(self.cols).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, self.cols)
        };

        let arr_slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        for i in 0..self.cols {
            result[i] = Array::zeros(self.rows);

            for j in 0..self.rows {
                result[i].set(arr_slice[j].get(i), j);
            }
        }

        Matrix {
            rows: self.cols,
            cols: self.rows,
            arrays: result.as_mut_ptr(),
        }
    }

    /// Perform matrix multiplication on two matrices
    ///
    /// # Arguments
    ///
    /// * `other` - the other matrix to multiply with.
    ///
    /// # Panics
    ///
    /// For matrix multiplication of two matrices, A and B,
    /// A must have the dimensions `n`x`m` and B must have the dimensions `r`x`n`
    /// in order for the multiplication to be valid.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create two matrices `a` and `b` and multiply them.
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    /// let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
    ///
    /// assert_eq!(Matrix::new(&mut [Array::from(&mut [10.0, 19.0]), Array::from(&mut [21.0, 50.0])]), a.mult(&b));
    /// ```
    pub fn mult(&self, other: &Matrix) -> Matrix {
        assert!(
            self.rows == other.cols,
            "ERROR - Matrix multiplication: Invalid dimensions."
        );

        let result = unsafe {
            let layout = Layout::array::<Array>(self.cols).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut Array, self.cols)
        };

        let mat_t = self.transpose();

        let mat_slice1 = unsafe { std::slice::from_raw_parts_mut(mat_t.arrays, mat_t.rows) };

        let mat_slice2 = unsafe { std::slice::from_raw_parts_mut(other.arrays, other.rows) };

        for i in 0..self.cols {
            result[i] = Array::zeros(other.rows);

            for j in 0..other.rows {
                result[i].set(mat_slice1[j].dotp(&mat_slice2[i]), j);
            }
        }

        Matrix {
            rows: other.rows,
            cols: self.cols,
            arrays: result.as_mut_ptr(),
        }
    }

    /// Returns the element at the index (i,j)
    ///
    /// # Arguments
    ///
    /// * `i` - the i-th row index.
    /// * `j` - the j-th column index.
    ///
    /// # Panics
    ///
    /// If any of the argument indexes go beyond the dimensions of the matrix
    /// the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix and get the value at (1,0)
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    ///
    /// assert_eq!(3.0, a.get(1, 0));
    /// // Use the `[]`-operator twice as a shorthand for indexing.
    /// assert_eq!(3.0, a[1][0]);
    /// ```
    pub fn get(&self, i: usize, j: usize) -> f64 {
        let slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        slice[i].get(j)
    }

    /// Returns a subsection of a row in the matrix as an Array without modifying the matrix itself.
    ///
    /// # Arguments
    ///
    /// * `row` - the row of the matrix to splice.
    /// * `first` - the first index of the Array to splice from.
    /// * `last` - the last (exclusive) index of the Array to splice from.
    ///
    /// # Panics
    ///
    /// The first index must be strictly smaller than the last index. Also the indexes must be
    /// within the bounds of the underlying Array. Otherwise the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix and get a copy of the second row with `splice()`
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    ///
    /// assert_eq!(Array::from(&mut [3.0, 5.0]), a.splice(1, 0, 2));
    /// ```
    pub fn splice(&self, row: usize, first: usize, last: usize) -> Array {
        assert!(
            first < last,
            "ERROR - matrix splice: first index must be smaller than last index."
        );
        let slice = unsafe {
            let layout = Layout::array::<f64>(last - first).unwrap();
            let ptr = alloc(layout);
            std::slice::from_raw_parts_mut(ptr as *mut f64, last - first)
        };

        for col in first..last {
            slice[(col - first) as usize] = self.get(row, col);
        }

        Array::from(slice)
    }

    /// Changes the element at the index (i,j).
    ///
    /// # Arguments
    ///
    /// * `val` - the new value for the element to be changed.
    /// * `i` - the i-th row index.
    /// * `j` - the j-th column index.
    ///
    /// # Panics
    ///
    /// If any of the argument indexes go beyond the dimensions of the matrix
    /// the code will panic.
    ///
    /// # Examples
    ///
    /// ```
    /// // Create a 2x2 matrix and set the value at (1,0) to 8.0
    /// let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
    ///
    /// a.set(8.0, 1, 0);
    /// // use the `[]`-operator twice as a shorthand for indexing
    /// // a[1][0] = 8.0;
    /// assert_eq!(8.0, a.get(1, 0));
    /// ```
    pub fn set(&mut self, val: f64, i: usize, j: usize) {
        let slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        slice[i].set(val, j);
    }

    pub fn set_row(&mut self, arr: Array, row: usize) {
        let mut offset: usize = 0;
        if arr.len() < self.cols {
            offset = self.cols - arr.len();
        }

        for elem in 0..arr.len() {
            self.set(arr.get(elem), row, elem + offset);
        }
    }

    /// Returns the dimensions of a matrix in the form of a tuple.
    pub fn dimensions(&self) -> (usize, usize) {
        (self.rows, self.cols)
    }

    /// Returns a raw mutable pointer of the elements in a matrix.
    pub fn to_raw(mat: Matrix) -> *mut Matrix {
        Box::into_raw(Box::new(mat))
    }
}

impl Display for Matrix {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(f, "{}", self.to_string())
    }
}

impl PartialEq for Matrix {
    fn eq(&self, other: &Self) -> bool {
        if self.rows != other.rows {
            return false;
        }

        if self.cols != other.cols {
            return false;
        }

        let slice1 = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };

        let slice2 = unsafe { std::slice::from_raw_parts_mut(other.arrays, other.rows) };

        for i in 0..self.rows {
            if slice1[i] != slice2[i] {
                return false;
            }
        }

        true
    }
}

impl Deref for Matrix {
    type Target = [Array];
    fn deref(&self) -> &[Array] {
        unsafe { std::slice::from_raw_parts(self.arrays, self.rows) }
    }
}

impl DerefMut for Matrix {
    fn deref_mut(&mut self) -> &mut [Array] {
        unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) }
    }
}

impl Index<usize> for Matrix {
    type Output = Array;

    fn index(&self, i: usize) -> &Self::Output {
        assert!(i < self.rows, "ERROR - Matrix: Index out of bounds.");
        let slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };
        &slice[i]
    }
}

impl IndexMut<usize> for Matrix {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        assert!(index < self.rows, "ERROR - Matrix: Index out of bounds.");
        let slice = unsafe { std::slice::from_raw_parts_mut(self.arrays, self.rows) };
        &mut slice[index]
    }
}

impl Add for Matrix {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        self.plus(&other)
    }
}

impl Sub for Matrix {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        self.minus(&other)
    }
}

impl Mul for Matrix {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        self.elem_mult(&other)
    }
}

impl Neg for Matrix {
    type Output = Self;
    fn neg(self) -> Self::Output {
        self.scalar(-1.0)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn index() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);

        assert_eq!(3.0, a[1][0]);
    }

    #[test]
    #[should_panic]
    fn index_out_of_bounds_rows() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);

        a[2][1];
    }

    #[test]
    #[should_panic]
    fn index_out_of_bounds_columns() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);

        a[1][2];
    }

    #[test]
    fn zeros() {
        let z = Matrix::zeros(2, 2);
        let r = Matrix::new(&mut [Array::from(&mut [0.0, 0.0]), Array::from(&mut [0.0, 0.0])]);

        assert_eq!(r, z);
    }

    #[test]
    fn ones() {
        let o = Matrix::ones(2, 2);
        let r = Matrix::new(&mut [Array::from(&mut [1.0, 1.0]), Array::from(&mut [1.0, 1.0])]);

        assert_eq!(r, o);
    }

    #[test]
    fn identity() {
        let i = Matrix::identity(2);
        let r = Matrix::new(&mut [Array::from(&mut [1.0, 0.0]), Array::from(&mut [0.0, 1.0])]);

        assert_eq!(r, i);
    }

    #[test]
    fn add() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
        let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
        let r = Matrix::new(&mut [Array::from(&mut [3.0, 5.0]), Array::from(&mut [8.0, 13.0])]);
        assert_eq!(r, a + b);
    }

    #[test]
    fn sub() {
        let a = Matrix::new(&mut [Array::from(&mut [3.0, 5.0]), Array::from(&mut [8.0, 13.0])]);
        let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
        let r = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
        assert_eq!(r, a - b);
    }

    #[test]
    fn scalar() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
        let r = Matrix::new(&mut [Array::from(&mut [2.0, 4.0]), Array::from(&mut [6.0, 10.0])]);

        assert_eq!(r, a.scalar(2.0));
    }

    #[test]
    fn neg() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 5.0])]);
        let r = Matrix::new(&mut [
            Array::from(&mut [-1.0, -2.0]),
            Array::from(&mut [-3.0, -5.0]),
        ]);

        assert_eq!(r, -a);
    }

    #[test]
    fn elem_mult() {
        let a = Matrix::new(&mut [Array::from(&mut [3.0, 5.0]), Array::from(&mut [8.0, 13.0])]);
        let b = Matrix::new(&mut [Array::from(&mut [2.0, 3.0]), Array::from(&mut [5.0, 8.0])]);
        let r = Matrix::new(&mut [
            Array::from(&mut [6.0, 15.0]),
            Array::from(&mut [40.0, 104.0]),
        ]);
        assert_eq!(r, a * b);
    }

    #[test]
    fn mult() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 4.0])]);
        let r = Matrix::new(&mut [
            Array::from(&mut [7.0, 10.0]),
            Array::from(&mut [15.0, 22.0]),
        ]);
        assert_eq!(r, a.mult(&a));
    }

    #[test]
    fn transpose() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 4.0])]);
        let r = Matrix::new(&mut [Array::from(&mut [1.0, 3.0]), Array::from(&mut [2.0, 4.0])]);
        assert_eq!(r, a.transpose());
    }

    #[test]
    fn get() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 4.0])]);
        assert_eq!(3.0, a.get(1, 0));
    }

    #[test]
    fn set() {
        let mut a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 4.0])]);
        let r = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 8.0])]);

        a.set(8.0, 1, 1);

        assert_eq!(r, a);
    }

    #[test]
    fn iterator() {
        let a = Matrix::new(&mut [Array::from(&mut [1.0, 2.0]), Array::from(&mut [3.0, 4.0])]);
        let first = Array::from(&mut [1.0, 2.0]);
        let second = Array::from(&mut [3.0, 4.0]);

        let mut it = a.iter();

        assert_eq!(it.next(), Some(first).as_ref());
        assert_eq!(it.next(), Some(second).as_ref());
    }
}