-
Notifications
You must be signed in to change notification settings - Fork 2
/
MbMatrix.h
1015 lines (903 loc) · 27.9 KB
/
MbMatrix.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
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
/*!
* \file
* This file declares the class MbMatrix, a templated class for
* matrices and common operations on matrices (2D arrays). It
* also contains definitions for MbMatrix and related functions
* operating on templated matrices, such as printing and reading.
*
* \brief Declaration and definitions for MbMatrix
*
* MrBayes version 4.0 beta
*
* (c) Copyright 2005.
* \version 4.0 Beta
* \date Last modified: $Date: 2006/08/25 14:52:04 $
* \author John Huelsenbeck (1)
* \author Bret Larget (2)
* \author Paul van der Mark (3)
* \author Fredrik Ronquist (3)
* \author Donald Simon (4)
* \author (authors listed in alphabetical order)
* (1) Division of Biological Science, University of California, San Diego
* (2) Departments of Botany and of Statistics, University of Wisconsin - Madison
* (3) School of Computational Science, Florida State University
* (4) Department of Mathematics/Computer Science, Duquesne University
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License (the file gpl.txt included with this
* distribution or http: *www.gnu.org/licenses/gpl.txt) for more
* details.
*
* $Id: MbMatrix.h,v 1.9 2006/08/25 14:52:04 paulvdm Exp $
*/
#ifndef MbMatrix_H
#define MbMatrix_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
/*!
* MrBayes templated matrix type. We used the Template Numerical
* Toolkit (TNT) code as a model for this class. TNT is similar to the
* LAPACK matrix type. The TNT code comes with the following
* disclaimer:
*
* "Template Numerical Toolkit (TNT)
*
* Mathematical and Computational Sciences Division
* National Institute of Technology,
* Gaithersburg, MD USA
*
* This software was developed at the National Institute of Standards and
* Technology (NIST) by employees of the Federal Government in the course
* of their official duties. Pursuant to title 17 Section 105 of the
* United States Code, this software is not subject to copyright protection
* and is in the public domain. NIST assumes no responsibility whatsoever for
* its use by other parties, and makes no guarantees, expressed or implied,
* about its quality, reliability, or any other characteristic."
*
* Storage corresponds to C (row-major) ordering.
* Elements are accessed via A[i][j] notation.
*
* Array assignment is by reference (i.e. shallow assignment).
* That is, B=A implies that the A and B point to the
* same matrix, so modifications to the elements of A
* will be reflected in B. If an independent copy
* is required, then B = A.copy() can be used. Note
* that this facilitates returning matrices from functions
* without relying on compiler optimizations to eliminate
* extensive data copying.
*
* The indexing and layout of this matrix object makes
* it compatible with C and C++ algorithms that utilize
* the familiar C[i][j] notation. This includes numerous
* textbooks, such as Numerical Recipes, and various
* public domain codes.
*
* This class employs its own garbage collection via
* the use of reference counts. That is, whenever
* an internal array storage no longer has any references
* to it, it is destroyed.
*
* Note that the multiplication operator is overloaded to
* do matrix multiplication rather than element-wise
* multiplication.
*
* \brief Templated matrices and matrix operations
*/
template <class T>
class MbMatrix {
public:
MbMatrix(void); //!< null constructor (0 X 0 matrix)
MbMatrix(int m, int n); //!< creates a m X n matrix without initialization
MbMatrix(int m, int n, T *a); //!< creates a m X n matrix as a view of a one-dimensional array
MbMatrix(int m, int n, const T &a); //!< creates a m X n matrix initializing all elements to a constant
inline MbMatrix(const MbMatrix &A); //!< creates a m X n matrix with elements shared by another matrix, A
~MbMatrix(void); //!< destructor
operator T**() { return &(v[0]); } //!< type cast operator
operator const T**() { return &(v[0]); } //!< type cast operator for const
inline MbMatrix &operator=(const T &a); //!< assignment operator (all elements have the value a)
MbMatrix &operator=(const MbMatrix &A) { return ref(A); } //!< assignment operator (shallow copy, elements share data)
bool operator==(const MbMatrix &A) const; //!< equality operator
T *operator[](int i) { return v[i]; } //!< indexing operator (allows reference of data using [][] notation)
const T *operator[](int i) const { return v[i]; } //!< indexing operator (allows reference of data using [][] notation)
inline MbMatrix &ref(const MbMatrix &A); //!< creates a reference to another matrix (shallow copy)
MbMatrix copy(void) const; //!< creates a copy of another matrix (deep copy, with separate data elements)
MbMatrix &inject(const MbMatrix &A); //!< copy the values of elements from one matrix to another
int dim1(void) const { return m; } //!< number of rows
int dim2(void) const { return n; } //!< number of columns
int getRefCount(void) const { return *refCount; } //!< get the number of matrices that share the same data
private:
T **v;
int m;
int n;
int *refCount;
bool cArray;
void destroy(void);
};
template <class T> std::ostream& operator<<(std::ostream &s, const MbMatrix<T> &A); //!< operator <<
template <class T> std::istream& operator>>(std::istream &s, MbMatrix<T> &A); //!< operator >>
template <class T> MbMatrix<T> operator+(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< operator +
template <class T> MbMatrix<T> operator-(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< operator -
template <class T> MbMatrix<T> operator*(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< operator * (matrix multiplication)
template <class T> MbMatrix<T> &operator+=(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< operator +=
template <class T> MbMatrix<T> &operator-=(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< operator -=
template <class T> MbMatrix<T> &operator*=(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< operator *= (matrix multiplication)
template <class T> MbMatrix<T> operator+(const T &a, const MbMatrix<T> &B); //!< operator + for scalar + matrix
template <class T> MbMatrix<T> operator-(const T &a, const MbMatrix<T> &B); //!< operator - for scalar - matrix
template <class T> MbMatrix<T> operator*(const T &a, const MbMatrix<T> &B); //!< operator * for scalar * matrix
template <class T> MbMatrix<T> operator/(const T &a, const MbMatrix<T> &B); //!< operator / for scalar / matrix
template <class T> MbMatrix<T> operator+(const MbMatrix<T> &A, const T &b); //!< operator + for matrix + scalar
template <class T> MbMatrix<T> operator-(const MbMatrix<T> &A, const T &b); //!< operator - for matrix - scalar
template <class T> MbMatrix<T> operator*(const MbMatrix<T> &A, const T &b); //!< operator * for matrix * scalar
template <class T> MbMatrix<T> operator/(const MbMatrix<T> &A, const T &b); //!< operator / for matrix / scalar
template <class T> MbMatrix<T> &operator+=(const MbMatrix<T> &A, const T &b); //!< operator += for scalar
template <class T> MbMatrix<T> &operator-=(const MbMatrix<T> &A, const T &b); //!< operator -= for scalar
template <class T> MbMatrix<T> &operator*=(const MbMatrix<T> &A, const T &b); //!< operator *= for scalar
template <class T> MbMatrix<T> &operator/=(const MbMatrix<T> &A, const T &b); //!< operator /= for scalar
template <class T> bool operator!=(const MbMatrix<T> &A, const MbMatrix<T> &B); //!< inequality (operator !=)
// Definitions of inlined member functions
/*!
* Copy constructor, which creates a shallow copy of the
* MbMatrix argument. Matrix data are not copied but shared.
* Thus, in MbMatrix B(A), subsequent changes to A will be
* reflected by changes in B. For an independent copy, use
* MbMatrix B(A.copy()), or B = A.copy(), instead. Note
* the use of garbage collection in this class, through the
* reference counter refCount.
*
* \brief Shallow copy constructor
* \param A Matrix to copy
*/
template <class T>
inline MbMatrix<T>::MbMatrix(const MbMatrix &A)
: v(A.v), m(A.m), n(A.n), refCount(A.refCount), cArray(A.cArray) {
(*refCount)++;
}
/*!
* Assign all elements of the matrix the value of
* the constant scalar a.
*
* \brief Assign scalar to all elements
* \param a Scalar used in assignment
* \return Assigned matrix
*/
template <class T>
inline MbMatrix<T> &MbMatrix<T>::operator=(const T &a) {
T *p = &(v[0][0]);
T *end = p + m*n;
for (; p<end; p++)
*p = a;
return *this;
}
/*!
* Create a reference (shallow assignment) to another existing matrix.
* In B.ref(A), B and A share the same data and subsequent changes
* to the matrix elements of one will be reflected in the other. Note that
* the reference counter is always allocated, even for null matrices,
* so we need not test whether refCount is NULL.
*
* This is what operator= calls, and B=A and B.ref(A) are equivalent
* operations.
*
* \brief Make this reference to A
* \param A Matrix to take reference of
* \return Matrix to which the reference was assigned
*/
template <class T>
inline MbMatrix<T> &MbMatrix<T>::ref(const MbMatrix &A) {
if (this != &A)
{
(*refCount)--;
if (*refCount < 1)
destroy();
m = A.m;
n = A.n;
v = A.v;
refCount = A.refCount;
cArray = A.cArray;
(*refCount)++;
}
return *this;
}
// Defintions of member functions that are not inlined
/*!
* Null constructor. Creates a (0 X O) ('null') matrix.
* Note that the reference count will be set to 1 for this
* null matrix. This is to simplify the rest of the
* code at the cost of allocating and deleting an int
* everytime a null matrix is needed.
*
* \brief Null constructor
*/
template <class T>
MbMatrix<T>::MbMatrix(void)
: v(0), m(0), n(0), refCount(0), cArray(false) {
refCount = new int;
*refCount = 1;
}
/*!
* Create a new (m X n) matrix, without initializing matrix
* elements. If m or n are not positive, a null matrix is created. Note
* that the reference count will be set to 1 regardless of whether
* we create a null matrix. This is to simplify the rest of the
* code at the cost of allocating and deleting an int every time
* a null matrix is needed.
*
* This version avoids the O(m*n) initialization overhead.
*
* \brief Constructor of uninitialized matrix
* \param m The first (row) dimension of the matrix
* \param n The second (column) dimension of the matrix
*/
template <class T>
MbMatrix<T>::MbMatrix(int m, int n)
: v(0), m(0), n(0), refCount(0), cArray(false) {
if (m > 0 && n > 0) {
// allocate and initialize pointers
T *p = new T[m * n];
v = new T*[m];
for (int i=0; i<m; i++) {
v[i] = p;
p += n;
}
this->m = m;
this->n = n;
}
refCount = new int;
*refCount = 1;
}
/*!
* Create a new (m X n) matrix as a view of an existing 'two-dimensional'
* C array stored in C order, i.e. right-most dimension varying fastest,
* often referred to as "row-major" ordering. The storage for this pre-existing
* array will never be destroyed by the MbMatrix class because of the cArray
* flag, which is set to true for matrix space allocated in this way. However,
* garbage collection will make sure that row pointers are deallocated when
* appropriate.
*
* \param m The first (row) dimension of the matrix
* \param n The second (column) dimension of the matrix
* \param a Pointer to C array used as data storage
*/
template <class T>
MbMatrix<T>::MbMatrix(int m, int n, T *a)
: v(0), m(0), n(0) , refCount(0), cArray(false) {
if (m > 0 && n > 0) {
// initialize pointers
T *p = a;
v = new T*[m];
for (int i=0; i<m; i++) {
v[i] = p;
p += n;
}
this->m = m;
this->n = n;
}
refCount = new int;
*refCount = 1;
cArray = true;
}
/*!
* Create a new (m X n) matrix, initializing matrix elements
* to the constant value specified by the third argument. Most
* often used to create a matrix of zeros, as in MbMatrix A(m, n, 0.0).
*
* \brief Constructor of initialized matrix.
* \param m The first (row) dimension of the matrix
* \param n The second (column) dimension of the matrix
* \param a Value for initialization.
*/
template <class T>
MbMatrix<T>::MbMatrix(int m, int n, const T &a)
: v(0), m(0), n(0), refCount(0), cArray(false) {
if (m > 0 && n > 0) {
// allocate and initialize pointers
T *p = new T[m * n];
v = new T*[m];
for (int i=0; i<m; i++) {
v[i] = p;
p += n;
}
this->m = m;
this->n = n;
// set values
T *end = &(v[0][0]) + m*n;
for (p=&(v[0][0]); p<end; p++)
*p = a;
}
refCount = new int;
*refCount = 1;
}
/*!
* Destructor. Note that refCount is decreased and only if
* refCount reaches 0 do we delete allocated memory. This is
* garbage collection as implemented in JAVA and other languages.
* Note that null matrices also have a reference count allocated,
* so that we can always access the value of refCount.
*
* \brief Destructor with garbage collection
*/
template <class T>
MbMatrix<T>::~MbMatrix(void) {
(*refCount)--;
if (*refCount < 1)
destroy();
}
/*!
* Equality operator. The dimensions of the two matrices
* are first compared. If they are not the same, then false
* is returned. Second, all elements are compared. If
* they are the same, true is returned, otherwise false
* is returned. Note that this operator is not useful
* for float and double matrices, but it is handy for int
* and bool matrices, as well as for matrices of other types
* that have a sensible operator!= defined.
*
* \brief Equality operator
* \param A Matrix to compare (*this) to
* \return True if (*this)==A, false otherwise.
*/
template <class T>
bool MbMatrix<T>::operator==(const MbMatrix &A) const {
if (m != A.m || n != A.n)
return false;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++)
if (v[i][j] != A.v[i][j])
return false;
}
return true;
}
/*!
* Create a new version of an existing matrix. Used in B = A.copy()
* or in the construction of a new matrix that does not share
* data with the copied matrix, e.g. in MbMatrix B(A.copy()).
*
* \brief Create independent copy
* \return Copy of this.
*/
template <class T>
MbMatrix<T> MbMatrix<T>::copy(void) const {
MbMatrix A(m, n);
memcpy (&(A.v[0][0]), &(v[0][0]), m*n*sizeof(T));
return A;
}
/*
* Copy the elements from one matrix to another, in place.
* That is, if you call B.inject(A), both A and B must conform
* (i.e. have the same row and column dimensions).
*
* This differs from B = A.copy() in that references to B
* before this assignment are also affected. That is, if
* we have
*
* MbMatrix A(n);
* MbMatrix C(n);
* MbMatrix B(C); (elements of B and C are shared)
*
* then B.inject(A) affects both B and C, while B=A.copy() creates
* a new matrix B which shares no data with C or A.
*
* A is the matrix from which elements will be copied.
* The function returns an instance of the modifed matrix. That is, in
* B.inject(A), it returns B. If A and B are not conformant, no
* modifications to B are made.
*
* \brief Inject elements of A into (*this)
* \param A Matrix with elements to inject
* \return Injected matrix
*/
template <class T>
MbMatrix<T> &MbMatrix<T>::inject(const MbMatrix &A) {
if (A.m == m && A.n == n)
memcpy(&(v[0][0]), &(A.v[0][0]), m*n*sizeof(T));
return *this;
}
/*!
* This is a garbage collector, which is
* called only when there is no more element
* referencing this matrix.
*
* \brief Garbage collection
*/
template <class T>
void MbMatrix<T>::destroy(void) {
if (v != 0) {
if (cArray == false)
{
delete [] (v[0]);
}
delete [] (v);
}
if (refCount != 0)
{
delete refCount;
}
cArray = false;
}
// Definitions of related templated functions on matrices
/*!
* Printing of a matrix to an ostream object.
* We use the format:
* [<m>,<n>]
* ((v_11,v_12,v_13,...,v_1n),
* (v_i1,v_i2,v_i3,...,v_in),
* (v_m1,v_m2,v_m3,...,v_mn))
*
* \brief operator<<
* \param A Matrix to output
* \param s ostream to output to
* \return ostream object (for additional printing)
*/
template <class T>
std::ostream& operator<<(std::ostream &s, const MbMatrix<T> &A) {
int M = A.dim1();
int N = A.dim2();
s << "[" << M << "," << N << "]\n";
s << "(";
for (int i=0; i<M; i++) {
s << "(";
for (int j=0; j<N; j++) {
s << A[i][j];
if (j != N-1)
s << ",";
}
if (i != M-1)
s << "),\n";
else
s << ")";
}
s << ")";
return s;
}
/*!
* Reading of a matrix from an istream object.
* We expect the format:
* [<m>,<n>]
* ((v_11,v_12,v_13,...,v_1n),
* (v_i1,v_i2,v_i3,...,v_in),
* (v_m1,v_m2,v_m3,...,v_mn))
* On failure, a null matrix is returned.
*
* \brief operator>>
* \param A Matrix to receive input
* \param s istream to read from
* \return istream object (for additional reading)
*/
template <class T>
std::istream& operator>>(std::istream &s, MbMatrix<T> &A) {
A = MbMatrix<T>(); // make sure we return null matrix on failure
int M, N;
char c;
s >> c;
if (c != '[')
return s;
s >> M;
s >> c;
if (c != ',')
return s;
s >> N;
MbMatrix<T> B(M,N);
s >> c;
if (c != ']')
return s;
s.ignore(); // ignore the newline
s >> c;
if (c != '(')
return s;
for (int i=0; i<M; i++) {
s >> c;
if (c != '(')
return s;
for (int j=0; j<N; j++) {
s >> B[i][j];
if (j < N-1) {
s >> c;
if (c != ',') return s;
}
}
s >> c;
if (c != ')')
return s;
if (i != M-1)
s.ignore(); // ignore newline
}
s >> c;
if (c != ')')
return s;
A = B;
return s;
}
/*!
* This function performs addition of a scalar to
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator+ (scalar)
* \param A Matrix
* \param b Scalar
* \return A + b
*/
template <class T>
MbMatrix<T> operator+(const MbMatrix<T> &A, const T &b) {
MbMatrix<T> B(A.copy());
for (int i=0; i<B.dim1(); i++)
for (int j=0; j<B.dim2(); j++)
B[i][j] = A[i][j] + b;
return B;
}
/*!
* This function performs subtraction of a scalar from
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator- (scalar)
* \param A Matrix
* \param b Scalar
* \return A - b
*/
template <class T>
MbMatrix<T> operator-(const MbMatrix<T> &A, const T &b) {
MbMatrix<T> B(A.copy());
for (int i=0; i<B.dim1(); i++)
for (int j=0; j<B.dim2(); j++)
B[i][j] = A[i][j] - b;
return B;
}
/*!
* This function performs multiplication of a scalar to
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator* (scalar)
* \param A Matrix
* \param b Scalar
* \return A * b
*/
template <class T>
MbMatrix<T> operator*(const MbMatrix<T> &A, const T &b) {
MbMatrix<T> B(A.copy());
for (int i=0; i<B.dim1(); i++)
for (int j=0; j<B.dim2(); j++)
B[i][j] = A[i][j] * b;
return B;
}
/*!
* This function performs division with a scalar of
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator/ (scalar)
* \param A Matrix
* \param b Scalar
* \return A / b
*/
template <class T>
MbMatrix<T> operator/(const MbMatrix<T> &A, const T &b) {
MbMatrix<T> B(A.copy());
for (int i=0; i<B.dim1(); i++)
for (int j=0; j<B.dim2(); j++)
B[i][j] = A[i][j] / b;
return B;
}
/*!
* This function performs addition of a scalar to
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator+ (scalar first)
* \param a Scalar
* \param B Matrix
* \return a + B
*/
template <class T>
MbMatrix<T> operator+(const T &a, const MbMatrix<T> &B) {
MbMatrix<T> A(B.copy());
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] = a + B[i][j];
return A;
}
/*!
* This function subtracts each element of a
* a matrix from a scalar and returns the
* resulting matrix.
*
* \brief operator- (scalar first)
* \param a Scalar
* \param B Matrix
* \return a - B
*/
template <class T>
MbMatrix<T> operator-(const T &a, const MbMatrix<T> &B) {
MbMatrix<T> A(B.copy());
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] = a - B[i][j];
return A;
}
/*!
* This function performs multiplication of a scalar to
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator* (scalar first)
* \param a Scalar
* \param B Matrix
* \return a * B
*/
template <class T>
MbMatrix<T> operator*(const T &a, const MbMatrix<T> &B) {
MbMatrix<T> A(B.copy());
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] = a * B[i][j];
return A;
}
/*!
* This function performs division of a scalar by
* each element of a matrix and returns the
* resulting matrix.
*
* \brief operator/ (scalar first)
* \param a Scalar
* \param B Matrix
* \return a / B
*/
template <class T>
MbMatrix<T> operator/(const T &a, const MbMatrix<T> &B) {
MbMatrix<T> A(B.copy());
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] = a / B[i][j];
return A;
}
/*!
* This function performs addition of a scalar to
* each element of a matrix in place and returns the
* resulting matrix.
*
* \brief operator+= (scalar)
* \param A Matrix
* \param b Scalar
* \return A += b
*/
template <class T>
MbMatrix<T> &operator+=(MbMatrix<T> &A, const T &b) {
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] += b;
return A;
}
/*!
* This function performs subtraction of a scalar from
* each element of a matrix in place and returns the
* resulting matrix.
*
* \brief operator-= (scalar)
* \param A Matrix
* \param b Scalar
* \return A -= b
*/
template <class T>
MbMatrix<T> &operator-=(MbMatrix<T> &A, const T &b) {
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] -= b;
return A;
}
/*!
* This function performs multiplication of a scalar to
* each element of a matrix in place and returns the
* resulting matrix.
*
* \brief operator*= (scalar)
* \param A Matrix
* \param b Scalar
* \return A *= b
*/
template <class T>
MbMatrix<T> &operator*=(MbMatrix<T> &A, const T &b) {
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] *= b;
return A;
}
/*!
* This function performs division with a scalar of
* each element of a matrix in place and returns the
* resulting matrix.
*
* \brief operator/= (scalar)
* \param A Matrix
* \param b Scalar
* \return A /= b
*/
template <class T>
MbMatrix<T> &operator/=(MbMatrix<T> &A, const T &b) {
for (int i=0; i<A.dim1(); i++)
for (int j=0; j<A.dim2(); j++)
A[i][j] /= b;
return A;
}
/*!
* This function performs elementwise addition of two
* matrices and returns the resulting matrix. If the
* matrices are not conformant, a null matrix is returned.
*
* \brief operator+
* \param A Matrix 1
* \param B Matrix 2
* \return A + B, null matrix on failure
*/
template <class T>
MbMatrix<T> operator+(const MbMatrix<T> &A, const MbMatrix<T> &B) {
int m = A.dim1();
int n = A.dim2();
if (B.dim1() != m || B.dim2() != n)
return MbMatrix<T>();
else {
MbMatrix<T> C(m,n);
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++)
C[i][j] = A[i][j] + B[i][j];
}
return C;
}
}
/*!
* This function performs elementwise subtraction of two
* matrices and returns the resulting matrix. If the
* matrices are not conformant, a null matrix is returned.
*
* \brief operator-
* \param A Matrix 1
* \param B Matrix 2
* \return A - B, null matrix on failure
*/
template <class T>
MbMatrix<T> operator-(const MbMatrix<T> &A, const MbMatrix<T> &B) {
int m = A.dim1();
int n = A.dim2();
if (B.dim1() != m || B.dim2() != n)
return MbMatrix<T>();
else {
MbMatrix<T> C(m,n);
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++)
C[i][j] = A[i][j] - B[i][j];
}
return C;
}
}
/*!
* Compute C = A*B, where C[i][j] is the dot-product of
* row i of A and column j of B. Note that this operator
* does not perform elementwise multiplication. If the
* matrices do not have the right dimensions for matrix
* multiplication (that is, if the number of columns of A
* is different from the number of rows of B), the function
* returns a null matrix.
*
* \brief Matrix multiplication
* \param A An (m X n) matrix
* \param B An (n X k) matrix
* \return A * B, an (m X k) matrix, or null matrix on failure
*/
template <class T>
MbMatrix<T> operator*(const MbMatrix<T> &A, const MbMatrix<T> &B) {
if ( A.dim2() != B.dim1() )
return MbMatrix<T>();
int M = A.dim1();
int N = A.dim2();
int K = B.dim2();
MbMatrix<T> C(M,K);
for (int i=0; i<M; i++) {
for (int j=0; j<K; j++) {
T sum = 0;
for (int k=0; k<N; k++)
sum += A[i][k] * B [k][j];
C[i][j] = sum;
}
}
return C;
}
/*!
* This function performs elementwise addition on two
* matrices and puts the result in the first matrix.
* If the two matrices are nonconformant, the first
* matrix is left intact.
*
* \brief operator+=
* \param A Matrix 1
* \param B Matrix 2
* \return A += B, A unmodified on failure
*/
template <class T>
MbMatrix<T>& operator+=(MbMatrix<T> &A, const MbMatrix<T> &B) {
int m = A.dim1();
int n = A.dim2();
if (B.dim1() == m && B.dim2() == n) {
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++)
A[i][j] += B[i][j];
}
}
return A;
}
/*!
* This function performs elementwise subtraction on two
* matrices and puts the result in the first matrix.
* If the two matrices are nonconformant, the first
* matrix is left intact.
*
* \brief operator-=
* \param A Matrix 1
* \param B Matrix 2
* \return A -= B, A unmodified on failure
*/
template <class T>
MbMatrix<T>& operator-=(MbMatrix<T> &A, const MbMatrix<T> &B) {
int m = A.dim1();
int n = A.dim2();
if (B.dim1() == m && B.dim2() == n) {
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++)
A[i][j] -= B[i][j];
}
}
return A;
}
/*!
* Compute C = A*B, where C[i][j] is the dot-product of
* row i of A and column j of B. Then assign the result to
* A. Note that this operator does not perform elementwise
* multiplication. If the matrices are not both square of the
* same dimension, then the operation is not possible to
* perform and we return an unomidified A.
*
* \brief Matrix multiplication with assignment (operator *=)
* \param A An (n X n) matrix
* \param B An (n X n) matrix
* \return A = A * B, an (n X n) matrix, or unmodified A on failure
*/
template <class T>
MbMatrix<T> &operator*=(MbMatrix<T> &A, const MbMatrix<T> &B) {
if (A.dim1()==A.dim2() && B.dim1()==B.dim2() && A.dim1()==B.dim1()) {
int N = A.dim1();
MbMatrix<T> C(N,N);
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
T sum = 0;
for (int k=0; k<N; k++)
sum += A[i][k] * B [k][j];
C[i][j] = sum;
}
}
A = C;
}
return A;
}
/*!
* Inequality operator. It calls operator== and returns
* the reverse of the bool result. Note that this operator
* is not useful for float and double matrices, but it is handy
* for int and bool matrices, as well as for matrices of other types
* that have a sensible operator!= defined.
*
* \brief Inequality operator