| 1 | package ICC::Support::rbf; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = 0.21; | ||
| 7 | |||
| 8 | # revised 2016-05-17 | ||
| 9 | # | ||
| 10 | # Copyright © 2004-2018 by William B. Birkett | ||
| 11 | |||
| 12 | # add development directory | ||
| 13 | use lib 'lib'; | ||
| 14 | |||
| 15 | # inherit from Shared | ||
| 16 | use parent qw(ICC::Shared); | ||
| 17 | |||
| 18 | # parameter count by function type | ||
| 19 | my @Np = (1, 1, 1, 1, 2, 0, 1); | ||
| 20 | |||
| 21 | # create new rbf object | ||
| 22 | # parameters: ([ref_to_parameter_array, ref_to_center_array, [ref_to_covariance_matrix]]) | ||
| 23 | # returns: (ref_to_object) | ||
| 24 | sub new { | ||
| 25 | |||
| 26 | # get object class | ||
| 27 | my $class = shift(); | ||
| 28 | |||
| 29 | # create empty rbf object | ||
| 30 | my $self = [ | ||
| 31 | {}, # object header | ||
| 32 | [], # parameter array | ||
| 33 | [], # center array | ||
| 34 | [] # inverse covariance matrix | ||
| 35 | ]; | ||
| 36 | |||
| 37 | # if two or three parameters supplied | ||
| 38 | if (@_ == 2 || @_ == 3) { | ||
| 39 | |||
| 40 | # verify parameter array reference | ||
| 41 | (ref($_[0]) eq 'ARRAY') or croak('not an array reference'); | ||
| 42 | |||
| 43 | # verify function type | ||
| 44 | ($_[0][0] == int($_[0][0]) && defined($Np[$_[0][0]])) or croak('invalid function type'); | ||
| 45 | |||
| 46 | # verify number of parameters | ||
| 47 | ($#{$_[0]} == $Np[$_[0][0]]) or croak('wrong number of parameters'); | ||
| 48 | |||
| 49 | # copy parameter array | ||
| 50 | $self->[1] = [@{shift()}]; | ||
| 51 | |||
| 52 | # verify center array reference | ||
| 53 | (ref($_[0]) eq 'ARRAY') or croak('not an array reference'); | ||
| 54 | |||
| 55 | # copy center array | ||
| 56 | $self->[2] = [@{shift()}]; | ||
| 57 | |||
| 58 | # if third parameter (covariance matrix) supplied | ||
| 59 | if (@_) { | ||
| 60 | |||
| 61 | # invert covariance matrix | ||
| 62 | (my $info, $self->[3]) = ICC::Support::Lapack::inv(shift()); | ||
| 63 | |||
| 64 | # quit on error | ||
| 65 | ($info == 0) or croak("inversion of convariance matrix failed: info= $info"); | ||
| 66 | |||
| 67 | # verify center and covariance are same size | ||
| 68 | ($#{$self->[2]} == $#{$self->[3]}) or croak('center and covariance are different sizes'); | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | } elsif (@_) { | ||
| 73 | |||
| 74 | # wrong number of parameters | ||
| 75 | croak('wrong number of parameters'); | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | # bless object | ||
| 80 | bless($self, $class); | ||
| 81 | |||
| 82 | # return object reference | ||
| 83 | return($self); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | # compute rbf function | ||
| 88 | # parameters: (ref_to_input_array) | ||
| 89 | # returns: (output_value) | ||
| 90 | sub transform { | ||
| 91 | |||
| 92 | # get parameters | ||
| 93 | my ($self, $in) = @_; | ||
| 94 | |||
| 95 | # local variables | ||
| 96 | my ($r, $array, $type); | ||
| 97 | |||
| 98 | # compute radius | ||
| 99 | $r = radius($self, $in); | ||
| 100 | |||
| 101 | # get parameter array reference | ||
| 102 | $array = $self->[1]; | ||
| 103 | |||
| 104 | # get function type | ||
| 105 | $type = $array->[0]; | ||
| 106 | |||
| 107 | # function type 0 (Gaussian) | ||
| 108 | if ($type == 0) { | ||
| 109 | |||
| 110 | # return value | ||
| 111 | return(exp(-($array->[1] * $r)**2)); | ||
| 112 | |||
| 113 | # function type 1 (multiquadric) | ||
| 114 | } elsif ($type == 1) { | ||
| 115 | |||
| 116 | # return value | ||
| 117 | return(sqrt(1 + ($array->[1] * $r)**2)); | ||
| 118 | |||
| 119 | # function type 2 (inverse quadratic) | ||
| 120 | } elsif ($type == 2) { | ||
| 121 | |||
| 122 | # return value | ||
| 123 | return(1/(1 + ($array->[1] * $r)**2)); | ||
| 124 | |||
| 125 | # function type 3 (inverse multiquadric) | ||
| 126 | } elsif ($type == 3) { | ||
| 127 | |||
| 128 | # return value | ||
| 129 | return(1/(sqrt(1 + ($array->[1] * $r)**2))); | ||
| 130 | |||
| 131 | # function type 4 (generalized multiquadric) | ||
| 132 | } elsif ($type == 4) { | ||
| 133 | |||
| 134 | # return value | ||
| 135 | return((1 + ($array->[1] * $r)**2)**$array->[2]); | ||
| 136 | |||
| 137 | # function type 5 (thin plate spline) | ||
| 138 | } elsif ($type == 5) { | ||
| 139 | |||
| 140 | # if r is 0 | ||
| 141 | if ($r == 0) { | ||
| 142 | |||
| 143 | # return value | ||
| 144 | return(0); | ||
| 145 | |||
| 146 | } else { | ||
| 147 | |||
| 148 | # return value | ||
| 149 | return($r**2 * log($r)); | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | # function type 6 (polyharmonic spline) | ||
| 154 | } elsif ($type == 6) { | ||
| 155 | |||
| 156 | # if k is odd | ||
| 157 | if ($array->[1] % 2) { | ||
| 158 | |||
| 159 | # return value | ||
| 160 | return($r**$array->[1]); | ||
| 161 | |||
| 162 | } else { | ||
| 163 | |||
| 164 | # if r is 0 | ||
| 165 | if ($r == 0) { | ||
| 166 | |||
| 167 | # return value | ||
| 168 | return(0); | ||
| 169 | |||
| 170 | } else { | ||
| 171 | |||
| 172 | # return value | ||
| 173 | return($r**$array->[1] * log($r)); | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | } else { | ||
| 180 | |||
| 181 | # error | ||
| 182 | croak('invalid rbf function type'); | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | # compute rbf Jacobian | ||
| 189 | # parameters: (ref_to_input_array) | ||
| 190 | # returns: (Jacobian_vector, [output_value]) | ||
| 191 | sub jacobian { | ||
| 192 | |||
| 193 | # get parameters | ||
| 194 | my ($self, $in) = @_; | ||
| 195 | |||
| 196 | # local variables | ||
| 197 | my ($r, $jac, $array, $type); | ||
| 198 | my ($er, $beta, $k, $derv, $out); | ||
| 199 | |||
| 200 | # compute radius and radial Jacobian | ||
| 201 | ($r, $jac) = _jacobian($self, $in); | ||
| 202 | |||
| 203 | # get parameter array reference | ||
| 204 | $array = $self->[1]; | ||
| 205 | |||
| 206 | # get function type | ||
| 207 | $type = $array->[0]; | ||
| 208 | |||
| 209 | # function type 0 (Gaussian) | ||
| 210 | if ($type == 0) { | ||
| 211 | |||
| 212 | # compute ɛr | ||
| 213 | $er = $array->[1] * $r; | ||
| 214 | |||
| 215 | # compute dɸ/dr | ||
| 216 | $derv = -2 * $array->[1] * $er * exp(-$er**2); | ||
| 217 | |||
| 218 | # compute output value, if requested | ||
| 219 | $out = exp(-$er**2) if wantarray; | ||
| 220 | |||
| 221 | # function type 1 (multiquadric) | ||
| 222 | } elsif ($type == 1) { | ||
| 223 | |||
| 224 | # compute ɛr | ||
| 225 | $er = $array->[1] * $r; | ||
| 226 | |||
| 227 | # compute dɸ/dr | ||
| 228 | $derv = $array->[1] * $er/sqrt(1 + $er**2); | ||
| 229 | |||
| 230 | # compute output value, if requested | ||
| 231 | $out = sqrt(1 + $er**2) if wantarray; | ||
| 232 | |||
| 233 | # function type 2 (inverse quadratic) | ||
| 234 | } elsif ($type == 2) { | ||
| 235 | |||
| 236 | # compute ɛr | ||
| 237 | $er = $array->[1] * $r; | ||
| 238 | |||
| 239 | # compute dɸ/dr | ||
| 240 | $derv = -2 * $array->[1] * $er/(1 + $er**2)**2; | ||
| 241 | |||
| 242 | # compute output value, if requested | ||
| 243 | $out = 1/(1 + $er**2) if wantarray; | ||
| 244 | |||
| 245 | # function type 3 (inverse multiquadric) | ||
| 246 | } elsif ($type == 3) { | ||
| 247 | |||
| 248 | # compute ɛr | ||
| 249 | $er = $array->[1] * $r; | ||
| 250 | |||
| 251 | # compute dɸ/dr | ||
| 252 | $derv = -$array->[1] * $er/(1 + $er**2)**1.5; | ||
| 253 | |||
| 254 | # compute output value, if requested | ||
| 255 | $out = 1/(sqrt(1 + $er**2)) if wantarray; | ||
| 256 | |||
| 257 | # function type 4 (generalized multiquadric) | ||
| 258 | } elsif ($type == 4) { | ||
| 259 | |||
| 260 | # compute ɛr | ||
| 261 | $er = $array->[1] * $r; | ||
| 262 | |||
| 263 | # get β | ||
| 264 | $beta = $array->[2]; | ||
| 265 | |||
| 266 | # compute dɸ/dr | ||
| 267 | $derv = 2 * $beta * $array->[1] * $er * (1 + $er**2)**($beta - 1); | ||
| 268 | |||
| 269 | # compute output value, if requested | ||
| 270 | $out = (1 + $er**2)**$beta if wantarray; | ||
| 271 | |||
| 272 | # function type 5 (thin plate spline) | ||
| 273 | } elsif ($type == 5) { | ||
| 274 | |||
| 275 | # if r is 0 | ||
| 276 | if ($r == 0) { | ||
| 277 | |||
| 278 | # dɸ/dr = 0 | ||
| 279 | $derv = 0; | ||
| 280 | |||
| 281 | # compute output value, if requested | ||
| 282 | $out = 0 if wantarray; | ||
| 283 | |||
| 284 | } else { | ||
| 285 | |||
| 286 | # compute dɸ/dr | ||
| 287 | $derv = $r * (2 * log($r) + 1); | ||
| 288 | |||
| 289 | # compute output value, if requested | ||
| 290 | $out = $r**2 * log($r) if wantarray; | ||
| 291 | |||
| 292 | } | ||
| 293 | |||
| 294 | # function type 6 (polyharmonic spline) | ||
| 295 | } elsif ($type == 6) { | ||
| 296 | |||
| 297 | # get k | ||
| 298 | $k = $array->[1]; | ||
| 299 | |||
| 300 | # if k is odd | ||
| 301 | if ($k % 2) { | ||
| 302 | |||
| 303 | # compute dɸ/dr | ||
| 304 | $derv = $k * $r**($k - 1); | ||
| 305 | |||
| 306 | # compute output value, if requested | ||
| 307 | $out = $r**$k if wantarray; | ||
| 308 | |||
| 309 | } else { | ||
| 310 | |||
| 311 | # if r is 0 | ||
| 312 | if ($r == 0) { | ||
| 313 | |||
| 314 | # dɸ/dr = 0 | ||
| 315 | $derv = 0; | ||
| 316 | |||
| 317 | # compute output value, if requested | ||
| 318 | $out = 0 if wantarray; | ||
| 319 | |||
| 320 | } else { | ||
| 321 | |||
| 322 | # compute dɸ/dr | ||
| 323 | $derv = $r**($k - 1) * ($k * log($r) + 1); | ||
| 324 | |||
| 325 | # compute output value, if requested | ||
| 326 | $out = $r**$k * log($r) if wantarray; | ||
| 327 | |||
| 328 | } | ||
| 329 | |||
| 330 | } | ||
| 331 | |||
| 332 | } else { | ||
| 333 | |||
| 334 | # error | ||
| 335 | croak('invalid rbf function type'); | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | # for each Jacobian element | ||
| 340 | for my $i (0 .. $#{$jac}) { | ||
| 341 | |||
| 342 | # multiply by dɸ/dr | ||
| 343 | $jac->[$i] *= $derv; | ||
| 344 | |||
| 345 | } | ||
| 346 | |||
| 347 | # if output requested | ||
| 348 | if (wantarray) { | ||
| 349 | |||
| 350 | # return Jacobian and output | ||
| 351 | return($jac, $out); | ||
| 352 | |||
| 353 | } else { | ||
| 354 | |||
| 355 | # return Jacobian | ||
| 356 | return($jac); | ||
| 357 | |||
| 358 | } | ||
| 359 | |||
| 360 | } | ||
| 361 | |||
| 362 | # get/set parameter array | ||
| 363 | # parameters: ([ref_to_parameter_array]) | ||
| 364 | # returns: (ref_to_parameter_array) | ||
| 365 | sub array { | ||
| 366 | |||
| 367 | # get object reference | ||
| 368 | my $self = shift(); | ||
| 369 | |||
| 370 | # local variables | ||
| 371 | my ($array, $type); | ||
| 372 | |||
| 373 | # if parameter | ||
| 374 | if (@_) { | ||
| 375 | |||
| 376 | # get array reference | ||
| 377 | $array = shift(); | ||
| 378 | |||
| 379 | # verify array reference | ||
| 380 | (ref($array) eq 'ARRAY') or croak('not an array reference'); | ||
| 381 | |||
| 382 | # get function type | ||
| 383 | $type = $array->[0]; | ||
| 384 | |||
| 385 | # verify function type | ||
| 386 | ($type == int($type) && $type >= 0 && defined($Np[$_[0][0]])) or croak('invalid function type'); | ||
| 387 | |||
| 388 | # verify number of parameters | ||
| 389 | ($#{$array} == $Np[$type]) or croak('wrong number of parameters'); | ||
| 390 | |||
| 391 | # set array reference | ||
| 392 | $self->[1] = $array; | ||
| 393 | |||
| 394 | } | ||
| 395 | |||
| 396 | # return center array reference | ||
| 397 | return($self->[1]); | ||
| 398 | |||
| 399 | } | ||
| 400 | |||
| 401 | # get/set center | ||
| 402 | # parameters: ([ref_to_center_array]) | ||
| 403 | # returns: (ref_to_center_array) | ||
| 404 | sub center { | ||
| 405 | |||
| 406 | # get object reference | ||
| 407 | my $self = shift(); | ||
| 408 | |||
| 409 | # local variables | ||
| 410 | my ($array); | ||
| 411 | |||
| 412 | # if parameter | ||
| 413 | if (@_) { | ||
| 414 | |||
| 415 | # get array reference | ||
| 416 | $array = shift(); | ||
| 417 | |||
| 418 | # verify array reference | ||
| 419 | (ref($array) eq 'ARRAY') or croak('not an array reference'); | ||
| 420 | |||
| 421 | # set array reference | ||
| 422 | $self->[2] = $array; | ||
| 423 | |||
| 424 | } | ||
| 425 | |||
| 426 | # return center array reference | ||
| 427 | return($self->[2]); | ||
| 428 | |||
| 429 | } | ||
| 430 | |||
| 431 | # get/set inverse covariance matrix | ||
| 432 | # parameters: ([ref_to_inverse_covariance_matrix]) | ||
| 433 | # returns: (ref_to_inverse_covariance_matrix) | ||
| 434 | sub matrix { | ||
| 435 | |||
| 436 | # get object reference | ||
| 437 | my $self = shift(); | ||
| 438 | |||
| 439 | # local variables | ||
| 440 | my ($matrix); | ||
| 441 | |||
| 442 | # if parameter | ||
| 443 | if (@_) { | ||
| 444 | |||
| 445 | # get matrix reference | ||
| 446 | $matrix = shift(); | ||
| 447 | |||
| 448 | # verify matrix reference | ||
| 449 | (ref($matrix) eq 'ARRAY' && ref($matrix->[0]) eq 'ARRAY') or croak('not a matrix reference'); | ||
| 450 | |||
| 451 | # set matrix reference | ||
| 452 | $self->[3] = $matrix; | ||
| 453 | |||
| 454 | } | ||
| 455 | |||
| 456 | # return matrix reference | ||
| 457 | return($self->[3]); | ||
| 458 | |||
| 459 | } | ||
| 460 | |||
| 461 | # print object contents to string | ||
| 462 | # format is an array structure | ||
| 463 | # parameter: ([format]) | ||
| 464 | # returns: (string) | ||
| 465 | sub sdump { | ||
| 466 | |||
| 467 | # get parameters | ||
| 468 | my ($self, $p) = @_; | ||
| 469 | |||
| 470 | # local variables | ||
| 471 | my ($s, $fmt); | ||
| 472 | |||
| 473 | # resolve parameter to an array reference | ||
| 474 | $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : []; | ||
| 475 | |||
| 476 | # get format string | ||
| 477 | $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef'; | ||
| 478 | |||
| 479 | # set string to object ID | ||
| 480 | $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self); | ||
| 481 | |||
| 482 | # return | ||
| 483 | return($s); | ||
| 484 | |||
| 485 | } | ||
| 486 | |||
| 487 | # compute radius | ||
| 488 | # note: no error checking | ||
| 489 | # parameters: (ref_to_input_vector) | ||
| 490 | # returns: (radius) | ||
| 491 | sub radius { | ||
| 492 | |||
| 493 | # get parameters | ||
| 494 | my ($self, $in) = @_; | ||
| 495 | |||
| 496 | # if object has covariance matrix | ||
| 497 | if (defined($self->[3]) && (0 != @{$self->[3]})) { | ||
| 498 | |||
| 499 | # return Mahalanobis distance | ||
| 500 | return(ICC::Support::Lapack::mahal($in, $self->[2], $self->[3])); | ||
| 501 | |||
| 502 | } else { | ||
| 503 | |||
| 504 | # return Euclidean distance | ||
| 505 | return(ICC::Support::Lapack::euclid($in, $self->[2])); | ||
| 506 | |||
| 507 | } | ||
| 508 | |||
| 509 | } | ||
| 510 | |||
| 511 | # compute radial Jacobian | ||
| 512 | # note: no error checking | ||
| 513 | # parameters: (ref_to_object, ref_to_input_vector) | ||
| 514 | # returns: (radius, jacobian_vector) | ||
| 515 | sub _jacobian { | ||
| 516 | |||
| 517 | # get parameters | ||
| 518 | my ($self, $in) = @_; | ||
| 519 | |||
| 520 | # local variables | ||
| 521 | my ($r, $jac, $wwt); | ||
| 522 | |||
| 523 | # for each dimension | ||
| 524 | for my $i (0 .. $#{$self->[2]}) { | ||
| 525 | |||
| 526 | # compute Jacobian element | ||
| 527 | $jac->[$i] = ($in->[$i] - $self->[2][$i]); | ||
| 528 | |||
| 529 | } | ||
| 530 | |||
| 531 | # if object has covariance matrix | ||
| 532 | if (defined($self->[3]) && @{$self->[3]}) { | ||
| 533 | |||
| 534 | # radius is Mahalanobis distance | ||
| 535 | $r = ICC::Support::Lapack::mahal($in, $self->[2], $self->[3]); | ||
| 536 | |||
| 537 | # for each row | ||
| 538 | for my $i (0 .. $#{$self->[3]}) { | ||
| 539 | |||
| 540 | # for each column | ||
| 541 | for my $j (0 .. $#{$self->[3]}) { | ||
| 542 | |||
| 543 | # set (W + Wᵀ)/2 matrix element | ||
| 544 | $wwt->[$i][$j] = ($self->[3][$i][$j] + $self->[3][$j][$i])/2; | ||
| 545 | |||
| 546 | } | ||
| 547 | |||
| 548 | } | ||
| 549 | |||
| 550 | # multiply Jacobian by inverse covariance matrix | ||
| 551 | $jac = ICC::Support::Lapack::vec_xplus($wwt, $jac, {'trans' => 'T'}); | ||
| 552 | |||
| 553 | } else { | ||
| 554 | |||
| 555 | # radius is Euclidean distance | ||
| 556 | $r = ICC::Support::Lapack::euclid($in, $self->[2]); | ||
| 557 | |||
| 558 | } | ||
| 559 | |||
| 560 | # if radius is zero | ||
| 561 | if ($r == 0) { | ||
| 562 | |||
| 563 | # set Jacobian to all ones | ||
| 564 | $jac = [(1) x @{$self->[2]}]; | ||
| 565 | |||
| 566 | } else { | ||
| 567 | |||
| 568 | # for each dimension | ||
| 569 | for my $i (0 .. $#{$self->[2]}) { | ||
| 570 | |||
| 571 | # divide by radius | ||
| 572 | $jac->[$i] /= $r; | ||
| 573 | |||
| 574 | } | ||
| 575 | |||
| 576 | } | ||
| 577 | |||
| 578 | # return radius and Jacobian vector | ||
| 579 | return($r, $jac); | ||
| 580 | |||
| 581 | } | ||
| 582 | |||
| 583 | 1; |