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
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
mod imp;

use crate::{
    traits::{Real, Scalar},
    utils::ConstVec,
};
use imp::MatrixImpl;
use std::{
    cmp::PartialEq,
    fmt,
    ops::{Add, Div, Index, IndexMut, Mul, Sub},
};

fn mm_dot<'a, T: Scalar, const M: usize, const N: usize, const P: usize>(
    v: Row<'a, T, { M }, { N }>,
    u: Column<'a, T, { N }, { P }>,
) -> T {
    let mut out = T::ZERO;

    v.zip(u).for_each(|(&x, &y)| out += x * y);

    out
}

// ========================= Matrix

/// An M * N column-major Matrix of scalar values backed by an array. Scalar
/// refers to anything that implements the [`Scalar`] trait, which is any value
/// that is [`Copy`] and has a notion of arithmetic. However, many operations,
/// such as the determinant and Gaussian elimination may not be possible
/// with integer values.
#[derive(Clone)]
pub struct Matrix<T: Scalar, const M: usize, const N: usize> {
    data: MatrixImpl<T, { M }, { N }>,
}

impl<T: Scalar, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    /// Constructs a matrix filled with all zero values.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[0.0, 0.0], [0.0, 0.0]];
    /// let a = Matrix::<f64, 2, 2>::zero();
    /// let b = Matrix::from_array(array);
    ///
    /// assert_eq!(a, b);
    /// ```
    pub const fn zero() -> Self {
        Self {
            data: MatrixImpl::zero(),
        }
    }

    /// Constructs a matrix filled with all values equal to `t`.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 1.0], [1.0, 1.0]];
    /// let a = Matrix::<_, 2, 2>::from_val(1.0);
    /// let b = Matrix::from_array(array);
    ///
    /// assert_eq!(a, b);
    /// ```
    pub const fn from_val(t: T) -> Self {
        Self {
            data: MatrixImpl::from_val(t),
        }
    }

    /// Constructs a matrix from a given 2 dimensional array.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [1.0, 2.0, 3.0, 4.0];
    /// let m = Matrix::from_array(array);
    ///
    /// for (x, y) in m.column_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn from_array(array: [[T; M]; N]) -> Self {
        Self {
            data: MatrixImpl::from_array(array),
        }
    }

    /// Returns a reference to the backing array of the matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let m = Matrix::<_, 2, 2>::from_val(1.0);
    /// let slice = m.as_array();
    ///
    /// for a in slice {
    ///     println!("{:?}", &a[..]);
    /// }
    /// ```
    pub const fn as_array(&self) -> &[[T; M]; N] {
        self.data.as_array()
    }

    /// Returns a mutable reference to the backing array of the matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let mut m = Matrix::<_, 2, 2>::from_val(1.0);
    /// let slice = m.as_mut_array();
    ///
    /// for arr in slice {
    ///     for elem in arr {
    ///         *elem = 2.0;
    ///     }
    /// }
    ///
    /// assert_eq!(m, Matrix::from_val(2.0));
    /// ```
    pub const fn as_mut_array(&mut self) -> &mut [[T; M]; N] {
        self.data.as_mut_array()
    }

    /// Returns a raw pointer to the first element in the array. Keep in mind that
    /// since the matrix is represented in column-major order, offsetting this
    /// pointer by `mem::size_of::<T>` bytes will cause it to point to the next
    /// element in the column.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let m = Matrix::<_, 2, 2>::from_val(1.0);
    /// let ptr = m.as_ptr();
    ///
    /// unsafe {
    ///     assert_eq!(*ptr, 1.0);
    /// }
    /// ```
    pub const fn as_ptr(&self) -> *const T {
        self.as_array() as *const _ as _
    }

    /// Returns a mutable raw pointer to the first element in the array. Keep in mind
    /// that since the matrix is represented in column-major order, offsetting this
    /// pointer by `mem::size_of::<T>` bytes will cause it to point to the next
    /// element in the column.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let mut m = Matrix::<_, 2, 2>::from_val(1.0);
    /// let ptr = m.as_mut_ptr();
    ///
    /// unsafe {
    ///     *ptr = 2.0;
    ///     assert_ne!(*ptr, 1.0);
    /// }
    /// ```
    pub const fn as_mut_ptr(&mut self) -> *mut T {
        self.as_mut_array() as *mut _ as _
    }

    /// Returns the dimensions of the matrix, in the order (M, N).
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let m = Matrix::<_, 2, 3>::from_val(1.0);
    ///
    /// assert_eq!(m.dimensions(), (2, 3))
    /// ```
    pub const fn dimensions(&self) -> (usize, usize) {
        (M, N)
    }

    /// Returns a column-wise iterator of all elements in the matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [1.0, 2.0, 3.0, 4.0];
    /// let m = Matrix::from_array(array);
    ///
    /// for (x, y) in m.column_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn column_iter(&self) -> ColumnIter<'_, T, { M }, { N }> {
        ColumnIter {
            matrix: self,
            row: 0,
            col: 0,
        }
    }

    /// Returns a column-wise iterator of mutable references to
    /// all elements in the matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [2.0, 4.0, 6.0, 8.0];
    /// let mut m = Matrix::from_array(array);
    ///
    /// for elem in m.column_iter_mut() {
    ///     *elem *= 2.0;
    /// }
    ///
    /// for (x, y) in m.column_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn column_iter_mut(&mut self) -> ColumnIterMut<'_, T, { M }, { N }> {
        ColumnIterMut {
            matrix: self,
            row: 0,
            col: 0,
        }
    }

    /// Returns a row-wise iterator of all elements in the matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [1.0, 3.0, 2.0, 4.0];
    /// let m = Matrix::from_array(array);
    ///
    /// for (x, y) in m.row_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn row_iter(&self) -> RowIter<'_, T, { M }, { N }> {
        RowIter {
            matrix: self,
            row: 0,
            col: 0,
        }
    }

    /// Returns a row-wise iterator of mutable references to
    /// all elements in the matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [2.0, 6.0, 4.0, 8.0];
    /// let mut m = Matrix::from_array(array);
    ///
    /// for elem in m.row_iter_mut() {
    ///     *elem *= 2.0;
    /// }
    ///
    /// for (x, y) in m.row_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn row_iter_mut(&mut self) -> RowIterMut<'_, T, { M }, { N }> {
        RowIterMut {
            matrix: self,
            row: 0,
            col: 0,
        }
    }

    /// Returns an iterator of elements over the specified column in
    /// the matrix, starting from the `n`th element.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [1.0, 2.0];
    /// let m = Matrix::from_array(array);
    ///
    /// for (x, y) in m.column(0, 0).zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn column(&self, column: usize, n: usize) -> Column<'_, T, { M }, { N }> {
        Column {
            matrix: self,
            column,
            idx: n,
        }
    }

    /// Returns an iterator of mutable references over the specified column
    /// in the matrix, starting from the `n`th element.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [2.0, 4.0, 3.0, 4.0];
    /// let mut m = Matrix::from_array(array);
    ///
    /// for elem in m.column_mut(0, 0) {
    ///     *elem *= 2.0;
    /// }
    ///
    /// for (x, y) in m.column_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn column_mut(&mut self, column: usize, n: usize) -> ColumnMut<'_, T, { M }, { N }> {
        ColumnMut {
            matrix: self,
            column,
            idx: n,
        }
    }

    /// Returns an iterator of elements over the specified row in
    /// the matrix, starting from the `n`th element.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [1.0, 3.0];
    /// let m = Matrix::from_array(array);
    ///
    /// for (x, y) in m.row(0, 0).zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn row(&self, row: usize, n: usize) -> Row<'_, T, { M }, { N }> {
        Row {
            matrix: self,
            row,
            idx: n,
        }
    }

    /// Returns an iterator of mutable references over the specified row
    /// in the matrix, starting from the `n`th element.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1.0, 2.0], [3.0, 4.0]];
    /// let expected = [2.0, 6.0, 2.0, 4.0];
    /// let mut m = Matrix::from_array(array);
    ///
    /// for elem in m.row_mut(0, 0) {
    ///     *elem *= 2.0;
    /// }
    ///
    /// for (x, y) in m.row_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y)
    /// }
    /// ```
    pub const fn row_mut(&mut self, row: usize, n: usize) -> RowMut<'_, T, { M }, { N }> {
        RowMut {
            matrix: self,
            row,
            idx: n,
        }
    }

    /// Converts the provided matrix into one which holds [`f64`]
    /// values. This is required for some operations, such as
    /// computing the determinant of a matrix or putting the matrix
    /// into [row echelon form][1].
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;     
    ///      
    /// let array = [[1, 2], [3, 4]];
    /// let expected = [1.0, 2.0, 3.0, 4.0];
    /// let m = Matrix::from_array(array).into_f64();
    ///
    /// for (&x, &y) in m.column_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y as f64);
    /// }
    /// ```
    ///
    /// [1]: https://en.wikipedia.org/wiki/Row_echelon_form
    pub fn into_f64(self) -> Matrix<f64, { M }, { N }> {
        let mut out = Matrix::zero();

        out.column_iter_mut()
            .zip(self.column_iter().map(|elem| elem.to_f64()))
            .for_each(|(x, y)| *x = y);

        out
    }

    /// Converts the provided matrix into one which holds [`f32`]
    /// values. This is required for some operations, such as
    /// computing the determinant of a matrix or putting the matrix
    /// into [row echelon form][1].
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;     
    ///      
    /// let array = [[1, 2], [3, 4]];
    /// let expected = [1.0, 2.0, 3.0, 4.0];
    /// let m = Matrix::from_array(array).into_f32();
    ///
    /// for (&x, &y) in m.column_iter().zip(expected.iter()) {
    ///     assert_eq!(x, y as f32);
    /// }
    /// ```
    ///
    /// [1]: https://en.wikipedia.org/wiki/Row_echelon_form
    pub fn into_f32(self) -> Matrix<f32, { M }, { N }> {
        let mut out = Matrix::zero();

        out.column_iter_mut()
            .zip(self.column_iter().map(|elem| elem.to_f32()))
            .for_each(|(x, y)| *x = y);

        out
    }

    /// Returns the transpose of a matrix. This is a
    /// non-consuming operation, since the matrix needs
    /// to be copied anyways.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{matrix, Matrix};
    ///
    /// let m = matrix![
    ///     1.0, 2.0;
    ///     3.0, 4.0;
    ///     5.0, 6.0;
    /// ];
    ///
    /// let m_t = m.transpose();
    ///
    /// for (x, y) in m.column_iter().zip(m_t.row_iter()) {
    ///     assert_eq!(x, y);
    /// }
    /// ```
    pub fn transpose(&self) -> Matrix<T, { N }, { M }> {
        let mut out = Matrix::<T, { N }, { M }>::zero();

        for row in 0..M {
            for col in 0..N {
                out[(col, row)] = self[(row, col)];
            }
        }

        out
    }
}

