-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
847 lines (737 loc) · 22 KB
/
matrix.h
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
#pragma once
#ifndef MATRIX_H
#define MATRIX_H
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cmath>
#include <random>
#include <chrono>
namespace Matrix
{
template <typename MatrixRow>
class MatrixRowIterator
{
public:
// Member type definitions to conform to iterator requirements
using value_type = typename MatrixRow::value_type; // Type of elements the iterator refers to
using pointer = value_type *; // Pointer to the element type
using reference = value_type &; // Reference to the element type
using iterator_category = std::random_access_iterator_tag; // Iterator category to support random access
using difference_type = std::ptrdiff_t; // Type to express the difference between two iterators
// Constructor initializes the iterator with a pointer to a matrix row element
MatrixRowIterator(pointer ptr) : m_ptr(ptr)
{
}
// Pre-increment operator advances the iterator to the next element and returns a reference to the updated iterator
MatrixRowIterator &operator++()
{
m_ptr++;
return *this;
}
// Post-increment operator advances the iterator to the next element and returns the iterator before advancement
MatrixRowIterator operator++(int)
{
MatrixRowIterator it = *this;
++*this;
return it;
}
// Addition operator returns a new iterator advanced by 'n' positions
MatrixRowIterator operator+(difference_type n) const
{
return MatrixRowIterator(m_ptr + n);
}
// Compound addition operator advances the iterator by 'n' positions and returns a reference to the updated iterator
MatrixRowIterator &operator+=(difference_type n)
{
m_ptr += n;
return *this;
}
// Pre-decrement operator moves the iterator to the previous element and returns a reference to the updated iterator
MatrixRowIterator &operator--()
{
m_ptr--;
return *this;
}
// Post-decrement operator moves the iterator to the previous element and returns the iterator before movement
MatrixRowIterator operator--(int)
{
MatrixRowIterator it = *this;
--*this;
return it;
}
// Subtraction operator returns a new iterator moved back by 'n' positions
MatrixRowIterator operator-(difference_type n) const
{
return MatrixRowIterator(m_ptr - n);
}
// Compound subtraction operator moves the iterator back by 'n' positions and returns a reference to the updated iterator
MatrixRowIterator &operator-=(difference_type n)
{
m_ptr -= n;
return *this;
}
// Subtraction operator calculates the difference between two iterators
difference_type operator-(const MatrixRowIterator &other) const
{
return m_ptr - other.m_ptr;
}
// Arrow operator provides access to the element's members the iterator points to
pointer operator->() const
{
return m_ptr;
}
// Dereference operators return a (const) reference to the element the iterator points to
reference operator*()
{
return *m_ptr;
}
const reference operator*() const
{
return *m_ptr;
}
// Comparison operators for equality and inequality checks between iterators
bool operator==(const MatrixRowIterator &other) const
{
return m_ptr == other.m_ptr;
}
bool operator!=(const MatrixRowIterator &other) const
{
return m_ptr != other.m_ptr;
}
// Relational operators compare the positions of two iterators
bool operator<(const MatrixRowIterator &other) const
{
return m_ptr < other.m_ptr;
}
bool operator<=(const MatrixRowIterator &other) const
{
return m_ptr <= other.m_ptr;
}
bool operator>(const MatrixRowIterator &other) const
{
return m_ptr > other.m_ptr;
}
bool operator>=(const MatrixRowIterator &other) const
{
return m_ptr >= other.m_ptr;
}
// Subscript operator provides random access to elements relative to the current iterator position
reference operator[](difference_type n) const
{
return *(*this + n);
}
private:
pointer m_ptr; // Internal pointer to the current element
};
//--------------------------------------------------------------------------
template <typename T>
class MatrixColumnIterator
{
public:
// Type aliases for iterator traits
using value_type = T; // Type of elements the iterator can dereference
using pointer = T *; // Pointer to the element type
using reference = T &; // Reference to the element type
using iterator_category = std::random_access_iterator_tag; // Iterator category defining the capabilities of the iterator
using difference_type = std::ptrdiff_t; // Type to express the difference between two iterators
// Constructor initializes the iterator with a pointer to a matrix element and the total number of columns in the matrix
MatrixColumnIterator(pointer ptr, size_t totalColumns) : m_ptr(ptr), m_totalColumns(totalColumns)
{
}
// Pre-increment operator advances the iterator to the next element in the column and returns a reference to the updated iterator
MatrixColumnIterator &operator++()
{
m_ptr += m_totalColumns; // Move pointer down one row in the current column
return *this;
}
// Post-increment operator advances the iterator to the next element in the column and returns the iterator before the increment
MatrixColumnIterator operator++(int)
{
MatrixColumnIterator it = *this; // Make a copy of the current iterator
m_ptr += m_totalColumns; // Move pointer down one row in the current column
return it; // Return the copy representing the iterator before increment
}
// Addition operator returns a new iterator advanced by 'n' positions in the column
MatrixColumnIterator operator+(difference_type n) const
{
return MatrixColumnIterator(m_ptr + (n * m_totalColumns), m_totalColumns); // Calculate new position and create a new iterator
}
// Compound addition operator advances the iterator by 'n' positions in the column and returns a reference to the updated iterator
MatrixColumnIterator &operator+=(difference_type n)
{
m_ptr += (n * m_totalColumns); // Adjust pointer by 'n' rows down in the current column
return *this;
}
// Pre-decrement operator moves the iterator to the previous element in the column and returns a reference to the updated iterator
MatrixColumnIterator &operator--()
{
m_ptr -= m_totalColumns; // Move pointer up one row in the current column
return *this;
}
// Post-decrement operator moves the iterator to the previous element in the column and returns the iterator before the decrement
MatrixColumnIterator operator--(int)
{
MatrixColumnIterator it = *this; // Make a copy of the current iterator
m_ptr -= m_totalColumns; // Move pointer up one row in the current column
return it; // Return the copy representing the iterator before decrement
}
// Subtraction operator returns a new iterator moved back by 'n' positions in the column
MatrixColumnIterator operator-(difference_type n) const
{
return MatrixColumnIterator(m_ptr - (n * m_totalColumns), m_totalColumns); // Calculate new position and create a new iterator
}
// Compound subtraction operator moves the iterator back by 'n' positions in the column and returns a reference to the updated iterator
MatrixColumnIterator &operator-=(difference_type n)
{
m_ptr -= (n * m_totalColumns); // Adjust pointer by 'n' rows up in the current column
return *this;
}
// Subtraction operator calculates the difference between two iterators in terms of column positions
difference_type operator-(const MatrixColumnIterator &other) const
{
return (m_ptr - other.m_ptr) / m_totalColumns; // Calculate element-wise distance between iterators
}
// Comparison operators for checking equality and inequality between iterators
bool operator==(const MatrixColumnIterator &other) const
{
return m_ptr == other.m_ptr;
}
bool operator!=(const MatrixColumnIterator &other) const
{
return m_ptr != other.m_ptr;
}
// Relational operators for ordering iterators
bool operator<(const MatrixColumnIterator &other) const
{
return m_ptr < other.m_ptr;
}
bool operator<=(const MatrixColumnIterator &other) const
{
return m_ptr <= other.m_ptr;
}
bool operator>(const MatrixColumnIterator &other) const
{
return m_ptr > other.m_ptr;
}
bool operator>=(const MatrixColumnIterator &other) const
{
return m_ptr >= other.m_ptr;
}
// Dereference operator provides access to the current element the iterator points to
reference operator*() const
{
return *m_ptr;
}
// Member access operator allows access to the element's members
pointer operator->() const
{
return m_ptr;
}
// Subscript operator provides random access to elements relative to the current iterator position
reference operator[](difference_type n) const
{
return *(*this + n);
}
private:
pointer m_ptr; // Pointer to the current element in the matrix
size_t m_totalColumns; // Total number of columns in the matrix, used for column-wise navigation
};
//--------------------------------------------------------------------------
template <typename Matrix>
class MatrixIterator
{
public:
using value_type = typename Matrix::value_type;
using pointer = value_type *;
using reference = value_type &;
MatrixIterator(pointer ptr) : m_ptr(ptr)
{
}
MatrixIterator &operator++()
{
m_ptr++;
return *this;
}
MatrixIterator operator++(int)
{
MatrixIterator it = *this;
++(this);
return it;
}
MatrixIterator &operator--()
{
m_ptr--;
return *this;
}
MatrixIterator operator--(int)
{
MatrixIterator it = *this;
--(this);
return it;
}
pointer operator->()
{
return m_ptr;
}
reference operator*()
{
return *m_ptr;
}
bool operator==(MatrixIterator other)
{
return this->m_ptr == other.m_ptr;
}
bool operator!=(MatrixIterator other)
{
return this->m_ptr != other.m_ptr;
}
private:
pointer m_ptr;
};
//-------------------------------------------------------------------------
template <typename T>
class MatrixRow
{
public:
using value_type = T;
using Iterator = MatrixRowIterator<MatrixRow<T>>;
MatrixRow() = default;
explicit MatrixRow(size_t size) : m_Size(size), m_Capacity(size * sizeof(T)), m_Data(std::make_unique<T[]>(size)) {}
void resize(size_t newSize)
{
auto newData = std::make_unique<T[]>(newSize);
std::copy_n(m_Data.get(), std::min(m_Size, newSize), newData.get());
m_Data = std::move(newData);
m_Size = newSize;
m_Capacity = newSize * sizeof(T);
}
void assign(size_t size, T val)
{
resize(size);
std::fill_n(m_Data.get(), size, val)
}
void assign(T val) { std::fill_n(m_Data.get(), m_Size, val); }
size_t size() const { return m_Size; }
size_t capacity(){return m_Capacity}
T at(size_t i) const
{
if (i >= m__Size)
throw std::out_of_range("Index out of range");
return m_Data[i];
}
T &operator[](size_t i)
{
if (i >= m_Size)
throw std::out_of_range("Index out of range");
return m_Data[i];
}
const T &operator[](size_t i) const
{
if (i >= m_Size)
throw std::out_of_range("Index out of range");
return m_Data[i];
}
Iterator begin() { return Iterator(m_Data.get()); }
Iterator end() { return Iterator(m_Data.get() + m_Size); }
Iterator begin() const { return Iterator(m_Data.get()); }
Iterator end() const { return Iterator(m_Data.get() + m_Size); }
private:
size_t m_Size = 0;
size_t m_Capacity = 0;
std::unique_ptr<T[]> m_Data;
};
//---------------------------------------------------------------------------------------------
template <typename T>
class Matrix
{
public:
using value_type = MatrixRow<T>;
using Iterator = MatrixIterator<Matrix<T>>;
using ColumonIterator = MatrixColumnIterator<Matrix<T>>;
Matrix<T>() = default;
explicit Matrix<T>(int row_count, int column_count) : m_Rows(row_count), m_Cols(column_count), m_Size(row_count * column_count), m_Capacity(sizeof(T) * row_count * column_count) m_Data(std::make_unique<MatrixRow<T>[]>(row_count))
{
for (int i = 0; i < m_Rows; i++)
m_Data[i] = MatrixRow<T>(m_Cols);
}
size_t size() const { return m_Size; }
size_t rows() const { return m_Rows; }
size_t cols() const { return m_Cols; }
size_t capacity() const { return m_Capacity }
void resize(size_t row_count, size_t col_count)
{
auto newData = std::make_unique<MatrixRow<T>[]>(row_count);
for (size_t i = 0; i < std::min(m_Rows, row_count); ++i)
{
newData[i] = std::move(m_Data[i]);
newData[i].resize(col_count);
}
m_Data = std::move(newData);
m_Rows = row_count;
m_Cols = col_count;
m_Size = row_count * col_count;
m_Capacity = row_count * col_count * sizeof(T);
}
void assign(size_t row_count, size_t col_count, const T val)
{
resize(row_count, col_count) for (size_t i = 0; i < row_count; ++i)
std::fill_n(m_Data[i].get(), m_Data[i].size(), val);
}
void assign(const T val)
{
for (size_t i = 0; i < m_Rows; ++i)
for (size_t j = 0; j < m_Cols; ++j)
m_Data[i][j] = val;
}
Matrix<T> MergeVertical(const Matrix<T> &b) const
{
if (m_Cols != b.m_Cols)
throw std::invalid_argument("Matrices must have the same number of columns");
Matrix<T> result(m_Rows + b.m_Rows, m_Cols);
std::copy_n(m_Data.get(), m_Rows, result.m_Data.get());
std::copy_n(b.m_Data.get(), b.m_Rows, result.m_Data.get() + m_Rows);
return result;
}
Matrix<T> MergeHorizontal(const Matrix<T> &b) const
{
if (m_Rows != b.m_Rows)
throw std::invalid_argument("Matrices must have the same number of rows");
Matrix<T> result(m_Rows, m_Cols + b.m_Cols);
for (size_t i = 0; i < m_Rows; ++i)
{
std::copy_n(m_Data[i].begin(), m_Cols, result.m_Data[i].begin());
std::copy_n(b.m_Data[i].begin(), b.m_Cols, result.m_Data[i].begin() + m_Cols);
}
return result;
}
std::vector<Matrix<T>> SplitVertical() const
{
if (m_Rows % 2 != 0)
throw std::invalid_argument("Number of rows must be divisable by 2");
std::vector<Matrix<T>> result;
size_t split_size = m_Rows / 2;
for (size_t i = 0; i < 2; ++i)
{
Matrix<T> split(split_size, m_Cols);
std::copy_n(m_Data.get() + i * split_size, split_size, split.m_Data.get());
result.push_back(std::move(split));
}
return result;
}
std::vector<Matrix<T>> SplitVertical(size_t num) const
{
if (m_Rows % num != 0)
throw std::invalid_argument("Number of splits must evenly divide the number of rows");
std::vector<Matrix<T>> result;
size_t split_size = m_Rows / num;
for (size_t i = 0; i < num; ++i)
{
Matrix<T> split(split_size, m_Cols);
std::copy_n(m_Data.get() + i * split_size, split_size, split.m_Data.get());
result.push_back(std::move(split));
}
return result;
}
std::vector<Matrix<T>> SplitHorizontal() const
{
if (m_Cols % 2 != 0)
throw std::invalid_argument("Number of columns must be divisable by 2");
std::vector<Matrix<T>> result;
size_t split_size = m_Cols / 2;
for (size_t i = 0; i < 2; ++i)
{
Matrix<T> split(m_Rows, split_size);
for (size_t j = 0; j < m_Rows; ++j)
{
std::copy_n(m_Data[j].begin() + i * split_size, split_size, split.m_Data[j].begin());
}
result.push_back(std::move(split));
}
return result;
}
std::vector<Matrix<T>> SplitHorizontal(size_t num) const
{
if (m_Cols % num != 0)
throw std::invalid_argument("Number of splits must evenly divide the number of columns");
std::vector<Matrix<T>> result;
size_t split_size = m_Cols / num;
for (size_t i = 0; i < num; ++i)
{
Matrix<T> split(m_Rows, split_size);
for (size_t j = 0; j < m_Rows; ++j)
{
std::copy_n(m_Data[j].begin() + i * split_size, split_size, split.m_Data[j].begin());
}
result.push_back(std::move(split));
}
return result;
}
Matrix<T> SigmoidMatrix()
{
Matrix<T> result(*this);
for (auto &row : result)
{
for (auto &elem : row)
{
elem = 1 / (1 + std::exp(-elem));
}
}
}
Matrix<T> Randomize()
{
static std::mt19937 gen(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_real_distribution<> dis(-1.0, 1.0);
for (auto &row : *this)
{
for (auto &elem : row)
{
elem = dis(gen);
}
}
return *this;
}
Matrix<T> CreateIdentityMatrix()
{
if (m_Rows != m_Cols)
throw std::invalid_argument("Matrix must be square");
for (size_t i = 0; i < m_Rows; ++i)
{
std::fill(m_Data[i].begin(), m_Data[i].end(), T(0));
m_Data[i][i] = T(1);
}
return *this
}
Matrix<T> ZeroMatrix() const
{
for (auto &row : *this)
{
std::fill(row.begin(), row.end(), T(0);)
}
return *this;
}
Matrix<T> Transpose() const
{
Matrix<T> result(m_Cols, m_Rows);
for (size_t i = 0; i < m_Rows, ++i)
for (size_t j = 0; j < m_Cols; ++j)
result[j][i] = m_Data[i][j];
return result;
}
T Determinant() const
{
if (m_Rows != m_Cols)
throw std::invalid_argument("Matrix must be square");
size_t n = m_Rows;
if (n == 1)
return m_Data[0][0];
else if (n == 2)
return m_Data[0][0] * m_Data[1][1] - m_Data[0][1] * m_Data[1][0];
T det = 0;
for (size_t = 0; i < n; ++i)
{
Matrix<T> minor = getMinor(*this, 0, i);
int sign = ((i % 2) == 0) ? 1 : -1;
det += sign * matrix[0][i] * minor.Determinant();
}
return det;
}
Matrix<T> Inverse() const
{
if (m_Rows != m_Cols)
throw std::invalid_argument("Matrix must be square");
T det = Determinant();
if (det == 0)
throw std::runtime_error("Matrix is singular and cannot be inverted.");
// Step 2: Compute the cofactor matrix
Matrix<T> cofactors(m_Rows, m_Cols);
for (size_t i = 0; i < m_Rows; ++i)
{
for (size_t j = 0; j < m_Cols; ++j)
{
Matrix<T> minor = getMinor(*this, i, j);
T minor_det = minor.Determinant();
cofactors[i][j] = ((i + j) % 2 == 0 ? 1 : -1) * minor_det;
}
}
// Step 3: Compute the adjugate matrix (transpose of cofactor matrix)
Matrix<T> adjugate = cofactors.Transpose();
// Step 4: Compute the inverse
Matrix<T> inverse = adjugate * (1 / det);
return inverse;
}
MatrixRow<T> &
operator[](size_t i)
{
return m_Data[i];
}
const MatrixRow<T> &operator[](size_t i) const { return m_Data[i]; }
Matrix<T> operator+(const Matrix<T> &b)
{
if ((m_Rows == b.m_Rows) && (m_Cols == b.m_Cols))
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] + b[i][j];
return c;
}
else
return *this;
}
Matrix<T> operator+(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] + b;
return c;
}
Matrix<T> operator+=(const Matrix<T> &b) const
{
if ((m_Rows == b.m_Rows) && (m_Cols == b.m_Cols))
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] + b[i][j];
*this = c;
}
return *this;
}
Matrix<T> operator+=(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] + b;
*this = c;
return *this;
}
Matrix<T> operator-(const Matrix<T> &b) const
{
if ((m_Rows == b.m_Rows) && (m_Cols == b.m_Cols))
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] - b[i][j];
return c;
}
else
return *this;
}
Matrix<T> operator-(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] - b;
return c;
}
Matrix<T> operator-=(const Matrix<T> &b) const
{
if ((m_Rows == b.m_Rows) && (m_Cols == b.m_Cols))
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] - b[i][j];
*this = c;
}
return *this;
}
Matrix<T> operator-=(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] - b;
*this = c;
return *this;
}
Matrix<T> operator/(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] / b;
return c;
}
Matrix<T> operator/=(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] / b;
*this = c;
return *this;
}
Matrix<T> operator*(const Matrix<T> &b) const
{
if (m_Cols == b.m_Rows)
{
Matrix<T> c(m_Rows, b.m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
for (int k = 0; k < b.m_Cols; k++)
c[i][k] += m_Data[i][j] * b[j][k];
return c;
}
else
return *this;
}
Matrix<T> operator*(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] * b;
return c;
}
Matrix<T> operator*=(const T b) const
{
Matrix<T> c(m_Rows, m_Cols);
for (int i = 0; i < m_Rows; i++)
for (int j = 0; j < m_Cols; j++)
c[i][j] = m_Data[i][j] * b;
*this = c;
return *this;
}
Iterator begin() { return Iterator(m_Data); }
Iterator end() { return Iterator(m_Data + m_Rows); }
private:
Matrix<T> getMinor(const Matrix<T> &matrix, size_t row_to_remove, size_t col_to_remove) const
{
if (matrix.m_Rows != matrix.m_Cols)
throw std::invalid_argument("Matrix must be square to compute minor.");
size_t n = matrix.m_Rows;
Matrix<T> minor_matrix(n - 1, n - 1);
size_t minor_i = 0; // Row index for minor_matrix
for (size_t i = 0; i < n; ++i)
{
if (i == row_to_remove)
continue;
size_t minor_j = 0; // Column index for minor_matrix
for (size_t j = 0; j < n; ++j)
{
if (j == col_to_remove)
continue;
minor_matrix[minor_i][minor_j] = matrix.m_Data[i][j];
++minor_j;
}
++minor_i;
}
return minor_matrix;
}
size_t m_Rows = 0;
size_t m_Cols = 0;
size_t m_Size = 0;
size_t m_Capacity = 0;
std::unique_ptr<MatrixRow<T>[]> m_Data;
};
}
#endif