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
use std::{ cmp, fmt, ops };
use libc::{ c_float, size_t };

use super::Vector;

extern "C" {
  static SUB_MATRIX_DIM: size_t;
  fn eq_mats(lhs: *const c_float, rhs: *const c_float, len: size_t) -> bool;
  fn add_mats(lhs1: *const c_float, lhs2: *const c_float, rhs: *mut c_float, len: size_t);
  fn mul_scalar_mat(scalar: c_float, lhs: *const c_float, rhs: *mut c_float, len: size_t);
  fn mul_mats(
    lhs1: *const c_float, lhs1_rows: size_t, lhs1_cols: size_t,
    lhs2: *const c_float, lhs2_rows: size_t, lhs2_cols: size_t,
    rhs: *mut c_float
  );
  fn transpose_mat
    (lhs: *const c_float, lhs_rows: size_t, lhs_cols: size_t, rhs: *mut c_float);
}

/// A representation of a 32-bit float matrix.
///
/// A matrix's elements are stored as a flat, 1-dimensional vector, with an
/// internal tuple which defines the dimensions of the matrix, i.e. how the
/// matrix's elements are interpreted, in (rows, columns) format.
///
/// ```
/// use ml_rust_cuda::math::linear::Matrix;
///
/// let elements =
///   vec![vec![1_f32, 3_f32, 4_f32],
///        vec![2_f32, 5_f32, 8_f32]];
/// let matrix = Matrix::new(elements);
///
/// // "Matrix { elements: [ 1.0, 3.0, 4.0, 2.0, 5.0, 8.0 ], dims: (2, 3) }"
/// println!("{:?}", matrix);
/// ```
#[derive(Clone, Debug)]
pub struct Matrix {
  elements: Vec<f32>,
  dims: (usize, usize)
}

impl Matrix {
  /// Returns a matrix representation from a supplied 2-dimensional [f32] vector.
  ///
  /// # Arguments
  ///
  /// * `elements` - A [Vec] of rows of [f32]s for the matrix.
  ///
  /// # Examples
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let vec_of_vecs =
  ///   vec![vec![1_f32, 3_f32, 4_f32],
  ///        vec![2_f32, 5_f32, 8_f32],
  ///        vec![9_f32, 5_f32, 8_f32],
  ///        vec![3_f32, 4_f32, 0_f32]];
  /// let matrix = Matrix::new(vec_of_vecs);
  ///
  /// println!("{}", matrix);
  /// ```
  ///
  /// # Panics
  ///
  /// Panics if all [Vec]s within `elements` are not of equal length.
  pub fn new(elements: Vec<Vec<f32>>) -> Self {
    let n_cols: usize =
      if let Some(first_row) = elements.get(0) {
        first_row.len()
      } else {
        0
      };

    if let Some(mismatch) = elements.iter().find(|row| row.len() != n_cols) {
      panic!("Matrix row length mismatch: expected {} but got {}", n_cols, mismatch.len());
    }

    let n_rows = elements.len();
    let elements: Vec<f32> = elements.into_iter().flatten().collect();

    Self {
      elements,
      dims: (n_rows, n_cols)
    }
  }

  /// Returns a matrix representation from a flat 1-dimensional [f32] vector
  /// of specified dimensions.
  ///
  /// # Arguments
  ///
  /// * `elements` - A flat vector of [f32]s **stored in row-major form** (i.e.
  ///                elements in the same row are adjacent to each other in
  ///                `elements`).
  /// * `dims` - The dimensions of the matrix in (rows, columns).
  ///
  /// # Examples
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let flat_vec =
  ///   vec![1_f32, 3_f32, 4_f32,
  ///        2_f32, 5_f32, 8_f32,
  ///        9_f32, 5_f32, 8_f32,
  ///        3_f32, 4_f32, 0_f32];
  /// let matrix = Matrix::from_flat(flat_vec, (4, 3));
  ///
  /// println!("{}", matrix);
  /// ```
  pub fn from_flat(elements: Vec<f32>, dims: (usize, usize)) -> Self {
    Self { elements, dims }
  }

  /// Returns a matrix of specified dimensions filled with zeros.
  ///
  /// # Arguments
  ///
  /// * `dims` - The dimensions of the matrix in (rows, columns).
  ///
  /// # Examples
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let matrix = Matrix::zero((4, 3));
  ///
  /// println!("{}", matrix);
  /// ```
  pub fn zero(dims: (usize, usize)) -> Self {
    Self {
      elements: vec![0_f32; dims.0 * dims.1],
      dims: dims
    }
  }