// Methods that are only applicable when the matrix is square.
impl<T: Scalar, const N: usize> Matrix<T, { N }, { N }> {
    /// Returns the N x N identity matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::Matrix;
    ///
    /// let array = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
    ///
    /// let m = Matrix::<usize, 3, 3>::id();
    ///
    /// assert_eq!(m, Matrix::from_array(array));
    /// ```
    pub const fn id() -> Self {
        Self {
            data: MatrixImpl::id(),
        }
    }

    /// Returns the determinant of a given matrix. For N < 5,
    /// this is done manually, as matrices with dimensions of
    /// less than 5x5 are the most common ones encountered. For
    /// N >= 5, the matrix is first row reduced, and then the
    /// product of the main diagonal is returned. Note that
    /// this operation may not be 100% correct, as the matrix
    /// must first be converted into floating point
    /// representation, which may result in a loss of precision.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{matrix, Matrix};
    ///
    /// let m = matrix![
    ///     3, 3, 3;
    ///     -2, -1, -2;
    ///     1, -2, -3;
    /// ];
    ///
    /// assert_eq!(m.det(), -12.0);
    /// ```
    pub fn det(&self) -> f64 {
        // For matrix dimensions 0 to 4, we do the determinant
        // manually in order to speed up computations.
        // For dimensions above 4, use gaussian elimination
        // and find the product of the diagonals.
        match N {
            0 => 1.0,
            1 => self[(0, 0)].to_f64(),
            2 => {
                // | a b |
                // | c d |

                (self[(0, 0)] * self[(1, 1)] - self[(0, 1)] * self[(1, 0)]).to_f64()
            }
            3 => {
                // This is horrible.
                // | a b c |
                // | d e f |
                // | g h i |

                let a = self[(0, 0)];
                let b = self[(0, 1)];
                let c = self[(0, 2)];

                let d = self[(1, 0)];
                let e = self[(1, 1)];
                let f = self[(1, 2)];

                let g = self[(2, 0)];
                let h = self[(2, 1)];
                let i = self[(2, 2)];

                (a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)).to_f64()
            }
            4 => {
                // And this is even worse.
                // | a b c d |
                // | e f g h |
                // | i j k l |
                // | m n o p |

                let a = self[(0, 0)];
                let b = self[(0, 1)];
                let c = self[(0, 2)];
                let d = self[(0, 3)];

                let e = self[(1, 0)];
                let f = self[(1, 1)];
                let g = self[(1, 2)];
                let h = self[(1, 3)];

                let i = self[(2, 0)];
                let j = self[(2, 1)];
                let k = self[(2, 2)];
                let l = self[(2, 3)];

                let m = self[(3, 0)];
                let n = self[(3, 1)];
                let o = self[(3, 2)];
                let p = self[(3, 3)];

                let x = a * (f * (k * p - l * o) - g * (j * p - l * n) + h * (j * o - k * n));
                let y = b * (e * (k * p - l * o) - g * (i * p - l * m) + h * (i * o - k * m));
                let z = c * (e * (j * p - l * n) - f * (i * p - l * m) + h * (i * n - j * m));
                let w = d * (e * (j * o - k * n) - f * (i * o - k * m) + g * (i * n - j * m));

                (x - y + z - w).to_f64()
            }
            _ => {
                let matrix = self.clone().into_f64().gauss();

                let mut prod = 1.0;

                for i in 0..N {
                    prod *= matrix[(i, i)];
                }

                prod
            }
        }
    }

    /// Returns the determinant of a given matrix. For N < 5,
    /// this is done manually, as matrices with dimensions of
    /// less than 5x5 are the most common ones encountered. For
    /// N >= 5, the matrix is first row reduced, and then the
    /// product of the main diagonal is returned. Note that
    /// this operation may not be 100% correct, as the matrix
    /// must first be converted into floating point
    /// representation, which may result in a loss of precision.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{matrix, Matrix};
    ///
    /// let m = matrix![
    ///     3, 3, 3;
    ///     -2, -1, -2;
    ///     1, -2, -3;
    /// ];
    ///
    /// assert_eq!(m.det_f32(), -12.0);
    /// ```
    pub fn det_f32(&self) -> f32 {
        match N {
            0 => 1.0,
            1 => self[(0, 0)].to_f32(),
            2 => (self[(0, 0)] * self[(1, 1)] - self[(0, 1)] * self[(1, 0)]).to_f32(),
            3 => {
                let a = self[(0, 0)];
                let b = self[(0, 1)];
                let c = self[(0, 2)];

                let d = self[(1, 0)];
                let e = self[(1, 1)];
                let f = self[(1, 2)];

                let g = self[(2, 0)];
                let h = self[(2, 1)];
                let i = self[(2, 2)];

                (a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)).to_f32()
            }
            4 => {
                let a = self[(0, 0)];
                let b = self[(0, 1)];
                let c = self[(0, 2)];
                let d = self[(0, 3)];

                let e = self[(1, 0)];
                let f = self[(1, 1)];
                let g = self[(1, 2)];
                let h = self[(1, 3)];

                let i = self[(2, 0)];
                let j = self[(2, 1)];
                let k = self[(2, 2)];
                let l = self[(2, 3)];

                let m = self[(3, 0)];
                let n = self[(3, 1)];
                let o = self[(3, 2)];
                let p = self[(3, 3)];

                let x = a * (f * (k * p - l * o) - g * (j * p - l * n) + h * (j * o - k * n));
                let y = b * (e * (k * p - l * o) - g * (i * p - l * m) + h * (i * o - k * m));
                let z = c * (e * (j * p - l * n) - f * (i * p - l * m) + h * (i * n - j * m));
                let w = d * (e * (j * o - k * n) - f * (i * o - k * m) + g * (i * n - j * m));

                (x - y + z - w).to_f32()
            }
            _ => {
                let matrix = self.clone().into_f32().gauss();

                let mut prod = 1.0;

                for i in 0..N {
                    prod *= matrix[(i, i)];
                }

                prod
            }
        }
    }
}

