Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BooleanVector.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Text;
10
12{
16 public sealed class BooleanVector : VectorSpaceElement
17 {
18 private bool[] values;
19 private ICollection<IElement> elements;
20 private readonly int dimension;
21
26 public BooleanVector(params bool[] Values)
27 {
28 this.values = Values;
29 this.elements = null;
30 this.dimension = Values.Length;
31 }
32
37 public BooleanVector(ICollection<IElement> Elements)
38 {
39 this.values = null;
40 this.elements = Elements;
41 this.dimension = Elements.Count;
42 }
43
47 public bool[] Values
48 {
49 get
50 {
51 if (this.values is null)
52 {
53 bool[] v = new bool[this.dimension];
54 int i = 0;
55
56 foreach (IElement Element in this.elements)
57 {
58 if (!(Element.AssociatedObjectValue is bool b))
59 b = false;
60
61 v[i++] = b;
62 }
63
64 this.values = v;
65 }
66
67 return this.values;
68 }
69 }
70
74 public ICollection<IElement> Elements
75 {
76 get
77 {
78 if (this.elements is null)
79 {
80 int i;
81 IElement[] v = new IElement[this.dimension];
82
83 for (i = 0; i < this.dimension; i++)
84 v[i] = new BooleanValue(this.values[i]);
85
86 this.elements = v;
87 }
88
89 return this.elements;
90 }
91 }
92
96 public override int Dimension => this.dimension;
97
99 public override string ToString()
100 {
101 StringBuilder sb = null;
102
103 foreach (bool d in this.Values)
104 {
105 if (sb is null)
106 sb = new StringBuilder("[");
107 else
108 sb.Append(", ");
109
110 sb.Append(Expression.ToString(d));
111 }
112
113 if (sb is null)
114 return "[]";
115 else
116 {
117 sb.Append(']');
118 return sb.ToString();
119 }
120 }
121
126 {
127 get
128 {
129 if (this.associatedVectorSpace is null)
130 this.associatedVectorSpace = new BooleanVectors(this.dimension);
131
132 return this.associatedVectorSpace;
133 }
134 }
135
136 private BooleanVectors associatedVectorSpace = null;
137
141 public override object AssociatedObjectValue => this.Values;
142
149 {
151 if (BooleanValue is null)
152 return null;
153
154 bool d = BooleanValue.Value;
155 int i;
156 bool[] Values = this.Values;
157 bool[] v = new bool[this.dimension];
158
159 for (i = 0; i < this.dimension; i++)
160 v[i] = d && Values[i];
161
162 return new BooleanVector(v);
163 }
164
171 {
173 if (BooleanVector is null)
174 return null;
175
176 int i;
177 if (BooleanVector.dimension != this.dimension)
178 return null;
179
180 bool[] Values = this.Values;
181 bool[] Values2 = BooleanVector.Values;
182 bool[] v = new bool[this.dimension];
183 for (i = 0; i < this.dimension; i++)
184 v[i] = Values[i] ^ Values2[i];
185
186 return new BooleanVector(v);
187 }
188
193 public override IGroupElement Negate()
194 {
195 return this;
196 }
197
203 public override bool Equals(object obj)
204 {
206 if (BooleanVector is null)
207 return false;
208
209 int i;
210 if (BooleanVector.dimension != this.dimension)
211 return false;
212
213 bool[] Values = this.Values;
214 bool[] Values2 = BooleanVector.Values;
215 for (i = 0; i < this.dimension; i++)
216 {
217 if (Values[i] != Values2[i])
218 return false;
219 }
220
221 return true;
222 }
223
228 public override int GetHashCode()
229 {
230 bool[] Values = this.Values;
231 int Result = 0;
232 int i;
233
234 for (i = 0; i < this.dimension; i++)
235 Result ^= Values[i].GetHashCode();
236
237 return Result;
238 }
239
243 public override bool IsScalar
244 {
245 get { return false; }
246 }
247
251 public override ICollection<IElement> ChildElements => this.Elements;
252
259 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
260 {
261 return VectorDefinition.Encapsulate(Elements, true, Node);
262 }
263
268 {
269 get
270 {
271 if (this.zero is null)
272 this.zero = new BooleanVector(new bool[this.dimension]);
273
274 return this.zero;
275 }
276 }
277
278 private BooleanVector zero = null;
279
285 public override IElement GetElement(int Index)
286 {
287 if (Index < 0 || Index >= this.dimension)
288 throw new ScriptException("Index out of bounds.");
289
290 bool[] V = this.Values;
291
292 return new BooleanValue(V[Index]);
293 }
294
300 public override void SetElement(int Index, IElement Value)
301 {
302 if (Index < 0 || Index >= this.dimension)
303 throw new ScriptException("Index out of bounds.");
304
305 if (!(Value.AssociatedObjectValue is bool V))
306 throw new ScriptException("Elements in a boolean vector are required to be boolean values.");
307
308 bool[] Values = this.Values;
309 this.elements = null;
310
311 Values[Index] = V;
312 }
313
320 public override bool TryConvertTo(Type DesiredType, out object Value)
321 {
322 if (DesiredType == typeof(bool[]))
323 {
324 Value = this.Values;
325 return true;
326 }
327 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(BooleanVector).GetTypeInfo()))
328 {
329 Value = this;
330 return true;
331 }
332 else
333 return Expression.TryConvert(this.Values, DesiredType, out Value);
334 }
335
336 }
337}
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
Base class for all types of vector space elements (vectors).
Base class for script exceptions.
Class managing a script expression.
Definition: Expression.cs:39
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Boolean-valued number.
Definition: BooleanValue.cs:12
override IVectorSpaceElement MultiplyScalar(IFieldElement Scalar)
Tries to multiply a scalar to the current element.
override int Dimension
Dimension of vector.
override bool Equals(object obj)
Compares the element to another.
BooleanVector(params bool[] Values)
Boolean-valued vector.
override IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override IElement GetElement(int Index)
Gets an element of the vector.
override bool IsScalar
If the element represents a scalar value.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
ICollection< IElement > Elements
Vector elements.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override int GetHashCode()
Calculates a hash code of the element.
override IVectorSpace AssociatedVectorSpace
Associated Right-VectorSpace.
override object AssociatedObjectValue
Associated object value.
override void SetElement(int Index, IElement Value)
Sets an element in the vector.
BooleanVector(ICollection< IElement > Elements)
Boolean-valued vector.
override IGroupElement Negate()
Negates the element.
Pseudo-vector space of Boolean-valued vectors.
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Basic interface for all types of abelian group elements.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for all types of field elements.
Basic interface for all types of group elements.
Definition: IGroupElement.cs:9
Basic interface for all types of module elements.
Basic interface for all types of modules.
Definition: IVectorSpace.cs:10