  /// Returns the element of the matrix at a given position, or `None` if the index
  /// is out of bounds of the matrix.
  ///
  /// # Arguments
  ///
  /// * `pos` - The target element's position in (row, column) form, zero-indexed.
  ///
  /// # Examples
  ///
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let vec_of_vecs =
  ///   vec![vec![1_f32, 3_f32, 4_f32],
  ///        vec![2_f32, 5_f32, 8_f32]];
  /// let matrix = Matrix::new(vec_of_vecs);
  ///
  /// assert_eq!(matrix.get((1, 2)), Some(8_f32));
  /// assert!(matrix.get((2, 0)).is_none());
  /// ```
  pub fn get(&self, pos: (usize, usize)) -> Option<f32> {
    Some(self.elements.get(pos.0 * self.dims.1 + pos.1)?.clone())
  }

  /// Replaces the element of the matrix at a given position with a given value.
  /// Does nothing if the position is out of bounds of the matrix.
  ///
  /// # Arguments
  ///
  /// * `pos` - The target element's position in (row, column) form, zero-indexed.
  /// * `val` - The value with which to replace the target element.
  ///
  /// # Examples
  ///
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let vec_of_vecs =
  ///   vec![vec![1_f32, 3_f32, 4_f32],
  ///        vec![2_f32, 5_f32, 8_f32]];
  /// let mut matrix = Matrix::new(vec_of_vecs);
  ///
  /// matrix.set((1, 2), 5_f32);
  ///
  /// assert_eq!(matrix.get((1, 2)), Some(5_f32));
  /// ```
  pub fn set(&mut self, pos: (usize, usize), val: f32) {
    if let Some(elem) = self.elements.get_mut(pos.0 * self.dims.1 + pos.1) {
      *elem = val;
    }
  }

  /// Returns a reference to the matrix's internal flat element vector.
  pub fn elements(&self) -> &Vec<f32> {
    &self.elements
  }

  /// Returns a reference to the matrix's dimension tuple in the form (rows,
  /// columns), zero-indexed.
  pub fn dims(&self) -> &(usize, usize) {
    &self.dims
  }

  /// Returns a copy of the matrix padded with zeros such that each dimension
  /// of the resulting matrix is a multiple of a specified value.
  ///
  /// # Arguments
  ///
  /// * `target` - The number by which the resulting matrix's dimensions should
  ///              be divisible by.
  ///
  /// # Examples
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let vec_of_vecs =
  ///   vec![vec![1_f32, 3_f32],
  ///        vec![2_f32, 5_f32],
  ///        vec![0_f32, 7_f32],
  ///        vec![8_f32, 4_f32]];
  /// let matrix = Matrix::new(vec_of_vecs);
  ///
  /// println!("{}", matrix.zero_padded(3));
  /// ```
  pub fn zero_padded(&self, target: usize) -> Self {
    let n_extra_rows
      = target * ((self.dims.0 / target) + ((self.dims.0 % target != 0) as usize))
        - self.dims.0;
    let n_extra_cols
      = target * ((self.dims.1 / target) + ((self.dims.1 % target != 0) as usize))
        - self.dims.1;

    let mut new_elements: Vec<f32>
      = Vec::with_capacity((self.dims.0 + n_extra_rows) * (self.dims.1 + n_extra_cols));

    for i in 0..self.dims.0 {
      new_elements.extend(&self.elements[(i * self.dims.1)..((i+1) * self.dims.1)]);
      new_elements.extend(vec![0_f32; n_extra_cols]);
    }

    for _ in 0..n_extra_rows {
      new_elements.extend(vec![0_f32; self.dims.1 + n_extra_cols]);
    }

    let new_dims = (self.dims.0 + n_extra_rows, self.dims.1 + n_extra_cols);

    Matrix::from_flat(new_elements, new_dims) 
  }

