Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Ext
- {
- private static IEnumerable<decimal> LinearInterpolate(decimal lowerBound, decimal upperBound, int pointCount)
- {
- var gradient = (upperBound - lowerBound) / (pointCount + 1);
- for(var i = 1; i <= pointCount; i++)
- {
- yield return lowerBound + gradient * i;
- }
- }
- public static IEnumerable<decimal> Interpolate(this IEnumerable<decimal> original)
- {
- var zeroCount = 0;
- var previousValue = 0m;
- var enumerator = original.GetEnumerator();
- // Strip leading 0s
- while(enumerator.MoveNext())
- {
- if(enumerator.Current == 0)
- {
- yield return 0;
- }
- else
- {
- previousValue = enumerator.Current;
- yield return enumerator.Current;
- break;
- }
- }
- // Interpolate 0s
- while(enumerator.MoveNext())
- {
- if(enumerator.Current == 0)
- {
- zeroCount++;
- }
- else
- {
- if(zeroCount > 0)
- {
- foreach(var item in LinearInterpolate(previousValue, enumerator.Current, zeroCount))
- {
- yield return item;
- }
- zeroCount = 0;
- }
- yield return enumerator.Current;
- previousValue = enumerator.Current;
- }
- }
- // Strip trailing 0s
- for (var i = 0; i < zeroCount; i++)
- {
- yield return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment