Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StandardDeviationEvaluator.cs
1using System;
8
10{
15 {
16 private ChunkedList<double> doubleValues = new ChunkedList<double>();
17 private ChunkedList<IElement> elementValues = null;
18 private IElement sum = null;
19 private double doubleSum = 0;
20 private long count = 0;
21 private bool isDouble = true;
22
28 : base(Node.Argument)
29 {
30 }
31
35 public override void RestartEvaluator()
36 {
37 this.sum = null;
38 this.count = 0;
39 this.doubleSum = 0;
40 this.isDouble = true;
41
42 if (this.doubleValues is null)
43 this.doubleValues = new ChunkedList<double>();
44 else
45 this.doubleValues.Clear();
46
47 this.elementValues = null;
48 }
49
54 public override void AggregateElement(IElement Element)
55 {
56 if (this.isDouble && Element is DoubleNumber D)
57 {
58 this.doubleSum += D.Value;
59 this.doubleValues.Add(D.Value);
60 }
61 else if (this.sum is null)
62 {
63 this.isDouble = false;
64 this.elementValues = new ChunkedList<IElement>();
65
66 if (this.doubleValues.Count > 0)
67 {
68 ChunkNode<double> Loop = this.doubleValues.FirstChunk;
69 int i, c;
70
71 while (!(Loop is null))
72 {
73 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
74 this.elementValues.Add(new DoubleNumber(Loop.Elements[i]));
75
76 Loop = Loop.Next;
77 }
78 }
79
80 if (this.sum is null)
81 this.sum = Element;
82 else
83 this.sum = Add.EvaluateAddition(new DoubleNumber(this.doubleSum), Element, this.Node);
84
85 this.elementValues.Add(Element);
86 }
87 else
88 {
89 this.sum = Add.EvaluateAddition(this.sum, Element, this.Node);
90 this.elementValues.Add(Element);
91 }
92
93 this.count++;
94 }
95
99 public override IElement GetAggregatedResult()
100 {
101 if (this.count == 0)
102 return ObjectValue.Null;
103 else if (this.isDouble)
104 {
105 double Average = this.doubleSum / this.count;
106 double Result = 0;
107 double Term;
108
109 ChunkNode<double> Loop = this.doubleValues.FirstChunk;
110 int i, c;
111
112 while (!(Loop is null))
113 {
114 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
115 {
116 Term = Loop[i] - Average;
117 Result += Term * Term;
118 }
119
120 Loop = Loop.Next;
121 }
122
123 return new DoubleNumber(Math.Sqrt(Result / this.count));
124 }
125 else
126 {
127 IElement Average = Divide.EvaluateDivision(this.sum, new DoubleNumber(this.count), this.Node);
128 IElement Result = null;
129 IElement Term;
130
131 ChunkNode<IElement> Loop = this.elementValues.FirstChunk;
132 int i, c;
133
134 while (!(Loop is null))
135 {
136 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
137 {
138 Term = Subtract.EvaluateSubtraction(Loop[i], Average, this.Node);
139 Term = Multiply.EvaluateMultiplication(Term, Term, this.Node);
140
141 if (Result is null)
142 Result = Term;
143 else
144 Result = Add.EvaluateAddition(Result, Term, this.Node);
145 }
146
147 Loop = Loop.Next;
148 }
149
150 if (Result is null)
151 return ObjectValue.Null;
152 else
153 {
154 Result = Divide.EvaluateDivision(Result, new DoubleNumber(this.count), this.Node);
155 return Sqrt.EvaluateSquareRoot(Result, this.Node);
156 }
157 }
158 }
159 }
160}
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
static IElement EvaluateSquareRoot(IElement Operand, ScriptNode Node)
Calculates the square root of an operand.
Definition: Sqrt.cs:95
override IElement GetAggregatedResult()
Gets the aggregated result.
override void AggregateElement(IElement Element)
Aggregates one new element.
StandardDeviationEvaluator(StandardDeviation Node)
StandardDeviation(v) iterative evaluator
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