Debian Codegraph @

src/dr/math/matrixAlgebra/Vector.java

raw symbols SCIP
1 /*
2 * Vector.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.matrixAlgebra;
27
28 /**
29 * Vector implementation
30 *
31 * @author Didier H. Besset
32 */
33 public class Vector {
34 protected double[] components;
35
36 /**
37 * Create a vector of given dimension.
38 * NOTE: The supplied array of components must not be changed.
39 *
40 * @param comp double[]
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 // System.arraycopy( comp, 0, components, 0, n);
58 for (int i = 0; i < n; i++)
59 components[i] = comp[i];
60
61
62 }
63
64 /**
65 * Create a vector of given dimension.
66 *
67 * @param dimension int dimension of the vector; must be positive.
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 // clear(); // Java automatically zeros new vectors
75 }
76
77 /**
78 * @param v DHBmatrixAlgebra.DhbVector
79 * @throws DHBmatrixAlgebra.DhbIllegalDimension
80 * if the vector
81 * and supplied vector do not have the same dimension.
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 * @param v DHBmatrixAlgebra.DhbVector
94 * @throws DHBmatrixAlgebra.DhbIllegalDimension
95 * if the vector
96 * and supplied vector do not have the same dimension.
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 * @param v DHBmatrixAlgebra.DhbVector
109 * @throws DHBmatrixAlgebra.DhbIllegalDimension
110 * if the vector
111 * and supplied vector do not have the same dimension.
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 * @param v DHBmatrixAlgebra.DhbVector
124 * @throws DHBmatrixAlgebra.DhbIllegalDimension
125 * if the vector
126 * and supplied vector do not have the same dimension.
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 * @param v DHBmatrixAlgebra.DhbVector
139 * @return DHBmatrixAlgebra.DhbVector sum of the vector with
140 * the supplied vector
141 * @throws DHBmatrixAlgebra.DhbIllegalDimension
142 * if the vector
143 * and supplied vector do not have the same dimension.
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 * Sets all components of the receiver to 0.
158 */
159 public void clear() {
160 for (int i = 0; i < components.length; i++) components[i] = 0;
161 }
162
163 /**
164 * @param n int
165 * @return double
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 * Returns the dimension of the vector.
177 *
178 * @return int
179 */
180 public int dimension() {
181 return components.length;
182 }
183
184 /**
185 * @param v DHBmatrixAlgebra.DhbVector
186 * @return true if the supplied vector is equal to the receiver
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 * Computes the norm of a vector.
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 * @param x double
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 * Computes the product of the vector by a number.
220 *
221 * @param d double
222 * @return DHBmatrixAlgebra.DhbVector
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 * Compute the scalar product (or dot product) of two vectors.
233 *
234 * @param v DHBmatrixAlgebra.DhbVector
235 * @return double the scalar product of the receiver with the argument
236 * @throws DHBmatrixAlgebra.DhbIllegalDimension
237 * if the dimension
238 * of v is not the same.
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 * Computes the product of the transposed vector with a matrix
251 *
252 * @param a MatrixAlgebra.Matrix
253 * @return MatrixAlgebra.DhbVector
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 * @param x double
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 * Compute the scalar product (or dot product) of two vectors.
277 * No dimension checking is made.
278 *
279 * @param v DHBmatrixAlgebra.DhbVector
280 * @return double the scalar product of the receiver with the argument
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 * Computes the product of the transposed vector with a matrix
291 *
292 * @param a MatrixAlgebra.Matrix
293 * @return MatrixAlgebra.DhbVector
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 * @param v DHBmatrixAlgebra.DhbVector
309 * @return DHBmatrixAlgebra.DhbVector subtract the supplied vector
310 * to the receiver
311 * @throws DHBmatrixAlgebra.DhbIllegalDimension
312 * if the vector
313 * and supplied vector do not have the same dimension.
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 * @param v MatrixAlgebra.DhbVector second vector to build tensor
328 * product with.
329 * @return MatrixAlgebra.Matrix tensor product with the specified
330 * vector
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 * @return double[] a copy of the components of the receiver.
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 * Returns a string representation of the vector.
356 *
357 * @return java.lang.String
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 }