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.math.BigDecimal;
30 import java.util.Collection;
31
32
33
34 /**
35 * Static methods operating on the DoubleVector class.
36 *
37 * @version 1.0, Dec 2006
38 * @author Ueli Kurmann, igesture@uelikurmann.ch
39 * @author Beat Signer, signer@inf.ethz.ch
40 */
41 public class BDVectorTools {
42
43 /**
44 * Performs an addition of two double vectors.
45 *
46 * @param v1 the first vector.
47 * @param v2 the second vector.
48 * @return the sum of the two vectors.
49 */
50 public static BigDecimalVector add(BigDecimalVector v1, BigDecimalVector v2) {
51 assert (v1.size() == v2.size());
52 BigDecimalVector result = new BigDecimalVector(v1.size());
53
54 for (int i = 0; i < v1.size(); i++) {
55 result.set(i, v1.get(i).add(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(BigDecimalVector vector, BigDecimal 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 BigDecimalVector scalarDiv(BigDecimalVector vector, int divisor) {
84 BigDecimalVector result = new BigDecimalVector(vector.size());
85
86 BigDecimal divisor2 = new BigDecimal(divisor);
87 for (int i = 0; i < vector.size(); i++) {
88 result.set(i, vector.get(i).divide(divisor2, BigDecimal.ROUND_UP));
89 }
90
91 return result;
92 } // scalarDiv
93
94
95 /**
96 * Computes the sum of a collection of double vectors. By contract the size of
97 * all vectors must be the same.
98 *
99 * @param vectors a collection of double vectors.
100 * @return a double vector containing the sum of all vectors.
101 */
102 public static BigDecimalVector sum(Collection<BigDecimalVector> vectors) {
103 // FIXME Remvoe assert statements, use exceptions
104 assert (vectors.size() > 0);
105 int vectorLenght = vectors.iterator().next().size();
106 BigDecimalVector result = new BigDecimalVector(vectorLenght);
107
108 for (BigDecimalVector vector : vectors) {
109 assert (vector.size() == vectorLenght);
110 result = add(result, vector);
111 }
112
113 return result;
114 } // sum
115
116
117 /**
118 * Computes the mean vector of a set of vectors.
119 *
120 * @param vectors the collection of double vectors whose mean vector has to be computed.
121 * @return the mean vector of the specified set of vectors.
122 */
123 public static BigDecimalVector mean(Collection<BigDecimalVector> vectors) {
124 // FIXME replace assert, use exception handling
125 assert (vectors.size() > 0);
126 int numOfVectors = vectors.size();
127 return scalarDiv(sum(vectors), numOfVectors);
128 } // mean
129
130
131 /**
132 * Computes the LP norm of the vector.
133 *
134 * @param vector the vector to compute the LP norm.
135 * @return the LP norm of the vector.
136 */
137 public static BigDecimal normLP(BigDecimalVector vector) {
138 int dim = vector.size();
139 BigDecimal result = new BigDecimal(0);
140 for (int i = 0; i < dim; i++) {
141 result = result.add(vector.get(i).abs().pow(dim));
142 }
143
144 //FIXME double computation
145 return new BigDecimal(Math.pow(result.doubleValue(), 1d / dim));
146 } // normLP
147
148
149 /**
150 * Normalises a vector (LP norm is used).
151 *
152 * @param vector the vector to be normalised.
153 * @return the normalised vector.
154 */
155 public static BigDecimalVector normalize(BigDecimalVector vector) {
156 BigDecimal norm = normLP(vector);
157 BigDecimalVector result = new BigDecimalVector(vector.size());
158
159 for (int i = 0; i < vector.size(); i++) {
160 result.set(i, vector.get(i).divide(norm));
161 }
162
163 return result;
164 } // normalize
165
166 /**
167 * Tests if all values in the vector are valid. (not NaN, Infinite)
168 * @param vector the vector to test.
169 * @return true if all values are valid.
170 */
171 public static boolean hasValidValues(BigDecimalVector vector){
172 //FIXME
173 return true;
174 }
175
176 }