-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfractal.R
More file actions
54 lines (38 loc) · 1.24 KB
/
fractal.R
File metadata and controls
54 lines (38 loc) · 1.24 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
#### Script for coumputing Hausdorf fractal dimension of opinion space
## Encoding: windows-1250
## Created: 2022-01-14 Francesco
## Edited: 2022-01-14 Francesco
## Adapted from: https://stackoverflow.com/questions/58417161/box-counting-in-r
# create a dataset to calculate de Hausdorff-Besicovitch dimension
mat <- matrix(runif(512*512),nrow = 512,ncol = 512)
mat[mat<=0.3] <- 0
mat[mat>0.3] <- 1
cant <- sum(mat)
fragment <- rep(2,10)**(0:9)
Table <- data.frame(Delta = rep(512,10)/(fragment ), N = fragment**2)
Table$LogDelta <- log(Table$Delta)
for(i in 2:10){
delta_aux <- Table$Delta[i]
for(j in 1:fragment [i]){
row_id <- ((j-1)*delta_aux+1):(j*delta_aux)
for(k in 1:fragment [i]){
col_id <- ((k-1)*delta_aux+1):(k*delta_aux)
if(sum(mat[row_id,col_id]) == 0){
Table$N[i] <- Table$N[i] - 1
}
}
}
}
Table$LogN <- log(Table$N)
lm_dim <- lm(Table$LogN ~ Table$LogDelta)
plot(Table$LogN ~ Table$LogDelta)
abline(lm_dim)
print('The box-counting dimension is:')
print(-lm_dim$coefficients[2])
# without the borders
Table <- Table[2:nrow(Table),]
lm_dim <- lm(Table$LogN ~ Table$LogDelta)
plot(Table$LogN ~ Table$LogDelta)
abline(lm_dim)
print('The box-counting dimension is:')
print(-lm_dim$coefficients[2])