Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Histogram2D.cs
1using System;
8
10{
15 {
16 private static readonly ArgumentType[] argumentTypes7Parameters = new ArgumentType[]
17 {
18 ArgumentType.Matrix, // M
19 ArgumentType.Scalar, // MinX
20 ArgumentType.Scalar, // MaxX
21 ArgumentType.Scalar, // NX
22 ArgumentType.Scalar, // MinY
23 ArgumentType.Scalar, // MaxY
24 ArgumentType.Scalar // NY
25 };
26
42 : base(new ScriptNode[] { Data, MinX, MaxX, NX, MinY, MaxY, NY }, argumentTypes7Parameters, Start, Length, Expression)
43 {
44 }
45
49 public override string FunctionName => nameof(Histogram2D);
50
54 public override string[] DefaultArgumentNames
55 {
56 get { return new string[] { "data", "minX", "maxX", "NX", "minY", "maxY", "NY" }; }
57 }
58
66 {
67 IMatrix Data = Arguments[0] as IMatrix ?? throw new ScriptRuntimeException("First argument must be a matrix.", this);
68 double MinX = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
69 double MaxX = Expression.ToDouble(Arguments[2].AssociatedObjectValue);
70 double dX = Expression.ToDouble(Arguments[3].AssociatedObjectValue);
71 int NX = (int)Math.Round(dX);
72 if (NX <= 0 || NX != dX)
73 throw new ScriptRuntimeException("NX must be a positive integer.", this);
74
75 if (MaxX <= MinX)
76 throw new ScriptRuntimeException("MinX must be smaller than MaxX.", this);
77
78 double MinY = Expression.ToDouble(Arguments[4].AssociatedObjectValue);
79 double MaxY = Expression.ToDouble(Arguments[5].AssociatedObjectValue);
80 double dY = Expression.ToDouble(Arguments[6].AssociatedObjectValue);
81 int NY = (int)Math.Round(dY);
82 if (NY <= 0 || NY != dY)
83 throw new ScriptRuntimeException("NY must be a positive integer.", this);
84
85 if (MaxY <= MinY)
86 throw new ScriptRuntimeException("MinY must be smaller than MaxY.", this);
87
88 double[,] Result = new double[NX, NY];
89 double DiffX = MaxX - MinX;
90 double DiffY = MaxY - MinY;
91 double x, y;
92 int ix, iy, j, c;
93
94 if (Data is DoubleMatrix M)
95 {
96 double[,] w = M.Values;
97
98 if (w.GetLength(0) == 2)
99 {
100 c = w.GetLength(1);
101
102 for (j = 0; j < c; j++)
103 {
104 x = w[0, j];
105 y = w[1, j];
106
107 if (x >= MinX && x <= MaxX && y >= MinY && y <= MaxY)
108 {
109 ix = (int)(((x - MinX) * NX) / DiffX);
110 if (ix == NX)
111 ix--;
112
113 iy = (int)(((y - MinY) * NY) / DiffY);
114 if (iy == NY)
115 iy--;
116
117 Result[ix, iy]++;
118 }
119 }
120 }
121 else if (w.GetLength(1) == 2)
122 {
123 c = w.GetLength(0);
124
125 for (j = 0; j < c; j++)
126 {
127 x = w[j, 0];
128 y = w[j, 1];
129
130 if (x >= MinX && x <= MaxX && y >= MinY && y <= MaxY)
131 {
132 ix = (int)(((x - MinX) * NX) / DiffX);
133 if (ix == NX)
134 ix--;
135
136 iy = (int)(((y - MinY) * NY) / DiffY);
137 if (iy == NY)
138 iy--;
139
140 Result[ix, iy]++;
141 }
142 }
143 }
144 else
145 throw new ScriptRuntimeException("Matrix must have either 2 columns or 2 rows.", this);
146 }
147 else
148 {
149 IElement E;
150
151 if (Data.Rows == 2)
152 {
153 c = Data.Columns;
154
155 for (j = 0; j < c; j++)
156 {
157 E = Data.GetElement(j, 0);
158
159 if (E.AssociatedObjectValue is double x2)
160 x = x2;
161 else
162 {
163 try
164 {
166 }
167 catch (Exception)
168 {
169 continue;
170 }
171 }
172
173 E = Data.GetElement(j, 1);
174
175 if (E.AssociatedObjectValue is double y2)
176 y = y2;
177 else
178 {
179 try
180 {
182 }
183 catch (Exception)
184 {
185 continue;
186 }
187 }
188
189 if (x >= MinX && x <= MaxX && y >= MinY && y <= MaxY)
190 {
191 ix = (int)(((x - MinX) * NX) / DiffX);
192 if (ix == NX)
193 ix--;
194
195 iy = (int)(((y - MinY) * NY) / DiffY);
196 if (iy == NY)
197 iy--;
198
199 Result[ix, iy]++;
200 }
201 }
202 }
203 else if (Data.Columns == 2)
204 {
205 c = Data.Rows;
206
207 for (j = 0; j < c; j++)
208 {
209 E = Data.GetElement(0, j);
210
211 if (E.AssociatedObjectValue is double x2)
212 x = x2;
213 else
214 {
215 try
216 {
218 }
219 catch (Exception)
220 {
221 continue;
222 }
223 }
224
225 E = Data.GetElement(1, j);
226
227 if (E.AssociatedObjectValue is double y2)
228 y = y2;
229 else
230 {
231 try
232 {
234 }
235 catch (Exception)
236 {
237 continue;
238 }
239 }
240
241 if (x >= MinX && x <= MaxX && y >= MinY && y <= MaxY)
242 {
243 ix = (int)(((x - MinX) * NX) / DiffX);
244 if (ix == NX)
245 ix--;
246
247 iy = (int)(((y - MinY) * NY) / DiffY);
248 if (iy == NY)
249 iy--;
250
251 Result[ix, iy]++;
252 }
253 }
254 }
255 else
256 throw new ScriptRuntimeException("Matrix must have either 2 columns or 2 rows.", this);
257 }
258
259 return new ObjectVector(new IElement[]
260 {
261 new ObjectVector(Histogram.GetLabels(NX, MinX, DiffX)),
262 new ObjectVector(Histogram.GetLabels(NY, MinY, DiffY)),
263 new DoubleMatrix(Result)
264 });
265 }
266
267 }
268}
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
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
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Computes a two-dimensional histogram from a set of data.
Definition: Histogram2D.cs:15
override string[] DefaultArgumentNames
Default Argument names
Definition: Histogram2D.cs:55
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Histogram2D.cs:65
override string FunctionName
Name of the function
Definition: Histogram2D.cs:49
Histogram2D(ScriptNode Data, ScriptNode MinX, ScriptNode MaxX, ScriptNode NX, ScriptNode MinY, ScriptNode MaxY, ScriptNode NY, int Start, int Length, Expression Expression)
Computes a two-dimensional histogram from a set of data.
Definition: Histogram2D.cs:40
Computes a histogram from a set of data.
Definition: Histogram.cs:14
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for matrices.
Definition: IMatrix.cs:9
IElement GetElement(int Column, int Row)
Gets an element of the matrix.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9