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.
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.
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. */
18
* Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
22
package weka.classifiers.functions.pace;
24
import weka.core.matrix.DoubleVector;
25
import weka.core.matrix.FlexibleDecimalFormat;
26
import weka.core.matrix.IntVector;
29
/** Class for handling discrete functions. <p>
31
* A discrete function here is one that takes non-zero values over a finite
34
* @author Yong Wang (yongwang@cs.waikato.ac.nz)
35
* @version $Revision: 1.3 $ */
37
public class DiscreteFunction {
39
protected DoubleVector points;
40
protected DoubleVector values;
42
/** Constructs an empty discrete function */
43
public DiscreteFunction()
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
52
public DiscreteFunction( DoubleVector p )
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 )
68
private DiscreteFunction formalize()
70
if( points == null ) points = new DoubleVector();
71
if( values == null ) values = new DoubleVector();
73
if( points.isEmpty() ) {
74
if( ! values.isEmpty() )
75
throw new IllegalArgumentException("sizes not match");
78
int n = points.size();
79
if( values.isEmpty() ) {
80
values = new DoubleVector( n, 1./n );
83
if( values.size() != n )
84
throw new IllegalArgumentException("sizes not match");
91
* Normalizes the function values with L1-norm.
93
public DiscreteFunction normalize()
95
if ( ! values.isEmpty() ) {
96
double s = values.sum();
97
if( s != 0.0 && s != 1.0 ) values.timesEquals( 1. / s );
103
* Sorts the point values of the discrete function.
107
IntVector index = points.sortWithIndex();
108
values = values.subvector( index );
112
* Clones the discrete function
114
public Object clone()
116
DiscreteFunction d = new DiscreteFunction();
117
d.points = (DoubleVector) points.clone();
118
d.values = (DoubleVector) values.clone();
123
* Makes each individual point value unique
125
public DiscreteFunction unique()
129
if( size() < 2 ) return this;
130
for(int i = 1; i <= size() - 1; i++ ) {
131
if( points.get( count ) != points.get( i ) ) {
133
points.set( count, points.get( i ) );
134
values.set( count, values.get( i ) );
137
values.set( count, values.get(count) + values.get(i) );
140
points = (DoubleVector) points.subvector(0, count);
141
values = (DoubleVector) values.subvector(0, count);
146
* Returns the size of the point set.
150
if( points == null ) return 0;
151
return points.size();
155
* Gets a particular point value
158
public double getPointValue( int i )
160
return points.get(i);
164
* Gets a particular function value
167
public double getFunctionValue( int i )
169
return values.get(i);
173
* Sets a particular point value
176
public void setPointValue( int i, double p )
182
* Sets a particular function value
185
public void setFunctionValue( int i, double v )
191
* Gets all point values
193
protected DoubleVector getPointValues()
199
* Gets all function values
201
protected DoubleVector getFunctionValues()
207
* Returns true if it is empty.
209
public boolean isEmpty()
211
if( size() == 0 ) return true;
215
// public void addPoint( double x, double y ) {
216
// points.addPoint( x );
217
// values.addPoint( y );
221
* Returns the combined of two discrete functions
222
* @param d the second discrete function
223
* @return the combined discrte function
225
public DiscreteFunction plus( DiscreteFunction d )
227
return ((DiscreteFunction) clone()).plusEquals( d );
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 )
237
points = points.cat( d.points );
238
values = values.cat( d.values );
243
* All function values are multiplied by a double
244
* @param x the multiplier
246
public DiscreteFunction timesEquals( double x )
248
values.timesEquals( x );
253
* Converts the discrete function to string.
255
public String toString()
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) );
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" );
274
return text.toString();
277
public static void main( String args[] )
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 );
286
System.out.println( "d (after normalize) = \n" + d );
288
System.out.println( "d (after setting [1]) = \n" + d);
290
System.out.println( "d (after sorting) = \n" + d);
292
System.out.println( "d (after unique) = \n" + d );