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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
//! module for the dense matrix type

use crate::matrix::{Concatenate, IntoTranspose, IntoTransposeMut, Matrix, MatrixError, RowOps};
use crate::numerics::Numeric;
use crate::utilities::ApproxEq;
use crate::MatrixT;
use std::ops::{Index, IndexMut, Mul, MulAssign};

use std::borrow::Cow;
use std::fmt::{Debug, Display};

/// a dense matrix stores all the values of the matrix
/// a matrix is a vec with dimensional properties (m x n)
/// m the vertical length (rows)
/// n represents the horizontal length (columns)
/// it is stored as a row-major vector similarly to C
/// The matrix uses zero referencing
/// A dense matrix elements can be referenced by element
/// # indexing elements:
/// ```
/// # use numb_rs::{mat, Dense};
/// # fn main() {
/// let a = mat![
///     0, 1, 2;
///     3, 4, 5
/// ];
///
/// assert_eq!(a[[1, 2]], 5); // Direct access to element at 1, 2
/// assert_eq!(a[1], [3, 4, 5]); // access to row 1
/// assert_eq!(a[1][2], 5); // access to element 2 in row 1
/// # }
/// ```
/// # mutable indexing:
/// ```
/// # use numb_rs::{mat, Dense};
/// # fn main() {
/// let mut a = mat![
///     0, 1, 2;
///     3, 4, 5
/// ];
///
/// a[[1, 0]] = 6;
/// a[0][1] = -9;
///
/// assert_eq!(a[[1, 0]], 6);
/// assert_eq!(a[0], [0, -9, 2]);
/// assert_eq!(a[0][1], -9);
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Dense<T: Numeric> {
    /// a vector containing the Matrix data
    pub data: Vec<T>,
    /// number of rows
    pub m: usize,
    /// number of columns
    pub n: usize,
}

impl<T: Numeric> Matrix for Dense<T> {
    type Element = T;

    fn len(&self) -> usize {
        self.data.len()
    }

    fn size(&self) -> [usize; 2] {
        [self.m, self.n]
    }

    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    fn into_vec(self) -> Vec<T> {
        self.data
    }
}

impl<T: Numeric> Default for Dense<T> {
    fn default() -> Self {
        Dense {
            data: Vec::new(),
            m: 1,
            n: 0,
        }
    }
}

impl<T: Numeric> Index<[usize; 2]> for Dense<T> {
    type Output = T;
    /// takes i, j returns the element
    fn index(&self, idx: [usize; 2]) -> &T {
        &self.data[idx[1] + idx[0] * self.n]
    }
}

impl<T: Numeric> Index<usize> for Dense<T> {
    type Output = [T];

    fn index(&self, index: usize) -> &Self::Output {
        let a = self.n * index;
        &self.data[a..a + self.n]
    }
}

impl<T: Numeric> IndexMut<[usize; 2]> for Dense<T> {
    /// takes i, j returns a mutable reference
    fn index_mut(&mut self, idx: [usize; 2]) -> &mut T {
        &mut self.data[idx[1] + idx[0] * self.n]
    }
}

impl<T: Numeric> IndexMut<usize> for Dense<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        let a = self.n * index;
        &mut self.data[a..a + self.n]
    }
}

impl<T: Numeric> Display for Dense<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        // closure to format each element
        let precision = f.precision().unwrap_or(2);
        let format = |x: &T| format!("{:.*}", precision, x);

        // first run through to find the max length of each formatted element
        // elements are stored in a vec as we go
        let mut strings: Vec<String> = vec![];
        let max = self.data.iter().fold(0, |max: usize, x: &T| {
            let s = format(x);
            let disp_len = s.len();
            strings.push(s);
            if max > disp_len {
                max
            } else {
                disp_len
            }
        }) + 2;

        // iterate through the stored vector folding each formatted element into a final string
        // also adding a new line when each element divides evenly into the number of rows
        let string = strings
            .iter()
            .enumerate()
            .fold("".to_string(), |mut s, (i, x)| {
                if i % self.n == 0 && i != 0 {
                    s.push('\n')
                }
                format!("{}{:>width$}", s, x, width = max)
            });

        write!(f, "{}", string)
    }
}

