Click or drag to resize
Accord.NET (logo)

DistanceGetDistanceT Method

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
public static IDistance<T> GetDistance<T>(
	Func<T, T, double> func
Request Example View Source


Type: SystemFuncT, T, Double
The method of Distance.

Type Parameters

The type of the elements being compared in the distance function.

Return Value

Type: IDistanceT
An object of the class that implements the given distance.

This method is intended to be used in scenarios where you have been using any of the static methods in the Distance class, but now you would like to obtain a reference to an object that implements the same distance you have been using before, but in a object-oriented, polymorphic manner. Please see the example below for more details.

Note: This method relies on reflection and might not work on all scenarios, environments, and/or platforms.

// Let's say you have been using the static Distance.Euclidean() method in 
// your code, and now you would like to obtain a reference to a class that 
// implements the IDistance interface for this same distance, such that you 
// could pass it to some other method in the framework:

double[] x = new double[] { 2, 4, 1 };
double[] y = new double[] { 0, 0, 0 };

double a = Distance.Euclidean(x, y); // should be 4.58257569495584

// Use the GetDistance method to obtain an IDistance that implements it:
IDistance<double[]> obj = Distance.GetDistance<double[]>(Distance.Euclidean);

// We can continue computing the same distances as before using:
double b = obj.Distance(x, y); // should be 4.58257569495584
See Also