// Methods that are only applicable when the matrix contains floating point
// values.
impl<T: Real, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    /// Reduces the provided matrix to [row echelon form][1],
    /// consuming the original matrix in the process.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{matrix, Matrix};
    ///
    /// let m = matrix![
    ///     3, 3, 3;
    ///     -2, -1, -2;
    ///     1, -2, -3;
    /// ].into_f64().gauss();
    ///
    /// let rows = m.dimensions().0;
    ///
    /// for i in 0..rows {
    ///     println!("{:?}", m[(i, i)]);
    /// }
    ///
    /// ```
    ///
    /// [1]: https://en.wikipedia.org/wiki/Row_echelon_form
    pub fn gauss(mut self) -> Self {
        self.gauss_in_place();
        self
    }

    /// Reduces the provided matrix to [row echelon form][1] without
    /// consuming the original matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{matrix, Matrix};
    ///
    /// let mut m = matrix![
    ///     3, 3, 3;
    ///     -2, -1, -2;
    ///     1, -2, -3;
    /// ].into_f64();
    ///
    /// m.gauss_in_place();
    /// let rows = m.dimensions().0;
    ///
    /// for i in 0..rows {
    ///     println!("{:?}", m[(i, i)]);
    /// }
    ///
    /// ```
    ///
    /// [1]: https://en.wikipedia.org/wiki/Row_echelon_form
    pub fn gauss_in_place(&mut self) {
        for pivot_idx in 0..M - 1 {
            if self[(pivot_idx, pivot_idx)] != T::ZERO {
                let mut pivot = ConstVec::<_, { N }>::new();

                self.row(pivot_idx, pivot_idx)
                    .for_each(|&val| pivot.push(val).ok().unwrap());

                for row_idx in pivot_idx + 1..M {
                    // Find the ratio needed to remove the elements below the
                    // pivot.
                    let ratio = self[(row_idx, pivot_idx)] / pivot[0];

                    // subtract a multiple of the pivot row from this row.
                    self.row_mut(row_idx, pivot_idx)
                        .zip(pivot.iter())
                        .for_each(|(val, &sub)| *val -= ratio * sub);
                }
            }
        }
    }
}