impl<T: Numeric> Dense<T> {
    /// returns an empty matrix
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new matrix with a specific vector capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Dense {
            data: Vec::with_capacity(capacity),
            ..Dense::default()
        }
    }

    /// method for creating a Dense matrix column from a vector
    pub fn col_from_vec(v: Vec<T>) -> Self {
        let len = v.len();

        Dense {
            data: v,
            m: len,
            n: 1,
        }
    }

    /// Very efficient way to transpose a single dimension matrix
    pub fn swap_mn(&mut self) {
        unsafe { std::ptr::swap(&mut self.m, &mut self.n) }
    }

    /// returns an identity matrix
    /// Will require a type
    pub fn eye(size: usize) -> Dense<T>
    where
        T: std::convert::From<u32>,
    {
        let mut v: Vec<T> = Vec::new();
        for i in 0..size {
            for j in 0..size {
                if i == j {
                    v.push(1.into());
                } else {
                    v.push(0.into());
                }
            }
        }
        Dense::<T> {
            data: v,
            m: size,
            n: size,
        }
    }

    /// swaps two elements in the vector
    /// This method only swaps the pointers similar to the vector implementation
    #[inline]
    fn swap(&mut self, a: [usize; 2], b: [usize; 2]) {
        unsafe {
            let pa: *mut T = &mut self[a];
            let pb: *mut T = &mut self[b];
            std::ptr::swap(pa, pb)
        }
    }

    /// this method returns self wrapped in a MatrixT struct to indicate that methods should index
    /// the transpose of the struct it does not perform any matrix
    #[deprecated]
    pub fn t_old(&self) -> MatrixT<T> {
        MatrixT {
            /// a reference to the vector of the Matrix below
            data: &self.data,
            /// m is a reference to the 'n' column of the Matrix
            m: &self.n,
            /// n is a reference to the 'm' column of the Matrix
            n: &self.m,
        }
    }

    pub fn concatenate_vec(self, other: &[T]) -> Result<Dense<T>, MatrixError> {
        match self.m == other.len() {
            true => {
                let mut new: Dense<T> = Dense::with_capacity(self.data.capacity() + other.len());

                new.n = self.n + 1;
                new.m = self.m;

                for i in 0..self.m {
                    for j in 0..self.n {
                        new.data.push(self[[i, j]]);
                    }
                    new.data.push(other[i])
                }
                Ok(new)
            }
            false => Err(MatrixError::Incompatibility),
        }
    }
}

impl<T: Numeric> std::convert::From<Vec<T>> for Dense<T> {
    fn from(data: Vec<T>) -> Self {
        let n = data.len();
        Dense { data, m: 1, n }
    }
}

pub trait IntoCol<T: Numeric> {
    fn into_col(self) -> Dense<T>;
}

impl<T: Numeric> IntoCol<T> for Vec<T> {
    fn into_col(self) -> Dense<T> {
        Dense::col_from_vec(self)
    }
}

impl<T: Numeric> RowOps<T> for Dense<T> {
    /// Scales all elements in a given row
    fn scale_row(&mut self, i: usize, scale: T) {
        for j in 0..self.n {
            self[[i, j]] *= scale;
        }
    }

    /// adds one row to another with a scaling factor
    fn add_rows(&mut self, base: usize, row_to_add: usize, scale: T) {
        for j in 0..self.n {
            let x = self[[row_to_add, j]] * scale;
            self[[base, j]] += x;
        }
    }

    /// swaps rows a and b
    fn swap_rows(&mut self, a: usize, b: usize) {
        assert!(a < self.m && b < self.m);
        for (j, _) in (0..self.n).enumerate() {
            self.swap([a, j], [b, j])
        }
    }
}

impl<T: Numeric> Concatenate<Dense<T>, T> for Dense<T> {
    fn concatenate(self, other: Dense<T>) -> Result<Dense<T>, MatrixError> {
        // check that matrices are compatible
        match self.m == other.m {
            true => {
                // create a matrix with a capacity
                let mut new: Dense<T> =
                    Dense::with_capacity(self.data.capacity() + other.data.capacity());
                new.n = self.n + other.n;
                new.m = self.m;
                // TODO: Vectorise this loop

                // if we think of appending to a vector instead of a 2d array we might consider
                // that we wish to add a row starting at 'i' in the vector and push values
                // onto the new array

                for i in 0..self.m {
                    for j in 0..self.n {
                        new.data.push(self[[i, j]]);
                    }
                    for j in 0..other.n {
                        new.data.push(other[[i, j]])
                    }
                }

                Ok(new)
            }
            false => Err(MatrixError::Incompatibility),
        }
    }
}

pub struct DenseIntoIterator<'a, T: Numeric> {
    matrix: &'a Dense<T>,
    i: usize,
}

