matrix_kit 0.2.35

An abstract linear algebra library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
use std::cmp::min;
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Range, Sub, SubAssign};
use std::usize;
use algebra_kit::algebra::{Field, Ring};
use rand_distr::Distribution;
use crate::index;
use crate::dynamic::dynamic_vector_util::*;

// MARK: Matrix Type

/// A dynamically sized matrix with entries of a type `T`
/// 
/// Data is stored column-wise, so adjacent elements of the
/// flatmap vector are in the same column (modulo column breaks)
/// 
/// If T is a Ring, many more useful operations open up. When T is an abstract 
/// data type, this is pretty much only useful for storage.
#[derive(Clone)]
pub struct Matrix<T: Clone> {
	flatmap: Vec<T>,
	row_count: usize,
	col_count: usize
}


#[macro_export]
macro_rules! compatible_vectors {
	($a: expr, $b: expr) => {
		$a.is_vector() && $b.is_vector() && ($a.row_count() == $b.row_count())
	};
}

impl<T: Clone> Matrix<T> {

	// MARK: Constructors

	/// Constructs a matrix from a flat vector
	pub fn from_flatmap(rows: usize, cols: usize, flatmap: Vec<T>) -> Matrix<T> {
		Matrix { flatmap, row_count: rows, col_count: cols }
	}

	/// Constructs a matrix defined index-wise
	pub fn from_index_def(
		rows: usize,
		cols: usize,
		at_index: &mut dyn FnMut(usize, usize) -> T) -> Matrix<T> {

		Matrix::from_flatmap(rows, cols, Vec::from_iter((0..(rows * cols)).map(|i|
			at_index(i % rows, i / rows)
		)))
	}

	

	// MARK: Properties

	/// Returns whether or not this is a square matrix
	pub fn is_square(&self) -> bool {
		self.row_count() == self.col_count()
	}

	/// The amount of rows in this matrix
	#[inline]
	pub fn row_count(&self) -> usize {
		self.row_count
	}

	/// The amount of columns in this matrix
	#[inline]
	pub fn col_count(&self) -> usize {
		self.col_count
	}

	/// Returns a copy of the underlying vector of this matrix
	pub fn as_vec(&self) -> Vec<T> {
		self.flatmap.clone()
	}

	// MARK: Utility

	/// Returns the columns of this matrix
	pub fn columns(&self) -> Vec<Matrix<T>> {
		(0..self.col_count()).into_iter().map(|c| 
			Matrix::from_flatmap(self.row_count(), 1, self.flatmap[
				index!(self.row_count(), self.col_count(), 0, c)..index!(self.row_count(), self.col_count(), self.row_count(), c)
			].to_vec())
		).collect()
	}

	/// Returns `true` if this matrix is really just a column vector
	pub fn is_vector(&self) -> bool {
		self.col_count() == 1
	}

	/// Returns whether or not this is a row vector
	pub fn is_row_vector(&self) -> bool {
		self.row_count() == 1
	}

	/// Returns a copy of the entry at row `r` and column `c`
	pub fn get(&self, r: usize, c: usize) -> T {
		debug_assert!(r < self.row_count());
		debug_assert!(c < self.col_count());
		self.flatmap[index!(self.row_count, self.col_count, r, c)].clone()
	}

	/// Sets the entry at row `r` and column `c` to `x`
	pub fn set(&mut self, r: usize, c: usize, x: T) {
		debug_assert!(r < self.row_count());
		debug_assert!(c < self.col_count());
		self.flatmap[index!(self.row_count, self.col_count, r, c)] = x;
	} 

	/// Appends a reference to a column to this matrix on the right
	pub fn append_col_ref(&mut self, col: &mut Vec<T>) { 
		assert_eq!(col.len(), self.row_count());

		self.col_count += 1;
		self.flatmap.append(col);
	}

	/// Appends a reference to a matrix to this matrix on the right
	pub fn append_mat_right_ref(&mut self, mat: &mut Matrix<T>) {
		assert_eq!(self.row_count(), mat.row_count());

		self.col_count += mat.col_count();
		self.flatmap.append(&mut mat.flatmap);
	}

