-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcg-indicators.cpp
More file actions
270 lines (190 loc) · 6.32 KB
/
cg-indicators.cpp
File metadata and controls
270 lines (190 loc) · 6.32 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
//-------------This code was written by Sabiha Majumder in 2016--------------------------//
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
const int na = -9999;
const int range = 181; //No. of driver values in the transect
const int N = 128; //Dimention of each matrix to calculate the indicators
//-------------------------initialize the matrix-----------------------//
//This function makes a random initial matrix with a given density x
void create_random_matrix (int* A, float x) {
int i,j;
for (i=0;i<N;i++) {
for (j=0;j<N;j++){
float number = rand() / (float)RAND_MAX;
if (number < x) A[i*N+j] =1;
else A[i*N+j] = 0;
}
}
}
//-----------------------mean cover-----------------------------------//
//This function calcualtes the mean for a given matrix
double density (double* A, int size) {
int i,j;
double count=0,sum=0;
for (i=0;i<size;i++) {
for (j=0;j<size;j++){
if (A[i*size+j] != na) {
sum += A[i*size+j];
count += 1;
}
}
}
double average = sum/count;
//cout<<"mean="<<average<<endl;
return average;
}
//-----------------------coarse-graining-------------------------------//
//This function takes a discrete matrix with integers and coarse-grain it with nxn
void coarse_grain (int* A, double* A_cg, int n, int cgLength) { // n is the size of coarse-grained matrix
int i,j,k,l;
for ( i=0; i<n; i++) {
for ( j=0; j<n; j++) {
int init_row = i*cgLength;
int end_row = (i+1)*cgLength-1;
int init_col = j*cgLength;
int end_col = (j+1)*cgLength-1;
double sum = 0, count=0;
for ( k=init_row ; k<end_row+1; k++) {
for ( l=init_col; l<end_col+1; l++) {
if (A[k*N+l]==0 || A[k*N+l]==1) {
sum += A[k*N+l];
count += 1;
}
}
}
double reduced_mean = sum/count;
A_cg[i*n+j] = reduced_mean;
}
}
}
//---------------------------------varaince--------------------------------------//
// This function takes the coarse-geainined matrix and calulates spatial variance
double spVar(double* A, int size, double mean) {
double sum = 0,count = 0;
for (int i=0; i< size; i++) {
for (int j=0; j<size; j++) {
if (A[i*size+j]!=na) {
sum = sum + A[i*size+j]*A[i*size+j];
count +=1;
}
}
}
double var = sum/count - mean*mean;
//cout<<"var="<<var<<endl;
return var;
}
//--------------------------------skewness----------------------------------------------//
// This function takes the coarse-geainined matrix and calulates spatial skewness
double cg_skew (double* A, int n, double mean, double variance) { //n is the dimension of coarse graining i.e. dimension of the submatrix
int i,j,k,l,count = 0;
double skewness;
double sum_cube=0;
for (i=0; i<n*n; i++) {
sum_cube+= A[i]*A[i]*A[i];
}
double mean_cube = sum_cube/(float)(n*n);
if (variance == 0) skewness = 0;
else skewness = ( mean_cube - 3.0*mean*variance - mean*mean*mean )/ pow(variance,1.5) ;
return skewness;
}
//-------------------------------Correlation at lag1---------------------------------//
//// This function takes the coarse-geainined matrix and calulates spatial correlation at lag-1
double spCor (double* A ,int size,double mean, double variance){
double sum=0;
int i,j;
int down, right;
for (i=0; i< size; i++) {
for (j=0; j< size; j++) {
down = (i+1)%size;
right = (j+1)%size;
sum = sum + (A[i*size+j] * A[down*size+j]) + (A[i*size+j]* A[i*size+right]) ;
}
}
double corLag1;
if (variance==0) corLag1 = 0;
else corLag1= (sum/(double)(2*size*size)-mean*mean)/variance;
return corLag1;
}
//--------------------------------------int to string converter----------------------------//
string IntToStr(int n){
stringstream result;
result << n;
return result.str();
}
//----------------------------------main function-----------------------------//
int main() {
srand (time(NULL));
float initial_density;
double mean, correlation, variance,skewness;
int* mat = new int [N*N];
int*full = new int[N*N*range];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~reading data from a file~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//This part reads the data from a file and saves it in the array "full"
ifstream myfile;
myfile.open ("filename2.txt");
if (myfile.is_open()){
for (int x=0; x<range; x++){
for (int i=0; i<N; i++){
for (int j=0 ; j<N; j++) {
myfile >> full[x*N*N+i*N+j];
}
}
}
}
else cout << "Unable to open file";
myfile.close();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//Loop over different cg lengths
for (int cgLength=4;cgLength<5;cgLength++) { // choose coarse-graining length
//~~~~~~~~~~~~~ declare cg matrix~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int rem;
if (N%cgLength ==0) rem = 0;
else rem = N%cgLength ;
int L = N-rem;
int n = L/cgLength;
double* mat_cg = new double[n*n];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Loop over different 'p' values
for (int x=0; x<range; x++){
for (int i=0; i<N; i++){
for (int j=0 ; j<N; j++) {
mat[i*N+j] = full[x*N*N+i*N+j];
}
}
coarse_grain (mat,mat_cg,n,cgLength);
mean = density(mat_cg,n);
variance = spVar(mat_cg,n,mean);
correlation = spCor (mat_cg,n,mean,variance);
skewness = cg_skew (mat_cg,n,mean,variance);
//~~~~~~~~~~~~~~~~~~~~~~~ save the correlation and variance~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
string filename1="variance_Cg" + IntToStr(cgLength)+ ".txt";
string filename2="correaltion_Cg" + IntToStr(cgLength) + ".txt";
string filename3="skewness_Cg" + IntToStr(cgLength) + ".txt";
ofstream outdata1;
ofstream outdata2;
ofstream outdata3;
outdata1.open(filename1.c_str(),ios::app);
outdata2.open(filename2.c_str(),ios::app);
outdata3.open(filename3.c_str(),ios::app);
if ( outdata1.is_open()){
outdata1<< variance<< endl;
outdata2<<correlation<<endl;
outdata3<<skewness<<endl;
outdata1.close();
outdata2.close();
outdata3.close();
}
else cout<< "oudata not open";
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
}
delete [] mat_cg;
}
delete [] mat;
delete [] full;
return 0;
}