| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math.matrixAlgebra; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class Vector { |
| 34 |
protected double[] components; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public Vector(double comp[]) throws NegativeArraySizeException { |
| 43 |
int n = comp.length; |
| 44 |
if (n <= 0) |
| 45 |
throw new NegativeArraySizeException( |
| 46 |
"Vector components cannot be empty"); |
| 47 |
components = new double[n]; |
| 48 |
System.arraycopy(comp, 0, components, 0, n); |
| 49 |
} |
| 50 |
|
| 51 |
public Vector(int comp[]) throws NegativeArraySizeException { |
| 52 |
int n = comp.length; |
| 53 |
if (n <= 0) |
| 54 |
throw new NegativeArraySizeException( |
| 55 |
"Vector components cannot be empty"); |
| 56 |
components = new double[n]; |
| 57 |
|
| 58 |
for (int i = 0; i < n; i++) |
| 59 |
components[i] = comp[i]; |
| 60 |
|
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Vector(int dimension) throws NegativeArraySizeException { |
| 70 |
if (dimension <= 0) |
| 71 |
throw new NegativeArraySizeException( |
| 72 |
"Requested vector size: " + dimension); |
| 73 |
components = new double[dimension]; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void accumulate(double[] x) throws IllegalDimension { |
| 84 |
if (this.dimension() != x.length) |
| 85 |
throw new IllegalDimension("Attempt to add a " |
| 86 |
+ this.dimension() + "-dimension vector to a " |
| 87 |
+ x.length + "-dimension array"); |
| 88 |
for (int i = 0; i < this.dimension(); i++) |
| 89 |
components[i] += x[i]; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public void accumulate(Vector v) throws IllegalDimension { |
| 99 |
if (this.dimension() != v.dimension()) |
| 100 |
throw new IllegalDimension("Attempt to add a " |
| 101 |
+ this.dimension() + "-dimension vector to a " |
| 102 |
+ v.dimension() + "-dimension vector"); |
| 103 |
for (int i = 0; i < this.dimension(); i++) |
| 104 |
components[i] += v.components[i]; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void accumulateNegated(double[] x) throws IllegalDimension { |
| 114 |
if (this.dimension() != x.length) |
| 115 |
throw new IllegalDimension("Attempt to add a " |
| 116 |
+ this.dimension() + "-dimension vector to a " |
| 117 |
+ x.length + "-dimension array"); |
| 118 |
for (int i = 0; i < this.dimension(); i++) |
| 119 |
components[i] -= x[i]; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void accumulateNegated(Vector v) throws IllegalDimension { |
| 129 |
if (this.dimension() != v.dimension()) |
| 130 |
throw new IllegalDimension("Attempt to add a " |
| 131 |
+ this.dimension() + "-dimension vector to a " |
| 132 |
+ v.dimension() + "-dimension vector"); |
| 133 |
for (int i = 0; i < this.dimension(); i++) |
| 134 |
components[i] -= v.components[i]; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public Vector add(Vector v) throws IllegalDimension { |
| 146 |
if (this.dimension() != v.dimension()) |
| 147 |
throw new IllegalDimension("Attempt to add a " |
| 148 |
+ this.dimension() + "-dimension vector to a " |
| 149 |
+ v.dimension() + "-dimension vector"); |
| 150 |
double[] newComponents = new double[this.dimension()]; |
| 151 |
for (int i = 0; i < this.dimension(); i++) |
| 152 |
newComponents[i] = components[i] + v.components[i]; |
| 153 |
return new Vector(newComponents); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public void clear() { |
| 160 |
for (int i = 0; i < components.length; i++) components[i] = 0; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public double component(int n) { |
| 168 |
return components[n]; |
| 169 |
} |
| 170 |
|
| 171 |
public void set(int n, double value) { |
| 172 |
components[n] = value; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public int dimension() { |
| 181 |
return components.length; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public boolean equals(Vector v) { |
| 189 |
int n = this.dimension(); |
| 190 |
if (v.dimension() != n) |
| 191 |
return false; |
| 192 |
for (int i = 0; i < n; i++) { |
| 193 |
if (v.components[i] != components[i]) |
| 194 |
return false; |
| 195 |
} |
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public double norm() { |
| 203 |
double sum = 0; |
| 204 |
for (int i = 0; i < components.length; i++) |
| 205 |
sum += components[i] * components[i]; |
| 206 |
return Math.sqrt(sum); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public Vector normalizedBy(double x) { |
| 213 |
for (int i = 0; i < this.dimension(); i++) |
| 214 |
components[i] /= x; |
| 215 |
return this; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public Vector product(double d) { |
| 225 |
double newComponents[] = new double[components.length]; |
| 226 |
for (int i = 0; i < components.length; i++) |
| 227 |
newComponents[i] = d * components[i]; |
| 228 |
return new Vector(newComponents); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public double product(Vector v) throws IllegalDimension { |
| 241 |
int n = v.dimension(); |
| 242 |
if (components.length != n) |
| 243 |
throw new IllegalDimension( |
| 244 |
"Dot product with mismatched dimensions: " |
| 245 |
+ components.length + ", " + n); |
| 246 |
return secureProduct(v); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public Vector product(Matrix a) throws IllegalDimension { |
| 256 |
int n = a.rows(); |
| 257 |
int m = a.columns(); |
| 258 |
if (this.dimension() != n) |
| 259 |
throw new IllegalDimension( |
| 260 |
"Product error: transposed of a " + this.dimension() |
| 261 |
+ "-dimension vector cannot be multiplied with a " |
| 262 |
+ n + " by " + m + " matrix"); |
| 263 |
return secureProduct(a); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public Vector scaledBy(double x) { |
| 270 |
for (int i = 0; i < this.dimension(); i++) |
| 271 |
components[i] *= x; |
| 272 |
return this; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
protected double secureProduct(Vector v) { |
| 283 |
double sum = 0; |
| 284 |
for (int i = 0; i < v.dimension(); i++) |
| 285 |
sum += components[i] * v.components[i]; |
| 286 |
return sum; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
protected Vector secureProduct(Matrix a) { |
| 296 |
int n = a.rows(); |
| 297 |
int m = a.columns(); |
| 298 |
double[] vectorComponents = new double[m]; |
| 299 |
for (int j = 0; j < m; j++) { |
| 300 |
vectorComponents[j] = 0; |
| 301 |
for (int i = 0; i < n; i++) |
| 302 |
vectorComponents[j] += components[i] * a.components[i][j]; |
| 303 |
} |
| 304 |
return new Vector(vectorComponents); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public Vector subtract(Vector v) throws IllegalDimension { |
| 316 |
if (this.dimension() != v.dimension()) |
| 317 |
throw new IllegalDimension("Attempt to add a " |
| 318 |
+ this.dimension() + "-dimension vector to a " |
| 319 |
+ v.dimension() + "-dimension vector"); |
| 320 |
double[] newComponents = new double[this.dimension()]; |
| 321 |
for (int i = 0; i < this.dimension(); i++) |
| 322 |
newComponents[i] = components[i] - v.components[i]; |
| 323 |
return new Vector(newComponents); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public Matrix tensorProduct(Vector v) { |
| 333 |
int n = dimension(); |
| 334 |
int m = v.dimension(); |
| 335 |
double[][] newComponents = new double[n][m]; |
| 336 |
for (int i = 0; i < n; i++) { |
| 337 |
for (int j = 0; j < m; j++) |
| 338 |
newComponents[i][j] = components[i] * v.components[j]; |
| 339 |
} |
| 340 |
return n == m ? new SymmetricMatrix(newComponents) |
| 341 |
: new Matrix(newComponents); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public double[] toComponents() { |
| 348 |
int n = dimension(); |
| 349 |
double[] answer = new double[n]; |
| 350 |
System.arraycopy(components, 0, answer, 0, n); |
| 351 |
return answer; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public String toString() { |
| 360 |
StringBuffer sb = new StringBuffer(); |
| 361 |
char[] separator = {'[', ' '}; |
| 362 |
for (int i = 0; i < components.length; i++) { |
| 363 |
sb.append(separator); |
| 364 |
sb.append(components[i]); |
| 365 |
separator[0] = ','; |
| 366 |
} |
| 367 |
sb.append(']'); |
| 368 |
return sb.toString(); |
| 369 |
} |
| 370 |
|
| 371 |
public static Vector buildOneTimesElementVector(int dim, double element){ |
| 372 |
double[] component=new double[dim]; |
| 373 |
for (int i = 0; i <dim ; i++) { |
| 374 |
component[i]=element; |
| 375 |
} |
| 376 |
return new Vector(component); |
| 377 |
} |
| 378 |
} |