	/// Appends a column to this matrix on the right
	pub fn append_col(&mut self, col: Vec<T>) {
		assert_eq!(col.len(), self.row_count());

		let mut other_col = col.clone();
		self.flatmap.append(&mut other_col);

		self.col_count += 1;
	}

	/// Appends a matrix to this matrix, on the right
	pub fn append_mat_right(&mut self, mat: Matrix<T>) {
		assert_eq!(self.row_count(), mat.row_count());
		assert!(!mat.flatmap.is_empty());
		let mut new_flatmap = vec![mat.get(0, 0) ; self.flatmap.len() + mat.flatmap.len()];

		for i in 0..self.flatmap.len() {
			new_flatmap[i] = self.flatmap[i].clone();
		}

		for i in 0..mat.flatmap.len() {
			new_flatmap[i + self.flatmap.len()] = mat.flatmap[i].clone();
		}

		self.col_count += mat.col_count();
		self.flatmap = new_flatmap;
	}

	/// Appends a row to this matrix on the bottom
	pub fn append_row(&mut self, row: Vec<T>) {
		assert!(!row.is_empty());
		assert_eq!(row.len(), self.col_count());

		let mut new_flatmap = vec![row[0].clone() ; self.flatmap.len() + row.len()];
		
		// Set the original values in the new flatmap!
		for r in 0..self.row_count() {
			for c in 0..self.col_count() {
				new_flatmap[
					index!(self.row_count() + 1, self.col_count(), r, c)
				] = self.flatmap[
					index!(self.row_count(), self.col_count(), r, c)
				].clone();
			}
		}

		// set the new row in the flatmap!
		for c in 0..row.len() {
			new_flatmap[
				index!(
					self.row_count() + 1, 
					self.col_count(), 
					self.row_count(), c
				)
			] = row[c].clone();
		}

		self.row_count += 1;
		self.flatmap = new_flatmap;
	}

	/// Appends a matrix to this matrix, on the bottom
	pub fn append_mat_bottom(&mut self, mat: Matrix<T>) {
		debug_assert_eq!(self.col_count(), mat.col_count());


		let new = Matrix::from_index_def(self.row_count() + mat.row_count(), self.col_count(), &mut |r, c|
			if r < self.row_count() {
				self.get(r, c)
			} else {
				mat.get(r - self.row_count(), c)
			}
		);

		*self = new
	}

	// MARK: Utility

	/// Applies a function to all entries in this matrix, returning the result 
	/// as a separate matrix
	pub fn applying_to_all<J: Ring>(&self, f: &dyn Fn(T) -> J) -> Matrix<J> {
		Matrix { 
			flatmap: self.flatmap.iter().map(|x| f(x.clone())).collect(), 
			row_count: self.row_count(), 
			col_count: self.col_count() 
		}
	}

	/// Applies a function to all entries in this matrix, in place
	pub fn apply_to_all(&mut self, f: &dyn Fn(T) -> T) {
		for i in 0..self.flatmap.len() {
			self.flatmap[i] = f(self.flatmap[i].clone())
		}
	}

	/// The transpose of this matrix 
	pub fn transpose(&self) -> Matrix<T> {
		Matrix::from_index_def(self.col_count(), self.row_count, &mut |r, c| self.get(c, r))
	}

	/// Accesses a sub-matrix of this matrix
	pub fn get_submatrix(&self, row_range: Range<usize>, col_range: Range<usize>) -> Matrix<T> {
		Matrix::from_index_def(row_range.len(), col_range.len(), &mut |r, c| 
			self.get(r + row_range.start, c + col_range.start)
		)
	}

	/// Writes to a sub-matrix of this matrix
	pub fn set_submatrix(&mut self, row_range: Range<usize>, col_range: Range<usize>, submat: Matrix<T>) {
		debug_assert_eq!(row_range.len(), submat.row_count());
		debug_assert_eq!(col_range.len(), submat.col_count());

		debug_assert!(row_range.end <= self.row_count());
		debug_assert!(col_range.end <= self.col_count());

		for r in row_range.clone() {
			for c in col_range.clone() {
				self.set(r, c,
					submat.get(r - row_range.start, c - col_range.start));
			}
		}
	}

	

}