  /// Returns a copy of the matrix truncated such that the resulting matrix's
  /// dimensions match the provided dimensions.
  ///
  /// # Arguments
  ///
  /// * `new_dims` - The dimensions of the new matrix in (rows, columns).
  ///
  /// # Examples
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let vec_of_vecs =
  ///   vec![vec![1_f32, 3_f32, 0_f32, 0_f32],
  ///        vec![2_f32, 5_f32, 0_f32, 0_f32],
  ///        vec![0_f32, 7_f32, 0_f32, 0_f32],
  ///        vec![8_f32, 4_f32, 0_f32, 0_f32],
  ///        vec![0_f32, 0_f32, 0_f32, 0_f32],
  ///        vec![0_f32, 0_f32, 0_f32, 0_f32]];
  /// let matrix = Matrix::new(vec_of_vecs);
  ///
  /// println!("{}", matrix.truncated((4, 2)));
  /// ```
  pub fn truncated(&self, new_dims: (usize, usize)) -> Self {
    let new_dims =
      (if new_dims.0 > self.dims.0 { self.dims.0 } else { new_dims.0 },
       if new_dims.1 > self.dims.1 { self.dims.1 } else { new_dims.1 });

    let mut new_elements: Vec<f32>
      = Vec::with_capacity(new_dims.0 * new_dims.1);

    for i in 0..new_dims.0 {
      let adjusted_i = i * self.dims.1;
      new_elements.extend(&self.elements[adjusted_i..(adjusted_i + new_dims.1)]);
    }

    Matrix::from_flat(new_elements, new_dims)
  }

  /// Returns a transposed copy of the matrix.
  ///
  /// Uses a CUDA kernel under the hood.
  ///
  /// # Examples
  /// ```
  /// use ml_rust_cuda::math::linear::Matrix;
  ///
  /// let vec_of_vecs =
  ///   vec![vec![1_f32, 3_f32],
  ///        vec![2_f32, 5_f32],
  ///        vec![0_f32, 7_f32],
  ///        vec![8_f32, 4_f32]];
  /// let matrix = Matrix::new(vec_of_vecs);
  ///
  /// println!("{}", matrix.transposed());
  /// ```
  pub fn transposed(&self) -> Self {
    let block_size = unsafe { SUB_MATRIX_DIM };

    // Pad the matrices to BLOCK_SIZE since kernel operates on sub-matrices
    // of size BLOCK_SIZE x BLOCK_SIZE
    let mut result = Matrix::zero((self.dims.1, self.dims.0)).zero_padded(block_size);
    let lhs_padded = self.zero_padded(block_size);

    let lhs = lhs_padded.elements.as_ptr();
    let rhs = result.elements.as_mut_ptr();

    unsafe {
      transpose_mat(lhs, lhs_padded.dims.0, lhs_padded.dims.1, rhs);
    }

    result.truncated((self.dims.1, self.dims.0))
  }
}

impl cmp::PartialEq for Matrix {
  /// Uses a CUDA kernel under the hood.
  fn eq(&self, other: &Self) -> bool {
    if self.elements.len() != other.elements.len() {
      return false;
    }

    let len = self.elements.len();
    let lhs = self.elements.as_ptr();
    let rhs = other.elements.as_ptr();

    self.dims == other.dims && unsafe {
      eq_mats(lhs, rhs, len)
    }
  }
}

impl ops::Add for &Matrix {
  type Output = Matrix;

  /// Uses a CUDA kernel under the hood.
  /// 
  /// # Panics
  ///
  /// Panics if both operand matrices are not of the same dimension.
  fn add(self, other: Self) -> Self::Output {
    if self.dims != other.dims {
      panic!("Matrix addition dimension mismatch: \
              first operand is {}x{} \
              but second operand is {}x{}",
              self.dims.0, self.dims.1,
              other.dims.0, other.dims.1);
    }

    let dims = self.dims;
    let mut result = Matrix::zero(dims);
    let len = dims.0 * dims.1;

    let lhs1 = self.elements.as_ptr();
    let lhs2 = other.elements.as_ptr();
    let rhs  = result.elements.as_mut_ptr();

    unsafe {
      add_mats(lhs1, lhs2, rhs, len);
    }

    result
  }
}

impl ops::Sub for &Matrix {
  type Output = Matrix;

  /// Uses two CUDA kernels under the hood (one for scalar multiplication by -1,
  /// the other for addition).
  /// 
  /// # Panics
  ///
  /// Panics if both operand matrices are not of the same dimension.
  fn sub(self, other: Self) -> Self::Output {
    self + &(-1_f32 * other)
  }
}

impl ops::Mul<&Matrix> for f32 {
  type Output = Matrix;

  /// Uses a CUDA kernel under the hood.
  fn mul(self, other: &Matrix) -> Self::Output {
    let dims = other.dims;
    let mut result = Matrix::zero(dims);
    let len = dims.0 * dims.1;

    let lhs = other.elements.as_ptr();
    let rhs = result.elements.as_mut_ptr();

    unsafe {
      mul_scalar_mat(self, lhs, rhs, len)
    };

    result
  }
}

