UnityMol  0.9.6-875
UnityMol viewer / In developement
Vector.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using BenTools.Data;
4 
5 namespace BenTools.Mathematics
6 {
10  public class Vector : IEnumerable, IComparable
11  {
15  public static int Precision = 10;
16  double[] data;
17  public object Tag=null;
22  public Vector(int dim)
23  {
24  data = new double[dim];
25  }
30  public Vector(params double[] X)
31  {
32  data = new double[X.Length];
33  X.CopyTo(data,0);
34  }
39  public Vector(Vector O)
40  : this(O.data)
41  {}
46  public Vector(string S)
47  {
48  if(S[0]!='(' || S[S.Length-1]!=')')
49  throw new Exception("Formatfehler!");
50  string[] P = MathTools.HighLevelSplit(S.Substring(1,S.Length-2),';');
51  data = new double[P.Length];
52  int i;
53  for(i=0;i<data.Length;i++)
54  {
55  data[i] = double.Parse(P[i]);
56  }
57  }
61  public double this [int i]
62  {
63  get
64  {
65  return data[i];
66  }
67  set
68  {
69  data[i] = Math.Round(value,Precision);
70  }
71  }
72 
76  public int Dim
77  {
78  get
79  {
80  return data.Length;
81  }
82  }
83 
87  public double SquaredLength
88  {
89  get
90  {
91  return this*this;
92  }
93  }
94 
98  public double ElementSum
99  {
100  get
101  {
102  int i;
103  double E = 0;
104  for(i=0;i<Dim;i++)
105  E+=data[i];
106  return E;
107  }
108  }
114  public void Randomize(double Min, double Max)
115  {
116  int i;
117  for(i=0;i<data.Length;i++)
118  {
119  this[i] = Min + (Max-Min)*MathTools.R.NextDouble();
120  }
121  }
127  public void Randomize(Vector[] MinMax)
128  {
129  int i;
130  for(i=0;i<data.Length;i++)
131  {
132  this[i] = MinMax[0][i] + (MinMax[1][i]-MinMax[0][i])*MathTools.R.NextDouble();
133  }
134  }
139  public void Multiply(double r)
140  {
141  int i;
142  for(i=0;i<data.Length;i++)
143  {
144  this[i] *= r;
145  }
146  }
151  public void Add(Vector V)
152  {
153  int i;
154  for(i=0;i<data.Length;i++)
155  {
156  this[i] += V[i];
157  }
158  }
163  public void Add(double d)
164  {
165  int i;
166  for(i=0;i<data.Length;i++)
167  {
168  this[i] += d;
169  }
170  }
171  IEnumerator IEnumerable.GetEnumerator()
172  {
173  return data.GetEnumerator();
174  }
175 
180  public override string ToString()
181  {
182  string S = "(";
183  int i;
184  for(i=0;i<data.Length;i++)
185  {
186  S += data[i].ToString("G4");
187  if(i<data.Length-1)
188  S+=";";
189  }
190  S+=")";
191  return S;
192  }
193 
199  public override bool Equals(object obj)
200  {
201  Vector B = obj as Vector;
202  if(B==null || data.Length != B.data.Length)
203  return false;
204  int i;
205  for(i=0;i<data.Length;i++)
206  {
207  if(Math.Abs(data[i]-B.data[i])>1e-10)
208  return false;
209  }
210  return true;
211  }
212 
217  public override int GetHashCode()
218  {
219  int Erg = 0;
220  foreach(double D in data)
221  Erg = Erg ^ Math.Round(D,Precision).GetHashCode();
222  return Erg;
223  }
224 
228  public static Vector operator - (Vector A, Vector B)
229  {
230  if(A.Dim!=B.Dim)
231  throw new Exception("Vectors of different dimension!");
232  Vector Erg = new Vector(A.Dim);
233  int i;
234  for(i=0;i<A.Dim;i++)
235  Erg[i]=A[i]-B[i];
236  return Erg;
237  }
238 
242  public static Vector operator + (Vector A, Vector B)
243  {
244  if(A.Dim!=B.Dim)
245  throw new Exception("Vectors of different dimension!");
246  Vector Erg = new Vector(A.Dim);
247  int i;
248  for(i=0;i<A.Dim;i++)
249  Erg[i]=A[i]+B[i];
250  return Erg;
251  }
252 
256  public static double operator * (Vector A, Vector B)
257  {
258  if(A.Dim!=B.Dim)
259  throw new Exception("Vectors of different dimension!");
260  double Erg = 0;
261  int i;
262  for(i=0;i<A.Dim;i++)
263  Erg+=A[i]*B[i];
264  return Erg;
265  }
266 
270  public static Vector operator * (Vector A, double B)
271  {
272  Vector Erg = new Vector(A.Dim);
273  int i;
274  for(i=0;i<A.Dim;i++)
275  Erg[i]=A[i]*B;
276  return Erg;
277  }
278 
282  public static Vector operator * (double A, Vector B)
283  {
284  return B*A;
285  }
289  public static explicit operator double[](Vector A)
290  {
291  return A.data;
292  }
296  public static double Dist(Vector V1, Vector V2)
297  {
298  if(V1.Dim != V2.Dim)
299  return -1;
300  int i;
301  double E = 0,D;
302  for(i=0;i<V1.Dim;i++)
303  {
304  D=(V1[i]-V2[i]);
305  E+=D*D;
306  }
307  return E;
308 
309  }
310 
314  public int CompareTo(object obj)
315  {
316  Vector A = this;
317  Vector B = obj as Vector;
318  if(A==null || B==null)
319  return 0;
320  double Al,Bl;
321  Al = A.SquaredLength;
322  Bl = B.SquaredLength;
323  if(Al>Bl)
324  return 1;
325  if(Al<Bl)
326  return -1;
327  int i;
328  for(i=0;i<A.Dim;i++)
329  {
330  if(A[i]>B[i])
331  return 1;
332  if(A[i]<B[i])
333  return -1;
334  }
335  return 0;
336  }
341  public virtual Vector Clone()
342  {
343  return new Vector(data);
344  }
345  }
346 
347 }
Vector(int dim)
Build a new vector
Definition: Vector.cs:22
static string[] HighLevelSplit(string S, params char[] C)
Definition: ToolBox.cs:114
A vector class, implementing all interesting features of vectors
Definition: Vector.cs:10
double SquaredLength
The squared length of the vector
Definition: Vector.cs:88
override string ToString()
Convert the vector into a reconstructable string representation
Definition: Vector.cs:180
virtual Vector Clone()
Get a copy of one vector
Definition: Vector.cs:341
void Randomize(Vector[] MinMax)
Reset all elements with ransom values from the given range
Definition: Vector.cs:127
override bool Equals(object obj)
Compares this vector with another one
Definition: Vector.cs:199
int CompareTo(object obj)
Compare two vectors
Definition: Vector.cs:314
void Add(Vector V)
Add another vector
Definition: Vector.cs:151
Vector(params double[] X)
Build a new vector
Definition: Vector.cs:30
void Randomize(double Min, double Max)
Reset all elements with ransom values from the given range
Definition: Vector.cs:114
override int GetHashCode()
Retrieves a hashcode that is dependent on the elements
Definition: Vector.cs:217
int Dim
The dimension of the vector
Definition: Vector.cs:77
static double Dist(Vector V1, Vector V2)
Get the distance of two vectors
Definition: Vector.cs:296
void Add(double d)
Add a constant to all elements
Definition: Vector.cs:163
void Multiply(double r)
Scale all elements by r
Definition: Vector.cs:139
Vector(string S)
Build a new vector from a string
Definition: Vector.cs:46
Vector(Vector O)
Build a new vector as a copy of an existing one
Definition: Vector.cs:39
static readonly Random R
One static Random instance for use in the entire application
Definition: ToolBox.cs:32