impl<'a, T: Numeric> IntoIterator for &'a Dense<T> {
    type Item = &'a [T];
    type IntoIter = DenseIntoIterator<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        DenseIntoIterator { matrix: self, i: 0 }
    }
}

impl<'a, T: Numeric> Iterator for DenseIntoIterator<'a, T> {
    type Item = &'a [T];

    fn next(&mut self) -> Option<Self::Item> {
        if self.i < self.matrix.m {
            let out = &self.matrix[self.i];
            self.i += 1;
            Some(out)
        } else {
            None
        }
    }
}

/// multiplying a Matrix by a scalar of the same type
impl<T: Numeric> Mul<T> for Dense<T> {
    type Output = Self;

    fn mul(self, scalar: T) -> Self {
        let v: Vec<T> = self.data.into_iter().map(|x| x * scalar).collect();

        Dense { data: v, ..self }
    }
}

impl<T: Numeric> MulAssign<T> for Dense<T> {
    fn mul_assign(&mut self, scalar: T) {
        self.data.iter_mut().for_each(|x| *x *= scalar)
    }
}

/// Matrix multiplication returns the dot product
/// The matrices must have dimensions such that mn * nk = mk
/// This is a naive solution, there are more efficient computational methods tbd later
impl<T: Numeric> Mul<Dense<T>> for Dense<T> {
    type Output = Result<Self, MatrixError>;

    fn mul(self, other: Self) -> Self::Output {
        if self.n != other.m {
            Err(MatrixError::Incompatibility)
        } else {
            let mut out: Dense<T> = Dense::with_capacity(self.m * other.n);
            out.m = self.m;
            out.n = other.n;

            unsafe {
                out.data.set_len(out.m * out.n);
            }

            for i in 0..out.m {
                for j in 0..out.n {
                    out[[i, j]] = T::ZERO;
                    for k in 0..self.n {
                        out[[i, j]] += self[[i, k]] * other[[k, j]]
                    }
                }
            }
            Ok(out)
        }
    }
}

/// TODO: Create a Macro for this
/// There has to be an elegent way to do this, asking on SO will certainly flag duplicate questions
/// so why even bother.
/// - Macros don't really help (about same amount of code)
/// - Can't use Borrow trait as it's a standard op and it always takes by value
/// - Cow runs into a lot of errors
/// 'type parameter `M` must be covered by another type when it appears before the first local type (`Dense<f64>`)'
/// A DOT trait would be sensible?
impl<T: Numeric> Mul<&Dense<T>> for Dense<T> {
    type Output = Result<Self, MatrixError>;

    fn mul(self, other: &Self) -> Self::Output {
        if self.n != other.m {
            Err(MatrixError::Incompatibility)
        } else {
            let mut out: Dense<T> = Dense::with_capacity(self.m * other.n);
            out.m = self.m;
            out.n = other.n;

            unsafe {
                out.data.set_len(out.m * out.n);
            }

            for i in 0..out.m {
                for j in 0..out.n {
                    out[[i, j]] = T::ZERO;
                    for k in 0..self.n {
                        out[[i, j]] += self[[i, k]] * other[[k, j]]
                    }
                }
            }
            Ok(out)
        }
    }
}

impl<T: Numeric> Mul<Dense<T>> for &Dense<T> {
    type Output = Result<Dense<T>, MatrixError>;

    fn mul(self, other: Dense<T>) -> Self::Output {
        if self.n != other.m {
            Err(MatrixError::Incompatibility)
        } else {
            let mut out: Dense<T> = Dense::with_capacity(self.m * other.n);
            out.m = self.m;
            out.n = other.n;

            unsafe {
                out.data.set_len(out.m * out.n);
            }

            for i in 0..out.m {
                for j in 0..out.n {
                    out[[i, j]] = T::ZERO;
                    for k in 0..self.n {
                        out[[i, j]] += self[[i, k]] * other[[k, j]]
                    }
                }
            }
            Ok(out)
        }
    }
}

impl<T: Numeric> Mul<&Dense<T>> for &Dense<T> {
    type Output = Result<Dense<T>, MatrixError>;

    fn mul(self, other: &Dense<T>) -> Self::Output {
        if self.n != other.m {
            Err(MatrixError::Incompatibility)
        } else {
            let mut out: Dense<T> = Dense::with_capacity(self.m * other.n);
            out.m = self.m;
            out.n = other.n;

            unsafe {
                out.data.set_len(out.m * out.n);
            }

            for i in 0..out.m {
                for j in 0..out.n {
                    out[[i, j]] = T::ZERO;
                    for k in 0..self.n {
                        out[[i, j]] += self[[i, k]] * other[[k, j]]
                    }
                }
            }
            Ok(out)
        }
    }
}

