Allink  v0.1
MatematicaFilter.cpp
1 #include "../include/Matematica.h"
2 
3 int Matematica::PermuteRandomAll(PERMUTE *Perm,int NMass){
4  int *Sequence = (int *)calloc(NMass,sizeof(int));
5  for(int n=0;n<NMass;n++){
6  int nSquare = n;
7  int Ran = (int)(NMass*Casuale());
8  int IfGood=0;
9  int IfAlready=1;
10  for(int gg= nSquare-1;gg>=0;gg--){
11  //printf("%d %d) %d\n",nSquare,gg,Sequence[gg]);
12  if(nSquare == Sequence[gg]){
13  IfAlready = 0;
14  break;
15  }
16  }
17  if(!IfAlready) continue;
18  while(Ran==nSquare || !IfGood){// Ranw != Squarew && Ranh != Squareh){
19  IfGood = 1;
20  Ran = (int)(NMass*Casuale());
21  for(int gg= nSquare-1;gg>=0;gg--){
22  //printf("%d %d) %d %d\n",nSquare,gg,Ran,Sequence[gg]);
23  if(Ran == Sequence[gg]){
24  IfGood = 0;
25  break;
26  }
27  }
28  }
29  Sequence[nSquare] = Ran;
30  Sequence[Ran] = nSquare;
31  // for(int g=0;g<NGrid*NGrid;g++)printf("%d %d\n",g,Sequence[g]);
32  }
33  for(int n=0;n<NMass;n++){
34  Perm[n].n = n;
35  Perm[n].m = Sequence[n];
36  }
37  int nTemp = 0;
38  for(int n=0;n<NMass;n++){
39  if(Perm[n-nTemp].m < n ){
40  for(int nn=n-nTemp;nn<NMass-1-nTemp;nn++){
41  Perm[nn].n = nn+1+nTemp;
42  Perm[nn].m = Sequence[nn+1+nTemp];
43  }
44  nTemp++;
45  }
46  }
47  //for(int n=0;n<NMass;n++)printf("%d %d - %d %d \n",Perm[n].n,Perm[n].m,n,Sequence[n]);
48  return 1;
49 }
50 int Matematica::PermuteRandomAll(int *Sequence,int NMass){
51  for(int n=0;n<NMass;n++){
52  int nSquare = n;
53  int Ran = (int)(NMass*Casuale());
54  int IfGood=0;
55  int IfAlready=1;
56  for(int gg= nSquare-1;gg>=0;gg--){
57  //printf("%d %d) %d\n",nSquare,gg,Sequence[gg]);
58  if(nSquare == Sequence[gg]){
59  IfAlready = 0;
60  break;
61  }
62  }
63  if(!IfAlready) continue;
64  while(Ran==nSquare || !IfGood){// Ranw != Squarew && Ranh != Squareh){
65  IfGood = 1;
66  Ran = (int)(NMass*Casuale());
67  for(int gg= nSquare-1;gg>=0;gg--){
68  //printf("%d %d) %d %d\n",nSquare,gg,Ran,Sequence[gg]);
69  if(Ran == Sequence[gg]){
70  IfGood = 0;
71  break;
72  }
73  }
74  }
75  Sequence[nSquare] = Ran;
76  Sequence[Ran] = nSquare;
77  //if(Sequence[nSquare] < nSquare) Sequence[nSquare] = -1;
78  // for(int g=0;g<NGrid*NGrid;g++)printf("%d %d\n",g,Sequence[g]);
79  }
80  return 1;
81 }
83  if(Point->Size() != Res->Size()){
84  printf("Matrices differ! %d %d \n",Point->Size(),Res->Size());
85  return 0;
86  }
87  int NMaskh = Mask->Size();
88  int NMaskw = Mask->Size();
89  int height = Point->Size();
90  int width = Point->Size();
91  int NHalf = Mask->Size()/2;
92  for(int h=0;h<height; h++) {
93  for(int w=0;w<width; w++) {
94  //Res->setvalue(h,w,0.);
95  for(int lh=0;lh<NMaskh;lh++){
96  for(int lw=0;lw<NMaskw;lw++){
97  if(h+lh-NHalf <0 ) continue;
98  if(w+lw-NHalf <0 ) continue;
99  if(h+lh-NHalf > height-1) continue;
100  if(w+lw-NHalf > width-1) continue;
101  Res->Add(h,w,Mask->Val(lh,lw)*Point->Val(h+lh-NHalf,w+lw-NHalf));
102  }
103  }
104  }
105  }
106  return 1;
107 }
109  int NMaskh = Mask->pNRow();
110  int NMaskw = Mask->pNCol();
111  // double Temp[NMaskh][NMaskw];
112  int width = Res->pNRow();
113  int height = Res->pNCol();
114  Matrice *Temp = new Matrice(width,height);
115  Res->CopyOn(Temp);
116  //int NHalf = Mask->Size()/2;
117  int NHalf = (int)Mask->pNCol()/2;
118  for(int h=0;h<height; h++) {
119  for(int w=0;w<width; w++) {
120  //Res->setvalue(h,w,0.);
121  double dTemp=0.;
122  for(int lh=0;lh<NMaskh;lh++){
123  int l1h = h + lh - NHalf;
124  if(l1h >= height) continue;//g1x -= NGrid;
125  if(l1h < 0) continue;//g1x + NGrid;
126  for(int lw=0;lw<NMaskw;lw++){
127  int l1w = w + lw - NHalf;
128  if(l1w >= width) continue;//g1x -= NGrid;
129  if(l1w < 0) continue;//g1x + NGrid;
130  dTemp += Mask->Val(lw,lh)*Temp->Val(l1w,l1h);
131  }
132  }
133  Res->Set(w,h,dTemp);
134  }
135  }
136  delete Temp;
137  return 0;
138 }
139 int Matematica::Transform(int *Out,int *In,int NEdge,int operation){
140  int NMin = NEdge-1;
141  if( OP_IF(operation,OP_INVERT) ){
142  for(int r=0;r<NEdge;r++)
143  for(int c=0;c<NEdge;c++)
144  Out[r*NEdge+c] = In[r*NEdge+c] == 1 ? 0 : 1;
145  }
146  if( OP_IF(operation,OP_ROT_90) ){
147  for(int r=0;r<NEdge;r++)
148  for(int c=0;c<NEdge;c++)
149  Out[r*NEdge+c] = In[(NMin-c)*NEdge+r];
150  }
151  if( OP_IF(operation,OP_ROT_180) ){
152  for(int r=0;r<NEdge;r++)
153  for(int c=0;c<NEdge;c++)
154  Out[r*NEdge+c] = In[(NMin-r)*NEdge+(NMin-c)];
155  }
156  if( OP_IF(operation,OP_ROT_270) ){
157  for(int r=0;r<NEdge;r++)
158  for(int c=0;c<NEdge;c++)
159  Out[r*NEdge+c] = In[c*NEdge+(NMin-r)];
160  }
161  if( OP_IF(operation,OP_MIRROR) ){
162  for(int r=0;r<NEdge;r++)
163  for(int c=0;c<NEdge;c++)
164  Out[r*NEdge+c] = In[r*NEdge+(NMin-c)];
165  }
166  if( OP_IF(operation,OP_TRANSPOSE) ){
167  for(int r=0;r<NEdge;r++)
168  for(int c=0;c<NEdge;c++)
169  Out[r*NEdge+c] = In[c*NEdge+r];
170  }
171  return 1;
172 }
173 void Matematica::BackFold(Matrice *In,Matrice *Out,int NShift){
174  for(int r=0;r<In->pNRow();r++){
175  int r1 = r + NShift;
176  if(r1 >= In->pNRow()) r1 -= In->pNRow();
177  if(r1 < 0) r1 += In->pNRow();
178  for(int c=0;c<In->pNCol();c++){
179  Out->Set(r1,c,In->Val(r,c));
180  }
181  }
182 }
int pNCol()
Number of columns.
int m
Second number of the couple.
void BackFold(Matrice *In, Matrice *Out, int NShift)
Shift all the rows upwards/downwards.
int Transform(int *Out, int *In, int NEdge, int operation)
Boh.
void CopyOn(Matrice *B)
Copy on a matrix.
Indices for the permutation.
int PermuteRandomAll(int *Sequence, int NMass)
Permutes a sequence without repeating.
int n
First number of the couple.
double Casuale()
Random uniform number.
double Val(int row)
Returns a value in 1d.
bool Add(int row, int col, double Val)
Add the value of the coefficient to the previous one.
Matrice computes the algebric operations on matrices.
bool Set(int row, int column, double Val)
Set a coefficient.
int Size()
Size of Matrice.
int ApplyFilter(Matrice *Point, Matrice *Res, Matrice *Mask)
Applies the filter.
int pNRow()
Number of rows.