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;
3using System.Reflection;
4using System.Text;
11
13{
17 public sealed class BooleanVector : VectorSpaceElement
18 {
19 private bool[] values;
20 private ICollection<IElement> elements;
21 private readonly int dimension;
22
27 public BooleanVector(params bool[] Values)
28 {
29 this.values = Values;
30 this.elements = null;
31 this.dimension = Values.Length;
32 }
33
38 public BooleanVector(ICollection<IElement> Elements)
39 {
40 this.values = null;
41 this.elements = Elements;
42 this.dimension = Elements.Count;
43 }
44
48 public bool[] Values
49 {
50 get
51 {
52 if (this.values is null)
53 {
54 bool[] v = new bool[this.dimension];
55 int i = 0;
56
57 if (this.elements is ChunkedList<IElement> Values)
58 {
59 ChunkNode<IElement> Loop = Values.FirstChunk;
60 int j, c;
61
62 while (!(Loop is null))
63 {
64 for (j = Loop.Start, c = Loop.Pos; j < c; j++)
65 {
66 if (!(Loop[j].AssociatedObjectValue is bool b))
67 b = false;
68
69 v[i++] = b;
70 }
71
72 Loop = Loop.Next;
73 }
74 }
75 else
76 {
77 foreach (IElement Element in this.elements)
78 {
79 if (!(Element.AssociatedObjectValue is bool b))
80 b = false;
81
82 v[i++] = b;
83 }
84 }
85
86 this.values = v;
87 }
88
89 return this.values;
90 }
91 }
92
96 public ICollection<IElement> Elements
97 {
98 get
99 {
100 if (this.elements is null)
101 {
102 int i;
103 IElement[] v = new IElement[this.dimension];
104
105 for (i = 0; i < this.dimension; i++)
106 v[i] = new BooleanValue(this.values[i]);
107
108 this.elements = v;
109 }
110
111 return this.elements;
112 }
113 }
114
118 public override int Dimension => this.dimension;
119
121 public override string ToString()
122 {
123 StringBuilder sb = null;
124
125 foreach (bool d in this.Values)
126 {
127 if (sb is null)
128 sb = new StringBuilder("[");
129 else
130 sb.Append(", ");
131
132 sb.Append(Expression.ToString(d));
133 }
134
135 if (sb is null)
136 return "[]";
137 else
138 {
139 sb.Append(']');
140 return sb.ToString();
141 }
142 }
143
148 {
149 get
150 {
151 if (this.associatedVectorSpace is null)
152 this.associatedVectorSpace = new BooleanVectors(this.dimension);
153
154 return this.associatedVectorSpace;
155 }
156 }
157
158 private BooleanVectors associatedVectorSpace = null;
159
163 public override object AssociatedObjectValue => this.Values;
164
171 {
173 if (BooleanValue is null)
174 return null;
175
176 bool d = BooleanValue.Value;
177 int i;
178 bool[] Values = this.Values;
179 bool[] v = new bool[this.dimension];
180
181 for (i = 0; i < this.dimension; i++)
182 v[i] = d && Values[i];
183
184 return new BooleanVector(v);
185 }
186
193 {
195 if (BooleanVector is null)
196 return null;
197
198 int i;
199 if (BooleanVector.dimension != this.dimension)
200 return null;
201
202 bool[] Values = this.Values;
203 bool[] Values2 = BooleanVector.Values;
204 bool[] v = new bool[this.dimension];
205 for (i = 0; i < this.dimension; i++)
206 v[i] = Values[i] ^ Values2[i];
207
208 return new BooleanVector(v);
209 }
210
215 public override IGroupElement Negate()
216 {
217 return this;
218 }
219
225 public override bool Equals(object obj)
226 {
228 if (BooleanVector is null)
229 return false;
230
231 int i;
232 if (BooleanVector.dimension != this.dimension)
233 return false;
234
235 bool[] Values = this.Values;
236 bool[] Values2 = BooleanVector.Values;
237 for (i = 0; i < this.dimension; i++)
238 {
239 if (Values[i] != Values2[i])
240 return false;
241 }
242
243 return true;
244 }
245
250 public override int GetHashCode()
251 {
252 bool[] Values = this.Values;
253 int Result = 0;
254 int i;
255
256 for (i = 0; i < this.dimension; i++)
257 Result ^= Values[i].GetHashCode();
258
259 return Result;
260 }
261
265 public override bool IsScalar => false;
266
270 public override ICollection<IElement> ChildElements => this.Elements;
271
279 {
280 return VectorDefinition.Encapsulate(Elements, true, Node);
281 }
282
289 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
290 {
291 return VectorDefinition.Encapsulate(Elements, true, Node);
292 }
293
298 {
299 get
300 {
301 if (this.zero is null)
302 this.zero = new BooleanVector(new bool[this.dimension]);
303
304 return this.zero;
305 }
306 }
307
308 private BooleanVector zero = null;
309
315 public override IElement GetElement(int Index)
316 {
317 if (Index < 0 || Index >= this.dimension)
318 throw new ScriptException("Index out of bounds.");
319
320 bool[] V = this.Values;
321
322 return new BooleanValue(V[Index]);
323 }
324
330 public override void SetElement(int Index, IElement Value)
331 {
332 if (Index < 0 || Index >= this.dimension)
333 throw new ScriptException("Index out of bounds.");
334
335 if (!(Value.AssociatedObjectValue is bool V))
336 throw new ScriptException("Elements in a boolean vector are required to be boolean values.");
337
338 bool[] Values = this.Values;
339 this.elements = null;
340
341 Values[Index] = V;
342 }
343
350 public override bool TryConvertTo(Type DesiredType, out object Value)
351 {
352 if (DesiredType == typeof(bool[]))
353 {
354 Value = this.Values;
355 return true;
356 }
357 else if (DesiredType.IsAssignableFrom(typeof(BooleanVector).GetTypeInfo()))
358 {
359 Value = this;
360 return true;
361 }
362 else
363 return Expression.TryConvert(this.Values, DesiredType, true, out Value);
364 }
365
366 }
367}
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
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
Base class for all types of elements.
Definition: Element.cs:14
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
Base class for all types of vector space elements (vectors).
Base class for script exceptions.
Class managing a script expression.
Definition: Expression.cs:41
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5530
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
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 IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
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