impl<R: Ring> Matrix<R> {

	// MARK: Initializers

	/// Constructs a matrix with the given columns
	pub fn from_cols(columns: Vec<Matrix<R>>) -> Matrix<R> {
		let m = columns[0].row_count();
		// Make sure they are all vectors of the same size
		debug_assert!(columns.iter().map(|c| c.is_vector()).reduce(|acc, e| acc && e).unwrap());
		debug_assert!(columns.iter().map(|c| c.row_count() == m).reduce(|acc, e| acc && e).unwrap());

		let mut flatmap = vec![R::zero() ; columns.len() * m];

		for c in 0..columns.len() {
			for r in 0..m {
				flatmap[index!(m, columns.len(), r, c)] = columns[c].get(r, 0);
			}
		}

		Matrix { flatmap, row_count: m, col_count: columns.len() }
	}

	/// Constructs a matrix of all zeroes for a given dimension
	pub fn new(rows: usize, cols: usize) -> Matrix<R> {
		Matrix::from_flatmap(rows, cols, vec![R::zero() ; rows * cols])
	}

	/// Constructs a matrix of all ones
	pub fn ones(rows: usize, cols: usize) -> Matrix<R> {
		Matrix { 
			flatmap: vec![R::one() ; rows * cols], 
			row_count: rows, 
			col_count: cols 
		}
	}

	/// Returns the diagonal of this matrix as a list
	pub fn get_diagonal(&self) -> Vec<R> {
		let mut diagonal = vec![R::zero() ; min(self.col_count(), self.row_count())];
		for i in 0..diagonal.len() {
			diagonal[i] = self.get(i, i)
		}
		diagonal
	}

	/// Returns the upper diagonal of this matrix as a list
	pub fn get_upperdiagonal(&self) -> Vec<R> {
		let mut upper_diagonal = vec![R::zero() ; min(self.col_count(), self.row_count()) - 1];
		for i in 0..upper_diagonal.len() {
			upper_diagonal[i] = self.get(i, i + 1)
		}
		upper_diagonal
	}

	// MARK: Math

	/// Computes the inner-product of this vector with another vector
	/// 
	/// This operates in the entries in the flatmap of each matrix, so if 
	/// the argument to this function are not proper vectors (i.e., they 
	/// are matrices with both dimensions greater than 1) then the behavior
	/// here is not well-defined
	pub fn inner_product(&self, other: &Matrix<R>) -> R {
		debug_assert_eq!(self.flatmap.len(), other.flatmap.len());

		let mut inner_product = R::zero();

		for i in 0..self.flatmap.len() {
			inner_product += self.flatmap[i].clone() * other.flatmap[i].clone();
		}

		inner_product
	}

	/// The squared L2 norm of this vector
	pub fn l2_norm_squared(&self) -> R {
		self.inner_product(self)
	}

	/// Computes the point-wise product of this and another matrix of the same 
	/// dimension
	pub fn hadamard(&self, other: Matrix<R>) -> Matrix<R> {

		debug_assert_eq!(self.col_count(), other.col_count());
		debug_assert_eq!(self.row_count(), other.row_count());

		let mut hada = self.clone();

		for i in 0..(self.flatmap.len()) { 
			hada.flatmap[i] *= other.flatmap[i].clone()
		}

		hada
	}

	
	/// Constructs the rows * cols identity matrix
	pub fn identity(rows: usize, cols: usize) -> Matrix<R> {
		let mut mat = Matrix::new(rows, cols);
		let limiting_dimension = min(rows, cols);

		for i in 0..limiting_dimension {
			mat.set(i, i, R::one());
		}

		mat
	}

	/// Creates a diagonal matrix from a given diagonal
	pub fn from_diagonal(diagonal: Vec<R>) -> Matrix<R> {
		Matrix::from_index_def(diagonal.len(), diagonal.len(), &mut |r, c| if r == c {
			diagonal[r].clone()
		} else {
			R::zero()
		})
	}