impl<'a, T: Numeric> From<Dense<T>> for Cow<'a, Dense<T>> {
    fn from(m: Dense<T>) -> Self {
        Cow::Owned(m)
    }
}

impl<'a, T: Numeric> From<&'a Dense<T>> for Cow<'a, Dense<T>> {
    fn from(m: &'a Dense<T>) -> Self {
        Cow::Borrowed(m)
    }
}

impl ApproxEq<Dense<f64>> for Dense<f64> {
    type Check = f64;

    fn approx_eq(&self, other: &Dense<f64>, tolerance: Self::Check) -> bool {
        if self.m != other.m || self.n != other.n {
            return false;
        }

        for i in 0..self.m {
            for j in 0..self.n {
                if (self[[i, j]] - other[[i, j]]).abs() > tolerance {
                    return false;
                }
            }
        }
        true
    }

    fn assert_approx_eq(&self, other: &Dense<f64>, tolerance: Self::Check) {
        if self.m != other.m || self.n != other.n {
            panic!(
                r#"assertion failed: Dimension Inequality
    left  m x n: `{:?}`x`{:?}`
    right m x n: `{:?}`x`{:?}`"#,
                self.m, other.m, self.n, other.n
            )
        }

        for i in 0..self.m {
            for j in 0..self.n {
                let delta = (self[[i, j]] - other[[i, j]]).abs();
                if delta > tolerance {
                    panic!(
                        r#"assertion failed at element [{:?}, {:?}]: ± `{:?}`
    left: `{:?}`
    right: `{:?}`
    delta = `{:?}`"#,
                        i,
                        j,
                        tolerance,
                        self[[i, j]],
                        other[[i, j]],
                        delta
                    );
                }
            }
        }
    }
}

#[derive(Debug)]
pub struct DenseTranspose<'a, T: Numeric + 'a> {
    inner: &'a Dense<T>,
    m: usize,
    n: usize,
}

#[derive(Debug)]
pub struct DenseTransposeMut<'a, T: Numeric + 'a> {
    inner: &'a mut Dense<T>,
    m: usize,
    n: usize,
}

impl<'a, T: Numeric + 'a> IntoTranspose<'a> for Dense<T> {
    type TransposeView = DenseTranspose<'a, T>;

    fn t(&'a self) -> Self::TransposeView {
        DenseTranspose {
            inner: self,
            m: self.n,
            n: self.m,
        }
    }
}

impl<'a, T: Numeric + 'a> IntoTransposeMut<'a> for Dense<T> {
    type TransposeViewMut = DenseTransposeMut<'a, T>;

    fn t_mut(&'a mut self) -> Self::TransposeViewMut {
        let n = self.n;
        let m = self.m;

        DenseTransposeMut { inner: self, m, n }
    }
}

impl<T: Numeric> Index<[usize; 2]> for DenseTranspose<'_, T> {
    type Output = T;

    fn index(&self, index: [usize; 2]) -> &Self::Output {
        &self.inner[[index[1], index[0]]]
    }
}

impl<T: Numeric> Index<[usize; 2]> for DenseTransposeMut<'_, T> {
    type Output = T;

    fn index(&self, index: [usize; 2]) -> &Self::Output {
        &self.inner[[index[1], index[0]]]
    }
}

impl<T: Numeric> IndexMut<[usize; 2]> for DenseTransposeMut<'_, T> {
    fn index_mut(&mut self, index: [usize; 2]) -> &mut Self::Output {
        &mut self.inner[[index[1], index[0]]]
    }
}

impl<'a, T: Numeric + 'a> Matrix for DenseTranspose<'a, T> {
    type Element = T;

    fn len(&self) -> usize {
        self.inner.len()
    }

    fn size(&self) -> [usize; 2] {
        self.inner.size()
    }

    fn into_vec(self) -> Vec<Self::Element> {
        // TODO
        unimplemented!()
    }
}

