-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_matrix.h
2410 lines (2141 loc) · 69 KB
/
C_matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
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 : C_matrix.h
# ( C++ header file )
#
# Description : pas besoin
#
# Project manager : moi
#
# Licenses : This file is 'dual-licensed', you have to choose one
# of the two licenses below to apply.
#
# CeCILL-C
# The CeCILL-C license is close to the GNU LGPL.
# ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html )
#
# or CeCILL v2.0
# The CeCILL license is compatible with the GNU GPL.
# ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
#
# This software is governed either by the CeCILL or the CeCILL-C license
# under French law and abiding by the rules of distribution of free software.
# You can use, modify and or redistribute the software under the terms of
# the CeCILL or CeCILL-C licenses as circulated by CEA, CNRS and INRIA
# at the following URL: "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL and CeCILL-C licenses and that you accept its terms.
#
*/
#ifndef C_MATRIX_H
#define C_MATRIX_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <C_vector.h>
#include <cmath>
#include <float.h>
#include <cstring>
#include <vector>
const double epsilon=0.0000000001;
#ifndef ABS
#define ABS(a) ((a) >= 0 ? (a) : -(a))
#endif
#ifndef SQR
#define SQR(a) ((a)*(a))
#endif
#ifndef SIGN
#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
#endif
#ifndef SIGN_
#define SIGN_(a) ((a) >= 0.0 ? 1 : -1)
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a) >= (b) ? (b) : (a))
#endif
#ifndef MINMOD
#define MINMOD(a,b) (0.5*(SIGN_(a)+SIGN_(b))*MIN(ABS(a),ABS(b)))
#endif
#ifndef Pi
#define Pi 3.141592653589793238462643383279502884197169399375105820974944592L
#endif
#define SWAP(x,y) do \
{ unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
memcpy(swap_temp,&y,sizeof(x)); \
memcpy(&y,&x, sizeof(x)); \
memcpy(&x,swap_temp,sizeof(x)); \
} while(0)
#define NEAREST 0
#define LINEAR 1
#define SMALL_NUM_F 1e-37
#define SMALL_NUM_D 1e-307
//this is a template, meaning that you need to specify the type when you instanciate a object.
template<class dataType> class C_matrix
{
public:
//constructors and destructors
//default ctor: create empty matrix (use resize to set a new size and allocate the image container)
C_matrix();
//ctor: create mtrix of size _L rows, _C columns
C_matrix(int _L, int _C);
//copy ctor
C_matrix(const C_matrix &M);
//ctor: create a matrix copying _M
C_matrix(dataType **_M, int _L, int _C);
//dtor: free memory
virtual ~C_matrix();
//IO methods
dataType& operator()(const int l, const int c);
const dataType& operator()(const int l, const int c) const;
dataType& operator()(const long l, const long c);
const dataType& operator()(const long l, const long c) const;
dataType& operator()(const short l, const short c);
const dataType& operator()(const short l, const short c) const;
dataType& operator()(const unsigned int l, const unsigned int c);
const dataType& operator()(const unsigned int l, const unsigned int c) const;
dataType& operator()(const unsigned short l, const unsigned short c);
const dataType& operator()(const unsigned short l, const unsigned short c) const;
dataType& operator()(const unsigned long l, const unsigned long c);
const dataType& operator()(const unsigned long l, const unsigned long c) const;
dataType& operator()(const float l, const float c);
const dataType& operator()(const float l, const float c, unsigned short INTERPOLATION=NEAREST) const;
dataType& operator()(const double l, const double c);
const dataType& operator()(const double l, const double c, unsigned short INTERPOLATION=NEAREST) const;
//overload operators
C_matrix<dataType> operator= (C_matrix const& c);
C_matrix<dataType> operator= (dataType const& x);
C_matrix<dataType> operator+ (C_matrix const& c);
C_matrix<dataType> operator+ (dataType const& x);
C_matrix<dataType> operator- (C_matrix const& c);
C_matrix<dataType> operator- (dataType const& x);
C_matrix<dataType> operator* (C_matrix const& c);
//C_vector<dataType>& operator* (C_vector<dataType> const& c); //obselete
C_matrix operator* (const dataType& x);
C_matrix<dataType> operator== (C_matrix const& c);
C_matrix<dataType> operator== (dataType const& x);
C_matrix<dataType> operator> (C_matrix const& c);
C_matrix<dataType> operator> (dataType const& x);
C_matrix<dataType> operator>= (C_matrix const& c);
C_matrix<dataType> operator>= (dataType const& x);
C_matrix<dataType> operator< (C_matrix const& c);
C_matrix<dataType> operator< (dataType const& x);
C_matrix<dataType> operator<= (C_matrix const& c);
C_matrix<dataType> operator<= (dataType const& x);
//offset
int offsetL, offsetC;
//cast operator (later)
//operator int();
//usual operations
C_matrix<dataType> m_abs(void); //must be multithreaded
dataType maxVal(void);
dataType minVal(void);
double sum(void);
double mean(void);
double var(void);
C_matrix<dataType> SQRT(void); //must be multithreaded
C_matrix<dataType> dotPower(double alpha);//octave .^ //must be multithreaded
C_matrix<dataType> dotExp(double alpha=1.0); //must be multithreaded
C_matrix<dataType> dotLog(double alpha=1.0); //must be multithreaded
C_matrix<dataType> dotProduct(C_matrix const& B);//octave .* //must be multithreaded
C_matrix<dataType> dotDiv(C_matrix const& B);//octave ./ //must be multithreaded
C_matrix<dataType> dotProduct(C_matrix& B); //must be multithreaded
C_matrix<dataType> dotDiv(C_matrix& B); //must be multithreaded
C_matrix<dataType> subset(int lbegin, int lend, int cbegin, int cend);
void subset(C_matrix<dataType> M, int lbegin, int lend, int cbegin, int cend);
//distance map
C_matrix<double> bwdistEuclidean(void); //Algorithme de Danielson
//convolution //must be multithreaded
C_matrix<dataType> conv2(C_matrix<dataType> const& h);
C_matrix<dataType> conv2(C_matrix<dataType>& h);
C_matrix<dataType> gradX(void);
C_matrix<dataType> gradY(void);
//random generator
void random(void);
void randomf(void);
void randomGauss(std::vector<double> mu, std::vector<double> sigma);
void randomGauss(double mu, double sigma);
//mesh
void meshRow(dataType minValue, dataType maxValue);
void meshColumn(dataType minValue, dataType maxValue);
//linespace (later)
//matrix inversion
C_matrix<dataType> inv(void);
C_matrix<dataType> pseudoInv(void);
//fft (later)
//tools for linear system solving
//LU
C_matrix<dataType> Transpose(void);
C_matrix<dataType> LU(void);
C_matrix<dataType> LUP(C_vector<int> &Indice);
C_matrix<dataType> LMU(void);
C_vector<dataType> LineAlgEq_LU(C_vector<dataType> &B);
//svd
std::vector<C_matrix<dataType> > svd(void);
//diagonalisation: sym sys
std::vector<C_matrix<dataType> > eigSym(bool yesvec);
protected:
void tred2(C_matrix<dataType>* z, C_matrix<dataType>* d, C_matrix<dataType>* e, bool yesvecs);
void tqli(C_matrix<dataType> *z, C_matrix<dataType> *d, C_matrix<dataType> *e, bool yesvecs);
void eigsrt(C_matrix<dataType> *d, C_matrix<dataType> *z);
void sortEig(C_matrix<dataType> *d, C_matrix<dataType> *z, bool yesvecs);
public:
//other tools
int getNbRow(void)const {return m_L;}
int getNbColumn(void)const {return m_C;}
int numel(void)const{return m_L*m_C;}
void show(void);
void save(std::string fileName);
int getIndex(int l, int c){return l*m_C+c;}
int getRow(int idx){return floor(idx/m_C);}//floor();
int getColumn(int idx){return idx - (floor(idx/m_C)*m_C);}// return idx%m_C;
//to use carefully
bool resize(int newL, int newC);
int endL;
int endC;
protected:
int m_L;
int m_C;
dataType* m_A;
//privates tools to allocate and free memory
dataType *allocate(int M, int N);
void deallocation(dataType* B, int M, int N);
double pythag(const double a, const double b);
};
template<class dataType> C_matrix<dataType>::C_matrix()
{
m_L=0;
m_C=0;
m_A=NULL;
endL = m_L-1;
endC = m_C-1;
}
template<class dataType> C_matrix<dataType>::C_matrix(int _L, int _C) : m_L(_L),m_C(_C)
{
m_A = allocate(m_L,m_C);
if(m_A==NULL)
{
m_L=0;
m_C=0;
}
endL = m_L-1;
endC = m_C-1;
*this = 0.0;
}
template<class dataType> C_matrix<dataType>::C_matrix(dataType **_M, int _L, int _C) : m_L(_L),m_C(_C)
{
m_A = allocate(m_L, m_C);
if(m_A==NULL)
{
m_L=0;
m_C=0;
}
else
{
for(int i=0 ; i<m_L ; i++)
{
for(int j=0 ; j<m_C ; j++)
m_A[getIndex(i,j)] = _M[i][j];
}
}
endL = m_L-1;
endC = m_C-1;
}
template<class dataType> C_matrix<dataType>::C_matrix(const C_matrix &X)
{
m_L = X.getNbRow();
m_C = X.getNbColumn();
m_A = allocate(m_L, m_C);
if(m_A==NULL)
{
m_L=0;
m_C=0;
}
else
{
for(int i=0 ; i<m_L ; i++)
{
for(int j=0 ; j<m_C ; j++)
{
m_A[getIndex(i,j)] = X(i,j);
}
}
}
endL = m_L-1;
endC = m_C-1;
offsetL = X.offsetL;
offsetC = X.offsetC;
}
template<class dataType> C_matrix<dataType>::~C_matrix()
{
deallocation(m_A,m_L,m_C);
}
//to use carefully
template<class dataType> bool C_matrix<dataType>::resize(int newL, int newC)
{
deallocation(m_A,m_L,m_C);
m_A = NULL;
m_L = 0;
m_C = 0;
m_A = allocate(newL, newC);
if(m_A==NULL) return false;
m_L = newL;
m_C = newC;
endL = m_L-1;
endC = m_C-1;
*this = 0.0;
return true;
}
template<class dataType> dataType* C_matrix<dataType>::allocate(int M, int N)
{
return new dataType[M*N];
// dataType* A = new dataType[M*N];
// return A
// if(A==NULL) return A;
// for(int i=0 ; i<M ; i++)
// A[i] = new dataType[N];
// //should check if all allocation is OK
// bool cond=true;
// for(int i=0 ; i<M ; i++)
// {
// if(A[i]==NULL)
// {
// cond=false;
// i=M;
// }
// }
// if(!cond)
// {
// deallocation(A,M,N);
// }
// return A;
}
template<class dataType> void C_matrix<dataType>::deallocation(dataType *B, int M, int N)
{
if(B!=NULL)
{
// for(int i=0 ; i<M ; i++)
// {
// if(B[i]!=NULL)
// {
// delete [] B[i];
// }
// }
delete [] B;
B=NULL;
}
return;
}
template<class dataType> void C_matrix<dataType>::show(void)
{
std::cout << "--------------------------------------------------------" << std::endl;
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
std::cout << m_A[getIndex(i,j)] << "\t";
}
std::cout << std::endl;
}
std::cout << "--------------------------------------------------------" << std::endl;
std::cout << std::endl;
}
template<class dataType> void C_matrix<dataType>::save(std::string fileName)
{
std::ofstream myfileX;
myfileX.open (fileName.data(), std::ios::out );
if(myfileX.is_open())
{
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
myfileX << m_A[getIndex(i,j)] << "\t";
}
myfileX << std::endl;
}
myfileX.close();
}
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator= (C_matrix const& c)
{
if(this==&c)return *this;
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow())
{
this->resize(c.getNbRow(),c.getNbColumn());
//throw "dimension matrix must agree";
}
for(unsigned short i=0 ; i<this->m_L ; i++)
{
for(unsigned short j=0 ; j<this->m_C ; j++)
{
this->m_A[getIndex(i,j)] = c(i,j);
}
}
return *this;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator= (dataType const& x)
{
for(unsigned short i=0 ; i<this->m_L ; i++)
{
for(unsigned short j=0 ; j<this->m_C ; j++)
{
m_A[getIndex(i,j)] = x;
}
}
return *this;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator+ (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
B(i,j) = m_A[getIndex(i,j)] + c(i,j);
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator+ (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<this->m_L ; i++)
{
for(unsigned short j=0 ; j<this->m_C ; j++)
{
B(i,j) = m_A[getIndex(i,j)] + x;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator- (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);// = new C_matrix(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
B(i,j) = m_A[getIndex(i,j)] - c(i,j);
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator- (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);// = new C_matrix
for(unsigned short i=0 ; i<this->m_L ; i++)
{
for(unsigned short j=0 ; j<this->m_C ; j++)
{
B(i,j) = m_A[getIndex(i,j)] - x;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator* (C_matrix const& c)
{
if(m_C!=c.getNbRow()) throw "mismatch dimension matrix";
C_matrix B(m_L,c.getNbColumn());
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<c.getNbColumn() ; j++)
{
dataType S = (dataType) 0;
for(unsigned short k=0 ; k<m_C ; k++)
{
S += m_A[getIndex(i,j)]*c(k,j);
}
B(i,j) = S;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator* (const dataType& x)
{
C_matrix B(m_L,m_C);
for(unsigned short i=0 ; i<this->m_L ; i++)
{
for(unsigned short j=0 ; j<this->m_C ; j++)
{
B(i,j) = m_A[getIndex(i,j)]*x;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator== (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]==c(i,j)) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator== (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]==x) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator> (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]>c(i,j)) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator> (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]>x) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator>= (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]>=c(i,j)) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator>= (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]>=x) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator< (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]<c(i,j)) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator< (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]<x) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator<= (C_matrix const& c)
{
if(c.getNbColumn()!=this->getNbColumn() || c.getNbRow()!=this->getNbRow()) throw "dimension matrix must agree";
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]<=c(i,j)) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::operator<= (dataType const& x)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(m_A[getIndex(i,j)]<=x) B(i,j)=1.0;
else B(i,j) = 0.0;
}
}
return B;
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const int l, const int c)
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const int l, const int c) const
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[l*m_C+l];//TODO explain why getIndex(l,c) generates an error
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const long l, const long c)
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const long l, const long c) const
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const short l, const short c)
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const short l, const short c) const
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const unsigned int l, const unsigned int c)
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const unsigned int l, const unsigned int c) const
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const unsigned short l, const unsigned short c)
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const unsigned short l, const unsigned short c) const
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[l*m_C+c];//TODO explain why getIndex(l,c) generates an error
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const unsigned long l, const unsigned long c)
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const unsigned long l, const unsigned long c) const
{
if( l>= this->m_L || c>= this->m_C )
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
return m_A[getIndex(l,c)];
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const float l, const float c)
{
if( (l<0.0) || (l>=(float) (this->m_L)) || (c<0.0) || (c>= (float) (this->m_C)))
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
unsigned int ll = (unsigned int) floor((double) l);
unsigned int cc = (unsigned int) floor((double) c);
return m_A[getIndex(ll,cc)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const float l, const float c, unsigned short INTERPOLATION) const
{
if( (l<0.0) || (l>=(float) (this->m_L)) || (c<0.0) || (c>= (float) (this->m_C)))
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
if(INTERPOLATION==NEAREST)
{
unsigned int ll = (unsigned int) floor((double) l);
unsigned int cc = (unsigned int) floor((double) c);
return m_A[getIndex(ll,cc)];
}
else
{
//not yet available
unsigned int ll = (unsigned int) floor((double) l);
unsigned int cc = (unsigned int) floor((double) c);
//return m_A[ll][cc];
if(l<SMALL_NUM_F && c<SMALL_NUM_F)
{
return m_A[getIndex(ll,cc)];
}
if(l<SMALL_NUM_F)//column interpolation
{
unsigned int cc1 = cc+1;
return (dataType) (((double)m_A[getIndex(ll,cc)])*( ((double)cc1) - ((double)c) ) + ((double)m_A[getIndex(ll,cc1)])*( ((double)c) - ((double)cc) ));
}
if(c<SMALL_NUM_F)//row interpolation
{
unsigned int ll1 = ll+1;
return (dataType) (((double)m_A[getIndex(ll,cc)])*( ((double)ll1) - ((double)l) ) + ((double)m_A[getIndex(ll1,cc)])*( ((double)l) - ((double)ll) ));
}
//main case: bilinear interpolation
unsigned int ll1 = ll+1;
unsigned int cc1 = cc+1;
double valuell = (((double)m_A[getIndex(ll,cc)])*( ((double)cc1) - ((double)c) ) + ((double)m_A[getIndex(ll,cc1)])*( ((double)c) - ((double)cc) ));
double valuell1 = (((double)m_A[getIndex(ll1,cc)])*( ((double)cc1) - ((double)c) ) + ((double)m_A[getIndex(ll1,cc1)])*( ((double)c) - ((double)cc) ));
return (dataType) (valuell*( ((double)ll1) - ((double)l) ) + valuell1*( ((double)l) - ((double)ll) ));
}
}
template<class dataType> dataType& C_matrix<dataType>::operator()(const double l, const double c)
{
if( (l<0.0) || (l>=(double) (this->m_L)) || (c<0.0) || (c>= (double) (this->m_C)))
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
unsigned int ll = (unsigned int) floor(l);
unsigned int cc = (unsigned int) floor(c);
return m_A[getIndex(ll,cc)];
}
template<class dataType> const dataType& C_matrix<dataType>::operator()(const double l, const double c, unsigned short INTERPOLATION) const
{
if( (l<0.0) || (l>=(double) (this->m_L)) || (c<0.0) || (c>= (double) (this->m_C)))
{
throw "index must be larger than 0 and smaller than the matrix dimensions";
}
if(INTERPOLATION==NEAREST)
{
unsigned int ll = (unsigned int) floor(l);
unsigned int cc = (unsigned int) floor(c);
return m_A[getIndex(ll,cc)];
}
else
{
//not yet available
unsigned int ll = (unsigned int) floor(l);
unsigned int cc = (unsigned int) floor(c);
//return m_A[ll][cc];
if(l<SMALL_NUM_F && c<SMALL_NUM_F)
{
return m_A[getIndex(ll,cc)];
}
if(l<SMALL_NUM_F)//column interpolation
{
unsigned int cc1 = cc+1;
return (dataType) (((double)m_A[getIndex(ll,cc)])*( ((double)cc1) - c ) + ((double)m_A[getIndex(ll,cc1)])*( c - ((double)cc) ));
}
if(c<SMALL_NUM_F)//row interpolation
{
unsigned int ll1 = ll+1;
return (dataType) (((double)m_A[getIndex(ll,cc)])*( ((double)ll1) - l ) + ((double)m_A[getIndex(ll1,cc)])*( l - ((double)ll) ));
}
//main case: bilinear interpolation
unsigned int ll1 = ll+1;
unsigned int cc1 = cc+1;
double valuell = (((double)m_A[getIndex(ll,cc)])*( ((double)cc1) - c ) + ((double)m_A[getIndex(ll,cc1)])*( c - ((double)cc) ));
double valuell1 = (((double)m_A[getIndex(ll1,cc)])*( ((double)cc1) - c ) + ((double)m_A[getIndex(ll1,cc1)])*( c - ((double)cc) ));
return (dataType) (valuell*( ((double)ll1) - l ) + valuell1*( l - ((double)ll) ));
}
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::subset(int lbegin, int lend, int cbegin, int cend)
{
if(lend<lbegin || cend<cbegin) throw "end must be larger than beginning";
if(lbegin<0 || cbegin<0) throw "begining must be larger than 0";
if(lend>this->endL || cend>this->endC) throw "end must be smaller size-1";
C_matrix<dataType> B(lend-lbegin+1,cend-cbegin+1);
for(int l=0 ; l<lend-lbegin+1 ; l++)
{
for(int c=0 ; c<cend-cbegin+1 ; c++)
{
B(l,c) = m_A[getIndex(lbegin+l,cbegin+c)];
}
}
return B;
}
template<class dataType> void C_matrix<dataType>::subset(C_matrix<dataType> M, int lbegin, int lend, int cbegin, int cend)
{
if(lend<lbegin || cend<cbegin) throw "end must be larger than beginning";
if(lbegin<0 || cbegin<0) throw "begining must be larger than 0";
if(lend>this->endL || cend>this->endC) throw "end must be smaller size-1 k";
if(M.getNbRow()!=(lend-lbegin+1) || M.getNbColumn()!=(cend-cbegin+1)) throw "dimension matrix must agree";
for(int l=0 ; l<M.getNbRow() ; l++)
{
for(int c=0 ; c<M.getNbColumn() ; c++)
{
m_A[getIndex(lbegin+l,cbegin+c)] = M(l,c);
}
}
return;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::m_abs(void)
{
C_matrix<dataType> B(this->m_L,this->m_C);// = new C_matrix
for(unsigned short i=0 ; i<this->m_L ; i++)
{
for(unsigned short j=0 ; j<this->m_C ; j++)
{
B(i,j) = ABS(m_A[i][j]);
}
}
return B;
}
template<class dataType> C_matrix<dataType> C_matrix<dataType>::SQRT(void)
{
C_matrix<dataType> B(m_L,m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
B(i,j) = sqrt(m_A[getIndex(i,j)]);
}
}
return B;
}
template<class dataType> C_matrix<double> C_matrix<dataType>::bwdistEuclidean(void)
{
C_matrix<double> B(m_L,m_C), B2(m_L,m_C), B3(m_L,m_C), B4(m_L,m_C);
C_matrix<double> dL(m_L,m_C), dC(m_L,m_C), dL2(m_L,m_C), dC2(m_L,m_C), dL3(m_L,m_C), dC3(m_L,m_C), dL4(m_L,m_C), dC4(m_L,m_C);
double BIGNUM = ((double)m_L)*((double)m_C);
for(unsigned short i=0 ; i<m_L ; i++)
{
for(unsigned short j=0 ; j<m_C ; j++)
{
if(ABS(m_A[i][j])<SMALL_NUM_F)//if not in the ocean
{
B(i,j) = 0.0; B2(i,j) = 0.0; B3(i,j) = 0.0; B4(i,j) = 0.0;
dL(i,j) = 0.0; dL2(i,j) = 0.0; dL3(i,j) = 0.0; dL4(i,j) = 0.0;
dC(i,j) = 0.0; dC2(i,j) = 0.0; dC3(i,j) = 0.0; dC4(i,j) = 0.0;
}
else
{
B(i,j) = BIGNUM; B2(i,j) = BIGNUM; B3(i,j) = BIGNUM; B4(i,j) = BIGNUM;
dL(i,j) = BIGNUM; dL2(i,j) = BIGNUM; dL3(i,j) = BIGNUM; dL4(i,j) = BIGNUM;
dC(i,j) = BIGNUM; dC2(i,j) = BIGNUM; dC3(i,j) = BIGNUM; dC4(i,j) = BIGNUM;
}
}
}
double v, vi, vj;
bool firstIslandMet=false;