	/// Creates a block-diagonal matrix from square blocks
	pub fn from_block_diagonal(blocks: Vec<Matrix<R>>) -> Matrix<R> {
		debug_assert!(blocks.iter().map(|b| b.is_square()).reduce(|b1, b2| b1 && b2).unwrap());

		let sizes: Vec<usize> = blocks.iter().map(|b| b.col_count()).collect();
		let n = sizes.iter().sum();

		let mut matrix = Matrix::new(n, n);

		let mut current_start = 0;

		for i in 0..blocks.len() {
			matrix.set_submatrix(
				current_start..(current_start + sizes[i]), 
				current_start..(current_start + sizes[i]), 
				blocks[i].clone()
			);
			current_start += sizes[i];
		}

		matrix
	}

	/// Creates an upper bidiagonal matrix from a diagonal and superdiagonal
	pub fn from_bidiagonal(diagonal: Vec<R>, superdiagonal: Vec<R>) -> Matrix<R> {
		debug_assert_eq!(diagonal.len(), superdiagonal.len() + 1);
		Matrix::from_index_def(diagonal.len(), diagonal.len(), &mut |r, c| if r == c {
			diagonal[r].clone()
		} else if c > 0 && r == c - 1 {
			superdiagonal[r].clone()
		} else {
			R::zero()
		})
	}

	/// Returns true if this matrix is the identity matrix
	pub fn is_identity(&self) -> bool {
		for r in 0..self.row_count() {
			for c in 0..self.col_count() {
				if r == c {
					if self.get(r, c) != R::one() {
						return false;
					}
					else if self.get(r, c) != R::zero() {
						return false;
					}
				}
			}
		}

		return true;
	}
}

// MARK: Debug

impl<T: Debug + Clone> Debug for Matrix<T> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		// get the widest value so we know how many spaces we need!
		let mut widest_str_len = 0;
		for i in 0..(self.flatmap.len()) {
			let as_str = format!("{:?}", self.flatmap[i]);
			let this_len = as_str.chars().count();
			if this_len > widest_str_len {
				widest_str_len = this_len;
			}
		}

		let m = self.row_count();
		let n = self.col_count();

		// now, we make a vector of strings!
		let mut lines = Vec::<String>::new();

		if m == 1 {

			let mut this_line = Vec::<String>::new();

			this_line.push("[ ".to_string());

			for c in 0..n {
				let this_entry_str = format!("{:?}", self.flatmap[index!(m, n, 0, c)]);
				let this_entry_len = this_entry_str.chars().count();
				this_line.push(format!("{}{}", this_entry_str, " ".repeat(widest_str_len - this_entry_len + 1)));
			}

			this_line.push("]".to_string());

			lines.push(this_line.join(""));
			
		} else {
			for r in 0..m {
				let mut this_line = Vec::<String>::new();
	
				if r == 0 {
					this_line.push("".to_string());
				} else if r == m - 1 {
					this_line.push("".to_string())
				} else {
					this_line.push("".to_string())
				}
	
				for c in 0..n {
					let this_entry_str = format!("{:?}", self.flatmap[index!(m, n, r, c)]);
					let this_entry_len = this_entry_str.chars().count();
					this_line.push(format!("{}{}", this_entry_str, " ".repeat(widest_str_len - this_entry_len + 1)));
				}
	
				if r == 0 {
					this_line.push("".to_string());
				} else if r == m - 1 {
					this_line.push("".to_string())
				} else {
					this_line.push("".to_string())
				}
	
				lines.push(this_line.join(""));
			}
		}
		

		write!(f, "\n{}", lines.join("\n"))
	}
}

// MARK: Index

impl<T: Clone> Index<usize> for Matrix<T> {
	type Output = [T];

	/// Returns the column `index` as a slice
	fn index(&self, index: usize) -> &Self::Output {
		&self.flatmap[
			index!(self.row_count, self.col_count, 0, index)..
			index!(self.row_count, self.col_count, self.row_count, index)
		]
	}
}

impl<T: Clone> IndexMut<usize> for Matrix<T> {

