| 312 | } |
| 313 | |
| 314 | double Kernel::k_function(const svm_node *x, const svm_node *y, |
| 315 | const svm_parameter& param) |
| 316 | { |
| 317 | switch(param.kernel_type) |
| 318 | { |
| 319 | case LINEAR: |
| 320 | return dot(x,y); |
| 321 | case POLY: |
| 322 | return powi(param.gamma*dot(x,y)+param.coef0,param.degree); |
| 323 | case RBF: |
| 324 | { |
| 325 | double sum = 0; |
| 326 | while(x->index != -1 && y->index !=-1) |
| 327 | { |
| 328 | if(x->index == y->index) |
| 329 | { |
| 330 | double d = x->value - y->value; |
| 331 | sum += d*d; |
| 332 | ++x; |
| 333 | ++y; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | if(x->index > y->index) |
| 338 | { |
| 339 | sum += y->value * y->value; |
| 340 | ++y; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | sum += x->value * x->value; |
| 345 | ++x; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | while(x->index != -1) |
| 351 | { |
| 352 | sum += x->value * x->value; |
| 353 | ++x; |
| 354 | } |
| 355 | |
| 356 | while(y->index != -1) |
| 357 | { |
| 358 | sum += y->value * y->value; |
| 359 | ++y; |
| 360 | } |
| 361 | |
| 362 | return exp(-param.gamma*sum); |
| 363 | } |
| 364 | case SIGMOID: |
| 365 | return tanh(param.gamma*dot(x,y)+param.coef0); |
| 366 | case PRECOMPUTED: //x: test (validation), y: SV |
| 367 | return x[(int)(y->value)].value; |
| 368 | default: |
| 369 | return 0; // Unreachable |
| 370 | } |
| 371 | } |