| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include <math.h> |
| 6 |
|
| #include "FastSOM.h" |
| 7 |
|
| #include "proto.h" |
| 8 |
|
| |
| 9 |
|
| /* |
| 10 |
|
| * generic funtions |
| 11 |
|
| * |
| 12 |
|
| * NOT superclass functions, but usable by all |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
79 |
| NV _vector_distance(AV* V1, AV* V2) { |
| 16 |
|
| NV diff,sum; |
| 17 |
|
| I32 w_ptr; |
| 18 |
|
| |
| 19 |
79 |
| sum = 0; |
| 20 |
316 |
▶ | for ( w_ptr=av_len(V2) ; w_ptr>=0 ; w_ptr-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( w_ptr=av_len(V2) ; w_ptr>=0 ; w_ptr-- ) { |
237 |
79 |
316 |
|
| 21 |
237 |
| diff = SvNV(*av_fetch(V1, w_ptr, FALSE)) |
| 22 |
237 |
| - SvNV(*av_fetch(V2, w_ptr, FALSE)); |
| 23 |
237 |
| sum += diff * diff; |
| 24 |
|
| } |
| 25 |
79 |
| return sqrt(sum); |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
24056 |
| void _bmuguts(SOM_GENERIC *som,AV *sample,IV *bx,IV *by,NV *bd) { |
| 29 |
|
| IV x,y,z,X,Y,Z; |
| 30 |
|
| NV sum,diff,distance; |
| 31 |
|
| SOM_Map *map; |
| 32 |
|
| SOM_Array *array; |
| 33 |
|
| SOM_Vector *vector; |
| 34 |
|
| |
| 35 |
24056 |
| map = som->map; |
| 36 |
24056 |
| X = som->X; |
| 37 |
24056 |
| Y = som->Y; |
| 38 |
24056 |
| Z = som->Z; |
| 39 |
|
| |
| 40 |
24056 |
| *bx = -1; |
| 41 |
24056 |
| *by = 0; |
| 42 |
24056 |
| *bd = 0.0; |
| 43 |
|
| |
| 44 |
141122 |
▶ | for ( x=0 ; x<X ; x++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=0 ; x<X ; x++ ) { |
117066 |
24056 |
141122 |
|
| 45 |
117066 |
| array = (&map->array)[x]; |
| 46 |
805010 |
▶ | for ( y=0 ; y<Y ; y++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=0 ; y<Y ; y++ ) { |
687944 |
117066 |
805010 |
|
| 47 |
687944 |
| vector = (&array->vector)[y]; |
| 48 |
|
| |
| 49 |
687944 |
| sum = 0; |
| 50 |
2751776 |
▶ | for ( z=0 ; z<Z ; z++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( z=0 ; z<Z ; z++ ) { |
2063832 |
687944 |
2751776 |
|
| 51 |
2063832 |
| diff = SvNV(*av_fetch(sample,z,0)) |
| 52 |
2063832 |
| - (&vector->element)[z]; |
| 53 |
2063832 |
| sum += diff * diff; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
687944 |
| distance = sqrt(sum); |
| 57 |
|
| |
| 58 |
687944 |
▶ | if ( *bx < 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( *bx < 0 ) |
24056 |
663888 |
687944 |
|
| 59 |
24056 |
| { *bx = 0; *by = 0; *bd = distance; } |
| 60 |
687944 |
▶ | if ( distance < *bd ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( distance < *bd ) |
90634 |
597310 |
687944 |
|
| 61 |
90634 |
| { *bx = x; *by = y; *bd = distance; } |
| 62 |
|
| } |
| 63 |
|
| } |
| 64 |
24056 |
| } |
| 65 |
|
| |
| 66 |
|
| |
| 67 |
|
| /* http://www.ai-junkie.com/ann/som/som4.html */ |
| 68 |
0 |
| void _adjust(SV* self,NV l,NV sigma,AV* unit,AV* v) { |
| 69 |
|
| IV x,y; |
| 70 |
|
| I32 z,Z; |
| 71 |
|
| NV d,theta,vold,wold; |
| 72 |
|
| MAGIC *mg; |
| 73 |
|
| SOM_Map *map; |
| 74 |
|
| SOM_Array *array; |
| 75 |
|
| SOM_Vector *vector; |
| 76 |
|
| SOM_GENERIC *som; |
| 77 |
|
| |
| 78 |
0 |
| x = SvIV(*av_fetch(unit, 0, FALSE)); |
| 79 |
0 |
| y = SvIV(*av_fetch(unit, 1, FALSE)); |
| 80 |
0 |
| d = SvNV(*av_fetch(unit, 2, FALSE)); |
| 81 |
0 |
| theta = exp( -d*d/2/sigma/sigma ); |
| 82 |
|
| |
| 83 |
0 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
0 |
0 |
0 |
| if ( !(mg = selfmagic(self)) ) |
0 |
0 |
0 |
|
| 84 |
0 |
| croak("self has no magic!\n"); |
| 85 |
0 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
0 |
0 |
0 |
|
| 86 |
|
| |
| 87 |
0 |
| map = som->map; |
| 88 |
0 |
| array = (&map->array)[x]; |
| 89 |
0 |
| vector = (&array->vector)[y]; |
| 90 |
|
| |
| 91 |
|
| /* hmm.. casting IV to I32.. is that sane? */ |
| 92 |
0 |
| Z = (I32)som->Z; |
| 93 |
|
| |
| 94 |
0 |
▶ | for ( z=0 ; z<Z ; z++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( z=0 ; z<Z ; z++ ) { |
0 |
0 |
0 |
|
| 95 |
0 |
| wold = (&vector->element)[z]; |
| 96 |
0 |
| vold = SvNV(*av_fetch(v,z,FALSE)); |
| 97 |
0 |
| (&vector->element)[z] = (vold - wold) * l * theta + wold; |
| 98 |
|
| } |
| 99 |
0 |
| } |
| 100 |
|
| |
| 101 |
23560 |
| void _adjustn(SOM_GENERIC* som,NV l,NV sigma,NV* n,AV* v) { |
| 102 |
|
| IV x,y,X,Y; |
| 103 |
|
| I32 z,Z; |
| 104 |
|
| NV d,theta,vold,wold; |
| 105 |
|
| SOM_Map *map; |
| 106 |
|
| SOM_Array *array; |
| 107 |
|
| SOM_Vector *vector; |
| 108 |
|
| |
| 109 |
23560 |
| map = som->map; |
| 110 |
23560 |
| X = som->X; |
| 111 |
23560 |
| Y = som->Y; |
| 112 |
|
| |
| 113 |
138160 |
▶ | for ( x=0 ; x<X ; x++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=0 ; x<X ; x++ ) { |
114600 |
23560 |
138160 |
|
| 114 |
114600 |
| array = (&map->array)[x]; |
| 115 |
787800 |
▶ | for ( y=0 ; y<Y ; y++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=0 ; y<Y ; y++ ) { |
673200 |
114600 |
787800 |
|
| 116 |
673200 |
| d = n[x*Y+y]; |
| 117 |
673200 |
▶ | if (d < 0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d < 0) continue; |
507742 |
165458 |
673200 |
|
| 118 |
165458 |
| theta = exp( -d*d/2/sigma/sigma ); |
| 119 |
165458 |
| vector = (&array->vector)[y]; |
| 120 |
|
| |
| 121 |
|
| /* hmm.. casting IV to I32.. is that sane? */ |
| 122 |
165458 |
| Z = (I32)som->Z; |
| 123 |
|
| |
| 124 |
661832 |
▶ | for ( z=0 ; z<Z ; z++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( z=0 ; z<Z ; z++ ) { |
496374 |
165458 |
661832 |
|
| 125 |
496374 |
| wold = (&vector->element)[z]; |
| 126 |
496374 |
| vold = SvNV(*av_fetch(v,z,FALSE)); |
| 127 |
496374 |
| (&vector->element)[z] = |
| 128 |
496374 |
| (vold - wold) * l * theta + wold; |
| 129 |
|
| } |
| 130 |
|
| } |
| 131 |
|
| } |
| 132 |
23560 |
| } |
| 133 |
|
| |
| 134 |
10 |
| AV* _neighbors(SV* self,NV sigma,IV X0,IV Y0,...) { |
| 135 |
|
| IV i,x,y,X,Y; |
| 136 |
|
| NV distance,*n; |
| 137 |
|
| AV *tmp,*neighbors; |
| 138 |
|
| MAGIC *mg; |
| 139 |
|
| SOM_GENERIC *som; |
| 140 |
|
| void (*neiguts)(SOM_GENERIC* som,NV sigma,IV X0,IV Y0,NV *n); |
| 141 |
|
| |
| 142 |
10 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
10 |
0 |
10 |
| if ( !(mg = selfmagic(self)) ) |
0 |
10 |
10 |
|
| 143 |
0 |
| croak("self has no magic!\n"); |
| 144 |
10 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
10 |
0 |
10 |
|
| 145 |
|
| |
| 146 |
10 |
| X = som->X; |
| 147 |
10 |
| Y = som->Y; |
| 148 |
|
| |
| 149 |
10 |
| i = X*Y; |
| 150 |
10 |
▶ | Newx(n,i,NV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(n,i,NV); |
0 |
10 |
10 |
|
| 151 |
346 |
▶ | for ( i-=1 ; i>=0 ; i-- ) |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i-=1 ; i>=0 ; i-- ) |
336 |
10 |
346 |
|
| 152 |
336 |
| n[i] = -1; |
| 153 |
|
| |
| 154 |
10 |
▶ | if ( som->type == SOMType_Torus ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( som->type == SOMType_Torus ) |
4 |
6 |
10 |
|
| 155 |
4 |
| neiguts = _torus_neiguts; |
| 156 |
6 |
▶ | else if ( som->type == SOMType_Hexa ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( som->type == SOMType_Hexa ) |
6 |
0 |
6 |
|
| 157 |
6 |
| neiguts = _hexa_neiguts; |
| 158 |
0 |
▶ | else if ( som->type == SOMType_Rect ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( som->type == SOMType_Rect ) |
0 |
0 |
0 |
|
| 159 |
0 |
| neiguts = _rect_neiguts; |
| 160 |
|
| else |
| 161 |
0 |
| croak("unknown type"); |
| 162 |
|
| |
| 163 |
10 |
| neiguts(som,sigma,X0,Y0,n); |
| 164 |
|
| |
| 165 |
10 |
| neighbors = newAV(); |
| 166 |
|
| |
| 167 |
66 |
▶ | for ( x=0 ; x<X ; x++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=0 ; x<X ; x++ ) { |
56 |
10 |
66 |
|
| 168 |
392 |
▶ | for ( y=0 ; y<Y ; y++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=0 ; y<Y ; y++ ) { |
336 |
56 |
392 |
|
| 169 |
336 |
| distance = n[x*Y+y]; |
| 170 |
336 |
▶ | if ( distance >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( distance >= 0 ) { |
44 |
292 |
336 |
|
| 171 |
44 |
| tmp = newAV(); |
| 172 |
44 |
| av_push(tmp,newSViv(x)); |
| 173 |
44 |
| av_push(tmp,newSViv(y)); |
| 174 |
44 |
| av_push(tmp,newSVnv(distance)); |
| 175 |
44 |
| av_push(neighbors,newRV_noinc((SV*)tmp)); |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
|
| } |
| 179 |
10 |
| Safefree(n); |
| 180 |
10 |
| return neighbors; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
912 |
| SOM_Vector* _make_vector(SOM_Array* array) { |
| 184 |
|
| IV z,len; |
| 185 |
|
| AV *thingy; |
| 186 |
|
| SV *tie; |
| 187 |
|
| HV *stash; |
| 188 |
|
| SOM_Vector *vector; |
| 189 |
|
| |
| 190 |
912 |
| z = array->Z; |
| 191 |
|
| |
| 192 |
912 |
| len = sizeof(SOM_Vector)+z*sizeof(NV); |
| 193 |
912 |
| Newxc(vector, len, char, SOM_Vector); |
| 194 |
912 |
| Zero(vector, len, char); |
| 195 |
|
| |
| 196 |
912 |
| vector->Z = z; |
| 197 |
|
| |
| 198 |
912 |
| thingy = newAV(); |
| 199 |
912 |
| tie = newRV_noinc(newSViv(PTR2IV(vector))); |
| 200 |
912 |
| stash = gv_stashpv("AI::NeuralNet::FastSOM::VECTOR", GV_ADD); |
| 201 |
912 |
| sv_bless(tie, stash); |
| 202 |
912 |
| hv_magic((HV*)thingy, (GV*)tie, 'P'); |
| 203 |
912 |
| vector->ref = newRV_noinc((SV*)thingy); |
| 204 |
|
| |
| 205 |
912 |
| (&vector->element)[z] = 0.0; |
| 206 |
3648 |
▶ | for ( z-=1 ; z>=0 ; z-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( z-=1 ; z>=0 ; z-- ) { |
2736 |
912 |
3648 |
|
| 207 |
2736 |
| (&vector->element)[z] = 0.0; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
912 |
| return vector; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
169 |
| SOM_Array* _make_array(SOM_Map* map) { |
| 214 |
|
| IV y,len; |
| 215 |
|
| AV *thingy; |
| 216 |
|
| SV *tie; |
| 217 |
|
| HV *stash; |
| 218 |
|
| SOM_Array *array; |
| 219 |
|
| |
| 220 |
169 |
| y = map->Y; |
| 221 |
|
| |
| 222 |
169 |
| len = sizeof(SOM_Array)+y*sizeof(SOM_Vector*); |
| 223 |
169 |
| Newxc(array, len, char, SOM_Array); |
| 224 |
169 |
| Zero(array, len, char); |
| 225 |
|
| |
| 226 |
169 |
| array->Y = y; |
| 227 |
169 |
| array->Z = map->Z; |
| 228 |
|
| |
| 229 |
169 |
| thingy = newAV(); |
| 230 |
169 |
| tie = newRV_noinc(newSViv(PTR2IV(array))); |
| 231 |
169 |
| stash = gv_stashpv("AI::NeuralNet::FastSOM::ARRAY", GV_ADD); |
| 232 |
169 |
| sv_bless(tie, stash); |
| 233 |
169 |
| hv_magic((HV*)thingy, (GV*)tie, PERL_MAGIC_tied); |
| 234 |
169 |
| array->ref = newRV_noinc((SV*)thingy); |
| 235 |
|
| |
| 236 |
169 |
| (&array->vector)[y] = NULL; |
| 237 |
1081 |
▶ | for ( y-=1 ; y>=0 ; y-- ) |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y-=1 ; y>=0 ; y-- ) |
912 |
169 |
1081 |
|
| 238 |
912 |
| (&array->vector)[y] = _make_vector( array ); |
| 239 |
|
| |
| 240 |
169 |
| return array; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
39 |
| SOM_Map* _make_map(SOM_GENERIC *som) { |
| 244 |
|
| IV x,len; |
| 245 |
|
| AV *thingy; |
| 246 |
|
| SV *tie; |
| 247 |
|
| HV *stash; |
| 248 |
|
| SOM_Map *map; |
| 249 |
|
| |
| 250 |
39 |
| x = som->X; |
| 251 |
|
| |
| 252 |
39 |
| len = sizeof(SOM_Map)+x*sizeof(SOM_Array*); |
| 253 |
39 |
| Newxc(map, len, char, SOM_Map); |
| 254 |
39 |
| Zero(map, len, char); |
| 255 |
|
| |
| 256 |
39 |
| map->X = x; |
| 257 |
39 |
| map->Y = som->Y; |
| 258 |
39 |
| map->Z = som->Z; |
| 259 |
|
| |
| 260 |
39 |
| thingy = newAV(); |
| 261 |
39 |
| tie = newRV_noinc(newSViv(PTR2IV(map))); |
| 262 |
39 |
| stash = gv_stashpv("AI::NeuralNet::FastSOM::MAP", GV_ADD); |
| 263 |
39 |
| sv_bless(tie, stash); |
| 264 |
39 |
| hv_magic((HV*)thingy, (GV*)tie, PERL_MAGIC_tied); |
| 265 |
39 |
| map->ref = newRV_noinc((SV*)thingy); |
| 266 |
|
| |
| 267 |
39 |
| (&map->array)[x] = NULL; |
| 268 |
208 |
▶ | for ( x-=1 ; x>=0 ; x-- ) |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x-=1 ; x>=0 ; x-- ) |
169 |
39 |
208 |
|
| 269 |
169 |
| (&map->array)[x] = _make_array( map ); |
| 270 |
|
| |
| 271 |
39 |
| return map; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| |
| 275 |
|
| |
| 276 |
|
| /* |
| 277 |
|
| * som functions |
| 278 |
|
| */ |
| 279 |
|
| |
| 280 |
496 |
| void _som_bmu(SV* self, AV* sample) { |
| 281 |
|
| IV cx,cy; |
| 282 |
|
| NV cd; |
| 283 |
|
| MAGIC *mg; |
| 284 |
|
| SOM_GENERIC *som; |
| 285 |
496 |
| dXSARGS; |
| 286 |
|
| |
| 287 |
496 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
496 |
0 |
496 |
| if ( !(mg = selfmagic(self)) ) |
0 |
496 |
496 |
|
| 288 |
0 |
| croak("self has no magic!\n"); |
| 289 |
496 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
496 |
0 |
496 |
|
| 290 |
|
| |
| 291 |
496 |
| _bmuguts(som,sample,&cx,&cy,&cd); |
| 292 |
|
| |
| 293 |
|
| PERL_UNUSED_VAR(items); /* -W */ |
| 294 |
496 |
| sp = mark; |
| 295 |
496 |
▶ | XPUSHs(sv_2mortal(newSViv(cx))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(cx))); |
0 |
496 |
496 |
|
| 296 |
496 |
▶ | XPUSHs(sv_2mortal(newSViv(cy))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(cy))); |
0 |
496 |
496 |
|
| 297 |
496 |
▶ | XPUSHs(sv_2mortal(newSVnv(cd))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVnv(cd))); |
0 |
496 |
496 |
|
| 298 |
496 |
| PUTBACK; |
| 299 |
496 |
| } |
| 300 |
|
| |
| 301 |
20 |
| SV* _som_map(SV* self) { |
| 302 |
|
| MAGIC *mg; |
| 303 |
|
| SOM_GENERIC *som; |
| 304 |
|
| |
| 305 |
20 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
20 |
0 |
20 |
| if ( !(mg = selfmagic(self)) ) |
0 |
20 |
20 |
|
| 306 |
0 |
| croak("self has no magic!\n"); |
| 307 |
20 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
20 |
0 |
20 |
|
| 308 |
|
| |
| 309 |
20 |
| SvREFCNT_inc(som->map->ref); |
| 310 |
20 |
| return som->map->ref; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
4 |
| SV* _som_output_dim(SV* self) { |
| 314 |
|
| MAGIC *mg; |
| 315 |
|
| SOM_GENERIC *som; |
| 316 |
|
| |
| 317 |
4 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
4 |
0 |
4 |
| if ( !(mg = selfmagic(self)) ) |
0 |
4 |
4 |
|
| 318 |
0 |
| croak("self has no magic!\n"); |
| 319 |
4 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
4 |
0 |
4 |
|
| 320 |
|
| |
| 321 |
4 |
| SvREFCNT_inc(som->output_dim); |
| 322 |
4 |
| return som->output_dim; |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
99 |
| void _som_train(SV* self,IV epochs) { |
| 326 |
|
| IV i,X,Y,bx,by,epoch; |
| 327 |
|
| NV bd,l,sigma,*n; |
| 328 |
|
| AV **org,**veg,*sample; |
| 329 |
|
| I32 p,pick,nitems,oitems,vitems; |
| 330 |
|
| MAGIC *mg; |
| 331 |
|
| SOM_GENERIC *som; |
| 332 |
|
| bool wantarray; |
| 333 |
|
| void (*neiguts)(SOM_GENERIC* som,NV sigma,IV X0,IV Y0,NV* n); |
| 334 |
99 |
| dXSARGS; |
| 335 |
|
| |
| 336 |
99 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
99 |
0 |
99 |
| if ( !(mg = selfmagic(self)) ) |
0 |
99 |
99 |
|
| 337 |
0 |
| croak("self has no magic!"); |
| 338 |
99 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
99 |
0 |
99 |
|
| 339 |
|
| |
| 340 |
99 |
▶ | if ( epochs < 1 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( epochs < 1 ) |
0 |
99 |
99 |
|
| 341 |
0 |
| epochs = 1; |
| 342 |
|
| |
| 343 |
99 |
▶ | if ( items < 3 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( items < 3 ) |
0 |
99 |
99 |
|
| 344 |
0 |
| croak("no data to learn"); |
| 345 |
|
| |
| 346 |
99 |
| oitems = items - 2; |
| 347 |
99 |
| Newx(org,oitems,AV*); |
| 348 |
99 |
| Newx(veg,oitems,AV*); |
| 349 |
|
| |
| 350 |
388 |
▶ | for ( i=2 ; i<items ; i++ ) |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i=2 ; i<items ; i++ ) |
289 |
99 |
388 |
|
| 351 |
289 |
▶ | if ( SvTYPE(SvRV(ST(i))) != SVt_PVAV ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvTYPE(SvRV(ST(i))) != SVt_PVAV ) |
0 |
289 |
289 |
|
| 352 |
0 |
| croak("training item %i is not an array ref", (int)i); |
| 353 |
|
| else |
| 354 |
289 |
| org[i-2] = (AV*)SvRV(ST(i)); |
| 355 |
|
| |
| 356 |
99 |
| som->LAMBDA = epochs / log( som->Sigma0 ); |
| 357 |
|
| |
| 358 |
99 |
| X = som->X; |
| 359 |
99 |
| Y = som->Y; |
| 360 |
|
| |
| 361 |
99 |
| nitems = X*Y; |
| 362 |
99 |
| Newx(n,nitems,NV); |
| 363 |
|
| |
| 364 |
99 |
▶ | if ( som->type == SOMType_Torus ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( som->type == SOMType_Torus ) |
4 |
95 |
99 |
|
| 365 |
4 |
| neiguts = _torus_neiguts; |
| 366 |
95 |
▶ | else if ( som->type == SOMType_Hexa ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( som->type == SOMType_Hexa ) |
5 |
90 |
95 |
|
| 367 |
5 |
| neiguts = _hexa_neiguts; |
| 368 |
90 |
▶ | else if ( som->type == SOMType_Rect ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( som->type == SOMType_Rect ) |
90 |
0 |
90 |
|
| 369 |
90 |
| neiguts = _rect_neiguts; |
| 370 |
|
| else |
| 371 |
0 |
| croak("unknown type"); |
| 372 |
|
| |
| 373 |
99 |
| wantarray = GIMME_V == G_ARRAY ? TRUE : FALSE; |
| 374 |
|
| |
| 375 |
|
| /* should this be moved somewhere more global? */ |
| 376 |
99 |
▶ | if ( !PL_srand_called ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !PL_srand_called ) { |
0 |
99 |
99 |
|
| 377 |
0 |
| seedDrand01((Rand_seed_t)(time(NULL)+PerlProc_getpid())); |
| 378 |
0 |
| PL_srand_called = TRUE; |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
99 |
| sp = mark; |
| 382 |
|
| |
| 383 |
8219 |
▶ | for ( epoch=1 ; epoch<=epochs ; epoch++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( epoch=1 ; epoch<=epochs ; epoch++ ) { |
8120 |
99 |
8219 |
|
| 384 |
8120 |
| som->T = epoch; |
| 385 |
8120 |
| sigma = som->Sigma0 * exp(-epoch / som->LAMBDA); |
| 386 |
8120 |
| l = som->L0 * exp(-epoch / epochs); |
| 387 |
|
| |
| 388 |
8120 |
| Copy(org,veg,oitems,AV*); |
| 389 |
8120 |
| vitems = oitems; |
| 390 |
|
| |
| 391 |
31680 |
▶ | while ( vitems > 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( vitems > 0 ) { |
23560 |
8120 |
31680 |
|
| 392 |
|
| |
| 393 |
23560 |
| pick = (I32)(Drand01() * vitems); |
| 394 |
|
| |
| 395 |
23560 |
| sample = (AV*)veg[pick]; |
| 396 |
|
| |
| 397 |
|
| /* optimize me! */ |
| 398 |
34955 |
▶ | for ( p=pick+1 ; p<vitems ; p++ ) veg[p-1] = veg[p]; |
Branch: 1 branch
| Branch | True | False | Total |
| for ( p=pick+1 ; p<vitems ; p++ ) veg[p-1] = veg[p]; |
11395 |
23560 |
34955 |
|
| 399 |
23560 |
| vitems--; |
| 400 |
|
| |
| 401 |
23560 |
| _bmuguts(som,sample,&bx,&by,&bd); |
| 402 |
|
| |
| 403 |
23560 |
▶ | if ( wantarray ) XPUSHs(newSVnv(bd)); |
Branch: 2 branches
| Branch | True | False | Total |
| if ( wantarray ) XPUSHs(newSVnv(bd)); |
360 |
23200 |
23560 |
| if ( wantarray ) XPUSHs(newSVnv(bd)); |
0 |
360 |
360 |
|
| 404 |
|
| |
| 405 |
696760 |
▶ | for ( i=0 ; i<nitems ; i++ ) n[i] = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i=0 ; i<nitems ; i++ ) n[i] = -1; |
673200 |
23560 |
696760 |
|
| 406 |
|
| |
| 407 |
23560 |
| neiguts(som,sigma,bx,by,n); |
| 408 |
|
| |
| 409 |
23560 |
| _adjustn(som,l,sigma,n,sample); |
| 410 |
|
| |
| 411 |
|
| } |
| 412 |
|
| } |
| 413 |
|
| |
| 414 |
99 |
| Safefree(n); |
| 415 |
99 |
| Safefree(org); |
| 416 |
99 |
| Safefree(veg); |
| 417 |
|
| |
| 418 |
99 |
| PUTBACK; |
| 419 |
99 |
| } |
| 420 |
|
| |
| 421 |
6 |
| void _som_FREEZE(SV* self,SV* cloning) { |
| 422 |
|
| IV x,y,z; |
| 423 |
|
| MAGIC *mg; |
| 424 |
|
| SOM_Map *m; |
| 425 |
|
| SOM_Array *a; |
| 426 |
|
| SOM_Vector *v; |
| 427 |
|
| SOM_GENERIC *som; |
| 428 |
6 |
| dXSARGS; |
| 429 |
|
| |
| 430 |
|
| PERL_UNUSED_VAR(items); /* -W */ |
| 431 |
6 |
| sp = mark; |
| 432 |
|
| |
| 433 |
6 |
▶ | if ( !SvTRUE(cloning) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvTRUE(cloning) ) { |
6 |
0 |
6 |
|
| 434 |
|
| |
| 435 |
6 |
▶ | if ( (mg = selfmagic(self)) != NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( (mg = selfmagic(self)) != NULL) { |
3 |
3 |
6 |
| if ( (mg = selfmagic(self)) != NULL) { |
3 |
3 |
6 |
|
| 436 |
|
| |
| 437 |
|
| /* |
| 438 |
|
| * we should get here on the first pass. this is where we |
| 439 |
|
| * serialize the hash seen from perl. |
| 440 |
|
| */ |
| 441 |
|
| |
| 442 |
3 |
▶ | XPUSHs(INT2PTR(SV*,newSVpvn("i wanna be a cowboy",19))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(INT2PTR(SV*,newSVpvn("i wanna be a cowboy",19))); |
0 |
3 |
3 |
|
| 443 |
|
| |
| 444 |
|
| } |
| 445 |
3 |
▶ | else if ( SvTYPE(SvRV(self)) == SVt_PVMG ) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( SvTYPE(SvRV(self)) == SVt_PVMG ) { |
3 |
0 |
3 |
|
| 446 |
|
| |
| 447 |
|
| /* |
| 448 |
|
| * this should be the second pass. here we need to serialize |
| 449 |
|
| * the tied part not seen from the perl side. |
| 450 |
|
| */ |
| 451 |
|
| |
| 452 |
3 |
| som = INT2PTR(SOM_GENERIC*,self2iv(self)); |
| 453 |
|
| |
| 454 |
3 |
▶ | XPUSHs( INT2PTR(SV*,newSVpvn( |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( INT2PTR(SV*,newSVpvn( |
0 |
3 |
3 |
|
| 455 |
|
| "beat me whip me make me code badly",34)) ); |
| 456 |
3 |
▶ | XPUSHs( newRV_noinc(newSViv(som->type)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSViv(som->type)) ); |
0 |
3 |
3 |
|
| 457 |
3 |
▶ | XPUSHs( newRV_noinc(newSViv(som->X)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSViv(som->X)) ); |
0 |
3 |
3 |
|
| 458 |
3 |
▶ | XPUSHs( newRV_noinc(newSViv(som->Y)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSViv(som->Y)) ); |
0 |
3 |
3 |
|
| 459 |
3 |
▶ | XPUSHs( newRV_noinc(newSViv(som->Z)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSViv(som->Z)) ); |
0 |
3 |
3 |
|
| 460 |
3 |
▶ | XPUSHs( newRV_noinc(newSVnv(som->R)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSVnv(som->R)) ); |
0 |
3 |
3 |
|
| 461 |
3 |
▶ | XPUSHs( newRV_noinc(newSVnv(som->Sigma0)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSVnv(som->Sigma0)) ); |
0 |
3 |
3 |
|
| 462 |
3 |
▶ | XPUSHs( newRV_noinc(newSVnv(som->L0)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSVnv(som->L0)) ); |
0 |
3 |
3 |
|
| 463 |
3 |
▶ | XPUSHs( newRV_noinc(newSVnv(som->LAMBDA)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSVnv(som->LAMBDA)) ); |
0 |
3 |
3 |
|
| 464 |
3 |
▶ | XPUSHs( newRV_noinc(newSVnv(som->T)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(newSVnv(som->T)) ); |
0 |
3 |
3 |
|
| 465 |
3 |
▶ | XPUSHs( newRV_noinc(som->output_dim) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc(som->output_dim) ); |
0 |
3 |
3 |
|
| 466 |
3 |
▶ | XPUSHs( newRV_noinc((SV*)som->labels) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc((SV*)som->labels) ); |
0 |
3 |
3 |
|
| 467 |
|
| |
| 468 |
3 |
| m = som->map; |
| 469 |
16 |
▶ | for ( x=som->X-1 ; x>=0 ; x-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=som->X-1 ; x>=0 ; x-- ) { |
13 |
3 |
16 |
|
| 470 |
13 |
| a = (&m->array)[x]; |
| 471 |
82 |
▶ | for ( y=som->Y-1 ; y>=0 ; y-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=som->Y-1 ; y>=0 ; y-- ) { |
69 |
13 |
82 |
|
| 472 |
69 |
| v = (&a->vector)[y]; |
| 473 |
276 |
▶ | for ( z=som->Z-1 ; z>=0 ; z-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( z=som->Z-1 ; z>=0 ; z-- ) { |
207 |
69 |
276 |
|
| 474 |
207 |
▶ | XPUSHs(newRV_noinc(newSVnv( |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(newRV_noinc(newSVnv( |
0 |
207 |
207 |
|
| 475 |
|
| (&v->element)[z]))); |
| 476 |
|
| } |
| 477 |
|
| } |
| 478 |
|
| } |
| 479 |
|
| } |
| 480 |
|
| else { |
| 481 |
0 |
| croak("i wanna run with scissors!"); |
| 482 |
|
| } |
| 483 |
|
| } /* cloning */ |
| 484 |
6 |
| PUTBACK; |
| 485 |
6 |
| } |
| 486 |
|
| |
| 487 |
6 |
| void _som_THAW(SV* self,SV* cloning,SV* serialized) { |
| 488 |
|
| IV x,y,z,i; |
| 489 |
|
| SV *rrr; |
| 490 |
|
| HV *stash; |
| 491 |
|
| SOM_Map *m; |
| 492 |
|
| SOM_Array *a; |
| 493 |
|
| SOM_Vector *v; |
| 494 |
|
| SOM_GENERIC *som; |
| 495 |
6 |
| dXSARGS; |
| 496 |
|
| |
| 497 |
|
| PERL_UNUSED_VAR(serialized); /* -W */ |
| 498 |
|
| |
| 499 |
6 |
▶ | if (!SvTRUE(cloning)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvTRUE(cloning)) { |
6 |
0 |
6 |
|
| 500 |
|
| |
| 501 |
6 |
▶ | if ( SvTYPE(SvRV(self)) == SVt_PVMG ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvTYPE(SvRV(self)) == SVt_PVMG ) { |
3 |
3 |
6 |
|
| 502 |
3 |
| Newxz(som,1,SOM_GENERIC); |
| 503 |
|
| |
| 504 |
3 |
| som->type = SvIV(SvRV(ST(3))); |
| 505 |
3 |
| som->X = SvIV(SvRV(ST(4))); |
| 506 |
3 |
| som->Y = SvIV(SvRV(ST(5))); |
| 507 |
3 |
| som->Z = SvIV(SvRV(ST(6))); |
| 508 |
3 |
| som->R = SvNV(SvRV(ST(7))); |
| 509 |
3 |
| som->Sigma0 = SvNV(SvRV(ST(8))); |
| 510 |
3 |
| som->L0 = SvNV(SvRV(ST(9))); |
| 511 |
3 |
| som->LAMBDA = SvNV(SvRV(ST(10))); |
| 512 |
3 |
| som->T = SvNV(SvRV(ST(11))); |
| 513 |
3 |
| som->output_dim = newSVsv(SvRV(ST(12))); |
| 514 |
3 |
| som->labels = (AV*)SvRV(ST(13)); |
| 515 |
|
| |
| 516 |
3 |
| som->map = _make_map( som ); |
| 517 |
|
| |
| 518 |
3 |
| i = 14; |
| 519 |
3 |
| m = som->map; |
| 520 |
16 |
▶ | for ( x=som->X-1 ; x>=0 ; x-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=som->X-1 ; x>=0 ; x-- ) { |
13 |
3 |
16 |
|
| 521 |
13 |
| a = (&m->array)[x]; |
| 522 |
82 |
▶ | for ( y=som->Y-1 ; y>=0 ; y-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=som->Y-1 ; y>=0 ; y-- ) { |
69 |
13 |
82 |
|
| 523 |
69 |
| v = (&a->vector)[y]; |
| 524 |
276 |
▶ | for ( z=som->Z-1 ; z>=0 ; z-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( z=som->Z-1 ; z>=0 ; z-- ) { |
207 |
69 |
276 |
|
| 525 |
|
| /* |
| 526 |
|
| (&v->element)[z] = |
| 527 |
|
| SvNV(SvRV(ST(i++))); |
| 528 |
|
| */ |
| 529 |
207 |
| rrr = SvRV(ST(i++)); |
| 530 |
207 |
| (&v->element)[z] = SvNV(rrr); |
| 531 |
|
| } |
| 532 |
|
| } |
| 533 |
|
| } |
| 534 |
|
| |
| 535 |
3 |
▶ | SvSetSV( SvRV(self), sv_2mortal(newSViv((IV)PTR2IV(som))) ); |
Branch: 1 branch
| Branch | True | False | Total |
| SvSetSV( SvRV(self), sv_2mortal(newSViv((IV)PTR2IV(som))) ); |
3 |
0 |
3 |
|
| 536 |
|
| |
| 537 |
3 |
| stash = SvSTASH(SvRV(self)); |
| 538 |
3 |
| som->ref = sv_bless(newRV_inc((SV*)self),stash); |
| 539 |
|
| |
| 540 |
|
| } |
| 541 |
|
| |
| 542 |
3 |
▶ | else if ( SvTYPE(SvRV(self)) != SVt_PVHV ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( SvTYPE(SvRV(self)) != SVt_PVHV ) |
0 |
3 |
3 |
|
| 543 |
0 |
| croak("you'll put an eye out!"); |
| 544 |
|
| |
| 545 |
|
| } /* cloning */ |
| 546 |
|
| |
| 547 |
|
| PERL_UNUSED_VAR(items); /* -W */ |
| 548 |
6 |
| sp = mark; |
| 549 |
6 |
| PUTBACK; |
| 550 |
6 |
| } |
| 551 |
|
| |
| 552 |
1877 |
| SV* _som_FETCH(SV* self,SV* key) { |
| 553 |
1877 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("map",3) ) ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("map",3) ) ) ) { |
1255 |
622 |
1877 |
|
| 554 |
1255 |
| SOM_GENERIC *som = INT2PTR(SOM_Rect*,self2iv(self)); |
| 555 |
1255 |
| SvREFCNT_inc(som->map->ref); |
| 556 |
1255 |
| return som->map->ref; |
| 557 |
|
| } |
| 558 |
622 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_X",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_X",2) ) ) ) |
81 |
541 |
622 |
|
| 559 |
81 |
| return newSViv(tied2ptr(self)->X); |
| 560 |
541 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Y",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Y",2) ) ) ) |
150 |
391 |
541 |
|
| 561 |
150 |
| return newSViv(tied2ptr(self)->Y); |
| 562 |
391 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Z",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Z",2) ) ) ) |
384 |
7 |
391 |
|
| 563 |
384 |
| return newSViv(tied2ptr(self)->Z); |
| 564 |
7 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_R",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_R",2) ) ) ) |
4 |
3 |
7 |
|
| 565 |
4 |
| return newSVnv(tied2ptr(self)->R); |
| 566 |
3 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_L0",3) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_L0",3) ) ) ) |
0 |
3 |
3 |
|
| 567 |
0 |
| return newSVnv(tied2ptr(self)->L0); |
| 568 |
3 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Sigma0",7) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Sigma0",7) ) ) ) |
0 |
3 |
3 |
|
| 569 |
0 |
| return newSVnv(tied2ptr(self)->Sigma0); |
| 570 |
3 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("output_dim",10) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("output_dim",10) ) ) ) |
0 |
3 |
3 |
|
| 571 |
0 |
| return newSVsv(tied2ptr(self)->output_dim); |
| 572 |
3 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("LAMBDA",6) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("LAMBDA",6) ) ) ) |
0 |
3 |
3 |
|
| 573 |
0 |
| return newSVnv(tied2ptr(self)->LAMBDA); |
| 574 |
3 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("T",1) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("T",1) ) ) ) |
0 |
3 |
3 |
|
| 575 |
0 |
| return newSVnv(tied2ptr(self)->T); |
| 576 |
3 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("labels",6) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("labels",6) ) ) ) |
3 |
0 |
3 |
|
| 577 |
3 |
| return newRV_inc((SV*)(tied2ptr(self)->labels)); |
| 578 |
0 |
| croak("%s not accessible for read", SvPV_nolen(key)); |
| 579 |
|
| } |
| 580 |
|
| |
| 581 |
0 |
| SV* _som_STORE(SV* self,SV* key,SV* val) { |
| 582 |
0 |
▶ | if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_X",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_X",2) ) ) ) |
0 |
0 |
0 |
|
| 583 |
0 |
| tied2ptr(self)->X = SvIV(val); |
| 584 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Y",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Y",2) ) ) ) |
0 |
0 |
0 |
|
| 585 |
0 |
| tied2ptr(self)->Y = SvIV(val); |
| 586 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Z",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Z",2) ) ) ) |
0 |
0 |
0 |
|
| 587 |
0 |
| tied2ptr(self)->Z = SvIV(val); |
| 588 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_R",2) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_R",2) ) ) ) |
0 |
0 |
0 |
|
| 589 |
0 |
| tied2ptr(self)->R = SvNV(val); |
| 590 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_L0",3) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_L0",3) ) ) ) |
0 |
0 |
0 |
|
| 591 |
0 |
| tied2ptr(self)->L0 = SvNV(val); |
| 592 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Sigma0",7) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("_Sigma0",7) ) ) ) |
0 |
0 |
0 |
|
| 593 |
0 |
| tied2ptr(self)->Sigma0 = SvNV(val); |
| 594 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("output_dim",10) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("output_dim",10) ) ) ) |
0 |
0 |
0 |
|
| 595 |
0 |
| tied2ptr(self)->output_dim = newSVsv(val); |
| 596 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("LAMBDA",6) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("LAMBDA",6) ) ) ) |
0 |
0 |
0 |
|
| 597 |
0 |
| tied2ptr(self)->LAMBDA = SvNV(val); |
| 598 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("T",1) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("T",1) ) ) ) |
0 |
0 |
0 |
|
| 599 |
0 |
| tied2ptr(self)->T = SvNV(val); |
| 600 |
0 |
▶ | else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("map",3) ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( !sv_cmp( key, INT2PTR(SV*,newSVpvn("map",3) ) ) ) |
0 |
0 |
0 |
|
| 601 |
0 |
| croak("cant assign to map"); |
| 602 |
|
| else |
| 603 |
0 |
| croak("%s not accessible for write", SvPV_nolen(key)); |
| 604 |
0 |
| return val; |
| 605 |
|
| } |
| 606 |
|
| |
| 607 |
4 |
| SV* _som_FIRSTKEY() { |
| 608 |
4 |
| return INT2PTR(SV*,newSVpvn("_X",2)); |
| 609 |
|
| } |
| 610 |
|
| |
| 611 |
40 |
| SV* _som_NEXTKEY(SV* prev) { |
| 612 |
40 |
▶ | if ( strEQ( SvPVX(prev), "_X" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( strEQ( SvPVX(prev), "_X" ) ) |
4 |
36 |
40 |
|
| 613 |
4 |
| return INT2PTR(SV*,newSVpvn("_Y",2)); |
| 614 |
36 |
▶ | else if ( strEQ( SvPVX(prev), "_Y" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "_Y" ) ) |
4 |
32 |
36 |
|
| 615 |
4 |
| return INT2PTR(SV*,newSVpvn("_Z",2)); |
| 616 |
32 |
▶ | else if ( strEQ( SvPVX(prev), "_Z" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "_Z" ) ) |
4 |
28 |
32 |
|
| 617 |
4 |
| return INT2PTR(SV*,newSVpvn("_R",2)); |
| 618 |
28 |
▶ | else if ( strEQ( SvPVX(prev), "_R" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "_R" ) ) |
4 |
24 |
28 |
|
| 619 |
4 |
| return INT2PTR(SV*,newSVpvn("_Sigma0",7)); |
| 620 |
24 |
▶ | else if ( strEQ( SvPVX(prev), "_Sigma0" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "_Sigma0" ) ) |
4 |
20 |
24 |
|
| 621 |
4 |
| return INT2PTR(SV*,newSVpvn("_L0",3)); |
| 622 |
20 |
▶ | else if ( strEQ( SvPVX(prev), "_L0" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "_L0" ) ) |
4 |
16 |
20 |
|
| 623 |
4 |
| return INT2PTR(SV*,newSVpvn("LAMBDA",6)); |
| 624 |
16 |
▶ | else if ( strEQ( SvPVX(prev), "LAMBDA" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "LAMBDA" ) ) |
4 |
12 |
16 |
|
| 625 |
4 |
| return INT2PTR(SV*,newSVpvn("T",1)); |
| 626 |
12 |
▶ | else if ( strEQ( SvPVX(prev), "T" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "T" ) ) |
4 |
8 |
12 |
|
| 627 |
4 |
| return INT2PTR(SV*,newSVpvn("labels",6)); |
| 628 |
8 |
▶ | else if ( strEQ( SvPVX(prev), "labels" ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( strEQ( SvPVX(prev), "labels" ) ) |
4 |
4 |
8 |
|
| 629 |
4 |
| return INT2PTR(SV*,newSVpvn("map",3)); |
| 630 |
4 |
| return &PL_sv_undef; |
| 631 |
|
| } |
| 632 |
|
| |
| 633 |
81 |
| void _som_DESTROY(SV* self) { |
| 634 |
|
| IV iv; |
| 635 |
|
| SV *ref; |
| 636 |
|
| SOM_Map *map; |
| 637 |
|
| SOM_GENERIC *som; |
| 638 |
|
| |
| 639 |
81 |
▶ | if ( !SvROK(self) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvROK(self) ) |
0 |
81 |
81 |
|
| 640 |
0 |
| return; |
| 641 |
81 |
| ref = SvRV(self); |
| 642 |
81 |
▶ | if ( !SvIOK(ref) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvIOK(ref) ) |
42 |
39 |
81 |
|
| 643 |
42 |
| return; |
| 644 |
39 |
| iv = SvIV(ref); |
| 645 |
39 |
| som = INT2PTR(SOM_GENERIC*,iv); |
| 646 |
39 |
▶ | if ( !som ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !som ) |
0 |
39 |
39 |
|
| 647 |
0 |
| return; |
| 648 |
39 |
| map = som->map; |
| 649 |
|
| /* more to do here ? */ |
| 650 |
|
| } |
| 651 |
|
| |
| 652 |
|
| |
| 653 |
|
| |
| 654 |
|
| /* |
| 655 |
|
| * rect functions |
| 656 |
|
| */ |
| 657 |
|
| |
| 658 |
17160 |
| void _rect_neiguts(SOM_Rect* som,NV sigma,IV X0,IV Y0,NV* n) { |
| 659 |
|
| IV x,y,X,Y; |
| 660 |
|
| NV d2,s2; |
| 661 |
|
| |
| 662 |
17160 |
| X = som->X; |
| 663 |
17160 |
| Y = som->Y; |
| 664 |
|
| |
| 665 |
17160 |
| s2 = sigma * sigma; |
| 666 |
|
| |
| 667 |
102960 |
▶ | for ( x=0 ; x<X ; x++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=0 ; x<X ; x++ ) { |
85800 |
17160 |
102960 |
|
| 668 |
600600 |
▶ | for ( y=0 ; y<Y ; y++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=0 ; y<Y ; y++ ) { |
514800 |
85800 |
600600 |
|
| 669 |
514800 |
| d2 = (x-X0)*(x-X0)+(y-Y0)*(y-Y0); |
| 670 |
514800 |
▶ | if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
114940 |
399860 |
514800 |
|
| 671 |
|
| } |
| 672 |
|
| } |
| 673 |
17160 |
| } |
| 674 |
|
| |
| 675 |
21 |
| void _rect_new(const char* class,...) { |
| 676 |
|
| IV i; |
| 677 |
|
| NV sigma0,rate; |
| 678 |
|
| SV *tie,*rv,*key,*val,*od,*sclass; |
| 679 |
|
| HV *options,*hash,*stash; |
| 680 |
|
| char *begptr,*endptr,*xstart,*ystart,*yend; |
| 681 |
|
| STRLEN len; |
| 682 |
|
| SOM_GENERIC *som; |
| 683 |
21 |
| dXSARGS; |
| 684 |
|
| |
| 685 |
21 |
▶ | if ( (items & 1) ^ 1 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (items & 1) ^ 1 ) |
0 |
21 |
21 |
|
| 686 |
0 |
| croak( "Weird number of arguments\n" ); |
| 687 |
|
| |
| 688 |
21 |
| options = newHV(); |
| 689 |
63 |
▶ | for ( i=1 ; i<items ; i+=2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i=1 ; i<items ; i+=2 ) { |
42 |
21 |
63 |
|
| 690 |
42 |
| key = ST(i); |
| 691 |
42 |
| val = ST(i+1); |
| 692 |
42 |
| len = sv_len(key); |
| 693 |
84 |
| hv_store( options, SvPV_nolen(key), len, val, 0 ); |
| 694 |
|
| } |
| 695 |
|
| |
| 696 |
21 |
▶ | if ( !hv_exists( options, "input_dim", 9 ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !hv_exists( options, "input_dim", 9 ) ) |
0 |
21 |
21 |
|
| 697 |
0 |
| croak( "no input_dim argument" ); |
| 698 |
21 |
▶ | if ( !hv_exists( options, "output_dim", 10 ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !hv_exists( options, "output_dim", 10 ) ) |
0 |
21 |
21 |
|
| 699 |
0 |
| croak( "no output_dim argument" ); |
| 700 |
|
| |
| 701 |
21 |
| Newxz(som,1,SOM_GENERIC); |
| 702 |
|
| |
| 703 |
21 |
| od = newSVsv(*hv_fetch(options,"output_dim",10,FALSE)); |
| 704 |
21 |
| som->output_dim = od; |
| 705 |
|
| |
| 706 |
21 |
| begptr = SvPV_force(od,SvLEN(od)); |
| 707 |
21 |
| endptr = SvEND(od) - 1; /* allow for terminating character */ |
| 708 |
21 |
▶ | if ( endptr < begptr ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( endptr < begptr ) |
0 |
21 |
21 |
|
| 709 |
0 |
| croak("brain damage!!!"); |
| 710 |
|
| |
| 711 |
21 |
| xstart = begptr; |
| 712 |
21 |
▶ | if ( !isDIGIT((char)*xstart) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !isDIGIT((char)*xstart) ) |
0 |
21 |
21 |
|
| 713 |
0 |
| croak("no x-dimension found"); |
| 714 |
21 |
| som->X = Atol(xstart); |
| 715 |
|
| |
| 716 |
21 |
| ystart = yend = endptr; |
| 717 |
21 |
▶ | if ( !isDIGIT((char)*ystart) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !isDIGIT((char)*ystart) ) |
0 |
21 |
21 |
|
| 718 |
0 |
| croak("no y-dimension found"); |
| 719 |
21 |
▶ | while (--ystart >= begptr) |
Branch: 1 branch
| Branch | True | False | Total |
| while (--ystart >= begptr) |
21 |
0 |
21 |
|
| 720 |
21 |
▶ | if ( !isDIGIT((char)*ystart) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !isDIGIT((char)*ystart) ) |
21 |
0 |
21 |
|
| 721 |
21 |
| break; |
| 722 |
21 |
| som->Y = Atol(++ystart); |
| 723 |
|
| |
| 724 |
21 |
| som->Z = SvIV(*hv_fetch(options,"input_dim",9,FALSE)); |
| 725 |
|
| |
| 726 |
42 |
| som->R = som->X > som->Y |
| 727 |
0 |
| ? som->Y / 2.0 |
| 728 |
21 |
▶ | : som->X / 2.0; |
Branch: 1 branch
| Branch | True | False | Total |
| : som->X / 2.0; |
0 |
21 |
21 |
|
| 729 |
|
| |
| 730 |
21 |
▶ | if ( hv_exists( options, "sigma0", 6 ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hv_exists( options, "sigma0", 6 ) ) { |
0 |
21 |
21 |
|
| 731 |
0 |
| sigma0 = SvNV(*hv_fetch(options,"sigma0",6,0)); |
| 732 |
0 |
▶ | if ( sigma0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( sigma0 ) |
0 |
0 |
0 |
|
| 733 |
0 |
| som->Sigma0 = sigma0; |
| 734 |
|
| else |
| 735 |
0 |
| som->Sigma0 = som->R; |
| 736 |
|
| } |
| 737 |
|
| else |
| 738 |
21 |
| som->Sigma0 = som->R; |
| 739 |
|
| |
| 740 |
21 |
▶ | if ( hv_exists( options, "learning_rate", 13 ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hv_exists( options, "learning_rate", 13 ) ) { |
0 |
21 |
21 |
|
| 741 |
0 |
| rate = SvNV(*hv_fetch(options,"learning_rate",13,0)); |
| 742 |
0 |
▶ | if ( rate ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rate ) |
0 |
0 |
0 |
|
| 743 |
0 |
| som->L0 = rate; |
| 744 |
|
| else |
| 745 |
0 |
| som->L0 = 0.1; |
| 746 |
|
| } |
| 747 |
|
| else |
| 748 |
21 |
| som->L0 = 0.1; |
| 749 |
|
| |
| 750 |
21 |
| som->map = _make_map(som); |
| 751 |
21 |
| som->labels = newAV(); |
| 752 |
|
| |
| 753 |
21 |
| sclass = sv_2mortal(newSVpvf("%s",class)); |
| 754 |
21 |
▶ | if (!sv_cmp(sclass,INT2PTR( |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_cmp(sclass,INT2PTR( |
13 |
8 |
21 |
|
| 755 |
|
| SV*,newSVpvn("AI::NeuralNet::FastSOM::Rect",28)))) |
| 756 |
13 |
| som->type = SOMType_Rect; |
| 757 |
|
| /* |
| 758 |
|
| else if (!sv_cmp(sclass,INT2PTR( |
| 759 |
|
| SV*,newSVpvn("AI::NeuralNet::FastSOM::Hexa",28)))) |
| 760 |
|
| som->type = SOMType_Hexa; |
| 761 |
|
| */ |
| 762 |
8 |
▶ | else if (!sv_cmp(sclass,INT2PTR( |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!sv_cmp(sclass,INT2PTR( |
8 |
0 |
8 |
|
| 763 |
|
| SV*,newSVpvn("AI::NeuralNet::FastSOM::Torus",29)))) |
| 764 |
8 |
| som->type = SOMType_Torus; |
| 765 |
|
| else |
| 766 |
0 |
| croak("unknown type"); |
| 767 |
|
| |
| 768 |
21 |
| hash = (HV*)sv_2mortal((SV*)newHV()); |
| 769 |
21 |
| tie = newRV_noinc(newSViv(PTR2IV(som))); |
| 770 |
21 |
| stash = gv_stashpv(class, GV_ADD); |
| 771 |
21 |
| sv_bless(tie, stash); |
| 772 |
21 |
| hv_magic(hash, (GV*)tie, PERL_MAGIC_tied); |
| 773 |
21 |
| rv = sv_bless(newRV_noinc((SV*)hash),stash); |
| 774 |
|
| |
| 775 |
21 |
| som->ref = rv; |
| 776 |
|
| |
| 777 |
|
| /* |
| 778 |
|
| * here 'hash' is the object seen from the perl side. |
| 779 |
|
| * 'tie' is what we see from the c side when accessing the tied |
| 780 |
|
| * functionality. |
| 781 |
|
| */ |
| 782 |
|
| |
| 783 |
21 |
| sp = mark; |
| 784 |
21 |
▶ | XPUSHs(rv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(rv); |
0 |
21 |
21 |
|
| 785 |
21 |
| PUTBACK; |
| 786 |
21 |
| } |
| 787 |
|
| |
| 788 |
4 |
| SV* _rect_radius(SV* self) { |
| 789 |
|
| MAGIC *mg; |
| 790 |
|
| SOM_GENERIC *som; |
| 791 |
|
| |
| 792 |
4 |
▶ | if ( !(mg = selfmagic(self)) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(mg = selfmagic(self)) ) |
4 |
0 |
4 |
| if ( !(mg = selfmagic(self)) ) |
0 |
4 |
4 |
|
| 793 |
0 |
| croak("self has no magic!\n"); |
| 794 |
4 |
▶ | som = self2somptr(self,mg); |
Branch: 1 branch
| Branch | True | False | Total |
| som = self2somptr(self,mg); |
4 |
0 |
4 |
|
| 795 |
|
| |
| 796 |
4 |
| return newSVnv(som->R); |
| 797 |
|
| } |
| 798 |
|
| |
| 799 |
|
| |
| 800 |
|
| |
| 801 |
|
| /* |
| 802 |
|
| * torus functions |
| 803 |
|
| */ |
| 804 |
|
| |
| 805 |
4804 |
| void _torus_neiguts(SOM_Torus* som,NV sigma,IV X0,IV Y0,NV* n) { |
| 806 |
|
| IV x,y,X,Y; |
| 807 |
|
| NV d2,s2; |
| 808 |
|
| |
| 809 |
4804 |
| X = som->X; |
| 810 |
4804 |
| Y = som->Y; |
| 811 |
|
| |
| 812 |
4804 |
| s2 = sigma * sigma; |
| 813 |
|
| |
| 814 |
28824 |
▶ | for ( x=0 ; x<X ; x++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=0 ; x<X ; x++ ) { |
24020 |
4804 |
28824 |
|
| 815 |
168140 |
▶ | for ( y=0 ; y<Y ; y++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=0 ; y<Y ; y++ ) { |
144120 |
24020 |
168140 |
|
| 816 |
|
| |
| 817 |
|
| /* |
| 818 |
|
| * which one of these should "win"? |
| 819 |
|
| */ |
| 820 |
|
| |
| 821 |
144120 |
| d2 = (x-X0)*(x-X0) + (y-Y0)*(y-Y0); |
| 822 |
144120 |
▶ | if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
29968 |
114152 |
144120 |
|
| 823 |
|
| |
| 824 |
144120 |
| d2 = (x-X-X0)*(x-X-X0) + (y-Y0)*(y-Y0); |
| 825 |
144120 |
▶ | if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
1693 |
142427 |
144120 |
|
| 826 |
|
| |
| 827 |
144120 |
| d2 = (x+X-X0)*(x+X-X0) + (y-Y0)*(y-Y0); |
| 828 |
144120 |
▶ | if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
4900 |
139220 |
144120 |
|
| 829 |
|
| |
| 830 |
144120 |
| d2 = (x-X0)*(x-X0) + (y-Y-Y0)*(y-Y-Y0); |
| 831 |
144120 |
▶ | if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
3049 |
141071 |
144120 |
|
| 832 |
|
| |
| 833 |
144120 |
| d2 = (x-X0)*(x-X0) + (y+Y-Y0)*(y+Y-Y0); |
| 834 |
144120 |
▶ | if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d2 <= s2) n[x*Y+y] = sqrt(d2); |
3998 |
140122 |
144120 |
|
| 835 |
|
| } |
| 836 |
|
| } |
| 837 |
4804 |
| } |
| 838 |
|
| |
| 839 |
|
| /* http://www.ai-junkie.com/ann/som/som3.html */ |
| 840 |
|
| |
| 841 |
|
| |
| 842 |
|
| |
| 843 |
|
| /* |
| 844 |
|
| * hexa functions |
| 845 |
|
| */ |
| 846 |
|
| |
| 847 |
15 |
| void _hexa_new(const char* class) { |
| 848 |
|
| IV i; |
| 849 |
|
| SV *key,*val,*od,*tie,*rv; |
| 850 |
|
| NV sigma0,rate; |
| 851 |
|
| HV *options,*hash,*stash; |
| 852 |
|
| STRLEN len; |
| 853 |
|
| SOM_Hexa *hexa; |
| 854 |
15 |
| dXSARGS; |
| 855 |
|
| |
| 856 |
15 |
▶ | if ( (items & 1) ^ 1 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (items & 1) ^ 1 ) |
0 |
15 |
15 |
|
| 857 |
0 |
| croak( "Weird number of arguments\n" ); |
| 858 |
|
| |
| 859 |
15 |
| options = newHV(); |
| 860 |
47 |
▶ | for ( i=1 ; i<items ; i+=2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i=1 ; i<items ; i+=2 ) { |
32 |
15 |
47 |
|
| 861 |
32 |
| key = ST(i); |
| 862 |
32 |
| val = ST(i+1); |
| 863 |
32 |
| len = sv_len(key); |
| 864 |
64 |
| hv_store( options, SvPV_nolen(key), len, val, 0 ); |
| 865 |
|
| } |
| 866 |
|
| |
| 867 |
15 |
▶ | if ( !hv_exists( options, "input_dim", 9 ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !hv_exists( options, "input_dim", 9 ) ) |
0 |
15 |
15 |
|
| 868 |
0 |
| croak( "no input_dim argument" ); |
| 869 |
15 |
▶ | if ( !hv_exists( options, "output_dim", 10 ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !hv_exists( options, "output_dim", 10 ) ) |
0 |
15 |
15 |
|
| 870 |
0 |
| croak( "no ouput_dim argument" ); |
| 871 |
|
| |
| 872 |
15 |
| Newxz(hexa,1,SOM_Hexa); |
| 873 |
|
| |
| 874 |
15 |
| od = newSVsv(*hv_fetch(options,"output_dim",10,FALSE)); |
| 875 |
15 |
| hexa->output_dim = od; |
| 876 |
|
| |
| 877 |
15 |
| hexa->X=SvIV(*hv_fetch(options,"output_dim",10,FALSE)); |
| 878 |
15 |
| hexa->Z=SvIV(*hv_fetch(options,"input_dim",9,FALSE)); |
| 879 |
|
| |
| 880 |
15 |
| hexa->Y = hexa->X; |
| 881 |
15 |
| hexa->R = hexa->X / 2.0; |
| 882 |
|
| |
| 883 |
15 |
▶ | if ( hv_exists( options, "sigma0", 6 ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hv_exists( options, "sigma0", 6 ) ) { |
2 |
13 |
15 |
|
| 884 |
2 |
| sigma0 = SvNV(*hv_fetch(options,"sigma0",6,0)); |
| 885 |
2 |
▶ | if ( sigma0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( sigma0 ) |
2 |
0 |
2 |
|
| 886 |
2 |
| hexa->Sigma0 = sigma0; |
| 887 |
|
| else |
| 888 |
0 |
| hexa->Sigma0 = hexa->R; |
| 889 |
|
| } |
| 890 |
|
| else |
| 891 |
13 |
| hexa->Sigma0 = hexa->R; |
| 892 |
|
| |
| 893 |
15 |
▶ | if ( hv_exists( options, "learning_rate", 13 ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hv_exists( options, "learning_rate", 13 ) ) { |
0 |
15 |
15 |
|
| 894 |
0 |
| rate = SvNV(*hv_fetch(options,"learning_rate",13,0)); |
| 895 |
0 |
▶ | if ( rate ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rate ) |
0 |
0 |
0 |
|
| 896 |
0 |
| hexa->L0 = rate; |
| 897 |
|
| else |
| 898 |
0 |
| hexa->L0 = 0.1; |
| 899 |
|
| } |
| 900 |
|
| else |
| 901 |
15 |
| hexa->L0 = 0.1; |
| 902 |
|
| |
| 903 |
15 |
| hexa->map = _make_map( hexa ); |
| 904 |
15 |
| hexa->labels = newAV(); |
| 905 |
|
| |
| 906 |
15 |
| hexa->type = SOMType_Hexa; |
| 907 |
|
| |
| 908 |
15 |
| hash = (HV*)sv_2mortal((SV*)newHV()); |
| 909 |
15 |
| tie = newRV_noinc(newSViv(PTR2IV(hexa))); |
| 910 |
15 |
| stash = gv_stashpv(class, GV_ADD); |
| 911 |
15 |
| sv_bless(tie, stash); |
| 912 |
15 |
| hv_magic(hash, (GV*)tie, PERL_MAGIC_tied); |
| 913 |
15 |
| rv = sv_bless(newRV_noinc((SV*)hash),stash); |
| 914 |
|
| |
| 915 |
15 |
| hexa->ref = rv; |
| 916 |
|
| |
| 917 |
15 |
| sp = mark; |
| 918 |
15 |
▶ | XPUSHs(rv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(rv); |
0 |
15 |
15 |
|
| 919 |
15 |
| PUTBACK; |
| 920 |
15 |
| } |
| 921 |
|
| |
| 922 |
14616 |
| NV _hexa_distance(NV x1,NV y1,NV x2,NV y2) { |
| 923 |
|
| NV tmp,dx,dy; |
| 924 |
|
| |
| 925 |
14616 |
▶ | if ( x1+y1 > x2+y2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( x1+y1 > x2+y2 ) { |
4855 |
9761 |
14616 |
|
| 926 |
4855 |
| tmp=x1; x1=x2; x2=tmp; |
| 927 |
4855 |
| tmp=y1; y1=y2; y2=tmp; |
| 928 |
|
| } |
| 929 |
|
| |
| 930 |
14616 |
| dx = x2 - x1; |
| 931 |
14616 |
| dy = y2 - y1; |
| 932 |
|
| |
| 933 |
14616 |
▶ | if ( dx<0 || dy<0 ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( dx<0 || dy<0 ) |
13381 |
1235 |
14616 |
| if ( dx<0 || dy<0 ) |
1225 |
12156 |
13381 |
|
| 934 |
2460 |
| return abs(dx) + abs(dy); |
| 935 |
|
| else |
| 936 |
12156 |
▶ | return dx<dy ? dy : dx; |
Branch: 1 branch
| Branch | True | False | Total |
| return dx<dy ? dy : dx; |
4060 |
8096 |
12156 |
|
| 937 |
|
| } |
| 938 |
|
| |
| 939 |
1606 |
| void _hexa_neiguts(SOM_Hexa* som,NV sigma,IV X0,IV Y0,NV* n) { |
| 940 |
|
| IV x,y,X,Y; |
| 941 |
|
| NV d; |
| 942 |
|
| |
| 943 |
1606 |
| X = som->X; |
| 944 |
1606 |
| Y = som->Y; |
| 945 |
|
| |
| 946 |
6442 |
▶ | for ( x=0 ; x<X ; x++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( x=0 ; x<X ; x++ ) { |
4836 |
1606 |
6442 |
|
| 947 |
19452 |
▶ | for ( y=0 ; y<Y ; y++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( y=0 ; y<Y ; y++ ) { |
14616 |
4836 |
19452 |
|
| 948 |
14616 |
| d = _hexa_distance(X0,Y0,x,y); |
| 949 |
14616 |
▶ | if (d <= sigma) n[x*Y+y] = d; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d <= sigma) n[x*Y+y] = d; |
6954 |
7662 |
14616 |
|
| 950 |
|
| } |
| 951 |
|
| } |
| 952 |
1606 |
| } |
| 953 |
|
| |
| 954 |
|
| |
| 955 |
|
| |
| 956 |
|
| /* |
| 957 |
|
| * map functions |
| 958 |
|
| */ |
| 959 |
|
| |
| 960 |
1369 |
| SV* _map_FETCH(SV* self,I32 x) { |
| 961 |
|
| SOM_Map *map; |
| 962 |
|
| SOM_Array *array; |
| 963 |
|
| |
| 964 |
1369 |
| map = INT2PTR(SOM_Map*,self2iv(self)); |
| 965 |
1369 |
| array = (&map->array)[x]; |
| 966 |
1369 |
| SvREFCNT_inc(array->ref); |
| 967 |
1369 |
| return array->ref; |
| 968 |
|
| } |
| 969 |
|
| |
| 970 |
39 |
| void _map_DESTROY(SV* self) { |
| 971 |
|
| SOM_Map *map; |
| 972 |
|
| |
| 973 |
39 |
| map = INT2PTR(SOM_Map*,self2iv(self)); |
| 974 |
|
| /* need more done here ? */ |
| 975 |
39 |
| Safefree( map ); |
| 976 |
39 |
| } |
| 977 |
|
| |
| 978 |
|
| |
| 979 |
|
| |
| 980 |
|
| /* |
| 981 |
|
| * array functions |
| 982 |
|
| */ |
| 983 |
|
| |
| 984 |
431 |
| void _array_STORE(SV* self,IV y,SV* aref) { |
| 985 |
|
| I32 len; |
| 986 |
|
| NV tmp; |
| 987 |
|
| AV *src; |
| 988 |
|
| SV **ptr; |
| 989 |
|
| SOM_Array *array; |
| 990 |
|
| SOM_Vector *dst; |
| 991 |
|
| |
| 992 |
431 |
▶ | if ( SvTYPE( SvRV(aref) ) != SVt_PVAV ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvTYPE( SvRV(aref) ) != SVt_PVAV ) |
0 |
431 |
431 |
|
| 993 |
0 |
| croak("value to store is not a reference to an array\n"); |
| 994 |
431 |
| src = (AV*)SvRV( aref ); |
| 995 |
|
| |
| 996 |
431 |
| array = INT2PTR(SOM_Array*,self2iv(self)); |
| 997 |
431 |
| dst = (&array->vector)[y]; |
| 998 |
|
| |
| 999 |
431 |
▶ | if ( y < 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( y < 0 ) |
0 |
431 |
431 |
|
| 1000 |
0 |
| croak("storing y-index < 0 not supported\n"); |
| 1001 |
431 |
▶ | if ( y >= array->Y ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( y >= array->Y ) |
0 |
431 |
431 |
|
| 1002 |
0 |
| croak("storing y-index > y-dimension of SOM\n"); |
| 1003 |
|
| |
| 1004 |
431 |
| len = av_len( src ); |
| 1005 |
431 |
▶ | if ( len < 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( len < 0 ) |
0 |
431 |
431 |
|
| 1006 |
0 |
| croak("cant store empty vector\n"); |
| 1007 |
431 |
▶ | if ( len+1 > array->Z ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( len+1 > array->Z ) |
0 |
431 |
431 |
|
| 1008 |
0 |
| croak("vector too long\n"); |
| 1009 |
431 |
▶ | if ( len+1 < array->Z ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( len+1 < array->Z ) |
0 |
431 |
431 |
|
| 1010 |
0 |
| croak("vector too short\n"); |
| 1011 |
|
| |
| 1012 |
1724 |
▶ | for ( ; len >= 0 ; len-- ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; len >= 0 ; len-- ) { |
1293 |
431 |
1724 |
|
| 1013 |
1293 |
| ptr = av_fetch( src, len, 0 ); |
| 1014 |
1293 |
▶ | if ( ptr == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ptr == NULL ) |
0 |
1293 |
1293 |
|
| 1015 |
0 |
| croak("NULL ptr!\n"); |
| 1016 |
1293 |
| tmp = SvNV(*ptr); |
| 1017 |
1293 |
| (&dst->element)[len] = tmp; |
| 1018 |
|
| } |
| 1019 |
431 |
| } |
| 1020 |
|
| |
| 1021 |
934 |
| SV* _array_FETCH(SV* self,I32 y) { |
| 1022 |
|
| SOM_Array *array; |
| 1023 |
|
| SOM_Vector *vector; |
| 1024 |
|
| |
| 1025 |
934 |
| array = INT2PTR(SOM_Array*,self2iv(self)); |
| 1026 |
934 |
| vector = (&array->vector)[y]; |
| 1027 |
934 |
| SvREFCNT_inc(vector->ref); |
| 1028 |
934 |
| return vector->ref; |
| 1029 |
|
| } |
| 1030 |
|
| |
| 1031 |
169 |
| void _array_DESTROY(SV* self) { |
| 1032 |
|
| SOM_Array *array; |
| 1033 |
|
| |
| 1034 |
169 |
| array = INT2PTR(SOM_Array*,self2iv(self)); |
| 1035 |
|
| /* need more done here ? */ |
| 1036 |
169 |
| Safefree( array ); |
| 1037 |
169 |
| } |
| 1038 |
|
| |
| 1039 |
|
| |
| 1040 |
|
| |
| 1041 |
|
| /* |
| 1042 |
|
| * vector functions |
| 1043 |
|
| */ |
| 1044 |
|
| |
| 1045 |
9 |
| void _vector_STORE(SV* self,I32 z,NV val) { |
| 1046 |
|
| SOM_Vector *vector; |
| 1047 |
|
| |
| 1048 |
9 |
| vector = INT2PTR(SOM_Vector*,self2iv(self)); |
| 1049 |
9 |
▶ | if ( z < 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( z < 0 ) |
0 |
9 |
9 |
|
| 1050 |
0 |
| croak("negative z-index not supported\n"); |
| 1051 |
9 |
▶ | if ( z >= vector->Z ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( z >= vector->Z ) |
0 |
9 |
9 |
|
| 1052 |
0 |
| croak("z-index larger than vector dimension\n"); |
| 1053 |
9 |
| (&vector->element)[z] = val; |
| 1054 |
9 |
| } |
| 1055 |
|
| |
| 1056 |
1261 |
| SV* _vector_FETCH(SV* self,I32 z) { |
| 1057 |
|
| SOM_Vector *vector; |
| 1058 |
|
| |
| 1059 |
1261 |
| vector = INT2PTR(SOM_Vector*,self2iv(self)); |
| 1060 |
1261 |
| return newSVnv((&vector->element)[z]); |
| 1061 |
|
| } |
| 1062 |
|
| |
| 1063 |
912 |
| void _vector_DESTROY(SV* self) { |
| 1064 |
|
| SOM_Vector *vector; |
| 1065 |
|
| |
| 1066 |
912 |
| vector = INT2PTR(SOM_Vector*,self2iv(self)); |
| 1067 |
|
| /* need more done here ? */ |
| 1068 |
912 |
| Safefree( vector ); |
| 1069 |
912 |
| } |
| 1070 |
|
| |
| 1071 |
|
| |
| 1072 |
|
| |
| 1073 |
|
| /* |
| 1074 |
|
| * |
| 1075 |
|
| * End of C code. Begin XS. |
| 1076 |
|
| * |
| 1077 |
|
| */ |
| 1078 |
|
| |
| 1079 |
|
| |
| 1080 |
|
| |
| 1081 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM |
| 1082 |
|
| |
| 1083 |
|
| PROTOTYPES: DISABLE |
| 1084 |
|
| |
| 1085 |
|
| |
| 1086 |
|
| void |
| 1087 |
|
| train (self, epochs, ...) |
| 1088 |
|
| SV * self |
| 1089 |
|
| IV epochs |
| 1090 |
|
| PREINIT: |
| 1091 |
|
| I32* temp; |
| 1092 |
|
| PPCODE: |
| 1093 |
99 |
| temp = PL_markstack_ptr++; |
| 1094 |
99 |
| _som_train(self,epochs); |
| 1095 |
99 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
99 |
99 |
|
| 1096 |
0 |
| PL_markstack_ptr = temp; |
| 1097 |
0 |
| XSRETURN_EMPTY; |
| 1098 |
|
| } |
| 1099 |
99 |
| return; |
| 1100 |
|
| |
| 1101 |
|
| void |
| 1102 |
|
| bmu (self, sample) |
| 1103 |
|
| SV * self |
| 1104 |
|
| AV * sample |
| 1105 |
|
| PREINIT: |
| 1106 |
|
| I32* temp; |
| 1107 |
|
| PPCODE: |
| 1108 |
496 |
| temp = PL_markstack_ptr++; |
| 1109 |
496 |
| _som_bmu(self,sample); |
| 1110 |
496 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
496 |
496 |
|
| 1111 |
0 |
| PL_markstack_ptr = temp; |
| 1112 |
0 |
| XSRETURN_EMPTY; |
| 1113 |
|
| } |
| 1114 |
496 |
| return; |
| 1115 |
|
| |
| 1116 |
|
| SV * |
| 1117 |
|
| map (self) |
| 1118 |
|
| SV * self |
| 1119 |
|
| PREINIT: |
| 1120 |
|
| SV* rv; |
| 1121 |
|
| CODE: |
| 1122 |
20 |
| rv = _som_map(self); |
| 1123 |
20 |
| ST(0) = rv; |
| 1124 |
20 |
| sv_2mortal(ST(0)); |
| 1125 |
|
| |
| 1126 |
|
| SV * |
| 1127 |
|
| output_dim (self) |
| 1128 |
|
| SV * self |
| 1129 |
|
| PREINIT: |
| 1130 |
|
| SV* rv; |
| 1131 |
|
| CODE: |
| 1132 |
4 |
| rv = _som_output_dim(self); |
| 1133 |
4 |
| ST(0) = rv; |
| 1134 |
4 |
| sv_2mortal(ST(0)); |
| 1135 |
|
| |
| 1136 |
|
| void |
| 1137 |
|
| _adjust (self, l, sigma, unit, v) |
| 1138 |
|
| SV * self |
| 1139 |
|
| NV l |
| 1140 |
|
| NV sigma |
| 1141 |
|
| AV * unit |
| 1142 |
|
| AV * v |
| 1143 |
|
| PREINIT: |
| 1144 |
|
| I32* temp; |
| 1145 |
|
| PPCODE: |
| 1146 |
0 |
| temp = PL_markstack_ptr++; |
| 1147 |
0 |
| _adjust(self, l, sigma, unit, v); |
| 1148 |
0 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
0 |
0 |
|
| 1149 |
0 |
| PL_markstack_ptr = temp; |
| 1150 |
0 |
| XSRETURN_EMPTY; |
| 1151 |
|
| } |
| 1152 |
0 |
| return; |
| 1153 |
|
| |
| 1154 |
|
| void |
| 1155 |
|
| STORABLE_freeze (self, cloning) |
| 1156 |
|
| SV * self |
| 1157 |
|
| SV * cloning |
| 1158 |
|
| PREINIT: |
| 1159 |
|
| I32* temp; |
| 1160 |
|
| PPCODE: |
| 1161 |
6 |
| temp = PL_markstack_ptr++; |
| 1162 |
6 |
| _som_FREEZE(self,cloning); |
| 1163 |
6 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
6 |
6 |
|
| 1164 |
0 |
| PL_markstack_ptr = temp; |
| 1165 |
0 |
| XSRETURN_EMPTY; |
| 1166 |
|
| } |
| 1167 |
6 |
| return; |
| 1168 |
|
| |
| 1169 |
|
| void |
| 1170 |
|
| STORABLE_thaw (self, cloning, serialized, ...) |
| 1171 |
|
| SV * self |
| 1172 |
|
| SV * cloning |
| 1173 |
|
| SV * serialized |
| 1174 |
|
| PREINIT: |
| 1175 |
|
| I32* temp; |
| 1176 |
|
| PPCODE: |
| 1177 |
6 |
| temp = PL_markstack_ptr++; |
| 1178 |
6 |
| _som_THAW(self,cloning,serialized); |
| 1179 |
6 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
6 |
6 |
|
| 1180 |
0 |
| PL_markstack_ptr = temp; |
| 1181 |
0 |
| XSRETURN_UNDEF; |
| 1182 |
|
| } |
| 1183 |
6 |
| return; |
| 1184 |
|
| |
| 1185 |
|
| SV * |
| 1186 |
|
| FETCH (self, key) |
| 1187 |
|
| SV * self |
| 1188 |
|
| SV * key |
| 1189 |
|
| PREINIT: |
| 1190 |
|
| SV* rv; |
| 1191 |
|
| CODE: |
| 1192 |
1877 |
| rv = _som_FETCH(self, key); |
| 1193 |
1877 |
| ST(0) = rv; |
| 1194 |
1877 |
| sv_2mortal(ST(0)); |
| 1195 |
|
| |
| 1196 |
|
| void |
| 1197 |
|
| STORE (self, key, val) |
| 1198 |
|
| SV * self |
| 1199 |
|
| SV * key |
| 1200 |
|
| SV * val |
| 1201 |
|
| PREINIT: |
| 1202 |
|
| I32* temp; |
| 1203 |
|
| PPCODE: |
| 1204 |
0 |
| temp = PL_markstack_ptr++; |
| 1205 |
0 |
| _som_STORE(self, key, val); |
| 1206 |
0 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
0 |
0 |
|
| 1207 |
0 |
| PL_markstack_ptr = temp; |
| 1208 |
0 |
| XSRETURN_EMPTY; |
| 1209 |
|
| } |
| 1210 |
0 |
| return; |
| 1211 |
|
| |
| 1212 |
|
| SV * |
| 1213 |
|
| FIRSTKEY (self) |
| 1214 |
|
| SV * self |
| 1215 |
|
| PREINIT: |
| 1216 |
|
| SV* rv; |
| 1217 |
|
| CODE: |
| 1218 |
4 |
▶ | if (!self) croak("avoiding -Wextra"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self) croak("avoiding -Wextra"); |
0 |
4 |
4 |
|
| 1219 |
4 |
| rv = _som_FIRSTKEY(); |
| 1220 |
4 |
| ST(0) = rv; |
| 1221 |
4 |
| sv_2mortal(ST(0)); |
| 1222 |
|
| |
| 1223 |
|
| SV * |
| 1224 |
|
| NEXTKEY (self,prev) |
| 1225 |
|
| SV * self |
| 1226 |
|
| SV * prev |
| 1227 |
|
| PREINIT: |
| 1228 |
|
| SV* rv; |
| 1229 |
|
| CODE: |
| 1230 |
40 |
▶ | if (!self) croak("avoiding -Wextra"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self) croak("avoiding -Wextra"); |
0 |
40 |
40 |
|
| 1231 |
40 |
| rv = _som_NEXTKEY(prev); |
| 1232 |
40 |
| ST(0) = rv; |
| 1233 |
40 |
| sv_2mortal(ST(0)); |
| 1234 |
|
| |
| 1235 |
|
| void |
| 1236 |
|
| DESTROY (obj) |
| 1237 |
|
| SV * obj |
| 1238 |
|
| PREINIT: |
| 1239 |
|
| I32* temp; |
| 1240 |
|
| PPCODE: |
| 1241 |
81 |
| temp = PL_markstack_ptr++; |
| 1242 |
81 |
| _som_DESTROY(obj); |
| 1243 |
81 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
81 |
0 |
81 |
|
| 1244 |
81 |
| PL_markstack_ptr = temp; |
| 1245 |
81 |
| XSRETURN_EMPTY; |
| 1246 |
|
| } |
| 1247 |
0 |
| return; |
| 1248 |
|
| |
| 1249 |
|
| |
| 1250 |
|
| |
| 1251 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::Rect |
| 1252 |
|
| |
| 1253 |
|
| PROTOTYPES: DISABLE |
| 1254 |
|
| |
| 1255 |
|
| |
| 1256 |
|
| void |
| 1257 |
|
| new (class, ...) |
| 1258 |
|
| const char * class |
| 1259 |
|
| PREINIT: |
| 1260 |
|
| I32* temp; |
| 1261 |
|
| PPCODE: |
| 1262 |
21 |
| temp = PL_markstack_ptr++; |
| 1263 |
21 |
| _rect_new(class); |
| 1264 |
21 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
21 |
21 |
|
| 1265 |
0 |
| PL_markstack_ptr = temp; |
| 1266 |
0 |
| XSRETURN_EMPTY; |
| 1267 |
|
| } |
| 1268 |
21 |
| return; |
| 1269 |
|
| |
| 1270 |
|
| AV * |
| 1271 |
|
| neighbors (self, sigma, X, Y, ...) |
| 1272 |
|
| SV * self |
| 1273 |
|
| NV sigma |
| 1274 |
|
| IV X |
| 1275 |
|
| IV Y |
| 1276 |
|
| PREINIT: |
| 1277 |
|
| I32* temp; |
| 1278 |
|
| CODE: |
| 1279 |
0 |
| temp = PL_markstack_ptr++; |
| 1280 |
0 |
| RETVAL = _neighbors(self, sigma, X, Y); |
| 1281 |
0 |
| PL_markstack_ptr = temp; |
| 1282 |
|
| OUTPUT: |
| 1283 |
|
| RETVAL |
| 1284 |
|
| |
| 1285 |
|
| SV * |
| 1286 |
|
| radius (self) |
| 1287 |
|
| SV * self |
| 1288 |
|
| PREINIT: |
| 1289 |
|
| SV* rv; |
| 1290 |
|
| CODE: |
| 1291 |
4 |
| rv = _rect_radius(self); |
| 1292 |
4 |
| ST(0) = rv; |
| 1293 |
4 |
| sv_2mortal(ST(0)); |
| 1294 |
|
| |
| 1295 |
|
| |
| 1296 |
|
| |
| 1297 |
|
| |
| 1298 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::Torus |
| 1299 |
|
| |
| 1300 |
|
| PROTOTYPES: DISABLE |
| 1301 |
|
| |
| 1302 |
|
| |
| 1303 |
|
| AV * |
| 1304 |
|
| neighbors (self, sigma, X, Y, ...) |
| 1305 |
|
| SV * self |
| 1306 |
|
| NV sigma |
| 1307 |
|
| IV X |
| 1308 |
|
| IV Y |
| 1309 |
|
| PREINIT: |
| 1310 |
|
| I32* temp; |
| 1311 |
|
| CODE: |
| 1312 |
4 |
| temp = PL_markstack_ptr++; |
| 1313 |
4 |
| RETVAL = _neighbors(self, sigma, X, Y); |
| 1314 |
4 |
| PL_markstack_ptr = temp; |
| 1315 |
|
| OUTPUT: |
| 1316 |
|
| RETVAL |
| 1317 |
|
| |
| 1318 |
|
| |
| 1319 |
|
| |
| 1320 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::Hexa |
| 1321 |
|
| |
| 1322 |
|
| PROTOTYPES: DISABLE |
| 1323 |
|
| |
| 1324 |
|
| |
| 1325 |
|
| void |
| 1326 |
|
| new (class, ...) |
| 1327 |
|
| const char * class |
| 1328 |
|
| PREINIT: |
| 1329 |
|
| I32* temp; |
| 1330 |
|
| PPCODE: |
| 1331 |
15 |
| temp = PL_markstack_ptr++; |
| 1332 |
15 |
| _hexa_new(class); |
| 1333 |
15 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
15 |
15 |
|
| 1334 |
0 |
| PL_markstack_ptr = temp; |
| 1335 |
0 |
| XSRETURN_EMPTY; |
| 1336 |
|
| } |
| 1337 |
15 |
| return; |
| 1338 |
|
| |
| 1339 |
|
| AV * |
| 1340 |
|
| neighbors (self, sigma, X, Y, ...) |
| 1341 |
|
| SV * self |
| 1342 |
|
| NV sigma |
| 1343 |
|
| IV X |
| 1344 |
|
| IV Y |
| 1345 |
|
| PREINIT: |
| 1346 |
|
| I32* temp; |
| 1347 |
|
| CODE: |
| 1348 |
6 |
| temp = PL_markstack_ptr++; |
| 1349 |
6 |
| RETVAL = _neighbors(self, sigma, X, Y); |
| 1350 |
6 |
| PL_markstack_ptr = temp; |
| 1351 |
|
| OUTPUT: |
| 1352 |
|
| RETVAL |
| 1353 |
|
| |
| 1354 |
|
| |
| 1355 |
|
| |
| 1356 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::Utils |
| 1357 |
|
| |
| 1358 |
|
| PROTOTYPES: DISABLE |
| 1359 |
|
| |
| 1360 |
|
| |
| 1361 |
|
| NV |
| 1362 |
|
| vector_distance (V1, V2) |
| 1363 |
|
| AV_SPECIAL* V1; |
| 1364 |
|
| AV_SPECIAL* V2; |
| 1365 |
|
| PREINIT: |
| 1366 |
|
| NV rv; |
| 1367 |
|
| CODE: |
| 1368 |
79 |
| rv = _vector_distance((AV*)V1, (AV*)V2); |
| 1369 |
79 |
▶ | XSprePUSH; PUSHn((NV)rv); |
Branch: 1 branch
| Branch | True | False | Total |
| XSprePUSH; PUSHn((NV)rv); |
75 |
4 |
79 |
|
| 1370 |
|
| |
| 1371 |
|
| |
| 1372 |
|
| |
| 1373 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::MAP |
| 1374 |
|
| |
| 1375 |
|
| PROTOTYPES: DISABLE |
| 1376 |
|
| |
| 1377 |
|
| |
| 1378 |
|
| SV * |
| 1379 |
|
| FETCH (self, x) |
| 1380 |
|
| SV * self |
| 1381 |
|
| I32 x |
| 1382 |
|
| PREINIT: |
| 1383 |
|
| SV* rv; |
| 1384 |
|
| CODE: |
| 1385 |
1369 |
| rv = _map_FETCH(self, x); |
| 1386 |
1369 |
| ST(0) = rv; |
| 1387 |
1369 |
| sv_2mortal(ST(0)); |
| 1388 |
|
| |
| 1389 |
|
| IV |
| 1390 |
|
| FETCHSIZE (self) |
| 1391 |
|
| SV * self |
| 1392 |
|
| PREINIT: |
| 1393 |
|
| IV rv; |
| 1394 |
|
| CODE: |
| 1395 |
2 |
| rv = (INT2PTR(SOM_Map*,self2iv(self)))->X; |
| 1396 |
2 |
▶ | XSprePUSH; PUSHi((IV)rv); |
Branch: 1 branch
| Branch | True | False | Total |
| XSprePUSH; PUSHi((IV)rv); |
0 |
2 |
2 |
|
| 1397 |
|
| |
| 1398 |
|
| void |
| 1399 |
|
| DESTROY (obj) |
| 1400 |
|
| SV * obj |
| 1401 |
|
| PREINIT: |
| 1402 |
|
| I32* temp; |
| 1403 |
|
| PPCODE: |
| 1404 |
39 |
| temp = PL_markstack_ptr++; |
| 1405 |
39 |
| _map_DESTROY(obj); |
| 1406 |
39 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
39 |
0 |
39 |
|
| 1407 |
39 |
| PL_markstack_ptr = temp; |
| 1408 |
39 |
| XSRETURN_EMPTY; |
| 1409 |
|
| } |
| 1410 |
0 |
| return; |
| 1411 |
|
| |
| 1412 |
|
| |
| 1413 |
|
| |
| 1414 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::ARRAY |
| 1415 |
|
| |
| 1416 |
|
| PROTOTYPES: DISABLE |
| 1417 |
|
| |
| 1418 |
|
| |
| 1419 |
|
| void |
| 1420 |
|
| STORE (self, y, aref) |
| 1421 |
|
| SV * self |
| 1422 |
|
| IV y |
| 1423 |
|
| SV * aref |
| 1424 |
|
| PREINIT: |
| 1425 |
|
| I32* temp; |
| 1426 |
|
| PPCODE: |
| 1427 |
431 |
| temp = PL_markstack_ptr++; |
| 1428 |
431 |
| _array_STORE(self, y, aref); |
| 1429 |
431 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
431 |
0 |
431 |
|
| 1430 |
431 |
| PL_markstack_ptr = temp; |
| 1431 |
431 |
| XSRETURN_EMPTY; |
| 1432 |
|
| } |
| 1433 |
0 |
| return; |
| 1434 |
|
| |
| 1435 |
|
| SV * |
| 1436 |
|
| FETCH (self, y) |
| 1437 |
|
| SV * self |
| 1438 |
|
| I32 y |
| 1439 |
|
| PREINIT: |
| 1440 |
|
| SV* rv; |
| 1441 |
|
| CODE: |
| 1442 |
934 |
| rv = _array_FETCH(self, y); |
| 1443 |
934 |
| ST(0) = rv; |
| 1444 |
934 |
| sv_2mortal(ST(0)); |
| 1445 |
|
| |
| 1446 |
|
| IV |
| 1447 |
|
| FETCHSIZE (self) |
| 1448 |
|
| SV * self |
| 1449 |
|
| PREINIT: |
| 1450 |
|
| IV rv; |
| 1451 |
|
| CODE: |
| 1452 |
2 |
| rv = (INT2PTR(SOM_Array*,self2iv(self)))->Y; |
| 1453 |
2 |
▶ | XSprePUSH; PUSHi((IV)rv); |
Branch: 1 branch
| Branch | True | False | Total |
| XSprePUSH; PUSHi((IV)rv); |
0 |
2 |
2 |
|
| 1454 |
|
| |
| 1455 |
|
| void |
| 1456 |
|
| DESTROY (obj) |
| 1457 |
|
| SV * obj |
| 1458 |
|
| PREINIT: |
| 1459 |
|
| I32* temp; |
| 1460 |
|
| PPCODE: |
| 1461 |
169 |
| temp = PL_markstack_ptr++; |
| 1462 |
169 |
| _array_DESTROY(obj); |
| 1463 |
169 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
169 |
0 |
169 |
|
| 1464 |
169 |
| PL_markstack_ptr = temp; |
| 1465 |
169 |
| XSRETURN_EMPTY; |
| 1466 |
|
| } |
| 1467 |
0 |
| return; |
| 1468 |
|
| |
| 1469 |
|
| |
| 1470 |
|
| |
| 1471 |
|
| MODULE = AI::NeuralNet::FastSOM PACKAGE = AI::NeuralNet::FastSOM::VECTOR |
| 1472 |
|
| |
| 1473 |
|
| PROTOTYPES: DISABLE |
| 1474 |
|
| |
| 1475 |
|
| |
| 1476 |
|
| void |
| 1477 |
|
| STORE (self, z, val) |
| 1478 |
|
| SV * self |
| 1479 |
|
| I32 z |
| 1480 |
|
| NV val |
| 1481 |
|
| PREINIT: |
| 1482 |
|
| I32* temp; |
| 1483 |
|
| PPCODE: |
| 1484 |
9 |
| temp = PL_markstack_ptr++; |
| 1485 |
9 |
| _vector_STORE(self, z, val); |
| 1486 |
9 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
9 |
0 |
9 |
|
| 1487 |
9 |
| PL_markstack_ptr = temp; |
| 1488 |
9 |
| XSRETURN_EMPTY; |
| 1489 |
|
| } |
| 1490 |
0 |
| return; |
| 1491 |
|
| |
| 1492 |
|
| SV * |
| 1493 |
|
| FETCH (self, z) |
| 1494 |
|
| SV * self |
| 1495 |
|
| I32 z |
| 1496 |
|
| PREINIT: |
| 1497 |
|
| SV* rv; |
| 1498 |
|
| CODE: |
| 1499 |
1261 |
| rv = _vector_FETCH(self, z); |
| 1500 |
1261 |
| ST(0) = rv; |
| 1501 |
1261 |
| sv_2mortal(ST(0)); |
| 1502 |
|
| |
| 1503 |
|
| IV |
| 1504 |
|
| FETCHSIZE (self) |
| 1505 |
|
| SV * self |
| 1506 |
|
| PREINIT: |
| 1507 |
|
| IV rv; |
| 1508 |
|
| CODE: |
| 1509 |
136 |
| rv = (INT2PTR(SOM_Vector*,self2iv(self)))->Z; |
| 1510 |
136 |
▶ | XSprePUSH; PUSHi((IV)rv); |
Branch: 1 branch
| Branch | True | False | Total |
| XSprePUSH; PUSHi((IV)rv); |
0 |
136 |
136 |
|
| 1511 |
|
| |
| 1512 |
|
| void |
| 1513 |
|
| DESTROY (obj) |
| 1514 |
|
| SV * obj |
| 1515 |
|
| PREINIT: |
| 1516 |
|
| I32* temp; |
| 1517 |
|
| PPCODE: |
| 1518 |
912 |
| temp = PL_markstack_ptr++; |
| 1519 |
912 |
| _vector_DESTROY(obj); |
| 1520 |
912 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
912 |
0 |
912 |
|
| 1521 |
912 |
| PL_markstack_ptr = temp; |
| 1522 |
912 |
| XSRETURN_EMPTY; |
| 1523 |
|
| } |
| 1524 |
0 |
| return; |
| 1525 |
|
| |