00001
00002
00003
00004
00005
00006 namespace NewGamePhysics.Mathematics
00007 {
00008 using System;
00009
00015 public class VectorN
00016 {
00020 int n;
00021
00025 double [] x;
00026
00032 public VectorN(int n)
00033 {
00034 if (n < 0)
00035 {
00036 throw new ArgumentOutOfRangeException("n","Size must be greater than zero.");
00037 }
00038
00039 this.n = n;
00040 this.x = new double[n];
00041 }
00042
00047 public VectorN(VectorN a)
00048 {
00049 this.n = a.n;
00050 this.x = new double[n];
00051 for (int i = 0; i < this.n; i++)
00052 {
00053 this.x[i] = a.x[i];
00054 }
00055 }
00056
00060 public int N
00061 {
00062 get
00063 {
00064 return this.n;
00065 }
00066 }
00067
00073 public double this[int i]
00074 {
00075 get
00076 {
00077 if (i < 0 || i >= this.n)
00078 {
00079 throw new IndexOutOfRangeException("Index out of range.");
00080 }
00081
00082 return this.x[i];
00083 }
00084
00085 set
00086 {
00087 if (i < 0 || i >= this.n)
00088 {
00089 throw new IndexOutOfRangeException("Index out of range.");
00090 }
00091
00092 this.x[i] = value;
00093 }
00094 }
00095
00100 public VectorN Copy()
00101 {
00102 VectorN r = new VectorN(this.n);
00103 for (int i = 0; i < this.n; i++)
00104 {
00105 r.x[i] = this.x[i];
00106 }
00107
00108 return r;
00109 }
00110
00116 public double Abs()
00117 {
00118 double r = 0;
00119 for (int i = 0; i < this.n; i++)
00120 {
00121 double v = this.x[i];
00122 r += v * v;
00123 }
00124
00125 r = Math.Sqrt(r);
00126 return r;
00127 }
00128
00135 public static VectorN operator +(VectorN a, VectorN b)
00136 {
00137 if (a.n != b.n)
00138 {
00139 throw new ArgumentException("Vectors must be of same size");
00140 }
00141
00142 VectorN r = new VectorN(a.n);
00143 for (int i = 0; i < a.n; i++)
00144 {
00145 r.x[i] = a.x[i] + b.x[i];
00146 }
00147
00148 return r;
00149 }
00150
00157 public static VectorN operator -(VectorN a, VectorN b)
00158 {
00159 if (a.n != b.n)
00160 {
00161 throw new ArgumentException("Vectors must be of same size");
00162 }
00163
00164 VectorN r = new VectorN(a.n);
00165 for (int i = 0; i < a.n; i++)
00166 {
00167 r.x[i] = a.x[i] - b.x[i];
00168 }
00169
00170 return r;
00171 }
00172
00179 public static VectorN operator *(double c, VectorN a)
00180 {
00181 VectorN r = new VectorN(a.n);
00182 for (int i = 0; i < a.n; i++)
00183 {
00184 r.x[i] = c * a.x[i];
00185 }
00186
00187 return r;
00188 }
00189
00196 public static VectorN operator *(VectorN a, double c)
00197 {
00198 return c * a;
00199 }
00200
00207 public static VectorN operator /(double c, VectorN a)
00208 {
00209 for (int i = 0; i < a.n; i++)
00210 {
00211 if (a[i] == 0.0)
00212 {
00213 throw new ArgumentOutOfRangeException("a", "All components must be non-zero");
00214 }
00215 }
00216
00217 VectorN r = new VectorN(a.n);
00218 for (int i = 0; i < a.n; i++)
00219 {
00220 r.x[i] = c / a.x[i];
00221 }
00222
00223 return r;
00224 }
00225
00232 public static VectorN operator /(VectorN a, double c)
00233 {
00234 if (c == 0.0)
00235 {
00236 throw new ArgumentOutOfRangeException("c", "Must be non-zero");
00237 }
00238
00239 double d = 1.0 / c;
00240 return d * a;
00241 }
00242 }
00243 }