Accord.Statistics.Kernels.Sparse.SparseLinear.SquaredEuclidean C# (CSharp) Method

SquaredEuclidean() public static method

Computes the squared Euclidean distance of two vectors given in sparse representation.
public static SquaredEuclidean ( double x, double y ) : double
x double The first vector x.
y double The second vector y.
return double
        public static double SquaredEuclidean(double[] x, double[] y)
        {
            double sum = 0;

            int i = 0, j = 0;

            while (i < x.Length && j < y.Length)
            {
                double posx = x[i];
                double posy = y[j];

                if (posx == posy)
                {
                    double d = x[i + 1] - y[j + 1];

                    sum += d * d;

                    i += 2; j += 2;
                }
                else if (posx < posy)
                {
                    double d = x[j + 1];
                    sum += d * d;
                    i += 2;
                }
                else if (posx > posy)
                {
                    double d = y[j + 1];
                    sum += d * d;
                    j += 2;
                }
            }

            for (; i < x.Length; i += 2)
                sum += x[i + 1] * x[i + 1];

            for (; j < y.Length; j += 2)
                sum += y[j + 1] * y[j + 1];

            return sum;
        }

Usage Example

        /// <summary>
        ///   Computes the distance in input space
        ///   between two points given in feature space.
        /// </summary>
        ///
        /// <param name="x">Vector <c>x</c> in feature (kernel) space.</param>
        /// <param name="y">Vector <c>y</c> in feature (kernel) space.</param>
        ///
        /// <returns>Distance between <c>x</c> and <c>y</c> in input space.</returns>
        ///
        public override double Distance(double[] x, double[] y)
        {
            if (x == y)
            {
                return(0.0);
            }

            double norm = SparseLinear.SquaredEuclidean(x, y);

            return(2 - 2 * Math.Exp(-gamma * Math.Sqrt(norm)));
        }
All Usage Examples Of Accord.Statistics.Kernels.Sparse.SparseLinear::SquaredEuclidean