-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMatrix2.h
More file actions
1010 lines (857 loc) · 26.6 KB
/
GenericMatrix2.h
File metadata and controls
1010 lines (857 loc) · 26.6 KB
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
/****************************************************************************
**
** Copyright (C) 2019 Shaoguang. All rights reserved.
**
** GenericMatrix2, a generic template matrix class,
** the matrix elements are managed by a two-dimension pointer.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** https://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
****************************************************************************/
#ifndef __GERNERICMATRIX2_H__
#define __GERNERICMATRIX2_H__
#include <ostream>
#include <utility> // std::move,
#include <algorithm> // std::fill_n, std::copy_n
/*!
\class GenericMatrix2
\brief The GenericMatrix2 class defines a generic template matrix class,
the matrix elements are managed by a two-dimension pointer.
The GenericMatrix2 template has one parameter:
\li \b Elem Element type that is visible to users of the class.
*/
template<typename Elem = float>
class GenericMatrix2
{
public:
using size_type = std::size_t;
using value_type = Elem;
GenericMatrix2();
GenericMatrix2(size_type row, size_type col);
GenericMatrix2(size_type row, size_type col, const Elem &initialValue);
GenericMatrix2(const GenericMatrix2 &other);
GenericMatrix2(GenericMatrix2 &&other) noexcept;
~GenericMatrix2();
GenericMatrix2 &operator=(const GenericMatrix2 &other);
GenericMatrix2 &operator=(GenericMatrix2 &&other) noexcept;
size_type rows() const;
size_type columns() const;
size_type size() const;
Elem **data();
const Elem * const *data() const;
const Elem * const *constData() const;
Elem *operator[](size_type row);
const Elem *operator[](size_type row) const;
Elem *rowData(size_type row);
const Elem *rowData(size_type row) const;
const Elem *constRowData(size_type row) const;
Elem *at(size_type row);
const Elem *at(size_type row) const;
Elem &operator()(size_type row, size_type col);
const Elem &operator()(size_type row, size_type col) const;
Elem &at(size_type row, size_type col);
const Elem &at(size_type row, size_type col) const;
bool empty() const;
bool isValid() const;
bool isIdentity() const;
inline bool isHomomorphicTo(const GenericMatrix2<Elem> &m);
static inline bool isHomomorphic(const GenericMatrix2<Elem> &m1, const GenericMatrix2<Elem> &m2);
void resize(size_type row, size_type col);
void resize(size_type row, size_type col, const Elem &initialValue);
void setToIdentity();
void fill(const Elem &value);
void swap(GenericMatrix2<Elem> &other);
GenericMatrix2<Elem> transposed() const;
void doHadamardProduct(const GenericMatrix2<Elem> &m);
static GenericMatrix2<Elem> hadamardProduct(const GenericMatrix2<Elem> &m1, const GenericMatrix2<Elem> &m2);
GenericMatrix2<Elem> &operator+=(const GenericMatrix2<Elem> &m);
GenericMatrix2<Elem> &operator-=(const GenericMatrix2<Elem> &m);
GenericMatrix2<Elem> &operator*=(const GenericMatrix2<Elem> &m);
GenericMatrix2<Elem> &operator*=(const Elem &factor);
GenericMatrix2<Elem> &operator/=(const Elem &divisor);
bool operator==(const GenericMatrix2<Elem> &m) const;
bool operator!=(const GenericMatrix2<Elem> &m) const;
template<typename __ElemDTo, typename __Elem>
friend GenericMatrix2<__ElemDTo> matrix_cast(const GenericMatrix2<__Elem> &m);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator+(const GenericMatrix2<__Elem> &m1, const GenericMatrix2<__Elem> &m2);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator-(const GenericMatrix2<__Elem> &m1, const GenericMatrix2<__Elem> &m2);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator*(const GenericMatrix2<__Elem> &m1, const GenericMatrix2<__Elem> &m2);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator*(const GenericMatrix2<__Elem> &matrix, const __Elem &factor);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator*(const __Elem &factor, const GenericMatrix2<__Elem> &matrix);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator/(const GenericMatrix2<__Elem> &matrix, const __Elem &divisor);
template<typename __Elem>
friend GenericMatrix2<__Elem> operator-(const GenericMatrix2<__Elem> &matrix);
template<typename __Elem>
friend std::ostream &operator<<(std::ostream &os, const GenericMatrix2<__Elem> &matrix);
private:
void alloc();
void free();
private:
size_type m_rows;
size_type m_cols;
Elem **m_data;
};
/*!
\typedef GenericMatrix2::size_type
Type for declaring the matrix's rows and columns.
*/
/*!
\typedef GenericMatrix2::value_type
The value type of the template parameter \a Elem.
*/
/*!
Constructs a invalid matrix.
\sa isValid()
*/
template<typename Elem>
GenericMatrix2<Elem>::GenericMatrix2()
: m_rows(0), m_cols(0), m_data(nullptr)
{
}
/*!
Constructs a \a row x \a col matrix without initializing the contents.
*/
template<typename Elem>
GenericMatrix2<Elem>::GenericMatrix2(size_type row, size_type col)
: m_rows(row), m_cols(col), m_data(nullptr)
{
alloc();
}
/*!
Constructs a \a row x \a col matrix and initialize all values with \a initialValue.
\sa fill()
*/
template<typename Elem>
GenericMatrix2<Elem>::GenericMatrix2(size_type row, size_type col, const Elem &initialValue)
: m_rows(row), m_cols(col), m_data(nullptr)
{
alloc();
fill(initialValue);
}
/*!
Destroys the matrix.
*/
template<typename Elem>
GenericMatrix2<Elem>::~GenericMatrix2()
{
free();
}
/*!
\internal
Alloc the matrix memory from \a m_rows and \a m_cols.
*/
template<typename Elem>
void GenericMatrix2<Elem>::alloc()
{
if (m_rows > 0 && m_cols > 0) {
m_data = new Elem*[m_rows];
for (size_type i = 0; i < m_rows; ++i)
m_data[i] = new Elem[m_cols];
}
}
/*!
\internal
Frees the matrix memory.
*/
template<typename Elem>
void GenericMatrix2<Elem>::free()
{
if (m_data) {
for (size_type i = 0; i < m_rows; ++i) {
if (m_data[i]) {
delete[] m_data[i];
m_data[i] = nullptr;
}
}
delete[] m_data;
m_data = nullptr;
}
}
/*!
Constructs a copy of \a other.
*/
template<typename Elem>
GenericMatrix2<Elem>::GenericMatrix2(const GenericMatrix2 &other)
: m_rows(other.m_rows), m_cols(other.m_cols), m_data(nullptr)
{
alloc();
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
m_data[i][j] = other.m_data[i][j];
}
}
}
/*!
Move-constructs a GenericMatrix2 instance, making it point at the same object that \a other was pointing to.
*/
template<typename Elem>
GenericMatrix2<Elem>::GenericMatrix2(GenericMatrix2 &&other) noexcept
: m_rows(other.m_rows), m_cols(other.m_cols), m_data(other.m_data)
{
other.m_data = nullptr;
other.m_rows = 0;
other.m_cols = 0;
}
/*!
Assigns \a other to this matrix and returns a reference to this matrix.
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator=(const GenericMatrix2 &other)
{
if (this == &other) {
return *this;
}
if (m_rows != other.m_rows || m_cols != other.m_cols) {
free();
m_rows = other.m_rows;
m_cols = other.m_cols;
alloc();
}
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
m_data[i][j] = other.m_data[i][j];
}
}
return *this;
}
/*!
Move-assigns \a other to this GenericMatrix2 instance.
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator=(GenericMatrix2 &&other) noexcept
{
if (this == &other) {
return *this;
}
free();
m_rows = other.m_rows;
m_cols = other.m_cols;
other.m_rows = 0;
other.m_cols = 0;
m_data = other.m_data;
other.m_data = nullptr;
return *this;
}
/*!
Returns the number of matrix rows.
\sa columns()
*/
template<typename Elem>
typename GenericMatrix2<Elem>::size_type GenericMatrix2<Elem>::rows() const
{
return m_rows;
}
/*!
Returns the number of matrix columns.
\sa rows()
*/
template<typename Elem>
typename GenericMatrix2<Elem>::size_type GenericMatrix2<Elem>::columns() const
{
return m_cols;
}
/*!
Returns the number of matrix elements.
\sa rows(), columns()
*/
template<typename Elem>
typename GenericMatrix2<Elem>::size_type GenericMatrix2<Elem>::size() const
{
return m_rows * m_cols;
}
/*!
Returns a pointer to the raw data of this matrix.
\sa constData()
*/
template<typename Elem>
Elem **GenericMatrix2<Elem>::data()
{
return m_data;
}
/*!
Returns a constant pointer to the raw data of this matrix.
\sa constData()
*/
template<typename Elem>
const Elem * const *GenericMatrix2<Elem>::data() const
{
return m_data;
}
/*!
Returns a constant pointer to the raw data of this matrix.
\sa data()
*/
template<typename Elem>
const Elem * const *GenericMatrix2<Elem>::constData() const
{
return m_data;
}
/*!
Returns a pointer to the \a row data of this matrix.
\note No bounds checking is performed.
\sa rowData()
*/
template<typename Elem>
Elem *GenericMatrix2<Elem>::operator[](size_type row)
{
return m_data[row];
}
/*!
Returns a pointer to the \a row data of this matrix.
\note No bounds checking is performed.
\sa constRowData()
*/
template<typename Elem>
const Elem *GenericMatrix2<Elem>::operator[](size_type row) const
{
return m_data[row];
}
/*!
Returns a pointer to the \a row data of this matrix.
\note No bounds checking is performed.
\sa constRowData()
*/
template<typename Elem>
Elem *GenericMatrix2<Elem>::rowData(size_type row)
{
return m_data[row];
}
/*!
Returns a constant pointer to the \a row data of this matrix.
\note No bounds checking is performed.
\sa constRowData()
*/
template<typename Elem>
const Elem *GenericMatrix2<Elem>::rowData(size_type row) const
{
return m_data[row];
}
/*!
Returns a constant pointer to the \a row data of this matrix.
\note No bounds checking is performed.
\sa rowData()
*/
template<typename Elem>
const Elem *GenericMatrix2<Elem>::constRowData(size_type row) const
{
return m_data[row];
}
/*!
Returns a pointer to the \a row data of this matrix, with bounds checking.
\exception std::out_of_range if position \a row is not within the range of the matrix.
\sa rowData(), constRowDataAt()
*/
template<typename Elem>
Elem *GenericMatrix2<Elem>::at(size_type row)
{
if (row < m_rows)
return m_data[row];
throw std::out_of_range("out of range.");
}
/*!
Returns a constant pointer to the \a row data of this matrix, with bounds checking.
\exception std::out_of_range if position \a row is not within the range of the matrix.
\sa constRowData(), rowDataAt()
*/
template<typename Elem>
const Elem *GenericMatrix2<Elem>::at(size_type row) const
{
if (row < m_rows)
return m_data[row];
throw std::out_of_range("out of range.");
}
/*!
Returns a constant reference to the element at position (\a row, \a column) in this matrix.
\note No bounds checking is performed.
\sa at()
*/
template<typename Elem>
const Elem &GenericMatrix2<Elem>::operator()(size_type row, size_type column) const
{
return m_data[row][column];
}
/*!
Returns a reference to the element at position (\a row, \a column)
in this matrix so that the element can be assigned to.
\note No bounds checking is performed.
\sa at()
*/
template<typename Elem>
Elem &GenericMatrix2<Elem>::operator()(size_type row, size_type column)
{
return m_data[row][column];
}
/*!
Returns a constant reference to the element at position (\a row, \a column)
in this matrix, with bounds checking.
\exception std::out_of_range if position (\a row, \a col) is not within the range of the matrix.
\sa operator()()
*/
template<typename Elem>
const Elem &GenericMatrix2<Elem>::at(size_type row, size_type col) const
{
if (row >= 0 && row < m_rows && col >= 0 && col < m_cols)
return m_data[row][col];
throw std::out_of_range("out of range.");
}
/*!
Returns a reference to the element at position (\a row, \a column)
in this matrix, with bounds checking.
\exception std::out_of_range if position (\a row, \a col) is not within the range of the matrix.
\sa operator()()
*/
template<typename Elem>
Elem &GenericMatrix2<Elem>::at(size_type row, size_type col)
{
if (row >= 0 && row < m_rows && col >= 0 && col < m_cols)
return m_data[row][col];
throw std::out_of_range("out of range.");
}
/*!
Returns \c true if this matrix element's size equal to 0,
otherwise returns \c false.
*/
template<typename Elem>
bool GenericMatrix2<Elem>::empty() const
{
return 0 == size();
}
/*!
Returns \c true if this matrix internal data is not null pointer and matrix rows/cols greater than 0, otherwise returns \c false.
*/
template<typename Elem>
bool GenericMatrix2<Elem>::isValid() const
{
return m_data && m_rows > 0 && m_cols > 0;
}
/*!
Returns \c true if this matrix is the identity; false otherwise.
\sa setToIdentity()
*/
template<typename Elem>
bool GenericMatrix2<Elem>::isIdentity() const
{
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
if (i == j) {
if (m_data[i][j] != 1)
return false;
} else {
if (m_data[i][j] != 0)
return false;
}
}
}
return true;
}
/*!
Reconstructs a \a row x \a col matrix without initializing the contents.
*/
template<typename Elem>
void GenericMatrix2<Elem>::resize(size_type row, size_type col)
{
if (row != m_rows && col != m_cols) {
m_rows = row;
m_cols = col;
alloc();
}
}
/*!
Reconstructs a \a row x \a col matrix and initialize all values with \a initialValue.
*/
template<typename Elem>
void GenericMatrix2<Elem>::resize(size_type row, size_type col, const Elem &initialValue)
{
resize(row, col);
fill(initialValue);
}
/*!
Sets this matrix to the identity.
\sa isIdentity()
*/
template<typename Elem>
void GenericMatrix2<Elem>::setToIdentity()
{
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
if (j == i) {
m_data[i][j] = 1;
} else {
m_data[i][j] = 0;
}
}
}
}
/*!
Returns this matrix, transposed about its diagonal.
*/
template<typename Elem>
GenericMatrix2<Elem> GenericMatrix2<Elem>::transposed() const
{
GenericMatrix2<Elem> result(m_cols, m_rows);
for (size_type i = 0; i < m_cols; ++i) {
for (size_type j = 0; j < m_rows; ++j) {
result.m_data[i][j] = m_data[j][i];
}
}
return result;
}
/*!
Fills all elements of this matrix with \a value.
*/
template<typename Elem>
void GenericMatrix2<Elem>::fill(const Elem &value)
{
for (size_type i = 0; i < m_rows; ++i) {
std::fill_n(m_data[i], m_cols, value);
}
}
/*!
Do the hadamard product of this matrix and the matrix \a m.
\exception std::invalid_argument if \a m1 and \a m2 are not homomorphic.
\sa hadamardProduct()
*/
template<typename Elem>
void GenericMatrix2<Elem>::doHadamardProduct(const GenericMatrix2<Elem> &m)
{
if (!GenericMatrix2<Elem>::isHomomorphic(*this, m))
throw std::invalid_argument("invalid argument.");
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; i < m_cols; ++j) {
m_data[i][j] *= m.m_data[i][j];
}
}
}
/*!
Returns the hadamard product of the matrix \a m1 and the matrix \a m2.
\exception std::invalid_argument if \a m1 and \a m2 are not homomorphic.
\sa doHadamardProduct()
*/
template<typename Elem>
GenericMatrix2<Elem> GenericMatrix2<Elem>::hadamardProduct(const GenericMatrix2<Elem> &m1, const GenericMatrix2<Elem> &m2)
{
if (!GenericMatrix2<Elem>::isHomomorphic(m1, m2))
throw std::invalid_argument("invalid argument.");
GenericMatrix2<Elem> result(m1.m_rows, m1.m_cols);
for (size_type i = 0; i < m1.m_rows; ++i) {
for (size_type j = 0; i < m2.m_cols; ++j) {
result.m_data[i][j] = m1.m_data[i][j] * m2.m_data[i][j];
}
}
return result;
}
/*!
Returns \c true if this matrix and matrix \a m are homomorphic; false otherwise.
\sa isHomomorphic()
*/
template<typename Elem>
inline bool GenericMatrix2<Elem>::isHomomorphicTo(const GenericMatrix2<Elem> &m)
{
return((m_rows == m.m_rows) && (m_cols == m.m_cols));
}
/*!
Returns \c true if matrix \a m1 and matrix \a m2 are homomorphic; false otherwise.
\sa isHomomorphicTo()
*/
template<typename Elem>
inline bool GenericMatrix2<Elem>::isHomomorphic(const GenericMatrix2<Elem> &m1, const GenericMatrix2<Elem> &m2)
{
return ((m1.m_rows == m2.m_rows) && (m1.m_cols == m2.m_cols));
}
/*!
Swaps matrix \a other with this matrix.
*/
template<typename Elem>
void GenericMatrix2<Elem>::swap(GenericMatrix2<Elem> &other)
{
std::swap(*this, other);
}
/*!
Adds the contents of \a m to this matrix.
\exception std::invalid_argument if this matrix and matrix \a m are not homomorphic.
\sa operator-=()
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator+=(const GenericMatrix2<Elem> &m)
{
if (!isHomomorphicTo(m))
throw std::invalid_argument("invalid argument.");
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
m_data[i][j] += m.m_data[i][j];
}
}
return *this;
}
/*!
Subtracts the contents of \a m from this matrix.
\exception std::invalid_argument if this matrix and matrix \a m are not homomorphic.
\sa operator+=()
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator-=(const GenericMatrix2<Elem> &m)
{
if (!isHomomorphicTo(m))
throw std::invalid_argument("invalid argument.");
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
m_data[i][j] -= m.m_data[i][j];
}
}
return *this;
}
/*!
Multiplies this matrix and matrix \a m, in format: this = this * m.
\exception std::invalid_argument if this matrix's columns is not equal to matrix \a m's rows.
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator*=(const GenericMatrix2<Elem> &m)
{
if (m_cols != m.m_rows)
throw std::invalid_argument("invalid argument.");
GenericMatrix2<Elem> result(m_rows, m.m_cols, 0);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
for (size_type k = 0; k < m_cols; ++k) {
result.m_data[i][j] += (m_data[i][k] * m.m_data[k][j]);
}
}
}
return (*this = std::move(result));
}
/*!
Multiplies all elements of this matrix by \a factor.
\sa operator/=()
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator*=(const Elem &factor)
{
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
m_data[i][j] *= factor;
}
}
return *this;
}
/*!
Divides all elements of this matrix by \a divisor.
\sa operator*=()
*/
template<typename Elem>
GenericMatrix2<Elem> &GenericMatrix2<Elem>::operator/=(const Elem &divisor)
{
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
m_data[i][j] /= divisor;
}
}
return *this;
}
/*!
Returns \c true if this matrix is identical to \a m; false otherwise.
\sa operator!=()
*/
template<typename Elem>
bool GenericMatrix2<Elem>::operator==(const GenericMatrix2<Elem> &m) const
{
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
if (m_data[i][j] != m.m_data[i][j])
return false;
}
}
return true;
}
/*!
Returns \c true if this matrix is not identical to \a m; false otherwise.
\sa operator==()
*/
template<typename Elem>
bool GenericMatrix2<Elem>::operator!=(const GenericMatrix2<Elem> &m) const
{
for (size_type i = 0; i < m_rows; ++i) {
for (size_type j = 0; j < m_cols; ++j) {
if (m_data[i][j] != m.m_data[i][j])
return true;
}
}
return false;
}
/*****************************************************************************
friend functions
*****************************************************************************/
/*!
\relates GenericMatrix2
Converts a matrix to a different type matrix.
\note The \b __ElemDst must can do \c static_cast<__ElemDst>(__Elem).
*/
template<typename __ElemDTo, typename __Elem>
GenericMatrix2<__ElemDTo> matrix_cast(const GenericMatrix2<__Elem> &m)
{
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__ElemDTo> result(m.rows(), m.columns());
for (size_type i = 0; i < m.rows(); ++i) {
std::copy_n(m[i], m.columns(), result[i]);
}
return result;
}
/*!
\relates GenericMatrix2
Returns the sum of \a m1 and \a m2.
\exception std::invalid_argument if this matrix and matrix \a m are not homomorphic.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator+(const GenericMatrix2<__Elem> &m1, const GenericMatrix2<__Elem> &m2)
{
if (!GenericMatrix2<__Elem>::isHomomorphic(m1, m2))
throw std::invalid_argument("invalid argument.");
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(m1.m_rows, m1.m_cols);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
result.m_data[i][j] = m1.m_data[i][j] + m2.m_data[i][j];
}
}
return result;
}
/*!
\relates GenericMatrix2
Returns the difference of \a m1 and \a m2.
\exception std::invalid_argument if this matrix and matrix \a m are not homomorphic.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator-(const GenericMatrix2<__Elem> &m1, const GenericMatrix2<__Elem> &m2)
{
if (!GenericMatrix2<__Elem>::isHomomorphic(m1, m2))
throw std::invalid_argument("invalid argument.");
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(m1.m_rows, m1.m_cols);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
result.m_data[i][j] = m1.m_data[i][j] - m2.m_data[i][j];
}
}
return result;
}
/*!
\relates GenericMatrix2
Returns the product of the \c M1xNN matrix \a m1 and the \c NNxM2 matrix \a m2
to produce a \c M1xM2 matrix result.
\exception std::invalid_argument if \a m1's columns is not equal to matrix \a m2's rows.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator*(const GenericMatrix2<__Elem> &m1, const GenericMatrix2<__Elem> &m2)
{
if (m1.m_cols != m2.m_rows)
throw std::invalid_argument("invalid argument.");
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(m1.m_rows, m2.m_cols, 0);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
for (size_type k = 0; k < m1.m_cols; ++k) {
result.m_data[i][j] += m1.m_data[i][k] * m2.m_data[k][j];
}
}
}
return result;
}
/*!
\relates GenericMatrix2
Returns the result of multiplying all elements of \a matrix by \a factor.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator*(const GenericMatrix2<__Elem> &matrix, const __Elem &factor)
{
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(matrix.m_rows, matrix.m_cols);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
result.m_data[i][j] = matrix.m_data[i][j] * factor;
}
}
return result;
}
/*!
\relates GenericMatrix2
Returns the result of multiplying all elements of \a matrix by \a factor.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator*(const __Elem &factor, const GenericMatrix2<__Elem> &matrix)
{
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(matrix.m_rows, matrix.m_cols);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
result.m_data[i][j] = matrix.m_data[i][j] * factor;
}
}
return result;
}
/*!
\relates GenericMatrix2
Returns the result of dividing all elements of \a matrix by \a divisor.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator/(const GenericMatrix2<__Elem> &matrix, const __Elem &divisor)
{
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(matrix.m_rows, matrix.m_cols);
for (size_type i = 0; i < result.m_rows; ++i) {
for (size_type j = 0; j < result.m_cols; ++j) {
result.m_data[i][j] = matrix.m_data[i][j] / divisor;
}
}
return result;
}
/*!
\relates GenericMatrix2
Returns the negation of \a matrix.
*/
template<typename __Elem>
GenericMatrix2<__Elem> operator-(const GenericMatrix2<__Elem> &matrix)
{
using size_type = typename GenericMatrix2<__Elem>::size_type;
GenericMatrix2<__Elem> result(matrix.m_rows, matrix.m_cols);
for (size_type i = 0; i < matrix.m_rows; ++i) {
for (size_type j = 0; j < matrix.m_cols; ++j) {
result.m_data[i][j] = -matrix.m_data[i][j];
}
}
return result;
}
/*!
\relates GenericMatrix2
Writes the given \a matrix to the given \a stream and returns a
reference to the stream.
*/
template<typename __Elem>
std::ostream &operator<<(std::ostream &stream, const GenericMatrix2<__Elem> &matrix)
{
using size_type = typename GenericMatrix2<__Elem>::size_type;
stream << "GenericMatrix2<" << matrix.m_rows << ", " << matrix.m_cols << ", " << typeid(__Elem).name() << ">(" << std::endl;
for (size_type i = 0; i < matrix.m_rows; ++i) {