// ========================= Iterators

/// A column-wise iterator over the elements of a matrix.
pub struct ColumnIter<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a Matrix<T, { M }, { N }>,
    row: usize,
    col: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator
    for ColumnIter<'a, T, { M }, { N }>
{
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        if self.col == N {
            None
        } else {
            let ret = &self.matrix.index((self.row, self.col));

            if self.row == M - 1 {
                self.row = 0;
                self.col += 1;
            } else {
                self.row += 1;
            }

            Some(ret)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (M * N, Some(M * N))
    }
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> ExactSizeIterator
    for ColumnIter<'a, T, { M }, { N }>
{
}

/// A column-wise iterator over mutable references to the elements of a matrix.
pub struct ColumnIterMut<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a mut Matrix<T, { M }, { N }>,
    row: usize,
    col: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator
    for ColumnIterMut<'a, T, { M }, { N }>
{
    type Item = &'a mut T;

    fn next(&mut self) -> Option<&'a mut T> {
        if self.col == N {
            None
        } else {
            let ret = self.matrix.index_mut((self.row, self.col)) as *mut T;

            if self.row == M - 1 {
                self.row = 0;
                self.col += 1;
            } else {
                self.row += 1;
            }

            // SAFETY:
            // 1. The pointer can never be invalid, since the Matrix can not
            // be dropped due to it being mutably borrowed.
            // 2. The code above only ever gets one mutable borrow to each
            // element, so there is no pointer aliasing.
            unsafe { Some(&mut *ret) }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (M * N, Some(M * N))
    }
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> ExactSizeIterator
    for ColumnIterMut<'a, T, { M }, { N }>
{
}

/// A row-wise iterator over the elements of a matrix.
pub struct RowIter<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a Matrix<T, { M }, { N }>,
    row: usize,
    col: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator for RowIter<'a, T, { M }, { N }> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        if self.row == M {
            None
        } else {
            let ret = &self.matrix.index((self.row, self.col));

            if self.col == N - 1 {
                self.col = 0;
                self.row += 1;
            } else {
                self.col += 1;
            }

            Some(ret)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (M * N, Some(M * N))
    }
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> ExactSizeIterator
    for RowIter<'a, T, { M }, { N }>
{
}

/// A row-wise iterator over mutable references to the elements of a matrix.
pub struct RowIterMut<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a mut Matrix<T, { M }, { N }>,
    row: usize,
    col: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator
    for RowIterMut<'a, T, { M }, { N }>
{
    type Item = &'a mut T;

    fn next(&mut self) -> Option<&'a mut T> {
        if self.row == M {
            None
        } else {
            let ret = self.matrix.index_mut((self.row, self.col)) as *mut T;

            if self.col == N - 1 {
                self.col = 0;
                self.row += 1;
            } else {
                self.col += 1;
            }

            // SAFETY:
            // 1. The pointer can never be invalid, since the Matrix can not
            // be dropped due to it being mutably borrowed.
            // 2. The code above only ever gets one mutable borrow to each
            // element, so there is no pointer aliasing.
            unsafe { Some(&mut *ret) }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (M * N, Some(M * N))
    }
}

impl<'a, T: Scalar, const M: usize, const N: usize> ExactSizeIterator
    for RowIterMut<'a, T, { M }, { N }>
{
}

/// An iterator over elements in one column of a matrix.
pub struct Column<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a Matrix<T, { M }, { N }>,
    column: usize,
    idx: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator for Column<'a, T, { M }, { N }> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        if self.idx == M {
            None
        } else {
            let ret = &self.matrix.index((self.idx, self.column));

            self.idx += 1;

            Some(ret)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (M, Some(M))
    }
}