impl<'a, T: Numeric + 'a> Matrix for DenseTransposeMut<'a, T> {
    type Element = T;

    fn len(&self) -> usize {
        self.inner.len()
    }

    fn size(&self) -> [usize; 2] {
        self.inner.size()
    }

    fn into_vec(self) -> Vec<Self::Element> {
        //TODO
        unimplemented!()
    }
}

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

    #[test]
    fn test_transpose() {
        let mut a = mat![1, 2, 3; 4, 5, 6];
        assert_eq!(a[[0, 1]], 2);

        let b = a.t();
        assert_eq!(b[[0, 1]], 4);

        a.t_mut()[[2, 0]] = 7;
        assert_eq!(a[[0, 2]], 7);
    }

    #[test]
    fn test_raw_matrix() {
        let a = Dense {
            data: vec![0, 1, 2, 3, 4, 5],
            n: 3,
            m: 2,
        };
        assert_eq!(a[[0, 0]], 0);
        assert_eq!(a[[0, 1]], 1);
        assert_eq!(a[[0, 2]], 2);
        assert_eq!(a[[1, 0]], 3);
        assert_eq!(a[[1, 1]], 4);
        assert_eq!(a[[1, 2]], 5);

        let i: Dense<u32> = Dense::eye(3);
        let eye3 = mat![
            1, 0, 0;
            0, 1, 0;
            0, 0, 1
        ];

        assert_eq!(i, eye3);

        let j: Dense<f64> = Dense::eye(2);
        j.assert_approx_eq(&mat![1., 0.; 0., 1.], f64::EPSILON)
    }

    #[test]
    fn col_from_vec_test() {
        let v = vec![1, 2, 3, 4, 5];
        let ans = mat![1; 2; 3; 4; 5];

        let m = Dense::col_from_vec(v);

        assert_eq!(ans, m);

        let v = vec![7, 3, 1, 6, 5];
        let ans = mat![7; 3; 1; 6; 5];

        let m = v.into_col();

        assert_eq!(ans, m);
    }

    #[test]
    fn macro_tests() {
        let a: Dense<f64> = mat!();
        assert!(a.is_empty());

        let b: Dense<u8> = mat![0;1;2;3;4];
        assert!(!b.is_empty());
        assert_eq!(b.m, 5);
        assert_eq!(b.n, 1);

        let c = mat![1];
        assert_eq!(c.len(), 1);

        let d = mat![0, 1, 2];
        assert_eq!(d.len(), 3);

        let e = mat![
            0, 1, 2;
            3, 4, 5
        ];

        assert_eq!(e.len(), 6);
        assert_eq!(e[[0, 0]], 0);
        assert_eq!(e[[0, 1]], 1);
        assert_eq!(e[[0, 2]], 2);
        assert_eq!(e[[1, 0]], 3);
        assert_eq!(e[[1, 1]], 4);
        assert_eq!(e[[1, 2]], 5);

        let f = mat![3 => 2, 2];
        assert_eq!(f[[0, 0]], 3);
        assert_eq!(f[[0, 1]], 3);
        assert_eq!(f[[1, 0]], 3);
        assert_eq!(f[[1, 1]], 3);
    }

    #[test]
    fn approx_matrix_test() {
        let a: Dense<f64> = mat![
            1., 2., 3.;
            0.000001, 0., 1000.
        ];

        let b: Dense<f64> = mat![
            1., 2., 3.;
            0.0000011, 0., 1000.
        ];

        assert!(&a.approx_eq(&b, 0.0000002));
    }

    #[test]
    fn from_vec_test() {
        let v = vec![1, 2, 3, 4];

        let m = Dense::from(v);

        assert_eq!(m, mat![1, 2, 3, 4])
    }

    #[test]
    fn swap() {
        let mut a: Dense<u32> = mat![1,2,3;4,5,6;7,8,9];
        a.swap([0, 0], [2, 2]);
        a.swap([0, 1], [2, 0]);
        assert_eq!(a.data, vec![9, 7, 3, 4, 5, 6, 2, 8, 1]);
        assert_eq!(a, mat![9,7,3;4,5,6;2,8,1])
    }

    #[test]
    fn mn_swap() {
        let mut m = mat![1, 2, 3, 4, 5];
        assert_ne!(m, mat![1; 2; 3; 4; 5]);

        m.swap_mn();
        assert_eq!(m, mat![1; 2; 3; 4; 5]);
    }

    #[test]
    fn row_swap() {
        let mut a: Dense<i32> = mat![1,2,3,4; 5,6,7,8; 9,10,11,12];
        a.swap_rows(0, 2);
        let b: Dense<i32> = mat![9,10,11,12; 5,6,7,8; 1,2,3,4];
        assert_eq!(a, b)
    }

    #[test]
    fn concatenate() {
        let a = mat![1, 2; 3, 4];
        let b = mat![5; 6];
        let ans = mat![1, 2, 5; 3, 4, 6];

        assert_eq!(a.concatenate(b).unwrap(), ans);

        let a = mat![1, 5; 2, 6; 3, 7; 4, 8];
        let b = mat![9, 13; 10, 14; 11, 15; 12, 16];
        let ans = mat![1, 5, 9, 13; 2, 6, 10, 14; 3, 7, 11, 15; 4, 8, 12, 16];

        assert_eq!(a.concatenate(b).unwrap(), ans);
    }

    #[test]
    fn concatenate_vec() {
        let a = mat![1, 2; 3, 4];
        let b = vec![5, 6];
        let ans = mat![1, 2, 5; 3, 4, 6];

        assert_eq!(a.concatenate_vec(&b).unwrap(), ans);
    }

    mod ops {
        use super::*;

        #[test]
        fn scalar_mul() {
            let x = mat![1, 2; 3, 4];
            assert_eq!(x * 2, mat![2, 4; 6, 8]);

            let mut x = mat![0, 4; 8, 10];
            x *= 3;
            assert_eq!(x, mat![0, 12; 24, 30]);
        }

        #[test]
        fn row_mul() {
            let mut x = mat![0, 4; 8, 10];

            x.scale_row(1, 2);

            assert_eq!(mat![0, 4; 16, 20], x)
        }

        #[test]
        fn row_add() {
            let mut x = mat![1, 2; 8, 10];

            x.add_rows(1, 0, 2);

            assert_eq!(mat![1, 2; 10, 14], x)
        }

        #[test]
        fn matrix_mul() {
            let a = mat![ 1, 3, 5; 7, 4, 6];
            let b = mat![4, 5; 2, 8; 4, 1];
            let c = a * b;
            let ans = mat![30, 34; 60, 73];
            assert!(c.is_ok());
            assert_eq!(c.unwrap(), ans);
        }

        #[test]
        fn matrix_mul_refs() {
            let a = mat![ 1, 3, 5; 7, 4, 6];
            let b = mat![4, 5; 2, 8; 4, 1];
            let ans = mat![30, 34; 60, 73];

            let c = &a * &b;
            assert!(c.is_ok());
            assert_eq!(c.unwrap(), ans);

            let c = &a * b;
            assert!(c.is_ok());
            assert_eq!(c.unwrap(), ans);

            let b = mat![4, 5; 2, 8; 4, 1];
            let c = a * &b;
            assert!(c.is_ok());
            assert_eq!(c.unwrap(), ans);
        }

        #[test]
        fn test_fkn_borrows() {
            let a = mat![ 1, 3, 5; 7, 4, 6];
            let b = mat![4, 5; 2, 8; 4, 1];
            let c = a * &b;
            let ans = mat![30, 34; 60, 73];
            assert!(c.is_ok());
            assert_eq!(c.unwrap(), ans);
        }

        #[test]
        fn matrix_incompatibilities() {
            let a = mat![1, 2, 3];
            let b = mat![2, 3; 4, 5];
            let c = a * b;
            assert!(c.is_err());
            assert_eq!(c.unwrap_err(), MatrixError::Incompatibility)
        }
    }

    #[test]
    fn matrix_print() {
        let i = mat![
            1, 2, 3;
            4, 5, 6;
            7, 8, 9
        ];

        assert_eq!(
            format!("{}", i),
            "  1  2  3\n  4  5  6\n  7  8  9".to_string()
        );

        let f = mat![
            0.1, 2.34, 3.14;
            4.05, -5.2, -6.84;
            7.999, 8.0023, 9.99
        ];

        assert_eq!(
            format!("{:.3}", f),
            "   0.100   2.340   3.140\n   4.050  -5.200  -6.840\n   7.999   8.002   9.990"
                .to_string()
        );

        assert_eq!(
            format!("{}", f),
            "   0.10   2.34   3.14\n   4.05  -5.20  -6.84\n   8.00   8.00   9.99".to_string()
        );
    }

    #[test]
    fn iters() {
        let a = mat![1, 2, 3; 4, 5, 6; 7, 8, 9];
        a.into_iter().for_each(|x| println!("{:?}", x));

        for x in &a {
            println!("{:?}", x)
        }

        a.into_iter().enumerate().for_each(|x| println!("{:?}", x));
        a.into_iter()
            .for_each(|x| x.into_iter().for_each(|y| println!("{:?} {:?}", y, x)));

        a.into_iter().enumerate().for_each(|(i, row)| {
            row.into_iter().enumerate().for_each(|(j, x)| {
                println!("[{},{}] = {}", i, j, x);
            })
        });
    }
}