Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Median.cs
1using System;
2using System.Collections.Generic;
9
11{
16 {
26 {
27 }
28
32 public override string FunctionName => nameof(Median);
33
41 {
42 double[] v = Argument.Values;
43
44 if (v.Length == 0)
45 return ObjectValue.Null;
46
47 return new DoubleNumber(CalcMedian(Argument.Values, this));
48 }
49
56 public static double CalcMedian(double[] Values, ScriptNode Node)
57 {
58 int c = Values.Length;
59
60 if (c == 0)
61 throw new ScriptRuntimeException("Empty set of values.", Node);
62
63 Array.Sort(Values);
64
65 return Values[c / 2];
66 }
67
75 {
76 ICollection<IElement> Elements = Argument.ChildElements;
77 int c = 0;
78
79 if (c == 0)
80 throw new ScriptRuntimeException("Empty set of values.", this);
81
82 IElement[] A = new IElement[Elements.Count];
83 Elements.CopyTo(A, 0);
84
85 Array.Sort(A, this.Compare);
86
87 return A[c / 2];
88 }
89
90 private int Compare(IElement e1, IElement e2)
91 {
92 if (e1.AssociatedSet is IOrderedSet S1 && e2.AssociatedSet is IOrderedSet S2 && S1 == S2)
93 return S1.Compare(e1, e2);
94 else
95 throw new ScriptRuntimeException("Cannot order elements.", this);
96 }
97
98 }
99}
Class managing a script expression.
Definition: Expression.cs:39
override IElement EvaluateVector(DoubleVector Argument, Variables Variables)
Evaluates the function on a vector argument.
Definition: Median.cs:40
override IElement EvaluateVector(IVector Argument, Variables Variables)
Evaluates the function on a vector argument.
Definition: Median.cs:74
override string FunctionName
Name of the function
Definition: Median.cs:32
Median(ScriptNode Argument, int Start, int Length, Expression Expression)
Median(v)
Definition: Median.cs:24
static double CalcMedian(double[] Values, ScriptNode Node)
Returns the median value.
Definition: Median.cs:56
ScriptNode Argument
Function argument.
Base class for funcions of one vector variable.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11