/// An iterator over mutable references to elements in one column of a matrix.
pub struct ColumnMut<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a mut Matrix<T, { M }, { N }>,
    column: usize,
    idx: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator
    for ColumnMut<'a, T, { M }, { N }>
{
    type Item = &'a mut T;

    fn next(&mut self) -> Option<&'a mut T> {
        if self.idx == M {
            None
        } else {
            let ret = self.matrix.index_mut((self.idx, self.column)) as *mut T;

            self.idx += 1;

            unsafe { Some(&mut *ret) }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (M, Some(M))
    }
}

/// An iterator over elements in one row of a matrix.
pub struct Row<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a Matrix<T, { M }, { N }>,
    row: usize,
    idx: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator for Row<'a, T, { M }, { N }> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        if self.idx == N {
            None
        } else {
            let ret = &self.matrix.index((self.row, self.idx));

            self.idx += 1;

            Some(ret)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (N, Some(N))
    }
}

/// An iterator over mutable references to elements in one row of a matrix.
pub struct RowMut<'a, T: Scalar + 'a, const M: usize, const N: usize> {
    matrix: &'a mut Matrix<T, { M }, { N }>,
    row: usize,
    idx: usize,
}

impl<'a, T: Scalar + 'a, const M: usize, const N: usize> Iterator for RowMut<'a, T, { M }, { N }> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<&'a mut T> {
        if self.idx == N {
            None
        } else {
            let ret = self.matrix.index_mut((self.row, self.idx)) as *mut T;

            self.idx += 1;

            unsafe { Some(&mut *ret) }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (N, Some(N))
    }
}

// ========================= Matrix Trait Impls

impl<T: Scalar, const M: usize, const N: usize> fmt::Debug for Matrix<T, { M }, { N }> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Matrix {{")?;
        writeln!(f, "    [")?;

        for col in self.as_array().iter() {
            writeln!(f, "        {:?},", &col[..])?;
        }

        writeln!(f, "    ],")?;
        writeln!(f, "}}")
    }
}

impl<T: Scalar, const M: usize, const N: usize> PartialEq for Matrix<T, { M }, { N }> {
    fn eq(&self, other: &Self) -> bool {
        self.column_iter()
            .zip(other.column_iter())
            .all(|(x, y)| x == y)
    }
}

impl<T: Scalar + Eq, const M: usize, const N: usize> Eq for Matrix<T, { M }, { N }> {}

impl<T: Scalar, const M: usize, const N: usize> Index<(usize, usize)> for Matrix<T, { M }, { N }> {
    type Output = T;