impl ops::Mul for &Matrix {
  type Output = Matrix;

  /// Uses a CUDA kernel under the hood.
  /// 
  /// # Panics
  ///
  /// Panics if the number of columns in the first operand is not equal to the
  /// number of rows in the second operand.
  fn mul(self, other: Self) -> Self::Output {
    if self.dims.1 != other.dims.0 {
      panic!("Matrix multiplication dimension mismatch: \
              first operand is {}x{} \
              but second operand is {}x{}",
              self.dims.0, self.dims.1,
              other.dims.0, other.dims.1);
    }

    let block_size = unsafe { SUB_MATRIX_DIM };

    // Pad the matrices to BLOCK_SIZE since kernel operates on sub-matrices
    // of size BLOCK_SIZE x BLOCK_SIZE
    let mut result
      = Matrix::zero((self.dims.0, other.dims.1)).zero_padded(block_size);
    let lhs1_padded = self.zero_padded(block_size);
    let lhs2_padded = other.zero_padded(block_size);

    let lhs1 = lhs1_padded.elements.as_ptr();
    let lhs2 = lhs2_padded.elements.as_ptr();
    let rhs  = result.elements.as_mut_ptr();

    unsafe {
      mul_mats(
        lhs1, lhs1_padded.dims.0, lhs1_padded.dims.1,
        lhs2, lhs2_padded.dims.0, lhs2_padded.dims.1,
        rhs
      );
    }

    result.truncated((self.dims.0, other.dims.1))
  }
}

impl ops::Mul<&Vector> for &Matrix {
  type Output = Vector;

  /// Uses the same CUDA kernel as matrix multiplication.
  ///
  /// # Panics
  ///
  /// Panics if the number of columns in the matrix is not equal to the
  /// dimension of the vector.
  fn mul(self, other: &Vector) -> Self::Output {
    Vector::from_matrix(self * other.matrix())
  }
}

impl fmt::Display for Matrix {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let mut matrix_str: String = "[".to_owned();

    for i in 0..self.dims.0 {
      if i != 0 {
        matrix_str += " ";
      }
      matrix_str += "[";
      for j in 0..self.dims.1 {
        matrix_str +=
          &format!("{:>4}", self.elements[i * self.dims.1 + j]);
        if j != self.dims.1 - 1 {
          matrix_str += ",\t";
        }
      }
      matrix_str += "]";
      if i != self.dims.0 - 1 {
        matrix_str += ", \n";
      }
    }
    matrix_str += "]";

