I need to compute outputs from inputs using the formula:
Output(i) = inputs(i) * factor + outputs(i - 1) * (1 - factor)
I implemented this using a for loop and MoreLinq's Scan extension:
Int32 p = 5;
Decimal factor = (Decimal) 2 / (p + 1);
List<Decimal?> inputs = Enumerable.Range(1, 40).Select(x => (Decimal?)x).ToList();
// Using Scan extension
List<Decimal?> outputs1 = inputs.Scan((x, y) => x * factor + (y * (1 - factor)) ?? 0).ToList();
// Using for loop
List<Decimal?> outputs2 = new List<Decimal?> { inputs[0] };
for (int i = 1; i < inputs.Count(); i++)
outputs2.Add(inputs[i] * factor + (outputs2[i - 1] * (1 - factor)) ?? 0);
However, I get different outputs results. What am I missing?
Am I using Scan incorrectly?
You are interpreting parameters of
transformationfunction in the wrong order (check out the source code to see howtransformationis invoked). Change your code to:xis aggregator, i.e. previous value, y is the current one.