~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/classifiers/functions/pace/DiscreteFunction.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.matrix.DoubleVector;
 
25
import weka.core.matrix.FlexibleDecimalFormat;
 
26
import weka.core.matrix.IntVector;
 
27
 
 
28
 
 
29
/** Class for handling discrete functions. <p>
 
30
 * 
 
31
 * A discrete function here is one that takes non-zero values over a finite
 
32
 * set of points. <p>
 
33
 * 
 
34
 * @author Yong Wang (yongwang@cs.waikato.ac.nz)
 
35
 * @version $Revision: 1.3 $ */
 
36
 
 
37
public class  DiscreteFunction {
 
38
    
 
39
  protected DoubleVector  points;
 
40
  protected DoubleVector  values;
 
41
 
 
42
  /** Constructs an empty discrete function */
 
43
  public DiscreteFunction() 
 
44
  {
 
45
    this(null, null);
 
46
  }
 
47
    
 
48
  /** Constructs a discrete function with the point values provides and the
 
49
   *  function values are all 1/n. 
 
50
   * @param p the point values
 
51
   */
 
52
  public DiscreteFunction( DoubleVector p ) 
 
53
  {
 
54
    this( p, null );
 
55
  }
 
56
    
 
57
  /** Constructs a discrete function with both the point values and
 
58
   *  function values provided.
 
59
   * @param p the point values
 
60
   * @param v the function values */
 
61
  public DiscreteFunction( DoubleVector p, DoubleVector v ) 
 
62
  {
 
63
    points = p;
 
64
    values = v;
 
65
    formalize();
 
66
  }
 
67
    
 
68
  private DiscreteFunction  formalize() 
 
69
  {
 
70
    if( points == null ) points = new DoubleVector();
 
71
    if( values == null ) values = new DoubleVector();
 
72
        
 
73
    if( points.isEmpty() ) {
 
74
      if( ! values.isEmpty() )
 
75
        throw new IllegalArgumentException("sizes not match");
 
76
    }
 
77
    else {
 
78
      int n = points.size();
 
79
      if( values.isEmpty() ) {
 
80
        values = new DoubleVector( n, 1./n );
 
81
      }
 
82
      else {
 
83
        if( values.size() != n )
 
84
          throw new IllegalArgumentException("sizes not match");
 
85
      }
 
86
    }
 
87
    return this;
 
88
  }
 
89
    
 
90
  /** 
 
91
   * Normalizes the function values with L1-norm.
 
92
   */
 
93
  public DiscreteFunction  normalize() 
 
94
  {
 
95
    if ( ! values.isEmpty() ) {
 
96
      double s = values.sum();
 
97
      if( s != 0.0 && s != 1.0 ) values.timesEquals( 1. / s ); 
 
98
    }
 
99
    return this;
 
100
  }
 
101
  
 
102
  /** 
 
103
   * Sorts the point values of the discrete function.
 
104
   */
 
105
  public void  sort() 
 
106
  {
 
107
    IntVector index = points.sortWithIndex();
 
108
    values = values.subvector( index );
 
109
  }
 
110
  
 
111
  /**
 
112
   * Clones the discrete function
 
113
   */
 
114
  public Object  clone() 
 
115
  {
 
116
    DiscreteFunction d = new DiscreteFunction();
 
117
    d.points = (DoubleVector) points.clone();
 
118
    d.values = (DoubleVector) values.clone();
 
119
    return d;
 
120
  }
 
121
  
 
122
  /**
 
123
   * Makes each individual point value unique 
 
124
   */
 
125
  public DiscreteFunction  unique() 
 
126
  {
 
127
    int count = 0;
 
128
    
 
129
    if( size() < 2 ) return this;
 
130
    for(int i = 1; i <= size() - 1; i++ ) {
 
131
      if( points.get( count ) != points.get( i ) ) {
 
132
        count++;
 
133
        points.set( count, points.get( i ) );
 
134
        values.set( count, values.get( i ) );
 
135
      } 
 
136
      else {
 
137
        values.set( count, values.get(count) + values.get(i) );
 
138
      } 
 
139
    }
 
140
    points = (DoubleVector) points.subvector(0, count);
 
141
    values = (DoubleVector) values.subvector(0, count);
 
142
    return this;
 
143
  }
 
144
 
 
145
  /** 
 
146
   * Returns the size of the point set.
 
147
   */
 
148
  public int  size() 
 
149
  {
 
150
    if( points == null ) return 0;
 
151
    return points.size();
 
152
  }
 
153
  
 
154
  /**
 
155
   * Gets a particular point value
 
156
   * @param i the index
 
157
   */
 
158
  public double  getPointValue( int i ) 
 
159
  {
 
160
    return points.get(i);
 
161
  }
 
162
  
 
163
  /**
 
164
   * Gets a particular function value
 
165
   * @param i the index
 
166
   */
 
167
  public double  getFunctionValue( int i ) 
 
168
  {
 
169
    return values.get(i);
 
170
  }
 
171
    
 
172
  /**
 
173
   * Sets a particular point value
 
174
   * @param i the index
 
175
   */
 
176
  public void  setPointValue( int i, double p )
 
177
  {
 
178
    points.set(i, p);
 
179
  }
 
180
    
 
181
  /**
 
182
   * Sets a particular function value
 
183
   * @param i the index
 
184
   */
 
185
  public void  setFunctionValue( int i, double v )
 
186
  {
 
187
    values.set(i, v);
 
188
  }
 
189
    
 
190
  /**
 
191
   * Gets all point values
 
192
   */
 
193
  protected DoubleVector  getPointValues() 
 
194
  {
 
195
    return points;
 
196
  }
 
197
    
 
198
  /**
 
199
   * Gets all function values
 
200
   */
 
201
  protected DoubleVector  getFunctionValues() 
 
202
  {
 
203
    return values;
 
204
  }
 
205
  
 
206
  /**
 
207
   * Returns true if it is empty.
 
208
   */
 
209
  public boolean  isEmpty() 
 
210
  {
 
211
    if( size() == 0 ) return true;
 
212
    return false;
 
213
  }
 
214
  
 
215
  //    public void  addPoint( double x, double y ) {
 
216
  //      points.addPoint( x );
 
217
  //      values.addPoint( y );
 
218
  //    }
 
219
  
 
220
  /** 
 
221
   * Returns the combined of two discrete functions
 
222
   * @param d the second discrete function
 
223
   * @return the combined discrte function
 
224
   */
 
225
  public DiscreteFunction  plus( DiscreteFunction d ) 
 
226
  {
 
227
    return ((DiscreteFunction) clone()).plusEquals( d );
 
228
  }
 
229
  
 
230
  /** 
 
231
   * Returns the combined of two discrete functions. The first function is
 
232
   * replaced with the new one.
 
233
   * @param d the second discrete function
 
234
   * @return the combined discrte function */
 
235
  public DiscreteFunction  plusEquals( DiscreteFunction d ) 
 
236
  {
 
237
    points = points.cat( d.points );
 
238
    values = values.cat( d.values );
 
239
    return this;
 
240
  }
 
241
  
 
242
  /**
 
243
   * All function values are multiplied by a double
 
244
   * @param x the multiplier
 
245
   */
 
246
  public DiscreteFunction  timesEquals( double x ) 
 
247
  {
 
248
    values.timesEquals( x );
 
249
    return this;
 
250
  }
 
251
 
 
252
  /**
 
253
   * Converts the discrete function to string.
 
254
   */
 
255
  public String  toString() 
 
256
  {
 
257
    StringBuffer text = new StringBuffer();
 
258
    FlexibleDecimalFormat nf1 = new FlexibleDecimalFormat( 5 );
 
259
    nf1.grouping( true ); 
 
260
    FlexibleDecimalFormat nf2 = new FlexibleDecimalFormat( 5 );
 
261
    nf2.grouping( true );
 
262
    for(int i = 0; i < size(); i++) {
 
263
      nf1.update( points.get(i) );
 
264
      nf2.update( values.get(i) );
 
265
    }
 
266
 
 
267
    text.append("\t" + nf1.formatString("Points") + 
 
268
                "\t" + nf2.formatString("Values") + "\n\n");
 
269
    for(int i = 0; i <= size() - 1; i++) {
 
270
      text.append( "\t" + nf1.format( points.get(i) ) + "\t" + 
 
271
                   nf2.format( values.get(i) ) + "\n" );
 
272
    }
 
273
        
 
274
    return text.toString();
 
275
  }
 
276
 
 
277
  public static void main( String args[] )
 
278
  {
 
279
        
 
280
    double points[] = {2,1,2,3,3};
 
281
    double values[] = {3,2,4,1,3};
 
282
    DiscreteFunction d = new DiscreteFunction( new DoubleVector( points ), 
 
283
                                               new DoubleVector( values ));
 
284
    System.out.println( d );
 
285
    d.normalize();
 
286
    System.out.println( "d (after normalize) = \n" + d );
 
287
    points[1] = 10;
 
288
    System.out.println( "d (after setting [1]) = \n" + d);
 
289
    d.sort();
 
290
    System.out.println( "d (after sorting) = \n" + d);
 
291
    d.unique();
 
292
    System.out.println( "d (after unique) = \n" + d );
 
293
  }
 
294
}
 
295