    f.write_str(&matrix_str)
  }
}

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

  use crate::math::f32_eq;

  // Happy path tests

  #[test]
  fn test_pad_mat() {
    let pad = 32;
    let dims = ((1 << 10) - 123, (1 << 8) + 42);
    let m = Matrix::new(vec![vec![2_f32; dims.1]; dims.0]);

    let pad_dims = (pad * ((dims.0 / pad) + 1), pad * ((dims.1 / pad) + 1));
    let mut row = vec![2_f32; dims.1];
    row.append(&mut vec![0_f32; pad_dims.1 - dims.1]);
    let mut rows = vec![row; dims.0];
    rows.append(&mut vec![vec![0_f32; pad_dims.1]; pad_dims.0 - dims.0]);
    let expected = Matrix::new(rows);
    assert_eq!(m.zero_padded(pad), expected);
  }

  #[test]
  fn test_trunc_mat() {
    let pad = 32;
    let dims = ((1 << 10) - 123, (1 << 8) + 42);
    let m = Matrix::new(vec![vec![3.14_f32; dims.1]; dims.0]);

    assert_eq!(m.zero_padded(pad).truncated(dims), m);
  }

  #[test]
  fn test_transpose_mat() {
    let dims = ((1 << 10) - 123, (1 << 7) + 13);
    let m = Matrix::new(vec![vec![2.71_f32; dims.1]; dims.0]);

    let expected = Matrix::new(vec![vec![2.71_f32; dims.0]; dims.1]);
    assert_eq!(m.transposed(), expected);
  }

  #[test]
  fn test_mats_equal() {
    let m1 = Matrix::new(vec![vec![1.618_f32; 1 << 10]; (1 << 9) + 10]);
    let m2 = Matrix::new(vec![vec![1.618_f32; 1 << 10]; (1 << 9) + 10]);

    let mut equal = true;
    for i in 0..m1.elements().len() {
      if !f32_eq(m1.elements()[i], m2.elements()[i]) {
        equal = false;
        break;
      }
    }
    assert_eq!(m1 == m2, equal);
  }

  #[test]
  fn test_mats_not_equal() {
    let m1 = Matrix::new(vec![vec![1.618_f32; 1 << 10]; (1 << 9) + 10]);
    let mut m2 = Matrix::new(vec![vec![1.618_f32; 1 << 10]; (1 << 9) + 10]);
    m2.set((1 << 7, 1 << 9), 1.619_f32);
    m2.set((1 << 8, 1 << 5), 2.71_f32);
    m2.set((1 << 9, 1 << 9), 4.20_f32);


    let mut equal = true;
    for i in 0..m1.elements().len() {
      if !f32_eq(m1.elements()[i], m2.elements()[i]) {
        equal = false;
        break;
      }
    }
    assert_eq!(m1 == m2, equal);
  }

  #[test]
  fn test_add_mats() {
    let m1 = Matrix::new(vec![vec![1_f32; 1 << 10]; (1 << 7) + 10]);
    let m2 = Matrix::new(vec![vec![2_f32; 1 << 10]; (1 << 7) + 10]);

    let expected = Matrix::new(vec![vec![3_f32; 1 << 10]; (1 << 7) + 10]);
    assert_eq!(&m1 + &m2, expected);
  }

  #[test]
  fn test_sub_mats() {
    let m1 = Matrix::new(vec![vec![1_f32; 1 << 10]; (1 << 7) + 10]);
    let m2 = Matrix::new(vec![vec![2_f32; 1 << 10]; (1 << 7) + 10]);

    let expected = Matrix::new(vec![vec![-1_f32; 1 << 10]; (1 << 7) + 10]);
    assert_eq!(&m1 - &m2, expected);
  }

  #[test]
  fn test_mul_scalar_mat() {
    let k = 6.022_f32;
    let m = Matrix::new(vec![vec![1_f32; 1 << 10]; (1 << 8) + 10]);

    let expected = Matrix::new(vec![vec![6.022_f32; 1 << 10]; (1 << 8) + 10]);
    assert_eq!(k * &m, expected);
  }

  #[test]
  fn test_mul_mats() {
    let dims = ((1 << 10) + 10, (1 << 10) - 123, 1 << 5);
    let m1 = Matrix::new(vec![vec![2_f32; dims.0]; dims.1]);
    let m2 = Matrix::new(vec![vec![3_f32; dims.2]; dims.0]);

    let expected
      = Matrix::new(vec![vec![2_f32 * 3_f32 * dims.0 as f32; dims.2]; dims.1]);
    assert_eq!(&m1 * &m2, expected);
  }

  #[test]
  fn test_mul_mat_vec() {
    let dims = ((1 << 10) + 10, (1 << 10) - 123, 1 << 5);
    let m = Matrix::new(vec![vec![2_f32; dims.0]; dims.1]);
    let v = Vector::new(vec![3_f32; dims.0]);

    let expected
      = Vector::new(vec![2_f32 * 3_f32 * dims.0 as f32; dims.1]);
    assert_eq!(&m * &v, expected);
  }

  // Failure tests

  #[test]
  #[should_panic(expected = "Matrix row length mismatch")]
  fn test_new_row_mismatch() {
    let mut vecs = vec![vec![1_f32; 1 << 8]; 1 << 9];
    vecs.push(vec![1_f32; 1 << 10]);
    vecs.extend(vec![vec![1_f32; 1 << 8]; 1 << 9]);

    Matrix::new(vecs);
  }

  #[test]
  #[should_panic(expected = "Matrix addition dimension mismatch")]
  #[allow(unused_must_use)]
  fn test_add_dim_mismatch() {
    let m1 = Matrix::new(vec![vec![3_f32; 1 << 10]; 1 << 10]);
    let m2 = Matrix::new(vec![vec![3_f32; (1 << 10) + 12]; 1 << 10]);

    &m1 + &m2;
  }

  #[test]
  #[should_panic(expected = "Matrix multiplication dimension mismatch")]
  #[allow(unused_must_use)]
  fn test_mul_dim_mismatch() {
    let m1 = Matrix::new(vec![vec![3_f32; 1 << 9]; 1 << 10]);
    let m2 = Matrix::new(vec![vec![3_f32; 1 << 9]; 1 << 10]);

    &m1 * &m2;
  }
}