1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or (at |
---|
5 | * your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, but |
---|
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
10 | * General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
---|
15 | |
---|
16 | /* |
---|
17 | * ChisqMixture.java |
---|
18 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.classifiers.functions.pace; |
---|
23 | |
---|
24 | import weka.core.RevisionHandler; |
---|
25 | import weka.core.RevisionUtils; |
---|
26 | import weka.core.matrix.DoubleVector; |
---|
27 | import weka.core.matrix.FlexibleDecimalFormat; |
---|
28 | import weka.core.matrix.IntVector; |
---|
29 | |
---|
30 | |
---|
31 | /** Class for handling discrete functions. <p> |
---|
32 | * |
---|
33 | * A discrete function here is one that takes non-zero values over a finite |
---|
34 | * set of points. <p> |
---|
35 | * |
---|
36 | * @author Yong Wang (yongwang@cs.waikato.ac.nz) |
---|
37 | * @version $Revision: 1.4 $ */ |
---|
38 | |
---|
39 | public class DiscreteFunction |
---|
40 | implements RevisionHandler { |
---|
41 | |
---|
42 | protected DoubleVector points; |
---|
43 | protected DoubleVector values; |
---|
44 | |
---|
45 | /** Constructs an empty discrete function */ |
---|
46 | public DiscreteFunction() |
---|
47 | { |
---|
48 | this(null, null); |
---|
49 | } |
---|
50 | |
---|
51 | /** Constructs a discrete function with the point values provides and the |
---|
52 | * function values are all 1/n. |
---|
53 | * @param p the point values |
---|
54 | */ |
---|
55 | public DiscreteFunction( DoubleVector p ) |
---|
56 | { |
---|
57 | this( p, null ); |
---|
58 | } |
---|
59 | |
---|
60 | /** Constructs a discrete function with both the point values and |
---|
61 | * function values provided. |
---|
62 | * @param p the point values |
---|
63 | * @param v the function values */ |
---|
64 | public DiscreteFunction( DoubleVector p, DoubleVector v ) |
---|
65 | { |
---|
66 | points = p; |
---|
67 | values = v; |
---|
68 | formalize(); |
---|
69 | } |
---|
70 | |
---|
71 | private DiscreteFunction formalize() |
---|
72 | { |
---|
73 | if( points == null ) points = new DoubleVector(); |
---|
74 | if( values == null ) values = new DoubleVector(); |
---|
75 | |
---|
76 | if( points.isEmpty() ) { |
---|
77 | if( ! values.isEmpty() ) |
---|
78 | throw new IllegalArgumentException("sizes not match"); |
---|
79 | } |
---|
80 | else { |
---|
81 | int n = points.size(); |
---|
82 | if( values.isEmpty() ) { |
---|
83 | values = new DoubleVector( n, 1./n ); |
---|
84 | } |
---|
85 | else { |
---|
86 | if( values.size() != n ) |
---|
87 | throw new IllegalArgumentException("sizes not match"); |
---|
88 | } |
---|
89 | } |
---|
90 | return this; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Normalizes the function values with L1-norm. |
---|
95 | */ |
---|
96 | public DiscreteFunction normalize() |
---|
97 | { |
---|
98 | if ( ! values.isEmpty() ) { |
---|
99 | double s = values.sum(); |
---|
100 | if( s != 0.0 && s != 1.0 ) values.timesEquals( 1. / s ); |
---|
101 | } |
---|
102 | return this; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Sorts the point values of the discrete function. |
---|
107 | */ |
---|
108 | public void sort() |
---|
109 | { |
---|
110 | IntVector index = points.sortWithIndex(); |
---|
111 | values = values.subvector( index ); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Clones the discrete function |
---|
116 | */ |
---|
117 | public Object clone() |
---|
118 | { |
---|
119 | DiscreteFunction d = new DiscreteFunction(); |
---|
120 | d.points = (DoubleVector) points.clone(); |
---|
121 | d.values = (DoubleVector) values.clone(); |
---|
122 | return d; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Makes each individual point value unique |
---|
127 | */ |
---|
128 | public DiscreteFunction unique() |
---|
129 | { |
---|
130 | int count = 0; |
---|
131 | |
---|
132 | if( size() < 2 ) return this; |
---|
133 | for(int i = 1; i <= size() - 1; i++ ) { |
---|
134 | if( points.get( count ) != points.get( i ) ) { |
---|
135 | count++; |
---|
136 | points.set( count, points.get( i ) ); |
---|
137 | values.set( count, values.get( i ) ); |
---|
138 | } |
---|
139 | else { |
---|
140 | values.set( count, values.get(count) + values.get(i) ); |
---|
141 | } |
---|
142 | } |
---|
143 | points = (DoubleVector) points.subvector(0, count); |
---|
144 | values = (DoubleVector) values.subvector(0, count); |
---|
145 | return this; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Returns the size of the point set. |
---|
150 | */ |
---|
151 | public int size() |
---|
152 | { |
---|
153 | if( points == null ) return 0; |
---|
154 | return points.size(); |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Gets a particular point value |
---|
159 | * @param i the index |
---|
160 | */ |
---|
161 | public double getPointValue( int i ) |
---|
162 | { |
---|
163 | return points.get(i); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Gets a particular function value |
---|
168 | * @param i the index |
---|
169 | */ |
---|
170 | public double getFunctionValue( int i ) |
---|
171 | { |
---|
172 | return values.get(i); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Sets a particular point value |
---|
177 | * @param i the index |
---|
178 | */ |
---|
179 | public void setPointValue( int i, double p ) |
---|
180 | { |
---|
181 | points.set(i, p); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Sets a particular function value |
---|
186 | * @param i the index |
---|
187 | */ |
---|
188 | public void setFunctionValue( int i, double v ) |
---|
189 | { |
---|
190 | values.set(i, v); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Gets all point values |
---|
195 | */ |
---|
196 | protected DoubleVector getPointValues() |
---|
197 | { |
---|
198 | return points; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * Gets all function values |
---|
203 | */ |
---|
204 | protected DoubleVector getFunctionValues() |
---|
205 | { |
---|
206 | return values; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Returns true if it is empty. |
---|
211 | */ |
---|
212 | public boolean isEmpty() |
---|
213 | { |
---|
214 | if( size() == 0 ) return true; |
---|
215 | return false; |
---|
216 | } |
---|
217 | |
---|
218 | // public void addPoint( double x, double y ) { |
---|
219 | // points.addPoint( x ); |
---|
220 | // values.addPoint( y ); |
---|
221 | // } |
---|
222 | |
---|
223 | /** |
---|
224 | * Returns the combined of two discrete functions |
---|
225 | * @param d the second discrete function |
---|
226 | * @return the combined discrte function |
---|
227 | */ |
---|
228 | public DiscreteFunction plus( DiscreteFunction d ) |
---|
229 | { |
---|
230 | return ((DiscreteFunction) clone()).plusEquals( d ); |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Returns the combined of two discrete functions. The first function is |
---|
235 | * replaced with the new one. |
---|
236 | * @param d the second discrete function |
---|
237 | * @return the combined discrte function */ |
---|
238 | public DiscreteFunction plusEquals( DiscreteFunction d ) |
---|
239 | { |
---|
240 | points = points.cat( d.points ); |
---|
241 | values = values.cat( d.values ); |
---|
242 | return this; |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * All function values are multiplied by a double |
---|
247 | * @param x the multiplier |
---|
248 | */ |
---|
249 | public DiscreteFunction timesEquals( double x ) |
---|
250 | { |
---|
251 | values.timesEquals( x ); |
---|
252 | return this; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Converts the discrete function to string. |
---|
257 | */ |
---|
258 | public String toString() |
---|
259 | { |
---|
260 | StringBuffer text = new StringBuffer(); |
---|
261 | FlexibleDecimalFormat nf1 = new FlexibleDecimalFormat( 5 ); |
---|
262 | nf1.grouping( true ); |
---|
263 | FlexibleDecimalFormat nf2 = new FlexibleDecimalFormat( 5 ); |
---|
264 | nf2.grouping( true ); |
---|
265 | for(int i = 0; i < size(); i++) { |
---|
266 | nf1.update( points.get(i) ); |
---|
267 | nf2.update( values.get(i) ); |
---|
268 | } |
---|
269 | |
---|
270 | text.append("\t" + nf1.formatString("Points") + |
---|
271 | "\t" + nf2.formatString("Values") + "\n\n"); |
---|
272 | for(int i = 0; i <= size() - 1; i++) { |
---|
273 | text.append( "\t" + nf1.format( points.get(i) ) + "\t" + |
---|
274 | nf2.format( values.get(i) ) + "\n" ); |
---|
275 | } |
---|
276 | |
---|
277 | return text.toString(); |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Returns the revision string. |
---|
282 | * |
---|
283 | * @return the revision |
---|
284 | */ |
---|
285 | public String getRevision() { |
---|
286 | return RevisionUtils.extract("$Revision: 1.4 $"); |
---|
287 | } |
---|
288 | |
---|
289 | public static void main( String args[] ) |
---|
290 | { |
---|
291 | |
---|
292 | double points[] = {2,1,2,3,3}; |
---|
293 | double values[] = {3,2,4,1,3}; |
---|
294 | DiscreteFunction d = new DiscreteFunction( new DoubleVector( points ), |
---|
295 | new DoubleVector( values )); |
---|
296 | System.out.println( d ); |
---|
297 | d.normalize(); |
---|
298 | System.out.println( "d (after normalize) = \n" + d ); |
---|
299 | points[1] = 10; |
---|
300 | System.out.println( "d (after setting [1]) = \n" + d); |
---|
301 | d.sort(); |
---|
302 | System.out.println( "d (after sorting) = \n" + d); |
---|
303 | d.unique(); |
---|
304 | System.out.println( "d (after unique) = \n" + d ); |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|