Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VarianceEvaluator.cs
6
8{
13 {
14 private ChunkedList<double> doubleValues = new ChunkedList<double>();
15 private ChunkedList<IElement> elementValues = null;
16 private IElement sum = null;
17 private double doubleSum = 0;
18 private long count = 0;
19 private bool isDouble = true;
20
26 : base(Node.Argument)
27 {
28 }
29
33 public override void RestartEvaluator()
34 {
35 this.sum = null;
36 this.count = 0;
37 this.doubleSum = 0;
38 this.isDouble = true;
39
40 if (this.doubleValues is null)
41 this.doubleValues = new ChunkedList<double>();
42 else
43 this.doubleValues.Clear();
44
45 this.elementValues = null;
46 }
47
52 public override void AggregateElement(IElement Element)
53 {
54 if (this.isDouble && Element is DoubleNumber D)
55 {
56 this.doubleSum += D.Value;
57 this.doubleValues.Add(D.Value);
58 }
59 else if (this.sum is null)
60 {
61 this.isDouble = false;
62 this.elementValues = new ChunkedList<IElement>();
63
64 if (this.doubleValues.Count > 0)
65 {
66 ChunkNode<double> Loop = this.doubleValues.FirstChunk;
67 int i, c;
68
69 while (!(Loop is null))
70 {
71 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
72 this.elementValues.Add(new DoubleNumber(Loop.Elements[i]));
73
74 Loop = Loop.Next;
75 }
76 }
77
78 if (this.sum is null)
79 this.sum = Element;
80 else
81 this.sum = Add.EvaluateAddition(new DoubleNumber(this.doubleSum), Element, this.Node);
82
83 this.elementValues.Add(Element);
84 }
85 else
86 {
87 this.sum = Add.EvaluateAddition(this.sum, Element, this.Node);
88 this.elementValues.Add(Element);
89 }
90
91 this.count++;
92 }
93
97 public override IElement GetAggregatedResult()
98 {
99 if (this.count == 0)
100 return ObjectValue.Null;
101 else if (this.isDouble)
102 {
103 double Average = this.doubleSum / this.count;
104 double Result = 0;
105 double Term;
106
107 ChunkNode<double> Loop = this.doubleValues.FirstChunk;
108 int i, c;
109
110 while (!(Loop is null))
111 {
112 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
113 {
114 Term = Loop[i] - Average;
115 Result += Term * Term;
116 }
117
118 Loop = Loop.Next;
119 }
120
121 return new DoubleNumber(Result / this.count);
122 }
123 else
124 {
125 IElement Average = Divide.EvaluateDivision(this.sum, new DoubleNumber(this.count), this.Node);
126 IElement Result = null;
127 IElement Term;
128
129 ChunkNode<IElement> Loop = this.elementValues.FirstChunk;
130 int i, c;
131
132 while (!(Loop is null))
133 {
134 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
135 {
136 Term = Subtract.EvaluateSubtraction(Loop[i], Average, this.Node);
137 Term = Multiply.EvaluateMultiplication(Term, Term, this.Node);
138
139 if (Result is null)
140 Result = Term;
141 else
142 Result = Add.EvaluateAddition(Result, Term, this.Node);
143 }
144
145 Loop = Loop.Next;
146 }
147
148 if (Result is null)
149 return ObjectValue.Null;
150 else
151 return Divide.EvaluateDivision(Result, new DoubleNumber(this.count), this.Node);
152 }
153 }
154 }
155}
Node referencing a chunk in a ChunkedList<T>
Definition: ChunkNode.cs:11
ChunkNode< T > Next
Next chunk
Definition: ChunkNode.cs:26
int Pos
Index after the last element in chunk.
Definition: ChunkNode.cs:51
T[] Elements
Array of elements in chunk.
Definition: ChunkNode.cs:36
int Start
Index of first element in chunk.
Definition: ChunkNode.cs:46
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Clear()
Clears the collection.
Definition: ChunkedList.cs:306
ChunkNode< T > FirstChunk
First chunk
Definition: ChunkedList.cs:259
int Count
Number of elements in collection.
Definition: ChunkedList.cs:68
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Base class for all types of elements.
Definition: Element.cs:14
override void RestartEvaluator()
Restarts the evaluator.
override IElement GetAggregatedResult()
Gets the aggregated result.
VarianceEvaluator(Variance Node)
Variance(v) iterative evaluator
override void AggregateElement(IElement Element)
Aggregates one new element.
An interative evaluator of a function or operation defined by a single ScriptNode.
ScriptNode Node
Node being iteratively evaluated.
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
static IElement EvaluateAddition(IElement Left, IElement Right, ScriptNode Node)
Adds two operands.
Definition: Add.cs:65
static IElement EvaluateDivision(IElement Left, IElement Right, ScriptNode Node)
Divides the right operand from the left one.
Definition: Divide.cs:65
static IElement EvaluateMultiplication(IElement Left, IElement Right, ScriptNode Node)
Multiplies two operands.
Definition: Multiply.cs:69
static IElement EvaluateSubtraction(IElement Left, IElement Right, ScriptNode Node)
Subtracts the right operand from the left one.
Definition: Subtract.cs:65
Basic interface for all types of elements.
Definition: IElement.cs:21