    fn index(&self, index: (usize, usize)) -> &T {
        &self.as_array()[index.1][index.0]
    }
}

impl<T: Scalar, const M: usize, const N: usize> IndexMut<(usize, usize)>
    for Matrix<T, { M }, { N }>
{
    fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
        &mut self.as_mut_array()[index.1][index.0]
    }
}

impl<T: Scalar, const M: usize, const N: usize> Add for Matrix<T, { M }, { N }> {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self.column_iter_mut()
            .zip(rhs.column_iter())
            .for_each(|(self_val, &rhs_val)| *self_val += rhs_val);
        self
    }
}

impl<T: Scalar, const M: usize, const N: usize> Sub for Matrix<T, { M }, { N }> {
    type Output = Self;

    fn sub(mut self, rhs: Self) -> Self::Output {
        self.column_iter_mut()
            .zip(rhs.column_iter())
            .for_each(|(self_val, &rhs_val)| *self_val -= rhs_val);
        self
    }
}

impl<T: Scalar, const M: usize, const N: usize, const P: usize> Mul<Matrix<T, { N }, { P }>>
    for Matrix<T, { M }, { N }>
{
    type Output = Matrix<T, { M }, { P }>;

    fn mul(self, rhs: Matrix<T, { N }, { P }>) -> Self::Output {
        let mut out = Self::Output::zero();

        for i in 0..M {
            for j in 0..P {
                let row = self.row(i, 0);
                let column = rhs.column(j, 0);

                out[(i, j)] = mm_dot(row, column);
            }
        }

        out
    }
}

impl<T: Scalar, const R: usize, const C: usize> Mul<T> for Matrix<T, { R }, { C }> {
    type Output = Self;

    fn mul(mut self, rhs: T) -> Self {
        for elem in self.column_iter_mut() {
            *elem *= rhs;
        }

        self
    }
}

// ========================= Vectors (Special case of Matrix)

/// An N dimensional vector, represented as a matrix
/// with dimensions N x 1.
pub type Vector<T, const N: usize> = Matrix<T, { N }, 1>;

/// An N dimensional vector of f64 values.
pub type VectorF64<const N: usize> = Vector<f64, { N }>;

/// An N dimensional vector of f32 values.
pub type VectorF32<const N: usize> = Vector<f32, { N }>;

impl<T: Scalar, const N: usize> Vector<T, { N }> {
    /// Returns the length of the vector as an [`f64`].
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{vector, Vector};
    ///
    /// let v = vector![1, 1, 1];
    ///
    /// assert_eq!(v.length(), f64::sqrt(3.0));
    /// ```
    pub fn length(&self) -> f64 {
        f64::sqrt(self.squared_sum().to_f64())
    }

    /// Returns the length of the vector as an [`f32`].
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{vector, Vector};
    ///
    /// let v = vector![1, 1, 1];
    ///
    /// assert_eq!(v.length(), f64::sqrt(3.0));
    /// ```
    ///
    pub fn length_f32(&self) -> f32 {
        f32::sqrt(self.squared_sum().to_f32())
    }

    /// Consumes and normalizes the vector, returning the
    /// unit vector of [`f64`] values in the same direction
    /// as the original vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{vector, Vector};
    ///
    /// let v = vector![1, 1, 1].normalize();
    ///
    /// assert_eq!(v.length(), 1.0);
    /// ```
    pub fn normalize(self) -> VectorF64<{ N }> {
        let len = self.length();

        // A vector's length is zero iff it is the zero vector.
        if len == f64::ZERO {
            VectorF64::<{ N }>::zero()
        } else {
            self.into_f64() / len
        }
    }

    /// Consumes and normalizes the vector, returning the
    /// unit vector of [`f32`] values in the same direction
    /// as the original vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::{
    ///     traits::Real,
    ///     vector, Vector,
    /// };
    ///
    /// let v = vector![1, 1, 1].normalize_f32();
    ///
    /// // Relative equality is needed here, since
    /// // the value doesn't come out as *exactly*
    /// // 1.0f32.
    /// let epsilon = std::f32::EPSILON;
    /// let relative = 1.0E-7f32;
    ///
    /// assert!(v.length_f32().approx_eq(1.0f32, epsilon, relative));
    /// ```
    pub fn normalize_f32(self) -> VectorF32<{ N }> {
        let len = self.length_f32();

        if len == f32::ZERO {
            VectorF32::<{ N }>::zero()
        } else {
            self.into_f32() / len
        }
    }

    pub fn dot(&self, rhs: &Self) -> T {
        self.column_iter()
            .zip(rhs.column_iter())
            .fold(T::ZERO, |acc, (&x, &y)| acc + (x * y))
    }

    /// Returns the sum of every elements squared.
    fn squared_sum(&self) -> T {
        let mut sum = T::ZERO;
        for &x in self.column_iter() {
            sum += x * x;
        }
        sum
    }
}

