1 /*
2 * @(#)$Id: VectorTools.java 689 2009-07-22 00:10:27Z bsigner $
3 *
4 * Author : Ueli Kurmann, igesture@uelikurmann.ch
5 *
6 * Purpose : Static methods operating on the DoubleVector class.
7 *
8 * -----------------------------------------------------------------------
9 *
10 * Revision Information:
11 *
12 * Date Who Reason
13 *
14 * Dec 26, 2006 ukurmann Initial Release
15 * Mar 22, 2007 bsigner Cleanup
16 *
17 * -----------------------------------------------------------------------
18 *
19 * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20 *
21 * This software is the proprietary information of ETH Zurich.
22 * Use is subject to license terms.
23 *
24 */
25
26
27 package org.ximtec.igesture.util;
28
29 import java.util.Collection;
30
31
32
33 /**
34 * Static methods operating on the DoubleVector class.
35 *
36 * @version 1.0, Dec 2006
37 * @author Ueli Kurmann, igesture@uelikurmann.ch
38 * @author Beat Signer, signer@inf.ethz.ch
39 */
40 public class VectorTools {
41
42 /**
43 * Performs an addition of two double vectors.
44 *
45 * @param v1 the first vector.
46 * @param v2 the second vector.
47 * @return the sum of the two vectors.
48 */
49 public static DoubleVector add(DoubleVector v1, DoubleVector v2) {
50 // FIXME remove assert statement. Exception handling?
51 assert (v1.size() == v2.size());
52 DoubleVector result = new DoubleVector(v1.size());
53
54 for (int i = 0; i < v1.size(); i++) {
55 result.set(i, v1.get(i) + v2.get(i));
56 }
57
58 return result;
59 } // add
60
61
62 /**
63 * Initialises the vector with a given value.
64 *
65 * @param vector the vector to be initialised.
66 * @param initValue the initialisation value.
67 */
68 public static void init(DoubleVector vector, double initValue) {
69 for (int i = 0; i < vector.size(); i++) {
70 vector.add(i, initValue);
71 }
72
73 } // init
74
75
76 /**
77 * Computes the scalar division of a double vector.
78 *
79 * @param vector the vector.
80 * @param divisor the divisor.
81 * @return the vector divided by divisor.
82 */
83 public static DoubleVector scalarDiv(DoubleVector vector, double divisor) {
84 DoubleVector result = new DoubleVector(vector.size());
85
86 for (int i = 0; i < vector.size(); i++) {
87 result.set(i, vector.get(i) / divisor);
88 }
89
90 return result;
91 } // scalarDiv
92
93
94 /**
95 * Computes the sum of a collection of double vectors. By contract the size of
96 * all vectors must be the same.
97 *
98 * @param vectors a collection of double vectors.
99 * @return a double vector containing the sum of all vectors.
100 */
101 public static DoubleVector sum(Collection<DoubleVector> vectors) {
102 // FIXME Remvoe assert statements, use exceptions
103 assert (vectors.size() > 0);
104 int vectorLenght = vectors.iterator().next().size();
105 DoubleVector result = new DoubleVector(vectorLenght);
106
107 for (DoubleVector vector : vectors) {
108 assert (vector.size() == vectorLenght);
109 result = add(result, vector);
110 }
111
112 return result;
113 } // sum
114
115
116 /**
117 * Computes the mean vector of a set of vectors.
118 *
119 * @param vectors the collection of double vectors whose mean vector has to be computed.
120 * @return the mean vector of the specified set of vectors.
121 */
122 public static DoubleVector mean(Collection<DoubleVector> vectors) {
123 // FIXME replace assert, use exception handling
124 assert (vectors.size() > 0);
125 int numOfVectors = vectors.size();
126 return scalarDiv(sum(vectors), numOfVectors);
127 } // mean
128
129
130 /**
131 * Computes the LP norm of the vector.
132 *
133 * @param vector the vector to compute the LP norm.
134 * @return the LP norm of the vector.
135 */
136 public static double normLP(DoubleVector vector) {
137 int dim = vector.size();
138 double result = 0;
139 for (int i = 0; i < dim; i++) {
140 result += Math.pow(Math.abs(vector.get(i)), dim);
141 }
142 return Math.pow(result, 1d / dim);
143 } // normLP
144
145
146 /**
147 * Normalises a vector (LP norm is used).
148 *
149 * @param vector the vector to be normalised.
150 * @return the normalised vector.
151 */
152 public static DoubleVector normalize(DoubleVector vector) {
153 double norm = normLP(vector);
154 DoubleVector result = new DoubleVector(vector.size());
155
156 for (int i = 0; i < vector.size(); i++) {
157 result.set(i, vector.get(i) / norm);
158 }
159
160 return result;
161 } // normalize
162
163 /**
164 * Tests if all values in the vector are valid. (not NaN, Infinite)
165 * @param vector the vector to test.
166 * @return true if all values are valid.
167 */
168 public static boolean hasValidValues(DoubleVector vector){
169 for(double d:vector){
170 if(Double.isNaN(d) || Double.isInfinite(d)){
171 return false;
172 }
173 }
174 return true;
175 }
176
177 }