-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDMatrix.h
More file actions
1608 lines (1359 loc) · 45.4 KB
/
DMatrix.h
File metadata and controls
1608 lines (1359 loc) · 45.4 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
/*****************************************************************************
********************************* DMatrix.h **********************************
*****************************************************************************/
#if !defined(__DMATRIX_H__)
#define __DMATRIX_H__
/*
Templated classes providing Vector, 2D Array, and Matrix classes. The 2D
Array classes are separated from the Matrix classes so different 2D Array
storage schemes can be used as the basis of the Matrix classes.
None of the "standard" libraries I found satisfied my needs for something
understandable and documented well enough to be usable. One of the driving
factors in the design of these classes was to make the element access
methods as "natural" as possible, ie using the double subscript operation
to access individual elements of a 2D Array and hence matrix. Other
operator "eye candy" is provided with the caveats that some of those
operations are inefficient compared to their functional counterparts.
*/
/*****************************************************************************
****************************** I N C L U D E *******************************
*****************************************************************************/
#include <vector>
#include <limits>
#include <algorithm>
#include <cmath>
#include <cassert>
#include "DVector.h"
/*****************************************************************************
******************************* class DArray2D *******************************
*****************************************************************************/
/* Dense 2D Array. The elements are all stored in a single vector and another
vector of row pointers is provided for efficiency. This efficiency comes
at the cost of additional storage for the pointers, some initial
computation of the pointer values, maintenance of the pointers should the
arrangment change, and a bit of structural complexity. However, access
efficiency is increased since the row offset need not be computed every
time an element is accessed.
*/
#define DArray2DTemplate template <typename T>
DArray2DTemplate
class DArray2D
{
public :
DArray2D()
{
m_bRowsSwapped = false;
return;
}
DArray2D(size_t nRows, size_t nCols) : m_nRows(nRows), m_nCols(nCols),
m_E(nRows * nCols), m_R(nRows)
{
InitRowPtrs();
return;
}
DArray2D(const DArray2D& src)
{
Copy(src);
return;
}
~DArray2D()
{
return;
}
DArray2D<T>& operator=(const DArray2D<T>& rhs)
{
Copy(rhs);
return (*this);
}
size_t NumRows() const
{
return (m_nRows);
}
size_t NumCols() const
{
return (m_nCols);
}
T* operator[](size_t nRow)
{
return (m_R[nRow]);
}
const T* operator[](size_t nRow) const
{
return (m_R[nRow]);
}
// Initialize with a native array trusting it's large enough
void Initialize(const T Init[])
{
for (size_t i = 0 ; i < NumRows() * NumCols() ; i++)
{
m_E[i] = Init[i];
} // end for
return;
}
void Resize(size_t nRows, size_t nCols);
// Exchange two rows
void SwapRows(size_t r1, size_t r2)
{
// Just exchange the row pointers
std::swap(m_R[r1], m_R[r2]);
m_bRowsSwapped = true;
return;
}
protected :
// Array size information
size_t m_nRows;
size_t m_nCols;
// Storage vector for the individual elements
DVector<T> m_E;
// Vector of pointers to each row of the matrix
std::vector<T*> m_R;
// A flag indicating of any of the rows of the array have been swapped
// by exchanging row pointers such that the element vector cannot be
// considered continuous
bool m_bRowsSwapped;
// Initialize the row pointers
void InitRowPtrs()
{
for (size_t r = 0 ; r < NumRows() ; r++)
{
m_R[r] = &m_E[r * NumCols()];
} // end for
m_bRowsSwapped = false;
return;
}
// Make a copy
void Copy(const DArray2D<T>& src);
private :
}; // End of class DArray2D
/*****************************************************************************
*********************** Class DArray2D Implementation ************************
*****************************************************************************/
/*****************************************************************************
*
* DArray2D::Resize
*
* Resize the array to conform to a new number of rows and/or columns. Does
* NOT preserve element values after the resize. Safe to call with the same
* sizes.
*
*****************************************************************************/
DArray2DTemplate
void DArray2D<T>::Resize(size_t nRows, size_t nCols)
{
if ((nRows != NumRows()) || (nCols != NumCols()))
{
m_E.resize(nRows * nCols);
m_nCols = nCols;
if (nRows != NumRows())
{
m_R.resize(nRows);
m_nRows = nRows;
} // end if
InitRowPtrs();
} // end if
return;
} // End of function DArray2D::Resize
/*****************************************************************************
*
* DArray2D::Copy
*
* Make a copy of an array. Since the row pointers in the source may have
* been manipulated by row exchange operations, we can't simply use the
* vector::operator=() to copy the elements.
*
*****************************************************************************/
DArray2DTemplate
void DArray2D<T>::Copy(const DArray2D<T>& src)
{
m_bRowsSwapped = src.m_bRowsSwapped;
// Allocate storage and initialize row pointers
Resize(src.NumRows(), src.NumCols());
// Now copy the elements
for (size_t r = 0 ; r < NumRows() ; r++)
{
// Optimize access to the rows
T* R = (*this)[r];
const T* RS = src[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] = RS[c];
} // end for
} // end for
return;
} // End of function DArray2D::Copy
/*****************************************************************************
**************************** class DFixedArray2D *****************************
*****************************************************************************/
/*
A 2D Array where the storage is based on a fixed size native 2D Array.
Because of the storage scheme, some operations, particularly resizing,
cannot be performed.
*/
#define DFixedArray2DTemplate template <typename T, size_t ROWS, size_t COLS>
DFixedArray2DTemplate
class DFixedArray2D
{
public :
DFixedArray2D()
{
return;
}
// Dummy to allow this as template parameter for DMatrix
DFixedArray2D(size_t nRows, size_t nCols)
{
assert((nRows == ROWS) && (nCols == COLS));
// Throw an exception or just do nothing?
return;
}
DFixedArray2D(const DFixedArray2D<T, ROWS, COLS>& src)
{
Copy(src);
return;
}
~DFixedArray2D()
{
return;
}
DFixedArray2D& operator=(const DFixedArray2D<T, ROWS, COLS>& rhs)
{
Copy(rhs);
return (*this);
}
size_t NumRows() const
{
return (ROWS);
}
size_t NumCols() const
{
return (COLS);
}
// Initialize with a native array trusting it's large enough
void Initialize(const T Init[])
{
for (size_t i = 0, r = 0 ; r < NumRows() ; r++)
{
for (size_t c = 0 ; c < NumCols() ; c++)
{
m_E[r][c] = Init[i++];
} // end for
} // end for
return;
}
T* operator[](size_t nRow)
{
assert(nRow < ROWS);
return (m_E[nRow]);
}
const T* operator[](size_t nRow) const
{
assert(nRow < ROWS);
return (m_E[nRow]);
}
void Resize(size_t nRows, size_t nCols)
{
if ((nRows != ROWS) || (nCols != COLS))
{
// throw an exception
} // end if
return;
}
// Exchange two rows
void SwapRows(size_t r1, size_t r2)
{
for (size_t c = 0 ; c < COLS ; c++)
{
// Exchange each column in the two rows
std::swap(m_E[r1][c], m_E[r2][c]);
} // end for
return;
}
protected :
T m_E[ROWS][COLS];
// Make a copy
void Copy(const DFixedArray2D<T, ROWS, COLS>& src);
private :
}; // End of class DFixedArray2D
/*****************************************************************************
********************* Class DFixedArray2D Implementation *********************
*****************************************************************************/
/*****************************************************************************
*
* DFixedArray2D::Copy
*
* Make a copy of an array. Since the row pointers in the source may have
* been manipulated by row exchange operations, we can't simply use the
* vector::operator=() to copy the elements.
*
*****************************************************************************/
DFixedArray2DTemplate
void DFixedArray2D<T, ROWS, COLS>::Copy(
const DFixedArray2D<T, ROWS, COLS>& src)
{
// Can't be resized but Resize will check and handle the error
Resize(src.NumRows(), src.NumCols());
// Now copy the elements
for (size_t r = 0 ; r < NumRows() ; r++)
{
// Optimize access to the rows
T* R = (*this)[r];
const T* RS = src[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] = RS[c];
} // end for
} // end for
return;
} // End of function DFixedArray2D::Copy
/*****************************************************************************
******************************* class DMatrix ********************************
*****************************************************************************/
/*
A simple 2D Matrix implementation. The matrix is dense and uses a single
allocated array to store the data. The underlying 2D Array type used to
store the elements is specified by the template parameter ARRAY.
(I suppose another option is to use ARRAY as a base class.)
*/
// Make it easy to change the template parameters
#define DMatrixTemplate template <typename T, typename ARRAY>
DMatrixTemplate
class DMatrix
{
public :
DMatrix(T ZeroTest = 100 * std::numeric_limits<T>::epsilon())
{
m_ZeroTest = ZeroTest;
return;
}
DMatrix(size_t nRows, size_t nCols,
T ZeroTest = 100 * std::numeric_limits<T>::epsilon())
: m_A(nRows , nCols)
{
m_ZeroTest = ZeroTest;
return;
}
DMatrix(const DMatrix<T, ARRAY>& src)
: m_A(src.NumRows(), src.NumCols())
{
Copy(src);
return;
}
~DMatrix()
{
return;
}
DMatrix<T, ARRAY>& operator=(const DMatrix<T, ARRAY>& rhs)
{
Copy(rhs);
return (*this);
}
bool operator==(const DMatrix<T, ARRAY>& rhs);
size_t NumRows() const
{
return (m_A.NumRows());
}
size_t NumCols() const
{
return (m_A.NumCols());
}
void Resize(size_t nRows, size_t nCols)
{
m_A.Resize(nRows, nCols);
return;
}
// Initialize with a native array trusting it's large enough
void Initialize(const T Init[])
{
m_A.Initialize(Init);
return;
}
T& GetZeroTest()
{
return (m_ZeroTest);
}
const T& GetZeroTest() const
{
return (m_ZeroTest);
}
void SetZeroTest(const T& ZeroTest)
{
m_ZeroTest = ZeroTest;
return;
}
T* operator[](size_t nRow)
{
return (m_A.operator[](nRow));
}
const T* operator[](size_t nRow) const
{
return (m_A.operator[](nRow));
}
// Make this matrix the same size as the source
void MakeSameSize(const DMatrix<T, ARRAY>& src)
{
Resize(src.NumRows(), src.NumCols());
return;
}
bool IsSquare() const
{
return (NumRows() == NumCols());
}
bool IsSameSize(const DMatrix<T, ARRAY>& a) const
{
return (IsSameSize(*this, a));
}
static bool IsSameSize(const DMatrix<T, ARRAY>& a,
const DMatrix<T, ARRAY>& b)
{
return ((a.NumRows() == b.NumRows())&& (a.NumCols() == b.NumCols()));
}
void Set(T v = 0)
{
for (size_t i = 0 ; i < m_A.size() ; i++)
{
m_A[i] = v;
} // end for
return;
}
void Zero()
{
Set(0);
return;
}
// Make this matrix a diagonal matrix with d on the diagonal
bool Diagonal(const T& d);
// Make this matrix the Identity matrix
bool Identity()
{
return (Diagonal(1));
}
void Add(T s);
static void Add(DMatrix<T, ARRAY>& Result,
const DMatrix<T, ARRAY>& A, T s);
void Add(const DMatrix<T, ARRAY>& A, T s)
{
Add(*this, A, s);
return;
}
bool Add(const DMatrix<T, ARRAY>& A);
static bool Add(DMatrix<T, ARRAY>& Result, const DMatrix<T, ARRAY>& A,
const DMatrix<T, ARRAY>& B);
bool Add(const DMatrix<T, ARRAY>& A, const DMatrix<T, ARRAY>& B)
{
return (Add(*this, A, B));
}
void Sub(T s);
static void Sub(DMatrix<T, ARRAY>& Result,
const DMatrix<T, ARRAY>& A, T s);
void Sub(const DMatrix<T, ARRAY>& A, T s)
{
Sub(*this, A, s);
return;
}
bool Sub(const DMatrix<T, ARRAY>& A);
static bool Sub(DMatrix<T, ARRAY>& Result, const DMatrix<T, ARRAY>& A,
const DMatrix<T, ARRAY>& B);
bool Sub(const DMatrix<T, ARRAY>& A, const DMatrix<T, ARRAY>& B)
{
return (Sub(*this, A, B));
}
// Multiply by a scalar
void Mul(T s);
static void Mul(DMatrix<T, ARRAY>& Result,
const DMatrix<T, ARRAY>& A, T s);
void Mul(const DMatrix<T, ARRAY>& A, T s)
{
Mul(*this, A, s);
return;
}
// Matrix multiplication
static bool Mul(DMatrix<T, ARRAY>& Result, const DMatrix<T, ARRAY>& A,
const DMatrix<T, ARRAY>& B);
bool Mul(const DMatrix<T, ARRAY>& A, const DMatrix<T, ARRAY>& B)
{
return (Mul(*this, A, B));
}
// Transpose multiplication
bool MulAxBTranspose(const DMatrix<T, ARRAY>& A,
const DMatrix<T, ARRAY>& B);
bool MulATransposexB(const DMatrix<T, ARRAY>& A,
const DMatrix<T, ARRAY>& B);
// Arithmetic operators
// Possibly introduces temporaries, copy ops, & exceptions)
DMatrix<T, ARRAY> operator+(DMatrix<T, ARRAY>& rhs) const
{
DMatrix<T, ARRAY> C(NumRows(), NumCols());
if (!DMatrix<T, ARRAY>::Add(C, (*this), rhs))
{
// Throw
} // end if
return (C);
}
void operator+=(const DMatrix<T, ARRAY>& rhs)
{
Add(*this, rhs);
return;
}
DMatrix<T, ARRAY> operator+(T rhs) const
{
DMatrix<T, ARRAY> C(NumRows(), NumCols());
if (!DMatrix<T, ARRAY>::Add(C, (*this), rhs))
{
// Throw
} // end if
return (C);
}
void operator+=(T s)
{
Add(s);
return;
}
DMatrix<T, ARRAY> operator-(DMatrix<T, ARRAY>& rhs) const
{
DMatrix<T, ARRAY> C(NumRows(), NumCols());
if (!DMatrix<T, ARRAY>::Sub(C, (*this), rhs))
{
// Throw
} // end if
return (C);
}
void operator-=(const DMatrix<T, ARRAY>& rhs)
{
Sub(*this, rhs);
return;
}
DMatrix<T, ARRAY> operator-(T rhs) const
{
DMatrix<T, ARRAY> C(NumRows(), NumCols());
if (!DMatrix<T, ARRAY>::Sub(C, (*this), rhs))
{
// Throw
} // end if
return (C);
}
void operator-=(T s)
{
Sub(s);
return;
}
DMatrix<T, ARRAY> operator*(DMatrix<T, ARRAY>& rhs) const
{
DMatrix<T, ARRAY> C(NumRows(), rhs.NumCols());
if (!DMatrix<T, ARRAY>::Mul(C, (*this), rhs))
{
// Throw
} // end if
return (C);
}
DMatrix<T, ARRAY> operator*(T rhs) const
{
DMatrix<T, ARRAY> C(NumRows(), NumCols());
if (!DMatrix<T, ARRAY>::Mul(C, (*this), rhs))
{
// Throw
} // end if
return (C);
}
void operator*=(T s)
{
Mul(s);
return;
}
// Transpose this matrix in place
void Transpose();
// Make this matrix the transpose of A
void Transpose(const DMatrix<T, ARRAY>& A);
// Compute the inverse of A
static bool Invert(DMatrix<T, ARRAY>& A, DMatrix<T, ARRAY>& AI);
bool Invert(DMatrix<T, ARRAY>& AI)
{
return (Invert((*this), AI));
}
// Solve simultaneous equations
static bool GaussElim(DMatrix<T, ARRAY>& A, std::vector<T>& b,
std::vector<T>& x);
// Given an upper triangular matix, back solve equations
static bool SolveTriangular(DMatrix<T, ARRAY>& A, std::vector<T>& b,
std::vector<T>& x);
// Multiply vector rhs by the matrix A
static bool MulVector(DMatrix<T, ARRAY>& A, std::vector<T>& rhs,
std::vector<T>& out);
// Solve simultaneous equations with this matrix
bool GaussElim(std::vector<T>& b, std::vector<T>& x)
{
return (GaussElim(*this, b, x));
}
// Multiply the vector rhs by this matrix
bool MulVector(std::vector<T>& rhs, std::vector<T>& out)
{
return (MulVector(*this, rhs, out));
}
// Exchange two rows in the matrix
void SwapRows(size_t r1, size_t r2)
{
m_A.SwapRows(r1, r2);
return;
}
protected :
ARRAY m_A; // Elements
T m_ZeroTest;
// Make a copy
void Copy(const DMatrix<T, ARRAY>& src)
{
m_ZeroTest = src.m_ZeroTest;
m_A = src.m_A;
return;
}
// Find the largest column element to use as the pivot row
size_t FindMaxPivot(size_t nCol, T& MaxPivot) const
{
T AbsMaxPivot = std::abs((*this)[nCol][nCol]);
size_t nPivotRow = nCol;
for (size_t r = nCol + 1 ; r < NumRows() ; r++)
{
T Test = std::abs((*this)[r][nCol]);
if (Test > AbsMaxPivot)
{
AbsMaxPivot = Test;
nPivotRow = r;
} // end if
} // end for
MaxPivot = (*this)[nPivotRow][nCol];
return (nPivotRow);
}
private :
}; // End of class DMatrix
/*****************************************************************************
************************ Class DMatrix Implementation ************************
*****************************************************************************/
/*****************************************************************************
*
* DMatrix::operator==
*
* Test the passed right hand side matrix with this matrix for equality.
* We accept the matrices are equal if they're the same size and the element
* differences are within the Zero Test.
*
*****************************************************************************/
DMatrixTemplate
bool DMatrix<T, ARRAY>::operator==(const DMatrix<T, ARRAY>& rhs)
{
bool bRet = (NumRows() == rhs.NumRows()) && (NumCols() == rhs.NumCols());
for (size_t r = 0 ; (r < NumRows()) && bRet ; r++)
{
T* R = (*this)[r];
const T* RT = rhs[r];
for (size_t c = 0 ; (c < NumCols()) && bRet ; c++)
{
bRet = std::abs(R[c] - RT[c]) < m_ZeroTest;
} // end for
} // end for
return (bRet);
} // End of function DMatrix::operator==
/*****************************************************************************
*
* DMatrix::Diagonal
*
* Set this matrix to a diagonal matrix. This matrix must be square.
*
*****************************************************************************/
DMatrixTemplate
bool DMatrix<T, ARRAY>::Diagonal(const T& d)
{
bool bRet = IsSquare();
if (bRet)
{
for (size_t r = 0 ; r < NumRows() ; r++)
{
T* R = (*this)[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] = (r == c) ? d : static_cast<T>(0);
} // end for
} // end for
} // end if
return (bRet);
} // End of function DMatrix::Diagonal
/*****************************************************************************
*
* DMatrix::Add
*
* Add a scalar to this matrix.
*
*****************************************************************************/
DMatrixTemplate
void DMatrix<T, ARRAY>::Add(T s)
{
for (size_t r = 0 ; r < NumRows() ; r++)
{
T* R = (*this)[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] += s;
} // end for
} // end for
return;
} // End of function DMatrix::Add
/*****************************************************************************
*
* DMatrix::Add
*
* Make the Result matrix equal to the sum of the matrix A and the scalar s.
*
*****************************************************************************/
DMatrixTemplate
void DMatrix<T, ARRAY>::Add(DMatrix<T, ARRAY>& Result,
const DMatrix<T, ARRAY>& A, T s)
{
Result.MakeSameSize(A);
for (size_t r = 0 ; r < Result.NumRows() ; r++)
{
T* R = Result[r];
const T* RA = A[r];
for (size_t c = 0 ; c < Result.NumCols() ; c++)
{
R[c] = RA[c] + s;
} // end for
} // end for
return;
} // End of function DMatrix::Add
/*****************************************************************************
*
* DMatrix::Add
*
* Add the matrix a to this matrix. Sizes must be the same.
*
*****************************************************************************/
DMatrixTemplate
bool DMatrix<T, ARRAY>::Add(const DMatrix<T, ARRAY>& A)
{
bool bRet = IsSameSize(A);
if (bRet)
{
for (size_t r = 0 ; r < NumRows() ; r++)
{
T* R = (*this)[r];
T* RA = A[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] += RA[c];
} // end for
} // end for
} // end if
return (bRet);
} // End of function DMatrix::Add
/*****************************************************************************
*
* DMatrix::Add
*
* Make the Result matrix the sum of the matrices A & B. The input matrices
* must be the same size.
*
*****************************************************************************/
DMatrixTemplate
bool DMatrix<T, ARRAY>::Add(DMatrix<T, ARRAY>& Result,
const DMatrix<T, ARRAY>& A, const DMatrix<T, ARRAY>& B)
{
bool bRet = B.IsSameSize(A);
if (bRet)
{
Result.MakeSameSize(A);
for (size_t r = 0 ; r < Result.NumRows() ; r++)
{
T* R = Result[r];
const T* RA = A[r];
const T* RB = B[r];
for (size_t c = 0 ; c < Result.NumCols() ; c++)
{
R[c] = RA[c] + RB[c];
} // end for
} // end for
} // end if
return (bRet);
} // End of function DMatrix::Add
/*****************************************************************************
*
* DMatrix::Sub
*
* Subtract a scalar from this matrix.
*
*****************************************************************************/
DMatrixTemplate
void DMatrix<T, ARRAY>::Sub(T s)
{
for (size_t r = 0 ; r < NumRows() ; r++)
{
T* R = (*this)[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] -= s;
} // end for
} // end for
return;
} // End of function DMatrix::Sub
/*****************************************************************************
*
* DMatrix::Sub
*
* Make the Result matrix equal to the differece of the matrix A and the
* scalar s.
*
*****************************************************************************/
DMatrixTemplate
void DMatrix<T, ARRAY>::Sub(DMatrix<T, ARRAY>& Result,
const DMatrix<T, ARRAY>& A, T s)
{
Result.MakeSameSize(A);
for (size_t r = 0 ; r < Result.NumRows() ; r++)
{
T* R = Result[r];
const T* RA = A[r];
for (size_t c = 0 ; c < Result.NumCols() ; c++)
{
R[c] = RA[c] - s;
} // end for
} // end for
return;
} // End of function DMatrix::Sub
/*****************************************************************************
*
* DMatrix::Sub
*
* Subtract the matrix A from this matrix. Sizes must be the same.
*
*****************************************************************************/
DMatrixTemplate
bool DMatrix<T, ARRAY>::Sub(const DMatrix<T, ARRAY>& A)
{
bool bRet = IsSameSize(A);
if (bRet)
{
for (size_t r = 0 ; r < NumRows() ; r++)
{
T* R = (*this)[r];
T* RA = A[r];
for (size_t c = 0 ; c < NumCols() ; c++)
{
R[c] -= RA[c];
} // end for
} // end for
} // end if
return (bRet);