impl<T: Scalar> Vector<T, { 3 }> {
    pub fn cross(&self, rhs: &Self) -> Self {
        vector![
            self[(1, 0)] * rhs[(2, 0)] - self[(2, 0)] * rhs[(1, 0)],
            self[(2, 0)] * rhs[(0, 0)] - self[(0, 0)] * rhs[(2, 0)],
            self[(0, 0)] * rhs[(1, 0)] - self[(1, 0)] * rhs[(0, 0)],
        ]
    }
}

// ========================= Vector Trait Impls

impl<T: Scalar, const N: usize> Add<T> for Vector<T, { N }> {
    type Output = Self;

    fn add(mut self, rhs: T) -> Self::Output {
        for elem in self.column_iter_mut() {
            *elem += rhs;
        }

        self
    }
}

impl<T: Scalar, const N: usize> Sub<T> for Vector<T, { N }> {
    type Output = Self;

    fn sub(mut self, rhs: T) -> Self::Output {
        for elem in self.column_iter_mut() {
            *elem -= rhs;
        }

        self
    }
}

/// Note that this impl will do *integer* division if `T`
/// happens to be an integer. If you want a floating point
/// representation, call [`Matrix::into_f64`] or
/// [`Matrix::into_f32`] before dividing.
impl<T: Scalar, const N: usize> Div<T> for Vector<T, { N }> {
    type Output = Vector<T, { N }>;