	/// Returns a mutable reference to the column `index`
	fn index_mut(&mut self, index: usize) -> &mut Self::Output {
		&mut self.flatmap[
			index!(self.row_count, self.col_count, 0, index)..
			index!(self.row_count, self.col_count, self.row_count, index)
		]
	}
}

// MARK: Comparison

impl<T: Clone + PartialEq> PartialEq for Matrix<T> {
	fn eq(&self, other: &Self) -> bool {
		self.row_count == other.row_count && self.col_count == other.col_count && self.flatmap == other.flatmap
	}
}


// MARK: Operations

impl<R: Ring> Add for Matrix<R> {
	type Output = Matrix<R>;

	fn add(self, rhs: Matrix<R>) -> Matrix<R> {
		assert_eq!(self.row_count, rhs.row_count);
		assert_eq!(self.col_count, rhs.col_count);

		let mut out = Matrix::<R>::new(self.row_count, self.col_count);
		mat_add(self.row_count, self.col_count, 
			&self.flatmap, &rhs.flatmap,
			&mut out.flatmap
		);
		out
	}
}

impl<R: Ring> AddAssign for Matrix<R> {
	fn add_assign(&mut self, rhs: Matrix<R>) {
		assert_eq!(self.row_count, rhs.row_count);
		assert_eq!(self.col_count, rhs.col_count);

		mat_add_assign(self.row_count, self.col_count, 
			&mut self.flatmap, &rhs.flatmap
		);
	}
}

impl<R: Ring> Sub for Matrix<R> {
	type Output = Matrix<R>;

	fn sub(self, rhs: Matrix<R>) -> Matrix<R> {
		assert_eq!(self.row_count, rhs.row_count);
		assert_eq!(self.col_count, rhs.col_count);

		let mut out = Matrix::<R>::new(self.row_count, self.col_count);
		mat_sub(self.row_count, self.col_count, 
			&self.flatmap, &rhs.flatmap, 
			&mut out.flatmap
		);
		out
	}
}

impl<R: Ring> SubAssign for Matrix<R> {
	fn sub_assign(&mut self, rhs: Matrix<R>) {
		assert_eq!(self.row_count, rhs.row_count);
		assert_eq!(self.col_count, rhs.col_count);

		mat_sub_assign(self.row_count, self.col_count, 
			&mut self.flatmap, &rhs.flatmap
		);
	}
}

impl<R: Ring> Mul<R> for Matrix<R> {
	type Output = Matrix<R>;

	fn mul(self, rhs: R) -> Self::Output {
		let mut out = Matrix::<R>::new(self.row_count, self.col_count);
		scalar_mul(self.row_count * self.col_count, 
			rhs, 
			&self.flatmap, &mut out.flatmap
		);
		out
	}
}

impl<R: Ring> MulAssign<R> for Matrix<R> {
	fn mul_assign(&mut self, rhs: R) {
		scalar_mul_assign(self.row_count * self.col_count, 
			rhs, &mut self.flatmap
		);
	}
}

impl<F: Field> Div<F> for Matrix<F> {
	type Output = Matrix<F>;

	fn div(self, rhs: F) -> Matrix<F> {
		let mut out = Matrix::<F>::new(self.row_count, self.col_count);
		scalar_div(self.row_count * self.col_count, 
			rhs, 
			&self.flatmap, &mut out.flatmap
		);
		out
	}
}

impl<F: Field> DivAssign<F> for Matrix<F> {
	fn div_assign(&mut self, rhs: F) {
		scalar_div_assign(self.row_count * self.col_count, 
			rhs, &mut self.flatmap
		);
	}
}

impl<R: Ring> Mul<Matrix<R>> for Matrix<R> {
	type Output = Matrix<R>;

	fn mul(self, rhs: Matrix<R>) -> Self::Output {
		assert_eq!(self.col_count, rhs.row_count, "Attempting to multiply a {} x {} matrix with a {} x {} matrix.", self.row_count(), self.col_count(), rhs.row_count(), rhs.col_count());

		let mut out = Matrix::<R>::new(self.row_count, rhs.col_count);
		mat_mul_ptrs::<R>(self.row_count, self.col_count, rhs.col_count, 
			&self.flatmap, &rhs.flatmap, &mut out.flatmap
		);
		out
	}
}

