/// <summary> /// Gets an array of coefficients that can be used to weight a numerical sequence of the specified length. /// The value of an element in the array represents the weighting coefficient for the item at the corresponding index in the sequence. /// </summary> /// <param name="rateOfChange">The rate at which the ratios change.</param> /// <param name="length">The length of the sequence for which weighting coefficients will be returned.</param> /// <returns>An array of <paramref name="length"/> length, that contains the weighting coefficients.</returns> /// <remarks> /// The sum of the array's elements should always equal 1. /// When a low value is provided for <paramref name="length"/>, the sum of the array elements is measurably smaller than 1. /// Consequently, after all elements in the sequence have been calculated, the remainder is divided equally between all elements such that their sum approaches 1. /// </remarks> public Double[] GetRatios(Int32 length, Double rateOfChange = 0.5) { Double[] ratios = new Double[length]; Double currentAmount = 1; for (Int32 i = 0; i < ratios.Length; i++) { Double currentVal = currentAmount * rateOfChange; Double remaining = currentAmount - currentVal; currentAmount = remaining; ratios[i] = currentVal; } // As the number of buckets increases, the sum of the buckets approaches 1. // When the number of buckets is very small, a significant portion is not represented in the output. // To account for this, we split the remaining portion and apply it evenly among every bucket. currentAmount /= length; // Sometimes Resharper is a little dumb. // ReSharper disable once RedundantAssignment ratios = ratios.Select(bucket => bucket += currentAmount).ToArray(); return ratios; }
private static Double[] CalculateNormalizeVector(Double[,] matrix) { Int32 n = matrix.GetLength(0); Double[] notNormalizeVector = new Double[n]; for (Int32 i = 0; i < n; i++) { Double tmp = 1; for (Int32 j = 0; j < n; j++) { tmp *= matrix[i, j]; } notNormalizeVector[i] = Math.Pow(tmp, (Double)1/n); } Double sumOfVector = notNormalizeVector.Sum(); Double[] normalizeVector = notNormalizeVector.Select(v => v / sumOfVector).ToArray(); return normalizeVector; }