-
Notifications
You must be signed in to change notification settings - Fork 0
/
blMatrixOneDim.hpp
2925 lines (2360 loc) · 87.5 KB
/
blMatrixOneDim.hpp
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
//---------------------------------------------------------------------------------------
// CLASS: vbMatrix<vbType>
// BASE CLASS: None
// PURPOSE: Matrix container with various algorithms
// AUTHOR: Vincenzo Barbato
// (713)402-8451
// DATE CREATED: Aug/27/2009
// DATE UPDATED:
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// Make sure we only load this file once
#ifndef _VBMATRIX_
#define _VBMATRIX_
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
class vbMatrix
{
public: // Default constructors and destructors
// Default constructor builds an mxn matrix with an initialize value
vbMatrix(const int& NumOfRows = 0,
const int& NumOfCols = 0,
const vbType& InitialValue = vbType(0));
// Copy constructor
vbMatrix(const vbMatrix<vbType>& Matrix);
// Constructor builds matrix from a two dimensional array
template<int m,int n>
vbMatrix(const vbType (&MatrixArray)[m][n]);
// Constructor builds matrix from augmenting two matrices
vbMatrix(const vbMatrix<vbType>& M1,
const vbMatrix<vbType>& M2,
const bool& AreMatricesToBeAugmentedHorizontally);
// Default destructor
~vbMatrix(void);
public: // Overloaded operators
// Used to access an individual value of the matrix in matrix format (i,j)
vbType& operator()(const int& i,const int& j);
const vbType& operator()(const int& i,const int& j)const;
// Used to access an individual value of the matrix in array format (i) or [i]
vbType& operator()(const int& i);
const vbType& operator()(const int& i)const;
vbType& operator[](const int& i);
const vbType& operator[](const int& i)const;
// Used for basic matrix operations
vbMatrix<vbType>& operator+=(const vbMatrix<vbType>& M);
vbMatrix<vbType>& operator-=(const vbMatrix<vbType>& M);
vbMatrix<vbType>& operator*=(const vbType& x);
vbMatrix<vbType>& operator*=(const vbMatrix<vbType>& M);
vbMatrix<vbType>& operator/=(const vbType& x);
vbMatrix<vbType>& operator/=(const vbMatrix<vbType>& M);
void operator++(int);
vbMatrix<vbType>& operator++();
void operator--(int);
vbMatrix<vbType>& operator--();
public: // Public variables
public: // Public functions
// Used to get the matrix array
vector<vbType>& GetMatrixArray();
const vector<vbType>& GetMatrixArray()const;
// Used to save the matrix to an xml file
void SaveToXml(CkXml* XmlFile,
const string& Name = "");
bool LoadFromXml(CkXml* XmlFile);
// Used to re-zero the matrix
void ReZero();
// Functions derived from std::vector
void clear();
const int& size()const;
vbType& front();
const vbType& front()const;
// Used to round-off all values in the matrix
void RoundOff(const int& Precision);
// Used to make a matrix square (if it's not already square)
// by adding zero rows or columns as needed
void MakeSquare();
// Used to set a block of the matrix using another matrix
void SetMatrixBlock(const int& i,
const int& j,
const vbMatrix<vbType>& M);
// Used to get block-matrices from this matrix
vbMatrix<vbType> GetMatrixBlock(const int& i1,
const int& j1,
const int& i2,
const int& j2);
void GetMatrixBlock(const int& i1,
const int& j1,
const int& i2,
const int& j2,
vbMatrix<vbType>& M);
void GetMatrixBlocks(vbMatrix<vbType>& A,vbMatrix<vbType>& B,
vbMatrix<vbType>& C,vbMatrix<vbType>& D);
void GetMatrixBlocks2(vbMatrix<vbType>& A,vbMatrix<vbType>& B,
vbMatrix<vbType>& C,vbMatrix<vbType>& D);
// Used to define a zero (where anything smaller than this number
// might as well be considered zero due to the finite machine precision)
vbType DefineZero();
// Static functions used to create unit row or column vectors
static vbMatrix<vbType> CreateUnitRowVector(const int& NumOfCols,
const int& WhichComponentToMakeUnity);
static vbMatrix<vbType> CreateUnitColVector(const int& NumOfRows,
const int& WhichComponentToMakeUnity);
// Used to zero out all the very small values in the matrix
void CleanZeroes(const vbType& Zero);
// Used to get row/column vectors from this matrix
const vbMatrix<vbType> GetRowVector(const int& i)const;
const vbMatrix<vbType> GetColVector(const int& i)const;
// Used to get the magnitude of a row or column vector
vbType GetRowVectorMagnitude(const int& WhichRow);
vbType GetColVectorMagnitude(const int& WhichColumn);
// Used to get non-zero column vector(s) from this matrix
vbMatrix<vbType> GetNonZeroColVectors(const vbType& Zero,
const int& NumOfNonZeroVectorsToGet);
// Functions used to randomize the matrix
void Mutate(const vbType& MutationAmountPercentage);
void RandomizeMatrix(const vbType& MinValue,
const vbType& MaxValue);
void RandomizeMatrixIntelligently(const vbType& MinValue,
const vbType& MaxValue,
const vbType& SmoothnessConstant);
void RowSwap(const int& Row1,const int& Row2);
void ColSwap(const int& Col1,const int& Col2);
void Normalize();
void OrthoNormalizeMatrix();
const vbMatrix<vbType> GetNormalizedMatrix()const;
const vbMatrix<vbType> GetOrthonormalizedMatrix()const;
const int& GetNumOfRows()const;
const int& GetNumOfCols()const;
void SetRowVector(const int& i,const vbMatrix<vbType>& Vector);
void SetColVector(const int& i,const vbMatrix<vbType>& Vector);
void AddZeroRowVectors(const int& NumOfRowVectorsToAdd);
void AddZeroColVectors(const int& NumOfColVectorsToAdd);
void AddRowVector(const vbMatrix<vbType>& RowVector);
void AddColVector(const vbMatrix<vbType>& ColVector);
private: // Private variables
// Matrix array (holds the matrix values)
vector<vbType> m_Matrix;
// Variables used to hold the size of the matrix
int m_NumOfRows;
int m_NumOfCols;
private: // Private functions
};
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>::~vbMatrix(void)
{
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>::vbMatrix(const int& NumOfRows,
const int& NumOfCols,
const vbType& InitialValue)
{
// Store the number of rows and columns
m_NumOfRows = NumOfRows;
m_NumOfCols = NumOfCols;
// The matrix will have m_NumOfRows*m_NumOfCols values in it
m_Matrix = vector<vbType>((m_NumOfRows*m_NumOfCols),InitialValue);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>::vbMatrix(const vbMatrix<vbType>& Matrix)
{
// Copy the matrix size
m_NumOfRows = Matrix.GetNumOfRows();
m_NumOfCols = Matrix.GetNumOfCols();
// Copy the matrix
m_Matrix = Matrix.GetMatrixArray();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>::vbMatrix(const vbMatrix<vbType>& M1,const vbMatrix<vbType>& M2,
const bool& AreMatricesToBeAugmentedHorizontally)
{
// Let's get the number of rows and columns of the two matrices
int m1 = M1.GetNumOfRows();
int n1 = M1.GetNumOfCols();
int m2 = M2.GetNumOfRows();
int n2 = M2.GetNumOfCols();
// Let's check which way we have to augment the matrices,
// where we either stack the matrices side to side or
// stack them vertically
if(AreMatricesToBeAugmentedHorizontally)
{
// Check which matrix has more rows and reserve the
// necessary memory size of the new matrix accordingly
if(m1 >= m2)
(*this) = vbMatrix(m1,n1+n2,0);
else
(*this) = vbMatrix(m2,n1+n2,0);
// Use the two matrices to fill the new matrix values
SetMatrixBlock(0,0,M1);
SetMatrixBlock(0,n1,M2);
}
else
{
// Check which matrix has more columns and reserve the
// necessary memory size of the new matrix accordingly
if(n1 >= n2)
(*this) = vbMatrix(m1+m2,n1,0);
else
(*this) = vbMatrix(m1+m2,n2,0);
// Use the two matrices to fill the new matrix values
SetMatrixBlock(0,0,M1);
SetMatrixBlock(m1,0,M2);
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
template<int m,int n>
inline vbMatrix<vbType>::vbMatrix(const vbType (&a)[m][n])
{
// Store the number of rows and columns
m_NumOfRows = m;
m_NumOfCols = n;
// Create the matrix array
m_Matrix = vector<vbType>((m_NumOfRows*m_NumOfCols),0);
#pragma omp parallel
{
#pragma omp for
for(int i = 0; i < m_NumOfRows; ++i)
for(int j = 0; j < m_NumOfColumns; ++j)
(*this)(i,j) = a[i][j];
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vector<vbType>& vbMatrix<vbType>::GetMatrixArray()
{
return m_Matrix;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vector<vbType>& vbMatrix<vbType>::GetMatrixArray()const
{
return m_Matrix;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::clear()
{
// Reset the number of rows and columns to zero
m_NumOfRows = 0;
m_NumOfCols = 0;
// Clear the entire matrix array
m_Matrix.clear();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const int& vbMatrix<vbType>::size()const
{
return m_Matrix.size();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType& vbMatrix<vbType>::front()
{
return m_Matrix.front();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vbType& vbMatrix<vbType>::front()const
{
return m_Matrix.front();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::ReZero()
{
#pragma omp parallel
{
#pragma omp for
for(int i = 0; i < m_NumOfRows; ++i)
for(int j = 0; j < m_NumOfColumns; ++j)
(*this)(i,j) = vbType(0);
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::RoundOff(const int& Precision)
{
// Step through all the values and round-off each value to
// the desired precision
#pragma omp parallel
{
#pragma omp for
for(int i = 0; i < m_NumOfRows; ++i)
for(int j = 0; j < m_NumOfColumns; ++j)
(*this)(i,j) = vbMath::RoundOff((*this)(i,j),Precision);
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::MakeSquare()
{
// Check the matrix size and do the following
if(m_NumOfRows == m_NumOfCols)
{
// The matrix is already square, so we just return
return;
}
if(m_NumOfRows > m_NumOfCols)
{
// There are more rows than columns, so we add zero columns
AddZeroColVectors(m_NumOfRows - m_NumOfCols);
return;
}
if(m_NumOfRows < m_NumOfCols)
{
// There are more columns than rows, so we add zero rows
AddZeroRowVectors(m_NumOfCols - m_NumOfRows);
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType vbMatrix<vbType>::DefineZero()
{
// Define what it means to be zero
if(m_NumOfRows >= m_NumOfCols)
return (numeric_limits<vbType>::epsilon() * m_NumOfRows * vbType(10) * Norm1(*this));
else
return (numeric_limits<vbType>::epsilon() * m_NumOfCols * vbType(10) * NormInf(*this));
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
static inline vbMatrix<vbType> vbMatrix<vbType>::CreateUnitRowVector(const int& NumOfCols,
const int& WhichComponentToMakeUnity)
{
// Check index validity
if(WhichComponentToMakeUnity < 0 || WhichComponentToMakeUnity >= NumOfCols)
return CreateUnitRowVector<vbType>(NumOfCols,0);
vbMatrix<vbType> Matrix(1,NumOfCols,0);
Matrix(0,WhichComponentToMakeUnity) = vbType(1);
return Matrix;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
static inline vbMatrix<vbType> vbMatrix<vbType>::CreateUnitColVector(const int& NumOfRows,
const int& WhichComponentToMakeUnity)
{
// Check index validity
if(WhichComponentToMakeUnity < 0 || WhichComponentToMakeUnity >= NumOfRows)
return CreateUnitColVector<vbType>(NumOfRows,0);
vbMatrix<vbType> Matrix(NumOfRows,1,0);
Matrix(WhichComponentToMakeUnity,0) = vbType(1);
return Matrix;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vbMatrix<vbType> vbMatrix<vbType>::GetRowVector(const int& i)const
{
if(i >= m_NumOfRows)
{
GlobalErrorLog += "\nTried to access row vector outside of matrix range";
return vbMatrix<vbType> EmptyMatrix;
}
else
{
vbMatrix<vbType> RowVector(1,m_NumOfCols,0);
for(int j = 0; j < m_NumOfCols; ++j)
RowVector(0,j) = (*this)(i,j);
return RowVector;
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vbMatrix<vbType> vbMatrix<vbType>::GetColVector(const int& i)const
{
if(i >= m_NumOfCols)
{
GlobalErrorLog += "\nTried to access col vector outside of matrix range";
return vbMatrix<vbType>(0,0,vbType(0));
}
else
{
vbMatrix<vbType> ColVector(m_NumOfRows,1,0);
for(int j = 0; j < m_NumOfRows; ++j)
ColVector(j,0) = (*this)(j,i);
return ColVector;
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType vbMatrix<vbType>::GetRowVectorMagnitude(const int& WhichRow)
{
// Check index validity
if(i < 0 || i >= m_NumOfRows)
{
GlobalErrorLog += "\nTried to calculate the magnitude of a row vector using the wrong index";
return vbType(0);
}
vbType Mag = 0;
for(int j = 0; j < m_NumOfCols; ++j)
Mag += ((*this)(WhichRow,j) * (*this)(WhichRow,j));
return std::sqrt(Mag);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType vbMatrix<vbType>::GetColVectorMagnitude(const int& WhichColumn)
{
// Check index validity
if(i < 0 || i >= m_NumOfCols)
{
GlobalErrorLog += "\nTried to calculate the magnitude of a col vector with out of bounds index";
return vbType(0);
}
vbType Mag = 0;
for(int j = 0; j < m_NumOfRows; ++j)
Mag += ((*this)(j,WhichColumn) * (*this)(j,WhichColumn));
return std::sqrt(Mag);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType> vbMatrix<vbType>::GetNonZeroColVectors(const vbType& Zero,
const int& NumOfNonZeroVectorsToGet)
{
// Check for input validity
if(NumOfNonZeroVectorsToGet > m_NumOfCols)
{
GlobalErrorLog += "\nTried to get more non-zero column vectors from matrix than there are column in this matrix";
return GetNonZeroColVectors(Zero,m_NumOfCols);
}
if(NumOfNonZeroVectorsToGet <= 0)
{
GlobalErrorLog += "\nTried to get a \"negative\" number of non-zero column vectors from matrix, which doesn't make any sense";
return vbMatrix<vbType>(0,0,vbType(0));
}
// Prepare the NonZeroColVectors matrix
vbMatrix<vbType> NonZeroColVectors(0,0,0);
// Index used to keep track of non zero vectors
int Index;
// Get the first non zero col vector from the matrix
for(int i = 0; i < m_NumOfRows; ++i)
{
for(int j = 0; j < m_NumOfCols; ++j)
{
if(!(EqualsZero(at(i,j),Zero)))
{
NonZeroColVectors.AddColVector(GetColVector(j));
Index = j;
// break from both for loops
i = m_NumOfRows;
j = m_NumOfCols;
}
}
}
// If only one vector is needed then return
if(NumOfNonZeroVectorsToGet == 1)
return NonZeroColVectors;
// Go through all the vectors until we have found
// the needed independent non-zero vectors
for(int i = Index+1; i < m_NumOfCols; ++i)
{
// Add the next vector to the NonZeroColVectors matrix and check the rank
// If it has full rank, then keep it, otherwise discard it and move on to the next
// col vector
vbMatrix<vbType> CCC = vbMatrix<vbType>(NonZeroColVectors,GetColVector(i),true);
if(rank(CCC,Zero) == (NonZeroColVectors.GetNumOfCols() + 1))
NonZeroColVectors.AddColVector(GetColVector(i));
// Check if it found all the needed independent vectors
if(NonZeroColVectors.GetNumOfCols() >= NumOfNonZeroVectorsToGet)
return NonZeroColVectors;
}
return NonZeroColVectors;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::GetMatrixBlock(const int& i1,
const int& j1,
const int& i2,
const int& j2,
vbMatrix<vbType>& M)
{
// Check for index validity
if((i1 < 0) || (j1 < 0) || (i2 < 0) || (j2 < 0) ||
(i1 > m_NumOfRows) || (j1 > m_NumOfCols) || (i2 > m_NumOfRows) || (j2> m_NumOfCols))
{
GlobalErrorLog += "\nUsed wrong indeces when trying to access a matrix sub-block";
return;
}
// Sort the indeces
if(i1 > i2)
std::swap(i1,i2);
if(j1 > j2)
std::swap(j1,j2);
// Initialize the matrix
M = vbMatrix<vbType>(i2-i1+1,j2-j1+1,0);
for(int i = i1; i <= i2; ++i)
for(int j = j1; j <= j2; ++j)
M(i-i1,j-j1) = m_Matrix[i][j];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType> vbMatrix<vbType>::GetMatrixBlock(const int& i1,
const int& j1,
const int& i2,
const int& j2)
{
// Check for index validity
if((i1 < 0) || (j1 < 0) || (i2 < 0) || (j2 < 0) ||
(i1 >= m_NumOfRows) || (j1 >= m_NumOfCols) || (i2 >= m_NumOfRows) || (j2>= m_NumOfCols))
{
GlobalErrorLog += "\nUsed wrong indeces when trying to access a matrix sub-block";
return vbMatrix<vbType>(0,0,vbType(0));
}
// Sort the indeces
if(i1 > i2)
std::swap(i1,i2);
if(j1 > j2)
std::swap(j1,j2);
// Initialize the matrix
vbMatrix<vbType> M = vbMatrix<vbType>(i2-i1+1,j2-j1+1,0);
for(int i = i1; i <= i2; ++i)
for(int j = j1; j <= j2; ++j)
M(i-i1,j-j1) = m_Matrix[i][j];
return M;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::GetMatrixBlocks(vbMatrix<vbType> &A,vbMatrix<vbType> &B,
vbMatrix<vbType> &C,vbMatrix<vbType> &D)
{
// Divide the matrix into blocks as follows: M = [A,B;C,D]
// A -- (m-1)x(m-1)
// B -- (m-1)x1
// C -- 1x(m-1)
// D -- 1x1
A = GetMatrixBlock(0,0,m_NumOfRows-2,m_NumOfRows-2);
B = GetMatrixBlock(0,m_NumOfRows-1,m_NumOfRows-2,m_NumOfRows-1);
C = GetMatrixBlock(m_NumOfRows-1,0,m_NumOfRows-1,m_NumOfRows-2);
D = GetMatrixBlock(m_NumOfRows-1,m_NumOfRows-1,m_NumOfRows-1,m_NumOfRows-1);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::GetMatrixBlocks2(vbMatrix<vbType> &A,vbMatrix<vbType> &B,
vbMatrix<vbType> &C,vbMatrix<vbType> &D)
{
// Divide the matrix into blocks as follows: M = [A,B;C,D]
// A -- 1x1
// B -- 1x(m-1)
// C -- m(-1)x1
// D -- (m-1)x(m-1)
A = GetMatrixBlock(0,0,0,0);
B = GetMatrixBlock(0,1,0,m_NumOfRows-1);
C = GetMatrixBlock(1,0,m_NumOfRows-1,0);
D = GetMatrixBlock(1,1,m_NumOfRows-1,m_NumOfRows-1);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::SetMatrixBlock(const int& i1,
const int& j1,
const vbMath::vbMatrix<vbType> &M)
{
// Check for index validity
if((i1 < 0) || (j1 < 0) || (i1 >= m_NumOfRows) || (j1 >= m_NumOfCols))
{
GlobalErrorLog += "\nUsed wrong indeces when trying to set a matrix sub-block";
return;
}
int m,n;
// Check to see if the matrix M is bigger than this matrix
if((i1 + M.GetNumOfRows()) > m_NumOfRows)
m = m_NumOfRows - i1;
else
m = M.GetNumOfRows();
if((j1 + M.GetNumOfCols()) > m_NumOfCols)
n = m_NumOfCols - j1;
else
n = M.GetNumOfCols();
// Assign the elements of M to this matrix
for(int i = i1; i < i1+m; ++i)
for(int j = j1; j < j1+n; ++j)
(*this)(i,j) = M(i-i1,j-j1);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline void vbMatrix<vbType>::SaveToXml(CkXml *XmlFile,const string& Name)
{
// Create the matrix tag
XmlFile = XmlFile->NewChild("Matrix","");
// Add name attribute
XmlFile->AddAttribute("Name",Name.c_str());
// Add size attribute
XmlFile->AddAttribute("NumOfRows",ConvertNumber(m_NumOfRows).c_str());
XmlFile->AddAttribute("NumOfCols",ConvertNumber(m_NumOfCols).c_str());
// Write the actual values
for(int i = 0; i < m_NumOfRows; ++i)
{
// Create the row tag
XmlFile = XmlFile->NewChild("Row","");
// Add the row number attribute
XmlFile->AddAttribute("RowNumber",ConvertNumber(i).c_str());
// Add the columns tabs for each row tab
for(int j = 0; j < m_NumOfCols; ++j)
{
// Create the column tab
XmlFile = XmlFile->NewChild("Col","");
// Add the column number attribute
XmlFile->AddAttribute("ColNumber",ConvertNumber(j).c_str());
// Add the value attribute
XmlFile->AddAttribute("Value",ConvertNumber(at(i,j)).c_str());
// Get back to the row level
XmlFile = XmlFile->GetParent();
}
// Get back to the matrix level
XmlFile = XmlFile->GetParent();
}
// Get back to the parent level
XmlFile = XmlFile->GetParent();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline bool vbMatrix<vbType>::LoadFromXml(CkXml* XmlFile)
{
// Enter the matrix tag
if(!(XmlFile = XmlFile->FindChild("Matrix")))
return false;
CkString Value;
// Get the number of rows
if(!(XmlFile->GetAttrValue("NumOfRows",Value)))
return false;
int NumOfRows = Value.intValue();
// Get the number of cols
if(!(XmlFile->GetAttrValue("NumOfCols",Value)))
return false;
int NumOfCols = Value.intValue();
Create(NumOfRows,NumOfCols,0);
// Enter the row tag
if(!(XmlFile = XmlFile->FirstChild()))
return false;
// Read the individual values from the xml file
for(int i = 0; i < NumOfRows; ++i)
{
// Enter the first col tag
if(!(XmlFile = XmlFile->FirstChild()))
return false;
// Go through the columns
for(int j = 0; j < NumOfCols; ++j)
{
// Get the value
if(!(XmlFile->GetAttrValue("Value",Value)))
return false;
// Write the value to the matrix
at(i,j) = Value.doubleValue();
// Go to the next col tag
if(j < (NumOfCols-1))
if(!(XmlFile = XmlFile->NextSibling()))
return false;
}
// Get back to the row tag
if(!(XmlFile = XmlFile->GetParent()))
return false;
// Go to the next row tag
if(i < (NumOfRows-1))
if(!(XmlFile = XmlFile->NextSibling()))
return false;
}
// Get back to the matrix tag
if(!(XmlFile = XmlFile->GetParent()))
return false;
// Get back to the parent level
if(!(XmlFile = XmlFile->GetParent()))
return false;
return true;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline ostream& operator<<(ostream& os,const vbMatrix<vbType>& A)
{
int m = A.GetNumOfRows();
int n = A.GetNumOfCols();
// Get the maximum length of all the entries so as to set the format
// of the matrix output text
vbMatrix<int> Widths(1,n,0);
string TempString;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
TempString = ConvertNumber(A(i,j));
if(TempString.length() > std::real(Widths(0,j)))
Widths(0,j) = TempString.length();
}
}
// Write the matrix to the output stream formatting it
// using the calculated maximum string length
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
// Write the ith,jth value to the string
os << setw(std::real(Widths(0,j))) << std::left << ConvertNumber(A(i,j));
if(j == (n-1) && i != (m-1))
os << "\n";
else if(j != (n-1))
os << " ";
}
}
return os;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vbType& vbMatrix<vbType>::operator()(const int& i,const int& j)const
{
return m_Matrix[i*m_NumOfCols + j];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType& vbMatrix<vbType>::operator()(const int& i,const int& j)
{
return m_Matrix[i*m_NumOfCols + j];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vbType& vbMatrix<vbType>::operator()(const int& i)const
{
return m_Matrix[i];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType& vbMatrix<vbType>::operator()(const int& i)
{
return m_Matrix[i];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline const vbType& vbMatrix<vbType>::operator[](const int& i)const
{
return m_Matrix[i];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbType& vbMatrix<vbType>::operator[](const int& i)
{
return m_Matrix[i];
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>& vbMatrix<vbType>::operator*=(const vbType& x)
{
(*this) = (*this)*x;
return (*this);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>& vbMatrix<vbType>::operator*=(const vbMatrix<vbType>& M)
{
(*this) = (*this)*M;
return (*this);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>& vbMatrix<vbType>::operator/=(const vbType& x)
{
(*this) = (*this)*(vbType(1)/x);
return (*this);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
template<typename vbType>
inline vbMatrix<vbType>& vbMatrix<vbType>::operator/=(const vbMatrix<vbType>& M)
{
(*this) = (*this)*inv(M);
return (*this);
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------