/*
 * This file is part of ELKI:
 * Environment for Developing KDD-Applications Supported by Index-Structures
 *
 * Copyright (C) 2019
 * ELKI Development Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package elki.similarity.kernel;

import elki.data.NumberVector;
import elki.distance.AbstractNumberVectorDistance;
import elki.similarity.AbstractVectorSimilarity;
import elki.utilities.Alias;
import elki.utilities.Priority;
import elki.utilities.optionhandling.Parameterizer;
import elki.utilities.optionhandling.OptionID;
import elki.utilities.optionhandling.constraints.CommonConstraints;
import elki.utilities.optionhandling.parameterization.Parameterization;
import elki.utilities.optionhandling.parameters.DoubleParameter;
import net.jafama.FastMath;

/**
 * Gaussian radial basis function kernel (RBF Kernel).
 * 
 * @author Erich Schubert
 * @since 0.6.0
 */
@Priority(Priority.RECOMMENDED)
@Alias("rbf")
public class RadialBasisFunctionKernel extends AbstractVectorSimilarity {
  /**
   * Scaling factor gamma. (= - 1/(2sigma^2))
   */
  private final double gamma;

  /**
   * Constructor.
   * 
   * @param sigma Scaling parameter sigma
   */
  public RadialBasisFunctionKernel(double sigma) {
    super();
    this.gamma = -.5 / (sigma * sigma);
  }

  @Override
  public double similarity(NumberVector o1, NumberVector o2) {
    final int dim = AbstractNumberVectorDistance.dimensionality(o1, o2);
    double sim = 0.;
    for(int i = 0; i < dim; i++) {
      final double v = o1.doubleValue(i) - o2.doubleValue(i);
      sim += v * v;
    }
    return FastMath.exp(gamma * sim);
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   */
  public static class Par implements Parameterizer {
    /**
     * Sigma parameter: standard deviation.
     */
    public static final OptionID SIGMA_ID = new OptionID("kernel.rbf.sigma", "Standard deviation of the Gaussian RBF kernel.");

    /**
     * Sigma parameter
     */
    protected double sigma = 1.;

    @Override
    public void configure(Parameterization config) {
      new DoubleParameter(SIGMA_ID, 1.) //
          .addConstraint(CommonConstraints.GREATER_THAN_ZERO_DOUBLE) //
          .grab(config, x -> sigma = x);
    }

    @Override
    public RadialBasisFunctionKernel make() {
      return new RadialBasisFunctionKernel(sigma);
    }
  }
}

@@ -1642,32 +1642,31 @@
 tor 
+m
 gamma
-. (
+ 
 = -
- 
 1/(2
+ 
 sigma%5E2)
 )%0A  
@@ -1661,17 +1661,17 @@
 sigma%5E2)
-)
+.
 %0A   */%0A 
@@ -1692,16 +1692,17 @@
  double 
+m
 gamma;%0A%0A
@@ -1743,19 +1743,19 @@
  @param 
-sig
+gam
 ma Scali
@@ -1767,19 +1767,19 @@
 rameter 
-sig
+gam
 ma%0A   */
@@ -1821,19 +1821,19 @@
 (double 
-sig
+gam
 ma) %7B%0A  
@@ -1852,16 +1852,17 @@
    this.
+m
 gamma = 
@@ -1866,28 +1866,13 @@
  = -
-.5 / (sigma * sig
+gam
 ma
-)
 ;%0A  
@@ -2190,16 +2190,17 @@
 ath.exp(
+m
 gamma * 
@@ -2338,16 +2338,205 @@
 rizer %7B%0A
+    /**%0A     * Gamma parameter.%0A     */%0A    public static final OptionID GAMMA_ID = new OptionID(%22kernel.rbf.gamma%22, %22Gamma parameter of the Gaussian RBF kernel (alternative to sigma).%22);%0A%0A
     /**%0A
@@ -2710,16 +2710,39 @@
 F kernel
+ (alternative to gamma)
 .%22);%0A%0A  
@@ -2746,37 +2746,45 @@
 %0A    /**%0A     * 
-Sigma
+Gamma scaling
  parameter%0A     
@@ -2811,18 +2811,18 @@
 ble 
-sig
+gam
 ma = 
-1
 .
+5
 ;%0A%0A 
@@ -2892,16 +2892,36 @@
  %7B%0A     
+ DoubleParameter g =
  new Dou
@@ -2937,20 +2937,16 @@
 ter(
-SIG
+GAM
 MA_ID
-, 1.
 ) //
@@ -3014,24 +3014,256 @@
 _DOUBLE) //%0A
+          .setOptional(true);%0A      if(config.grab(g)) %7B%0A        gamma = g.doubleValue();%0A      %7D%0A      else %7B%0A        new DoubleParameter(SIGMA_ID, 1.) //%0A            .addConstraint(CommonConstraints.GREATER_THAN_ZERO_DOUBLE) //%0A  
           .g
@@ -3283,19 +3283,38 @@
  -%3E 
-sig
+gam
 ma = 
-x);
+.5 / (x * x));%0A      %7D
 %0A   
@@ -3420,19 +3420,19 @@
 nKernel(
-sig
+gam
 ma);%0A   

allow gamma or sigma parameterization