/// <summary> /// Calculates the centroid for a given set of __inputVecType__s. /// </summary> public static __outputVecType__ ComputeCentroid(this __inputVecType__[] vectors, int[] indices) { __outputVecType__ sum = __outputVecType__.Zero; for (var i = 0; i < indices.Length; i++) { sum += __cast__vectors[indices[i]]; } return(sum / (__scalarType__)indices.Length); }
/// <summary> /// Calculates a weighted Centroid for a given array of __inputVecType__s /// </summary> public static __outputVecType__ ComputeCentroid(this __inputVecType__[] vectors, __scalarType__[] weights) { __outputVecType__ sum = __outputVecType__.Zero; __scalarType__ weightSum = 0; for (int i = 0; i < vectors.Length; i++) { sum += weights[i] * __cast__vectors[i]; weightSum += weights[i]; } return(sum / weightSum); }
//# foreach(var scalar in Meta.VecFieldTypes) //# { //# foreach(var dim in Meta.VecTypeDimensions) //# { //# var inputVecType = Meta.VecTypeOf(dim, scalar).Name; //# var outputVecType = scalar.IsReal ? inputVecType : Meta.VecTypeOf(dim, Meta.DoubleType).Name; //# var scalarType = scalar.IsReal ? scalar.Name : Meta.DoubleType.Name; //# var cast = scalar.IsReal ? "" : "(" + outputVecType + ")"; /// <summary> /// Calculates the Centroid for a given set of __inputVecType__s /// </summary> public static __outputVecType__ ComputeCentroid(this IEnumerable <__inputVecType__> vectors) { __outputVecType__ sum = __outputVecType__.Zero; int count = 0; foreach (var e in vectors) { sum += __cast__e; count++; } return(sum / (__scalarType__)count); }
/// <summary> /// Calculates a weighted centroid for vectors and weights given by indices. /// Sum(vectors[indices[i]] * weights[indices[i]]) / Sum(weights[indices[i]]. /// </summary> public static __outputVecType__ ComputeCentroid(this __inputVecType__[] vectors, __scalarType__[] weights, int[] indices) { __outputVecType__ sum = __outputVecType__.Zero; __scalarType__ weightSum = 0; for (int i = 0; i < indices.Length; i++) { var w = weights[indices[i]]; sum += w * __cast__vectors[indices[i]]; weightSum += w; } return(sum / weightSum); }