KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > wavelet > DiscreteHilbertSpace


1 package JSci.maths.wavelet;
2
3 import JSci.maths.*;
4 import JSci.maths.vectors.AbstractDoubleVector;
5 import JSci.maths.vectors.DoubleSparseVector;
6 import JSci.maths.vectors.DoubleVector;
7 import JSci.maths.wavelet.*;
8 import JSci.maths.wavelet.splines.*;
9
10 /****************************************
11 * This class provides support for basic
12 * operations on MultiscaleFunction.
13 * Whenever it isn't specified we are working
14 * over the interval [0,1]
15 * Be careful when using integrate...
16 * Indeed, integrate(f,g) is not, in
17 * general, the same thing as integrate(g,f).
18 * @author Daniel Lemire
19 *****************************************/

20 public final class DiscreteHilbertSpace {
21         private DiscreteHilbertSpace() {}
22
23   /*************************************
24   * Maximum number of allowed iterations
25   * in order to match objects.
26   **************************************/

27     public static int maxiter=20;
28
29     /***********************************
30     * Calculates f+a*g. Careful before
31     * using this method since it uses
32     * a specific normalisation. Useful
33     * for matching pursuit purpose.
34     ************************************/

35     public static double[] add(MultiscaleFunction f,double a ,MultiscaleFunction g,int j1) {
36         if(j1<0) {
37                         throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
38         }
39         if (j1>maxiter) {
40             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
41         }
42         int[] Compatibilite=getScalingCoefficients(f,g,j1);
43         int K=Compatibilite[0];
44         int Ks=Compatibilite[1];
45                 double[] v1=ArrayMath.scalarMultiply(Cascades.PowerOf2(K+j1),f.evaluate(Ks+j1));
46                 double[] v2prime=ArrayMath.scalarMultiply(Cascades.PowerOf2(Ks+j1),g.evaluate(K+j1));
47                 double[] v2=ArrayMath.scalarMultiply(a,v2prime);
48         return(ArrayMath.add(v1,v2));
49         }
50     /***********************************
51     * Calculates f+a*g. Careful before
52     * using this method since it uses
53     * a specific normalisation. Useful
54     * for matching pursuit purpose.
55     ************************************/

56     public static AbstractDoubleVector add(SparseDiscreteFunction f,double a ,MultiscaleFunction g, int j1) {
57         if(j1<0) {
58                         throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
59         }
60         if (j1>maxiter) {
61                         throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
62         }
63         int[] Compatibilite=getScalingCoefficients(f,g,j1);
64         int K=Compatibilite[0];
65         int Ks=Compatibilite[1];
66                 AbstractDoubleVector v1=f.data.scalarMultiply((double) Cascades.PowerOf2(K+j1));
67                 double[] v2prime=ArrayMath.scalarMultiply(Cascades.PowerOf2(Ks+j1),g.evaluate(K+j1));
68                 double[] v2=ArrayMath.scalarMultiply(a,v2prime);
69                 return(v1.add(new DoubleSparseVector(v2)));
70     }
71
72     /***********************************
73     * Calculates f+a*g. Careful before
74     * using this method since it uses
75     * a specific normalisation. Useful
76     * for matching pursuit purpose.
77     ************************************/

78     public static AbstractDoubleVector add (MultiscaleFunction g,double a ,SparseDiscreteFunction f, int j1) {
79                 return(add(f,a,g,j1));
80         }
81
82     /***********************************
83     * Calculates f+a*g. Careful before
84     * using this method since it uses
85     * a specific normalisation. Useful
86     * for matching pursuit purpose.
87     ************************************/

88     public static double[] add(DiscreteFunction f,double a ,DiscreteFunction g) {
89                 if((g instanceof SparseDiscreteFunction)&&(f instanceof SparseDiscreteFunction)) {
90                         integrate((SparseDiscreteFunction)f,(SparseDiscreteFunction)g);
91                 }
92                 if(g instanceof SparseDiscreteFunction) {
93                         integrate(f,(SparseDiscreteFunction)g) ;
94                 }
95                 if(f instanceof SparseDiscreteFunction) {
96                         integrate((SparseDiscreteFunction)f,g) ;
97                 }
98                 double[] v1=f.evaluate();
99                 double[] v2prime=g.evaluate(0);
100                 double[] v2=ArrayMath.scalarMultiply(a,v2prime);
101         return(ArrayMath.add(v1,v2));
102     }
103     /***********************************
104     * Calculates f+a*g. Careful before
105     * using this method since it uses
106     * a specific normalisation. Useful
107     * for matching pursuit purpose.
108     ************************************/

109     public static AbstractDoubleVector add (SparseDiscreteFunction f,double a ,SparseDiscreteFunction g) {
110                 AbstractDoubleVector v2=g.data.scalarMultiply(a);
111         return(v2.add(f.data));
112     }
113     /***********************************
114     * Calculates f+a*g. Careful before
115     * using this method since it uses
116     * a specific normalisation. Useful
117     * for matching pursuit purpose.
118     ************************************/

119     public static AbstractDoubleVector add (DiscreteFunction f,double a ,SparseDiscreteFunction g) {
120                 if(f instanceof SparseDiscreteFunction) {
121                         integrate((SparseDiscreteFunction)f,g);
122                 }
123                 AbstractDoubleVector v2=g.data.scalarMultiply(a);
124                 return(v2.add(f.data));
125     }
126     /***********************************
127     * Calculates f+a*g. Careful before
128     * using this method since it uses
129     * a specific normalisation. Useful
130     * for matching pursuit purpose.
131     ************************************/

132     public static AbstractDoubleVector add (SparseDiscreteFunction f,double a ,DiscreteFunction g) {
133                 if(g instanceof SparseDiscreteFunction) {
134                         integrate(f,(SparseDiscreteFunction)f) ;
135                 }
136         return(add(g,a,f));
137     }
138
139
140     /***********************************************
141     ************************************************/

142     public static double integrate (MultiscaleFunction f,MultiscaleFunction g, int j1, double a, double b) {
143         if(j1<0) {
144                         throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
145         }
146         if(j1>maxiter) {
147             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
148         }
149         int[] Compatibilite=getScalingCoefficients(f,g,j1);
150         int K=Compatibilite[0];
151         int Ks=Compatibilite[1];
152         double massbrute=integrate(f,g,j1,K,Ks);
153         double mass=massbrute*Math.abs(b-a);//Normalisation
154
return(mass);
155     }
156
157     public static double integrate (DiscreteFunction f,DiscreteFunction g) {
158 // System.out.println(f.evaluate().length+" "+g.evaluate().length);
159
if((f instanceof SparseDiscreteFunction)&&(g instanceof SparseDiscreteFunction)) {
160                         integrate((SparseDiscreteFunction)f,(SparseDiscreteFunction)g);
161                 }
162                 if(f instanceof SparseDiscreteFunction) {
163                         integrate((SparseDiscreteFunction)f,g);
164                 }
165                 if(g instanceof SparseDiscreteFunction) {
166                         integrate(f,(SparseDiscreteFunction)g);
167                 }
168         return f.data.scalarProduct(g.data);
169     }
170     public static double integrate (SparseDiscreteFunction f,DiscreteFunction g) {
171                 if(g instanceof SparseDiscreteFunction) {
172                         integrate(f,(SparseDiscreteFunction)g) ;
173                 }
174         return f.data.scalarProduct(g.data);
175     }
176     public static double integrate (DiscreteFunction f,SparseDiscreteFunction g) {
177                 if(f instanceof SparseDiscreteFunction) {
178                         integrate((SparseDiscreteFunction)f,g) ;
179                 }
180         return g.data.scalarProduct(f.data);
181     }
182   /************************
183   * This method could be
184   * optimized further
185   *************************/

186     public static double integrate(SparseDiscreteFunction f,SparseDiscreteFunction g) {
187         return g.data.scalarProduct(f.data);
188     }
189
190     public static double integrate(Cosine f,DiscreteFunction g) {
191                 if(g instanceof SparseDiscreteFunction) {
192                         integrate(f,(SparseDiscreteFunction)g) ;
193                 }
194         return ArrayMath.scalarProduct(f.evaluate(),g.evaluate());
195     }
196
197     public static double integrate(Sine f,DiscreteFunction g) {
198                 if(g instanceof SparseDiscreteFunction) {
199                         integrate(f,(SparseDiscreteFunction)g) ;
200                 }
201         return ArrayMath.scalarProduct(f.evaluate(),g.evaluate());
202     }
203
204     public static double integrate(DiscreteFunction f,Sine g) {
205                 if(f instanceof SparseDiscreteFunction) {
206                         integrate((SparseDiscreteFunction)f,g) ;
207                 }
208         return ArrayMath.scalarProduct(f.evaluate(),g.evaluate());
209     }
210
211     public static double integrate(DiscreteFunction f,Cosine g) {
212                 if(f instanceof SparseDiscreteFunction) {
213                         integrate((SparseDiscreteFunction)f,g);
214                 }
215         return ArrayMath.scalarProduct(f.evaluate(),g.evaluate());
216     }
217     public static double integrate (Cosine f,SparseDiscreteFunction g) {
218         return g.data.scalarProduct(new DoubleVector(f.evaluate()));
219     }
220
221     public static double integrate (Sine f,SparseDiscreteFunction g) {
222         return g.data.scalarProduct(new DoubleVector(f.evaluate()));
223     }
224
225     public static double integrate (SparseDiscreteFunction f,Sine g) {
226         return f.data.scalarProduct(new DoubleVector(g.evaluate()));
227         }
228
229     public static double integrate (SparseDiscreteFunction f,Cosine g) {
230         return f.data.scalarProduct(new DoubleVector(g.evaluate()));
231     }
232
233
234
235     /***********************************************
236     ************************************************/

237     public static double integrate (MultiscaleFunction f,MultiscaleFunction g, int j1) {
238         if(j1<0) {
239       throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
240         }
241         if (j1>maxiter) {
242             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
243         }
244         int[] Compatibilite=getScalingCoefficients(f,g,j1);
245         int K=Compatibilite[0];
246         int Ks=Compatibilite[1];
247         return(integrate(f,g,j1,K,Ks));
248     }
249
250
251     /***********************************************
252     ************************************************/

253     private static double integrate (MultiscaleFunction f,MultiscaleFunction g, int j1, int K, int Ks) {
254         if(j1<0) {
255       throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
256         }
257         if (j1>maxiter) {
258             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
259         }
260         double massbrute=ArrayMath.scalarProduct(f.evaluate(Ks+j1),g.evaluate(K+j1));
261         int normalisation=Cascades.PowerOf2(Math.min(Ks,K)+j1);
262         return(massbrute/normalisation);
263     }
264
265     /***********************************************
266     ***********************************************/

267     public static double integrate (PiecewiseConstant f,MultiscaleFunction g, int j1, double a, double b) {
268         if(j1<0) {
269 throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
270         }
271         if (j1>maxiter) {
272             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
273         }
274         LinearSpline ftest=new LinearSpline(new double[f.dimension(0)+1]);
275         int[] Compatibilite=getScalingCoefficients(ftest,g,j1);
276         int K=Compatibilite[0];
277         int Ks=Compatibilite[1];
278
279         double[] fdata=f.evaluate(K);
280         double[] gdata=g.evaluate(Ks+j1);
281
282         int sampling= (int) Math.round((gdata.length-1)/(double)fdata.length);
283         double massbrute=0.0;
284         for(int k=0;k<fdata.length;k++) {
285             double moyenne=0.0;
286             for(int l=sampling*k;l<=sampling*(k+1);l++) {
287                 moyenne+=gdata[l]/(sampling+1);
288             }
289             massbrute+=moyenne*fdata[k];
290         }
291         double mass=massbrute*Math.abs(b-a);//Normalisation
292
return(mass);
293     }
294     /***********************************************
295     ***********************************************/

296     public static double integrate (PiecewiseConstant f,MultiscaleFunction g, int j1) {
297         if(j1<0) {
298 throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
299         }
300         if (j1>maxiter) {
301             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
302         }
303         return(integrate(f,g,j1,0.0,1.0));
304     }
305
306     /**********************************************
307     ***********************************************/

308     public static double integrate (SumOfDiracs f,MultiscaleFunction g, int j1, double a, double b) {
309         if(j1<0) {
310 throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
311         }
312         if (j1>maxiter) {
313             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
314         }
315         return(integrate(f,g,j1));
316     }
317
318     /**********************************************
319     ***********************************************/

320     public static double integrate (SumOfDiracs f,MultiscaleFunction g, int j1) {
321         if(j1<0) {
322                         throw new IllegalArgumentException JavaDoc("The precision parameter must be positive : "+j1);
323         }
324         if (j1>maxiter) {
325             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
326         }
327         int[] Compatibilite=getScalingCoefficients(f,g,j1);
328         int K=Compatibilite[0];
329         int Ks=Compatibilite[1];
330         double[] fdata=f.evaluate(Ks+j1);
331         double[] gdata=g.evaluate (K+j1);
332         if(fdata.length==gdata.length) {
333             return(ArrayMath.scalarProduct(fdata,gdata));
334         } else {
335             throw new IllegalArgumentException JavaDoc("This type of integration is not handled by this class. Please use 0 as the precision parameter: "+j1+" != 0");
336         }
337     }
338
339
340     /*************************************************
341     *************************************************/

342     public static double integrate (SumOfDiracs f,double[] v) {
343         if(f.dimension()!=v.length) {
344             throw new IllegalArgumentException JavaDoc("Can only integrate if the number of knots is the same: "+f.dimension()+", "+v.length);
345         }
346         double somme=0.0;
347         for(int L=0;L<v.length;L++) {
348             somme+=f.getValue(L)*v[L];
349         }
350         return(somme);
351     }
352
353     /******************************************************
354     * While this method is public (for transparency reasons)
355     * it should not be called by other classes unless
356     * absolutely necessary.
357     *******************************************************/

358     public static int[] getScalingCoefficients(MultiscaleFunction f,MultiscaleFunction g, int j1) {
359         int[] sortie=new int[2];
360         for(sortie[0]=0;f.dimension(j1)>g.dimension(sortie[0]+j1);sortie[0]++) {
361             if((f.dimension(j1)<g.dimension(sortie[0]+j1))||(sortie[0]>maxiter)) {
362                 throw new IllegalArgumentException JavaDoc("The objects are incompatible and cannot be integrated according to this class (1) : "+f.dimension(j1)+", "+g.dimension(j1)+" : "+sortie[0]+"\nYou might have to write you own method.");
363             }
364         }
365         for(sortie[1]=0;f.dimension(sortie[1]+j1)<g.dimension(j1+sortie[0]);sortie[1]++) {
366             if((f.dimension(j1+sortie[1])>g.dimension(sortie[0]+j1))||(sortie[1]>maxiter)) {
367                 throw new IllegalArgumentException JavaDoc("The objects are incompatible and cannot be integrated according to this class (2) : "+f.dimension(j1)+", "+g.dimension(j1)+" : "+sortie[1]+"\nYou might have to write you own method.");
368             }
369         }
370         if(f.dimension(sortie[1]+j1)!=g.dimension(sortie[0]+j1)) {
371             throw new IllegalArgumentException JavaDoc("The objects are incompatible and cannot be integrated according to this class (?) :"+f.dimension(j1)+", "+g.dimension(j1)+" : "+sortie[0]+" "+sortie[1]+"\nYou might have to write you own method.");
372         }
373         return(sortie);
374     }
375 }
376
377
Popular Tags