    fn div(mut self, rhs: T) -> Self::Output {
        for elem in self.column_iter_mut() {
            *elem /= rhs
        }

        self
    }
}

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

    #[test]
    fn index() {
        const ARRAY: [[f64; 2]; 3] = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];

        let matrix = Matrix::from_array(ARRAY);

        assert_eq!(matrix[(1, 2)], 6.0);
    }

    #[test]
    fn iter() {
        const ARRAY: [[f64; 2]; 3] = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        const COLUMN_FLATTENED: [f64; 6] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        const ROW_FLATTENED: [f64; 6] = [1.0, 3.0, 5.0, 2.0, 4.0, 6.0];

        let matrix = Matrix::from_array(ARRAY);

        // Check that column_iter() is indeed a column-wise iterator.
        for (i, &matrix_elem) in matrix.column_iter().enumerate() {
            assert_eq!(COLUMN_FLATTENED[i], matrix_elem);
        }

        // And likewise for row_iter()
        for (i, &matrix_elem) in matrix.row_iter().enumerate() {
            assert_eq!(ROW_FLATTENED[i], matrix_elem);
        }

        // The below code shows that `matrix` can not be dropped
        // as it is mutably borrowed. This is just a test two ensure
        // that the unsafe code works as intended.
        //        let mut mut_col_iter = matrix.column_iter_mut();
        //
        //        let mut_borrowed = mut_col_iter.next().unwrap();
        //
        //        drop(mut_col_iter);
        //        drop(matrix);
        //
        //        *mut_borrowed = 0.0;
    }

    #[test]
    fn transpose() {
        const ARRAY: [[f64; 2]; 3] = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        const ITER_TRANSPOSED: [f64; 6] = [1.0, 3.0, 5.0, 2.0, 4.0, 6.0];

        let matrix = Matrix::from_array(ARRAY).transpose();

        // Ensure we get the expected results.
        for (i, &matrix_elem) in matrix.column_iter().enumerate() {
            // eprintln!("{}", matrix_elem);
            assert_eq!(ITER_TRANSPOSED[i], matrix_elem);
        }

        // Check that (Matrix^T)^T is the same as the original
        // matrix.
        assert!(matrix
            .column_iter()
            .zip(matrix.transpose().transpose().column_iter())
            .all(|(x, y)| x == y));

        // Four times?
        assert!(matrix
            .column_iter()
            .zip(
                matrix
                    .transpose()
                    .transpose()
                    .transpose()
                    .transpose()
                    .column_iter()
            )
            .all(|(x, y)| x == y));
    }

    #[test]
    fn matrix_matrix_mul() {
        let first = [[1usize, 2], [3, 4]];
        let second = [[1, 2], [3, 4], [5, 6]];

        let expected = [7usize, 10, 15, 22, 23, 34];

        let multiplied = Matrix::from_array(first) * Matrix::from_array(second);

        // Check each element is correct.
        for (i, &elem) in multiplied.column_iter().enumerate() {
            assert_eq!(expected[i], elem);
        }

        // Sanity check for square matrices, as Matrix::<N, N> * Matrix<N, N> -> Matrix<N, N>
        let m = Matrix::<usize, 5, 5>::from_val(1);
        let n = Matrix::<usize, 5, 5>::from_val(1);

        for arr in m.as_array() {
            for elem in arr {
                eprint!("{:?}, ", elem)
            }
        }

        let square_matrix = m * n;
        let expected = [5; 25];

        for (i, &elem) in square_matrix.column_iter().enumerate() {
            assert_eq!(expected[i], elem);
        }
    }

    #[test]
    fn gauss_elim() {
        let array = [[2, -3, -2], [1, -1, 1], [-1, 2, 2]];
        let expected = [[2.0, 0.0, 0.0], [1.0, 0.5, 0.0], [-1.0, 0.5, -1.0]];

        let matrix = Matrix::from_array(array).into_f64().gauss();
        let eliminated = Matrix::from_array(expected).into_f64();

        // For this matrix, all of these turn out to be completely equal,
        // but approx_eq is used as a safety measure, since this may not
        // always be the case.
        dbg!(matrix.as_array());
        dbg!(eliminated.as_array());
        for (x, y) in matrix.column_iter().zip(eliminated.column_iter()) {
            assert!(x.approx_eq(*y, std::f64::EPSILON, 1.0E-10));
        }
    }

    #[test]
    fn matrix_macro() {
        let first = [1usize, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
        let second = [1usize; 16];
        let third = [1usize, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];

        let mat_one = matrix![
             1usize, 2, 3, 4;
             5, 6, 7, 8;
             9, 10, 11, 12;
             13, 14, 15, 16;
        ];

        let mat_two = matrix![1usize; 4, 4];

        let mat_three = matrix![usize; 4];

        for (i, &elem) in mat_one.column_iter().enumerate() {
            assert_eq!(first[i], elem);
        }

        for (i, &elem) in mat_two.column_iter().enumerate() {
            assert_eq!(second[i], elem);
        }

        for (i, &elem) in mat_three.column_iter().enumerate() {
            assert_eq!(third[i], elem);
        }
    }

    #[test]
    fn vector_macro() {
        let first = [1, 2, 3];
        let second = [1, 1, 1];

        let vec_one = vector![1, 2, 3];
        let vec_two = vector![1; 3];

        for (i, &elem) in vec_one.column_iter().enumerate() {
            assert_eq!(first[i], elem);
        }

        for (i, &elem) in vec_two.column_iter().enumerate() {
            assert_eq!(second[i], elem);
        }
    }

    #[test]
    fn vector_length() {
        use crate::traits::Real as _;

        let expected_f64 = std::f64::consts::SQRT_2;
        let expected_f32 = std::f32::consts::SQRT_2;

        let vec_one = vector![1.0f64; 2];
        let vec_two = vector![1.0f32; 2];

        // eprintln!("{}, {}", vec_one.length(), expected_f64);
        // eprintln!("{}, {}", vec_two.length_f32(), expected_f32);

        assert!(vec_one
            .length()
            .approx_eq(expected_f64, std::f64::EPSILON, 1.0e-6));
        assert!(vec_two
            .length_f32()
            .approx_eq(expected_f32, std::f32::EPSILON, 1.0e-6));
    }

    #[test]
    fn normalize() {
        use crate::traits::Real as _;

        let vector = vector![1, 2, 3];
        let normalized = vector.normalize();

        // eprintln!("{}", normalized.length());
        // eprintln!("{:?}", normalized.as_array());

        assert!(normalized
            .length()
            .approx_eq(1.0f64, std::f64::EPSILON, 1.0e-15));
    }

    #[test]
    fn cross() {
        let v = vector![0, 4, -2];
        let w = vector![3, -1, 5];

        let expected = vector![18, -6, -12];

        assert!(v.cross(&w) == expected);
    }

    #[test]
    #[ignore]
    fn large_matrix_time() {
        // let m1 = matrix![1.0; 16, 16];
        // let n1 = matrix![1.0; 16, 16];
        // let m2 = matrix![1; 32, 32];
        // let n2 = matrix![1; 32, 32];
        // let m3 = matrix![1; 64, 64];
        // let n3 = matrix![1; 64, 64];
        // let m4 = matrix![1; 128, 128];
        // let n4 = matrix![1; 128, 128];
        // let m5 = matrix![1; 4, 4];
        // let n5 = matrix![1; 4, 4];
        // // let n2 = n.clone();
        // // let n3 = n.clone();
        // // let n4 = n.clone();
        // // let n5 = n.clone();
        //
        // let start = Instant::now();
        // let _ = m5 * n5;
        // eprintln!("4: {:?}", start.elapsed());
        //
        // let start = Instant::now();
        // let _ = m1 * n1;
        // eprintln!("16: {:?}", start.elapsed());
        //
        // let start = Instant::now();
        // let _ = m2 * n2;
        // eprintln!("32: {:?}", start.elapsed());
        //
        // let start = Instant::now();
        // let _ = m3 * n3;
        // eprintln!("64: {:?}", start.elapsed());
        //
        // let start = Instant::now();
        // let _ = m4 * n4;
        // eprintln!("128: {:?}", start.elapsed());

        let mut a = matrix![
            1.0, -2.0, 5.0, 2.0;
            5.0, 3.0, 1.0, 3.0;
            4.0, 6.0, 0.0, -4.0;
            2.0, 4.0, -1.0, 0.0;
        ];

        let b = a.clone();

        let c = a.clone();

        let start_gauss = Instant::now();
        a.gauss_in_place();
        eprintln!("gauss: {:?}", start_gauss.elapsed());

        let start_mm = Instant::now();
        let _ = b * c;
        eprintln!("mm: {:?}", start_mm.elapsed());

        let start_det = Instant::now();
        let _ = a.det();
        eprintln!("det: {:?}", start_det.elapsed());
    }

    #[test]
    #[ignore]
    fn debug() {
        let m = matrix![usize; 16];

        dbg!(m);
    }
}