impl<R: Ring> MulAssign<Matrix<R>> for Matrix<R> {

	/// Performs in-place multiplication, only valid on square matrices
	fn mul_assign(&mut self, rhs: Matrix<R>) {
		assert_eq!(self.col_count, self.row_count); // Only on square matrices!
		assert_eq!(self.col_count, rhs.col_count); // make sure the other matrix is chill
		assert_eq!(self.row_count, rhs.row_count);

		mat_mul_ptrs_assign(self.row_count, 
			&mut self.flatmap, &rhs.flatmap
		);
	}
}

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

	// MARK: Operator tests

	#[test]
	fn test_add() {
		let a = Matrix::from_flatmap(2, 2, vec![1, 2, 3, 4]);
		let b = Matrix::from_flatmap(2, 2, vec![5, 6, 7, 8]);
		let c = a + b;
		assert_eq!(c.flatmap, vec![6, 8, 10, 12]);
	}

	#[test]
	fn test_transpose() {
		let a = Matrix::from_flatmap(16, 1, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
		println!("{:?}", a.transpose());
	}

	#[test]
	fn test_index_def() {
		let a = Matrix::from_index_def(10, 9, &mut {|r, c|
			if r == c { 1 } else {0}
		} );

		println!("{:?}", a.transpose());
	}

}



// MARK: Matrices over Fields

impl<F: Field> Matrix<F> {

	/// Returns whether or not two vectors are orthogonal
	pub fn is_orthogonal_to(&self, other: Matrix<F>) -> bool {
		debug_assert!(compatible_vectors!(self, other));
		self.inner_product(&other) == F::zero()
	}

	/// Returns whether or not a matrix is orthogonal (meaning its columns 
	/// are each orthogonal and of unit length)
	pub fn is_orthogonal(&self) -> bool {
		(self.transpose() * self.clone()).is_identity()
	}

	/// Projects this vector onto another vector
	pub fn proj_onto(&self, other: Matrix<F>) -> Matrix<F> {
		debug_assert!(compatible_vectors!(self, other));
		let scalar = other.inner_product(self) / other.inner_product(&other);
		other * scalar
	}

	/// Performs Gram-Schmidt Orthogonalization on a matrix, returning a
	/// matrix whose columns are orthogonal, and span the same column space 
	/// as the original matrix. This does NOT normalize the GS vectors.
	pub fn gram_schmidt(&self) -> Matrix<F> {
		let v = self.columns();

		let mut u = v.clone();

		// The first GS vector is just the normalized regular guy
		u[0] = v[0].clone();

		for k in 1..u.len() {
				u[k] = v[k].clone();
			for i in 0..k {
				u[k] = u[k].clone() - u[k].proj_onto(u[i].clone())
			}
		}

		Matrix::from_cols(u)
	}

}

// MARK: Linear Algebra over the Reals

impl Matrix<f64> {

	// MARK: Matrix Initialization

	/// Constructs a random matrix with gaussianly distributed entries
    pub fn random_normal(rows: usize, cols: usize, mean: f64, variance: f64) -> Matrix<f64> {
        let mut rand_gen = rand::rng();
        let normal = rand_distr::Normal::new(mean, variance).unwrap();
		Matrix::from_index_def(rows, cols, &mut |_, _| normal.sample(&mut rand_gen))
    }

	// MARK: Vector utility

	/// Normalizes this vector
	pub fn normalize(&mut self) {
		debug_assert!(self.is_vector());
		*self /= self.l2_norm_squared().sqrt();
	}

	/// Returns the normalized version of this vector
	pub fn normalized(&self) -> Matrix<f64> {
		debug_assert!(self.is_vector());
		let mut unit = self.clone();
		unit.normalize();
		unit
	}

	/// Returns the angle of this 2D vector with the x-axis
	pub fn angle(&self) -> f64 {
		debug_assert!(self.is_vector());
		debug_assert_eq!(self.row_count(), 2);

		let x = self.get(0, 0);
		let y = self.get(1, 0);

		y.atan2(x)
	}

}